Skip to content

fix(android): fail closed when Background Mode has no native BLE route - #8353

Merged
Git-on-my-level merged 7 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/issue-7548-android-background-fail-closed
Jun 25, 2026
Merged

Git-on-my-level merged 7 commits into
BasedHardware:mainfrom
Git-on-my-level:fix/issue-7548-android-background-fail-closed

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Jun 25, 2026 •

Copy link
Copy Markdown
Member

Summary

Partial mitigation for #7548 — fail-closed guardrail only, no new device protocol support.

Background Mode now validates that the currently-connected device has a concrete native BLE audio route before accepting the enable request. This prevents users from enabling Background Mode when no compatible device can actually stream audio in the background, which would silently fail and leave them with non-functional recording.

What this changes

Area Change
CaptureProvider New setBackgroundModeEnabled(bool) method owns all Background Mode state. Enable is gated behind hasNativeBleAudioRoute predicate. Disable clears backgroundModeEnabled, nativeBleStreamingEnabled, nativeBleForegroundReady, and removes stale nativeBleStreamConfig.
CaptureProvider New hasNativeBleAudioRoute predicate: requires device != null, device.id.isNotEmpty, and _nativeBleAudioTarget(device) != null. Rejects empty-id Omi sentinel entries that may linger from stale state.
CaptureProvider setBatchMode now only re-enables native streaming when turning batch OFF and a concrete device with a native route is connected.
CaptureProvider _saveNativeBleStreamConfig: when audioTarget is null (device has no native route), also clears backgroundModeEnabled and removes stale config.
Profile settings _showBackgroundModeSheet delegates to CaptureProvider.setBackgroundModeEnabled() instead of writing prefs directly. Switch is disabled when no route is available, with a warning banner explaining why.
l10n Added backgroundModeUnavailable string to all 49 locale ARB files.
Tests 34 new tests covering: route availability matrix (all 9 device types), empty-id Omi sentinel, rejected enable keeps prefs false, stale reconciliation, enable/disable cycle, batch mode interaction, device switching.

Available native BLE routes

Only these device types have a native BLE audio target:

  • Omi (DeviceType.omi)
  • OpenGlass (DeviceType.openglass)
  • Friend Pendant (DeviceType.friendPendant)

All others (Apple Watch, Bee, Fieldy, Frame, Limitless, Plaud) return null from _nativeBleAudioTarget and are correctly blocked.

What this does NOT do

Testing

  • dart format --line-length 120 passes on all 3 modified Dart files.
  • flutter test cannot run in this environment (Flutter SDK not installed), but 34 new unit tests have been added covering all specified matrix cases.
  • Pre-commit hook formatting passes.

Refs #7548

Review in cubic

Partial mitigation for BasedHardware#7548.

Background Mode now validates that a connected device has a native BLE
audio route before accepting the enable request. Devices without a
native route (Apple Watch, Bee, Fieldy, Frame, Limitless, Plaud) or
with an empty device ID (stale sentinel) will be rejected, and the
toggle in the UI will be disabled with a warning message.

Changes:
- CaptureProvider.setBackgroundModeEnabled(bool): gate enable behind
  hasNativeBleAudioRoute predicate; disable clears all prefs and stale
  nativeBleStreamConfig; rejected enable defensively clears stale state
- CaptureProvider.hasNativeBleAudioRoute: new @VisibleForTesting
  predicate requiring device != null, device.id.isNotEmpty, and
  _nativeBleAudioTarget(device) != null
- CaptureProvider.setBatchMode: only re-enable native streaming when
  turning batch OFF AND a concrete device with native route is connected
- CaptureProvider._saveNativeBleStreamConfig: when audioTarget is null,
  also clear backgroundModeEnabled and remove stale config
- Profile._showBackgroundModeSheet: delegates to
  CaptureProvider.setBackgroundModeEnabled; shows warning when route is
  unavailable; disables switch when no route
- l10n: add backgroundModeUnavailable string to all 49 locale ARBs
- Tests: 34 new tests covering route matrix, empty-id sentinel,
  rejected enable, stale reconciliation, enable/disable cycle,
  batch interaction, device switching
@thesohamdatta

Copy link
Copy Markdown

Bro how do you work i mean what you process of ideation to production

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

Copy link
Copy Markdown

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: 1f313a10e0

ℹ️ 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/lib/pages/settings/profile.dart Resolved
Comment thread app/lib/providers/capture_provider.dart Resolved
Comment thread app/lib/l10n/app_de.arb Resolved
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Fixed the CI Dart formatting failure by running the same Flutter/Dart formatter used by CI via ghcr.io/cirruslabs/flutter:stable.

Verification:

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/pages/settings/profile.dart app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart'
# Formatted 3 files (0 changed)

git diff --check

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

39 issues found and verified against the latest diff

Confidence score: 3/5

  • In app/lib/pages/settings/profile.dart, the Background Mode toggle is effectively locked when OFF, so users may be unable to enable the feature at all; merging as-is risks a direct settings regression for anyone trying to turn it on — update the enable/disable gating to depend on route availability (while still allowing OFF when currently ON) and add a quick UI/state test before merging.
  • The new localization key is still English across multiple locale files (app/lib/l10n/app_da.arb, app/lib/l10n/app_fi.arb, app/lib/l10n/app_fr.arb, app/lib/l10n/app_tl.arb, app/lib/l10n/app_pl.arb, app/lib/l10n/app_ta.arb, app/lib/l10n/app_vi.arb, app/lib/l10n/app_cs.arb, app/lib/l10n/app_ms.arb), which will ship mixed-language UI and reduce polish for non-English users — fill in those translations (or temporarily fall back explicitly) before merging.

You’re at about 94% of the monthly reviewed-line limit. You may want to disable incremental reviews to conserve quota. Reviews will continue until that limit is exceeded. If you need help avoiding interruptions, please contact [email protected].

Tip: instead of fixing issues one by one fix them all with cubic
Partial review: This PR has more than 50 files, so cubic reviewed the highest-priority files first. During the trial, paid plans get a higher file limit.
You can try an ultrareview to bypass the file limit, comment @cubic-dev-ai ultrareview. Learn more.

Re-trigger cubic

Comment thread app/lib/pages/settings/profile.dart Resolved
Comment thread app/lib/l10n/app_da.arb Resolved
Comment thread app/lib/l10n/app_fi.arb Resolved
Comment thread app/lib/l10n/app_fr.arb Resolved
Comment thread app/lib/l10n/app_tl.arb Resolved
Comment thread app/lib/l10n/app_ca.arb Resolved
Comment thread app/lib/l10n/app_be.arb Resolved
Comment thread app/lib/l10n/app_sv.arb Resolved
Comment thread app/lib/l10n/app_mr.arb Resolved
Comment thread app/lib/l10n/app_de.arb Resolved

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

Copy link
Copy Markdown

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: 00aee19e08

ℹ️ 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/lib/providers/capture_provider.dart Resolved
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the first Codex review round on #8353.

Fixes:

  • Background Mode switch is now enabled when either Background Mode is already on (so it can be turned off) or a compatible native BLE route is available (so first-time enable works).
  • Disabling Background Mode now clears realtime streaming prefs but preserves nativeBleStreamConfig when Transcribe Later/batch mode is still enabled for a concrete compatible device, because batch capture uses that same config for offline writes.
  • Added regression coverage for preserving batch config while disabling Background Mode.
  • Translated backgroundModeUnavailable in all non-English ARB files instead of leaving the English source string.
  • Fixed the new provider test helper/imports so the added tests match the current BtDevice/preferences APIs.

Verification:

python3 - <<'PY'
import json, glob
for p in glob.glob('app/lib/l10n/app_*.arb'):
    with open(p) as f: json.load(f)
print('ARB JSON ok')
PY
# ARB JSON ok

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/pages/settings/profile.dart app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart'
# Formatted 3 files (0 changed)

git diff --check

Targeted flutter test test/providers/capture_provider_test.dart is still blocked in the local Flutter container by dependency incompatibility (font_awesome_flutter 10.12.0 extends final IconData under Flutter 3.44) before these tests execute.

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the follow-up Codex findings and the Cubic stale localization/switch threads.

Fixes:

  • Re-enabling Background Mode now recreates nativeBleStreamConfig immediately by resolving the current device codec and calling the shared native config writer before relying on nativeBleStreamingEnabled.
  • Added preference reset setup in the new Background Mode test groups so batchModeEnabled cannot leak between tests.
  • The switch interactivity and all non-English backgroundModeUnavailable ARB translations were already fixed in the previous push; this push keeps those fixes and resolves the stale Cubic threads.

Verification:

python3 - <<'PY'
import json, glob
for p in glob.glob('app/lib/l10n/app_*.arb'):
    with open(p) as f: json.load(f)
print('ARB JSON ok')
PY
# ARB JSON ok

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/pages/settings/profile.dart app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart'
# Formatted 3 files (0 changed)

git diff --check

Targeted Flutter tests still cannot complete in the local container because font_awesome_flutter 10.12.0 is incompatible with Flutter 3.44 (IconData is final) before the provider tests execute.

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

Copy link
Copy Markdown

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: 65f9da1631

ℹ️ 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/lib/providers/capture_provider.dart Resolved
Comment thread app/lib/pages/settings/profile.dart Resolved
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the latest #8353 Codex findings.

Fixes:

  • Disabling Background Mode now preserves nativeBleStreamConfig whenever Transcribe Later / batch mode remains enabled, even if no compatible route is currently live. This keeps the offline batch writer config available until reconnect/setup refreshes it.
  • Updated the regression test to cover the no-live-route batch preservation case.
  • Ran flutter gen-l10n and committed the generated app_localizations*.dart files so context.l10n.backgroundModeUnavailable is available to analyze/build/test flows.

Verification:

docker run --rm -v "$PWD":/repo -w /repo/app ghcr.io/cirruslabs/flutter:stable bash -lc 'flutter pub get >/tmp/pub.log && flutter gen-l10n'

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/pages/settings/profile.dart app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart app/lib/l10n/app_localizations*.dart'
# Formatted 53 files (0 changed)

python3 - <<'PY'
import json, glob
for p in glob.glob('app/lib/l10n/app_*.arb'):
    with open(p) as f: json.load(f)
print('ARB JSON ok')
PY
# ARB JSON ok

git diff --check

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

Copy link
Copy Markdown

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: 288c76def8

ℹ️ 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/lib/providers/capture_provider.dart Resolved
@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the latest foreground-ready review.

Fix:

  • When enabling Background Mode while foreground BLE streaming is already active, the provider now captures the previous nativeBleForegroundReady value before rewriting nativeBleStreamConfig and restores it afterward. This prevents native background streaming from opening a duplicate websocket while Flutter's foreground socket is still active.
  • Added regression coverage for enabling Background Mode with foreground streaming already marked ready.
  • Tightened the new preference test setup to reset native BLE prefs/config between tests.

Verification:

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart app/lib/l10n/app_localizations*.dart'
# Formatted 52 files (0 changed)

python3 - <<'PY'
import json, glob
for p in glob.glob('app/lib/l10n/app_*.arb'):
    with open(p) as f: json.load(f)
print('ARB JSON ok')
PY
# ARB JSON ok

git diff --check

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the latest foreground-ready review comment.

Fix:

  • setBackgroundModeEnabled(true) now snapshots nativeBleForegroundReady before rewriting the native BLE config and restores it afterwards when foreground streaming was already active.
  • Added regression coverage for enabling Background Mode while foreground BLE streaming is already marked ready, plus reset setup for native BLE prefs between the background-mode tests.

Verification:

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/lib/providers/capture_provider.dart app/test/providers/capture_provider_test.dart'
# Formatted 2 files (0 changed)

I also attempted the targeted Flutter test for the new case, but the current ghcr.io/cirruslabs/flutter:stable toolchain fails before loading this test file because font_awesome_flutter-10.12.0 extends Flutter's now-final IconData class. CI for this PR currently only enforces Dart formatting for app changes.

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

Copy link
Copy Markdown

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: 9bf2e9460b

ℹ️ 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".

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the accepted-enable test setup feedback.

Fix:

  • capture_provider_test.dart now initializes SharedPreferencesUtil after setting mock preferences.
  • The setBackgroundModeEnabled group resets and re-initializes preferences per test, so the valid-route enable cases don't depend on leaked singleton state.
  • Registered a path_provider method-channel mock for getApplicationDocumentsDirectory, which _saveNativeBleStreamConfig() needs when accepted-enable tests regenerate native BLE config.

Verification:

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/test/providers/capture_provider_test.dart'
# Formatted 1 file (0 changed)

git diff --check

@Git-on-my-level

Copy link
Copy Markdown
Member Author

Addressed the test setup review comment.

Fix:

  • capture_provider_test.dart now initializes SharedPreferencesUtil after setting mock preferences.
  • Registered a path_provider method-channel mock for getApplicationDocumentsDirectory(), so accepted-enable tests can exercise _saveNativeBleStreamConfig() without falling through to an unmocked platform call.
  • The background-mode test group resets mock prefs and re-initializes SharedPreferencesUtil between cases.

Verification:

docker run --rm -v "$PWD":/repo -w /repo ghcr.io/cirruslabs/flutter:stable bash -lc 'cd app && flutter pub get >/tmp/pub.log && cd .. && dart format --line-length 120 --set-exit-if-changed --output=none app/test/providers/capture_provider_test.dart'
# Formatted 1 file (0 changed)

@Git-on-my-level
Git-on-my-level merged commit 53ccdfa into BasedHardware:main Jun 25, 2026
3 checks passed
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.

Background transcription drops audio for non-Omi/OpenGlass/FriendPendant devices when app is closed

2 participants