From a8f124704d7eb8b3490efa1033cd95d3f2d14266 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 20 Nov 2017 03:09:08 +0800 Subject: [PATCH] Allow custom update channels --- .../com/topjohnwu/magisk/MagiskManager.java | 2 + .../topjohnwu/magisk/SettingsActivity.java | 43 +++++++++++++++---- .../topjohnwu/magisk/asyncs/CheckUpdates.java | 7 +-- .../com/topjohnwu/magisk/utils/Const.java | 4 +- .../com/topjohnwu/magisk/utils/Utils.java | 6 +++ app/src/main/res/layout/alert_dialog.xml | 27 +++++------- app/src/main/res/values/arrays.xml | 1 + app/src/main/res/values/strings.xml | 2 + 8 files changed, 63 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index d0ae1fcdc..eb4931669 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -75,6 +75,7 @@ public class MagiskManager extends Application { public String localeConfig; public int updateChannel; public String bootFormat; + public String customChannelUrl; // Global resources public SharedPreferences prefs; @@ -152,6 +153,7 @@ public class MagiskManager extends Application { bootFormat = prefs.getString(Const.Key.BOOT_FORMAT, ".img"); snet_version = prefs.getInt(Const.Key.SNET_VER, -1); updateServiceVersion = prefs.getInt(Const.Key.UPDATE_SERVICE_VER, -1); + customChannelUrl = prefs.getString(Const.Key.CUSTOM_CHANNEL, ""); } public static void toast(String msg, int duration) { diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index dd955b40d..ac76a52c7 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -8,16 +8,19 @@ import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceCategory; import android.preference.PreferenceFragment; -import android.preference.PreferenceManager; import android.preference.PreferenceScreen; import android.preference.SwitchPreference; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; +import android.text.InputType; +import android.widget.EditText; +import android.widget.LinearLayout; 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.components.AlertDialogBuilder; import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Topic; @@ -71,8 +74,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { } public static class SettingsFragment extends PreferenceFragment - implements SharedPreferences.OnSharedPreferenceChangeListener, - Topic.Subscriber { + implements SharedPreferences.OnSharedPreferenceChangeListener, Topic.Subscriber { private SharedPreferences prefs; private PreferenceScreen prefScreen; @@ -86,16 +88,16 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.app_settings); - prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); - prefScreen = getPreferenceScreen(); mm = Utils.getMagiskManager(getActivity()); + prefs = mm.prefs; + prefScreen = getPreferenceScreen(); generalCatagory = (PreferenceCategory) findPreference("general"); PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk"); PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser"); Preference hideManager = findPreference("hide"); findPreference("clear").setOnPreferenceClickListener((pref) -> { - mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply(); + prefs.edit().remove(Const.Key.ETAG_KEY).apply(); mm.repoDB.clearRepo(); MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); return true; @@ -110,6 +112,32 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { namespaceMode = (ListPreference) findPreference(Const.Key.SU_MNT_NS); SwitchPreference reauth = (SwitchPreference) findPreference(Const.Key.SU_REAUTH); + updateChannel.setOnPreferenceChangeListener((pref, o) -> { + mm.updateChannel = Integer.parseInt((String) o); + if (mm.updateChannel == Const.Value.CUSTOM_CHANNEL) { + LinearLayout layout = new LinearLayout(getActivity()); + EditText url = new EditText(getActivity()); + url.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_URI); + url.setText(mm.customChannelUrl); + layout.setOrientation(LinearLayout.VERTICAL); + layout.addView(url); + LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) url.getLayoutParams(); + params.setMargins(Utils.dpInPx(15), 0, Utils.dpInPx(15), 0); + new AlertDialogBuilder(getActivity()) + .setTitle(R.string.settings_update_custom) + .setMessage(R.string.settings_update_custom_msg) + .setView(layout) + .setPositiveButton(R.string.ok, (d, i) -> { + prefs.edit().putString(Const.Key.CUSTOM_CHANNEL, url.getText().toString()).apply(); + }) + .setNegativeButton(R.string.close, (d, i) -> { + mm.updateChannel = Const.Value.STABLE_CHANNEL; + prefs.edit().putString(Const.Key.UPDATE_CHANNEL, String.valueOf(Const.Value.STABLE_CHANNEL)).apply(); + }) + .show(); + } + return true; + }); setSummary(); @@ -235,8 +263,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { mm.reloadActivity.publish(false); break; case Const.Key.UPDATE_CHANNEL: - mm.updateChannel = Utils.getPrefsInt(prefs, Const.Key.UPDATE_CHANNEL); - new CheckUpdates(true).exec(); + new CheckUpdates().exec(); break; } mm.loadConfig(); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java index 1cf6ded2b..15419d90c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/CheckUpdates.java @@ -24,7 +24,7 @@ public class CheckUpdates extends ParallelTask { @Override protected Void doInBackground(Void... voids) { MagiskManager mm = MagiskManager.get(); - String jsonStr; + String jsonStr = ""; switch (mm.updateChannel) { case Const.Value.STABLE_CHANNEL: jsonStr = WebService.getString(Const.Url.STABLE_URL); @@ -32,8 +32,9 @@ public class CheckUpdates extends ParallelTask { case Const.Value.BETA_CHANNEL: jsonStr = WebService.getString(Const.Url.BETA_URL); break; - default: - jsonStr = null; + case Const.Value.CUSTOM_CHANNEL: + jsonStr = WebService.getString(mm.customChannelUrl); + break; } try { JSONObject json = new JSONObject(jsonStr); diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java index b43551a4b..af9e41546 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -68,7 +68,7 @@ public class Const { public static class Url { public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json"; - public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json"; + public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/beta.json"; public static final String SNET_URL = "https://www.dropbox.com/s/vbq6y8pzfn4rq60/snet.apk?dl=1"; public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&page=%d"; public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s"; @@ -104,6 +104,7 @@ public class Const { // others public static final String UPDATE_NOTIFICATION = "notification"; public static final String UPDATE_CHANNEL = "update_channel"; + public static final String CUSTOM_CHANNEL = "custom_channel"; public static final String BOOT_FORMAT = "boot_format"; public static final String SNET_VER = "snet_version"; public static final String UPDATE_SERVICE_VER = "update_service_version"; @@ -121,6 +122,7 @@ public class Const { public static class Value { public static final int STABLE_CHANNEL = 0; public static final int BETA_CHANNEL = 1; + public static final int CUSTOM_CHANNEL = 2; public static final int ROOT_ACCESS_DISABLED = 0; public static final int ROOT_ACCESS_APPS_ONLY = 1; public static final int ROOT_ACCESS_ADB_ONLY = 2; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index a8314daeb..3430d7f2c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -244,4 +244,10 @@ public class Utils { } } } + + public static int dpInPx(int dp) { + Context context = MagiskManager.get(); + float scale = context.getResources().getDisplayMetrics().density; + return (int) (dp * scale + 0.5); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/alert_dialog.xml b/app/src/main/res/layout/alert_dialog.xml index 2bed6a9db..f5fb196d1 100644 --- a/app/src/main/res/layout/alert_dialog.xml +++ b/app/src/main/res/layout/alert_dialog.xml @@ -9,26 +9,19 @@ android:id="@+id/message_panel" android:layout_width="match_parent" android:layout_height="match_parent" - android:orientation="vertical"> + android:orientation="vertical" + android:paddingBottom="12dip" + android:paddingEnd="20dip" + android:paddingStart="20dip" + android:paddingTop="12dip"> - - - - - + android:padding="5dip" /> diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml index cec6cd796..9fe7656cb 100644 --- a/app/src/main/res/values/arrays.xml +++ b/app/src/main/res/values/arrays.xml @@ -75,6 +75,7 @@ @string/settings_update_stable @string/settings_update_beta + @string/settings_update_custom diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index bc0282824..bed2ae5c4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -135,6 +135,8 @@ Update Channel Stable Beta + Custom + Insert a custom URL Patched Boot Output Format Select the format of the output patched boot image.\nChoose .img to flash through fastboot/download mode; choose .img.tar to flash with ODIN. Magisk Core Only Mode