feat(interface,android): Allow passing an initial locale to the service

This commit is contained in:
PapaTutuWawa 2023-09-03 21:57:25 +02:00
parent 81e819b4a7
commit 4167227c7b
8 changed files with 25 additions and 11 deletions

View File

@ -114,7 +114,7 @@ public class BackgroundService extends Service implements MethodChannel.MethodCa
| (PendingIntent.FLAG_MUTABLE & (aboveS ? PendingIntent.FLAG_MUTABLE : 0))
);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "FOREGROUND_DEFAULT")
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "foreground_service")
.setSmallIcon(R.drawable.ic_service_icon)
.setAutoCancel(true)
.setOngoing(true)

View File

@ -52,8 +52,9 @@ Future<void> androidEntrypoint() async {
final srv = AndroidBackgroundService();
GetIt.I.registerSingleton<BackgroundService>(srv);
srv.init(
entrypoint! as Future<void> Function(),
entrypoint! as Future<void> Function(String),
handleUIEvent! as Future<void> Function(Map<String, dynamic>? data),
data['initialLocale']! as String,
);
}
@ -80,9 +81,10 @@ class AndroidIsolateHandler extends IsolateHandler {
@override
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(String initialLocale) entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
String initialLocale,
) async {
_log.finest('Called start');
WidgetsFlutterBinding.ensureInitialized();
@ -96,7 +98,8 @@ class AndroidIsolateHandler extends IsolateHandler {
'genericEntrypoint':
PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle':
PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle(),
'initialLocale': initialLocale,
}),
]);

View File

@ -24,7 +24,10 @@ class AndroidPlatformImplementation extends PlatformImplementation {
@override
Future<bool> generateVideoThumbnail(
String src, String dest, int width) async {
String src,
String dest,
int width,
) async {
return MoxplatformInterface.api.generateVideoThumbnail(src, dest, width);
}
}

View File

@ -39,8 +39,9 @@ class AndroidBackgroundService extends BackgroundService {
@override
void init(
Future<void> Function() entrypoint,
Future<void> Function(String initialLocale) entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent,
String initialLocale,
) {
WidgetsFlutterBinding.ensureInitialized();
@ -58,6 +59,6 @@ class AndroidBackgroundService extends BackgroundService {
_log.finest('Running...');
entrypoint();
entrypoint(initialLocale);
}
}

View File

@ -8,10 +8,12 @@ abstract class IsolateHandler {
/// [entrypoint] is the entrypoint that is run inside the new isolate.
/// [handleUIEvent] is a handler function that is called when the isolate receives data from the UI.
/// [handleIsolateEvent] is a handler function that is called when the UI receives data from the service.
/// [initialLocale] the locale to pass to the background service when first starting.
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(String initialLocale) entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
String initialLocale,
);
/// Make sure that the UI event handler is registered without starting the isolate.

View File

@ -22,9 +22,10 @@ class StubIsolateHandler extends IsolateHandler {
@override
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(String initialLocale) entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
String initialLocale,
) async {
// ignore: avoid_print
print('STUB STARTED!!!!!!');

View File

@ -17,6 +17,9 @@ class StubPlatformImplementation extends PlatformImplementation {
@override
Future<bool> generateVideoThumbnail(
String src, String dest, int width) async =>
String src,
String dest,
int width,
) async =>
false;
}

View File

@ -11,7 +11,8 @@ abstract class BackgroundService {
/// [handleEvent] is a function that is called whenever the service receives
/// data.
void init(
Future<void> Function() entrypoint,
Future<void> Function(String initialLocale) entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent,
String initialLocale,
);
}