Oh, it's so buttery...

Running out of stuff to fix.  👯
This commit is contained in:
d8ahazard 2016-09-23 23:25:12 -05:00
parent bee9be534c
commit 5b2dce6cf6
15 changed files with 214 additions and 164 deletions

View File

@ -32,9 +32,9 @@ repositories {
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:recyclerview-v7:24.2.1'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.android.support:recyclerview-v7:24.2.0'
compile 'com.android.support:cardview-v7:24.2.0'
compile 'com.android.support:design:24.2.0'
compile 'com.github.d8ahazard:BroadcastTileSupportUpdate:master'
compile 'com.jakewharton:butterknife:8.4.0'
compile 'com.github.michalis-vitos:aFileChooser:master'

View File

@ -62,7 +62,7 @@ public class AutoRootFragment extends ListFragment {
super.onResume();
initializeElements();
super.onResume();
getActivity().setTitle("Auto-toggle");
getActivity().setTitle(R.string.auto_toggle_apps);
}

View File

@ -23,6 +23,7 @@ import android.widget.Toast;
import com.topjohnwu.magisk.receivers.Receiver;
import com.topjohnwu.magisk.services.MonitorService;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.PrefHelper;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
@ -88,6 +89,7 @@ public class RootFragment extends Fragment implements Receiver {
private boolean autoRootStatus;
private View view;
private SharedPreferences.OnSharedPreferenceChangeListener listener;
@Nullable
@Override
@ -107,8 +109,8 @@ public class RootFragment extends Fragment implements Receiver {
new updateUI().execute();
rootToggle.setOnClickListener(toggle -> {
Utils.toggleRoot(((CompoundButton) toggle).isChecked());
new updateUI().execute();
Utils.toggleRoot(((CompoundButton) toggle).isChecked(), getActivity());
});
autoRootToggle.setOnClickListener(toggle -> {
@ -121,29 +123,34 @@ public class RootFragment extends Fragment implements Receiver {
}
}
);
listener = (prefs1, key) -> {
if ((key.contains("autoRootEnable")) | (key.equals("root"))) {
Logger.dh("RootFragmnet, keychange detected for " + key);
new updateUI().execute();
}
};
prefs.registerOnSharedPreferenceChangeListener(listener);
selinuxToggle.setOnClickListener(toggle -> {
Shell.su(((CompoundButton) toggle).isChecked() ? "setenforce 1" : "setenforce 0");
new updateUI().execute();
});
// LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver,
// new IntentFilter("com.magisk.UPDATEUI"));
return view;
}
// private final BroadcastReceiver mYourBroadcastReceiver = new RootFragmentReceiver(Re) {
// @Override
// public void onReceive(Context context, Intent intent) {
//
// Log.d("Magisk", "RootFragment: UpdateRF called and fired");
// new updateUI().execute();
// }
//
//
// };
@Override
public void onDestroy() {
super.onDestroy();
prefs.unregisterOnSharedPreferenceChangeListener(listener);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
@ -158,9 +165,6 @@ public class RootFragment extends Fragment implements Receiver {
Snackbar.make(view, getActivity().getString(R.string.auto_toggle) + " disabled, permissions required.", Snackbar.LENGTH_LONG).show();
}
} else if (requestCode == 420) {
Log.d("Magisk", "Got result code OK for UI update.");
new updateUI().execute();
}
}
@ -182,7 +186,7 @@ public class RootFragment extends Fragment implements Receiver {
rootToggle.setEnabled(true);
}
new updateUI().execute();
}
@ -190,7 +194,6 @@ public class RootFragment extends Fragment implements Receiver {
public void onResume() {
super.onResume();
getActivity().setTitle("Root");
new updateUI().execute();
}
@ -209,6 +212,7 @@ public class RootFragment extends Fragment implements Receiver {
if (PrefHelper.CheckBool("enable_quicktile", getActivity())) {
Utils.SetupQuickSettingsTile(getActivity());
}
autoRootStatus = Utils.autoToggleEnabled(getActivity());
return null;
}

View File

@ -39,6 +39,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
private final Handler mDrawerHandler = new Handler();
private String currentTitle;
private String tag;
@BindView(R.id.toolbar)
Toolbar toolbar;
@ -73,6 +74,66 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
this.getFragmentManager().addOnBackStackChangedListener(
() -> {
FragmentManager fragmentManager = getFragmentManager();
Fragment hm=getFragmentManager().findFragmentByTag("root");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.root);
}
}
hm=getFragmentManager().findFragmentByTag("autoroot");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.autoroot);
}
}
hm=getFragmentManager().findFragmentByTag("magisk");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.magisk);
}
}
hm=getFragmentManager().findFragmentByTag("modules");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.modules);
}
}
hm=getFragmentManager().findFragmentByTag("downloads");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.downloads);
}
}
hm=getFragmentManager().findFragmentByTag("log");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.log);
}
}
hm=getFragmentManager().findFragmentByTag("settings");
if(hm!=null)
{
if(hm.isVisible())
{
navigationView.setCheckedItem(R.id.settings);
}
}
});
@ -170,7 +231,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
public void navigate(final int itemId) {
Fragment navFragment = null;
String tag = "";
tag = "";
switch (itemId) {
case R.id.magisk:
setTitle(R.string.magisk);
@ -183,8 +244,8 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
navFragment = new RootFragment();
break;
case R.id.autoroot:
setTitle(R.string.auto_toggle);
tag = "ic_autoroot";
setTitle(R.string.auto_toggle_apps);
tag = "autoroot";
navFragment = new AutoRootFragment();
break;
case R.id.modules:

View File

@ -16,7 +16,7 @@ public class AutoStartReceiver extends BroadcastReceiver {
Intent myIntent = new Intent(context, MonitorService.class);
context.startService(myIntent);
if (PrefHelper.CheckBool("keep_root_off",context)) {
Utils.toggleRoot(false);
Utils.toggleRoot(false,context);
}
if (PrefHelper.CheckBool("enable_quicktile",context)) {
Utils.SetupQuickSettingsTile(context);

View File

@ -25,11 +25,11 @@ public final class PrivateBroadcastReceiver extends BroadcastReceiver {
}
if (ACTION_ENABLEROOT.equals(action)) {
Utils.toggleAutoRoot(false, context);
Utils.toggleRoot(true);
Utils.toggleRoot(true,context);
}
if (ACTION_DISABLEROOT.equals(action)) {
Utils.toggleAutoRoot(false, context);
Utils.toggleRoot(false);
Utils.toggleRoot(false,context);
}
Utils.SetupQuickSettingsTile(context);

View File

@ -111,9 +111,9 @@ public class MonitorService extends AccessibilityService {
String rootString = rootToggle ? "on" : "off";
if (Utils.rootEnabled() != rootToggle) {
Logger.dh("MonitorService: toggling root " + rootString);
Utils.toggleRoot(rootToggle);
Utils.toggleRoot(rootToggle,getApplicationContext());
if (Utils.rootEnabled() != rootToggle) {
Utils.toggleRoot(rootToggle);
Utils.toggleRoot(rootToggle,getApplicationContext());
Logger.dh("MonitorService: FORCING to " + rootString);
}
@ -122,9 +122,9 @@ public class MonitorService extends AccessibilityService {
private void ForceEnableRoot() {
Log.d("Magisk", "MonitorService: ForceEnable called.");
Utils.toggleRoot(true);
Utils.toggleRoot(true,getApplicationContext());
if (!Utils.rootEnabled()) {
Utils.toggleRoot(true);
Utils.toggleRoot(true,getApplicationContext());
}
}

View File

@ -75,7 +75,7 @@ public class TileServiceCompat extends Service {
private void onSimpleClick() {
updateRoots();
updateTile();
Utils.toggleRoot(!root);
Utils.toggleRoot(!root,getApplicationContext());
}
private void onLongClick() {

View File

@ -1,94 +1,102 @@
package com.topjohnwu.magisk.services;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.preference.PreferenceManager;
import android.service.quicksettings.Tile;
import android.util.Log;
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 implements
SharedPreferences.OnSharedPreferenceChangeListener {
private int STATE_CURRENT;
public class TileServiceNewApi extends android.service.quicksettings.TileService {
private int mRootsState;
public TileServiceNewApi() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Logger.dh("QST (New): Service start");
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onTileAdded() {
super.onTileAdded();
Logger.dh("QST (New): Tile added");
setupState();
this.getQsTile().updateTile();
}
@Override
public void onClick() {
switchState();
this.getQsTile().updateTile();
mRootsState = Utils.CheckRootsState(getApplicationContext());
switchState(mRootsState);
Logger.dh("QST (New): Tile clicked");
}
@Override
public void onStartListening() {
super.onStartListening();
setupState();
Logger.dh("QST (New): Tile is listening");
}
@Override
public void onStopListening() {
super.onStopListening();
Logger.dh("QST (New): Tile stopped listening");
}
private void setupState() {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
preferences.registerOnSharedPreferenceChangeListener(this);
Logger.dh("TileService(New): SetupState");
mRootsState = Utils.CheckRootsState(getApplicationContext());
Logger.dh("QST (New): SetupState");
Icon iconRoot = Icon.createWithResource(getApplicationContext(), R.drawable.root);
Icon iconAuto = Icon.createWithResource(getApplicationContext(), R.drawable.ic_autoroot);
Tile tile = this.getQsTile();
boolean autoRootStatus = Utils.autoToggleEnabled(getApplicationContext());
boolean rootStatus = Utils.rootEnabled();
int rootsStatus = Utils.CheckRootsState(getApplicationContext());
Log.d("Magisk", "QST: Auto and root are " + autoRootStatus + " and " + rootStatus + Utils.CheckRootsState(getApplicationContext()));
if (rootsStatus == 2) {
tile.setLabel(getApplicationContext().getString(R.string.auto_toggle));
tile.setIcon(iconAuto);
tile.setState(Tile.STATE_ACTIVE);
} else if (rootsStatus == 1) {
tile.setLabel("Root enabled");
tile.setIcon(iconRoot);
tile.setState(Tile.STATE_ACTIVE);
} else {
tile.setLabel("Root disabled");
tile.setIcon(iconRoot);
tile.setState(Tile.STATE_INACTIVE);
Tile tile = getQsTile();
Logger.dh("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();
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key)
{
Logger.dh("TileService: Key Change registered for " + key);
if (key.equals("autoRootEnable")) {
private void switchState(int rootsState) {
}
}
private void switchState() {
switch (Utils.CheckRootsState(getApplicationContext())) {
switch (rootsState) {
case 2:
Utils.toggleRoot(true);
Utils.toggleRoot(true, getApplicationContext());
Utils.toggleAutoRoot(false, getApplicationContext());
break;
case 1:
Utils.toggleRoot(false);
Utils.toggleRoot(false, getApplicationContext());
break;
case 0:
Utils.toggleAutoRoot(true, getApplicationContext());
break;
}
this.onStartListening();
setupState();
}

View File

@ -15,6 +15,11 @@ public class PrefHelper {
return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(key, false);
}
public static void SetBool(String key, Boolean value, Context context) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit();
}
}

View File

@ -16,6 +16,7 @@ import android.os.Build;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.service.quicksettings.TileService;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
@ -30,8 +31,8 @@ import com.topjohnwu.magisk.RootFragment;
import com.topjohnwu.magisk.module.BaseModule;
import com.topjohnwu.magisk.receivers.PrivateBroadcastReceiver;
import com.topjohnwu.magisk.services.MonitorService;
import com.topjohnwu.magisk.services.TileServiceNewApi;
import com.topjohnwu.magisk.services.TileServiceCompat;
import com.topjohnwu.magisk.services.TileServiceNewApi;
import java.io.File;
import java.io.UnsupportedEncodingException;
@ -71,10 +72,10 @@ public class Utils {
if (!Shell.rootAccess()) {
Snackbar.make(((Activity) context).findViewById(android.R.id.content), R.string.no_root_access, Snackbar.LENGTH_LONG).show();
}
if (PrefHelper.CheckBool("keep_root_off",context)) {
Utils.toggleRoot(false);
if (PrefHelper.CheckBool("keep_root_off", context)) {
Utils.toggleRoot(false, context);
}
if (PrefHelper.CheckBool("enable_quicktile",context)) {
if (PrefHelper.CheckBool("enable_quicktile", context)) {
Utils.SetupQuickSettingsTile(context);
}
}
@ -114,39 +115,58 @@ public class Utils {
}
public static boolean removeFile(String path) {
boolean check;
String command = "rm -f " + path + " 2>/dev/null; if [ -f " + path + " ]; then echo false; else echo true; fi";
if (!Shell.rootAccess()) {
return false;
} else {
return Boolean.parseBoolean(Shell.su(command).get(0));
try {
check = Boolean.parseBoolean(Shell.su(command).get(0));
return check;
} catch (NullPointerException e) {
Log.d("Magisk:", "SU error executing removeFile " + e);
return false;
}
}
}
public static void toggleRoot(Boolean b) {
public static void toggleRoot(Boolean b, Context context) {
if (Utils.magiskVersion != -1) {
if (b) {
Shell.su("ln -s $(getprop magisk.supath) /magisk/.core/bin", "setprop magisk.root 1");
} else {
Shell.su("rm -rf /magisk/.core/bin", "setprop magisk.root 0");
}
if (PrefHelper.CheckBool("enable_quicktile", context)) {
SetupQuickSettingsTile(context);
}
PrefHelper.SetBool("root",b,context);
}
}
public static void toggleAutoRoot(Boolean b, Context context) {
if (Utils.magiskVersion != -1) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("autoRootEnable", b).apply();
Intent myServiceIntent = new Intent(context, MonitorService.class);
if (b) {
context.startService(myServiceIntent);
if (!Utils.hasServicePermission(context)) {
Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
Toast.makeText(context, "Please enable Magisk in accessibility for auto-toggle work.", Toast.LENGTH_LONG).show();
context.startActivity(intent);
} else {
context.stopService(myServiceIntent);
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("autoRootEnable", b).apply();
Intent myServiceIntent = new Intent(context, MonitorService.class);
if (b) {
context.startService(myServiceIntent);
} else {
context.stopService(myServiceIntent);
}
}
}
if (PrefHelper.CheckBool("enable_quicktile", context)) {
SetupQuickSettingsTile(context);
}
UpdateRootFragmentUI(context);
}
public static List<String> getModList(String path) {
static List<String> getModList(String path) {
List<String> ret;
String command = "find " + path + " -type d -maxdepth 1 ! -name \"*.core\" ! -name \"*lost+found\" ! -name \"*magisk\"";
if (Shell.rootAccess()) {
@ -175,8 +195,18 @@ public class Utils {
}
File downloadFile, dir = new File(Environment.getExternalStorageDirectory() + "/MagiskManager");
downloadFile = new File(dir + "/" + file);
if (!dir.exists()) dir.mkdir();
if (downloadFile.exists()) downloadFile.delete();
if (!dir.exists()) {
if (!dir.mkdirs()) {
Toast.makeText(context, R.string.toast_error_makedir, Toast.LENGTH_LONG).show();
return;
}
}
if (downloadFile.exists()) {
if (!downloadFile.delete()) {
Toast.makeText(context, R.string.toast_error_removing_files, Toast.LENGTH_LONG).show();
return;
}
}
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(link));
@ -213,8 +243,10 @@ public class Utils {
public static void SetupQuickSettingsTile(Context mContext) {
Logger.dh("Utils: SetupQuickSettings called");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Logger.dh("Utils: Starting N quick settings service");
Intent serviceIntent = new Intent(mContext, TileServiceNewApi.class);
mContext.startService(serviceIntent);
}
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
Logger.dh("Utils: Marshmallow build detected");
@ -275,10 +307,8 @@ public class Utils {
List<String> lines = Shell.su("settings get secure sysui_qs_tiles");
if (lines != null && lines.size() == 1) {
List<String> tiles = new LinkedList<String>(Arrays.asList(lines.get(0).split(",")));
List<String> tiles2;
int tileSpace = Math.round(tiles.size()/2);
Logger.dh("Utils: Current Tile String is "+ tiles);
List<String> tiles = new LinkedList<>(Arrays.asList(lines.get(0).split(",")));
Logger.dh("Utils: Current Tile String is " + tiles);
if (tiles.size() > 1) {
for (String tile : tiles) {
if (tile.equals(qsTileId)) {
@ -287,9 +317,9 @@ public class Utils {
}
}
tiles.add(Math.round(tiles.size()/2), qsTileId);
tiles.add(Math.round(tiles.size() / 2), qsTileId);
String newTiles = TextUtils.join(",", tiles);
Logger.dh("Utils: NewtilesString is "+ newTiles);
Logger.dh("Utils: NewtilesString is " + newTiles);
Shell.su("settings put secure sysui_qs_tiles \"" + newTiles + "\"");
Toast.makeText(context, "Tile installed", Toast.LENGTH_SHORT).show();
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) {
@ -301,7 +331,6 @@ public class Utils {
Toast.makeText(context, "Tile installation error", Toast.LENGTH_SHORT).show();
}
public static void uninstallTile(Context context) {
String qsTileId;
@ -312,7 +341,7 @@ public class Utils {
}
List<String> lines = Shell.su("settings get secure sysui_qs_tiles");
if (lines != null && lines.size() == 1) {
List<String> tiles = new LinkedList<String>(Arrays.asList(lines.get(0).split(",")));
List<String> tiles = new LinkedList<>(Arrays.asList(lines.get(0).split(",")));
if (tiles.size() > 1) {
boolean isPresent = false;
for (int i = 0; i < tiles.size(); i++) {
@ -333,25 +362,20 @@ public class Utils {
}
Toast.makeText(context, "Tile already uninstalled", Toast.LENGTH_SHORT).show();
}
}
Toast.makeText(context, "Tile uninstallation error", Toast.LENGTH_SHORT).show();
}
private static void refreshService(Context context) {
context.startService(new Intent(context, TileServiceCompat.class));
}
public static void UpdateRootFragmentUI(Context context) {
Logger.dh("Magisk", "Utils: UpdateRF called");
Intent intent = new Intent(context, RootFragment.class);
intent.setAction("com.magisk.UPDATEUI");
intent.setAction("com.magisk.UPDATE");
context.sendBroadcast(intent);
}
@ -453,7 +477,6 @@ public class Utils {
public abstract void task(File file);
}
public static boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {

View File

@ -48,8 +48,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:checked="true"
android:text="@string/auto_root"
android:checked="false"
android:text="@string/auto_toggle"
android:textSize="16sp"/>
</android.support.v7.widget.CardView>

View File

@ -1,54 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_marginBottom="8dp"
android:layout_marginTop="?attr/actionBarSize"
android:layout_width="fill_parent">
<PreferenceCategory android:layout_width="match_parent"
android:layout_height="match_parent">
</PreferenceCategory>
<PreferenceCategory
android:title="Test"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="Pref1"
android:defaultValue="Foo"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<EditTextPreference
android:key="edittext_preference"
android:title="TextPref"
android:summary="This is the summary"
android:dialogTitle="Dialog Title"
android:dependency="checkbox_preference"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</PreferenceCategory>
<PreferenceCategory
android:title="Section 2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Preference
android:title="Intent Pref"
android:layout_width="match_parent"
android:layout_height="match_parent">
<intent android:action="android.intent.action.VIEW"
android:data="http://codepath.com/"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</Preference>
</PreferenceCategory>
</PreferenceScreen>

View File

@ -18,7 +18,7 @@
<item
android:id="@+id/autoroot"
android:icon="@drawable/ic_autoroot"
android:title="@string/auto_toggle"/>
android:title="@string/auto_toggle_apps"/>
<item
android:id="@+id/modules"

View File

@ -124,4 +124,7 @@
<!--General Use -->
<string name="auto_toggle">Auto-toggle</string>
<string name="auto_toggle_apps">Auto-toggle List</string>
<string name="toast_error_makedir">Error creating directory, could not download file.</string>
<string name="toast_error_removing_files">Error removing old files, cancelled.</string>
</resources>