diff --git a/android/app/src/main/kotlin/com/meshcore/meshcore_open/MainActivity.kt b/android/app/src/main/kotlin/com/meshcore/meshcore_open/MainActivity.kt index 62c4c316..ac104e12 100644 --- a/android/app/src/main/kotlin/com/meshcore/meshcore_open/MainActivity.kt +++ b/android/app/src/main/kotlin/com/meshcore/meshcore_open/MainActivity.kt @@ -13,6 +13,8 @@ class MainActivity : FlutterActivity() { } override fun onDestroy() { + // A rotation recreates the activity; only a real exit closes the app. + if (!isChangingConfigurations) UnreadWidget.markAppClosed(applicationContext) usbFunctions.dispose() super.onDestroy() } diff --git a/android/app/src/main/kotlin/com/meshcore/meshcore_open/UnreadWidget.kt b/android/app/src/main/kotlin/com/meshcore/meshcore_open/UnreadWidget.kt index ac115656..eb6d040f 100644 --- a/android/app/src/main/kotlin/com/meshcore/meshcore_open/UnreadWidget.kt +++ b/android/app/src/main/kotlin/com/meshcore/meshcore_open/UnreadWidget.kt @@ -15,6 +15,11 @@ import io.flutter.plugin.common.MethodChannel * messages and the companion's battery, terminal style. Fed by * lib/services/home_widget_bridge.dart; the last values live in * SharedPreferences so the widget survives the app being closed. + * + * Third line: "bt 87%" while connected, "cmp off" when the companion is + * disconnected, "app off" when the app is closed - on MainActivity.onDestroy, + * or when its 5-minute heartbeat has been missing for 15 minutes (killed by + * the system; the widget's 30-minute update period notices that). */ class UnreadWidget : AppWidgetProvider() { override fun onUpdate( @@ -34,6 +39,8 @@ class UnreadWidget : AppWidgetProvider() { private const val INK = 0xFFD4D4D4.toInt() private const val DIM = 0xFF6B6B6B.toInt() private const val RED = 0xFFEF4444.toInt() + private const val AMBER = 0xFFF59E0B.toInt() + private const val STALE_MS = 15 * 60 * 1000L fun configureFlutterEngine(context: Context, engine: FlutterEngine) { MethodChannel(engine.dartExecutor.binaryMessenger, CHANNEL) @@ -51,12 +58,23 @@ class UnreadWidget : AppWidgetProvider() { "connected", call.argument("connected") ?: false, ) + .putBoolean("running", true) + .putLong("seen", System.currentTimeMillis()) .apply() refresh(context) result.success(null) } } + /** The app is going away: show it on the widget right now. */ + fun markAppClosed(context: Context) { + context.getSharedPreferences(PREFS, Context.MODE_PRIVATE) + .edit() + .putBoolean("running", false) + .apply() + refresh(context) + } + private fun refresh(context: Context) { val manager = AppWidgetManager.getInstance(context) val ids = manager.getAppWidgetIds( @@ -73,24 +91,26 @@ class UnreadWidget : AppWidgetProvider() { val channels = prefs.getInt("channels", 0) val battery = prefs.getInt("battery", -1) val connected = prefs.getBoolean("connected", false) + val appAlive = prefs.getBoolean("running", false) && + System.currentTimeMillis() - prefs.getLong("seen", 0) < STALE_MS val views = RemoteViews(context.packageName, R.layout.unread_widget) views.setTextViewText(R.id.widget_dm, "dm ${count(dm)}") - views.setTextColor(R.id.widget_dm, if (dm > 0) GREEN else DIM) + views.setTextColor(R.id.widget_dm, if (dm > 0 && appAlive) GREEN else DIM) views.setTextViewText(R.id.widget_channels, "ch ${count(channels)}") - views.setTextColor(R.id.widget_channels, if (channels > 0) GREEN else DIM) - views.setTextViewText( - R.id.widget_battery, - if (battery < 0) "bt --" else "bt $battery%", - ) views.setTextColor( - R.id.widget_battery, - when { - !connected || battery < 0 -> DIM - battery <= 20 -> RED - else -> INK - }, + R.id.widget_channels, + if (channels > 0 && appAlive) GREEN else DIM, ) + val (status, color) = when { + !appAlive -> "app off" to RED + !connected -> "cmp off" to AMBER + battery < 0 -> "bt --" to DIM + battery <= 20 -> "bt $battery%" to RED + else -> "bt $battery%" to INK + } + views.setTextViewText(R.id.widget_battery, status) + views.setTextColor(R.id.widget_battery, color) val launch = context.packageManager .getLaunchIntentForPackage(context.packageName) diff --git a/android/app/src/main/res/layout/unread_widget.xml b/android/app/src/main/res/layout/unread_widget.xml index 6321e6be..482a71ce 100644 --- a/android/app/src/main/res/layout/unread_widget.xml +++ b/android/app/src/main/res/layout/unread_widget.xml @@ -18,7 +18,7 @@ android:maxLines="1" android:text="dm 0" android:textColor="#FF6B6B6B" - android:textSize="13sp" /> + android:textSize="12sp" /> + android:textSize="12sp" /> + android:textSize="12sp" /> diff --git a/android/app/src/main/res/xml/unread_widget_info.xml b/android/app/src/main/res/xml/unread_widget_info.xml index 567d1966..02aef2b6 100644 --- a/android/app/src/main/res/xml/unread_widget_info.xml +++ b/android/app/src/main/res/xml/unread_widget_info.xml @@ -8,5 +8,5 @@ android:resizeMode="horizontal|vertical" android:targetCellWidth="1" android:targetCellHeight="1" - android:updatePeriodMillis="0" + android:updatePeriodMillis="1800000" android:widgetCategory="home_screen" /> diff --git a/lib/services/home_widget_bridge.dart b/lib/services/home_widget_bridge.dart index f101d663..b3ae4c49 100644 --- a/lib/services/home_widget_bridge.dart +++ b/lib/services/home_widget_bridge.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:flutter/services.dart'; import '../connector/meshcore_connector.dart'; @@ -6,23 +8,27 @@ import '../utils/platform_info.dart'; /// tth fork: feeds the 1x1 Android home-screen widget (UnreadWidget.kt) with /// unread direct messages, unread channel messages and the companion's -/// battery. Pushes only when one of those changes - the connector notifies -/// on every frame. +/// battery. Pushes when one of those changes - the connector notifies on +/// every frame - plus a heartbeat, so the widget can tell an app that was +/// killed without a goodbye (no heartbeat for 15 min) from a quiet one. class HomeWidgetBridge { static const _channel = MethodChannel('meshcore_open/home_widget'); final MeshCoreConnector _connector; ({int dm, int channels, int? battery, bool connected})? _last; + static const _heartbeat = Duration(minutes: 5); + HomeWidgetBridge(this._connector); void start() { if (!PlatformInfo.isAndroid) return; _connector.addListener(_onChanged); + Timer.periodic(_heartbeat, (_) => _onChanged(force: true)); _onChanged(); } - void _onChanged() { + void _onChanged({bool force = false}) { final connected = _connector.isConnected; final state = ( dm: _connector.getTotalContactsUnreadCount(), @@ -31,7 +37,7 @@ class HomeWidgetBridge { battery: _connector.batteryPercent ?? _last?.battery, connected: connected, ); - if (state == _last) return; + if (!force && state == _last) return; _last = state; _channel .invokeMethod('update', {