From 3aaf49fee0f62caab68d8e95a0b124da02917320 Mon Sep 17 00:00:00 2001 From: Alberto Ponces Date: Sat, 17 Sep 2022 14:55:00 +0100 Subject: [PATCH] fix: Only init foreground service if user allowed IGNORE_BATTERY_OPTIMIZATIONS permission --- .../views/installer/installer_viewmodel.dart | 59 +++++++++++-------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/lib/ui/views/installer/installer_viewmodel.dart b/lib/ui/views/installer/installer_viewmodel.dart index 89c59f77..cca8a707 100644 --- a/lib/ui/views/installer/installer_viewmodel.dart +++ b/lib/ui/views/installer/installer_viewmodel.dart @@ -1,8 +1,11 @@ +import 'dart:html'; + import 'package:device_apps/device_apps.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_background/flutter_background.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/models/patch.dart'; import 'package:revanced_manager/models/patched_application.dart'; @@ -29,29 +32,31 @@ class InstallerViewModel extends BaseViewModel { bool hasErrors = false; Future initialize(BuildContext context) async { - try { - await FlutterBackground.initialize( - androidConfig: FlutterBackgroundAndroidConfig( - notificationTitle: FlutterI18n.translate( - context, - 'installerView.notificationTitle', + if (await Permission.ignoreBatteryOptimizations.isGranted) { + try { + await FlutterBackground.initialize( + androidConfig: FlutterBackgroundAndroidConfig( + notificationTitle: FlutterI18n.translate( + context, + 'installerView.notificationTitle', + ), + notificationText: FlutterI18n.translate( + context, + 'installerView.notificationText', + ), + notificationImportance: AndroidNotificationImportance.Default, + notificationIcon: const AndroidResource( + name: 'ic_notification', + defType: 'drawable', + ), ), - notificationText: FlutterI18n.translate( - context, - 'installerView.notificationText', - ), - notificationImportance: AndroidNotificationImportance.Default, - notificationIcon: const AndroidResource( - name: 'ic_notification', - defType: 'drawable', - ), - ), - ); - await FlutterBackground.enableBackgroundExecution(); - await Wakelock.enable(); - } on Exception { - // ignore + ); + await FlutterBackground.enableBackgroundExecution(); + } on Exception { + // ignore + } } + await Wakelock.enable(); await handlePlatformChannelMethods(); await runPatcher(); } @@ -119,12 +124,14 @@ class InstallerViewModel extends BaseViewModel { hasErrors = true; update(-1.0, 'Aborting...', 'No app or patches selected! Aborting'); } - try { - await FlutterBackground.disableBackgroundExecution(); - await Wakelock.disable(); - } on Exception { - // ignore + if (await Permission.ignoreBatteryOptimizations.isGranted) { + try { + await FlutterBackground.disableBackgroundExecution(); + } on Exception { + // ignore + } } + await Wakelock.disable(); isPatching = false; }