refactor: Rename src to packages

This commit is contained in:
2022-09-01 13:39:12 +02:00
parent a280146258
commit 7f5fd68789
93 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
import "dart:convert";
import "dart:ui";
import "package:moxplatform_android/service_android.dart";
import "package:flutter/widgets.dart";
import "package:flutter/services.dart";
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";
import "package:get_it/get_it.dart";
/// An [AwaitableDataSender] that uses flutter_background_service.
class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
final MethodChannel _channel;
BackgroundServiceDataSender() : _channel = MethodChannel("me.polynom.moxplatform_android"), super();
@override
Future<void> sendDataImpl(DataWrapper data) async {
await _channel.invokeMethod("sendData", jsonEncode(data.toJson()));
}
}
@pragma('vm:entry-point')
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"]!;
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);
final srv = AndroidBackgroundService();
GetIt.I.registerSingleton<BackgroundService>(srv);
srv.init(
entrypoint! as Future<void> Function(),
handleUIEvent! as Future<void> Function(Map<String, dynamic>? data)
);
}
/// The Android specific implementation of the [IsolateHandler].
class AndroidIsolateHandler extends IsolateHandler {
final BackgroundServiceDataSender _dataSender;
final MethodChannel _channel;
final Logger _log;
AndroidIsolateHandler()
: _channel = MethodChannel("me.polynom.moxplatform_android"),
_dataSender = BackgroundServiceDataSender(),
_log = Logger("AndroidIsolateHandler"),
super();
@override
Future<void> attach(Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent) async {
_channel.setMethodCallHandler((MethodCall call) async {
await handleIsolateEvent(jsonDecode(call.arguments));
});
}
@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();
final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
_log.finest('androidEntrypointHandle: $androidEntrypointHandle');
await _channel.invokeMethod("configure", [
androidEntrypointHandle,
jsonEncode({
"genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
"eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
}),
]);
await attach(handleIsolateEvent);
final result = await _channel.invokeMethod("start", []);
if (result) {
_log.finest("Service successfully started");
} else {
_log.severe("Service failed to start");
}
}
@override
Future<bool> isRunning() async {
WidgetsFlutterBinding.ensureInitialized();
return await _channel.invokeMethod("isRunning", []);
}
@override
BackgroundServiceDataSender getDataSender() => _dataSender;
}

View File

@@ -0,0 +1,9 @@
import "package:moxplatform_platform_interface/src/media.dart";
import "package:media_scanner/media_scanner.dart";
class AndroidMediaScannerImplementation extends MediaScannerImplementation {
@override
void scanFile(String path) {
MediaScanner.loadMedia(path: path);
}
}

View File

@@ -0,0 +1,15 @@
import "package:moxplatform_android/isolate_android.dart";
import "package:moxplatform_android/media_android.dart";
import "package:moxplatform_platform_interface/moxplatform_platform_interface.dart";
class MoxplatformAndroidPlugin extends MoxplatformInterface {
static void registerWith() {
print("MoxplatformAndroidPlugin: Registering implementation");
MoxplatformInterface.handler = AndroidIsolateHandler();
MoxplatformInterface.media = AndroidMediaScannerImplementation();
}
@override
Future<String> getPlatformName() async => "Android";
}

View File

@@ -0,0 +1,17 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'moxplatform_android_platform_interface.dart';
/// An implementation of [MoxplatformAndroidPlatform] that uses method channels.
class MethodChannelMoxplatformAndroid extends MoxplatformAndroidPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('moxplatform_android');
@override
Future<String?> getPlatformVersion() async {
final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}
}

View File

@@ -0,0 +1,29 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'moxplatform_android_method_channel.dart';
abstract class MoxplatformAndroidPlatform extends PlatformInterface {
/// Constructs a MoxplatformAndroidPlatform.
MoxplatformAndroidPlatform() : super(token: _token);
static final Object _token = Object();
static MoxplatformAndroidPlatform _instance = MethodChannelMoxplatformAndroid();
/// The default instance of [MoxplatformAndroidPlatform] to use.
///
/// Defaults to [MethodChannelMoxplatformAndroid].
static MoxplatformAndroidPlatform get instance => _instance;
/// Platform-specific implementations should set this with their own
/// platform-specific class that extends [MoxplatformAndroidPlatform] when
/// they register themselves.
static set instance(MoxplatformAndroidPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}
}

View File

@@ -0,0 +1,63 @@
import "dart:convert";
import "dart:ui";
import "package:moxplatform_platform_interface/src/service.dart";
import "package:moxplatform/types.dart";
import "package:moxlib/awaitabledatasender.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:logging/logging.dart";
import "package:uuid/uuid.dart";
import "package:meta/meta.dart";
class AndroidBackgroundService extends BackgroundService {
@internal
static const MethodChannel channel = MethodChannel("me.polynom.moxplatform_android_bg");
final Logger _log;
AndroidBackgroundService()
: _log = Logger("AndroidBackgroundService"),
super();
@override
void setNotification(String title, String body) {
channel.invokeMethod(
"setNotificationBody",
[ body ]
);
}
@override
void sendEvent(BackgroundEvent event, { String? id }) {
final data = DataWrapper(
id ?? const Uuid().v4(),
event
);
// NOTE: *S*erver to *F*oreground
_log.fine("S2F: ${data.toJson().toString()}");
channel.invokeMethod("sendData", jsonEncode(data.toJson()));
}
@override
void init(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent
) {
WidgetsFlutterBinding.ensureInitialized();
// Ensure that all native plugins are registered against this FlutterEngine, so that
// we can use path_provider, notifications, ...
DartPluginRegistrant.ensureInitialized();
// Register the event handler
channel.setMethodCallHandler((MethodCall call) async {
await handleEvent(jsonDecode(call.arguments));
});
setNotification("Moxxy", "Preparing...");
_log.finest("Running...");
entrypoint();
}
}