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:
co-authored by
Claude Sonnet 4.6
parent
7993b2bb97
commit
e64155ed63
@@ -254,6 +254,7 @@ STR_NO_REMOTE_MSG: "Аддалены прагрэс не знойдзены"
|
|||||||
STR_UPLOAD_PROMPT: "Адправіць бягучую пазіцыю?"
|
STR_UPLOAD_PROMPT: "Адправіць бягучую пазіцыю?"
|
||||||
STR_UPLOAD_SUCCESS: "Прагрэс адпраўлены!"
|
STR_UPLOAD_SUCCESS: "Прагрэс адпраўлены!"
|
||||||
STR_SYNC_FAILED_MSG: "Памылка сінхранізацыі"
|
STR_SYNC_FAILED_MSG: "Памылка сінхранізацыі"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Не ўдалося захаваць прагрэс"
|
||||||
STR_SECTION_PREFIX: "Раздзел"
|
STR_SECTION_PREFIX: "Раздзел"
|
||||||
STR_UPLOAD: "Адправіць"
|
STR_UPLOAD: "Адправіць"
|
||||||
STR_BOOK_S_STYLE: "Стыль кнігі"
|
STR_BOOK_S_STYLE: "Стыль кнігі"
|
||||||
|
|||||||
@@ -282,6 +282,7 @@ STR_NO_REMOTE_MSG: "No s'ha trobat progrés remot"
|
|||||||
STR_UPLOAD_PROMPT: "Voleu pujar la posició actual?"
|
STR_UPLOAD_PROMPT: "Voleu pujar la posició actual?"
|
||||||
STR_UPLOAD_SUCCESS: "Progrés pujat!"
|
STR_UPLOAD_SUCCESS: "Progrés pujat!"
|
||||||
STR_SYNC_FAILED_MSG: "Sincronització fallida"
|
STR_SYNC_FAILED_MSG: "Sincronització fallida"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "No s'ha pogut desar el progrés"
|
||||||
STR_SECTION_PREFIX: "Secció "
|
STR_SECTION_PREFIX: "Secció "
|
||||||
STR_UPLOAD: "Puja"
|
STR_UPLOAD: "Puja"
|
||||||
STR_BOOK_S_STYLE: "Estil del llibre"
|
STR_BOOK_S_STYLE: "Estil del llibre"
|
||||||
|
|||||||
@@ -259,6 +259,7 @@ STR_NO_REMOTE_MSG: "Nenalezen žádný vzdálený postup"
|
|||||||
STR_UPLOAD_PROMPT: "Nahrát aktuální pozici?"
|
STR_UPLOAD_PROMPT: "Nahrát aktuální pozici?"
|
||||||
STR_UPLOAD_SUCCESS: "Postup nahrán!"
|
STR_UPLOAD_SUCCESS: "Postup nahrán!"
|
||||||
STR_SYNC_FAILED_MSG: "Synchronizace se nezdařila"
|
STR_SYNC_FAILED_MSG: "Synchronizace se nezdařila"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Nepodařilo se uložit pokrok"
|
||||||
STR_SECTION_PREFIX: "Sekce"
|
STR_SECTION_PREFIX: "Sekce"
|
||||||
STR_UPLOAD: "Nahrát"
|
STR_UPLOAD: "Nahrát"
|
||||||
STR_BOOK_S_STYLE: "Styl knihy"
|
STR_BOOK_S_STYLE: "Styl knihy"
|
||||||
|
|||||||
@@ -282,6 +282,7 @@ STR_NO_REMOTE_MSG: "Ingen fjernfremskridt fundet"
|
|||||||
STR_UPLOAD_PROMPT: "Upload nuværende position?"
|
STR_UPLOAD_PROMPT: "Upload nuværende position?"
|
||||||
STR_UPLOAD_SUCCESS: "Fremskridt uploadet!"
|
STR_UPLOAD_SUCCESS: "Fremskridt uploadet!"
|
||||||
STR_SYNC_FAILED_MSG: "Synkronisering mislykkedes"
|
STR_SYNC_FAILED_MSG: "Synkronisering mislykkedes"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Kunne ikke gemme fremskridt"
|
||||||
STR_SECTION_PREFIX: "Afsnit "
|
STR_SECTION_PREFIX: "Afsnit "
|
||||||
STR_UPLOAD: "Upload"
|
STR_UPLOAD: "Upload"
|
||||||
STR_BOOK_S_STYLE: "Bogens stil"
|
STR_BOOK_S_STYLE: "Bogens stil"
|
||||||
|
|||||||
@@ -282,6 +282,7 @@ STR_NO_REMOTE_MSG: "Geen externe voortgang gevonden"
|
|||||||
STR_UPLOAD_PROMPT: "Huidige positie uploaden?"
|
STR_UPLOAD_PROMPT: "Huidige positie uploaden?"
|
||||||
STR_UPLOAD_SUCCESS: "Voortgang geüpload!"
|
STR_UPLOAD_SUCCESS: "Voortgang geüpload!"
|
||||||
STR_SYNC_FAILED_MSG: "Sync mislukt"
|
STR_SYNC_FAILED_MSG: "Sync mislukt"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Voortgang opslaan mislukt"
|
||||||
STR_SECTION_PREFIX: "Sectie "
|
STR_SECTION_PREFIX: "Sectie "
|
||||||
STR_UPLOAD: "Uploaden"
|
STR_UPLOAD: "Uploaden"
|
||||||
STR_BOOK_S_STYLE: "Stijl van boek"
|
STR_BOOK_S_STYLE: "Stijl van boek"
|
||||||
|
|||||||
@@ -291,6 +291,7 @@ STR_NO_REMOTE_MSG: "No remote progress found"
|
|||||||
STR_UPLOAD_PROMPT: "Upload current position?"
|
STR_UPLOAD_PROMPT: "Upload current position?"
|
||||||
STR_UPLOAD_SUCCESS: "Progress uploaded!"
|
STR_UPLOAD_SUCCESS: "Progress uploaded!"
|
||||||
STR_SYNC_FAILED_MSG: "Sync failed"
|
STR_SYNC_FAILED_MSG: "Sync failed"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Could not save progress"
|
||||||
STR_SECTION_PREFIX: "Section "
|
STR_SECTION_PREFIX: "Section "
|
||||||
STR_UPLOAD: "Upload"
|
STR_UPLOAD: "Upload"
|
||||||
STR_BOOK_S_STYLE: "Book's Style"
|
STR_BOOK_S_STYLE: "Book's Style"
|
||||||
|
|||||||
@@ -257,6 +257,7 @@ STR_NO_REMOTE_MSG: "Etäedistymistä ei löytynyt"
|
|||||||
STR_UPLOAD_PROMPT: "Lähetä nykyinen sijainti?"
|
STR_UPLOAD_PROMPT: "Lähetä nykyinen sijainti?"
|
||||||
STR_UPLOAD_SUCCESS: "Edistyminen lähetetty!"
|
STR_UPLOAD_SUCCESS: "Edistyminen lähetetty!"
|
||||||
STR_SYNC_FAILED_MSG: "Synkronointi epäonnistui"
|
STR_SYNC_FAILED_MSG: "Synkronointi epäonnistui"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Edistymisen tallennus epäonnistui"
|
||||||
STR_SECTION_PREFIX: "Osio "
|
STR_SECTION_PREFIX: "Osio "
|
||||||
STR_UPLOAD: "Lähetä"
|
STR_UPLOAD: "Lähetä"
|
||||||
STR_BOOK_S_STYLE: "Kirjan tyyli"
|
STR_BOOK_S_STYLE: "Kirjan tyyli"
|
||||||
|
|||||||
@@ -283,6 +283,7 @@ STR_NO_REMOTE_MSG: "Aucune progression en ligne"
|
|||||||
STR_UPLOAD_PROMPT: "Envoyer position actuelle ?"
|
STR_UPLOAD_PROMPT: "Envoyer position actuelle ?"
|
||||||
STR_UPLOAD_SUCCESS: "Progression envoyée !"
|
STR_UPLOAD_SUCCESS: "Progression envoyée !"
|
||||||
STR_SYNC_FAILED_MSG: "Échec de la synchro"
|
STR_SYNC_FAILED_MSG: "Échec de la synchro"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Échec de l'enregistrement"
|
||||||
STR_SECTION_PREFIX: "Section "
|
STR_SECTION_PREFIX: "Section "
|
||||||
STR_UPLOAD: "Envoyer"
|
STR_UPLOAD: "Envoyer"
|
||||||
STR_BOOK_S_STYLE: "Style du livre"
|
STR_BOOK_S_STYLE: "Style du livre"
|
||||||
|
|||||||
@@ -284,6 +284,7 @@ STR_NO_REMOTE_MSG: "Kein externer Fortschritt"
|
|||||||
STR_UPLOAD_PROMPT: "Aktuelle Position hochladen?"
|
STR_UPLOAD_PROMPT: "Aktuelle Position hochladen?"
|
||||||
STR_UPLOAD_SUCCESS: "Hochgeladen!"
|
STR_UPLOAD_SUCCESS: "Hochgeladen!"
|
||||||
STR_SYNC_FAILED_MSG: "Fehlgeschlagen"
|
STR_SYNC_FAILED_MSG: "Fehlgeschlagen"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Fortschritt konnte nicht gespeichert werden"
|
||||||
STR_SECTION_PREFIX: "Abschnitt"
|
STR_SECTION_PREFIX: "Abschnitt"
|
||||||
STR_UPLOAD: "Hochladen"
|
STR_UPLOAD: "Hochladen"
|
||||||
STR_BOOK_S_STYLE: "Buch-Stil"
|
STR_BOOK_S_STYLE: "Buch-Stil"
|
||||||
|
|||||||
@@ -279,6 +279,7 @@ STR_NO_REMOTE_MSG: "Nem található távoli haladás"
|
|||||||
STR_UPLOAD_PROMPT: "Jelenlegi pozíció feltöltése?"
|
STR_UPLOAD_PROMPT: "Jelenlegi pozíció feltöltése?"
|
||||||
STR_UPLOAD_SUCCESS: "Haladás feltöltve!"
|
STR_UPLOAD_SUCCESS: "Haladás feltöltve!"
|
||||||
STR_SYNC_FAILED_MSG: "Szinkronizálás sikertelen"
|
STR_SYNC_FAILED_MSG: "Szinkronizálás sikertelen"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "A haladás mentése sikertelen"
|
||||||
STR_SECTION_PREFIX: "Szakasz "
|
STR_SECTION_PREFIX: "Szakasz "
|
||||||
STR_UPLOAD: "Feltöltés"
|
STR_UPLOAD: "Feltöltés"
|
||||||
STR_BOOK_S_STYLE: "Könyv stílusa"
|
STR_BOOK_S_STYLE: "Könyv stílusa"
|
||||||
|
|||||||
@@ -283,6 +283,7 @@ STR_NO_REMOTE_MSG: "Nessun avanzamento remoto trovato"
|
|||||||
STR_UPLOAD_PROMPT: "Inviare la posizione attuale?"
|
STR_UPLOAD_PROMPT: "Inviare la posizione attuale?"
|
||||||
STR_UPLOAD_SUCCESS: "Avanzamenti inviati!"
|
STR_UPLOAD_SUCCESS: "Avanzamenti inviati!"
|
||||||
STR_SYNC_FAILED_MSG: "Sincronizzazione non riuscita"
|
STR_SYNC_FAILED_MSG: "Sincronizzazione non riuscita"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Impossibile salvare il progresso"
|
||||||
STR_SECTION_PREFIX: "Sezione "
|
STR_SECTION_PREFIX: "Sezione "
|
||||||
STR_UPLOAD: "Carica"
|
STR_UPLOAD: "Carica"
|
||||||
STR_BOOK_S_STYLE: "Stile libro"
|
STR_BOOK_S_STYLE: "Stile libro"
|
||||||
|
|||||||
@@ -253,6 +253,7 @@ STR_NO_REMOTE_MSG: "Қашықтағы үлгерім табылмады"
|
|||||||
STR_UPLOAD_PROMPT: "Ағымдағы орынды жүктеп салу керек пе?"
|
STR_UPLOAD_PROMPT: "Ағымдағы орынды жүктеп салу керек пе?"
|
||||||
STR_UPLOAD_SUCCESS: "Үлгерім жүктеп салынды!"
|
STR_UPLOAD_SUCCESS: "Үлгерім жүктеп салынды!"
|
||||||
STR_SYNC_FAILED_MSG: "Синхрондау сәтсіз"
|
STR_SYNC_FAILED_MSG: "Синхрондау сәтсіз"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Прогресті сақтау мүмкін болмады"
|
||||||
STR_SECTION_PREFIX: "Бөлім "
|
STR_SECTION_PREFIX: "Бөлім "
|
||||||
STR_UPLOAD: "Жүктеп салу"
|
STR_UPLOAD: "Жүктеп салу"
|
||||||
STR_BOOK_S_STYLE: "Кітап стилі"
|
STR_BOOK_S_STYLE: "Кітап стилі"
|
||||||
|
|||||||
@@ -279,6 +279,7 @@ STR_NO_REMOTE_MSG: "Progresų nerasta"
|
|||||||
STR_UPLOAD_PROMPT: "Įkelti poziciją?"
|
STR_UPLOAD_PROMPT: "Įkelti poziciją?"
|
||||||
STR_UPLOAD_SUCCESS: "Įkelta!"
|
STR_UPLOAD_SUCCESS: "Įkelta!"
|
||||||
STR_SYNC_FAILED_MSG: "Nepavyko"
|
STR_SYNC_FAILED_MSG: "Nepavyko"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Nepavyko išsaugoti progreso"
|
||||||
STR_SECTION_PREFIX: "Dalis "
|
STR_SECTION_PREFIX: "Dalis "
|
||||||
STR_UPLOAD: "Įkelti"
|
STR_UPLOAD: "Įkelti"
|
||||||
STR_BOOK_S_STYLE: "Knygos stilius"
|
STR_BOOK_S_STYLE: "Knygos stilius"
|
||||||
|
|||||||
@@ -282,6 +282,7 @@ STR_NO_REMOTE_MSG: "Nie znaleziono zdalnego postępu"
|
|||||||
STR_UPLOAD_PROMPT: "Wysłać obecną pozycję?"
|
STR_UPLOAD_PROMPT: "Wysłać obecną pozycję?"
|
||||||
STR_UPLOAD_SUCCESS: "Postęp wysłany!"
|
STR_UPLOAD_SUCCESS: "Postęp wysłany!"
|
||||||
STR_SYNC_FAILED_MSG: "Synchronizacja nieudana"
|
STR_SYNC_FAILED_MSG: "Synchronizacja nieudana"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Nie udało się zapisać postępu"
|
||||||
STR_SECTION_PREFIX: "Sekcja "
|
STR_SECTION_PREFIX: "Sekcja "
|
||||||
STR_UPLOAD: "Wyślij"
|
STR_UPLOAD: "Wyślij"
|
||||||
STR_BOOK_S_STYLE: "Styl książki"
|
STR_BOOK_S_STYLE: "Styl książki"
|
||||||
|
|||||||
@@ -259,6 +259,7 @@ STR_NO_REMOTE_MSG: "Nenhum progresso remoto encontrado"
|
|||||||
STR_UPLOAD_PROMPT: "Enviar posição atual?"
|
STR_UPLOAD_PROMPT: "Enviar posição atual?"
|
||||||
STR_UPLOAD_SUCCESS: "Progresso enviado!"
|
STR_UPLOAD_SUCCESS: "Progresso enviado!"
|
||||||
STR_SYNC_FAILED_MSG: "Falha na sincronização"
|
STR_SYNC_FAILED_MSG: "Falha na sincronização"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Não foi possível salvar o progresso"
|
||||||
STR_SECTION_PREFIX: "Seção"
|
STR_SECTION_PREFIX: "Seção"
|
||||||
STR_UPLOAD: "Enviar"
|
STR_UPLOAD: "Enviar"
|
||||||
STR_BOOK_S_STYLE: "Estilo do livro"
|
STR_BOOK_S_STYLE: "Estilo do livro"
|
||||||
|
|||||||
@@ -282,6 +282,7 @@ STR_NO_REMOTE_MSG: "Niciun progres remote găsit"
|
|||||||
STR_UPLOAD_PROMPT: "Încărcaţi poziţia curentă?"
|
STR_UPLOAD_PROMPT: "Încărcaţi poziţia curentă?"
|
||||||
STR_UPLOAD_SUCCESS: "Progres încărcat!"
|
STR_UPLOAD_SUCCESS: "Progres încărcat!"
|
||||||
STR_SYNC_FAILED_MSG: "Sincronizare eşuată"
|
STR_SYNC_FAILED_MSG: "Sincronizare eşuată"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Nu s-a putut salva progresul"
|
||||||
STR_SECTION_PREFIX: "Secţiune "
|
STR_SECTION_PREFIX: "Secţiune "
|
||||||
STR_UPLOAD: "Încărcare"
|
STR_UPLOAD: "Încărcare"
|
||||||
STR_BOOK_S_STYLE: "Stilul cărţii"
|
STR_BOOK_S_STYLE: "Stilul cărţii"
|
||||||
|
|||||||
@@ -286,6 +286,7 @@ STR_NO_REMOTE_MSG: "Удалённый прогресс не найден"
|
|||||||
STR_UPLOAD_PROMPT: "Отправить текущую позицию?"
|
STR_UPLOAD_PROMPT: "Отправить текущую позицию?"
|
||||||
STR_UPLOAD_SUCCESS: "Прогресс отправлен!"
|
STR_UPLOAD_SUCCESS: "Прогресс отправлен!"
|
||||||
STR_SYNC_FAILED_MSG: "Ошибка синхронизации"
|
STR_SYNC_FAILED_MSG: "Ошибка синхронизации"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Не удалось сохранить прогресс"
|
||||||
STR_SECTION_PREFIX: "Раздел "
|
STR_SECTION_PREFIX: "Раздел "
|
||||||
STR_UPLOAD: "Отправить"
|
STR_UPLOAD: "Отправить"
|
||||||
STR_BOOK_S_STYLE: "Стиль книги"
|
STR_BOOK_S_STYLE: "Стиль книги"
|
||||||
|
|||||||
@@ -279,6 +279,7 @@ STR_NO_REMOTE_MSG: "Oddaljen napredek ni bil najden"
|
|||||||
STR_UPLOAD_PROMPT: "Naložim trenutno pozicijo?"
|
STR_UPLOAD_PROMPT: "Naložim trenutno pozicijo?"
|
||||||
STR_UPLOAD_SUCCESS: "Napredek naložen!"
|
STR_UPLOAD_SUCCESS: "Napredek naložen!"
|
||||||
STR_SYNC_FAILED_MSG: "Sinhronizacija ni uspela"
|
STR_SYNC_FAILED_MSG: "Sinhronizacija ni uspela"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Napredka ni bilo mogoče shraniti"
|
||||||
STR_SECTION_PREFIX: "Razdelek "
|
STR_SECTION_PREFIX: "Razdelek "
|
||||||
STR_UPLOAD: "Naloži"
|
STR_UPLOAD: "Naloži"
|
||||||
STR_BOOK_S_STYLE: "Slog knjige"
|
STR_BOOK_S_STYLE: "Slog knjige"
|
||||||
|
|||||||
@@ -283,6 +283,7 @@ STR_NO_REMOTE_MSG: "No se encontró progreso remoto"
|
|||||||
STR_UPLOAD_PROMPT: "¿Subir posición actual?"
|
STR_UPLOAD_PROMPT: "¿Subir posición actual?"
|
||||||
STR_UPLOAD_SUCCESS: "¡Progreso subido!"
|
STR_UPLOAD_SUCCESS: "¡Progreso subido!"
|
||||||
STR_SYNC_FAILED_MSG: "Fallo de sincronización"
|
STR_SYNC_FAILED_MSG: "Fallo de sincronización"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "No se pudo guardar el progreso"
|
||||||
STR_SECTION_PREFIX: "Secc.:"
|
STR_SECTION_PREFIX: "Secc.:"
|
||||||
STR_UPLOAD: "Subir"
|
STR_UPLOAD: "Subir"
|
||||||
STR_BOOK_S_STYLE: "Estilo del libro"
|
STR_BOOK_S_STYLE: "Estilo del libro"
|
||||||
|
|||||||
@@ -288,6 +288,7 @@ STR_NO_REMOTE_MSG: "Inga fjärrframsteg funna"
|
|||||||
STR_UPLOAD_PROMPT: "Ladda upp nuvarande position?"
|
STR_UPLOAD_PROMPT: "Ladda upp nuvarande position?"
|
||||||
STR_UPLOAD_SUCCESS: "Framsteg uppladdade!"
|
STR_UPLOAD_SUCCESS: "Framsteg uppladdade!"
|
||||||
STR_SYNC_FAILED_MSG: "Synkronisering misslyckades"
|
STR_SYNC_FAILED_MSG: "Synkronisering misslyckades"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Kunde inte spara framsteg"
|
||||||
STR_SECTION_PREFIX: "Sektion"
|
STR_SECTION_PREFIX: "Sektion"
|
||||||
STR_UPLOAD: "Uppladdning"
|
STR_UPLOAD: "Uppladdning"
|
||||||
STR_BOOK_S_STYLE: "Bokstil"
|
STR_BOOK_S_STYLE: "Bokstil"
|
||||||
|
|||||||
@@ -257,6 +257,7 @@ STR_NO_REMOTE_MSG: "Uzak ilerleme bulunamadı"
|
|||||||
STR_UPLOAD_PROMPT: "Mevcut konumu yükle?"
|
STR_UPLOAD_PROMPT: "Mevcut konumu yükle?"
|
||||||
STR_UPLOAD_SUCCESS: "İlerleme yüklendi!"
|
STR_UPLOAD_SUCCESS: "İlerleme yüklendi!"
|
||||||
STR_SYNC_FAILED_MSG: "Senkronizasyon başarısız"
|
STR_SYNC_FAILED_MSG: "Senkronizasyon başarısız"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "İlerleme kaydedilemedi"
|
||||||
STR_SECTION_PREFIX: "Bölüm "
|
STR_SECTION_PREFIX: "Bölüm "
|
||||||
STR_UPLOAD: "Yükle"
|
STR_UPLOAD: "Yükle"
|
||||||
STR_BOOK_S_STYLE: "Kitabın Stili"
|
STR_BOOK_S_STYLE: "Kitabın Stili"
|
||||||
|
|||||||
@@ -287,6 +287,7 @@ STR_NO_REMOTE_MSG: "Віддаленого прогресу не знайден
|
|||||||
STR_UPLOAD_PROMPT: "Завантажити поточну позицію?"
|
STR_UPLOAD_PROMPT: "Завантажити поточну позицію?"
|
||||||
STR_UPLOAD_SUCCESS: "Прогрес завантажено!"
|
STR_UPLOAD_SUCCESS: "Прогрес завантажено!"
|
||||||
STR_SYNC_FAILED_MSG: "Синхронізація не вдалася"
|
STR_SYNC_FAILED_MSG: "Синхронізація не вдалася"
|
||||||
|
STR_SAVE_PROGRESS_FAILED: "Не вдалося зберегти прогрес"
|
||||||
STR_SECTION_PREFIX: "Розділ "
|
STR_SECTION_PREFIX: "Розділ "
|
||||||
STR_UPLOAD: "Завантажити"
|
STR_UPLOAD: "Завантажити"
|
||||||
STR_BOOK_S_STYLE: "Стиль книги"
|
STR_BOOK_S_STYLE: "Стиль книги"
|
||||||
|
|||||||
@@ -22,6 +22,13 @@ constexpr char DEVICE_ID[] = "crosspoint-reader";
|
|||||||
// Default 16KB buffers cause OOM during TLS handshake.
|
// Default 16KB buffers cause OOM during TLS handshake.
|
||||||
constexpr int HTTP_BUF_SIZE = 2048;
|
constexpr int HTTP_BUF_SIZE = 2048;
|
||||||
|
|
||||||
|
// Cloudflare tunnels send a 3-cert Google Trust Services chain. During the TLS handshake
|
||||||
|
// mbedTLS makes many small allocations that collectively consume ~48KB of heap. With only
|
||||||
|
// ~50KB free after WiFi connects, the session drove min-free-ever down to 2600 bytes before
|
||||||
|
// failing with MBEDTLS_ERR_X509_ALLOC_FAILED (-0x2880). Check total free heap (not max
|
||||||
|
// contiguous block) because the failure mode is aggregate exhaustion, not one large alloc.
|
||||||
|
constexpr uint32_t MIN_HEAP_FOR_TLS = 55000;
|
||||||
|
|
||||||
// Response buffer for reading HTTP body
|
// Response buffer for reading HTTP body
|
||||||
struct ResponseBuffer {
|
struct ResponseBuffer {
|
||||||
char* data = nullptr;
|
char* data = nullptr;
|
||||||
@@ -102,7 +109,12 @@ KOReaderSyncClient::Error KOReaderSyncClient::authenticate() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string url = KOREADER_STORE.getBaseUrl() + "/users/auth";
|
std::string url = KOREADER_STORE.getBaseUrl() + "/users/auth";
|
||||||
LOG_DBG("KOSync", "Authenticating: %s (heap: %u)", url.c_str(), (unsigned)ESP.getFreeHeap());
|
const uint32_t freeHeap = ESP.getFreeHeap();
|
||||||
|
LOG_DBG("KOSync", "Authenticating: %s (heap: %u)", url.c_str(), (unsigned)freeHeap);
|
||||||
|
if (freeHeap < MIN_HEAP_FOR_TLS) {
|
||||||
|
LOG_ERR("KOSync", "Insufficient heap for TLS handshake: %u bytes free (need %u)", freeHeap, MIN_HEAP_FOR_TLS);
|
||||||
|
return LOW_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
ResponseBuffer buf;
|
ResponseBuffer buf;
|
||||||
esp_http_client_handle_t client = createClient(url.c_str(), &buf);
|
esp_http_client_handle_t client = createClient(url.c_str(), &buf);
|
||||||
@@ -130,7 +142,12 @@ KOReaderSyncClient::Error KOReaderSyncClient::getProgress(const std::string& doc
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress/" + documentHash;
|
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress/" + documentHash;
|
||||||
LOG_DBG("KOSync", "Getting progress: %s (heap: %u)", url.c_str(), (unsigned)ESP.getFreeHeap());
|
const uint32_t freeHeap = ESP.getFreeHeap();
|
||||||
|
LOG_DBG("KOSync", "Getting progress: %s (heap: %u)", url.c_str(), (unsigned)freeHeap);
|
||||||
|
if (freeHeap < MIN_HEAP_FOR_TLS) {
|
||||||
|
LOG_ERR("KOSync", "Insufficient heap for TLS handshake: %u bytes free (need %u)", freeHeap, MIN_HEAP_FOR_TLS);
|
||||||
|
return LOW_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
ResponseBuffer buf;
|
ResponseBuffer buf;
|
||||||
esp_http_client_handle_t client = createClient(url.c_str(), &buf);
|
esp_http_client_handle_t client = createClient(url.c_str(), &buf);
|
||||||
@@ -178,7 +195,12 @@ KOReaderSyncClient::Error KOReaderSyncClient::updateProgress(const KOReaderProgr
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress";
|
std::string url = KOREADER_STORE.getBaseUrl() + "/syncs/progress";
|
||||||
LOG_DBG("KOSync", "Updating progress: %s (heap: %u)", url.c_str(), (unsigned)ESP.getFreeHeap());
|
const uint32_t freeHeap = ESP.getFreeHeap();
|
||||||
|
LOG_DBG("KOSync", "Updating progress: %s (heap: %u)", url.c_str(), (unsigned)freeHeap);
|
||||||
|
if (freeHeap < MIN_HEAP_FOR_TLS) {
|
||||||
|
LOG_ERR("KOSync", "Insufficient heap for TLS handshake: %u bytes free (need %u)", freeHeap, MIN_HEAP_FOR_TLS);
|
||||||
|
return LOW_MEMORY;
|
||||||
|
}
|
||||||
|
|
||||||
// Build JSON body
|
// Build JSON body
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
@@ -233,6 +255,8 @@ const char* KOReaderSyncClient::errorString(Error error) {
|
|||||||
return "JSON parse error";
|
return "JSON parse error";
|
||||||
case NOT_FOUND:
|
case NOT_FOUND:
|
||||||
return "No progress found";
|
return "No progress found";
|
||||||
|
case LOW_MEMORY:
|
||||||
|
return "Not enough memory for sync — please retry";
|
||||||
default:
|
default:
|
||||||
return "Unknown error";
|
return "Unknown error";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ struct KOReaderProgress {
|
|||||||
*/
|
*/
|
||||||
class KOReaderSyncClient {
|
class KOReaderSyncClient {
|
||||||
public:
|
public:
|
||||||
enum Error { OK = 0, NO_CREDENTIALS, NETWORK_ERROR, AUTH_FAILED, SERVER_ERROR, JSON_ERROR, NOT_FOUND };
|
enum Error { OK = 0, NO_CREDENTIALS, NETWORK_ERROR, AUTH_FAILED, SERVER_ERROR, JSON_ERROR, NOT_FOUND, LOW_MEMORY };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Authenticate with the sync server (validate credentials).
|
* Authenticate with the sync server (validate credentials).
|
||||||
|
|||||||
@@ -18,9 +18,11 @@
|
|||||||
#include "EpubReaderChapterSelectionActivity.h"
|
#include "EpubReaderChapterSelectionActivity.h"
|
||||||
#include "EpubReaderFootnotesActivity.h"
|
#include "EpubReaderFootnotesActivity.h"
|
||||||
#include "EpubReaderPercentSelectionActivity.h"
|
#include "EpubReaderPercentSelectionActivity.h"
|
||||||
|
#include "EpubReaderUtils.h"
|
||||||
#include "KOReaderCredentialStore.h"
|
#include "KOReaderCredentialStore.h"
|
||||||
#include "KOReaderSyncActivity.h"
|
#include "KOReaderSyncActivity.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
|
#include "ProgressMapper.h"
|
||||||
#include "QrDisplayActivity.h"
|
#include "QrDisplayActivity.h"
|
||||||
#include "ReaderUtils.h"
|
#include "ReaderUtils.h"
|
||||||
#include "RecentBooksStore.h"
|
#include "RecentBooksStore.h"
|
||||||
@@ -392,7 +394,9 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
section.reset();
|
section.reset();
|
||||||
epub->clearCache();
|
epub->clearCache();
|
||||||
epub->setupCacheDir();
|
epub->setupCacheDir();
|
||||||
saveProgress(backupSpine, backupPage, backupPageCount);
|
if (!saveProgress(backupSpine, backupPage, backupPageCount)) {
|
||||||
|
LOG_ERR("ERS", "Failed to save progress before cache clear");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onGoHome();
|
onGoHome();
|
||||||
@@ -418,23 +422,42 @@ void EpubReaderActivity::onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction
|
|||||||
paragraphIndex = *pIdx;
|
paragraphIndex = *pIdx;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
startActivityForResult(
|
|
||||||
std::make_unique<KOReaderSyncActivity>(renderer, mappedInput, epub, epub->getPath(), currentSpineIndex,
|
// Pre-compute local KO position and chapter name while Epub is still in RAM.
|
||||||
currentPage, totalPages, paragraphIndex),
|
CrossPointPosition localPos = {currentSpineIndex, currentPage, totalPages};
|
||||||
[this](const ActivityResult& result) {
|
if (paragraphIndex.has_value()) {
|
||||||
if (!result.isCancelled) {
|
localPos.paragraphIndex = *paragraphIndex;
|
||||||
const auto& sync = std::get<SyncResult>(result.data);
|
localPos.hasParagraphIndex = true;
|
||||||
if (currentSpineIndex != sync.spineIndex || (section && section->currentPage != sync.page)) {
|
}
|
||||||
|
KOReaderPosition localKoPos = ProgressMapper::toKOReader(epub, localPos);
|
||||||
|
const int tocIdx = epub->getTocIndexForSpineIndex(currentSpineIndex);
|
||||||
|
std::string localChapterName = (tocIdx >= 0) ? epub->getTocItem(tocIdx).title : "";
|
||||||
|
const std::string savedEpubPath = epub->getPath();
|
||||||
|
|
||||||
|
// Persist current position so the reader resumes at the right page on return.
|
||||||
|
// goToReader() depends on this file, so abort the sync if the write fails.
|
||||||
|
if (!saveProgress(currentSpineIndex, currentPage, totalPages)) {
|
||||||
|
LOG_ERR("KOSync", "Aborting sync because current progress could not be saved");
|
||||||
|
pendingSyncSaveError = true;
|
||||||
|
requestUpdate();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Release Epub and Section to free ~65KB RAM for the TLS handshake.
|
||||||
|
LOG_DBG("KOSync", "Releasing epub for sync (heap before: %u)", (unsigned)ESP.getFreeHeap());
|
||||||
|
{
|
||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
currentSpineIndex = sync.spineIndex;
|
if (section) {
|
||||||
nextPageNumber = sync.page;
|
nextPageNumber = section->currentPage;
|
||||||
cachedChapterTotalPageCount = 0; // Prevent rescaling sync page
|
}
|
||||||
pendingPageJump.reset();
|
|
||||||
saveProgress(currentSpineIndex, nextPageNumber, 0);
|
|
||||||
section.reset();
|
section.reset();
|
||||||
|
epub.reset();
|
||||||
}
|
}
|
||||||
}
|
LOG_DBG("KOSync", "Epub released (heap after: %u)", (unsigned)ESP.getFreeHeap());
|
||||||
});
|
|
||||||
|
activityManager.replaceActivity(std::make_unique<KOReaderSyncActivity>(
|
||||||
|
renderer, mappedInput, savedEpubPath, currentSpineIndex, currentPage, totalPages, std::move(localKoPos),
|
||||||
|
std::move(localChapterName), paragraphIndex));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -530,6 +553,12 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto showPendingSyncSaveError = [this]() {
|
||||||
|
if (!pendingSyncSaveError) return;
|
||||||
|
pendingSyncSaveError = false;
|
||||||
|
GUI.drawPopup(renderer, tr(STR_SAVE_PROGRESS_FAILED));
|
||||||
|
};
|
||||||
|
|
||||||
// edge case handling for sub-zero spine index
|
// edge case handling for sub-zero spine index
|
||||||
if (currentSpineIndex < 0) {
|
if (currentSpineIndex < 0) {
|
||||||
currentSpineIndex = 0;
|
currentSpineIndex = 0;
|
||||||
@@ -545,6 +574,7 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_END_OF_BOOK), true, EpdFontFamily::BOLD);
|
renderer.drawCenteredText(UI_12_FONT_ID, 300, tr(STR_END_OF_BOOK), true, EpdFontFamily::BOLD);
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
automaticPageTurnActive = false;
|
automaticPageTurnActive = false;
|
||||||
|
showPendingSyncSaveError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -592,6 +622,7 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
SETTINGS.imageRendering, popupFn)) {
|
SETTINGS.imageRendering, popupFn)) {
|
||||||
LOG_ERR("ERS", "Failed to persist page data to SD");
|
LOG_ERR("ERS", "Failed to persist page data to SD");
|
||||||
section.reset();
|
section.reset();
|
||||||
|
showPendingSyncSaveError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -655,6 +686,7 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
renderStatusBar();
|
renderStatusBar();
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
automaticPageTurnActive = false;
|
automaticPageTurnActive = false;
|
||||||
|
showPendingSyncSaveError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -664,6 +696,7 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
renderStatusBar();
|
renderStatusBar();
|
||||||
renderer.displayBuffer();
|
renderer.displayBuffer();
|
||||||
automaticPageTurnActive = false;
|
automaticPageTurnActive = false;
|
||||||
|
showPendingSyncSaveError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -676,6 +709,7 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
requestUpdate(); // Try again after clearing cache
|
requestUpdate(); // Try again after clearing cache
|
||||||
// TODO: prevent infinite loop if the page keeps failing to load for some reason
|
// TODO: prevent infinite loop if the page keeps failing to load for some reason
|
||||||
automaticPageTurnActive = false;
|
automaticPageTurnActive = false;
|
||||||
|
showPendingSyncSaveError();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -689,6 +723,8 @@ void EpubReaderActivity::render(RenderLock&& lock) {
|
|||||||
silentIndexNextChapterIfNeeded(viewportWidth, viewportHeight);
|
silentIndexNextChapterIfNeeded(viewportWidth, viewportHeight);
|
||||||
saveProgress(currentSpineIndex, section->currentPage, section->pageCount);
|
saveProgress(currentSpineIndex, section->currentPage, section->pageCount);
|
||||||
|
|
||||||
|
showPendingSyncSaveError();
|
||||||
|
|
||||||
if (pendingScreenshot) {
|
if (pendingScreenshot) {
|
||||||
pendingScreenshot = false;
|
pendingScreenshot = false;
|
||||||
ScreenshotUtil::takeScreenshot(renderer);
|
ScreenshotUtil::takeScreenshot(renderer);
|
||||||
@@ -727,21 +763,8 @@ void EpubReaderActivity::silentIndexNextChapterIfNeeded(const uint16_t viewportW
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void EpubReaderActivity::saveProgress(int spineIndex, int currentPage, int pageCount) {
|
bool EpubReaderActivity::saveProgress(int spineIndex, int currentPage, int pageCount) {
|
||||||
FsFile f;
|
return EpubReaderUtils::saveProgress(*epub, spineIndex, currentPage, pageCount);
|
||||||
if (Storage.openFileForWrite("ERS", epub->getCachePath() + "/progress.bin", f)) {
|
|
||||||
uint8_t data[6];
|
|
||||||
data[0] = currentSpineIndex & 0xFF;
|
|
||||||
data[1] = (currentSpineIndex >> 8) & 0xFF;
|
|
||||||
data[2] = currentPage & 0xFF;
|
|
||||||
data[3] = (currentPage >> 8) & 0xFF;
|
|
||||||
data[4] = pageCount & 0xFF;
|
|
||||||
data[5] = (pageCount >> 8) & 0xFF;
|
|
||||||
f.write(data, 6);
|
|
||||||
LOG_DBG("ERS", "Progress saved: Chapter %d, Page %d", spineIndex, currentPage);
|
|
||||||
} else {
|
|
||||||
LOG_ERR("ERS", "Could not save progress!");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int orientedMarginTop,
|
void EpubReaderActivity::renderContents(std::unique_ptr<Page> page, const int orientedMarginTop,
|
||||||
const int orientedMarginRight, const int orientedMarginBottom,
|
const int orientedMarginRight, const int orientedMarginBottom,
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ class EpubReaderActivity final : public Activity {
|
|||||||
// Normalized 0.0-1.0 progress within the target spine item, computed from book percentage.
|
// Normalized 0.0-1.0 progress within the target spine item, computed from book percentage.
|
||||||
float pendingSpineProgress = 0.0f;
|
float pendingSpineProgress = 0.0f;
|
||||||
bool pendingScreenshot = false;
|
bool pendingScreenshot = false;
|
||||||
|
bool pendingSyncSaveError = false;
|
||||||
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
|
bool skipNextButtonCheck = false; // Skip button processing for one frame after subactivity exit
|
||||||
bool automaticPageTurnActive = false;
|
bool automaticPageTurnActive = false;
|
||||||
|
|
||||||
@@ -45,7 +46,7 @@ class EpubReaderActivity final : public Activity {
|
|||||||
int orientedMarginBottom, int orientedMarginLeft);
|
int orientedMarginBottom, int orientedMarginLeft);
|
||||||
void renderStatusBar() const;
|
void renderStatusBar() const;
|
||||||
void silentIndexNextChapterIfNeeded(uint16_t viewportWidth, uint16_t viewportHeight);
|
void silentIndexNextChapterIfNeeded(uint16_t viewportWidth, uint16_t viewportHeight);
|
||||||
void saveProgress(int spineIndex, int currentPage, int pageCount);
|
bool saveProgress(int spineIndex, int currentPage, int pageCount);
|
||||||
// Jump to a percentage of the book (0-100), mapping it to spine and page.
|
// Jump to a percentage of the book (0-100), mapping it to spine and page.
|
||||||
void jumpToPercent(int percent);
|
void jumpToPercent(int percent);
|
||||||
void onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action);
|
void onReaderMenuConfirm(EpubReaderMenuActivity::MenuAction action);
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <Epub.h>
|
||||||
|
#include <HalStorage.h>
|
||||||
|
#include <Logging.h>
|
||||||
|
|
||||||
|
namespace EpubReaderUtils {
|
||||||
|
|
||||||
|
// Persists reader progress for an EPUB to its cache directory. Returns true on success.
|
||||||
|
inline bool saveProgress(Epub& epub, int spineIndex, int pageNumber, int pageCount) {
|
||||||
|
if (spineIndex < 0 || spineIndex > 0xFFFF || pageNumber < 0 || pageNumber > 0xFFFF || pageCount < 0 ||
|
||||||
|
pageCount > 0xFFFF) {
|
||||||
|
LOG_ERR("ERS", "Progress values out of range: spine=%d page=%d count=%d", spineIndex, pageNumber, pageCount);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
FsFile f;
|
||||||
|
if (!Storage.openFileForWrite("ERS", epub.getCachePath() + "/progress.bin", f)) {
|
||||||
|
LOG_ERR("ERS", "Could not open progress file for write!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
uint8_t data[6];
|
||||||
|
data[0] = spineIndex & 0xFF;
|
||||||
|
data[1] = (spineIndex >> 8) & 0xFF;
|
||||||
|
data[2] = pageNumber & 0xFF;
|
||||||
|
data[3] = (pageNumber >> 8) & 0xFF;
|
||||||
|
data[4] = pageCount & 0xFF;
|
||||||
|
data[5] = (pageCount >> 8) & 0xFF;
|
||||||
|
const size_t written = f.write(data, sizeof(data));
|
||||||
|
if (written != sizeof(data)) {
|
||||||
|
LOG_ERR("ERS", "Short write saving progress: %u/%u bytes", (unsigned)written, (unsigned)sizeof(data));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
LOG_DBG("ERS", "Progress saved: spine=%d page=%d", spineIndex, pageNumber);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace EpubReaderUtils
|
||||||
@@ -1,30 +1,26 @@
|
|||||||
#include "KOReaderSyncActivity.h"
|
#include "KOReaderSyncActivity.h"
|
||||||
|
|
||||||
#include <GfxRenderer.h>
|
#include <GfxRenderer.h>
|
||||||
|
#include <HalStorage.h>
|
||||||
#include <I18n.h>
|
#include <I18n.h>
|
||||||
#include <Logging.h>
|
#include <Logging.h>
|
||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_sntp.h>
|
#include <esp_sntp.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
#include "Epub/Section.h"
|
#include "Epub/Section.h"
|
||||||
|
#include "EpubReaderUtils.h"
|
||||||
#include "KOReaderCredentialStore.h"
|
#include "KOReaderCredentialStore.h"
|
||||||
#include "KOReaderDocumentId.h"
|
#include "KOReaderDocumentId.h"
|
||||||
#include "MappedInputManager.h"
|
#include "MappedInputManager.h"
|
||||||
|
#include "ReaderUtils.h"
|
||||||
|
#include "activities/ActivityManager.h"
|
||||||
#include "activities/network/WifiSelectionActivity.h"
|
#include "activities/network/WifiSelectionActivity.h"
|
||||||
#include "components/UITheme.h"
|
#include "components/UITheme.h"
|
||||||
#include "fontIds.h"
|
#include "fontIds.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
CrossPointPosition makeLocalPositionWithParagraph(const int spineIndex, const int page, const int totalPages,
|
|
||||||
const std::optional<uint16_t>& paragraphIndex) {
|
|
||||||
CrossPointPosition pos = {spineIndex, page, totalPages};
|
|
||||||
if (paragraphIndex.has_value()) {
|
|
||||||
pos.paragraphIndex = *paragraphIndex;
|
|
||||||
pos.hasParagraphIndex = true;
|
|
||||||
}
|
|
||||||
return pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
void syncTimeWithNTP() {
|
void syncTimeWithNTP() {
|
||||||
// Stop SNTP if already running (can't reconfigure while running)
|
// Stop SNTP if already running (can't reconfigure while running)
|
||||||
if (esp_sntp_enabled()) {
|
if (esp_sntp_enabled()) {
|
||||||
@@ -61,13 +57,43 @@ void wifiOff() {
|
|||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
void KOReaderSyncActivity::ensureEpubLoaded() {
|
||||||
|
if (!epub) {
|
||||||
|
LOG_DBG("KOSync", "Loading epub for progress mapping (heap: %u)", (unsigned)ESP.getFreeHeap());
|
||||||
|
epub = std::make_shared<Epub>(epubPath, "/.crosspoint");
|
||||||
|
epub->setupCacheDir();
|
||||||
|
// Load metadata only (no CSS needed for progress mapping, don't rebuild if cache is missing).
|
||||||
|
if (!epub->load(false, true)) {
|
||||||
|
LOG_ERR("KOSync", "Failed to load epub for progress mapping");
|
||||||
|
epub.reset();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG_DBG("KOSync", "Epub loaded (heap: %u)", (unsigned)ESP.getFreeHeap());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void KOReaderSyncActivity::saveProgressAndReturn(int spineIndex, int page) {
|
||||||
|
// epub is guaranteed non-null here: ensureEpubLoaded() was called in performSync() before
|
||||||
|
// SHOWING_RESULT state is entered, and this method is only called from that state.
|
||||||
|
assert(epub);
|
||||||
|
if (!EpubReaderUtils::saveProgress(*epub, spineIndex, page, 0)) {
|
||||||
|
{
|
||||||
|
RenderLock lock(*this);
|
||||||
|
state = SYNC_FAILED;
|
||||||
|
statusMessage = tr(STR_SAVE_PROGRESS_FAILED);
|
||||||
|
}
|
||||||
|
requestUpdate(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
returnToReader();
|
||||||
|
}
|
||||||
|
|
||||||
|
void KOReaderSyncActivity::returnToReader() { activityManager.goToReader(epubPath); }
|
||||||
|
|
||||||
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
void KOReaderSyncActivity::onWifiSelectionComplete(const bool success) {
|
||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_DBG("KOSync", "WiFi connection failed, exiting");
|
LOG_DBG("KOSync", "WiFi connection failed, exiting");
|
||||||
ActivityResult result;
|
returnToReader();
|
||||||
result.isCancelled = true;
|
|
||||||
setResult(std::move(result));
|
|
||||||
finish();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,8 +167,19 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert remote progress to CrossPoint position
|
// Epub was released before sync to free RAM for the TLS handshake — reload it now.
|
||||||
hasRemoteProgress = true;
|
hasRemoteProgress = true;
|
||||||
|
ensureEpubLoaded();
|
||||||
|
if (!epub) {
|
||||||
|
{
|
||||||
|
RenderLock lock(*this);
|
||||||
|
state = SYNC_FAILED;
|
||||||
|
statusMessage = "";
|
||||||
|
}
|
||||||
|
requestUpdate(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
KOReaderPosition koPos = {remoteProgress.progress, remoteProgress.percentage};
|
KOReaderPosition koPos = {remoteProgress.progress, remoteProgress.percentage};
|
||||||
remotePosition = ProgressMapper::toCrossPoint(epub, koPos, currentSpineIndex, totalPagesInSpine);
|
remotePosition = ProgressMapper::toCrossPoint(epub, koPos, currentSpineIndex, totalPagesInSpine);
|
||||||
|
|
||||||
@@ -157,11 +194,7 @@ void KOReaderSyncActivity::performSync() {
|
|||||||
remotePosition.pageNumber = *paragraphPage;
|
remotePosition.pageNumber = *paragraphPage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// localProgress was pre-computed in EpubReaderActivity before the Epub was released.
|
||||||
// Calculate local progress in KOReader format (for display)
|
|
||||||
CrossPointPosition localPos =
|
|
||||||
makeLocalPositionWithParagraph(currentSpineIndex, currentPage, totalPagesInSpine, currentParagraphIndex);
|
|
||||||
localProgress = ProgressMapper::toKOReader(epub, localPos);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
RenderLock lock(*this);
|
RenderLock lock(*this);
|
||||||
@@ -185,15 +218,11 @@ void KOReaderSyncActivity::performUpload() {
|
|||||||
}
|
}
|
||||||
requestUpdateAndWait();
|
requestUpdateAndWait();
|
||||||
|
|
||||||
// Convert current position to KOReader format
|
// localProgress was pre-computed in EpubReaderActivity before the Epub was released.
|
||||||
CrossPointPosition localPos =
|
|
||||||
makeLocalPositionWithParagraph(currentSpineIndex, currentPage, totalPagesInSpine, currentParagraphIndex);
|
|
||||||
KOReaderPosition koPos = ProgressMapper::toKOReader(epub, localPos);
|
|
||||||
|
|
||||||
KOReaderProgress progress;
|
KOReaderProgress progress;
|
||||||
progress.document = documentHash;
|
progress.document = documentHash;
|
||||||
progress.progress = koPos.xpath;
|
progress.progress = localProgress.xpath;
|
||||||
progress.percentage = koPos.percentage;
|
progress.percentage = localProgress.percentage;
|
||||||
|
|
||||||
const auto result = KOReaderSyncClient::updateProgress(progress);
|
const auto result = KOReaderSyncClient::updateProgress(progress);
|
||||||
|
|
||||||
@@ -218,6 +247,7 @@ void KOReaderSyncActivity::performUpload() {
|
|||||||
|
|
||||||
void KOReaderSyncActivity::onEnter() {
|
void KOReaderSyncActivity::onEnter() {
|
||||||
Activity::onEnter();
|
Activity::onEnter();
|
||||||
|
ReaderUtils::applyOrientation(renderer, SETTINGS.orientation);
|
||||||
|
|
||||||
// Check for credentials first
|
// Check for credentials first
|
||||||
if (!KOREADER_STORE.hasCredentials()) {
|
if (!KOREADER_STORE.hasCredentials()) {
|
||||||
@@ -271,14 +301,14 @@ void KOReaderSyncActivity::render(RenderLock&&) {
|
|||||||
// Show comparison
|
// Show comparison
|
||||||
renderer.drawCenteredText(UI_10_FONT_ID, 120, tr(STR_PROGRESS_FOUND), true, EpdFontFamily::BOLD);
|
renderer.drawCenteredText(UI_10_FONT_ID, 120, tr(STR_PROGRESS_FOUND), true, EpdFontFamily::BOLD);
|
||||||
|
|
||||||
// Get chapter names from TOC
|
// Remote chapter name requires Epub (loaded lazily in performSync before this state).
|
||||||
const int remoteTocIndex = epub->getTocIndexForSpineIndex(remotePosition.spineIndex);
|
const int remoteTocIndex = epub->getTocIndexForSpineIndex(remotePosition.spineIndex);
|
||||||
const int localTocIndex = epub->getTocIndexForSpineIndex(currentSpineIndex);
|
|
||||||
const std::string remoteChapter =
|
const std::string remoteChapter =
|
||||||
(remoteTocIndex >= 0) ? epub->getTocItem(remoteTocIndex).title
|
(remoteTocIndex >= 0) ? epub->getTocItem(remoteTocIndex).title
|
||||||
: (std::string(tr(STR_SECTION_PREFIX)) + std::to_string(remotePosition.spineIndex + 1));
|
: (std::string(tr(STR_SECTION_PREFIX)) + std::to_string(remotePosition.spineIndex + 1));
|
||||||
|
// Local chapter name was pre-computed before Epub was released.
|
||||||
const std::string localChapter =
|
const std::string localChapter =
|
||||||
(localTocIndex >= 0) ? epub->getTocItem(localTocIndex).title
|
!localChapterName.empty() ? localChapterName
|
||||||
: (std::string(tr(STR_SECTION_PREFIX)) + std::to_string(currentSpineIndex + 1));
|
: (std::string(tr(STR_SECTION_PREFIX)) + std::to_string(currentSpineIndex + 1));
|
||||||
|
|
||||||
// Remote progress - chapter and page
|
// Remote progress - chapter and page
|
||||||
@@ -362,10 +392,7 @@ void KOReaderSyncActivity::render(RenderLock&&) {
|
|||||||
void KOReaderSyncActivity::loop() {
|
void KOReaderSyncActivity::loop() {
|
||||||
if (state == NO_CREDENTIALS || state == SYNC_FAILED || state == UPLOAD_COMPLETE) {
|
if (state == NO_CREDENTIALS || state == SYNC_FAILED || state == UPLOAD_COMPLETE) {
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
ActivityResult result;
|
returnToReader();
|
||||||
result.isCancelled = true;
|
|
||||||
setResult(std::move(result));
|
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -384,9 +411,7 @@ void KOReaderSyncActivity::loop() {
|
|||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
|
||||||
if (selectedOption == 0) {
|
if (selectedOption == 0) {
|
||||||
// Wifi will be turned off in onExit()
|
saveProgressAndReturn(remotePosition.spineIndex, remotePosition.pageNumber);
|
||||||
setResult(SyncResult{remotePosition.spineIndex, remotePosition.pageNumber});
|
|
||||||
finish();
|
|
||||||
} else if (selectedOption == 1) {
|
} else if (selectedOption == 1) {
|
||||||
// Upload local progress
|
// Upload local progress
|
||||||
performUpload();
|
performUpload();
|
||||||
@@ -394,10 +419,7 @@ void KOReaderSyncActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
ActivityResult result;
|
returnToReader();
|
||||||
result.isCancelled = true;
|
|
||||||
setResult(std::move(result));
|
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -416,10 +438,7 @@ void KOReaderSyncActivity::loop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
if (mappedInput.wasReleased(MappedInputManager::Button::Back)) {
|
||||||
ActivityResult result;
|
returnToReader();
|
||||||
result.isCancelled = true;
|
|
||||||
setResult(std::move(result));
|
|
||||||
finish();
|
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,20 +21,20 @@
|
|||||||
*/
|
*/
|
||||||
class KOReaderSyncActivity final : public Activity {
|
class KOReaderSyncActivity final : public Activity {
|
||||||
public:
|
public:
|
||||||
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput,
|
explicit KOReaderSyncActivity(GfxRenderer& renderer, MappedInputManager& mappedInput, const std::string& epubPath,
|
||||||
const std::shared_ptr<Epub>& epub, const std::string& epubPath, int currentSpineIndex,
|
int currentSpineIndex, int currentPage, int totalPagesInSpine,
|
||||||
int currentPage, int totalPagesInSpine,
|
KOReaderPosition localKoPos, std::string localChapterName,
|
||||||
std::optional<uint16_t> currentParagraphIndex = std::nullopt)
|
std::optional<uint16_t> currentParagraphIndex = std::nullopt)
|
||||||
: Activity("KOReaderSync", renderer, mappedInput),
|
: Activity("KOReaderSync", renderer, mappedInput),
|
||||||
epub(epub),
|
|
||||||
epubPath(epubPath),
|
epubPath(epubPath),
|
||||||
currentSpineIndex(currentSpineIndex),
|
currentSpineIndex(currentSpineIndex),
|
||||||
currentPage(currentPage),
|
currentPage(currentPage),
|
||||||
totalPagesInSpine(totalPagesInSpine),
|
totalPagesInSpine(totalPagesInSpine),
|
||||||
currentParagraphIndex(currentParagraphIndex),
|
currentParagraphIndex(currentParagraphIndex),
|
||||||
|
localChapterName(std::move(localChapterName)),
|
||||||
remoteProgress{},
|
remoteProgress{},
|
||||||
remotePosition{},
|
remotePosition{},
|
||||||
localProgress{} {}
|
localProgress(std::move(localKoPos)) {}
|
||||||
|
|
||||||
void onEnter() override;
|
void onEnter() override;
|
||||||
void onExit() override;
|
void onExit() override;
|
||||||
@@ -55,8 +55,9 @@ class KOReaderSyncActivity final : public Activity {
|
|||||||
NO_CREDENTIALS
|
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 epubPath;
|
||||||
|
std::string localChapterName;
|
||||||
int currentSpineIndex;
|
int currentSpineIndex;
|
||||||
int currentPage;
|
int currentPage;
|
||||||
int totalPagesInSpine;
|
int totalPagesInSpine;
|
||||||
@@ -71,7 +72,7 @@ class KOReaderSyncActivity final : public Activity {
|
|||||||
KOReaderProgress remoteProgress;
|
KOReaderProgress remoteProgress;
|
||||||
CrossPointPosition remotePosition;
|
CrossPointPosition remotePosition;
|
||||||
|
|
||||||
// Local progress as KOReader format (for display)
|
// Local progress as KOReader format (pre-computed before Epub was released)
|
||||||
KOReaderPosition localProgress;
|
KOReaderPosition localProgress;
|
||||||
|
|
||||||
// Selection in result screen (0=Apply, 1=Upload)
|
// Selection in result screen (0=Apply, 1=Upload)
|
||||||
@@ -80,4 +81,7 @@ class KOReaderSyncActivity final : public Activity {
|
|||||||
void onWifiSelectionComplete(bool success);
|
void onWifiSelectionComplete(bool success);
|
||||||
void performSync();
|
void performSync();
|
||||||
void performUpload();
|
void performUpload();
|
||||||
|
void ensureEpubLoaded();
|
||||||
|
void saveProgressAndReturn(int spineIndex, int page);
|
||||||
|
void returnToReader();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user