Skip to content
This repository has been archived by the owner on Oct 26, 2024. It is now read-only.

Commit

Permalink
feat: Modernize restart logic (#527)
Browse files Browse the repository at this point in the history
  • Loading branch information
LisoUseInAIKyrios authored Nov 29, 2023
1 parent c6a1b9f commit 0ea4e72
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private void importSettings(String replacementSettings) {
ReVancedSettingsFragment.settingImportInProgress = true;
final boolean rebootNeeded = SettingsEnum.importJSON(replacementSettings);
if (rebootNeeded) {
ReVancedSettingsFragment.showRebootDialog(getContext());
ReVancedSettingsFragment.showRestartDialog(getContext());
}
} catch (Exception ex) {
LogHelper.printException(() -> "importSettings failure", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@
import static app.revanced.integrations.utils.StringRef.str;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Process;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
Expand All @@ -22,13 +17,12 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.apps.youtube.app.application.Shell_HomeActivity;

import app.revanced.integrations.patches.playback.speed.CustomPlaybackSpeedPatch;
import app.revanced.integrations.settings.SettingsEnum;
import app.revanced.integrations.settings.SharedPrefCategory;
import app.revanced.integrations.utils.LogHelper;
import app.revanced.integrations.utils.ReVancedUtils;
import app.revanced.shared.settings.SettingsUtils;

public class ReVancedSettingsFragment extends PreferenceFragment {
/**
Expand All @@ -37,23 +31,13 @@ public class ReVancedSettingsFragment extends PreferenceFragment {
*/
static boolean settingImportInProgress;

private static void reboot(@NonNull Context activity) {
final int intentFlags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
PendingIntent intent = PendingIntent.getActivity(activity, 0,
new Intent(activity, Shell_HomeActivity.class), intentFlags);
AlarmManager systemService = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
systemService.setExact(AlarmManager.ELAPSED_REALTIME, 1500L, intent);
Process.killProcess(Process.myPid());
}

static void showRebootDialog(@NonNull Context activity) {
static void showRestartDialog(@NonNull Context contxt) {
String positiveButton = str("in_app_update_restart_button");
String negativeButton = str("sign_in_cancel");
new AlertDialog.Builder(activity).setMessage(str("pref_refresh_config"))
new AlertDialog.Builder(contxt).setMessage(str("pref_refresh_config"))
.setPositiveButton(positiveButton, (dialog, id) -> {
reboot(activity);
SettingsUtils.restartApp(contxt);
})
.setNegativeButton(negativeButton, null)
.setNegativeButton(android.R.string.cancel, null)
.setCancelable(false)
.show();
}
Expand Down Expand Up @@ -110,9 +94,9 @@ static void showRebootDialog(@NonNull Context activity) {

if (!showingUserDialogMessage) {
if (setting.userDialogMessage != null && ((SwitchPreference) pref).isChecked() != (Boolean) setting.defaultValue) {
showSettingUserDialogConfirmation(getActivity(), (SwitchPreference) pref, setting);
showSettingUserDialogConfirmation(getContext(), (SwitchPreference) pref, setting);
} else if (setting.rebootApp) {
showRebootDialog(getActivity());
showRestartDialog(getContext());
}
}

Expand Down Expand Up @@ -188,14 +172,14 @@ private void updateListPreferenceSummary(ListPreference listPreference, Settings
}
}

private void showSettingUserDialogConfirmation(@NonNull Activity activity, SwitchPreference switchPref, SettingsEnum setting) {
private void showSettingUserDialogConfirmation(@NonNull Context context, SwitchPreference switchPref, SettingsEnum setting) {
showingUserDialogMessage = true;
new AlertDialog.Builder(activity)
new AlertDialog.Builder(context)
.setTitle(str("revanced_settings_confirm_user_dialog_title"))
.setMessage(setting.userDialogMessage.toString())
.setPositiveButton(android.R.string.ok, (dialog, id) -> {
if (setting.rebootApp) {
showRebootDialog(activity);
showRestartDialog(context);
}
})
.setNegativeButton(android.R.string.cancel, (dialog, id) -> {
Expand Down
30 changes: 30 additions & 0 deletions app/src/main/java/app/revanced/shared/settings/SettingsUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package app.revanced.shared.settings;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;

/**
* Class is used across multiple target apps.
*
* This entire class can _not_ reference:
* {@link app.revanced.integrations.settings.SettingsEnum}
* {@link app.revanced.twitch.settings.SettingsEnum}
* {@link app.revanced.tiktok.settings.SettingsEnum}
*
* or any other code that references these app specific integration classes.
*/
public class SettingsUtils {

public static void restartApp(@NonNull Context context) {
String packageName = context.getPackageName();
Intent intent = 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
mainIntent.setPackage(packageName);
context.startActivity(mainIntent);
System.exit(0);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package app.revanced.tiktok.settingsmenu;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Process;
import android.preference.*;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import app.revanced.integrations.utils.LogHelper;
import app.revanced.shared.settings.SettingsUtils;
import app.revanced.tiktok.settings.SettingsEnum;
import app.revanced.tiktok.settings.SharedPrefCategory;
import app.revanced.tiktok.settingsmenu.preference.DownloadPathPreference;
Expand All @@ -21,7 +26,6 @@
import app.revanced.tiktok.settingsmenu.preference.categories.IntegrationsPreferenceCategory;
import app.revanced.tiktok.settingsmenu.preference.categories.SimSpoofPreferenceCategory;
import app.revanced.tiktok.utils.ReVancedUtils;
import com.ss.android.ugc.aweme.splash.SplashActivity;

@SuppressWarnings("deprecation")
public class ReVancedPreferenceFragment extends PreferenceFragment {
Expand Down Expand Up @@ -107,13 +111,9 @@ public void onDestroy() {
super.onDestroy();
}

private void reboot(Activity activity) {
int intent = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
((AlarmManager) activity.getSystemService(Context.ALARM_SERVICE)).setExact(AlarmManager.ELAPSED_REALTIME, 1500L, PendingIntent.getActivity(activity, 0, new Intent(activity, SplashActivity.class), intent));
Process.killProcess(Process.myPid());
}

private void rebootDialog(final Activity activity) {
new AlertDialog.Builder(activity).setMessage("Refresh and restart").setPositiveButton("Restart", (dialog, i) -> reboot(activity)).setNegativeButton("Cancel", null).show();
private void rebootDialog(@NonNull Context context) {
new AlertDialog.Builder(context).setMessage("Refresh and restart")
.setPositiveButton("Restart", (dialog, i) -> SettingsUtils.restartApp(context))
.setNegativeButton(android.R.string.cancel, null).show();
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package app.revanced.twitch.settingsmenu;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Process;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.SwitchPreference;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import app.revanced.shared.settings.SettingsUtils;
import app.revanced.twitch.settings.SettingsEnum;
import app.revanced.twitch.utils.LogHelper;
import app.revanced.twitch.utils.ReVancedUtils;
import tv.twitch.android.app.core.LandingActivity;

public class ReVancedSettingsFragment extends PreferenceFragment {

Expand Down Expand Up @@ -62,7 +58,7 @@ else if (pref instanceof ListPreference) {
}

if (ReVancedUtils.getContext() != null && key != null && settingsInitialized && setting.rebootApp) {
rebootDialog(getActivity());
showRestartDialog(getContext());
}

// First onChange event is caused by initial state loading
Expand Down Expand Up @@ -122,19 +118,12 @@ public void onDestroy() {
super.onDestroy();
}

@SuppressLint("MissingPermission")
private void reboot(Activity activity) {
int flags;
flags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
((AlarmManager) activity.getSystemService(Context.ALARM_SERVICE)).setExact(AlarmManager.ELAPSED_REALTIME, 1500L, PendingIntent.getActivity(activity, 0, new Intent(activity, LandingActivity.class), flags));
Process.killProcess(Process.myPid());
}

private void rebootDialog(final Activity activity) {
new AlertDialog.Builder(activity).
private void showRestartDialog(@NonNull Context context) {
new AlertDialog.Builder(context).
setMessage(ReVancedUtils.getString("revanced_reboot_message")).
setPositiveButton(ReVancedUtils.getString("revanced_reboot"), (dialog, i) -> reboot(activity))
.setNegativeButton(ReVancedUtils.getString("revanced_cancel"), null)
setPositiveButton(ReVancedUtils.getString("revanced_reboot"),
(dialog, i) -> SettingsUtils.restartApp(context))
.setNegativeButton(android.R.string.cancel, null)
.show();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 0ea4e72

Please sign in to comment.