From 16c804106a0239891dd7f471ae10b91441f8323d Mon Sep 17 00:00:00 2001 From: d8ahazard Date: Sat, 24 Sep 2016 10:07:30 -0500 Subject: [PATCH] Code clean-up --- .../topjohnwu/magisk/AutoRootFragment.java | 1 + .../com/topjohnwu/magisk/RootFragment.java | 18 +---- .../com/topjohnwu/magisk/WelcomeActivity.java | 78 ++++++------------- .../magisk/receivers/AutoStartReceiver.java | 1 + .../topjohnwu/magisk/receivers/Receiver.java | 5 -- .../receivers/RootFragmentReceiver.java | 19 ----- .../magisk/receivers/TileReceiver.java | 25 ------ .../magisk/services/MonitorService.java | 46 +++-------- .../magisk/services/TileServiceCompat.java | 1 - .../magisk/utils/AnimationHelper.java | 58 -------------- .../topjohnwu/magisk/utils/PrefHelper.java | 4 +- .../com/topjohnwu/magisk/utils/Utils.java | 1 + 12 files changed, 41 insertions(+), 216 deletions(-) delete mode 100644 app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/receivers/TileReceiver.java delete mode 100644 app/src/main/java/com/topjohnwu/magisk/utils/AnimationHelper.java diff --git a/app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java b/app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java index cbe29a1d2..35e61e481 100644 --- a/app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java @@ -77,6 +77,7 @@ public class AutoRootFragment extends ListFragment { Set set = new HashSet<>(); set.add("com.google.android.apps.walletnfcrel"); set.add("com.google.android.gms"); + set.add("com.google.commerce.tapandpay"); editor.putStringSet("auto_blacklist", set); editor.apply(); } diff --git a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java index 89ebdeacc..81c22f112 100644 --- a/app/src/main/java/com/topjohnwu/magisk/RootFragment.java +++ b/app/src/main/java/com/topjohnwu/magisk/RootFragment.java @@ -21,7 +21,6 @@ import android.widget.Switch; import android.widget.TextView; 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; @@ -35,7 +34,7 @@ import butterknife.BindColor; import butterknife.BindView; import butterknife.ButterKnife; -public class RootFragment extends Fragment implements Receiver { +public class RootFragment extends Fragment { public SharedPreferences prefs; @BindView(R.id.progressBar) @@ -108,10 +107,7 @@ public class RootFragment extends Fragment implements Receiver { autoRootToggle.setChecked(autoRootStatus); new updateUI().execute(); - rootToggle.setOnClickListener(toggle -> { - Utils.toggleRoot(((CompoundButton) toggle).isChecked(), getActivity()); - - }); + rootToggle.setOnClickListener(toggle -> Utils.toggleRoot(((CompoundButton) toggle).isChecked(), getActivity())); autoRootToggle.setOnClickListener(toggle -> { if (!Utils.hasServicePermission(getActivity())) { @@ -128,7 +124,6 @@ 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(); @@ -186,8 +181,6 @@ public class RootFragment extends Fragment implements Receiver { rootToggle.setEnabled(true); } - - } @Override @@ -197,11 +190,6 @@ public class RootFragment extends Fragment implements Receiver { new updateUI().execute(); } - @Override - public void onResult() { - - } - public class updateUI extends AsyncTask { @Override @@ -219,10 +207,8 @@ public class RootFragment extends Fragment implements Receiver { @Override protected void onPostExecute(Void v) { super.onPostExecute(v); - autoRootToggle.setChecked(autoRootStatus); progressBar.setVisibility(View.GONE); - rootStatusView.setVisibility(View.VISIBLE); safetynetStatusView.setVisibility(View.VISIBLE); selinuxStatusView.setVisibility(View.VISIBLE); diff --git a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java index f48ae1516..182e4f345 100644 --- a/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java +++ b/app/src/main/java/com/topjohnwu/magisk/WelcomeActivity.java @@ -2,10 +2,8 @@ package com.topjohnwu.magisk; import android.Manifest; import android.app.Fragment; -import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; -import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.os.AsyncTask; import android.os.Build; @@ -24,7 +22,6 @@ import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; -import com.topjohnwu.magisk.module.RepoHelper; import com.topjohnwu.magisk.services.MonitorService; import com.topjohnwu.magisk.utils.Async; import com.topjohnwu.magisk.utils.Logger; @@ -39,7 +36,6 @@ 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; @@ -57,10 +53,8 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView setContentView(R.layout.activity_welcome); ButterKnife.bind(this); - // Startups PreferenceManager.setDefaultValues(this, R.xml.defaultpref, false); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); if (Utils.autoToggleEnabled(getApplicationContext())) { if (!Utils.isMyServiceRunning(MonitorService.class, getApplicationContext())) { Intent myIntent = new Intent(getApplication(), MonitorService.class); @@ -76,72 +70,52 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView this.getFragmentManager().addOnBackStackChangedListener( () -> { - FragmentManager fragmentManager = getFragmentManager(); - Fragment hm=getFragmentManager().findFragmentByTag("root"); - if(hm!=null) - { - if(hm.isVisible()) - { + 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()) - { + 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()) - { + 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()) - { + 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()) - { + 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()) - { + 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()) - { + hm = getFragmentManager().findFragmentByTag("settings"); + if (hm != null) { + if (hm.isVisible()) { navigationView.setCheckedItem(R.id.settings); } } }); - - Utils.init(this); new Async.CheckUpdates(this).execute(); - RepoHelper.TaskDelegate delegate = result -> { - //Do a thing here when we get a result we want - }; new Async.LoadModules(this).execute(); new Async.LoadRepos(this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); @@ -178,7 +152,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView } } - @Override protected void onDestroy() { super.onDestroy(); @@ -195,22 +168,18 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView @Override public void onBackPressed() { - if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { int backStackEntryCount = getFragmentManager().getBackStackEntryCount(); Logger.dh("Welcomeactivity: Entrycount is " + backStackEntryCount); - if(backStackEntryCount>=2) { + if (backStackEntryCount >= 2) { super.onBackPressed(); } else { finish(); } } - - - } @Override @@ -231,7 +200,7 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView public void navigate(final int itemId) { Fragment navFragment = null; - tag = ""; + String tag = ""; switch (itemId) { case R.id.magisk: setTitle(R.string.magisk); @@ -275,7 +244,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView if (navFragment != null) { - FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); try { diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/AutoStartReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/AutoStartReceiver.java index 70ff9c9ba..84ff2de9b 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/AutoStartReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/AutoStartReceiver.java @@ -20,6 +20,7 @@ public class AutoStartReceiver extends BroadcastReceiver { } if (PrefHelper.CheckBool("enable_quicktile",context)) { Utils.SetupQuickSettingsTile(context); + } diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java deleted file mode 100644 index 2121f5f57..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/Receiver.java +++ /dev/null @@ -1,5 +0,0 @@ -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 deleted file mode 100644 index b687eca66..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/RootFragmentReceiver.java +++ /dev/null @@ -1,19 +0,0 @@ -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/receivers/TileReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/TileReceiver.java deleted file mode 100644 index c3023caa5..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/TileReceiver.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.topjohnwu.magisk.receivers; - -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.util.Log; - -import com.topjohnwu.magisk.services.TileServiceCompat; - -public class TileReceiver extends BroadcastReceiver { - private static final String TAG = "MainReceiver"; - public TileReceiver() { - } - - @Override - public void onReceive(Context context, Intent intent) { - Log.d(TAG,"RECEIVED"); - String action = intent.getAction(); - - if (action.equals(Intent.ACTION_BOOT_COMPLETED) || action.equals(Intent.ACTION_USER_PRESENT) || action.equals(Intent.ACTION_SCREEN_ON)) { - context.startService(new Intent(context,TileServiceCompat.class)); - - } - } -} diff --git a/app/src/main/java/com/topjohnwu/magisk/services/MonitorService.java b/app/src/main/java/com/topjohnwu/magisk/services/MonitorService.java index cd5df34cf..ea378f2ab 100644 --- a/app/src/main/java/com/topjohnwu/magisk/services/MonitorService.java +++ b/app/src/main/java/com/topjohnwu/magisk/services/MonitorService.java @@ -7,7 +7,6 @@ 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; @@ -25,7 +24,6 @@ import com.topjohnwu.magisk.utils.Utils; import java.util.Set; public class MonitorService extends AccessibilityService { - private static final String TAG = "MonitorService"; private Boolean disableroot; @Override @@ -64,22 +62,18 @@ public class MonitorService extends AccessibilityService { event.getPackageName().toString(), event.getClassName().toString() ); + Logger.dh("MonitorService: CurrentActivity: " + event.getPackageName()); - ActivityInfo activityInfo = tryGetActivity(componentName); - boolean isActivity = activityInfo != null; - if (isActivity) { - Logger.dh("MonitorService: CurrentActivity: " + componentName.getPackageName()); - String mPackage = componentName.getPackageName(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); - if (Utils.autoToggleEnabled(getApplicationContext())) { - Set setBlackList = prefs.getStringSet("auto_blacklist", null); + String mPackage = componentName.getPackageName(); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); + if (Utils.autoToggleEnabled(getApplicationContext())) { + Set setBlackList = prefs.getStringSet("auto_blacklist", null); - if (setBlackList != null) { - disableroot = setBlackList.contains(mPackage); - ForceRoot(!disableroot); - String appFriendly = getAppName(mPackage); - ShowNotification(disableroot, appFriendly); - } + if (setBlackList != null) { + disableroot = setBlackList.contains(mPackage); + ForceRoot(!disableroot); + String appFriendly = getAppName(mPackage); + ShowNotification(disableroot, appFriendly); } } } @@ -98,36 +92,20 @@ public class MonitorService extends AccessibilityService { } } - private ActivityInfo tryGetActivity(ComponentName componentName) { - try { - return getPackageManager().getActivityInfo(componentName, 0); - } catch (PackageManager.NameNotFoundException e) { - return null; - } - } - private void ForceRoot(Boolean rootToggle) { String rootString = rootToggle ? "on" : "off"; if (Utils.rootEnabled() != rootToggle) { Logger.dh("MonitorService: toggling root " + rootString); - Utils.toggleRoot(rootToggle,getApplicationContext()); + Utils.toggleRoot(rootToggle, getApplicationContext()); if (Utils.rootEnabled() != rootToggle) { - Utils.toggleRoot(rootToggle,getApplicationContext()); + Utils.toggleRoot(rootToggle, getApplicationContext()); Logger.dh("MonitorService: FORCING to " + rootString); } } } - private void ForceEnableRoot() { - Log.d("Magisk", "MonitorService: ForceEnable called."); - Utils.toggleRoot(true,getApplicationContext()); - if (!Utils.rootEnabled()) { - Utils.toggleRoot(true,getApplicationContext()); - } - } - private void ShowNotification(boolean rootAction, String packageName) { NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); NotificationCompat.Builder mBuilder; diff --git a/app/src/main/java/com/topjohnwu/magisk/services/TileServiceCompat.java b/app/src/main/java/com/topjohnwu/magisk/services/TileServiceCompat.java index 14886fe2d..76c9c72e8 100644 --- a/app/src/main/java/com/topjohnwu/magisk/services/TileServiceCompat.java +++ b/app/src/main/java/com/topjohnwu/magisk/services/TileServiceCompat.java @@ -5,7 +5,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.PackageManager; import android.os.IBinder; import com.kcoppock.broadcasttilesupport.BroadcastTileIntentBuilder; diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/AnimationHelper.java b/app/src/main/java/com/topjohnwu/magisk/utils/AnimationHelper.java deleted file mode 100644 index ebb1d3301..000000000 --- a/app/src/main/java/com/topjohnwu/magisk/utils/AnimationHelper.java +++ /dev/null @@ -1,58 +0,0 @@ -package com.topjohnwu.magisk.utils; - -import android.view.View; -import android.view.animation.Animation; -import android.view.animation.Transformation; -import android.widget.LinearLayout; - -public class AnimationHelper { - public static void expand(final View v) { - v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); - final int targetHeight = v.getMeasuredHeight(); - - v.getLayoutParams().height = 0; - v.setVisibility(View.VISIBLE); - Animation a = new Animation() { - @Override - protected void applyTransformation(float interpolatedTime, Transformation t) { - v.getLayoutParams().height = interpolatedTime == 1 - ? LinearLayout.LayoutParams.WRAP_CONTENT - : (int) (targetHeight * interpolatedTime); - v.requestLayout(); - } - - @Override - public boolean willChangeBounds() { - return true; - } - }; - - // 1dp/ms - a.setDuration((int) (targetHeight / v.getContext().getResources().getDisplayMetrics().density)); - v.startAnimation(a); - } - - public static void collapse(final View v) { - final int initialHeight = v.getMeasuredHeight(); - - Animation a = new Animation() { - @Override - protected void applyTransformation(float interpolatedTime, Transformation t) { - if (interpolatedTime == 1) { - v.setVisibility(View.GONE); - } else { - v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime); - v.requestLayout(); - } - } - - @Override - public boolean willChangeBounds() { - return true; - } - }; - // 1dp/ms - a.setDuration((int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density)); - v.startAnimation(a); - } -} \ No newline at end of file diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/PrefHelper.java b/app/src/main/java/com/topjohnwu/magisk/utils/PrefHelper.java index d96b82fb8..d75c085fa 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/PrefHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/PrefHelper.java @@ -1,7 +1,5 @@ package com.topjohnwu.magisk.utils; - -import android.app.Application; import android.content.Context; import android.preference.PreferenceManager; @@ -17,7 +15,7 @@ public class PrefHelper { public static void SetBool(String key, Boolean value, Context context) { - PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).commit(); + PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(key, value).apply(); } 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 f614caff4..dc3aea940 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -293,6 +293,7 @@ public class Utils { .setVisible(true) .build(); mContext.sendBroadcast(tileConfigurationIntent); + mContext.startService(new Intent(mContext, TileServiceCompat.class)); } }