Allow custom update channels

This commit is contained in:
topjohnwu 2017-11-20 03:09:08 +08:00
parent eed2816491
commit a8f124704d
8 changed files with 63 additions and 29 deletions

View File

@ -75,6 +75,7 @@ public class MagiskManager extends Application {
public String localeConfig; public String localeConfig;
public int updateChannel; public int updateChannel;
public String bootFormat; public String bootFormat;
public String customChannelUrl;
// Global resources // Global resources
public SharedPreferences prefs; public SharedPreferences prefs;
@ -152,6 +153,7 @@ public class MagiskManager extends Application {
bootFormat = prefs.getString(Const.Key.BOOT_FORMAT, ".img"); bootFormat = prefs.getString(Const.Key.BOOT_FORMAT, ".img");
snet_version = prefs.getInt(Const.Key.SNET_VER, -1); snet_version = prefs.getInt(Const.Key.SNET_VER, -1);
updateServiceVersion = prefs.getInt(Const.Key.UPDATE_SERVICE_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) { public static void toast(String msg, int duration) {

View File

@ -8,16 +8,19 @@ import android.preference.ListPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.PreferenceCategory; import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.preference.SwitchPreference; import android.preference.SwitchPreference;
import android.support.v7.app.ActionBar; import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.asyncs.HideManager; import com.topjohnwu.magisk.asyncs.HideManager;
import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.utils.Const; import com.topjohnwu.magisk.utils.Const;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
@ -71,8 +74,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
} }
public static class SettingsFragment extends PreferenceFragment public static class SettingsFragment extends PreferenceFragment
implements SharedPreferences.OnSharedPreferenceChangeListener, implements SharedPreferences.OnSharedPreferenceChangeListener, Topic.Subscriber {
Topic.Subscriber {
private SharedPreferences prefs; private SharedPreferences prefs;
private PreferenceScreen prefScreen; private PreferenceScreen prefScreen;
@ -86,16 +88,16 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.app_settings); addPreferencesFromResource(R.xml.app_settings);
prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
prefScreen = getPreferenceScreen();
mm = Utils.getMagiskManager(getActivity()); mm = Utils.getMagiskManager(getActivity());
prefs = mm.prefs;
prefScreen = getPreferenceScreen();
generalCatagory = (PreferenceCategory) findPreference("general"); generalCatagory = (PreferenceCategory) findPreference("general");
PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk"); PreferenceCategory magiskCategory = (PreferenceCategory) findPreference("magisk");
PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser"); PreferenceCategory suCategory = (PreferenceCategory) findPreference("superuser");
Preference hideManager = findPreference("hide"); Preference hideManager = findPreference("hide");
findPreference("clear").setOnPreferenceClickListener((pref) -> { findPreference("clear").setOnPreferenceClickListener((pref) -> {
mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply(); prefs.edit().remove(Const.Key.ETAG_KEY).apply();
mm.repoDB.clearRepo(); mm.repoDB.clearRepo();
MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); MagiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
return true; return true;
@ -110,6 +112,32 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
namespaceMode = (ListPreference) findPreference(Const.Key.SU_MNT_NS); namespaceMode = (ListPreference) findPreference(Const.Key.SU_MNT_NS);
SwitchPreference reauth = (SwitchPreference) findPreference(Const.Key.SU_REAUTH); 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(); setSummary();
@ -235,8 +263,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
mm.reloadActivity.publish(false); mm.reloadActivity.publish(false);
break; break;
case Const.Key.UPDATE_CHANNEL: case Const.Key.UPDATE_CHANNEL:
mm.updateChannel = Utils.getPrefsInt(prefs, Const.Key.UPDATE_CHANNEL); new CheckUpdates().exec();
new CheckUpdates(true).exec();
break; break;
} }
mm.loadConfig(); mm.loadConfig();

View File

@ -24,7 +24,7 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
MagiskManager mm = MagiskManager.get(); MagiskManager mm = MagiskManager.get();
String jsonStr; String jsonStr = "";
switch (mm.updateChannel) { switch (mm.updateChannel) {
case Const.Value.STABLE_CHANNEL: case Const.Value.STABLE_CHANNEL:
jsonStr = WebService.getString(Const.Url.STABLE_URL); jsonStr = WebService.getString(Const.Url.STABLE_URL);
@ -32,8 +32,9 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
case Const.Value.BETA_CHANNEL: case Const.Value.BETA_CHANNEL:
jsonStr = WebService.getString(Const.Url.BETA_URL); jsonStr = WebService.getString(Const.Url.BETA_URL);
break; break;
default: case Const.Value.CUSTOM_CHANNEL:
jsonStr = null; jsonStr = WebService.getString(mm.customChannelUrl);
break;
} }
try { try {
JSONObject json = new JSONObject(jsonStr); JSONObject json = new JSONObject(jsonStr);

View File

@ -68,7 +68,7 @@ public class Const {
public static class Url { public static class Url {
public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/MagiskManager/update/stable.json"; 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 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 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"; public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s";
@ -104,6 +104,7 @@ public class Const {
// others // others
public static final String UPDATE_NOTIFICATION = "notification"; public static final String UPDATE_NOTIFICATION = "notification";
public static final String UPDATE_CHANNEL = "update_channel"; 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 BOOT_FORMAT = "boot_format";
public static final String SNET_VER = "snet_version"; public static final String SNET_VER = "snet_version";
public static final String UPDATE_SERVICE_VER = "update_service_version"; public static final String UPDATE_SERVICE_VER = "update_service_version";
@ -121,6 +122,7 @@ public class Const {
public static class Value { public static class Value {
public static final int STABLE_CHANNEL = 0; public static final int STABLE_CHANNEL = 0;
public static final int BETA_CHANNEL = 1; 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_DISABLED = 0;
public static final int ROOT_ACCESS_APPS_ONLY = 1; public static final int ROOT_ACCESS_APPS_ONLY = 1;
public static final int ROOT_ACCESS_ADB_ONLY = 2; public static final int ROOT_ACCESS_ADB_ONLY = 2;

View File

@ -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);
}
} }

View File

@ -9,26 +9,19 @@
android:id="@+id/message_panel" android:id="@+id/message_panel"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="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">
<ScrollView <TextView
android:id="@+id/message"
style="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:overScrollMode="ifContentScrolls" android:padding="5dip" />
android:paddingBottom="12dip"
android:paddingEnd="20dip"
android:paddingStart="20dip"
android:paddingTop="12dip">
<TextView
android:id="@+id/message"
style="?android:attr/textAppearanceMedium"
android:textColor="?android:attr/textColorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
</ScrollView>
</LinearLayout> </LinearLayout>

View File

@ -75,6 +75,7 @@
<string-array name="update_channel"> <string-array name="update_channel">
<item>@string/settings_update_stable</item> <item>@string/settings_update_stable</item>
<item>@string/settings_update_beta</item> <item>@string/settings_update_beta</item>
<item>@string/settings_update_custom</item>
</string-array> </string-array>
<string-array name="boot_formats"> <string-array name="boot_formats">

View File

@ -135,6 +135,8 @@
<string name="settings_update_channel_title">Update Channel</string> <string name="settings_update_channel_title">Update Channel</string>
<string name="settings_update_stable">Stable</string> <string name="settings_update_stable">Stable</string>
<string name="settings_update_beta">Beta</string> <string name="settings_update_beta">Beta</string>
<string name="settings_update_custom">Custom</string>
<string name="settings_update_custom_msg">Insert a custom URL</string>
<string name="settings_boot_format_title">Patched Boot Output Format</string> <string name="settings_boot_format_title">Patched Boot Output Format</string>
<string name="settings_boot_format_summary">Select the format of the output patched boot image.\nChoose .img to flash through fastboot/download mode; choose .img.tar to flash with ODIN.</string> <string name="settings_boot_format_summary">Select the format of the output patched boot image.\nChoose .img to flash through fastboot/download mode; choose .img.tar to flash with ODIN.</string>
<string name="settings_core_only_title">Magisk Core Only Mode</string> <string name="settings_core_only_title">Magisk Core Only Mode</string>