2023-09-10 11:28:12 +00:00
|
|
|
// ignore_for_file: avoid_print
|
2023-09-08 17:25:26 +00:00
|
|
|
import 'dart:io';
|
2023-09-07 16:51:22 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2023-09-09 18:18:56 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2023-09-09 23:02:56 +00:00
|
|
|
import 'package:get_it/get_it.dart';
|
2023-09-07 16:51:22 +00:00
|
|
|
import 'package:moxxy_native/moxxy_native.dart';
|
2023-09-18 15:58:16 +00:00
|
|
|
import 'package:path/path.dart' as p;
|
2023-09-08 22:28:01 +00:00
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
|
2023-09-09 23:02:56 +00:00
|
|
|
@pragma('vm:entrypoint')
|
|
|
|
Future<void> serviceHandleData(Map<String, dynamic>? data) async {
|
|
|
|
print('[BG] Received data $data');
|
|
|
|
GetIt.I.get<BackgroundService>().send(
|
2023-09-10 11:28:12 +00:00
|
|
|
TestEvent(),
|
|
|
|
id: data!['id']! as String,
|
|
|
|
);
|
2023-09-09 23:02:56 +00:00
|
|
|
}
|
2023-09-09 18:18:56 +00:00
|
|
|
|
2023-09-09 23:02:56 +00:00
|
|
|
@pragma('vm:entry-point')
|
|
|
|
Future<void> serviceEntrypoint(String initialLocale) async {
|
2023-09-10 11:28:12 +00:00
|
|
|
// avoid_print
|
2023-09-09 23:02:56 +00:00
|
|
|
print('Initial locale: $initialLocale');
|
2023-09-08 22:28:01 +00:00
|
|
|
}
|
2023-09-07 16:51:22 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(const MyApp());
|
|
|
|
}
|
|
|
|
|
2023-09-08 17:25:26 +00:00
|
|
|
class MyApp extends StatefulWidget {
|
2023-09-07 16:51:22 +00:00
|
|
|
const MyApp({super.key});
|
|
|
|
|
2023-09-08 17:25:26 +00:00
|
|
|
@override
|
|
|
|
MyAppState createState() => MyAppState();
|
|
|
|
}
|
|
|
|
|
2023-09-09 23:02:56 +00:00
|
|
|
class TestCommand extends BackgroundCommand {
|
|
|
|
@override
|
|
|
|
Map<String, dynamic> toJson() => {
|
2023-09-10 11:28:12 +00:00
|
|
|
'request': 'return_name',
|
|
|
|
};
|
2023-09-09 23:02:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestEvent extends BackgroundEvent {
|
|
|
|
@override
|
|
|
|
Map<String, dynamic> toJson() => {
|
2023-09-10 11:28:12 +00:00
|
|
|
'name': 'Moxxy',
|
|
|
|
};
|
2023-09-09 23:02:56 +00:00
|
|
|
}
|
|
|
|
|
2023-09-08 17:25:26 +00:00
|
|
|
class MyAppState extends State<MyApp> {
|
|
|
|
String? imagePath;
|
|
|
|
|
2023-09-18 18:50:08 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
|
|
|
|
const EventChannel('org.moxxy.moxxyv2/notification_stream')
|
|
|
|
.receiveBroadcastStream()
|
|
|
|
.listen(
|
|
|
|
(event) {
|
|
|
|
print('Keyboard height: ${event as double}');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-09-07 16:51:22 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Plugin example app'),
|
|
|
|
),
|
|
|
|
body: ListView(
|
|
|
|
children: [
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
2023-09-08 13:41:06 +00:00
|
|
|
final result = await MoxxyPickerApi()
|
|
|
|
.pickFiles(FilePickerType.image, false);
|
2023-09-07 16:51:22 +00:00
|
|
|
print('User picked: $result');
|
|
|
|
},
|
|
|
|
child: const Text('Photo picker'),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
2023-09-08 13:41:06 +00:00
|
|
|
final result = await MoxxyPickerApi()
|
|
|
|
.pickFiles(FilePickerType.imageAndVideo, true);
|
2023-09-07 16:51:22 +00:00
|
|
|
print('User picked: $result');
|
|
|
|
},
|
|
|
|
child: const Text('Photo/Video multi-picker'),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
2023-09-08 13:41:06 +00:00
|
|
|
final result = await MoxxyPickerApi()
|
|
|
|
.pickFiles(FilePickerType.generic, true);
|
2023-09-07 16:51:22 +00:00
|
|
|
print('User picked: $result');
|
|
|
|
},
|
|
|
|
child: const Text('Generic multi-picker'),
|
|
|
|
),
|
2023-09-08 17:25:26 +00:00
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
2023-09-08 19:44:37 +00:00
|
|
|
final result = await MoxxyPickerApi()
|
|
|
|
.pickFiles(FilePickerType.image, false);
|
2023-09-08 17:25:26 +00:00
|
|
|
if (result.isEmpty) return;
|
|
|
|
|
2023-09-08 19:44:37 +00:00
|
|
|
final encDest = '${result.first!}.enc';
|
|
|
|
final decDest = '${result.first!}.dec';
|
2023-09-08 17:25:26 +00:00
|
|
|
final encResult = await MoxxyCryptographyApi().encryptFile(
|
|
|
|
result.first!,
|
|
|
|
encDest,
|
|
|
|
Uint8List.fromList(List.filled(32, 1)),
|
|
|
|
Uint8List.fromList(List.filled(16, 2)),
|
|
|
|
CipherAlgorithm.aes256CbcPkcs7,
|
|
|
|
'SHA-256',
|
|
|
|
);
|
|
|
|
if (encResult == null) {
|
|
|
|
print('Failed to encrypt file');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
final decResult = await MoxxyCryptographyApi().decryptFile(
|
|
|
|
encDest,
|
|
|
|
decDest,
|
|
|
|
Uint8List.fromList(List.filled(32, 1)),
|
|
|
|
Uint8List.fromList(List.filled(16, 2)),
|
|
|
|
CipherAlgorithm.aes256CbcPkcs7,
|
|
|
|
'SHA-256',
|
|
|
|
);
|
|
|
|
if (decResult == null) {
|
|
|
|
print('Failed to decrypt file');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
imagePath = decDest;
|
|
|
|
});
|
|
|
|
},
|
2023-09-08 19:44:37 +00:00
|
|
|
child: const Text('Test cryptography'),
|
2023-09-08 17:25:26 +00:00
|
|
|
),
|
2023-09-08 19:44:37 +00:00
|
|
|
if (imagePath != null) Image.file(File(imagePath!)),
|
2023-09-08 22:28:01 +00:00
|
|
|
TextButton(
|
2023-09-18 15:58:16 +00:00
|
|
|
onPressed: () async {
|
|
|
|
// Create channel
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
await MoxxyNotificationsApi().createNotificationChannels(
|
|
|
|
[
|
|
|
|
NotificationChannel(
|
|
|
|
id: 'foreground_service',
|
|
|
|
title: 'Foreground service',
|
|
|
|
description: 'lol',
|
|
|
|
importance: NotificationChannelImportance.MIN,
|
|
|
|
showBadge: false,
|
|
|
|
vibration: false,
|
|
|
|
enableLights: false,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
await Permission.notification.request();
|
|
|
|
}
|
|
|
|
|
|
|
|
final srv = getForegroundService();
|
|
|
|
await srv.start(
|
|
|
|
const ServiceConfig(
|
|
|
|
serviceEntrypoint,
|
|
|
|
serviceHandleData,
|
|
|
|
'en',
|
|
|
|
),
|
|
|
|
(data) async {
|
|
|
|
print('[FG] Received data $data');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
await Future<void>.delayed(const Duration(milliseconds: 600));
|
|
|
|
await getForegroundService().send(
|
|
|
|
TestCommand(),
|
|
|
|
awaitable: false,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Start foreground service'),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
// Pick a file and copy it into the internal storage directory
|
|
|
|
final mediaDir = Directory(
|
|
|
|
p.join(
|
|
|
|
await MoxxyPlatformApi().getPersistentDataPath(),
|
|
|
|
'media',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (!mediaDir.existsSync()) {
|
|
|
|
await mediaDir.create(recursive: true);
|
|
|
|
}
|
|
|
|
final pickResult = await MoxxyPickerApi()
|
|
|
|
.pickFiles(FilePickerType.image, true);
|
|
|
|
if (pickResult.isEmpty) return;
|
|
|
|
|
|
|
|
final shareItems = List<ShareItem>.empty(growable: true);
|
|
|
|
for (final result in pickResult) {
|
|
|
|
final mediaDirPath = p.join(
|
|
|
|
mediaDir.path,
|
|
|
|
p.basename(result!),
|
2023-09-09 23:02:56 +00:00
|
|
|
);
|
2023-09-18 15:58:16 +00:00
|
|
|
await File(result).copy(mediaDirPath);
|
2023-09-09 23:02:56 +00:00
|
|
|
|
2023-09-18 15:58:16 +00:00
|
|
|
shareItems.add(
|
|
|
|
ShareItem(
|
|
|
|
path: mediaDirPath,
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
),
|
2023-09-09 23:02:56 +00:00
|
|
|
);
|
2023-09-18 15:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Share with the system
|
|
|
|
await MoxxyPlatformApi().shareItems(
|
|
|
|
shareItems,
|
|
|
|
'image/*',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Share internal files'),
|
|
|
|
),
|
|
|
|
TextButton(
|
|
|
|
onPressed: () async {
|
|
|
|
// Share with the system
|
|
|
|
await MoxxyPlatformApi().shareItems(
|
|
|
|
[
|
|
|
|
ShareItem(
|
|
|
|
mime: 'text/plain',
|
|
|
|
text: 'Hello World!',
|
|
|
|
),
|
|
|
|
],
|
|
|
|
'text/*',
|
|
|
|
);
|
|
|
|
},
|
|
|
|
child: const Text('Share some text'),
|
|
|
|
),
|
2023-09-18 18:50:08 +00:00
|
|
|
const TextField(),
|
2023-09-07 16:51:22 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|