2022-04-28 13:33:42 +00:00
|
|
|
import "dart:convert";
|
|
|
|
import "dart:ui";
|
|
|
|
|
|
|
|
import "package:moxplatform_android/service_android.dart";
|
|
|
|
|
|
|
|
import "package:flutter/widgets.dart";
|
2022-04-28 19:26:08 +00:00
|
|
|
import "package:flutter/services.dart";
|
2022-04-28 13:33:42 +00:00
|
|
|
import "package:logging/logging.dart";
|
|
|
|
import "package:moxplatform_platform_interface/src/isolate.dart";
|
|
|
|
import "package:moxplatform_platform_interface/src/service.dart";
|
|
|
|
import "package:moxplatform/types.dart";
|
|
|
|
import "package:moxlib/awaitabledatasender.dart";
|
2022-04-28 19:26:08 +00:00
|
|
|
import "package:get_it/get_it.dart";
|
2022-04-28 13:33:42 +00:00
|
|
|
|
|
|
|
/// An [AwaitableDataSender] that uses flutter_background_service.
|
|
|
|
class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
|
2022-04-28 19:26:08 +00:00
|
|
|
final MethodChannel _channel;
|
|
|
|
BackgroundServiceDataSender() : _channel = MethodChannel("me.polynom.moxplatform_android"), super();
|
2022-04-28 13:33:42 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> sendDataImpl(DataWrapper data) async {
|
2022-04-28 19:26:08 +00:00
|
|
|
await _channel.invokeMethod("sendData", jsonEncode(data.toJson()));
|
2022-04-28 13:33:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-28 19:26:08 +00:00
|
|
|
Future<void> androidEntrypoint() async {
|
|
|
|
print("androidEntrypoint: Called on new FlutterEngine");
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
|
|
|
/*
|
|
|
|
AndroidBackgroundService.channel.setMethodCallHandler((MethodCall call) async {
|
|
|
|
print(call.method);
|
|
|
|
});*/
|
|
|
|
|
|
|
|
final data = jsonDecode(await AndroidBackgroundService.channel.invokeMethod("getExtraData", []));
|
|
|
|
final int entrypointHandle = data["genericEntrypoint"]!;
|
2022-04-28 13:33:42 +00:00
|
|
|
final entrypointCallbackHandle = CallbackHandle.fromRawHandle(entrypointHandle);
|
|
|
|
final entrypoint = PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
|
|
|
|
final int handleUIEventHandle = data["eventHandle"]!;
|
|
|
|
final handleUIEventCallbackHandle = CallbackHandle.fromRawHandle(handleUIEventHandle);
|
|
|
|
final handleUIEvent = PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
|
|
|
|
|
2022-04-28 19:26:08 +00:00
|
|
|
final srv = AndroidBackgroundService();
|
2022-04-28 13:33:42 +00:00
|
|
|
GetIt.I.registerSingleton<BackgroundService>(srv);
|
|
|
|
srv.init(
|
2022-04-28 19:26:08 +00:00
|
|
|
entrypoint! as Future<void> Function(),
|
|
|
|
handleUIEvent! as Future<void> Function(Map<String, dynamic>? data)
|
2022-04-28 13:33:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The Android specific implementation of the [IsolateHandler].
|
|
|
|
class AndroidIsolateHandler extends IsolateHandler {
|
|
|
|
final BackgroundServiceDataSender _dataSender;
|
2022-04-28 19:26:08 +00:00
|
|
|
final MethodChannel _channel;
|
2022-04-28 13:33:42 +00:00
|
|
|
final Logger _log;
|
|
|
|
|
|
|
|
AndroidIsolateHandler()
|
2022-04-28 19:26:08 +00:00
|
|
|
: _channel = MethodChannel("me.polynom.moxplatform_android"),
|
2022-04-28 13:33:42 +00:00
|
|
|
_dataSender = BackgroundServiceDataSender(),
|
|
|
|
_log = Logger("AndroidIsolateHandler"),
|
|
|
|
super();
|
|
|
|
|
|
|
|
@override
|
2022-04-28 19:26:08 +00:00
|
|
|
Future<void> attach(Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent) async {
|
|
|
|
_channel.setMethodCallHandler((MethodCall call) async {
|
|
|
|
await handleIsolateEvent(jsonDecode(call.arguments));
|
|
|
|
});
|
2022-04-28 13:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> start(
|
|
|
|
Future<void> Function() entrypoint,
|
|
|
|
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
|
|
|
|
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
|
|
|
|
) async {
|
|
|
|
_log.finest("Called start");
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2022-04-28 19:26:08 +00:00
|
|
|
|
|
|
|
await _channel.invokeMethod("configure", [
|
|
|
|
PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle(),
|
|
|
|
jsonEncode({
|
|
|
|
"genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
|
2022-04-28 13:33:42 +00:00
|
|
|
"eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
|
2022-04-28 19:26:08 +00:00
|
|
|
})
|
|
|
|
]);
|
|
|
|
|
|
|
|
await attach(handleIsolateEvent);
|
|
|
|
|
|
|
|
final result = await _channel.invokeMethod("start", []);
|
|
|
|
if (result) {
|
2022-04-28 13:33:42 +00:00
|
|
|
_log.finest("Service successfully started");
|
|
|
|
} else {
|
|
|
|
_log.severe("Service failed to start");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> isRunning() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2022-04-28 19:26:08 +00:00
|
|
|
return await _channel.invokeMethod("isRunning", []);
|
2022-04-28 13:33:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
BackgroundServiceDataSender getDataSender() => _dataSender;
|
|
|
|
}
|