Skip to content

fix: refresh widgets after idle#978

Open
ovitrif wants to merge 6 commits into
masterfrom
codex/github-mention-bug]-android-os-widgets-show-stale-data-af
Open

fix: refresh widgets after idle#978
ovitrif wants to merge 6 commits into
masterfrom
codex/github-mention-bug]-android-os-widgets-show-stale-data-af

Conversation

@ovitrif
Copy link
Copy Markdown
Collaborator

@ovitrif ovitrif commented May 31, 2026

Fixes #975

Description

This PR:

  1. Refactors Android widget refresh scheduling into one AppWidgetRefreshScheduler entry point.
  2. Keeps periodic WorkManager refresh as the primary 15-minute best-effort engine, with a sparse inexact idle alarm for app-not-running catch-up and a 30-minute native widget fallback.
  3. Restores scheduling from app startup, foreground, widget enable/update/configuration, boot, package replacement, and the foreground-service stop action.
  4. Updates the worker to refresh only active widget types, track per-type attempt/success freshness, skip fresh remote-backed types, and retry stale/failed types independently.
  5. Treats Facts as local cached rotation, so a Facts update is not counted as proof that remote-backed widgets refreshed.
  6. Removes runtime-only user-present/idle-mode receiver registration and avoids tying widget refresh to LightningNodeService.onDestroy.

No exact-alarm permission, battery optimization exemption, debug-only shortcut, temporary patch, accelerated wait command, or manual widget refresh UI is added.

Preview

Before Idle After Idle
beforeIdle afterIdle

QA Notes

Manual Tests

  • 1. Android launcher → install the PR dev build → add Price, Headlines, Blocks, Facts, and Weather OS widgets → open Bitkit once → stop Bitkit from the persistent notification → keep Wi-Fi/data on → unplug USB/power → lock the phone and leave it untouched for 30+ minutes → unlock without opening Bitkit: remote-backed widgets update without launching the app.
    Result: Real idle wait from 19:15 to 20:43 on June 2, 2026: Price changed from $67.443 to $67.314, Blocks changed from 952,132 to 952,138, News changed headline, and Facts rotated locally. Weather stayed at $0.28, which is valid when the fee estimate itself has not changed.
  • 3. Logs: device logs after the real wait show Bitkit widget update notifications after unlock, successful widget update records, and a later catch-up alarm/worker pass that saw all active widget types and skipped remote-backed types as fresh.
  • 4. negative: Android quick settings -> disable Wi-Fi/data -> repeat the idle test: remote-backed widgets may stay stale while offline; Facts may still rotate from local cached facts only.
  • 5. negative: Android Settings -> Apps -> Bitkit -> Force stop -> return to launcher: Android 15 may disable widget updates/pending intents until the user launches or interacts with Bitkit again; this is expected platform behavior.

Automated Checks

  • Unit tests added: cover widget freshness policy, scheduler cancel/schedule behavior, boot/package/alarm receiver delegation, and worker attempt/success handling in app/src/test/java/to/bitkit/appwidget/.
  • Unit test modified: covers the foreground-service stop action scheduling widget catch-up before shutdown in app/src/test/java/to/bitkit/androidServices/LightningNodeServiceTest.kt.
  • git diff --check passed locally.
  • ./gradlew compileDevDebugKotlin passed locally.
  • ./gradlew testDevDebugUnitTest passed locally.
  • ./gradlew detekt passed locally.

@ovitrif ovitrif self-assigned this May 31, 2026
@ovitrif ovitrif force-pushed the codex/github-mention-bug]-android-os-widgets-show-stale-data-af branch from efea229 to 295079f Compare May 31, 2026 19:18
@ovitrif ovitrif removed the codex label May 31, 2026
@ovitrif ovitrif added this to the 2.3.0 milestone May 31, 2026
@ovitrif ovitrif requested a review from piotr-iohk May 31, 2026 19:50
@ovitrif ovitrif marked this pull request as ready for review May 31, 2026 19:51
@ovitrif ovitrif enabled auto-merge May 31, 2026 19:51
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 295079f67e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/src/main/java/to/bitkit/App.kt Outdated
@piotr-iohk
Copy link
Copy Markdown
Collaborator

Testing notes:

Prerequisite: add all OS widgets and lock the phone for a few (15+) minutes.

a) Bitkit running in bg

  • unlock and check widgets -> OK, widgets get refreshed after unlock ✅

b) Bitkit not running in background (foreground service stopped from the persistent Bitkit notification)

  • unlock and check widgets -> widgets are not refreshed ❌

Also noticed this after force shutdown the app - OS widgets loose the content, until tapped on. (however force shutdown warns that this action may cause app misbehavior).

Screen_Recording_20260601_162003_One.UI.Home.mp4

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 1, 2026

Testing notes:

Prerequisite: add all OS widgets and lock the phone for a few (15+) minutes.

a) Bitkit running in bg

  • unlock and check widgets -> OK, widgets get refreshed after unlock ✅

b) Bitkit not running in background (foreground service stopped from the persistent Bitkit notification)

  • unlock and check widgets -> widgets are not refreshed ❌

Also noticed this after force shutdown the app - OS widgets loose the content, until tapped on. (however force shutdown warns that this action may cause app misbehavior).

[media]

Resolved in de12165

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 1, 2026

Also noticed this after force shutdown the app - OS widgets loose the content, until tapped on. (however force shutdown warns that this action may cause app misbehavior).

Probably same behavior for all other apps 🤷🏻 ? That's what I'm observing:

@jvsena42
Copy link
Copy Markdown
Member

jvsena42 commented Jun 1, 2026

Starting review

jvsena42
jvsena42 previously approved these changes Jun 1, 2026
Copy link
Copy Markdown
Member

@jvsena42 jvsena42 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with 2 comments:

  1. There is a duplicated request flow for widgets, maybe we should monitor battery/internet usage

Tested:

  1. Notifications disabled
  2. Bitkit in BG
  3. Leave the phone offline and in idle > 15 min
  4. unlock the phone and turn on wifi
  5. Widgets refreshed

Comment thread app/src/main/java/to/bitkit/appwidget/AppWidgetRefreshWorker.kt Outdated
@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 1, 2026

  1. There is a duplicated request flow for widgets, maybe we should monitor battery/internet usage

Good point. The unique work names prevent parallel duplicate workers, but the alarm path can still overlap with the periodic worker over time. I’d prefer to keep this PR scoped to fixing the stale-widget bug and follow up with a small throttle/centralized scheduler so we can reduce battery/network churn without weakening the catch-up behavior.

Wdyt?

@piotr-iohk
Copy link
Copy Markdown
Collaborator

piotr-iohk commented Jun 2, 2026

On my side I don't see this case working:

b) Bitkit not running in background (foreground service stopped from the persistent Bitkit notification)
unlock and check widgets -> widgets are not refreshed ❌

If Bitkit app is not running - which I believe will be the most common case - the widgets don't seem to be refreshed. 🤷
(actually from what I observe only "Facts" seems to be refreshing - the other ones are stale)

For the record, using this to stop the app:
Screenshot 2026-06-02 at 14 31 58

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

On my side I don't see this case working:

b) Bitkit not running in background (foreground service stopped from the persistent Bitkit notification)
unlock and check widgets -> widgets are not refreshed ❌

If Bitkit app is not running - which I believe will be the most common case - the widgets don't seem to be refreshed. 🤷 (actually from what I observe only "Facts" seems to be refreshing - the other ones are stale)

For the record, using this to stop the app: [img]

I could repro it ONLY by not re-enabling the wifi & data via this guide:

# simulate bad condition:
adb shell svc wifi disable
adb shell svc data disable
adb shell dumpsys battery unplug
adb shell cmd deviceidle enable
adb shell cmd deviceidle force-idle

# Wait ~70-90 seconds because of the temporary 1-minute alarm patch.
# Wake + reconnect:

adb shell cmd deviceidle unforce
adb shell input keyevent KEYCODE_WAKEUP
adb shell wm dismiss-keyguard
# if I stop here: widgets never refresh (except Facts, which does)
adb shell svc wifi enable
adb shell svc data enable
On Idle After Wake Minutes Later After Wifi+Data re-enable

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

FYI, the reason Facts widget updates is because it uses local cached data, so it's not a real update, just rotating cached entries.

@piotr-iohk
Copy link
Copy Markdown
Collaborator

I could repro it ONLY by not re-enabling the wifi & data via this guide:

So, my test is really simple, not using any additional commands - not messing with wifi or other settings, just testing on a device. When I make the phone idle (by locking, or just putting away) - and I don't have Bitkit app running - then - after unlocking it after a while I don't see the OS widgets updating - they show old, stale data.

They only update if:

  • I start the Bitkit app
  • or edit particular widget - then the one I amended updates

That is basically what I observe...

btw seeing some slack issues on my side so not sure if you can see my messages there

@ovitrif ovitrif marked this pull request as draft June 2, 2026 14:50
auto-merge was automatically disabled June 2, 2026 14:50

Pull request was converted to draft

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

Drafted to refactor into a better solution. (including BOOT wake-up for widgets refresh)

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

When I make the phone idle (by locking, or just putting away) - and I don't have Bitkit app running - then - after unlocking it after a while I don't see the OS widgets updating - they show old, stale data.

They only update if:

  • I start the Bitkit app
  • or edit particular widget

Resolved in ddf4c64

@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

There is a duplicated request flow for widgets, maybe we should monitor battery/internet usage

Resolved in ddf4c64

@ovitrif ovitrif force-pushed the codex/github-mention-bug]-android-os-widgets-show-stale-data-af branch 2 times, most recently from 19f5fc1 to ddf4c64 Compare June 2, 2026 17:08
@ovitrif ovitrif marked this pull request as ready for review June 2, 2026 19:26
@ovitrif ovitrif force-pushed the codex/github-mention-bug]-android-os-widgets-show-stale-data-af branch from ddf4c64 to 89bb9cd Compare June 2, 2026 19:35
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 89bb9cd6b3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread app/src/main/java/to/bitkit/appwidget/AppWidgetRefreshWorker.kt
@ovitrif ovitrif force-pushed the codex/github-mention-bug]-android-os-widgets-show-stale-data-af branch from 89bb9cd to cafffb4 Compare June 2, 2026 19:42
@ovitrif ovitrif force-pushed the codex/github-mention-bug]-android-os-widgets-show-stale-data-af branch from cafffb4 to 718d445 Compare June 2, 2026 19:46
@ovitrif ovitrif requested a review from jvsena42 June 2, 2026 19:54
@ovitrif
Copy link
Copy Markdown
Collaborator Author

ovitrif commented Jun 2, 2026

@piotr-iohk refactored into something that worked reliable on my end
@jvsena42 please check refactoring, it changes quite a lot of the initial arch; note that many chunks of code are just moved around; but are still present, and the logic update is unit-tested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Android OS widgets show stale data after idle

3 participants