fix: free Epub RAM and simplify KOSync navigation via ActivityManager (#1860)

## Summary

* **What is the goal of this PR?** Fix KOSync failing with "Network
error" on large/complex EPUBs, and simplify the sync navigation flow by
removing the callback/result pattern.

* **What changes are included?**

This PR combines the approaches from #1855 and #1760 into a single,
cleaner solution:

  **Memory fix (from #1855):**
- `EpubReaderActivity` pre-computes the local KOReader position and
chapter name, then explicitly releases `epub` and `section` before
launching `KOReaderSyncActivity`. This frees ~65KB measured on device,
giving the TLS handshake sufficient heap. The root cause was
`MBEDTLS_ERR_X509_ALLOC_FAILED` (-0x2880) when a 3-cert chain consumed
~48KB during the handshake with only ~50KB available.
- `KOReaderSyncActivity` no longer receives a `shared_ptr<Epub>` at
construction — it lazy-loads the Epub after TLS only if remote progress
is found (`ensureEpubLoaded()`).
- Added `MIN_HEAP_FOR_TLS = 55000` guard in `KOReaderSyncClient` —
returns `LOW_MEMORY` early if aggregate free heap is too low before
attempting a TLS connection.

  **Navigation simplification (from #1760):**
- Replaced `startActivityForResult` + callback with
`activityManager.replaceActivity` / `activityManager.goToReader`.
Progress is saved to `progress.bin` before the epub is released
(cancel/upload paths) and in `saveProgressAndReturn` (apply remote
path). The reader re-launches from the saved position naturally via
`goToReader`, eliminating the need to reload the epub in a callback.
- Extracted `ReaderUtils::saveProgress()` as a shared helper used by
both `EpubReaderActivity` and `KOReaderSyncActivity`.
- Added `STR_SAVE_PROGRESS_FAILED` to all 22 language files for the case
where writing the synced position to SD fails.

  **Orientation fix (found during device testing):**
- `EpubReaderActivity::onExit()` resets the renderer to portrait before
destruction. With `replaceActivity` the reader is fully torn down before
KOSync starts, so KOSync was always rendering in portrait even when
reading in landscape. Fixed by calling `ReaderUtils::applyOrientation`
in `KOReaderSyncActivity::onEnter()`.

## Additional Context

Heap measurements on device (large EPUB with complex CSS):

| Metric | Before | After |
|---|---|---|
| Heap before Epub release | 88,156 bytes | — |
| Heap after Epub release | — | 153,892 bytes (+65,736) |
| Heap at TLS handshake | ~50,000 bytes (fails) | ~116,384 bytes
(passes) |
| Min-free-ever during sync session | 2,600 bytes | 33,052 bytes |
| TLS result | `MBEDTLS_ERR_X509_ALLOC_FAILED` | HTTP 200 |

Tested on device: sync from inside a large EPUB in both portrait and
landscape, cancel, apply remote progress, upload local progress.

---

### AI Usage

Did you use AI tools to help write this code? _**YES**_

---------

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Wylan Swets
2026-05-08 22:01:31 -05:00
committed by GitHub
co-authored by Claude Sonnet 4.6
parent 7993b2bb97
commit e64155ed63
29 changed files with 221 additions and 91 deletions
+11 -7
View File
@@ -21,20 +21,20 @@
*/
class KOReaderSyncActivity final : public Activity {
public:
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
const std::shared_ptr<Epub>& epub, const std::string& epubPath, int currentSpineIndex,
int currentPage, int totalPagesInSpine,
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& epubPath,
int currentSpineIndex, int currentPage, int totalPagesInSpine,
KOReaderPosition localKoPos, std::string localChapterName,
std::optional<uint16_t> currentParagraphIndex = std::nullopt)
: Activity("KOReaderSync", renderer, mappedInput),
epub(epub),
epubPath(epubPath),
currentSpineIndex(currentSpineIndex),
currentPage(currentPage),
totalPagesInSpine(totalPagesInSpine),
currentParagraphIndex(currentParagraphIndex),
localChapterName(std::move(localChapterName)),
remoteProgress{},
remotePosition{},
localProgress{} {}
localProgress(std::move(localKoPos)) {}
void onEnter() override;
void onExit() override;
@@ -55,8 +55,9 @@ class KOReaderSyncActivity final : public Activity {
NO_CREDENTIALS
};
std::shared_ptr<Epub> epub;
std::shared_ptr<Epub> epub; // null until lazy-loaded after TLS in performSync()
std::string epubPath;
std::string localChapterName;
int currentSpineIndex;
int currentPage;
int totalPagesInSpine;
@@ -71,7 +72,7 @@ class KOReaderSyncActivity final : public Activity {
KOReaderProgress remoteProgress;
CrossPointPosition remotePosition;
// Local progress as KOReader format (for display)
// Local progress as KOReader format (pre-computed before Epub was released)
KOReaderPosition localProgress;
// Selection in result screen (0=Apply, 1=Upload)
@@ -80,4 +81,7 @@ class KOReaderSyncActivity final : public Activity {
void onWifiSelectionComplete(bool success);
void performSync();
void performUpload();
void ensureEpubLoaded();
void saveProgressAndReturn(int spineIndex, int page);
void returnToReader();
};