Skip to content

Commit

Permalink
fix(GmsCore support): Adjust presentation of battery optimization dia…
Browse files Browse the repository at this point in the history
…log (#4091)

Co-authored-by: oSumAtrIX <johan.melkonyan1@web.de>
  • Loading branch information
LisoUseInAIKyrios and oSumAtrIX authored Dec 10, 2024
1 parent d6c649e commit 5d8fc1b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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");
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class Utils {
private static Context context;

private static String versionName;
private static String applicationLabel;

private Utils() {
} // utility class
Expand All @@ -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";
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -330,7 +329,7 @@ public boolean isSetToDefault() {
return value.equals(defaultValue);
}

@NotNull
@NonNull
@Override
public String toString() {
return key + "=" + get();
Expand Down
2 changes: 1 addition & 1 deletion patches/src/main/resources/addresources/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ This is because Crowdin requires temporarily flattening this file and removing t
<string name="gms_core_dialog_title">Action needed</string>
<string name="gms_core_dialog_not_whitelisted_not_allowed_in_background_message">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.</string>
<string name="gms_core_dialog_open_website_text">Open website</string>
<string name="gms_core_dialog_not_whitelisted_using_battery_optimizations_message">MicroG GmsCore battery optimizations must be disabled to prevent issues.\n\nTap on the continue button and disable battery optimizations.</string>
<string name="gms_core_dialog_not_whitelisted_using_battery_optimizations_message">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.</string>
<string name="gms_core_dialog_continue_text">Continue</string>
</patch>
</app>
Expand Down

0 comments on commit 5d8fc1b

Please sign in to comment.