Skip to content
This repository was archived by the owner on May 4, 2023. It is now read-only.

Commit 996a50f

Browse files
committed
Add reboot protection
1 parent 1cc6f8e commit 996a50f

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

app/src/main/java/com/fox2code/mmm/MainApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public static boolean isShowcaseMode() {
9191
return getSharedPreferences().getBoolean("pref_showcase_mode", false);
9292
}
9393

94+
public static boolean shouldPreventReboot() {
95+
return getSharedPreferences().getBoolean("pref_prevent_reboot", false);
96+
}
97+
9498
public static boolean isShowIncompatibleModules() {
9599
return getSharedPreferences().getBoolean("pref_show_incompatible", false);
96100
}

app/src/main/java/com/fox2code/mmm/installer/InstallerActivity.java

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.fox2code.mmm.utils.Http;
3030
import com.fox2code.mmm.utils.IntentHelper;
3131
import com.fox2code.mmm.utils.PropUtils;
32+
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
3233
import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
3334
import com.google.android.material.progressindicator.LinearProgressIndicator;
3435
import com.topjohnwu.superuser.CallbackList;
@@ -64,7 +65,8 @@ protected void onCreate(Bundle savedInstanceState) {
6465
this.setDisplayHomeAsUpEnabled(true);
6566
setActionBarBackground(null);
6667
this.setOnBackPressedCallback(a -> {
67-
this.canceled = true; return false;
68+
this.canceled = true;
69+
return false;
6870
});
6971
final Intent intent = this.getIntent();
7072
final String target;
@@ -97,7 +99,7 @@ protected void onCreate(Bundle savedInstanceState) {
9799
setTitle(name);
98100
this.textWrap = MainApplication.isTextWrapEnabled();
99101
setContentView(this.textWrap ?
100-
R.layout.installer_wrap :R.layout.installer);
102+
R.layout.installer_wrap : R.layout.installer);
101103
int background;
102104
int foreground;
103105
if (MainApplication.getINSTANCE().isLightTheme() &&
@@ -133,7 +135,7 @@ protected void onCreate(Bundle savedInstanceState) {
133135
String errMessage = "Failed to download module zip";
134136
try {
135137
Log.i(TAG, "Downloading: " + target);
136-
byte[] rawModule = Http.doHttpGet(target,(progress, max, done) -> {
138+
byte[] rawModule = Http.doHttpGet(target, (progress, max, done) -> {
137139
if (max <= 0 && this.progressIndicator.isIndeterminate())
138140
return;
139141
this.runOnUiThread(() -> {
@@ -240,7 +242,7 @@ protected void onCreate(Bundle savedInstanceState) {
240242
}
241243

242244

243-
private void doInstall(File file,boolean noExtensions,boolean rootless) {
245+
private void doInstall(File file, boolean noExtensions, boolean rootless) {
244246
if (this.canceled) return;
245247
UiThreadHandler.runAndWait(() -> {
246248
this.setOnBackPressedCallback(DISABLE_BACK_BUTTON);
@@ -278,7 +280,8 @@ private void doInstall(File file,boolean noExtensions,boolean rootless) {
278280
}
279281
moduleId = PropUtils.readModuleId(zipFile
280282
.getInputStream(zipFile.getEntry("module.prop")));
281-
} catch (IOException ignored) {}
283+
} catch (IOException ignored) {
284+
}
282285
int compatFlags = AppUpdateManager.getFlagsForModule(moduleId);
283286
if ((compatFlags & AppUpdateManager.FLAG_COMPAT_NEED_32BIT) != 0)
284287
needs32bit = true;
@@ -344,7 +347,8 @@ private void doInstall(File file,boolean noExtensions,boolean rootless) {
344347
}
345348
boolean success = installJob.exec().isSuccess();
346349
// Wait one UI cycle before disabling controller or processing results
347-
UiThreadHandler.runAndWait(() -> {}); // to avoid race conditions
350+
UiThreadHandler.runAndWait(() -> {
351+
}); // to avoid race conditions
348352
installerController.disable();
349353
String message = "- Install successful";
350354
if (!success) {
@@ -368,7 +372,7 @@ public static class InstallerController extends CallbackList<String> {
368372
private String supportLink = "";
369373

370374
private InstallerController(LinearProgressIndicator progressIndicator,
371-
InstallerTerminal terminal,File moduleFile,
375+
InstallerTerminal terminal, File moduleFile,
372376
boolean noExtension) {
373377
this.progressIndicator = progressIndicator;
374378
this.terminal = terminal;
@@ -451,7 +455,8 @@ private void processCommand(String rawCommand) {
451455
try {
452456
this.progressIndicator.setProgressCompat(
453457
Short.parseShort(arg), true);
454-
} catch (Exception ignored) {}
458+
} catch (Exception ignored) {
459+
}
455460
break;
456461
case "hideLoading":
457462
this.progressIndicator.setVisibility(View.GONE);
@@ -544,7 +549,7 @@ private File extractInstallScript(String script) {
544549
}
545550

546551
@SuppressWarnings("SameParameterValue")
547-
private void setInstallStateFinished(boolean success, String message,String optionalLink) {
552+
private void setInstallStateFinished(boolean success, String message, String optionalLink) {
548553
if (success && toDelete != null && !toDelete.delete()) {
549554
SuFile suFile = new SuFile(toDelete.getAbsolutePath());
550555
if (suFile.exists() && !suFile.delete())
@@ -558,17 +563,36 @@ private void setInstallStateFinished(boolean success, String message,String opti
558563
this.progressIndicator.setVisibility(View.GONE);
559564

560565
// This should be improved ?
561-
rebootFloatingButton.setOnClickListener(_view -> Shell.cmd("/system/bin/svc power reboot || /system/bin/reboot").submit());
566+
String reboot_cmd = "/system/bin/svc power reboot || /system/bin/reboot";
567+
rebootFloatingButton.setOnClickListener(_view -> {
568+
if (MainApplication.shouldPreventReboot()) {
569+
MaterialAlertDialogBuilder builder =
570+
new MaterialAlertDialogBuilder(this);
571+
572+
builder
573+
.setTitle(R.string.install_terminal_reboot_now)
574+
.setCancelable(false)
575+
.setIcon(R.drawable.ic_reboot_24)
576+
.setPositiveButton(R.string.yes, (x, y) -> {
577+
Shell.cmd(reboot_cmd).submit();
578+
})
579+
.setNegativeButton(R.string.no, (x, y) -> {
580+
x.dismiss();
581+
}).show();
582+
} else {
583+
Shell.cmd(reboot_cmd).submit();
584+
}
585+
});
562586
this.rebootFloatingButton.setVisibility(View.VISIBLE);
563587

564588
if (message != null && !message.isEmpty())
565589
this.installerTerminal.addLine(message);
566590
if (!optionalLink.isEmpty()) {
567591
this.setActionBarExtraMenuButton(ActionButtonType.supportIconForUrl(optionalLink),
568592
menu -> {
569-
IntentHelper.openUrl(this, optionalLink);
570-
return true;
571-
});
593+
IntentHelper.openUrl(this, optionalLink);
594+
return true;
595+
});
572596
} else if (success) {
573597
final Intent intent = this.getIntent();
574598
final String config = MainApplication.checkSecret(intent) ?

app/src/main/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
<string name="submit_modules">Submit a module</string>
2626
<string name="require_android_6">Require Android 6.0+</string>
2727
<string name="install_terminal_reboot_now">Reboot</string>
28+
<string name="yes">Yes</string>
29+
<string name="no">No</string>
2830

2931
<!-- Module section translation -->
3032
<string name="module_last_update">Last update:</string>
@@ -38,6 +40,8 @@
3840
<string name="manage_repos_pref">Manage repos</string>
3941
<string name="showcase_mode_pref">Lockdown mode</string>
4042
<string name="showcase_mode_desc">Lockdown mode prevent manager to do action on modules</string>
43+
<string name="prevent_reboot_pref">Prevent reboot</string>
44+
<string name="prevent_reboot_desc">Prevents unexpected reboots</string>
4145
<string name="pref_category_settings">Settings</string>
4246
<string name="pref_category_info">Info</string>
4347
<string name="show_licenses">Show licenses</string>

app/src/main/res/xml/root_preferences.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@
4747
app:summary="@string/showcase_mode_desc"
4848
app:singleLineTitle="false" />
4949

50+
<SwitchPreferenceCompat
51+
app:defaultValue="false"
52+
app:key="pref_prevent_reboot"
53+
app:icon="@drawable/ic_reboot_24"
54+
app:title="@string/prevent_reboot_pref"
55+
app:summary="@string/prevent_reboot_desc"
56+
app:singleLineTitle="false" />
57+
5058
<SwitchPreferenceCompat
5159
app:defaultValue="false"
5260
app:key="pref_wrap_text"

0 commit comments

Comments
 (0)