diff --git a/app/src/main/java/com/topjohnwu/magisk/MonitorService.java b/app/src/main/java/com/topjohnwu/magisk/MonitorService.java index 736c35784..3ac25a528 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MonitorService.java +++ b/app/src/main/java/com/topjohnwu/magisk/MonitorService.java @@ -36,7 +36,7 @@ public class MonitorService extends Service private Boolean disableroot; private Boolean disablerootprev; private int counter = 0; - private String mPackageName; + private String mPackageName = ""; @Nullable @Override @@ -46,8 +46,6 @@ public class MonitorService extends Service @Override public int onStartCommand(Intent intent, int flags, int startId) { - checkProcesses.run(); - return START_STICKY; } @@ -55,6 +53,7 @@ public class MonitorService extends Service @Override public void onCreate() { super.onCreate(); + checkProcesses.run(); Log.d("Magisk","MonitorService: Service created"); } @@ -94,7 +93,7 @@ public class MonitorService extends Service //Log.d(TAG,"Root check completed, set to " + (disableroot ? "disabled" : "enabled")); } - handler.postDelayed(checkProcesses, 1000); + handler.postDelayed(checkProcesses, 500); } }; @@ -182,29 +181,31 @@ public class MonitorService extends Service protected boolean isAppForeground(String packageName) { - UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); - long time = System.currentTimeMillis(); - List stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time); - String topPackageName = ""; - if (stats != null) { + UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); + long time = System.currentTimeMillis(); + List stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time); + String topPackageName = ""; + if (stats != null) { - SortedMap mySortedMap = new TreeMap<>(); - for (UsageStats usageStats : stats) { + SortedMap mySortedMap = new TreeMap<>(); + for (UsageStats usageStats : stats) { mySortedMap.put(usageStats.getLastTimeUsed(), usageStats); - } - if (!mySortedMap.isEmpty()) { - topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); - if (topPackageName.equals("com.topjohnwu.magisk")) { - mySortedMap.remove(mySortedMap.lastKey()); - topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); - mPackageName = getAppName(topPackageName); } + if (!mySortedMap.isEmpty()) { + topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); + if (topPackageName.equals("com.topjohnwu.magisk")) { + mySortedMap.remove(mySortedMap.lastKey()); + topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName(); + Log.d("Magisk","MonitorService: Package is " + topPackageName); + mPackageName = getAppName(topPackageName); + } + } } - } + return topPackageName.equals(packageName); + - return topPackageName.equals(packageName); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java index 86ee91f29..fe87ace29 100644 --- a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java @@ -168,6 +168,8 @@ public class RootFragment extends Fragment { getActivity().startService(myServiceIntent); } } else { + Intent myIntent = new Intent(getActivity(), MonitorService.class); + getActivity().stopService(myIntent); rootToggle.setEnabled(true); } } diff --git a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java index bbf2bf2e0..8b83f88cd 100644 --- a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java @@ -54,9 +54,14 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView // Startups PreferenceManager.setDefaultValues(this, R.xml.defaultpref, false); - if (!Utils.isMyServiceRunning(MonitorService.class, getApplicationContext())) { - Intent myIntent = new Intent(getApplication(), MonitorService.class); - getApplication().startService(myIntent); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + if (prefs.contains("autoRootEnable")) { + if (prefs.getBoolean("autoRootEnable",false)) { + if (!Utils.isMyServiceRunning(MonitorService.class, getApplicationContext())) { + Intent myIntent = new Intent(getApplication(), MonitorService.class); + getApplication().startService(myIntent); + } + } } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { @@ -66,7 +71,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView new Utils.Initialize(this).execute(); new Utils.CheckUpdates(this).execute(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); RepoHelper.TaskDelegate delegate = result -> { //Do a thing here when we get a result we want };