From b9e7d0faea530c28a1eb8293d66f3f4f41c3cd76 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 13 Feb 2018 03:22:41 +0800 Subject: [PATCH] Add option to restore Magisk Manager after repackage --- .../topjohnwu/magisk/SettingsActivity.java | 36 +++++++++++++++++++ src/main/res/values/strings.xml | 2 ++ src/main/res/xml/app_settings.xml | 5 +++ 3 files changed, 43 insertions(+) diff --git a/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index 6b4c15b94..42c3d5a58 100644 --- a/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -1,6 +1,7 @@ package com.topjohnwu.magisk; import android.Manifest; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; @@ -21,6 +22,7 @@ import android.widget.Toast; import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.HideManager; import com.topjohnwu.magisk.components.Activity; +import com.topjohnwu.magisk.receivers.ManagerUpdate; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.FingerprintHelper; import com.topjohnwu.magisk.utils.Topic; @@ -99,6 +101,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk"); PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser"); Preference hideManager = findPreference("hide"); + Preference restoreManager = findPreference("restore"); findPreference("clear").setOnPreferenceClickListener((pref) -> { prefs.edit().remove(Const.Key.ETAG_KEY).apply(); mm.repoDB.clearRepo(); @@ -152,6 +155,38 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { suCategory.removePreference(fingerprint); } + if (mm.magiskVersionCode >= 1440) { + if (mm.getPackageName().equals(Const.ORIG_PKG_NAME)) { + hideManager.setOnPreferenceClickListener((pref) -> { + Utils.runWithPermission(getActivity(), + Manifest.permission.WRITE_EXTERNAL_STORAGE, + () -> new HideManager(getActivity()).exec()); + return true; + }); + generalCatagory.removePreference(restoreManager); + } else { + if (Utils.checkNetworkStatus()) { + restoreManager.setOnPreferenceClickListener((pref) -> { + Utils.runWithPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE, () -> { + Intent intent = new Intent(mm, ManagerUpdate.class); + intent.putExtra(Const.Key.INTENT_SET_LINK, mm.managerLink); + intent.putExtra(Const.Key.INTENT_SET_FILENAME, + Utils.fmt("MagiskManager-v%s(%d).apk", + mm.remoteManagerVersionString, mm.remoteManagerVersionCode)); + mm.sendBroadcast(intent); + }); + return true; + }); + } else { + generalCatagory.removePreference(restoreManager); + } + generalCatagory.removePreference(hideManager); + } + } else { + generalCatagory.removePreference(restoreManager); + generalCatagory.removePreference(hideManager); + } + if (mm.getPackageName().equals(Const.ORIG_PKG_NAME) && mm.magiskVersionCode >= 1440) { hideManager.setOnPreferenceClickListener((pref) -> { Utils.runWithPermission(getActivity(), @@ -159,6 +194,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { () -> new HideManager(getActivity()).exec()); return true; }); + generalCatagory.removePreference(restoreManager); } else { generalCatagory.removePreference(hideManager); } diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index fc369bf99..a2f969de0 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -135,6 +135,8 @@ Clear the cached information for online repos, forces the app to refresh online Hide Magisk Manager Repackage Magisk Manager with random package name + Restore Magisk Manager + Restore Magisk Manager with original package Language (System Default) Update Settings diff --git a/src/main/res/xml/app_settings.xml b/src/main/res/xml/app_settings.xml index 9995f235f..677828165 100644 --- a/src/main/res/xml/app_settings.xml +++ b/src/main/res/xml/app_settings.xml @@ -20,6 +20,11 @@ android:title="@string/settings_hide_manager_title" android:summary="@string/settings_hide_manager_summary" /> + +