Files
FlClash/lib/plugins/app.dart
T

108 lines
3.0 KiB
Dart
Raw Normal View History

2024-04-30 23:38:49 +08:00
import 'dart:async';
2025-07-31 17:09:18 +08:00
import 'dart:io';
2024-04-30 23:38:49 +08:00
2025-12-16 11:23:09 +08:00
import 'package:fl_clash/common/common.dart';
2024-04-30 23:38:49 +08:00
import 'package:fl_clash/models/models.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
class App {
static App? _instance;
2024-07-13 16:36:08 +08:00
late MethodChannel methodChannel;
2024-04-30 23:38:49 +08:00
Function()? onExit;
App._internal() {
2025-07-31 17:09:18 +08:00
methodChannel = const MethodChannel('$packageName/app');
2024-07-13 16:36:08 +08:00
methodChannel.setMethodCallHandler((call) async {
switch (call.method) {
2025-06-07 01:48:34 +08:00
case 'exit':
2024-07-13 16:36:08 +08:00
if (onExit != null) {
await onExit!();
}
default:
throw MissingPluginException();
}
});
2024-04-30 23:38:49 +08:00
}
factory App() {
_instance ??= App._internal();
return _instance!;
}
Future<bool?> moveTaskToBack() async {
2026-02-02 10:14:13 +08:00
return methodChannel.invokeMethod<bool>('moveTaskToBack');
2024-04-30 23:38:49 +08:00
}
Future<List<Package>> getPackages() async {
2025-07-31 17:09:18 +08:00
final packagesString = await methodChannel.invokeMethod<String>(
'getPackages',
);
2026-02-02 10:14:13 +08:00
final List<dynamic> packagesRaw =
2025-12-16 11:23:09 +08:00
(await packagesString?.commonToJSON<List<dynamic>>()) ?? [];
return packagesRaw.map((e) => Package.fromJson(e)).toSet().toList();
2024-08-05 19:25:35 +08:00
}
Future<List<String>> getChinaPackageNames() async {
2025-07-31 17:09:18 +08:00
final packageNamesString = await methodChannel.invokeMethod<String>(
'getChinaPackageNames',
);
2026-02-02 10:14:13 +08:00
final List<dynamic> packageNamesRaw =
2025-12-16 11:23:09 +08:00
await packageNamesString?.commonToJSON<List<dynamic>>() ?? [];
return packageNamesRaw.map((e) => e.toString()).toList();
2024-04-30 23:38:49 +08:00
}
2025-07-31 17:09:18 +08:00
Future<bool?> requestNotificationsPermission() async {
2026-05-27 15:16:51 +08:00
return methodChannel.invokeMethod<bool>('requestNotificationsPermission');
2025-07-31 17:09:18 +08:00
}
2024-07-26 08:05:22 +08:00
Future<bool> openFile(String path) async {
2025-07-31 17:09:18 +08:00
return await methodChannel.invokeMethod<bool>('openFile', {'path': path}) ??
2024-07-26 08:05:22 +08:00
false;
}
2024-04-30 23:38:49 +08:00
Future<ImageProvider?> getPackageIcon(String packageName) async {
2025-07-31 17:09:18 +08:00
final path = await methodChannel.invokeMethod<String>('getPackageIcon', {
2025-06-07 01:48:34 +08:00
'packageName': packageName,
2024-04-30 23:38:49 +08:00
});
2025-07-31 17:09:18 +08:00
if (path == null) {
2024-04-30 23:38:49 +08:00
return null;
}
2025-07-31 17:09:18 +08:00
return FileImage(File(path));
2024-04-30 23:38:49 +08:00
}
Future<bool?> tip(String? message) async {
2026-05-27 15:16:51 +08:00
return methodChannel.invokeMethod<bool>('tip', {'message': '$message'});
2024-04-30 23:38:49 +08:00
}
2024-10-27 16:59:23 +08:00
Future<bool?> initShortcuts() async {
2026-02-02 10:14:13 +08:00
return methodChannel.invokeMethod<bool>(
2025-06-07 01:48:34 +08:00
'initShortcuts',
2026-02-02 10:14:13 +08:00
currentAppLocalizations.toggle,
2024-10-27 16:59:23 +08:00
);
}
2024-07-08 17:34:14 +08:00
Future<bool?> updateExcludeFromRecents(bool value) async {
2026-02-02 10:14:13 +08:00
return methodChannel.invokeMethod<bool>('updateExcludeFromRecents', {
2025-06-07 01:48:34 +08:00
'value': value,
2024-07-08 17:34:14 +08:00
});
}
2026-02-02 10:14:13 +08:00
Future<bool?> isBatteryOptimizationDisabled() async {
if (!Platform.isAndroid) return true;
return methodChannel.invokeMethod<bool>('isBatteryOptimizationDisabled');
}
Future<bool?> openBatteryOptimizationSettings() async {
if (!Platform.isAndroid) return false;
return methodChannel.invokeMethod<bool>('openBatteryOptimizationSettings');
}
Future<bool?> openAppSettings() async {
if (!Platform.isAndroid) return false;
return methodChannel.invokeMethod<bool>('openAppSettings');
}
2024-04-30 23:38:49 +08:00
}
2025-06-07 01:48:34 +08:00
final app = system.isAndroid ? App() : null;