Well, the disable part works...

Auto enable is still a bit trigger happy.  But, bugs worked out, might
try a method of detection that uses root...
This commit is contained in:
d8ahazard 2016-08-31 22:04:27 -05:00
parent fc5c9647d8
commit c992b89b2f
2 changed files with 90 additions and 105 deletions

View File

@ -29,13 +29,11 @@ public class AutoRootFragment extends ListFragment {
private ApplicationAdapter listadaptor = null;
public ListView listView;
public SharedPreferences prefs;
private View view;
List<String> arrayList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.auto_root_fragment, container, false);
return view;
return inflater.inflate(R.layout.auto_root_fragment, container, false);
}
@Override
@ -49,7 +47,7 @@ public class AutoRootFragment extends ListFragment {
Set<String> set = new HashSet<String>();
set.add("com.google.android.apps.walletnfcrel");
editor.putStringSet("autoapps", set);
editor.commit();
editor.apply();
}
new LoadApplications().execute();
}

View File

@ -1,7 +1,6 @@
package com.topjohnwu.magisk;
import android.app.AppOpsManager;
import android.app.Fragment;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
@ -11,7 +10,7 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Process;
import android.preference.PreferenceManager;
@ -32,25 +31,10 @@ public class MonitorService extends Service
{
private static final String TAG = "Magisk";
private UsageStatsManager mUsageStatsManager;
private SharedPreferences prefs;
Handler handler = new Handler();
private Boolean disableroot;
private Boolean disablerootprev;
public static boolean isRecursionEnable = false;
private NotificationManager mNotifyMgr;
private CountDownTimer timer = new CountDownTimer(1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
}
@Override
public void onFinish() {
CheckProcesses();
}
}.start();
private Boolean stopauto;
@Nullable
@Override
@ -60,46 +44,111 @@ public class MonitorService extends Service
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!intent.hasExtra("disable")) {
new Thread(new Runnable() {
@Override
public void run() {
// DO your work here
// get the data
timer.start();
}
new Thread(() -> {
checkProcesses.run();
}).start();
return START_STICKY;
} else return STOP_FOREGROUND_REMOVE;
}
}
@Override
public void onDestroy() {
Log.d(TAG, "Destroyah!");
android.os.Process.killProcess(android.os.Process.myPid());
super.onDestroy();
}
private Runnable checkProcesses = new Runnable() {
@Override
public void run() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (prefs.getBoolean("autoRootEnable", false)) {
Set<String> set = prefs.getStringSet("autoapps", null);
if (set != null) {
disableroot = getStats(set);
}
if (disableroot != disablerootprev) {
int counter = 0;
String rootstatus = (disableroot ? "disabled" : "enabled");
if (disableroot) {
Shell.su("setprop magisk.root 0");
if (Shell.sh("which su").contains("su")) {
Shell.su(("setprop magisk.root 0"));
}
if (Shell.sh("which su").contains("su")) {
Shell.su(("setprop magisk.root 0"));
}
if (Shell.sh("which su").contains("su")) {
Shell.su(("setprop magisk.root 0"));
}
} else {
counter +=1;
if (counter >=3) {
Shell.su("setprop magisk.root 1");
counter = 0;
}
}
// Shell.su((disableroot ? "setprop magisk.root 0" : "setprop magisk.root 1"));
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder;
mNotifyMgr.cancelAll();
if (disableroot) {
Intent intent = new Intent(getApplication(), WelcomeActivity.class);
intent.putExtra("relaunch", "relaunch");
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(disableroot ? R.drawable.ic_stat_notification_autoroot_off : R.drawable.ic_stat_notification_autoroot_on)
.setContentIntent(pendingIntent)
.setContentTitle("Auto-root status changed")
.setContentText("Auto root has been " + rootstatus + "! Tap to re-enable when done.");
} else {
mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setAutoCancel(true)
.setSmallIcon(disableroot ? R.drawable.ic_stat_notification_autoroot_off : R.drawable.ic_stat_notification_autoroot_on)
.setContentTitle("Auto-root status changed")
.setContentText("Auto root has been " + rootstatus + "!");
}
// Builds the notification and issues it.
int mNotificationId = 1;
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
disablerootprev = disableroot;
Log.d(TAG,"Root check completed, set to " + (disableroot ? "disabled" : "enabled"));
}
handler.postDelayed(checkProcesses, 1000);
}
};
private boolean getStats(Set<String> seti) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean inStats = false;
UsageStatsManager lUsageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
if (seti != null) {
ArrayList<String> statList = new ArrayList<>(seti);
for (int i = 0; i < statList.size(); i++) {
if (isAppForeground(statList.get(i))) {
inStats = (isAppForeground(statList.get(i)));
}
}
return inStats;
}
Log.d(TAG, "SDK check failed.");
}
@ -109,19 +158,14 @@ public class MonitorService extends Service
protected boolean isAppForeground(String packageName) {
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE);
long time = System.currentTimeMillis();
List<UsageStats> stats = usageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, time - 1000 * 10, time);
String topPackageName = new String();
String topPackageName = "";
if (stats != null) {
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
for (UsageStats usageStats : stats) {
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
}
if (mySortedMap != null && !mySortedMap.isEmpty()) {
if (!mySortedMap.isEmpty()) {
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
}
}
@ -129,62 +173,6 @@ public class MonitorService extends Service
return topPackageName.equals(packageName);
}
private void CheckProcesses() {
prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (prefs.getBoolean("autoRootEnable", false)) {
Set<String> set = prefs.getStringSet("autoapps", null);
ArrayList<String> arrayList = null;
if (set != null) {
disableroot = getStats(set);
}
if (disableroot != disablerootprev) {
String rootstatus = (disableroot ? "disabled" : "enabled");
Shell.su((disableroot ? "setprop magisk.root 0" : "setprop magisk.root 1"));
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder;
mNotifyMgr.cancelAll();
if (disableroot) {
timer.cancel();
this.stopSelf();
Intent intent = new Intent(this, WelcomeActivity.class);
intent.putExtra("relaunch", "relaunch");
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(disableroot ? R.drawable.ic_stat_notification_autoroot_off : R.drawable.ic_stat_notification_autoroot_on)
.setContentIntent(pendingIntent)
.setContentTitle("Auto-root status changed")
.setContentText("Auto root has been " + rootstatus + "! Tap to re-enable when done.");
} else {
mBuilder =
new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setSmallIcon(disableroot ? R.drawable.ic_stat_notification_autoroot_off : R.drawable.ic_stat_notification_autoroot_on)
.setContentTitle("Auto-root status changed")
.setContentText("Auto root has been " + rootstatus + "!");
}
// Builds the notification and issues it.
int mNotificationId = 001;
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
disablerootprev = disableroot;
Log.d(TAG, "Check Processes finished, result is " + disableroot + " and settings say we should be " + prefs.getBoolean("autoRootEnable", false));
timer.start();
}
}
private boolean hasUsagePermission() {
AppOpsManager appOps = (AppOpsManager)
getSystemService(Context.APP_OPS_SERVICE);
@ -193,6 +181,5 @@ public class MonitorService extends Service
return mode == AppOpsManager.MODE_ALLOWED;
}
}