From 668d85d14ebe6b20c870ae32f4e83adaa1a1226c Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 20 Jul 2017 01:44:32 +0800 Subject: [PATCH] Improve notification support --- .../com/topjohnwu/magisk/MagiskFragment.java | 73 ++++++++++--------- .../com/topjohnwu/magisk/MagiskManager.java | 2 + .../com/topjohnwu/magisk/MainActivity.java | 11 ++- .../com/topjohnwu/magisk/SplashActivity.java | 8 +- .../magisk/receivers/ManagerUpdate.java | 5 +- .../com/topjohnwu/magisk/utils/Utils.java | 6 +- 6 files changed, 63 insertions(+), 42 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java index 339d8b368..e1f3bbd80 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskFragment.java @@ -2,7 +2,9 @@ package com.topjohnwu.magisk; import android.animation.Animator; import android.animation.ValueAnimator; +import android.app.NotificationManager; import android.app.ProgressDialog; +import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Bundle; @@ -49,7 +51,8 @@ import butterknife.Unbinder; public class MagiskFragment extends Fragment implements CallbackEvent.Listener, SwipeRefreshLayout.OnRefreshListener { - private static boolean noDialog = false; + public static final String SHOW_DIALOG = "dialog"; + private static int expandHeight = 0; private static boolean mExpanded = false; @@ -126,31 +129,35 @@ public class MagiskFragment extends Fragment .setMessage(getString(R.string.repo_install_msg, filename)) .setCancelable(true) .setPositiveButton(Shell.rootAccess() ? R.string.install : R.string.download, - (d, i) -> - Utils.dlAndReceive( - getActivity(), - new DownloadReceiver() { - private String boot = finalBootImage; - private boolean enc = keepEncChkbox.isChecked(); - private boolean verity = keepVerityChkbox.isChecked(); + (d, i) -> { + ((NotificationManager) getActivity() + .getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll(); + Utils.dlAndReceive( + getActivity(), + new DownloadReceiver() { + private String boot = finalBootImage; + private boolean enc = keepEncChkbox.isChecked(); + private boolean verity = keepVerityChkbox.isChecked(); - @Override - public void onDownloadDone(Uri uri) { - if (Shell.rootAccess()) { - magiskManager.shell.su_raw( - "rm -f /dev/.magisk", - "echo \"BOOTIMAGE=" + boot + "\" >> /dev/.magisk", - "echo \"KEEPFORCEENCRYPT=" + String.valueOf(enc) + "\" >> /dev/.magisk", - "echo \"KEEPVERITY=" + String.valueOf(verity) + "\" >> /dev/.magisk" - ); - startActivity(new Intent(getActivity(), FlashActivity.class).setData(uri)); - } else { - Utils.showUriSnack(getActivity(), uri); + @Override + public void onDownloadDone(Uri uri) { + if (Shell.rootAccess()) { + magiskManager.shell.su_raw( + "rm -f /dev/.magisk", + "echo \"BOOTIMAGE=" + boot + "\" >> /dev/.magisk", + "echo \"KEEPFORCEENCRYPT=" + String.valueOf(enc) + "\" >> /dev/.magisk", + "echo \"KEEPVERITY=" + String.valueOf(verity) + "\" >> /dev/.magisk" + ); + startActivity(new Intent(getActivity(), FlashActivity.class).setData(uri)); + } else { + Utils.showUriSnack(getActivity(), uri); + } } - } - }, - magiskManager.magiskLink, - Utils.getLegalFilename(filename))) + }, + magiskManager.magiskLink, + Utils.getLegalFilename(filename)); + } + ) .setNeutralButton(R.string.release_notes, (d, i) -> { if (magiskManager.releaseNoteLink != null) { Intent openReleaseNoteLink = new Intent(Intent.ACTION_VIEW, Uri.parse(magiskManager.releaseNoteLink)); @@ -241,19 +248,11 @@ public class MagiskFragment extends Fragment mSwipeRefreshLayout.setOnRefreshListener(this); - if (magiskManager.magiskVersionCode < 0 && Shell.rootAccess() && !noDialog) { - noDialog = true; - new AlertDialogBuilder(getActivity()) - .setTitle(R.string.no_magisk_title) - .setMessage(R.string.no_magisk_msg) - .setCancelable(true) - .setPositiveButton(R.string.goto_install, (d, i) -> {}) - .setNegativeButton(R.string.no_thanks, null) - .show(); - } - updateUI(); + if (getArguments() != null && getArguments().getBoolean(SHOW_DIALOG)) + install(); + return v; } @@ -272,7 +271,6 @@ public class MagiskFragment extends Fragment magiskManager.remoteMagiskVersionString = null; magiskManager.remoteMagiskVersionCode = -1; collapse(); - noDialog = false; // Trigger state check if (Utils.checkNetworkStatus(magiskManager)) { @@ -410,6 +408,9 @@ public class MagiskFragment extends Fragment magiskUpdateProgress.setVisibility(View.GONE); mSwipeRefreshLayout.setRefreshing(false); + + if (magiskManager.remoteMagiskVersionCode > magiskManager.magiskVersionCode) + install(); } private void updateSafetyNetUI() { diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index c5cbf038c..d8fe340e5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -32,6 +32,8 @@ public class MagiskManager extends Application { public static final String UNINSTALLER = "magisk_uninstaller.sh"; public static final String UTIL_FUNCTIONS= "util_functions.sh"; public static final String INTENT_SECTION = "section"; + public static final String INTENT_VERSION = "version"; + public static final String INTENT_LINK = "link"; public static final String BUSYBOX_VERSION = "1.26.2"; public static final String MAGISKHIDE_PROP = "persist.magisk.hide"; public static final String DISABLE_INDICATION_PROP = "ro.magisk.disable"; diff --git a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java index 083c0413e..06b9078d9 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MainActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/MainActivity.java @@ -140,9 +140,11 @@ public class MainActivity extends Activity if (item != null) { switch (item) { case "magisk": - case "install": itemId = R.id.magisk; break; + case "install": + itemId = -1; + break; case "superuser": itemId = R.id.superuser; break; @@ -174,6 +176,13 @@ public class MainActivity extends Activity mDrawerItem = itemId; navigationView.setCheckedItem(itemId); switch (itemId) { + case -1: + Bundle args = new Bundle(); + args.putBoolean(MagiskFragment.SHOW_DIALOG, true); + Fragment frag = new MagiskFragment(); + frag.setArguments(args); + displayFragment(frag, "magisk", true); + break; case R.id.magisk: displayFragment(new MagiskFragment(), "magisk", true); break; diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index e0e5b43f7..59e4aa988 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -24,8 +24,14 @@ public class SplashActivity extends Activity{ super.onCreate(savedInstanceState); + MagiskManager magiskManager = getApplicationContext(); + // Init the info and configs and root sh - getApplicationContext().init(); + magiskManager.init(); + + // Get possible additional info from intent + magiskManager.remoteMagiskVersionString = getIntent().getStringExtra(MagiskManager.INTENT_VERSION); + magiskManager.magiskLink = getIntent().getStringExtra(MagiskManager.INTENT_LINK); // Now fire all async tasks new LoadModules(this) diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/ManagerUpdate.java b/app/src/main/java/com/topjohnwu/magisk/receivers/ManagerUpdate.java index a2a5f14a7..cb319d6b3 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/ManagerUpdate.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/ManagerUpdate.java @@ -7,6 +7,7 @@ import android.net.Uri; import android.os.Build; import android.support.v4.content.FileProvider; +import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.utils.Utils; import java.io.File; @@ -34,8 +35,8 @@ public class ManagerUpdate extends BroadcastReceiver { } } }, - intent.getStringExtra("link"), + intent.getStringExtra(MagiskManager.INTENT_LINK), Utils.getLegalFilename("MagiskManager-v" + - intent.getStringExtra("version") + ".apk")); + intent.getStringExtra(MagiskManager.INTENT_VERSION) + ".apk")); } } 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 aacecb6a3..ead1366af 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -196,6 +196,8 @@ public class Utils { .setAutoCancel(true); Intent intent = new Intent(magiskManager, SplashActivity.class); intent.putExtra(MagiskManager.INTENT_SECTION, "install"); + intent.putExtra(MagiskManager.INTENT_VERSION, magiskManager.remoteMagiskVersionString); + intent.putExtra(MagiskManager.INTENT_LINK, magiskManager.magiskLink); TaskStackBuilder stackBuilder = TaskStackBuilder.create(magiskManager); stackBuilder.addParentStack(SplashActivity.class); stackBuilder.addNextIntent(intent); @@ -216,8 +218,8 @@ public class Utils { .setVibrate(new long[]{0, 100, 100, 100}) .setAutoCancel(true); Intent intent = new Intent(magiskManager, ManagerUpdate.class); - intent.putExtra("link", magiskManager.managerLink); - intent.putExtra("version", magiskManager.remoteManagerVersionString); + intent.putExtra(MagiskManager.INTENT_LINK, magiskManager.managerLink); + intent.putExtra(MagiskManager.INTENT_VERSION, magiskManager.remoteManagerVersionString); PendingIntent pendingIntent = PendingIntent.getBroadcast(magiskManager, APK_UPDATE_NOTIFICATION_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); builder.setContentIntent(pendingIntent);