feat: Migrate to moxlib 0.2.0

This commit is contained in:
PapaTutuWawa 2023-06-18 12:19:24 +02:00
parent 7a999cf860
commit 3bc880079c
13 changed files with 124 additions and 75 deletions

View File

@ -1,4 +1,5 @@
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
abstract class BackgroundCommand implements JsonImplementation {}
abstract class BackgroundEvent implements JsonImplementation {}

View File

@ -1,11 +1,11 @@
name: moxplatform
description: Moxxy platform-specific code
version: 0.1.15
version: 0.1.16
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
homepage: https://codeberg.org/moxxy/moxplatform
environment:
sdk: ">=2.16.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"
flutter: ">=2.10.0"
flutter:
@ -22,17 +22,17 @@ dependencies:
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.4
version: ^0.2.0
moxplatform_android:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.15
version: ^0.1.16
moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.15
version: ^0.1.16
dev_dependencies:
flutter_test:
sdk: flutter
plugin_platform_interface: ^2.1.2
very_good_analysis: ^2.4.0
very_good_analysis: ^3.0.1

View File

@ -6,13 +6,21 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
final _methodChannel = const MethodChannel('me.polynom.moxplatform_android');
@override
Future<CryptographyResult?> encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
final dynamic resultRaw = await _methodChannel.invokeMethod<dynamic>('encryptFile', [
Future<CryptographyResult?> encryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
final dynamic resultRaw =
await _methodChannel.invokeMethod<dynamic>('encryptFile', [
sourcePath,
destPath,
key,
iv,
algorithm.toInt(),
algorithm.value,
hashSpec,
]);
if (resultRaw == null) return null;
@ -26,13 +34,21 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
}
@override
Future<CryptographyResult?> decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
final dynamic resultRaw = await _methodChannel.invokeMethod<dynamic>('decryptFile', [
Future<CryptographyResult?> decryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
final dynamic resultRaw =
await _methodChannel.invokeMethod<dynamic>('decryptFile', [
sourcePath,
destPath,
key,
iv,
algorithm.toInt(),
algorithm.value,
hashSpec,
]);
if (resultRaw == null) return null;
@ -47,7 +63,8 @@ class AndroidCryptographyImplementation extends CryptographyImplementation {
@override
Future<Uint8List?> hashFile(String path, String hashSpec) async {
final dynamic resultsRaw = await _methodChannel.invokeMethod<dynamic>('hashFile', [
final dynamic resultsRaw =
await _methodChannel.invokeMethod<dynamic>('hashFile', [
path,
hashSpec,
]);

View File

@ -4,18 +4,19 @@ import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_android/src/service_android.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
/// An [AwaitableDataSender] that uses flutter_background_service.
class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
class BackgroundServiceDataSender
extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
BackgroundServiceDataSender()
: _channel = const MethodChannel('me.polynom.moxplatform_android'), super();
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
super();
final MethodChannel _channel;
@override
Future<void> sendDataImpl(DataWrapper data) async {
await _channel.invokeMethod<void>('sendData', jsonEncode(data.toJson()));
@ -39,11 +40,15 @@ Future<void> androidEntrypoint() async {
);
final data = jsonDecode(result!) as Map<String, dynamic>;
final entrypointHandle = data['genericEntrypoint']! as int;
final entrypointCallbackHandle = CallbackHandle.fromRawHandle(entrypointHandle);
final entrypoint = PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
final entrypointCallbackHandle =
CallbackHandle.fromRawHandle(entrypointHandle);
final entrypoint =
PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
final handleUIEventHandle = data['eventHandle']! as int;
final handleUIEventCallbackHandle = CallbackHandle.fromRawHandle(handleUIEventHandle);
final handleUIEvent = PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
final handleUIEventCallbackHandle =
CallbackHandle.fromRawHandle(handleUIEventHandle);
final handleUIEvent =
PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
final srv = AndroidBackgroundService();
GetIt.I.registerSingleton<BackgroundService>(srv);
@ -55,24 +60,25 @@ Future<void> androidEntrypoint() async {
/// The Android specific implementation of the [IsolateHandler].
class AndroidIsolateHandler extends IsolateHandler {
AndroidIsolateHandler()
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
_dataSender = BackgroundServiceDataSender(),
_log = Logger('AndroidIsolateHandler'),
super();
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
_dataSender = BackgroundServiceDataSender(),
_log = Logger('AndroidIsolateHandler'),
super();
final BackgroundServiceDataSender _dataSender;
final MethodChannel _channel;
final Logger _log;
@override
Future<void> attach(Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent) async {
Future<void> attach(
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
) async {
_channel.setMethodCallHandler((MethodCall call) async {
final args = call.arguments as String;
await handleIsolateEvent(jsonDecode(args) as Map<String, dynamic>);
});
}
@override
Future<void> start(
Future<void> Function() entrypoint,
@ -82,13 +88,16 @@ class AndroidIsolateHandler extends IsolateHandler {
_log.finest('Called start');
WidgetsFlutterBinding.ensureInitialized();
final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
final androidEntrypointHandle =
PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
_log.finest('androidEntrypointHandle: $androidEntrypointHandle');
await _channel.invokeMethod<void>('configure', <dynamic>[
androidEntrypointHandle,
jsonEncode({
'genericEntrypoint': PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle': PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
'genericEntrypoint':
PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle':
PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
}),
]);

View File

@ -4,19 +4,19 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
import 'package:uuid/uuid.dart';
class AndroidBackgroundService extends BackgroundService {
AndroidBackgroundService()
: _log = Logger('AndroidBackgroundService'),
super();
: _log = Logger('AndroidBackgroundService'),
super();
@internal
static const MethodChannel channel = MethodChannel('me.polynom.moxplatform_android_bg');
static const MethodChannel channel =
MethodChannel('me.polynom.moxplatform_android_bg');
final Logger _log;
@override
@ -28,7 +28,7 @@ class AndroidBackgroundService extends BackgroundService {
}
@override
void sendEvent(BackgroundEvent event, { String? id }) {
void sendEvent(BackgroundEvent event, {String? id}) {
final data = DataWrapper(
id ?? const Uuid().v4(),
event,
@ -54,7 +54,7 @@ class AndroidBackgroundService extends BackgroundService {
final args = call.arguments! as String;
await handleEvent(jsonDecode(args) as Map<String, dynamic>);
});
setNotification('Moxxy', 'Preparing...');
_log.finest('Running...');

View File

@ -1,11 +1,11 @@
name: moxplatform_android
description: Android implementation of moxplatform
version: 0.1.15
version: 0.1.16
homepage: https://codeberg.org/moxxy/moxplatform
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
environment:
sdk: ">=2.16.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"
flutter: ">=2.10.0"
flutter:
@ -26,14 +26,14 @@ dependencies:
meta: ^1.7.0
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.4
version: ^0.2.0
moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.15
version: ^0.1.16
moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.15
version: ^0.1.16
plugin_platform_interface: ^2.1.2
uuid: ^3.0.5
@ -41,4 +41,4 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
very_good_analysis: ^2.4.0
very_good_analysis: ^3.0.1

View File

@ -1,23 +1,17 @@
import 'dart:typed_data';
enum CipherAlgorithm {
aes128GcmNoPadding,
aes256GcmNoPadding,
aes256CbcPkcs7,
}
aes128GcmNoPadding(0),
aes256GcmNoPadding(1),
aes256CbcPkcs7(2);
extension CipherAlgorithmToIntExtension on CipherAlgorithm {
int toInt() {
switch (this) {
case CipherAlgorithm.aes128GcmNoPadding: return 0;
case CipherAlgorithm.aes256GcmNoPadding: return 1;
case CipherAlgorithm.aes256CbcPkcs7: return 2;
}
}
const CipherAlgorithm(this.value);
/// The "id" of the algorithm choice.
final int value;
}
class CryptographyResult {
const CryptographyResult(this.plaintextHash, this.ciphertextHash);
final Uint8List plaintextHash;
final Uint8List ciphertextHash;
@ -30,14 +24,28 @@ abstract class CryptographyImplementation {
/// Note that this function runs off-thread as to not block the UI thread.
///
/// Resolves to true if the encryption was successful. Resolves to fale on failure.
Future<CryptographyResult?> encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec);
Future<CryptographyResult?> encryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
);
/// Decrypt the file at [sourcePath] using [algorithm] and write the result back to
/// [destPath]. [hashSpec] is the name of the Hash function to use, i.e. "SHA-256".
/// Note that this function runs off-thread as to not block the UI thread.
///
///
/// Resolves to true if the encryption was successful. Resolves to fale on failure.
Future<CryptographyResult?> decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec);
Future<CryptographyResult?> decryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
);
/// Hashes the file at [path] using the Hash function with name [hashSpec].
/// Note that this function runs off-thread as to not block the UI thread.

View File

@ -3,12 +3,26 @@ import 'package:moxplatform_platform_interface/src/crypto.dart';
class StubCryptographyImplementation extends CryptographyImplementation {
@override
Future<CryptographyResult?> encryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
Future<CryptographyResult?> encryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
return null;
}
@override
Future<CryptographyResult?> decryptFile(String sourcePath, String destPath, Uint8List key, Uint8List iv, CipherAlgorithm algorithm, String hashSpec) async {
Future<CryptographyResult?> decryptFile(
String sourcePath,
String destPath,
Uint8List key,
Uint8List iv,
CipherAlgorithm algorithm,
String hashSpec,
) async {
return null;
}

View File

@ -10,7 +10,7 @@ abstract class MoxplatformInterface extends PlatformInterface {
MoxplatformInterface() : super(token: _token);
static final Object _token = Object();
static IsolateHandler handler = StubIsolateHandler();
static MediaScannerImplementation media = StubMediaScannerImplementation();
static CryptographyImplementation crypto = StubCryptographyImplementation();

View File

@ -1,4 +1,4 @@
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
/// A class abstracting the interaction between the UI isolate and the background
/// service, which is either a regular isolate or an Android foreground service.
@ -18,7 +18,7 @@ abstract class IsolateHandler {
Future<void> attach(
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
);
/// Return true if the background service is running. False if it's not.
Future<bool> isRunning();

View File

@ -1,4 +1,4 @@
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxlib/moxlib.dart';
import 'package:moxplatform_platform_interface/src/isolate.dart';
class StubDataSender extends AwaitableDataSender {
@ -19,7 +19,7 @@ class StubIsolateHandler extends IsolateHandler {
// ignore: avoid_print
print('STUB ATTACHED!!!!!!');
}
@override
Future<void> start(
Future<void> Function() entrypoint,

View File

@ -5,7 +5,7 @@ abstract class BackgroundService {
void setNotification(String title, String body);
/// Send data from the background service to the UI.
void sendEvent(BackgroundEvent event, { String? id });
void sendEvent(BackgroundEvent event, {String? id});
/// Called before [entrypoint]. Sets up whatever it needs to set up.
/// [handleEvent] is a function that is called whenever the service receives

View File

@ -1,11 +1,11 @@
name: moxplatform_platform_interface
description: A common platform interface for the my_plugin plugin.
version: 0.1.15
version: 0.1.16
homepage: https://codeberg.org/moxxy/moxplatform
publish_to: https://git.polynom.me/api/packages/Moxxy/pub
environment:
sdk: ">=2.16.0 <3.0.0"
sdk: ">=2.17.0 <3.0.0"
flutter: ">=2.10.0"
dependencies:
@ -14,14 +14,14 @@ dependencies:
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.4
version: ^0.2.0
moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: ^0.1.15
version: ^0.1.16
plugin_platform_interface: ^2.1.2
dev_dependencies:
flutter_test:
sdk: flutter
very_good_analysis: ^2.4.0
very_good_analysis: ^3.0.1