From 5d8fc1bcd4e453298cfac086cdbdf279612bfb63 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Wed, 11 Dec 2024 01:43:57 +0400 Subject: [PATCH] fix(GmsCore support): Adjust presentation of battery optimization dialog (#4091) Co-authored-by: oSumAtrIX --- .../extension/shared/GmsCoreSupport.java | 46 +++++++++-------- .../app/revanced/extension/shared/Utils.java | 51 ++++++++++++------- .../extension/shared/settings/Setting.java | 3 +- .../resources/addresources/values/strings.xml | 2 +- 4 files changed, 61 insertions(+), 41 deletions(-) diff --git a/extensions/shared/library/src/main/java/app/revanced/extension/shared/GmsCoreSupport.java b/extensions/shared/library/src/main/java/app/revanced/extension/shared/GmsCoreSupport.java index c211385b4b..d7bd715905 100644 --- a/extensions/shared/library/src/main/java/app/revanced/extension/shared/GmsCoreSupport.java +++ b/extensions/shared/library/src/main/java/app/revanced/extension/shared/GmsCoreSupport.java @@ -54,17 +54,20 @@ private static void open(String queryOrLink) { private static void showBatteryOptimizationDialog(Activity context, String dialogMessageRef, - String positiveButtonStringRef, + String positiveButtonTextRef, DialogInterface.OnClickListener onPositiveClickListener) { - // Do not set cancelable to false, to allow using back button to skip the action, - // just in case the check can never be satisfied. - var dialog = new AlertDialog.Builder(context) - .setIconAttribute(android.R.attr.alertDialogIcon) - .setTitle(str("gms_core_dialog_title")) - .setMessage(str(dialogMessageRef)) - .setPositiveButton(str(positiveButtonStringRef), onPositiveClickListener) - .create(); - Utils.showDialog(context, dialog); + // Use a delay to allow the activity to finish initializing. + // Otherwise, if device is in dark mode the dialog is shown with wrong color scheme. + Utils.runOnMainThreadDelayed(() -> { + // Do not set cancelable to false, to allow using back button to skip the action, + // just in case the battery change can never be satisfied. + var dialog = new AlertDialog.Builder(context) + .setTitle(str("gms_core_dialog_title")) + .setMessage(str(dialogMessageRef)) + .setPositiveButton(str(positiveButtonTextRef), onPositiveClickListener) + .create(); + Utils.showDialog(context, dialog); + }, 100); } /** @@ -102,7 +105,18 @@ public static void checkGmsCore(Activity context) { return; } - // Check if GmsCore is running in the background. + // Check if GmsCore is whitelisted from battery optimizations. + if (batteryOptimizationsEnabled(context)) { + Logger.printInfo(() -> "GmsCore is not whitelisted from battery optimizations"); + + showBatteryOptimizationDialog(context, + "gms_core_dialog_not_whitelisted_using_battery_optimizations_message", + "gms_core_dialog_continue_text", + (dialog, id) -> openGmsCoreDisableBatteryOptimizationsIntent(context)); + return; + } + + // Check if GmsCore is currently running in the background. try (var client = context.getContentResolver().acquireContentProviderClient(GMS_CORE_PROVIDER)) { if (client == null) { Logger.printInfo(() -> "GmsCore is not running in the background"); @@ -111,18 +125,8 @@ public static void checkGmsCore(Activity context) { "gms_core_dialog_not_whitelisted_not_allowed_in_background_message", "gms_core_dialog_open_website_text", (dialog, id) -> open(DONT_KILL_MY_APP_LINK)); - return; } } - - // Check if GmsCore is whitelisted from battery optimizations. - if (batteryOptimizationsEnabled(context)) { - Logger.printInfo(() -> "GmsCore is not whitelisted from battery optimizations"); - showBatteryOptimizationDialog(context, - "gms_core_dialog_not_whitelisted_using_battery_optimizations_message", - "gms_core_dialog_continue_text", - (dialog, id) -> openGmsCoreDisableBatteryOptimizationsIntent(context)); - } } catch (Exception ex) { Logger.printException(() -> "checkGmsCore failure", ex); } diff --git a/extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java b/extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java index 13ad92d8ef..4e63e359f8 100644 --- a/extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java +++ b/extensions/shared/library/src/main/java/app/revanced/extension/shared/Utils.java @@ -4,6 +4,7 @@ import android.app.*; import android.content.Context; import android.content.Intent; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.res.Configuration; @@ -48,6 +49,7 @@ public class Utils { private static Context context; private static String versionName; + private static String applicationLabel; private Utils() { } // utility class @@ -62,28 +64,30 @@ public static String getPatchesReleaseVersion() { return ""; // Value is replaced during patching. } + private static PackageInfo getPackageInfo() throws PackageManager.NameNotFoundException { + final var packageName = Objects.requireNonNull(getContext()).getPackageName(); + + PackageManager packageManager = context.getPackageManager(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + return packageManager.getPackageInfo( + packageName, + PackageManager.PackageInfoFlags.of(0) + ); + } + + return packageManager.getPackageInfo( + packageName, + 0 + ); + } + /** * @return The version name of the app, such as 19.11.43 */ public static String getAppVersionName() { if (versionName == null) { try { - final var packageName = Objects.requireNonNull(getContext()).getPackageName(); - - PackageManager packageManager = context.getPackageManager(); - PackageInfo packageInfo; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - packageInfo = packageManager.getPackageInfo( - packageName, - PackageManager.PackageInfoFlags.of(0) - ); - } else { - packageInfo = packageManager.getPackageInfo( - packageName, - 0 - ); - } - versionName = packageInfo.versionName; + versionName = getPackageInfo().versionName; } catch (Exception ex) { Logger.printException(() -> "Failed to get package info", ex); versionName = "Unknown"; @@ -93,6 +97,19 @@ public static String getAppVersionName() { return versionName; } + public static String getApplicationName() { + if (applicationLabel == null) { + try { + ApplicationInfo applicationInfo = getPackageInfo().applicationInfo; + applicationLabel = (String) applicationInfo.loadLabel(context.getPackageManager()); + } catch (Exception ex) { + Logger.printException(() -> "Failed to get application name", ex); + applicationLabel = "Unknown"; + } + } + + return applicationLabel; + } /** * Hide a view by setting its layout height and width to 1dp. @@ -326,7 +343,7 @@ public static ViewParent getParentView(@NonNull View view, int nthParent) { public static void restartApp(@NonNull Context context) { String packageName = context.getPackageName(); - Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); + Intent intent = Objects.requireNonNull(context.getPackageManager().getLaunchIntentForPackage(packageName)); Intent mainIntent = Intent.makeRestartActivityTask(intent.getComponent()); // Required for API 34 and later // Ref: https://developer.android.com/about/versions/14/behavior-changes-14#safer-intents diff --git a/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java b/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java index db5ecc844b..8ce4a9fa76 100644 --- a/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java +++ b/extensions/shared/library/src/main/java/app/revanced/extension/shared/settings/Setting.java @@ -7,7 +7,6 @@ import app.revanced.extension.shared.StringRef; import app.revanced.extension.shared.Utils; import app.revanced.extension.shared.settings.preference.SharedPrefCategory; -import org.jetbrains.annotations.NotNull; import org.json.JSONException; import org.json.JSONObject; @@ -330,7 +329,7 @@ public boolean isSetToDefault() { return value.equals(defaultValue); } - @NotNull + @NonNull @Override public String toString() { return key + "=" + get(); diff --git a/patches/src/main/resources/addresources/values/strings.xml b/patches/src/main/resources/addresources/values/strings.xml index 83f9da6bd6..3da039e387 100644 --- a/patches/src/main/resources/addresources/values/strings.xml +++ b/patches/src/main/resources/addresources/values/strings.xml @@ -69,7 +69,7 @@ This is because Crowdin requires temporarily flattening this file and removing t Action needed MicroG GmsCore does not have permission to run in the background.\n\nFollow the \"Don\'t kill my app\" guide for your phone, and apply the instructions to your MicroG installation.\n\nThis is required for the app to work. Open website - MicroG GmsCore battery optimizations must be disabled to prevent issues.\n\nTap on the continue button and disable battery optimizations. + MicroG GmsCore battery optimizations must be disabled to prevent issues.\n\nDisabling battery optimizations for MicroG will not negatively affect battery usage.\n\nTap the continue button and allow optimization changes. Continue