From 22a5c11f0d89418cb6008e5f92901728f4008c54 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 7 Feb 2017 06:02:06 +0800 Subject: [PATCH] Fix MagiskHide startup issue --- app/src/main/AndroidManifest.xml | 2 ++ .../com/topjohnwu/magisk/AboutActivity.java | 2 +- .../com/topjohnwu/magisk/MagiskManager.java | 13 ++++++++ .../com/topjohnwu/magisk/SplashActivity.java | 6 ++++ .../com/topjohnwu/magisk/StatusFragment.java | 2 +- .../magisk/receivers/BootReceiver.java | 31 +++++++++++++------ .../com/topjohnwu/magisk/utils/Async.java | 10 +----- .../com/topjohnwu/magisk/utils/Shell.java | 7 +++++ .../com/topjohnwu/magisk/utils/Utils.java | 2 +- .../com/topjohnwu/magisk/utils/WebWindow.java | 2 +- 10 files changed, 55 insertions(+), 22 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index c86e264c0..6bad618a8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -60,6 +60,8 @@ + + blockDetectionDone = new CallbackEvent<>(); @@ -64,12 +67,22 @@ public class MagiskManager extends Application { public SharedPreferences prefs; + private static Handler mHandler = new Handler(); + @Override public void onCreate() { super.onCreate(); prefs = PreferenceManager.getDefaultSharedPreferences(this); } + public void toast(String msg, int duration) { + mHandler.post(() -> Toast.makeText(this, msg, duration).show()); + } + + public void toast(int resId, int duration) { + mHandler.post(() -> Toast.makeText(this, resId, duration).show()); + } + public void init() { isDarkTheme = prefs.getBoolean("dark_theme", false); devLogging = prefs.getBoolean("developer_logging", false); diff --git a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java index 117ff4c24..ad879fbf4 100644 --- a/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/SplashActivity.java @@ -5,6 +5,7 @@ import android.os.Bundle; import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.utils.Async; +import com.topjohnwu.magisk.utils.Utils; public class SplashActivity extends Activity { @@ -17,10 +18,15 @@ public class SplashActivity extends Activity { // Init the info and configs and root shell magiskManager.init(); + boolean boot_done = Utils.itemExist(MagiskManager.MAGISK_MANAGER_BOOT); // Now fire all async tasks new Async.CheckUpdates(magiskManager).exec(); new Async.GetBootBlocks(magiskManager).exec(); + if (magiskManager.prefs.getBoolean("magiskhide", false) && !magiskManager.disabled && + magiskManager.magiskVersion > 10.3 && boot_done) { + new Async.MagiskHide().enable(); + } new Async.LoadModules(magiskManager) { @Override protected void onPostExecute(Void v) { diff --git a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java index 936a54dec..094a5c10c 100644 --- a/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/StatusFragment.java @@ -1,6 +1,6 @@ package com.topjohnwu.magisk; -import android.app.AlertDialog; +import android.support.v7.app.AlertDialog; import android.content.Intent; import android.net.Uri; import android.os.Bundle; diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java index 53a8fd4a9..6b9e43ca8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/BootReceiver.java @@ -1,27 +1,40 @@ package com.topjohnwu.magisk.receivers; +import android.app.IntentService; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; import android.widget.Toast; import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.utils.Async; +import com.topjohnwu.magisk.utils.Shell; public class BootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - MagiskManager magiskManager = (MagiskManager) context.getApplicationContext(); - magiskManager.initSuAccess(); - magiskManager.updateMagiskInfo(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - if (prefs.getBoolean("magiskhide", false) && !magiskManager.disabled && magiskManager.magiskVersion > 10.3) { - Toast.makeText(context, R.string.start_magiskhide, Toast.LENGTH_SHORT).show(); - new Async.MagiskHide(true).enable(); + context.startService(new Intent(context, BootupIntentService.class)); + } + + public static class BootupIntentService extends IntentService { + + public BootupIntentService() { + super("BootupIntentService"); + } + + @Override + protected void onHandleIntent(Intent intent) { + MagiskManager magiskManager = (MagiskManager) getApplicationContext(); + magiskManager.initSuAccess(); + magiskManager.updateMagiskInfo(); + if (magiskManager.prefs.getBoolean("magiskhide", false) && + !magiskManager.disabled && magiskManager.magiskVersion > 10.3) { + magiskManager.toast(R.string.start_magiskhide, Toast.LENGTH_LONG); + Shell.su(true, Async.MAGISK_HIDE_PATH + "enable", + "touch " + MagiskManager.MAGISK_MANAGER_BOOT); + } } } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java index b65535625..00f57c1ba 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Async.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Async.java @@ -307,18 +307,10 @@ public class Async { public static class MagiskHide extends RootTask { - private boolean newShell = false; - - public MagiskHide() {} - - public MagiskHide(boolean b) { - newShell = b; - } - @Override protected Void doInBackground(Object... params) { String command = (String) params[0]; - Shell.su(newShell, MAGISK_HIDE_PATH + command); + Shell.su(MAGISK_HIDE_PATH + command); return null; } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java index 8fa728b6a..aaa4c1f66 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Shell.java @@ -132,6 +132,13 @@ public class Shell { process = Runtime.getRuntime().exec("su"); STDIN = new DataOutputStream(process.getOutputStream()); STDOUT = new StreamGobbler(process.getInputStream(), res); + + // Run the new shell with busybox and proper umask + STDIN.write(("umask 022\n").getBytes("UTF-8")); + STDIN.flush(); + STDIN.write(("PATH=`[ -e /dev/busybox ] && echo /dev/busybox || " + + "echo /data/busybox`:$PATH\n").getBytes("UTF-8")); + STDIN.flush(); } catch (IOException err) { return null; } 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 53d2d8776..6d4557adc 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -1,7 +1,6 @@ package com.topjohnwu.magisk.utils; import android.Manifest; -import android.app.AlertDialog; import android.app.DownloadManager; import android.content.Context; import android.content.IntentFilter; @@ -10,6 +9,7 @@ import android.content.pm.PackageManager; import android.net.Uri; import android.os.Environment; import android.support.v4.app.ActivityCompat; +import android.support.v7.app.AlertDialog; import android.text.TextUtils; import android.widget.Toast; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/WebWindow.java b/app/src/main/java/com/topjohnwu/magisk/utils/WebWindow.java index f58568023..089f2549f 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/WebWindow.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/WebWindow.java @@ -1,6 +1,6 @@ package com.topjohnwu.magisk.utils; -import android.app.AlertDialog; +import android.support.v7.app.AlertDialog; import android.content.Context; import android.webkit.WebResourceRequest; import android.webkit.WebView;