diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1e740d6d..c117eda3 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -28,6 +28,8 @@ + + diff --git a/lib/services/screen_recording/backends/android_screen_recorder.dart b/lib/services/screen_recording/backends/android_screen_recorder.dart new file mode 100644 index 00000000..bc7a67e9 --- /dev/null +++ b/lib/services/screen_recording/backends/android_screen_recorder.dart @@ -0,0 +1,51 @@ +import 'dart:io'; + +import 'package:bike_control/services/screen_recording/screen_recording_service.dart'; +import 'package:flutter/foundation.dart'; +import 'package:flutter_screen_recording/flutter_screen_recording.dart'; +import 'package:gal/gal.dart'; + +/// Android backend: system-wide MediaProjection capture via flutter_screen_recording, +/// then the resulting mp4 is copied into the device gallery via `gal`. +class AndroidScreenRecorder implements ScreenRecorderBackend { + @override + Future isAvailable() async => true; + + @override + Future ensurePermission() async { + // MediaProjection consent is requested by startRecordScreen itself. + // Gallery save needs gallery access on older Androids; request up front. + try { + if (await Gal.hasAccess(toAlbum: true)) return true; + return await Gal.requestAccess(toAlbum: true); + } catch (e, s) { + debugPrintStack(label: 'gal access: $e', stackTrace: s); + return true; // don't block recording if the gallery check itself failed + } + } + + @override + Future start() async { + try { + // Video only — use startRecordScreen (no audio variant). + final name = 'BikeControl_${DateTime.now().millisecondsSinceEpoch}'; + return await FlutterScreenRecording.startRecordScreen(name); + } catch (e, s) { + debugPrintStack(label: 'android startRecordScreen: $e', stackTrace: s); + return false; + } + } + + @override + Future stop() async { + try { + final path = await FlutterScreenRecording.stopRecordScreen; + if (path.isEmpty || !File(path).existsSync()) return null; + await Gal.putVideo(path, album: 'BikeControl'); + return path; + } catch (e, s) { + debugPrintStack(label: 'android stopRecordScreen/gal: $e', stackTrace: s); + return null; + } + } +} diff --git a/lib/services/screen_recording/screen_recording_service.dart b/lib/services/screen_recording/screen_recording_service.dart index 6ea7b376..ba178e8b 100644 --- a/lib/services/screen_recording/screen_recording_service.dart +++ b/lib/services/screen_recording/screen_recording_service.dart @@ -1,5 +1,6 @@ import 'dart:io'; +import 'package:bike_control/services/screen_recording/backends/android_screen_recorder.dart'; import 'package:bike_control/services/screen_recording/backends/native_channel_screen_recorder.dart'; import 'package:bike_control/services/screen_recording/backends/unsupported_screen_recorder.dart'; import 'package:flutter/foundation.dart'; @@ -104,6 +105,6 @@ ScreenRecorderBackend createScreenRecorderBackend() { if (Platform.isIOS || Platform.isMacOS || Platform.isWindows) { return NativeChannelScreenRecorder(); } - // Android backend added later; Linux unsupported. + if (Platform.isAndroid) return AndroidScreenRecorder(); return UnsupportedScreenRecorder(); }