Remove Auto Root Code

Revert this commit after things are sorted out
This commit is contained in:
topjohnwu
2016-09-30 18:07:08 +08:00
parent 21504f1329
commit eebd64bedb
47 changed files with 68 additions and 1469 deletions
@@ -1,158 +0,0 @@
package com.topjohnwu.magisk.services;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import com.topjohnwu.magisk.MainActivity;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
import java.util.Set;
public class MonitorService extends AccessibilityService {
private Boolean disableroot;
@Override
protected void onServiceConnected() {
super.onServiceConnected();
//Configure these here for compatibility with API 13 and below.
AccessibilityServiceInfo config = new AccessibilityServiceInfo();
config.eventTypes = AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED;
config.feedbackType = AccessibilityServiceInfo.FEEDBACK_GENERIC;
disableroot = false;
if (Build.VERSION.SDK_INT >= 16)
//Just in case this helps
config.flags = AccessibilityServiceInfo.FLAG_INCLUDE_NOT_IMPORTANT_VIEWS;
setServiceInfo(config);
}
@Override
public void onCreate() {
super.onCreate();
Log.d("Magisk", "MonitorService: Service created");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Magisk", "MonitorService: Service destroyed");
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
ComponentName componentName = new ComponentName(
event.getPackageName().toString(),
event.getClassName().toString()
);
ActivityInfo activityInfo = tryGetActivity(componentName);
boolean isActivity = activityInfo != null;
if (isActivity) {
Logger.dev("MonitorService: CurrentActivity: " + event.getPackageName());
String mPackage = componentName.getPackageName();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
if (Utils.autoToggleEnabled(getApplicationContext())) {
Set<String> setBlackList = prefs.getStringSet("auto_blacklist", null);
if (setBlackList != null) {
disableroot = setBlackList.contains(mPackage);
ForceRoot(!disableroot);
String appFriendly = getAppName(mPackage);
ShowNotification(disableroot, appFriendly);
}
}
}
}
}
private ActivityInfo tryGetActivity(ComponentName componentName) {
try {
return getPackageManager().getActivityInfo(componentName, 0);
} catch (PackageManager.NameNotFoundException e) {
return null;
}
}
private String getAppName(String packageName) {
PackageManager pkManager = getPackageManager();
ApplicationInfo appInfo;
String appName;
try {
appInfo = pkManager.getApplicationInfo(packageName, 0);
appName = (String) ((appInfo != null) ? pkManager.getApplicationLabel(appInfo) : "???");
return appName;
} catch (final PackageManager.NameNotFoundException e) {
return "";
}
}
private void ForceRoot(Boolean rootToggle) {
String rootString = rootToggle ? "on" : "off";
if (Utils.rootEnabled() != rootToggle) {
Logger.dev("MonitorService: toggling root " + rootString);
Utils.toggleRoot(rootToggle, getApplicationContext());
if (Utils.rootEnabled() != rootToggle) {
Utils.toggleRoot(rootToggle, getApplicationContext());
Logger.dev("MonitorService: FORCING to " + rootString);
}
}
}
private void ShowNotification(boolean rootAction, String packageName) {
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder;
if (!PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean("hide_root_notification", false)) {
if (rootAction) {
Intent intent = new Intent(getApplication(), MainActivity.class);
intent.putExtra("relaunch", "relaunch");
String rootMessage;
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(),
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT);
if (packageName.equals("")) {
rootMessage = "Root has been disabled";
} else {
rootMessage = "Root has been disabled for " + packageName;
}
mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(disableroot ? R.drawable.ic_stat_notification_autoroot_off : R.drawable.ic_stat_notification_autoroot_on)
.setContentIntent(pendingIntent)
.setContentTitle(getApplicationContext().getString(R.string.auto_toggle) + " status changed")
.setContentText(rootMessage);
int mNotificationId = 1;
mNotifyMgr.notify(mNotificationId, mBuilder.build());
} else {
mNotifyMgr.cancelAll();
}
}
}
@Override
public void onInterrupt() {
}
}
@@ -1,136 +0,0 @@
package com.topjohnwu.magisk.services;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Utils;
public class TileServiceCompat extends Service {
private static BroadcastReceiver clickTileReceiver;
private static boolean root, autoRoot;
public static final String TILE_ID = "Magisk";
public static final String ACTION_TILE_CLICK = "magisk.ACTION_TILE_CLICK";
public static final String EXTRA_CLICK_TYPE = "magisk.EXTRA_CLICK_TYPE";
public static final int CLICK_TYPE_UNKNOWN = -1;
public static final int CLICK_TYPE_SIMPLE = 0;
public static final int CLICK_TYPE_LONG = 1;
public TileServiceCompat() {
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
root = true;
registerClickTileReceiver();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
updateRoots();
updateTile();
return super.onStartCommand(intent, flags, startId);
}
private void updateRoots() {
root = Utils.rootEnabled();
autoRoot = Utils.autoToggleEnabled(getApplicationContext());
}
private void registerClickTileReceiver() {
clickTileReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int clickType = intent.getIntExtra(EXTRA_CLICK_TYPE, CLICK_TYPE_UNKNOWN);
switch (clickType) {
case CLICK_TYPE_SIMPLE:
onSimpleClick();
break;
case CLICK_TYPE_LONG:
onLongClick();
break;
}
}
};
registerReceiver(clickTileReceiver, new IntentFilter(ACTION_TILE_CLICK));
}
private void onSimpleClick() {
updateRoots();
updateTile();
if (autoRoot) {
Utils.toggleAutoRoot(false, getApplicationContext());
if (!Utils.hasServicePermission(getApplicationContext())) {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(it);
}
} else {
Utils.toggleRoot(!root, getApplicationContext());
}
}
private void onLongClick() {
updateRoots();
updateTile();
Utils.toggleAutoRoot(!autoRoot,getApplicationContext());
if (!Utils.hasServicePermission(getApplicationContext())) {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(it);
}
}
private void updateTile() {
BroadcastTileIntentBuilder broadcastTileIntentBuilder = new BroadcastTileIntentBuilder(this, TILE_ID);
if (autoRoot) {
broadcastTileIntentBuilder.setLabel(getApplicationContext().getString(R.string.auto_toggle));
broadcastTileIntentBuilder.setIconResource(R.drawable.ic_autoroot_white);
} else {
if (root) {
broadcastTileIntentBuilder.setLabel("Root enabled");
broadcastTileIntentBuilder.setIconResource(R.drawable.root_white);
} else {
broadcastTileIntentBuilder.setLabel("Root disabled");
broadcastTileIntentBuilder.setIconResource(R.drawable.root_grey);
}
}
Intent simpleClick = new Intent(ACTION_TILE_CLICK);
simpleClick.putExtra(EXTRA_CLICK_TYPE, CLICK_TYPE_SIMPLE);
Intent longClick = new Intent(ACTION_TILE_CLICK);
longClick.putExtra(EXTRA_CLICK_TYPE, CLICK_TYPE_LONG);
broadcastTileIntentBuilder.setVisible(true);
broadcastTileIntentBuilder.setOnClickBroadcast(simpleClick);
broadcastTileIntentBuilder.setOnLongClickBroadcast(longClick);
this.sendBroadcast(broadcastTileIntentBuilder.build());
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(clickTileReceiver);
}
}
@@ -1,116 +0,0 @@
package com.topjohnwu.magisk.services;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.preference.PreferenceManager;
import android.service.quicksettings.Tile;
import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Utils;
@SuppressLint("NewApi")
public class TileServiceNewApi extends android.service.quicksettings.TileService {
private int mRootsState;
public TileServiceNewApi() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.dev("QST (New): Service start");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onTileAdded() {
super.onTileAdded();
Logger.dev("QST (New): Tile added");
setupState();
this.getQsTile().updateTile();
}
@Override
public void onClick() {
mRootsState = Utils.CheckRootsState(getApplicationContext());
switchState(mRootsState);
Logger.dev("QST (New): Tile clicked");
}
@Override
public void onStartListening() {
super.onStartListening();
setupState();
Logger.dev("QST (New): Tile is listening");
}
@Override
public void onStopListening() {
super.onStopListening();
Logger.dev("QST (New): Tile stopped listening");
}
private void setupState() {
if (!Utils.hasServicePermission(getApplicationContext())) {
PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit().putBoolean("autoRootEnable",false).apply();
}
mRootsState = Utils.CheckRootsState(getApplicationContext());
Logger.dev("QST (New): SetupState");
Icon iconRoot = Icon.createWithResource(getApplicationContext(), R.drawable.root);
Icon iconAuto = Icon.createWithResource(getApplicationContext(), R.drawable.ic_autoroot);
Tile tile = getQsTile();
Logger.dev("QST: State is " + mRootsState);
switch (mRootsState) {
case 2:
tile.setLabel(getApplicationContext().getString(R.string.auto_toggle));
tile.setIcon(iconAuto);
tile.setState(Tile.STATE_ACTIVE);
break;
case 1:
tile.setLabel("Root enabled");
tile.setIcon(iconRoot);
tile.setState(Tile.STATE_ACTIVE);
break;
default:
tile.setLabel("Root disabled");
tile.setIcon(iconRoot);
tile.setState(Tile.STATE_INACTIVE);
break;
}
tile.updateTile();
}
private void switchState(int rootsState) {
switch (rootsState) {
case 2:
Utils.toggleRoot(true, getApplicationContext());
if (!Utils.hasServicePermission(getApplicationContext())) {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(it);
}
Utils.toggleAutoRoot(false, getApplicationContext());
break;
case 1:
Utils.toggleRoot(false, getApplicationContext());
break;
case 0:
if (!Utils.hasServicePermission(getApplicationContext())) {
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(it);
}
Utils.toggleAutoRoot(true, getApplicationContext());
break;
}
this.onStartListening();
setupState();
}
}