diff --git a/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt b/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt index 6b9dd45..14a17ad 100644 --- a/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt +++ b/stronglift_replacement/workout_app/android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt @@ -1,11 +1,43 @@ package com.kuhy.workout_app +import android.content.Intent +import android.os.Bundle import android.util.Log +import com.pravera.flutter_foreground_task.service.ForegroundService import io.flutter.embedding.android.FlutterActivity import io.flutter.embedding.engine.FlutterEngine import io.flutter.plugin.common.MethodChannel class MainActivity : FlutterActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + relayNotificationPress(intent) + } + + override fun onNewIntent(intent: Intent) { + super.onNewIntent(intent) + relayNotificationPress(intent) + } + + // flutter_foreground_task only tells the service a notification was tapped + // when the opening intent is MAIN + LAUNCHER. The sandbox flavor has no + // LAUNCHER filter (it must stay out of the launcher), so its press would + // be dropped and the run notification could never open the finish modal. + // Hand such a press to the plugin as if it were a LAUNCHER intent. A real + // LAUNCHER intent was already handled by the plugin: not relayed twice. + private fun relayNotificationPress(opened: Intent?) { + if (opened == null || + opened.getStringExtra(PRESS_EXTRA) != PRESS_ACTION || + opened.hasCategory(Intent.CATEGORY_LAUNCHER) + ) { + return + } + Log.i(LOG_TAG, "notification press relayed") + ForegroundService.handleNotificationContentIntent( + Intent(opened).addCategory(Intent.CATEGORY_LAUNCHER), + ) + } + override fun configureFlutterEngine(flutterEngine: FlutterEngine) { super.configureFlutterEngine(flutterEngine) // The sandbox flavor is a build-time fact; Dart asks for it once at @@ -50,5 +82,9 @@ class MainActivity : FlutterActivity() { const val PAIRING_CHANNEL = "com.kuhy.workout_app/pc_pairing" const val CHANNEL = "com.kuhy.workout_app/sandbox" const val LOG_TAG = "WorkoutSandbox" + + // The extra the plugin puts on its notification's launch intent. + private const val PRESS_EXTRA = "intentData" + private const val PRESS_ACTION = "onNotificationPressed" } } diff --git a/stronglift_replacement/workout_app/test/sandbox/notification_press_relay_test.dart b/stronglift_replacement/workout_app/test/sandbox/notification_press_relay_test.dart new file mode 100644 index 0000000..00971aa --- /dev/null +++ b/stronglift_replacement/workout_app/test/sandbox/notification_press_relay_test.dart @@ -0,0 +1,41 @@ +// Tapping the run notification must reach the run host, in every flavor. +// +// flutter_foreground_task 11.0.3 only calls the task handler's +// onNotificationPressed when the intent that opened the activity is MAIN + +// category LAUNCHER (ForegroundService.handleNotificationContentIntent). The +// sandbox flavor has no LAUNCHER filter (see sandbox_manifest_test.dart), so +// `getLaunchIntentForPackage` hands the notification a MAIN + INFO intent and +// the plugin silently drops the press: the app opened on the plain run screen, +// never on the finish modal. MainActivity therefore relays such presses +// itself, for the cold start (onCreate) and for a running app (onNewIntent). +import 'dart:io'; + +import 'package:flutter_test/flutter_test.dart'; + +/// Relative to the package root, which is `flutter test`'s cwd. +const _activityPath = + 'android/app/src/main/kotlin/com/kuhy/workout_app/MainActivity.kt'; + +void main() { + late String activity; + + setUpAll(() => activity = File(_activityPath).readAsStringSync()); + + test('the activity hands a press the plugin ignored to the plugin', () { + expect( + activity, + contains('ForegroundService.handleNotificationContentIntent'), + ); + expect(activity, contains('Intent.CATEGORY_LAUNCHER')); + expect(activity, contains('"onNotificationPressed"')); + }); + + test('both a cold start and a running app are covered', () { + expect(activity, contains('override fun onCreate')); + expect(activity, contains('override fun onNewIntent')); + }); + + test('a press the plugin already handled is not relayed twice', () { + expect(activity, contains('hasCategory(Intent.CATEGORY_LAUNCHER)')); + }); +} diff --git a/stronglift_replacement/workout_app/test/screens/run_screen_cold_prompt_test.dart b/stronglift_replacement/workout_app/test/screens/run_screen_cold_prompt_test.dart new file mode 100644 index 0000000..f1492d8 --- /dev/null +++ b/stronglift_replacement/workout_app/test/screens/run_screen_cold_prompt_test.dart @@ -0,0 +1,65 @@ +// A notification tap on standing advice that lands while no screen is +// listening (a cold start: the UI process was dead) still opens the finish +// modal once the screen attaches and asks the host where the run stands. +import 'package:flutter_test/flutter_test.dart'; +import 'package:material_ui/material_ui.dart'; +import 'package:shared_preferences_platform_interface/in_memory_shared_preferences_async.dart'; +import 'package:shared_preferences_platform_interface/shared_preferences_async_platform_interface.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart'; +import 'package:workout_app/services/run_phase.dart'; +import 'package:workout_app/services/storage_service.dart'; +import 'package:workout_app/ui/theme.dart'; + +import '../fake_audio_platform.dart'; +import '../run_persist_rig.dart'; +import '_run_screen_helpers.dart'; + +void main() { + setUpAll(() { + sqfliteFfiInit(); + databaseFactory = databaseFactoryFfi; + }); + + setUp(() async { + installFakeAudioPlatform(); + SharedPreferencesAsyncPlatform.instance = + InMemorySharedPreferencesAsync.empty(); + StorageService.resetForTesting(); + await StorageService.init(); + }); + + testWidgets('a press before the screen exists shows the modal on attach', ( + tester, + ) async { + final rig = PersistRig(); + await rig.host.cmd('start'); + rig.host.lastHooks.onSuggest('gpsRestored'); + // The plugin reaches the service while the UI process is still starting. + rig.host.host.notificationPressed(); + + // Not rig.pump: its pumpAndSettle never ends while the pulse repeats. + await tester.pumpWidget( + MaterialApp( + navigatorKey: rig.navigator, + theme: buildAppTheme(), + home: const SizedBox(), + ), + ); + rig.navigator.currentState!.push( + MaterialPageRoute(builder: (_) => rig.screen(resuming: true)), + ); + for (var i = 0; i < 6; i++) { + await tester.pump(const Duration(milliseconds: 100)); + } + + expect(key('run_finish_dialog'), findsOneWidget); + expect(rig.engine.phase, RunPhase.recording); + expect(key('run_finish_pulse'), findsOneWidget); + + await tester.tap(key('run_finish_cancel')); + await tester.pump(); + await tester.pump(const Duration(seconds: 1)); + expect(rig.engine.phase, RunPhase.recording); + expect(key('run_finish_dialog'), findsNothing); + }); +}