8000 Fix sensor warmup, scan recovery, and live glucose updates by shroominic · Pull Request #35 · shroominic/OpenGlucose · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,24 @@ expectations are defined in [docs/compatibility.md](docs/compatibility.md).

## [Unreleased]

### Changed

- During the sensor's initial 60-minute warmup, the dashboard now hides
History, Patterns, and Weekly recap. Warmup readings remain retained for a
complete disclosed archive export but are excluded from displayed history,
wellness analytics, and Apple Health export.

### Fixed

- Android live notifications and iOS Live Activities can again show ongoing
post-warmup glucose updates after the user explicitly opts in from current
sensor settings; the default remains redacted and disabling fails closed.
- iOS Live Activities now start during sensor warmup and continue showing the
private countdown until the first post-warmup reading is available.
- Sensor discovery now presents a safe Bluetooth-off state with enable and
retry guidance instead of exposing native exception types or an unrelated
no-sensors message.

## [0.1.1] - 2026-08-14

### Fixed
Expand Down
8 changes: 6 additions & 2 deletions docs/privacy/data-handling.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ as a private or supported health-data retention surface.
Lock-screen implementations default to redacted glucose on Android and iOS.
On iOS launch, any pre-upgrade Live Activity is ended when sensitive display has
not been explicitly enabled, preventing old unredacted state from surviving an
upgrade. Final device verification remains outstanding. There is intentionally
no user opt-in UI yet; adding one is a privacy-triggered feature.
upgrade. The current-sensor settings provide an explicit opt-in for showing
glucose, trend, and update time in the Android live notification or iOS Live
Activity, with a warning that anyone who can view the lock screen may see the
health data. Disabling the setting rebuilds or removes the existing surface
before a redacted update is published. Final device verification remains
outstanding.

## Retention and deletion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class GlucoseLiveUpdateService extends Service {
private static final String CHANNEL_NAME = "Live glucose";
static final String PREF_LAST_PAYLOAD = "last_payload";
private static final String EXTRA_PAYLOAD = "payload";
private static final int NOTIFICATION_ID = 64102;
static final int NOTIFICATION_ID = 64102;
private static final int COLOR_TEAL = 0xFF2E7D74;

@Override
Expand Down Expand Up @@ -126,11 +126,10 @@ private Notification buildNotification(Map<String, Object> payload) {
final String trendSymbol = stringValue(payload, "trendSymbol", "");
final String deltaText = stringValue(payload, "deltaText", "");

// Fail closed: the app has no opt-in UI yet, so this preference is false
// unless a future reviewed settings flow deliberately records consent.
// Fail closed until the reviewed Flutter settings flow records consent.
final SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
if (!preferences.getBoolean(PREF_SENSITIVE_LOCK_SCREEN_OPT_IN, false)) {
return buildRedactedNotification(stageLabel);
return buildRedactedNotification(payload);
}

final boolean hasValue = valueText != null && !"--".equals(valueText) && !valueText.isEmpty();
Expand All @@ -141,7 +140,7 @@ private Notification buildNotification(Map<String, Object> payload) {
: fallbackDetail(stageLabel, lastReadingText);
final String subText = hasValue ? sensorName : stageLabel;
final String bigText = buildBigText(sensorName, text, trendSymbol, deltaText);
final Notification publicVersion = buildRedactedNotification(stageLabel);
final Notification publicVersion = buildRedactedNotification(payload);

final Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Expand Down Expand Up @@ -201,9 +200,49 @@ static Notification buildRedactedNotificationForTest(
.build();
}

private Notification buildRedactedNotification(String stageLabel) {
private Notification buildRedactedNotification(Map<String, Object> payload) {
final Integer remainingMinutes = validatedWarmupMinutes(payload);
if (remainingMinutes != null) {
final Notification.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new Notification.Builder(this, CHANNEL_ID);
} else {
builder = new Notification.Builder(this);
}
return builder
.setSmallIcon(R.drawable.ic_glucose_notification)
.setContentTitle(remainingMinutes + " min")
.setContentText("Sensor warming up")
.setSubText("Warmup")
.setContentIntent(buildLaunchIntent())
.setCategory(Notification.CATEGORY_STATUS)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setOnlyAlertOnce(true)
.setShowWhen(false)
.setColor(COLOR_TEAL)
.build();
}
return buildRedactedNotificationForTest(
this, CHANNEL_ID, buildLaunchIntent(), stageLabel);
this,
CHANNEL_ID,
buildLaunchIntent(),
stringValue(payload, "stageLabel", "Sensor active"));
}

static Integer validatedWarmupMinutes(Map<String, Object> payload) {
if (!"progress".equals(stringValue(payload, "stageCode", ""))
|| !"WARMUP".equalsIgnoreCase(stringValue(payload, "stageLabel", "").trim())
|| !"min".equals(stringValue(payload, "unitText", ""))) {
return null;
}
final String valueText = stringValue(payload, "valueText", "");
try {
final int remainingMinutes = Integer.parseInt(valueText);
return remainingMinutes >= 1 && remainingMinutes <= 180 ? remainingMinutes : null;
} catch (NumberFormatException ignored) {
return null;
}
}

private PendingIntent buildLaunchIntent() {
Expand Down Expand Up @@ -325,6 +364,27 @@ static boolean clearPersistedPayload(Context context) {
return preferences.edit().remove(PREF_LAST_PAYLOAD).commit();
}

static boolean sensitiveContentEnabled(Context context) {
return context
.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
.getBoolean(PREF_SENSITIVE_LOCK_SCREEN_OPT_IN, false);
}

static boolean setSensitiveContentEnabled(Context context, boolean enabled) {
return context
.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
.edit()
.putBoolean(PREF_SENSITIVE_LOCK_SCREEN_OPT_IN, enabled)
.commit();
}

static boolean hasPersistedPayload(Context context) {
final String payload = context
.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)
.getString(PREF_LAST_PAYLOAD, null);
return payload != null && !payload.isEmpty();
}

private String buildBigText(
String sensorName, String detailText, String trendSymbol, String deltaText) {
final StringBuilder builder = new StringBuilder(sensorName);
Expand Down Expand Up @@ -353,7 +413,8 @@ private String fallbackDetail(String stageLabel, String lastReadingText) {
return stageLabel == null || stageLabel.isEmpty() ? "Waiting for glucose update" : stageLabel;
}

private String stringValue(Map<String, Object> payload, String key, String fallback) {
private static String stringValue(
Map<String, Object> payload, String key, String fallback) {
final Object value = payload.get(key);
if (value == null) {
return fallback;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aidex.aidex_flutter;

import android.Manifest;
import android.app.NotificationManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -85,6 +86,45 @@ private void handleLiveUpdateCall(MethodCall call, MethodChannel.Result result)
null);
}
return;
case "getSensitiveContentEnabled":
result.success(GlucoseLiveUpdateService.sensitiveContentEnabled(this));
return;
case "setSensitiveContentEnabled":
if (!(call.arguments instanceof Boolean)) {
result.error("bad_args", "Expected a sensitive-content boolean.", null);
return;
}
final boolean enabled = (Boolean) call.arguments;
if (!GlucoseLiveUpdateService.setSensitiveContentEnabled(this, enabled)) {
// Any failed privacy write fails closed. This also handles consent
// withdrawal: never leave an existing glucose notification visible
// merely because SharedPreferences could not persist the opt-out.
GlucoseLiveUpdateService.setSensitiveContentEnabled(this, false);
stopLiveUpdateService();
result.error(
"restricted_storage_failed",
"Could not save the live-notification privacy setting.",
null);
return;
}
if (enabled) {
// Flutter publishes the current payload only after this preference
// write succeeds. Keeping that second step separate lets Flutter
// roll consent back if publishing fails.
result.success(null);
return;
}
try {
removeVisibleLiveUpdateForPrivacy();
refreshLiveUpdateServiceIfActive();
} catch (RuntimeException error) {
// Consent has already been withdrawn. If the notification cannot
// be rebuilt redacted, remove it and its cached payload rather than
// surfacing a failure that could make Flutter restore consent.
stopLiveUpdateService();
}
result.success(null);
return;
default:
result.notImplemented();
}
Expand All @@ -101,6 +141,22 @@ private void startLiveUpdateService(Map<String, Object> payload) {
startService(intent);
}

private void refreshLiveUpdateServiceIfActive() {
if (!GlucoseLiveUpdateService.hasPersistedPayload(this)) {
return;
}
startLiveUpdateService(Collections.emptyMap());
}

private void removeVisibleLiveUpdateForPrivacy() {
stopService(new Intent(this, GlucoseLiveUpdateService.class));
final NotificationManager manager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (manager != null) {
manager.cancel(GlucoseLiveUpdateService.NOTIFICATION_ID);
}
}

private boolean stopLiveUpdateService() {
final boolean cleared = GlucoseLiveUpdateService.clearPersistedPayload(this);
stopService(new Intent(this, GlucoseLiveUpdateService.class));
Expand Down
Loading
0