refactor: implement ActivityManager (#1016)

## Summary

Ref comment:
https://github.com/crosspoint-reader/crosspoint-reader/pull/1010#pullrequestreview-3828854640

This PR introduces `ActivityManager`, which mirrors the same concept of
Activity in Android, where an activity represents a single screen of the
UI. The manager is responsible for launching activities, and ensuring
that only one activity is active at a time.

Main differences from Android's ActivityManager:
- No concept of Bundle or Intent extras
- No onPause/onResume, since we don't have a concept of background
activities
- onActivityResult is implemented via a callback instead of a separate
method, for simplicity

## Key changes

- Single `renderTask` shared across all activities
- No more sub-activity, we manage them using a stack; Results can be
passed via `startActivityForResult` and `setResult`
- Activity can call `finish()` to destroy themself, but the actual
deletion will be handled by `ActivityManager` to avoid `delete this`
pattern

As a bonus: the manager will automatically call `requestUpdate()` when
returning from another activity

## Example usage

**BEFORE**:

```cpp
// caller
    enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
                                               [this](const bool connected) { onWifiSelectionComplete(connected); }));

// subactivity
  onComplete(true); // will eventually call exitActivity(), which deletes the caller instance (dangerous behavior)
``` 

**AFTER**: (mirrors the `startActivityForResult` and `setResult` from
android)

```cpp
// caller
  startActivityForResult(new NetworkModeSelectionActivity(renderer, mappedInput),
                         [this](const ActivityResult& result) { onNetworkModeSelected(result.selectedNetworkMode); });

// subactivity
  ActivityResult result;
  result.isCancelled = false;
  result.selectedNetworkMode = mode;
  setResult(result);
  finish(); // signals to ActivityManager to go back to last activity AFTER this function returns
```

TODO:
- [x] Reconsider if the `Intent` is really necessary or it should be
removed (note: it's inspired by
[Intent](https://developer.android.com/guide/components/intents-common)
from Android API) ==> I decided to keep this pattern fr clarity
- [x] Verify if behavior is still correct (i.e. back from sub-activity)
- [x] Refactor the `ActivityWithSubactivity` to just simple `Activity`
--> We are using a stack for keeping track of sub-activity now
- [x] Use single task for rendering --> avoid allocating 8KB stack per
activity
- [x] Implement the idea of [Activity
result](https://developer.android.com/training/basics/intents/result)
--> Allow sub-activity like Wifi to report back the status (connected,
failed, etc)

---

### AI Usage

While CrossPoint doesn't have restrictions on AI tools in contributing,
please be transparent about their usage as it
helps set the right context for reviewers.

Did you use AI tools to help write this code? **PARTIALLY**, some
repetitive migrations are done by Claude, but I'm the one how ultimately
approve it

---------

Co-authored-by: Zach Nelson <zach@zdnelson.com>
This commit is contained in:
Xuan-Son Nguyen
2026-02-27 07:32:40 +01:00
committed by GitHub
parent 5b11e45a36
commit c4fc4effbd
69 changed files with 1095 additions and 1180 deletions

View File

@@ -51,11 +51,12 @@ void wifiOff() {
} // namespace
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
exitActivity();
if (!success) {
LOG_DBG("KOSync", "WiFi connection failed, exiting");
onCancel();
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
return;
}
@@ -66,7 +67,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
state = SYNCING;
statusMessage = tr(STR_SYNCING_TIME);
}
requestUpdate();
requestUpdate(true);
// Sync time with NTP before making API requests
syncTimeWithNTP();
@@ -75,7 +76,7 @@ void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
RenderLock lock(*this);
statusMessage = tr(STR_CALC_HASH);
}
requestUpdate();
requestUpdate(true);
performSync();
}
@@ -93,7 +94,7 @@ void KOReaderSyncActivity::performSync() {
state = SYNC_FAILED;
statusMessage = tr(STR_HASH_FAILED);
}
requestUpdate();
requestUpdate(true);
return;
}
@@ -115,7 +116,7 @@ void KOReaderSyncActivity::performSync() {
state = NO_REMOTE_PROGRESS;
hasRemoteProgress = false;
}
requestUpdate();
requestUpdate(true);
return;
}
@@ -125,7 +126,7 @@ void KOReaderSyncActivity::performSync() {
state = SYNC_FAILED;
statusMessage = KOReaderSyncClient::errorString(result);
}
requestUpdate();
requestUpdate(true);
return;
}
@@ -149,7 +150,7 @@ void KOReaderSyncActivity::performSync() {
selectedOption = 0; // Apply remote progress
}
}
requestUpdate();
requestUpdate(true);
}
void KOReaderSyncActivity::performUpload() {
@@ -158,7 +159,6 @@ void KOReaderSyncActivity::performUpload() {
state = UPLOADING;
statusMessage = tr(STR_UPLOAD_PROGRESS);
}
requestUpdate();
requestUpdateAndWait();
// Convert current position to KOReader format
@@ -188,11 +188,11 @@ void KOReaderSyncActivity::performUpload() {
RenderLock lock(*this);
state = UPLOAD_COMPLETE;
}
requestUpdate();
requestUpdate(true);
}
void KOReaderSyncActivity::onEnter() {
ActivityWithSubactivity::onEnter();
Activity::onEnter();
// Check for credentials first
if (!KOREADER_STORE.hasCredentials()) {
@@ -206,7 +206,7 @@ void KOReaderSyncActivity::onEnter() {
LOG_DBG("KOSync", "Already connected to WiFi");
state = SYNCING;
statusMessage = tr(STR_SYNCING_TIME);
requestUpdate();
requestUpdate(true);
// Perform sync directly (will be handled in loop)
xTaskCreate(
@@ -218,7 +218,7 @@ void KOReaderSyncActivity::onEnter() {
RenderLock lock(*self);
self->statusMessage = tr(STR_CALC_HASH);
}
self->requestUpdate();
self->requestUpdate(true);
self->performSync();
vTaskDelete(nullptr);
},
@@ -228,21 +228,17 @@ void KOReaderSyncActivity::onEnter() {
// Launch WiFi selection subactivity
LOG_DBG("KOSync", "Launching WifiSelectionActivity...");
enterNewActivity(new WifiSelectionActivity(renderer, mappedInput,
[this](const bool connected) { onWifiSelectionComplete(connected); }));
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
}
void KOReaderSyncActivity::onExit() {
ActivityWithSubactivity::onExit();
Activity::onExit();
wifiOff();
}
void KOReaderSyncActivity::render(Activity::RenderLock&&) {
if (subActivity) {
return;
}
void KOReaderSyncActivity::render(RenderLock&&) {
const auto pageWidth = renderer.getScreenWidth();
renderer.clearScreen();
@@ -357,49 +353,50 @@ void KOReaderSyncActivity::render(Activity::RenderLock&&) {
}
void KOReaderSyncActivity::loop() {
if (subActivity) {
subActivity->loop();
return;
}
if (state == NO_CREDENTIALS || state == SYNC_FAILED || state == UPLOAD_COMPLETE) {
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
onCancel();
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
return;
}
if (state == SHOWING_RESULT) {
// Navigate options
if (mappedInput.wasPressed(MappedInputManager::Button::Up) ||
mappedInput.wasPressed(MappedInputManager::Button::Left)) {
if (mappedInput.wasReleased(MappedInputManager::Button::Up) ||
mappedInput.wasReleased(MappedInputManager::Button::Left)) {
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
requestUpdate();
} else if (mappedInput.wasPressed(MappedInputManager::Button::Down) ||
mappedInput.wasPressed(MappedInputManager::Button::Right)) {
} else if (mappedInput.wasReleased(MappedInputManager::Button::Down) ||
mappedInput.wasReleased(MappedInputManager::Button::Right)) {
selectedOption = (selectedOption + 1) % 2; // Wrap around among 2 options
requestUpdate();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
if (selectedOption == 0) {
// Apply remote progress — WiFi no longer needed
wifiOff();
onSyncComplete(remotePosition.spineIndex, remotePosition.pageNumber);
// Wifi will be turned off in onExit()
setResult(SyncResult{remotePosition.spineIndex, remotePosition.pageNumber});
finish();
} else if (selectedOption == 1) {
// Upload local progress
performUpload();
}
}
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
onCancel();
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
return;
}
if (state == NO_REMOTE_PROGRESS) {
if (mappedInput.wasPressed(MappedInputManager::Button::Confirm)) {
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
// Calculate hash if not done yet
if (documentHash.empty()) {
if (KOREADER_STORE.getMatchMethod() == DocumentMatchMethod::FILENAME) {
@@ -411,8 +408,11 @@ void KOReaderSyncActivity::loop() {
performUpload();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Back)) {
onCancel();
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
ActivityResult result;
result.isCancelled = true;
setResult(std::move(result));
finish();
}
return;
}