LifeMate — Personal, Family & Learning Companion
fix(android): expose personal reminder sound controls safely
Quinn White committed
Oct 21, 2026 at 04:40 UTC
49702e8dc0a14bcaf4e7e5c53a0f0878fbb19f0b
6 files changed
+269
-2
apps/lifemate/android/app/src/main/kotlin/ir/lifeguide/app/DevicePlannerReminders.kt
+5
@@ -122,6 +122,11 @@ internal class DevicePlannerReminderChannel(private val activity: Activity) : Me
122
activity.requestPermissions(arrayOf(Manifest.permission.POST_NOTIFICATIONS), REQUEST)
123
} else result.success(DevicePlannerReminders.status(activity))
124
}
125
+ "openNotificationSettings" -> {
126
+ require(call.arguments == null)
127
+ LocalReminders.openNotificationSettings(activity)
128
+ result.success(true)
129
+ }
130
"requestExactAlarmAccess" -> {
131
if (Build.VERSION.SDK_INT >= 31 && !LocalReminders.exact(activity)) {
132
activity.startActivity(Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, Uri.parse("package:${activity.packageName}")))
apps/lifemate/android/app/src/main/kotlin/ir/lifeguide/app/LocalReminders.kt
+13
@@ -69,6 +69,19 @@ internal object LocalReminders {
69
setSound(Settings.System.DEFAULT_NOTIFICATION_URI, null)
70
})
71
}
72
+ // Explicit user navigation only: no permission, consent or alarm mutation.
73
+ fun openNotificationSettings(activity: Activity) {
74
+ channel(activity)
75
+ val details = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
76
+ Uri.parse("package:${activity.packageName}"))
77
+ val target = if (Build.VERSION.SDK_INT >= 26) {
78
+ Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
79
+ .putExtra(Settings.EXTRA_APP_PACKAGE, activity.packageName)
80
+ .putExtra(Settings.EXTRA_CHANNEL_ID, CHANNEL)
81
+ } else details
82
+ try { activity.startActivity(target) }
83
+ catch (_: android.content.ActivityNotFoundException) { activity.startActivity(details) }
84
+ }
85
private fun pending(c: Context, generation: String, id: String) = PendingIntent.getBroadcast(c, 0,
86
Intent(c, LocalReminderReceiver::class.java).apply {
87
action = ACTION
apps/lifemate/lib/standalone/reminder_coordinator.dart
+15
@@ -107,5 +107,20 @@ class StandaloneReminderCoordinator extends ChangeNotifier {
107
await refresh(store);
108
});
109
110
+ /// A settings-screen failure is not a storage failure. Keep the existing
111
+ /// choice and schedule; lifecycle resume performs the authoritative refresh.
112
+ Future<void> openNotificationSettings() async {
113
+ try {
114
+ await _port.openNotificationSettings();
115
+ error = null;
116
+ } catch (_) {
117
+ error =
118
+ 'تنظیمات اعلان باز نشد. از تنظیمات گوشی، بخش اعلانهای لایفگاید را باز کن.';
119
+ rethrow;
120
+ } finally {
121
+ notifyListeners();
122
+ }
123
+ }
124
+
125
Future<void> cancelOnStorageFailure() => _port.cancelOnStorageFailure();
126
}
apps/lifemate/lib/standalone/reminder_settings.dart
+14
-2
@@ -60,7 +60,7 @@ class _StandaloneReminderSettingsState extends State<StandaloneReminderSettings>
60
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold)),
61
const SizedBox(height: 12),
62
const Text(
63
- 'با اجازهٔ تو، نام همراهت و یک پیام عمومی نمایش داده میشود. عنوان خصوصی برنامه روی اعلان نمیآید. اینترنت و سرور لازم نیست.'),
63
+ 'وقتی زمان برنامهات برسد، همراهت یادت میاندازد. با اجازهٔ تو، نام همراه و یک پیام عمومی نمایش داده میشود؛ عنوان خصوصی برنامه روی اعلان نمیآید. اینترنت و سرور لازم نیست.'),
64
const SizedBox(height: 16),
65
if (widget.coordinator.error != null)
66
Card(
@@ -87,7 +87,19 @@ class _StandaloneReminderSettingsState extends State<StandaloneReminderSettings>
87
),
88
Text(state.permissionGranted
89
? 'مجوز اعلان: داده شده'
90
- : 'مجوز اعلان: داده نشده؛ برای اجازه دادن، اعلان را روشن کن.'),
90
+ : 'مجوز اعلان: داده نشده. اجازهٔ اعلان را از تنظیمات اندروید بررسی کن.'),
91
+ const SizedBox(height: 12),
92
+ OutlinedButton.icon(
93
+ key: const Key('personal-notification-system-settings'),
94
+ style:
95
+ OutlinedButton.styleFrom(minimumSize: const Size(48, 48)),
96
+ onPressed: _busy
97
+ ? null
98
+ : () => _run(widget.coordinator.openNotificationSettings),
99
+ icon: const Icon(Icons.volume_up_outlined),
100
+ label: const Text('تنظیم صدا و اعلان در اندروید')),
101
+ const Text(
102
+ 'صدایی را انتخاب کن که دوست داری. تنظیم صدا و لرزش در اندروید انجام میشود؛ پس از برگشت، اجازهٔ اعلان دوباره بررسی میشود.'),
103
const SizedBox(height: 12),
104
Text('یادآوری آیندهٔ زمانبندیشده: ${state.scheduledCount}'),
105
Text(state.timing == 'exact'
apps/lifemate/lib/standalone/reminders.dart
+7
@@ -326,6 +326,13 @@ final class StandaloneReminderPort {
326
return _serial(_deactivate);
327
}
328
329
+ /// Opens Android's controls only; it never grants consent or reschedules.
330
+ Future<void> openNotificationSettings() async {
331
+ if (!_native)
332
+ throw UnsupportedError('Android notification settings unavailable.');
333
+ await _write('openNotificationSettings');
334
+ }
335
+
336
Future<void> cancelOnStorageFailure({String? profileId}) =>
337
suspendUntilReconcile(profileId: profileId);
338
apps/lifemate/test/standalone_reminder_settings_test.dart
new
+215
@@ -0,0 +1,215 @@
1
+import 'dart:io';
2
+
3
+import 'package:flutter/foundation.dart';
4
+import 'package:flutter/material.dart';
5
+import 'package:flutter/services.dart';
6
+import 'package:flutter_localizations/flutter_localizations.dart';
7
+import 'package:flutter_test/flutter_test.dart';
8
+import 'package:lifemate/standalone/model.dart';
9
+import 'package:lifemate/standalone/reminder_coordinator.dart';
10
+import 'package:lifemate/standalone/reminder_settings.dart';
11
+import 'package:lifemate/standalone/reminders.dart';
12
+import 'package:lifemate/standalone/store.dart';
13
+import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart';
14
+import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart';
15
+import 'package:sqflite_common_ffi/sqflite_ffi.dart';
16
+
17
+void main() {
18
+ const channel = MethodChannel('lifeguide/device_planner_reminders');
19
+ final now = DateTime(2026, 10, 21, 8);
20
+ late Directory directory;
21
+ late StandaloneStore store;
22
+ late StandaloneReminderCoordinator coordinator;
23
+ final methods = <String>[];
24
+ bool enabled = false, active = false, failSettings = false;
25
+ bool permissionGranted = true;
26
+ int scheduled = 0;
27
+
28
+ setUpAll(() async {
29
+ sqfliteFfiInit();
30
+ await (FontLoader('LifeGuidePersian')
31
+ ..addFont(rootBundle.load('assets/fonts/LifeGuidePersian.ttf')))
32
+ .load();
33
+ });
34
+ setUp(() async {
35
+ SharedPreferencesAsyncPlatform.instance =
36
+ InMemorySharedPreferencesAsync.empty();
37
+ enabled = false;
38
+ active = false;
39
+ failSettings = false;
40
+ permissionGranted = true;
41
+ scheduled = 0;
42
+ methods.clear();
43
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
44
+ .setMockMethodCallHandler(channel, (call) async {
45
+ methods.add(call.method);
46
+ if (call.method == 'openNotificationSettings' && failSettings) {
47
+ throw PlatformException(code: 'settings_unavailable');
48
+ }
49
+ if (call.method == 'setEnabled')
50
+ enabled = (call.arguments as Map)['enabled'] == true;
51
+ if (call.method == 'activate') active = true;
52
+ if (call.method == 'deactivate') {
53
+ active = false;
54
+ scheduled = 0;
55
+ }
56
+ if (call.method == 'replace')
57
+ scheduled = ((call.arguments as Map)['reminders'] as List).length;
58
+ if (['status', 'requestPermission', 'requestExactAlarmAccess']
59
+ .contains(call.method)) {
60
+ return {
61
+ 'supported': true,
62
+ 'enabled': enabled,
63
+ 'permissionGranted': permissionGranted,
64
+ 'active': active,
65
+ 'scheduledCount': scheduled,
66
+ 'timing': 'approximate'
67
+ };
68
+ }
69
+ return true;
70
+ });
71
+ directory = await Directory.systemTemp.createTemp('lifeguide-reminder-ui-');
72
+ store = await StandaloneStore.open(
73
+ path: '${directory.path}/planner.sqlite',
74
+ factory: databaseFactoryFfiNoIsolate);
75
+ await store.createProfile(
76
+ displayName: 'نمونه ساختگی',
77
+ category: StandaloneProfileCategory.girlMinor);
78
+ await store.createPlan(StandalonePlanDraft(
79
+ title: 'برنامهٔ ساختگی',
80
+ kind: StandalonePlanKind.task,
81
+ recurrence: StandaloneRecurrence.once,
82
+ startDate: now,
83
+ startMinute: 600,
84
+ endMinute: 660,
85
+ reminderMinutes: 5));
86
+ coordinator = StandaloneReminderCoordinator(
87
+ port: StandaloneReminderPort(now: () => now), now: () => now);
88
+ });
89
+ tearDown(() async {
90
+ coordinator.dispose();
91
+ await store.close();
92
+ await directory.delete(recursive: true);
93
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
94
+ .setMockMethodCallHandler(channel, null);
95
+ });
96
+ Future<void> idle(WidgetTester tester) async {
97
+ for (var i = 0; i < 100; i++) {
98
+ await Future<void>.delayed(const Duration(milliseconds: 10));
99
+ await tester.pumpAndSettle();
100
+ if (find.byType(LinearProgressIndicator).evaluate().isEmpty &&
101
+ coordinator.status != null) return;
102
+ }
103
+ fail('Reminder settings never finished reading actual SQLite.');
104
+ }
105
+
106
+ Future<void> open(WidgetTester tester, {double scale = 1}) async {
107
+ await tester.pumpWidget(MaterialApp(
108
+ theme: ThemeData(fontFamily: 'LifeGuidePersian'),
109
+ locale: const Locale('fa'),
110
+ supportedLocales: const [Locale('fa')],
111
+ localizationsDelegates: const [
112
+ GlobalMaterialLocalizations.delegate,
113
+ GlobalWidgetsLocalizations.delegate,
114
+ GlobalCupertinoLocalizations.delegate,
115
+ ],
116
+ builder: (context, child) => MediaQuery(
117
+ data: MediaQuery.of(context)
118
+ .copyWith(textScaler: TextScaler.linear(scale)),
119
+ child: child!),
120
+ home: StandaloneReminderSettings(
121
+ store: store, coordinator: coordinator)));
122
+ await idle(tester);
123
+ }
124
+
125
+ Future<void> openSystemSettings(WidgetTester tester) async {
126
+ final button =
127
+ find.byKey(const Key('personal-notification-system-settings'));
128
+ expect(button, findsOneWidget);
129
+ await tester.ensureVisible(button);
130
+ await tester.tap(button);
131
+ await tester.pumpAndSettle();
132
+ await Future<void>.delayed(const Duration(milliseconds: 20));
133
+ await tester.pumpAndSettle();
134
+ }
135
+
136
+ testWidgets(
137
+ 'system notification settings never opts in or changes saved plans',
138
+ (tester) async {
139
+ await tester.runAsync(() async {
140
+ await open(tester);
141
+ final before = (await store.listPlans()).single;
142
+ methods.clear();
143
+ await openSystemSettings(tester);
144
+ expect(methods, ['openNotificationSettings']);
145
+ expect(enabled, isFalse);
146
+ expect(coordinator.status!.enabled, isFalse);
147
+ expect((await store.listPlans()).single.id, before.id);
148
+ expect(tester.takeException(), isNull);
149
+ });
150
+ }, variant: TargetPlatformVariant.only(TargetPlatform.android));
151
+ testWidgets('failed settings intent leaves existing reminder schedule intact',
152
+ (tester) async {
153
+ await tester.runAsync(() async {
154
+ await coordinator.enable(store);
155
+ await open(tester);
156
+ final before = coordinator.status!;
157
+ expect(scheduled, 1);
158
+ failSettings = true;
159
+ methods.clear();
160
+ await openSystemSettings(tester);
161
+ expect(methods, ['openNotificationSettings']);
162
+ expect(enabled, isTrue);
163
+ expect(active, isTrue);
164
+ expect(scheduled, 1);
165
+ expect(coordinator.status, same(before));
166
+ expect(find.textContaining('تنظیمات اعلان باز نشد'), findsOneWidget);
167
+ expect(tester.takeException(), isNull);
168
+ });
169
+ }, variant: TargetPlatformVariant.only(TargetPlatform.android));
170
+
171
+ testWidgets('return from Android settings rereads permission and stays OFF',
172
+ (tester) async {
173
+ await tester.runAsync(() async {
174
+ await open(tester);
175
+ await openSystemSettings(tester);
176
+ methods.clear();
177
+ permissionGranted = false;
178
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
179
+ tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.resumed);
180
+ await idle(tester);
181
+ expect(methods, contains('status'));
182
+ expect(methods, isNot(contains('requestPermission')));
183
+ expect(methods, isNot(contains('setEnabled')));
184
+ expect(methods, isNot(contains('activate')));
185
+ expect(coordinator.status!.enabled, isFalse);
186
+ expect(coordinator.status!.permissionGranted, isFalse);
187
+ expect(find.textContaining('مجوز اعلان: داده نشده'), findsOneWidget);
188
+ expect((await store.listPlans()), hasLength(1));
189
+ expect(tester.takeException(), isNull);
190
+ });
191
+ }, variant: TargetPlatformVariant.only(TargetPlatform.android));
192
+
193
+ testWidgets(
194
+ 'sound settings is usable at width 320 and text scale 200 percent',
195
+ (tester) async {
196
+ tester.view.devicePixelRatio = 1;
197
+ tester.view.physicalSize = const Size(320, 800);
198
+ addTearDown(tester.view.resetPhysicalSize);
199
+ addTearDown(tester.view.resetDevicePixelRatio);
200
+ await tester.runAsync(() async {
201
+ await open(tester, scale: 2);
202
+ final button =
203
+ find.byKey(const Key('personal-notification-system-settings'));
204
+ await tester.ensureVisible(button);
205
+ final size = tester.getSize(button);
206
+ expect(size.width, greaterThanOrEqualTo(48));
207
+ expect(size.height, greaterThanOrEqualTo(48));
208
+ expect(size.width, lessThanOrEqualTo(320));
209
+ methods.clear();
210
+ await openSystemSettings(tester);
211
+ expect(methods, ['openNotificationSettings']);
212
+ expect(tester.takeException(), isNull);
213
+ });
214
+ }, variant: TargetPlatformVariant.only(TargetPlatform.android));
215
+}