Reduce Android and iOS background work
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:flutter/foundation.dart' show defaultTargetPlatform;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:meshport_flutter/meshport_flutter.dart';
|
||||
@@ -12,6 +13,7 @@ import 'configuration_pages.dart';
|
||||
import 'detail_pages.dart';
|
||||
import 'runtime_diagnostics.dart';
|
||||
import 'proxy_speed_test.dart';
|
||||
import 'ui_lifecycle.dart';
|
||||
|
||||
class LabScreen extends StatefulWidget {
|
||||
const LabScreen({super.key, this.initializeNative = true});
|
||||
@@ -20,7 +22,7 @@ class LabScreen extends StatefulWidget {
|
||||
State<LabScreen> createState() => _LabScreenState();
|
||||
}
|
||||
|
||||
class _LabScreenState extends State<LabScreen> {
|
||||
class _LabScreenState extends State<LabScreen> with WidgetsBindingObserver {
|
||||
MeshPortClient? client, applicationClient, systemClient;
|
||||
MacSystemVpnSession? macVpnSession;
|
||||
bool legacyMigrationPending = false;
|
||||
@@ -48,6 +50,11 @@ class _LabScreenState extends State<LabScreen> {
|
||||
String? loadedConfig, loadedForm;
|
||||
static const vpnChannel = MethodChannel('meshport/system_vpn');
|
||||
Timer? timer, subscriptionTimer;
|
||||
bool initialized = false;
|
||||
bool get uiActive => shouldRefreshUi(WidgetsBinding.instance.lifecycleState);
|
||||
bool get detailedDiagnostics =>
|
||||
!(Platform.isAndroid || Platform.isIOS) ||
|
||||
const bool.fromEnvironment('MESHPORT_DETAILED_DIAGNOSTICS');
|
||||
bool subscriptionUpdating = false;
|
||||
String? savedProxyJson;
|
||||
final subscriptionRetry = <String, DateTime>{};
|
||||
@@ -98,6 +105,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
if (widget.initializeNative) {
|
||||
unawaited(initialize());
|
||||
}
|
||||
@@ -105,7 +113,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
|
||||
Future<void> initialize() async {
|
||||
try {
|
||||
log('客户端启动 · ${Platform.operatingSystem} · 代理诊断日志已开启');
|
||||
log('客户端启动 · ${Platform.operatingSystem}');
|
||||
// A --dart-define wins; the process environment serves `flutter test`
|
||||
// and development runs. Release builds use the bundled library.
|
||||
const defined = String.fromEnvironment('MESHPORT_LIBRARY');
|
||||
@@ -193,8 +201,12 @@ class _LabScreenState extends State<LabScreen> {
|
||||
proxyProfile.entry = runtimeMode;
|
||||
}
|
||||
singBoxEnabled = proxyProfile.proxyEnabled;
|
||||
final diagnostics = await opened.enableDiagnostics();
|
||||
log(diagnostics['enabled'] == true ? '穿透详细日志采集已开启' : '穿透日志未接管:宿主已有日志订阅器');
|
||||
if (detailedDiagnostics) {
|
||||
final diagnostics = await opened.enableDiagnostics();
|
||||
log(
|
||||
diagnostics['enabled'] == true ? '穿透详细日志采集已开启' : '穿透日志未接管:宿主已有日志订阅器',
|
||||
);
|
||||
}
|
||||
if (profileSupported) await checkLocalProfileMigration();
|
||||
if (Platform.isAndroid) {
|
||||
final snapshot = await androidVpnRequest({'op': 'status'});
|
||||
@@ -214,10 +226,6 @@ class _LabScreenState extends State<LabScreen> {
|
||||
await refresh();
|
||||
}
|
||||
}
|
||||
subscriptionTimer = Timer.periodic(
|
||||
const Duration(minutes: 1),
|
||||
(_) => unawaited(updateDueSubscriptions()),
|
||||
);
|
||||
if (Platform.isMacOS || Platform.isAndroid) {
|
||||
try {
|
||||
final preferences = await vpnChannel.invokeMapMethod<String, dynamic>(
|
||||
@@ -244,12 +252,9 @@ class _LabScreenState extends State<LabScreen> {
|
||||
log('自动连接未完成,请手动连接并检查授权');
|
||||
}
|
||||
}
|
||||
unawaited(updateDueSubscriptions());
|
||||
timer = Timer.periodic(const Duration(seconds: 2), (_) {
|
||||
if (!busy) {
|
||||
unawaited(refresh());
|
||||
}
|
||||
});
|
||||
if (!mounted) return;
|
||||
initialized = true;
|
||||
updateUiTimers();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
setState(() => error = '原生库未加载,请先构建并指定 MESHPORT_LIBRARY。$e');
|
||||
@@ -257,6 +262,34 @@ class _LabScreenState extends State<LabScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
if (defaultTargetPlatform == TargetPlatform.iOS &&
|
||||
(state == AppLifecycleState.hidden ||
|
||||
state == AppLifecycleState.paused ||
|
||||
state == AppLifecycleState.detached)) {
|
||||
cancel = true;
|
||||
if (speedTesting) proxySpeedTest.cancel();
|
||||
}
|
||||
if (usesMobileLifecycle && initialized) updateUiTimers();
|
||||
}
|
||||
|
||||
void updateUiTimers() {
|
||||
timer?.cancel();
|
||||
subscriptionTimer?.cancel();
|
||||
timer = subscriptionTimer = null;
|
||||
if (!uiActive) return;
|
||||
if (!busy) unawaited(refresh());
|
||||
unawaited(updateDueSubscriptions());
|
||||
timer = Timer.periodic(const Duration(seconds: 2), (_) {
|
||||
if (!busy) unawaited(refresh());
|
||||
});
|
||||
subscriptionTimer = Timer.periodic(
|
||||
const Duration(minutes: 1),
|
||||
(_) => unawaited(updateDueSubscriptions()),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> androidVpnRequest(
|
||||
Map<String, dynamic> request,
|
||||
) async {
|
||||
@@ -309,7 +342,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
}
|
||||
|
||||
Future<void> refresh() async {
|
||||
if (refreshing) return;
|
||||
if (refreshing || !uiActive) return;
|
||||
refreshing = true;
|
||||
try {
|
||||
final next = client == applicationClient && proxySession != null
|
||||
@@ -624,7 +657,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
),
|
||||
)).acquire();
|
||||
try {
|
||||
await opened.enableDiagnostics();
|
||||
if (detailedDiagnostics) await opened.enableDiagnostics();
|
||||
if (unified) {
|
||||
await diagnosticStage(
|
||||
'启动统一 VPN 代理',
|
||||
@@ -859,6 +892,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
cancel = true;
|
||||
proxySpeedTest.cancel();
|
||||
timer?.cancel();
|
||||
@@ -1496,6 +1530,7 @@ class _LabScreenState extends State<LabScreen> {
|
||||
);
|
||||
Future<void> updateDueSubscriptions() async {
|
||||
if (!mounted ||
|
||||
!uiActive ||
|
||||
busy ||
|
||||
subscriptionUpdating ||
|
||||
proxySavesPending > 0 ||
|
||||
|
||||
Reference in New Issue
Block a user