From 0cbf66996f5e14e574215014f1634c95225ace5c Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Wed, 21 Sep 2016 07:39:12 -0500 Subject: [PATCH] WIP Sync It might compile, is probably broken atm... --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 7 +- .../com/topjohnwu/magisk/RootFragment.java | 35 ++++++++- .../com/topjohnwu/magisk/WelcomeActivity.java | 2 +- .../PrivateBroadcastReceiver.java | 7 +- .../topjohnwu/magisk/receivers/Receiver.java | 5 ++ .../receivers/RootFragmentReceiver.java | 19 +++++ .../magisk/tile/CustomTileHelper.java | 59 -------------- .../magisk/tile/PublicBroadcastReceiver.java | 29 ------- .../magisk/tile/TilePreferenceHelper.java | 26 ------- .../com/topjohnwu/magisk/utils/Utils.java | 78 ++++++++++++------- .../main/res/drawable/ic_autoroot_white.xml | 4 + app/src/main/res/drawable/root_grey.xml | 9 +++ app/src/main/res/drawable/root_white.xml | 9 +++ 14 files changed, 140 insertions(+), 155 deletions(-) rename app/src/main/java/com/topjohnwu/magisk/{tile => receivers}/PrivateBroadcastReceiver.java (81%) create mode 100644 app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java create mode 100644 app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/tile/CustomTileHelper.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/tile/PublicBroadcastReceiver.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/tile/TilePreferenceHelper.java create mode 100644 app/src/main/res/drawable/ic_autoroot_white.xml create mode 100644 app/src/main/res/drawable/root_grey.xml create mode 100644 app/src/main/res/drawable/root_white.xml diff --git a/app/build.gradle b/app/build.gradle index 9721feb82..913193b4a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 96f10d257..f4da821eb 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -42,7 +42,7 @@ @@ -107,6 +107,11 @@ + + + + + diff --git a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java index 0fe3edcd8..2f823f98e 100644 --- a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java @@ -1,16 +1,19 @@ package com.topjohnwu.magisk; import android.app.Activity; +import android.content.BroadcastReceiver; +import android.content.Context; import android.content.Intent; +import android.content.IntentFilter; import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.PorterDuff; import android.os.AsyncTask; import android.os.Bundle; -import android.os.Handler; import android.preference.PreferenceManager; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -22,6 +25,8 @@ import android.widget.Switch; import android.widget.TextView; import android.widget.Toast; +import com.topjohnwu.magisk.receivers.Receiver; +import com.topjohnwu.magisk.receivers.RootFragmentReceiver; import com.topjohnwu.magisk.services.MonitorService; import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Utils; @@ -33,7 +38,7 @@ import butterknife.BindColor; import butterknife.BindView; import butterknife.ButterKnife; -public class RootFragment extends Fragment { +public class RootFragment extends Fragment implements Receiver{ public SharedPreferences prefs; @BindView(R.id.progressBar) @@ -92,7 +97,6 @@ public class RootFragment extends Fragment { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.root_fragment, container, false); ButterKnife.bind(this, view); - prefs = PreferenceManager.getDefaultSharedPreferences(getContext()); if (prefs.contains("autoRootEnable")) { @@ -112,7 +116,7 @@ public class RootFragment extends Fragment { autoRootToggle.setOnClickListener(toggle -> { ToggleAutoRoot(autoRootToggle.isChecked()); - new Handler().postDelayed(() -> new updateUI().execute(), 1000); + new updateUI().execute(); }); @@ -121,9 +125,23 @@ public class RootFragment extends Fragment { new updateUI().execute(); }); + LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mYourBroadcastReceiver, + new IntentFilter("com.magisk.UPDATEUI")); + return view; } + private final BroadcastReceiver mYourBroadcastReceiver = new RootFragmentReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + + Log.d("Magisk", "RootFragment: UpdateRF called and fired"); + new updateUI().execute(); + } + + + }; + @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request we're responding to @@ -139,6 +157,9 @@ public class RootFragment extends Fragment { } + } else if (requestCode == 420) { + Log.d("Magisk", "Got result code OK for UI update."); + new updateUI().execute(); } } @@ -174,6 +195,11 @@ public class RootFragment extends Fragment { new updateUI().execute(); } + @Override + public void onResult() { + + } + public class updateUI extends AsyncTask { @Override @@ -301,4 +327,5 @@ public class RootFragment extends Fragment { } } } + } diff --git a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java index eca29df52..ee678ec9d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java @@ -143,7 +143,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView - private void navigate(final int itemId) { + public void navigate(final int itemId) { Fragment navFragment = null; String tag = ""; switch (itemId) { diff --git a/app/src/main/java/com/topjohnwu/magisk/tile/PrivateBroadcastReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/PrivateBroadcastReceiver.java similarity index 81% rename from app/src/main/java/com/topjohnwu/magisk/tile/PrivateBroadcastReceiver.java rename to app/src/main/java/com/topjohnwu/magisk/receivers/PrivateBroadcastReceiver.java index 036d66c49..bc7c43993 100644 --- a/app/src/main/java/com/topjohnwu/magisk/tile/PrivateBroadcastReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/PrivateBroadcastReceiver.java @@ -1,4 +1,4 @@ -package com.topjohnwu.magisk.tile; +package com.topjohnwu.magisk.receivers; import android.content.BroadcastReceiver; import android.content.Context; @@ -19,15 +19,16 @@ public final class PrivateBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { final String action = intent.getAction(); - Log.d("Magisk","Broadcast Receiver, Made it this far!"); + Log.d("Magisk","Broadcast Receiver, Made it this far! We're trying to " + action); if (ACTION_AUTOROOT.equals(action)) { - Utils.toggleAutoRoot(true, context); + Utils.toggleAutoRoot(!Utils.autoRootEnabled(context),context); } if (ACTION_ENABLEROOT.equals(action)) { Utils.toggleAutoRoot(false, context); Utils.toggleRoot(true); } if (ACTION_DISABLEROOT.equals(action)) { + Utils.toggleAutoRoot(false, context); Utils.toggleRoot(false); } diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java new file mode 100644 index 000000000..2121f5f57 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java @@ -0,0 +1,5 @@ +package com.topjohnwu.magisk.receivers; + +public interface Receiver { + void onResult(); +} diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java new file mode 100644 index 000000000..b687eca66 --- /dev/null +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java @@ -0,0 +1,19 @@ +package com.topjohnwu.magisk.receivers; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; + +public class RootFragmentReceiver extends BroadcastReceiver { + + private Receiver mFragment; + public RootFragmentReceiver(Receiver fragment) { + mFragment = fragment; + } + + public void onReceive(Context context, Intent intent) { + if (intent.getAction().equals(420)) { + mFragment.onResult(); + } + } +} diff --git a/app/src/main/java/com/topjohnwu/magisk/tile/CustomTileHelper.java b/app/src/main/java/com/topjohnwu/magisk/tile/CustomTileHelper.java deleted file mode 100644 index 94a889f13..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/tile/CustomTileHelper.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.topjohnwu.magisk.tile; - -import android.content.Context; -import android.content.Intent; -import android.support.annotation.NonNull; - -import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder; -import com.topjohnwu.magisk.R; - -final public class CustomTileHelper { - /** - * This is the identifier of the custom Broadcast Tile. Whatever action you configured the tile - * for must be used when configuring the tile. For Broadcast tiles, only alphanumeric characters - * (and periods) are allowed. Keep in mind that this excludes underscores. - */ - private static final String BROADCAST_TILE_IDENTIFIER = "com.kcoppock.CUSTOMTILE"; - - /** - * Keeps track of the last known state of the Quick Settings custom tile. There doesn't seem to - * be a way to query the state of the tile. - */ - private static final String PREF_TILE_SHOWN = "com.kcoppock.CUSTOMTILE_SHOWN"; - - private final Context mContext; - private final TilePreferenceHelper mTilePreferenceHelper; - - CustomTileHelper(@NonNull Context context) { - mContext = context.getApplicationContext(); - mTilePreferenceHelper = new TilePreferenceHelper(mContext); - } - - void showTile() { - mTilePreferenceHelper.setBoolean(PREF_TILE_SHOWN, true); - - // Set up an Intent that will be broadcast by the system, and received by the exported - // PublicBroadcastReceiver. - - - - - // Send the update event to the Broadcast Tile. Custom tiles are hidden by default until - // enabled with this broadcast Intent. - mContext.sendBroadcast(new BroadcastTileIntentBuilder(mContext, BROADCAST_TILE_IDENTIFIER) - .setVisible(true) - .build()); - } - - void hideTile() { - mTilePreferenceHelper.setBoolean(PREF_TILE_SHOWN, false); - - mContext.sendBroadcast(new BroadcastTileIntentBuilder(mContext, BROADCAST_TILE_IDENTIFIER) - .setVisible(false) - .build()); - } - - boolean isLastTileStateShown() { - return mTilePreferenceHelper.getBoolean(PREF_TILE_SHOWN); - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/tile/PublicBroadcastReceiver.java b/app/src/main/java/com/topjohnwu/magisk/tile/PublicBroadcastReceiver.java deleted file mode 100644 index 1e6f3255c..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/tile/PublicBroadcastReceiver.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.topjohnwu.magisk.tile; - -import android.content.*; -import android.widget.Toast; - -/** - * Exported receiver for the custom event on the custom Quick Settings tile - */ -public final class PublicBroadcastReceiver extends BroadcastReceiver { - /** - * The action broadcast from the Quick Settings tile when clicked - */ - public static final String ACTION_TOAST = "com.kcoppock.CUSTOMTILE_ACTION_TOAST"; - - /** - * Constant for the String extra to be displayed in the Toast - */ - public static final String EXTRA_MESSAGE = "com.kcoppock.CUSTOMTILE_EXTRA_MESSAGE"; - - @Override - public void onReceive(Context context, Intent intent) { - final String action = intent.getAction(); - - if (ACTION_TOAST.equals(action)) { - final String message = intent.getStringExtra(EXTRA_MESSAGE); - Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); - } - } -} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/tile/TilePreferenceHelper.java b/app/src/main/java/com/topjohnwu/magisk/tile/TilePreferenceHelper.java deleted file mode 100644 index b64adb57b..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/tile/TilePreferenceHelper.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.topjohnwu.magisk.tile; - -import android.content.Context; -import android.content.SharedPreferences; -import android.support.annotation.NonNull; - -/** - * Helper class for tracking preference values to keep track of the state of the custom tile - */ -final public class TilePreferenceHelper { - private static final String PREFS_NAME = "tile_prefs"; - - private final SharedPreferences mSharedPreferences; - - TilePreferenceHelper(@NonNull Context context) { - mSharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); - } - - void setBoolean(@NonNull String key, boolean value) { - mSharedPreferences.edit().putBoolean(key, value).apply(); - } - - boolean getBoolean(@NonNull String key) { - return mSharedPreferences.getBoolean(key, false); - } -} \ No newline at end of file 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 d286b439b..d47381201 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -10,6 +10,7 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; @@ -32,13 +33,15 @@ import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder; import com.topjohnwu.magisk.ModulesFragment; import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.ReposFragment; +import com.topjohnwu.magisk.RootFragment; import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Repo; import com.topjohnwu.magisk.module.RepoHelper; -import com.topjohnwu.magisk.tile.PrivateBroadcastReceiver; +import com.topjohnwu.magisk.receivers.PrivateBroadcastReceiver; +import com.topjohnwu.magisk.receivers.Receiver; +import com.topjohnwu.magisk.receivers.RootFragmentReceiver; import com.topjohnwu.magisk.services.MonitorService; import com.topjohnwu.magisk.services.QuickSettingTileService; -import com.topjohnwu.magisk.tile.CustomTileHelper; import org.json.JSONException; import org.json.JSONObject; @@ -96,7 +99,9 @@ public class Utils { } public static boolean autoRootEnabled(Context context) { - return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("autoRootEnable", false); + SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); + Log.d("Magisk", "AutoRootEnableCheck is " + preferences.getBoolean("autoRootEnable", false)); + return PreferenceManager.getDefaultSharedPreferences(context).getBoolean("autoRootEnable", false); } @@ -119,22 +124,26 @@ public class Utils { } public static void toggleRoot(Boolean b) { - 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 (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"); + } } } public static void toggleAutoRoot(Boolean b, Context context) { - 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 (Utils.magiskVersion != -1) { + PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("autoRootEnable", b).apply(); + Intent myServiceIntent = new Intent(context, MonitorService.class); + if (b) { + context.startService(myServiceIntent); + } else { + context.stopService(myServiceIntent); + } } - + UpdateRootFragmentUI(context); } public static List getModList(String path) { @@ -196,22 +205,24 @@ public class Utils { } public static void SetupQuickSettingsTile(Context mContext) { - Log.d("Magisk","Utils: SetupQuickSettings called"); + Log.d("Magisk", "Utils: SetupQuickSettings called"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Intent serviceIntent = new Intent(mContext, QuickSettingTileService.class); mContext.startService(serviceIntent); } if (Build.VERSION.SDK_INT == Build.VERSION_CODES.M) { - Log.d("Magisk","Utils: Marshmallow build detected"); + Log.d("Magisk", "Utils: Marshmallow build detected"); String mLabelString; - int mRootIcon = R.drawable.root; - int mAutoRootIcon = R.drawable.ic_autoroot; + int mRootIcon = R.drawable.root_white; + int mAutoRootIcon = R.drawable.ic_autoroot_white; + int mRootDisabled = R.drawable.root_grey; int mRootsState = CheckRootsState(mContext); - Log.d("Magisk","Utils: Root State returned as " + mRootsState); + Log.d("Magisk", "Utils: Root State returned as " + mRootsState); final Intent enableBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_ENABLEROOT); final Intent disableBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_DISABLEROOT); final Intent autoBroadcast = new Intent(PrivateBroadcastReceiver.ACTION_AUTOROOT); Intent intent; + int mIcon; switch (mRootsState) { case 2: @@ -222,30 +233,41 @@ public class Utils { case 1: mLabelString = "Root enabled"; mIcon = mRootIcon; - intent = enableBroadcast; + intent = disableBroadcast; break; case 0: mLabelString = "Root disabled"; - mIcon = mRootIcon; - intent = disableBroadcast; + mIcon = mRootDisabled; + intent = enableBroadcast; break; default: - mLabelString = "Root enabled"; - mIcon = mRootIcon; - intent = enableBroadcast; + mLabelString = "Root disabled"; + mIcon = mRootDisabled; + intent = disableBroadcast; break; } - Intent tileConfigurationIntent = new BroadcastTileIntentBuilder(mContext, "ROOT") + Intent tileConfigurationIntent = new BroadcastTileIntentBuilder(mContext, "Magisk") .setLabel(mLabelString) .setIconResource(mIcon) .setOnClickBroadcast(intent) + .setOnLongClickBroadcast(autoBroadcast) + .setVisible(true) .build(); mContext.sendBroadcast(tileConfigurationIntent); - + } } + public static void UpdateRootFragmentUI(Context context) { + + Log.d("Magisk", "Utils: UpdateRF called"); + Intent intent = new Intent(context, RootFragment.class); + intent.setAction("com.magisk.UPDATEUI"); + context.sendBroadcast(intent); + + } + // Gets an overall state for the quick settings tile // 0 for root disabled, 1 for root enabled (no auto), 2 for auto-root @@ -562,8 +584,6 @@ public class Utils { } } - - 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)) { diff --git a/app/src/main/res/drawable/ic_autoroot_white.xml b/app/src/main/res/drawable/ic_autoroot_white.xml new file mode 100644 index 000000000..ec532328f --- /dev/null +++ b/app/src/main/res/drawable/ic_autoroot_white.xml @@ -0,0 +1,4 @@ + + + diff --git a/app/src/main/res/drawable/root_grey.xml b/app/src/main/res/drawable/root_grey.xml new file mode 100644 index 000000000..f6bb38dc2 --- /dev/null +++ b/app/src/main/res/drawable/root_grey.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/root_white.xml b/app/src/main/res/drawable/root_white.xml new file mode 100644 index 000000000..c56582098 --- /dev/null +++ b/app/src/main/res/drawable/root_white.xml @@ -0,0 +1,9 @@ + + + \ No newline at end of file