fix: Only init foreground service if user allowed IGNORE_BATTERY_OPTIMIZATIONS permission

This commit is contained in:
Alberto Ponces 2022-09-17 14:55:00 +01:00
parent 0424ee235c
commit 3aaf49fee0

View File

@ -1,8 +1,11 @@
import 'dart:html';
import 'package:device_apps/device_apps.dart'; import 'package:device_apps/device_apps.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_background/flutter_background.dart'; import 'package:flutter_background/flutter_background.dart';
import 'package:flutter_i18n/flutter_i18n.dart'; import 'package:flutter_i18n/flutter_i18n.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:revanced_manager/app/app.locator.dart'; import 'package:revanced_manager/app/app.locator.dart';
import 'package:revanced_manager/models/patch.dart'; import 'package:revanced_manager/models/patch.dart';
import 'package:revanced_manager/models/patched_application.dart'; import 'package:revanced_manager/models/patched_application.dart';
@ -29,29 +32,31 @@ class InstallerViewModel extends BaseViewModel {
bool hasErrors = false; bool hasErrors = false;
Future<void> initialize(BuildContext context) async { Future<void> initialize(BuildContext context) async {
try { if (await Permission.ignoreBatteryOptimizations.isGranted) {
await FlutterBackground.initialize( try {
androidConfig: FlutterBackgroundAndroidConfig( await FlutterBackground.initialize(
notificationTitle: FlutterI18n.translate( androidConfig: FlutterBackgroundAndroidConfig(
context, notificationTitle: FlutterI18n.translate(
'installerView.notificationTitle', context,
'installerView.notificationTitle',
),
notificationText: FlutterI18n.translate(
context,
'installerView.notificationText',
),
notificationImportance: AndroidNotificationImportance.Default,
notificationIcon: const AndroidResource(
name: 'ic_notification',
defType: 'drawable',
),
), ),
notificationText: FlutterI18n.translate( );
context, await FlutterBackground.enableBackgroundExecution();
'installerView.notificationText', } on Exception {
), // ignore
notificationImportance: AndroidNotificationImportance.Default, }
notificationIcon: const AndroidResource(
name: 'ic_notification',
defType: 'drawable',
),
),
);
await FlutterBackground.enableBackgroundExecution();
await Wakelock.enable();
} on Exception {
// ignore
} }
await Wakelock.enable();
await handlePlatformChannelMethods(); await handlePlatformChannelMethods();
await runPatcher(); await runPatcher();
} }
@ -119,12 +124,14 @@ class InstallerViewModel extends BaseViewModel {
hasErrors = true; hasErrors = true;
update(-1.0, 'Aborting...', 'No app or patches selected! Aborting'); update(-1.0, 'Aborting...', 'No app or patches selected! Aborting');
} }
try { if (await Permission.ignoreBatteryOptimizations.isGranted) {
await FlutterBackground.disableBackgroundExecution(); try {
await Wakelock.disable(); await FlutterBackground.disableBackgroundExecution();
} on Exception { } on Exception {
// ignore // ignore
}
} }
await Wakelock.disable();
isPatching = false; isPatching = false;
} }