From a78950e822148a877b5ad5d231eceb5a3432c9ec Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 28 Aug 2017 00:27:10 +0800 Subject: [PATCH] Reduce boilerplate --- .../java/com/topjohnwu/magisk/FlashActivity.java | 2 +- .../java/com/topjohnwu/magisk/MagiskFragment.java | 4 ++-- .../com/topjohnwu/magisk/MagiskLogFragment.java | 7 +++---- .../com/topjohnwu/magisk/SettingsActivity.java | 14 +++++++------- .../com/topjohnwu/magisk/components/Activity.java | 5 +++++ .../com/topjohnwu/magisk/components/Fragment.java | 5 +++++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java b/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java index eb37770ef..6cf3ffbe2 100644 --- a/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/FlashActivity.java @@ -35,7 +35,7 @@ public class FlashActivity extends Activity { @OnClick(R.id.reboot) public void reboot() { - Shell.getShell(this).su_raw("reboot"); + getShell().su_raw("reboot"); } @Override diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index f37df6d87..160335a5f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -145,7 +145,7 @@ public class MagiskFragment extends Fragment @Override public void onDownloadDone(Uri uri) { if (Shell.rootAccess()) { - Shell.getShell(getActivity()).su_raw( + getShell().su_raw( "rm -f /dev/.magisk", "echo \"BOOTIMAGE=" + boot + "\" >> /dev/.magisk", "echo \"KEEPFORCEENCRYPT=" + String.valueOf(enc) + "\" >> /dev/.magisk", @@ -209,7 +209,7 @@ public class MagiskFragment extends Fragment @Override public void onFinish() { progress.setMessage(getString(R.string.reboot_countdown, 0)); - Shell.getShell(getActivity()).su_raw( + getShell().su_raw( "mv -f " + uninstaller + " /cache/" + UNINSTALLER, "mv -f " + utils + " /data/magisk/" + UTIL_FUNCTIONS, "reboot" diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java index b627eefbc..1094f1e22 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskLogFragment.java @@ -130,16 +130,15 @@ public class MagiskLogFragment extends Fragment { @SuppressLint("DefaultLocale") @Override protected Object doInBackground(Object... params) { - MagiskManager magiskManager = MagiskLogFragment.this.getApplication(); mode = (int) params[0]; switch (mode) { case 0: StringBuildingList logList = new StringBuildingList(); - Shell.getShell(magiskManager).su(logList, "cat " + MAGISK_LOG); + getShell().su(logList, "cat " + MAGISK_LOG); return logList.toString(); case 1: - Shell.getShell(magiskManager).su_raw("echo > " + MAGISK_LOG); + getShell().su_raw("echo > " + MAGISK_LOG); SnackbarMaker.make(txtLog, R.string.logs_cleared, Snackbar.LENGTH_SHORT).show(); return ""; @@ -160,7 +159,7 @@ public class MagiskLogFragment extends Fragment { try (FileWriter out = new FileWriter(targetFile)) { FileWritingList fileWritingList = new FileWritingList(out); - Shell.getShell(magiskManager).su(fileWritingList, "cat " + MAGISK_LOG); + getShell().su(fileWritingList, "cat " + MAGISK_LOG); } catch (IOException e) { e.printStackTrace(); return false; diff --git a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java index d4baae8cd..8c2df1d05 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SettingsActivity.java @@ -69,7 +69,7 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { return new Topic[] { getApplicationContext().reloadActivity }; } - public static class SettingsFragment extends PreferenceFragment + public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener, Topic.Subscriber { @@ -197,28 +197,28 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { case "disable": enabled = prefs.getBoolean("disable", false); if (enabled) { - Utils.createFile(Shell.getShell(magiskManager), MagiskManager.MAGISK_DISABLE_FILE); + Utils.createFile(getShell(), MagiskManager.MAGISK_DISABLE_FILE); } else { - Utils.removeItem(Shell.getShell(magiskManager), MagiskManager.MAGISK_DISABLE_FILE); + Utils.removeItem(getShell(), MagiskManager.MAGISK_DISABLE_FILE); } Toast.makeText(getActivity(), R.string.settings_reboot_toast, Toast.LENGTH_LONG).show(); break; case "magiskhide": enabled = prefs.getBoolean("magiskhide", false); if (enabled) { - Utils.enableMagiskHide(Shell.getShell(magiskManager)); + Utils.enableMagiskHide(getShell()); } else { - Utils.disableMagiskHide(Shell.getShell(magiskManager)); + Utils.disableMagiskHide(getShell()); } break; case "hosts": enabled = prefs.getBoolean("hosts", false); if (enabled) { - Shell.getShell(magiskManager).su_raw( + getShell().su_raw( "cp -af /system/etc/hosts /magisk/.core/hosts", "mount -o bind /magisk/.core/hosts /system/etc/hosts"); } else { - Shell.getShell(magiskManager).su_raw( + getShell().su_raw( "umount -l /system/etc/hosts", "rm -f /magisk/.core/hosts"); } diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java index 0b410486b..90ea223a1 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Activity.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Activity.java @@ -8,6 +8,7 @@ import android.view.WindowManager; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; +import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Topic; public class Activity extends AppCompatActivity { @@ -40,6 +41,10 @@ public class Activity extends AppCompatActivity { return (MagiskManager) super.getApplicationContext(); } + public Shell getShell() { + return Shell.getShell(this); + } + protected void setFloating() { boolean isTablet = getResources().getBoolean(R.bool.isTablet); if (isTablet) { diff --git a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java index 74df99c32..494ae5637 100644 --- a/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/components/Fragment.java @@ -1,6 +1,7 @@ package com.topjohnwu.magisk.components; import com.topjohnwu.magisk.MagiskManager; +import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Utils; @@ -10,6 +11,10 @@ public class Fragment extends android.support.v4.app.Fragment { return Utils.getMagiskManager(getActivity()); } + public Shell getShell() { + return Shell.getShell(getActivity()); + } + @Override public void onResume() { super.onResume();