Skip to content

Commit

Permalink
Centralize configuration management
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Jan 21, 2019
1 parent 8582167 commit a189dec
Show file tree
Hide file tree
Showing 32 changed files with 583 additions and 418 deletions.
8 changes: 4 additions & 4 deletions app/src/full/java/com/topjohnwu/magisk/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import android.view.View;

import com.google.android.material.navigation.NavigationView;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.utils.Topic;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.components.BaseActivity;
Expand Down Expand Up @@ -125,10 +125,10 @@ public void onPublish(int topic, Object[] result) {
public void checkHideSection() {
Menu menu = navigationView.getMenu();
menu.findItem(R.id.magiskhide).setVisible(Shell.rootAccess() &&
app.prefs.getBoolean(Const.Key.MAGISKHIDE, false));
menu.findItem(R.id.modules).setVisible(Shell.rootAccess() && Data.magiskVersionCode >= 0);
(boolean) Config.get(Config.Key.MAGISKHIDE));
menu.findItem(R.id.modules).setVisible(Shell.rootAccess() && Config.magiskVersionCode >= 0);
menu.findItem(R.id.downloads).setVisible(Networking.checkNetworkStatus(this)
&& Shell.rootAccess() && Data.magiskVersionCode >= 0);
&& Shell.rootAccess() && Config.magiskVersionCode >= 0);
menu.findItem(R.id.log).setVisible(Shell.rootAccess());
menu.findItem(R.id.superuser).setVisible(Utils.showSuperUser());
}
Expand Down
14 changes: 6 additions & 8 deletions app/src/full/java/com/topjohnwu/magisk/SplashActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import android.os.Bundle;
import android.text.TextUtils;

import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.tasks.CheckUpdates;
import com.topjohnwu.core.tasks.UpdateRepos;
import com.topjohnwu.core.utils.LocaleManager;
Expand All @@ -27,9 +27,9 @@ protected void onCreate(Bundle savedInstanceState) {
// Dynamic detect all locales
LocaleManager.loadAvailableLocales(R.string.app_changelog);

String pkg = app.mDB.getStrings(Const.Key.SU_MANAGER, null);
String pkg = Config.get(Config.Key.SU_MANAGER);
if (pkg != null && getPackageName().equals(BuildConfig.APPLICATION_ID)) {
app.mDB.setStrings(Const.Key.SU_MANAGER, null);
Config.remove(Config.Key.SU_MANAGER);
Shell.su("pm uninstall " + pkg).exec();
}
if (TextUtils.equals(pkg, getPackageName())) {
Expand All @@ -41,12 +41,13 @@ protected void onCreate(Bundle savedInstanceState) {
}

// Magisk working as expected
if (Shell.rootAccess() && Data.magiskVersionCode > 0) {
if (Shell.rootAccess() && Config.magiskVersionCode > 0) {
// Load modules
Utils.loadModules();
}

Data.importPrefs();
// Set default configs
Config.initialize();

// Create notification channel on Android O
Notifications.setup(this);
Expand All @@ -64,9 +65,6 @@ protected void onCreate(Bundle savedInstanceState) {
new UpdateRepos().exec();
}

// Write back default values
Data.writeConfig();

app.init = true;

Intent intent = new Intent(this, ClassMap.get(MainActivity.class));
Expand Down
15 changes: 7 additions & 8 deletions app/src/full/java/com/topjohnwu/magisk/SuRequestActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import android.widget.Spinner;
import android.widget.TextView;

import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.container.Policy;
import com.topjohnwu.magisk.components.BaseActivity;
import com.topjohnwu.magisk.utils.FingerprintHelper;
Expand Down Expand Up @@ -103,14 +102,14 @@ protected void onResponse() throws IOException {
return;
}

switch (Data.suResponseType) {
case Const.Value.SU_AUTO_DENY:
switch ((int) Config.get(Config.Key.SU_AUTO_RESPONSE)) {
case Config.Value.SU_AUTO_DENY:
handleAction(Policy.DENY, 0);
return;
case Const.Value.SU_AUTO_ALLOW:
case Config.Value.SU_AUTO_ALLOW:
handleAction(Policy.ALLOW, 0);
return;
case Const.Value.SU_PROMPT:
case Config.Value.SU_PROMPT:
default:
}

Expand All @@ -134,7 +133,7 @@ protected void onResponse() throws IOException {
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
timeout.setAdapter(adapter);

timer = new CountDownTimer(Data.suRequestTimeout * 1000, 1000) {
timer = new CountDownTimer((int) Config.get(Config.Key.SU_REQUEST_TIMEOUT) * 1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
deny_btn.setText(getString(R.string.deny_with_str, "(" + millisUntilFinished / 1000 + ")"));
Expand Down Expand Up @@ -210,7 +209,7 @@ private void handleAction() {
}

private void handleAction(int action) {
handleAction(action, Const.Value.timeoutList[timeout.getSelectedItemPosition()]);
handleAction(action, Config.Value.TIMEOUT_LIST[timeout.getSelectedItemPosition()]);
}

private void handleAction(int action, int time) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import android.widget.Toast;

import com.topjohnwu.core.App;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.utils.LocaleManager;
import com.topjohnwu.core.utils.Topic;
import com.topjohnwu.magisk.R;
Expand Down Expand Up @@ -59,7 +59,7 @@ protected void attachBaseContext(Context base) {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Topic.subscribe(this);
if (Data.isDarkTheme && getDarkTheme() != -1) {
if (getDarkTheme() != -1 && (boolean) Config.get(Config.Key.DARK_THEME)) {
setTheme(getDarkTheme());
}
super.onCreate(savedInstanceState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.FlashActivity;
Expand Down Expand Up @@ -65,10 +65,10 @@ private void patchBoot(BaseActivity a) {
private void downloadOnly(BaseActivity a) {
a.runWithExternalRW(() -> {
String filename = Utils.fmt("Magisk-v%s(%d).zip",
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
Config.remoteMagiskVersionString, Config.remoteMagiskVersionCode);
File zip = new File(Const.EXTERNAL_PATH, filename);
ProgressNotification progress = new ProgressNotification(filename);
Networking.get(Data.magiskLink)
Networking.get(Config.magiskLink)
.setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(zip, f -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.net.Uri;
import android.text.TextUtils;

import com.topjohnwu.core.Data;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.AppUtils;
Expand All @@ -17,7 +17,7 @@ public class MagiskInstallDialog extends CustomAlertDialog {
public MagiskInstallDialog(BaseActivity a) {
super(a);
String filename = Utils.fmt("Magisk-v%s(%d).zip",
Data.remoteMagiskVersionString, Data.remoteMagiskVersionCode);
Config.remoteMagiskVersionString, Config.remoteMagiskVersionCode);
setTitle(a.getString(R.string.repo_install_title, a.getString(R.string.magisk)));
setMessage(a.getString(R.string.repo_install_msg, filename));
setCancelable(true);
Expand All @@ -35,13 +35,13 @@ public MagiskInstallDialog(BaseActivity a) {
new InstallMethodDialog(a, options).show();
});
setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(Data.magiskNoteLink)) {
if (!TextUtils.isEmpty(Config.magiskNoteLink)) {
setNeutralButton(R.string.release_notes, (d, i) -> {
if (Data.magiskNoteLink.contains("forum.xda-developers")) {
if (Config.magiskNoteLink.contains("forum.xda-developers")) {
// Open forum links in browser
AppUtils.openLink(a, Uri.parse(Data.magiskNoteLink));
AppUtils.openLink(a, Uri.parse(Config.magiskNoteLink));
} else {
MarkDownWindow.show(a, null, Data.magiskNoteLink);
MarkDownWindow.show(a, null, Config.magiskNoteLink);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.text.TextUtils;

import com.topjohnwu.core.Data;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.DownloadApp;
Expand All @@ -14,14 +14,14 @@ public class ManagerInstallDialog extends CustomAlertDialog {
public ManagerInstallDialog(@NonNull BaseActivity a) {
super(a);
String name = Utils.fmt("MagiskManager v%s(%d)",
Data.remoteManagerVersionString, Data.remoteManagerVersionCode);
Config.remoteManagerVersionString, Config.remoteManagerVersionCode);
setTitle(a.getString(R.string.repo_install_title, a.getString(R.string.app_name)));
setMessage(a.getString(R.string.repo_install_msg, name));
setCancelable(true);
setPositiveButton(R.string.install, (d, i) -> DownloadApp.upgrade(name));
setNegativeButton(R.string.no_thanks, null);
if (!TextUtils.isEmpty(Data.managerNoteLink)) {
setNeutralButton(R.string.app_changelog, (d, i) -> MarkDownWindow.show(a, null, Data.managerNoteLink));
if (!TextUtils.isEmpty(Config.managerNoteLink)) {
setNeutralButton(R.string.app_changelog, (d, i) -> MarkDownWindow.show(a, null, Config.managerNoteLink));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import android.os.Build;

import com.topjohnwu.core.App;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.R;
Expand Down Expand Up @@ -52,7 +52,7 @@ public static void magiskUpdate() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(app, Const.ID.UPDATE_NOTIFICATION_CHANNEL);
builder.setSmallIcon(R.drawable.ic_magisk_outline)
.setContentTitle(app.getString(R.string.magisk_update_title))
.setContentText(app.getString(R.string.magisk_update_available, Data.remoteMagiskVersionString))
.setContentText(app.getString(R.string.magisk_update_available, Config.remoteMagiskVersionString))
.setVibrate(new long[]{0, 100, 100, 100})
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Expand All @@ -63,11 +63,11 @@ public static void magiskUpdate() {
public static void managerUpdate() {
App app = App.self;
String name = Utils.fmt("MagiskManager v%s(%d)",
Data.remoteManagerVersionString, Data.remoteManagerVersionCode);
Config.remoteManagerVersionString, Config.remoteManagerVersionCode);

Intent intent = new Intent(app, ClassMap.get(GeneralReceiver.class));
intent.setAction(Const.Key.BROADCAST_MANAGER_UPDATE);
intent.putExtra(Const.Key.INTENT_SET_LINK, Data.managerLink);
intent.putExtra(Const.Key.INTENT_SET_LINK, Config.managerLink);
intent.putExtra(Const.Key.INTENT_SET_NAME, name);
PendingIntent pendingIntent = PendingIntent.getBroadcast(app,
Const.ID.APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import android.text.TextUtils;
import android.widget.Toast;

import com.topjohnwu.core.Config;
import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.utils.Utils;
import com.topjohnwu.magisk.ClassMap;
import com.topjohnwu.magisk.FlashActivity;
Expand Down Expand Up @@ -39,11 +39,11 @@ public UninstallDialog(@NonNull Activity activity) {
}
});
});
if (!TextUtils.isEmpty(Data.uninstallerLink)) {
if (!TextUtils.isEmpty(Config.uninstallerLink)) {
setPositiveButton(R.string.complete_uninstall, (d, i) -> {
File zip = new File(activity.getFilesDir(), "uninstaller.zip");
ProgressNotification progress = new ProgressNotification(zip.getName());
Networking.get(Data.uninstallerLink)
Networking.get(Config.uninstallerLink)
.setDownloadProgressListener(progress)
.setErrorHandler(((conn, e) -> progress.dlFail()))
.getAsFile(zip, f -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.topjohnwu.core.Const;
import com.topjohnwu.core.Data;
import com.topjohnwu.core.Config;
import com.topjohnwu.core.tasks.CheckUpdates;
import com.topjohnwu.core.tasks.SafetyNet;
import com.topjohnwu.core.utils.ISafetyNetHelper;
Expand Down Expand Up @@ -112,7 +111,7 @@ void safetyNet() {
@OnClick(R.id.install_button)
void install() {
// Show Manager update first
if (Data.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
if (Config.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
new ManagerInstallDialog((BaseActivity) requireActivity()).show();
return;
}
Expand All @@ -137,10 +136,10 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
expandableContainer.expandLayout = expandLayout;
setupExpandable();

keepVerityChkbox.setChecked(Data.keepVerity);
keepVerityChkbox.setOnCheckedChangeListener((view, checked) -> Data.keepVerity = checked);
keepEncChkbox.setChecked(Data.keepEnc);
keepEncChkbox.setOnCheckedChangeListener((view, checked) -> Data.keepEnc = checked);
keepVerityChkbox.setChecked(Config.keepVerity);
keepVerityChkbox.setOnCheckedChangeListener((view, checked) -> Config.keepVerity = checked);
keepEncChkbox.setChecked(Config.keepEnc);
keepEncChkbox.setOnCheckedChangeListener((view, checked) -> Config.keepEnc = checked);

mSwipeRefreshLayout.setOnRefreshListener(this);
updateUI();
Expand All @@ -150,7 +149,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c

@Override
public void onRefresh() {
Data.loadMagiskInfo();
Config.loadMagiskInfo();
updateUI();

magiskUpdateText.setText(R.string.checking_for_updates);
Expand All @@ -160,8 +159,8 @@ public void onRefresh() {
safetyNetStatusText.setText(R.string.safetyNet_check_text);

Topic.reset(getSubscribedTopics());
Data.remoteMagiskVersionString = null;
Data.remoteMagiskVersionCode = -1;
Config.remoteMagiskVersionString = null;
Config.remoteMagiskVersionCode = -1;
collapse();

shownDialog = false;
Expand Down Expand Up @@ -216,18 +215,18 @@ private void updateUI() {
magiskUpdate.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
installOptionCard.setVisibility(hasNetwork ? View.VISIBLE : View.GONE);
uninstallButton.setVisibility(hasRoot ? View.VISIBLE : View.GONE);
coreOnlyNotice.setVisibility(app.prefs.getBoolean(Const.Key.COREONLY, false) ? View.VISIBLE : View.GONE);
coreOnlyNotice.setVisibility(Config.get(Config.Key.COREONLY) ? View.VISIBLE : View.GONE);

int image, color;

if (Data.magiskVersionCode < 0) {
if (Config.magiskVersionCode < 0) {
color = colorBad;
image = R.drawable.ic_cancel;
magiskVersionText.setText(R.string.magisk_version_error);
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + Data.magiskVersionString));
magiskVersionText.setText(getString(R.string.current_magisk_title, "v" + Config.magiskVersionString));
}

magiskStatusIcon.setImageResource(image);
Expand All @@ -239,19 +238,19 @@ private void updateCheckUI() {

safetyNetCard.setVisibility(hasGms() ? View.VISIBLE : View.GONE);

if (Data.remoteMagiskVersionCode < 0) {
if (Config.remoteMagiskVersionCode < 0) {
color = colorNeutral;
image = R.drawable.ic_help;
magiskUpdateText.setText(R.string.invalid_update_channel);
installButton.setVisibility(View.GONE);
} else {
color = colorOK;
image = R.drawable.ic_check_circle;
magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + Data.remoteMagiskVersionString));
magiskUpdateText.setText(getString(R.string.install_magisk_title, "v" + Config.remoteMagiskVersionString));
installButton.setVisibility(View.VISIBLE);
if (Data.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
if (Config.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
installText.setText(getString(R.string.update, getString(R.string.app_name)));
} else if (Data.magiskVersionCode > 0 && Data.remoteMagiskVersionCode > Data.magiskVersionCode) {
} else if (Config.magiskVersionCode > 0 && Config.remoteMagiskVersionCode > Config.magiskVersionCode) {
installText.setText(getString(R.string.update, getString(R.string.magisk)));
} else {
installText.setText(R.string.install);
Expand Down
Loading

0 comments on commit a189dec

Please sign in to comment.