Home-screen widget 1x1: unread DMs, unread channels, companion battery
build / build (push) Successful in 16m14s

A terminal-style Android widget (UnreadWidget.kt):

    dm 3
    ch 12
    bt 87%

Counts turn green when non-zero, the battery red at 20 % and grey when
the companion is disconnected (last known level kept). Tapping opens the
app. HomeWidgetBridge pushes the values over a method channel only when
they change; they are kept in SharedPreferences so the widget survives
the app being closed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-10-22 22:53:07 +02:00
1 parent b47afe7854
commit 36806e289d
8 files changed
+235

No files matched your search

+12
View File
@@ -34,6 +34,18 @@
<receiver
android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver"
android:exported="false" />
<!-- tth fork: 1x1 home-screen widget, unread messages + battery -->
<receiver
android:name=".UnreadWidget"
android:label="MeshCore unread"
android:exported="false">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/unread_widget_info" />
</receiver>
<!-- Android Auto: message notifications can be read aloud and replied to -->
<meta-data
android:name="com.google.android.gms.car.application"
@@ -9,6 +9,7 @@ class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
super.configureFlutterEngine(flutterEngine)
usbFunctions.configureFlutterEngine(flutterEngine)
UnreadWidget.configureFlutterEngine(applicationContext, flutterEngine)
}
override fun onDestroy() {
@@ -0,0 +1,114 @@
package com.meshcore.meshcore_open
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.widget.RemoteViews
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
/**
* tth fork: 1x1 home-screen widget - unread direct messages, unread channel
* 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.
*/
class UnreadWidget : AppWidgetProvider() {
override fun onUpdate(
context: Context,
manager: AppWidgetManager,
ids: IntArray,
) {
val views = render(context)
ids.forEach { manager.updateAppWidget(it, views) }
}
companion object {
private const val PREFS = "meshcore_open_unread_widget"
private const val CHANNEL = "meshcore_open/home_widget"
private const val GREEN = 0xFF4ADE80.toInt()
private const val INK = 0xFFD4D4D4.toInt()
private const val DIM = 0xFF6B6B6B.toInt()
private const val RED = 0xFFEF4444.toInt()
fun configureFlutterEngine(context: Context, engine: FlutterEngine) {
MethodChannel(engine.dartExecutor.binaryMessenger, CHANNEL)
.setMethodCallHandler { call, result ->
if (call.method != "update") {
result.notImplemented()
return@setMethodCallHandler
}
context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
.edit()
.putInt("dm", call.argument<Int>("dm") ?: 0)
.putInt("channels", call.argument<Int>("channels") ?: 0)
.putInt("battery", call.argument<Int>("battery") ?: -1)
.putBoolean(
"connected",
call.argument<Boolean>("connected") ?: false,
)
.apply()
refresh(context)
result.success(null)
}
}
private fun refresh(context: Context) {
val manager = AppWidgetManager.getInstance(context)
val ids = manager.getAppWidgetIds(
ComponentName(context, UnreadWidget::class.java),
)
if (ids.isEmpty()) return
val views = render(context)
ids.forEach { manager.updateAppWidget(it, views) }
}
private fun render(context: Context): RemoteViews {
val prefs = context.getSharedPreferences(PREFS, Context.MODE_PRIVATE)
val dm = prefs.getInt("dm", 0)
val channels = prefs.getInt("channels", 0)
val battery = prefs.getInt("battery", -1)
val connected = prefs.getBoolean("connected", false)
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.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
},
)
val launch = context.packageManager
.getLaunchIntentForPackage(context.packageName)
?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
if (launch != null) {
views.setOnClickPendingIntent(
R.id.widget_root,
PendingIntent.getActivity(
context,
0,
launch,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE,
),
)
}
return views
}
private fun count(n: Int) = if (n > 99) "99+" else n.toString()
}
}
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- tth fork: 1x1 unread / battery widget, see UnreadWidget.kt -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6000000"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingStart="6dp"
android:paddingEnd="4dp">
<TextView
android:id="@+id/widget_dm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:maxLines="1"
android:text="dm 0"
android:textColor="#FF6B6B6B"
android:textSize="13sp" />
<TextView
android:id="@+id/widget_channels"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:maxLines="1"
android:text="ch 0"
android:textColor="#FF6B6B6B"
android:textSize="13sp" />
<TextView
android:id="@+id/widget_battery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:maxLines="1"
android:text="bt --"
android:textColor="#FF6B6B6B"
android:textSize="13sp" />
</LinearLayout>
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- tth fork -->
<resources>
<string name="unread_widget_description">Unread direct and channel messages, companion battery</string>
</resources>
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- tth fork: 1x1 unread / battery widget, see UnreadWidget.kt -->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/unread_widget_description"
android:initialLayout="@layout/unread_widget"
android:minWidth="40dp"
android:minHeight="40dp"
android:resizeMode="horizontal|vertical"
android:targetCellWidth="1"
android:targetCellHeight="1"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen" />
+2
View File
@@ -23,6 +23,7 @@ import 'services/storage_service.dart';
import 'services/message_retry_service.dart';
import 'services/path_history_service.dart';
import 'services/app_settings_service.dart';
import 'services/home_widget_bridge.dart';
import 'services/network_map/network_map_upload_service.dart';
import 'services/notification_service.dart';
import 'services/ble_debug_log_service.dart';
@@ -64,6 +65,7 @@ void main() async {
// Initialize services
final storage = StorageService();
final connector = MeshCoreConnector();
HomeWidgetBridge(connector).start(); // tth fork: home-screen widget
final pathHistoryService = PathHistoryService(storage);
final retryService = MessageRetryService();
final appSettingsService = AppSettingsService();
+47
View File
@@ -0,0 +1,47 @@
import 'package:flutter/services.dart';
import '../connector/meshcore_connector.dart';
import '../utils/app_logger.dart';
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.
class HomeWidgetBridge {
static const _channel = MethodChannel('meshcore_open/home_widget');
final MeshCoreConnector _connector;
({int dm, int channels, int? battery, bool connected})? _last;
HomeWidgetBridge(this._connector);
void start() {
if (!PlatformInfo.isAndroid) return;
_connector.addListener(_onChanged);
_onChanged();
}
void _onChanged() {
final connected = _connector.isConnected;
final state = (
dm: _connector.getTotalContactsUnreadCount(),
channels: _connector.getTotalChannelsUnreadCount(),
// Keep the last known level while disconnected; the widget dims it.
battery: _connector.batteryPercent ?? _last?.battery,
connected: connected,
);
if (state == _last) return;
_last = state;
_channel
.invokeMethod<void>('update', {
'dm': state.dm,
'channels': state.channels,
'battery': state.battery ?? -1,
'connected': state.connected,
})
.catchError((Object e) {
appLogger.warn('home widget update failed: $e', noNotify: true);
});
}
}