mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-06-12 05:07:45 +02:00
Initial implementation of auto-mount, WIP
Will manually merge working product into /master
This commit is contained in:
154
app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java
Normal file
154
app/src/main/java/com/topjohnwu/magisk/AutoRootFragment.java
Normal file
@ -0,0 +1,154 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.ListFragment;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.topjohnwu.magisk.utils.ApplicationAdapter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class AutoRootFragment extends ListFragment {
|
||||
private PackageManager packageManager = null;
|
||||
private List<ApplicationInfo> applist = null;
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
listView = getListView();
|
||||
packageManager = getActivity().getPackageManager();
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
if (!prefs.contains("autoapps")) {
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
Set<String> set = new HashSet<String>();
|
||||
set.add("com.google.android.apps.walletnfcrel");
|
||||
editor.putStringSet("autoapps", set);
|
||||
editor.commit();
|
||||
}
|
||||
new LoadApplications().execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
new LoadApplications().execute();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onListItemClick(ListView l, View v, int position, long id) {
|
||||
super.onListItemClick(l, v, position, id);
|
||||
ApplicationInfo app = applist.get(position);
|
||||
ToggleApp(app.packageName, position, v);
|
||||
|
||||
}
|
||||
|
||||
private void ToggleApp(String appToCheck, int position, View v) {
|
||||
|
||||
Set<String> set = prefs.getStringSet("autoapps", null);
|
||||
|
||||
arrayList = new ArrayList<>(set);
|
||||
Log.d("Magisk", "Trying to toggle for " + appToCheck + " stringset is " + arrayList.toString());
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
|
||||
if (arrayList.contains(appToCheck)) {
|
||||
Log.d("Magisk", "App is in array, removing");
|
||||
|
||||
for (int i = 0; i < arrayList.size(); i++) {
|
||||
if (appToCheck.equals(arrayList.get(i))) {
|
||||
arrayList.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
arrayList.add(appToCheck);
|
||||
|
||||
}
|
||||
Set<String> set2 = new HashSet<String>(arrayList);
|
||||
Log.d("Magisk", "Committing set, value is: " + set2);
|
||||
editor.putStringSet("autoapps", set2);
|
||||
editor.apply();
|
||||
listadaptor.UpdateRootStatusView(position,v);
|
||||
|
||||
|
||||
}
|
||||
|
||||
private List<ApplicationInfo> checkForLaunchIntent(List<ApplicationInfo> list) {
|
||||
ArrayList<ApplicationInfo> applist = new ArrayList<ApplicationInfo>();
|
||||
for (ApplicationInfo info : list) {
|
||||
try {
|
||||
if (null != packageManager.getLaunchIntentForPackage(info.packageName)) {
|
||||
applist.add(info);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return applist;
|
||||
}
|
||||
|
||||
private class LoadApplications extends AsyncTask<Void, Void, Void> {
|
||||
private ProgressDialog progress = null;
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
applist = checkForLaunchIntent(packageManager.getInstalledApplications(PackageManager.GET_META_DATA));
|
||||
listadaptor = new ApplicationAdapter(getActivity(),
|
||||
R.layout.app_list_row, applist);
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCancelled() {
|
||||
super.onCancelled();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Void result) {
|
||||
setListAdapter(listadaptor);
|
||||
progress.dismiss();
|
||||
super.onPostExecute(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
progress = ProgressDialog.show(getActivity(), null,
|
||||
"Loading application info...");
|
||||
super.onPreExecute();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onProgressUpdate(Void... values) {
|
||||
super.onProgressUpdate(values);
|
||||
}
|
||||
}
|
||||
}
|
16
app/src/main/java/com/topjohnwu/magisk/AutoStart.java
Normal file
16
app/src/main/java/com/topjohnwu/magisk/AutoStart.java
Normal file
@ -0,0 +1,16 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.util.Log;
|
||||
|
||||
public class AutoStart extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
Log.d("Magisk", "Received Boot call, attempting to start service");
|
||||
Intent myIntent = new Intent(context, MonitorService.class);
|
||||
context.startService(myIntent);
|
||||
|
||||
}
|
||||
}
|
198
app/src/main/java/com/topjohnwu/magisk/MonitorService.java
Normal file
198
app/src/main/java/com/topjohnwu/magisk/MonitorService.java
Normal file
@ -0,0 +1,198 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.app.AppOpsManager;
|
||||
import android.app.Fragment;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
import android.app.usage.UsageStats;
|
||||
import android.app.usage.UsageStatsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
import android.os.CountDownTimer;
|
||||
import android.os.IBinder;
|
||||
import android.os.Process;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.NotificationCompat;
|
||||
import android.util.Log;
|
||||
|
||||
import com.topjohnwu.magisk.utils.Shell;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.SortedMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public class MonitorService extends Service
|
||||
|
||||
{
|
||||
|
||||
private static final String TAG = "Magisk";
|
||||
private UsageStatsManager mUsageStatsManager;
|
||||
private SharedPreferences prefs;
|
||||
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();
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
}).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 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.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
if (stats != null) {
|
||||
SortedMap<Long, UsageStats> mySortedMap = new TreeMap<>();
|
||||
|
||||
for (UsageStats usageStats : stats) {
|
||||
mySortedMap.put(usageStats.getLastTimeUsed(), usageStats);
|
||||
}
|
||||
|
||||
if (mySortedMap != null && !mySortedMap.isEmpty()) {
|
||||
topPackageName = mySortedMap.get(mySortedMap.lastKey()).getPackageName();
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
|
||||
Process.myUid(), getPackageName());
|
||||
return mode == AppOpsManager.MODE_ALLOWED;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
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.view.LayoutInflater;
|
||||
@ -25,33 +28,51 @@ import butterknife.ButterKnife;
|
||||
|
||||
public class RootFragment extends Fragment {
|
||||
|
||||
@BindView(R.id.progressBar) ProgressBar progressBar;
|
||||
|
||||
@BindView(R.id.rootSwitchView) View rootToggleView;
|
||||
@BindView(R.id.selinuxSwitchView) View selinuxToggleView;
|
||||
@BindView(R.id.rootStatusView) View rootStatusView;
|
||||
@BindView(R.id.safetynetStatusView) View safetynetStatusView;
|
||||
@BindView(R.id.selinuxStatusView) View selinuxStatusView;
|
||||
|
||||
@BindView(R.id.root_toggle) Switch rootToggle;
|
||||
@BindView(R.id.selinux_toggle) Switch selinuxToggle;
|
||||
|
||||
@BindView(R.id.root_status_container) View rootStatusContainer;
|
||||
@BindView(R.id.root_status_icon) ImageView rootStatusIcon;
|
||||
@BindView(R.id.root_status) TextView rootStatus;
|
||||
|
||||
@BindView(R.id.selinux_status_container) View selinuxStatusContainer;
|
||||
@BindView(R.id.selinux_status_icon) ImageView selinuxStatusIcon;
|
||||
@BindView(R.id.selinux_status) TextView selinuxStatus;
|
||||
|
||||
@BindView(R.id.safety_net_status) TextView safetyNetStatus;
|
||||
@BindView(R.id.safety_net_icon) ImageView safetyNetStatusIcon;
|
||||
|
||||
@BindColor(R.color.red500) int red500;
|
||||
@BindColor(R.color.green500) int green500;
|
||||
@BindColor(R.color.grey500) int grey500;
|
||||
@BindColor(R.color.accent) int accent;
|
||||
|
||||
public SharedPreferences prefs;
|
||||
@BindView(R.id.progressBar)
|
||||
ProgressBar progressBar;
|
||||
@BindView(R.id.rootSwitchView)
|
||||
View rootToggleView;
|
||||
@BindView(R.id.autoRootSwitchView)
|
||||
View autoRootToggleView;
|
||||
@BindView(R.id.selinuxSwitchView)
|
||||
View selinuxToggleView;
|
||||
@BindView(R.id.rootStatusView)
|
||||
View rootStatusView;
|
||||
@BindView(R.id.safetynetStatusView)
|
||||
View safetynetStatusView;
|
||||
@BindView(R.id.selinuxStatusView)
|
||||
View selinuxStatusView;
|
||||
@BindView(R.id.root_toggle)
|
||||
Switch rootToggle;
|
||||
@BindView(R.id.auto_root_toggle)
|
||||
Switch autoRootToggle;
|
||||
@BindView(R.id.selinux_toggle)
|
||||
Switch selinuxToggle;
|
||||
@BindView(R.id.root_status_container)
|
||||
View rootStatusContainer;
|
||||
@BindView(R.id.root_status_icon)
|
||||
ImageView rootStatusIcon;
|
||||
@BindView(R.id.root_status)
|
||||
TextView rootStatus;
|
||||
@BindView(R.id.selinux_status_container)
|
||||
View selinuxStatusContainer;
|
||||
@BindView(R.id.selinux_status_icon)
|
||||
ImageView selinuxStatusIcon;
|
||||
@BindView(R.id.selinux_status)
|
||||
TextView selinuxStatus;
|
||||
@BindView(R.id.safety_net_status)
|
||||
TextView safetyNetStatus;
|
||||
@BindView(R.id.safety_net_icon)
|
||||
ImageView safetyNetStatusIcon;
|
||||
@BindColor(R.color.red500)
|
||||
int red500;
|
||||
@BindColor(R.color.green500)
|
||||
int green500;
|
||||
@BindColor(R.color.grey500)
|
||||
int grey500;
|
||||
@BindColor(R.color.accent)
|
||||
int accent;
|
||||
int statusOK = R.drawable.ic_check_circle;
|
||||
int statusError = R.drawable.ic_error;
|
||||
int statusUnknown = R.drawable.ic_help;
|
||||
@ -63,10 +84,25 @@ public class RootFragment extends Fragment {
|
||||
ButterKnife.bind(this, view);
|
||||
|
||||
new updateUI().execute();
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
|
||||
rootToggle.setOnClickListener(toggle -> {
|
||||
Shell.su(((CompoundButton) toggle).isChecked() ? "setprop magisk.root 1" : "setprop magisk.root 0");
|
||||
new Handler().postDelayed(() -> new updateUI().execute(), 1000);
|
||||
if (!((CompoundButton) toggle).isChecked()) {
|
||||
autoRootToggle.setChecked(false);
|
||||
}
|
||||
});
|
||||
|
||||
autoRootToggle.setOnClickListener(toggle -> {
|
||||
SharedPreferences.Editor editor = prefs.edit();
|
||||
editor.putBoolean("autoRootEnable", ((CompoundButton) toggle).isChecked());
|
||||
editor.commit();
|
||||
if (((CompoundButton) toggle).isChecked()) {
|
||||
Intent myIntent = new Intent(getActivity(), MonitorService.class);
|
||||
getActivity().startService(myIntent);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
selinuxToggle.setOnClickListener(toggle -> {
|
||||
@ -77,7 +113,13 @@ public class RootFragment extends Fragment {
|
||||
return view;
|
||||
}
|
||||
|
||||
private class updateUI extends AsyncTask<Void, Void, Void> {
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
new updateUI().execute();
|
||||
}
|
||||
|
||||
public class updateUI extends AsyncTask<Void, Void, Void> {
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
@ -98,6 +140,7 @@ public class RootFragment extends Fragment {
|
||||
|
||||
if (Shell.rootAccess()) {
|
||||
rootToggleView.setVisibility(View.VISIBLE);
|
||||
autoRootToggleView.setVisibility(View.VISIBLE);
|
||||
selinuxToggleView.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
||||
@ -183,7 +226,7 @@ public class RootFragment extends Fragment {
|
||||
rootToggle.setChecked(true);
|
||||
safetyNetStatusIcon.setImageResource(statusError);
|
||||
safetyNetStatus.setText(R.string.root_system_info);
|
||||
|
||||
autoRootToggleView.setVisibility(View.GONE);
|
||||
rootToggleView.setVisibility(View.GONE);
|
||||
selinuxToggleView.setVisibility(View.GONE);
|
||||
break;
|
||||
|
@ -1,11 +1,16 @@
|
||||
package com.topjohnwu.magisk;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.ActivityManager;
|
||||
import android.app.AppOpsManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.provider.Settings;
|
||||
import android.support.annotation.IdRes;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.NavigationView;
|
||||
@ -31,9 +36,12 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
|
||||
private final Handler mDrawerHandler = new Handler();
|
||||
|
||||
@BindView(R.id.toolbar) Toolbar toolbar;
|
||||
@BindView(R.id.drawer_layout) DrawerLayout drawer;
|
||||
@BindView(R.id.nav_view) NavigationView navigationView;
|
||||
@BindView(R.id.toolbar)
|
||||
Toolbar toolbar;
|
||||
@BindView(R.id.drawer_layout)
|
||||
DrawerLayout drawer;
|
||||
@BindView(R.id.nav_view)
|
||||
NavigationView navigationView;
|
||||
|
||||
@IdRes
|
||||
private int mSelectedId = R.id.magisk;
|
||||
@ -43,16 +51,25 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_welcome);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
|
||||
}
|
||||
|
||||
|
||||
// Startups
|
||||
PreferenceManager.setDefaultValues(this, R.xml.defaultpref, false);
|
||||
if (!isMyServiceRunning(MonitorService.class)) {
|
||||
Intent myIntent = new Intent(getApplication(), MonitorService.class);
|
||||
getApplication().startService(myIntent);
|
||||
}
|
||||
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|
||||
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
|
||||
}
|
||||
if (!hasPermission()) {
|
||||
startActivityForResult(new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS), 100);
|
||||
}
|
||||
|
||||
new Utils.Initialize(this).execute();
|
||||
new Utils.CheckUpdates(this).execute();
|
||||
new Utils.LoadModules(this).execute();
|
||||
@ -85,9 +102,12 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
}
|
||||
|
||||
navigationView.setNavigationItemSelectedListener(this);
|
||||
|
||||
if (getIntent().hasExtra("relaunch")) {
|
||||
navigate(R.id.root);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
@ -114,6 +134,25 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean hasPermission() {
|
||||
AppOpsManager appOps = (AppOpsManager)
|
||||
getSystemService(Context.APP_OPS_SERVICE);
|
||||
int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS,
|
||||
android.os.Process.myUid(), getPackageName());
|
||||
return mode == AppOpsManager.MODE_ALLOWED;
|
||||
|
||||
}
|
||||
|
||||
private boolean isMyServiceRunning(Class<?> serviceClass) {
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
|
||||
if (serviceClass.getName().equals(service.service.getClassName())) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void navigate(final int itemId) {
|
||||
Fragment navFragment = null;
|
||||
String tag = "";
|
||||
@ -128,6 +167,11 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
tag = "root";
|
||||
navFragment = new RootFragment();
|
||||
break;
|
||||
case R.id.autoroot:
|
||||
setTitle(R.string.auto_root);
|
||||
tag = "ic_autoroot";
|
||||
navFragment = new AutoRootFragment();
|
||||
break;
|
||||
case R.id.modules:
|
||||
setTitle(R.string.modules);
|
||||
tag = "modules";
|
||||
@ -154,4 +198,6 @@ public class WelcomeActivity extends AppCompatActivity implements NavigationView
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,107 @@
|
||||
package com.topjohnwu.magisk.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.topjohnwu.magisk.R;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class ApplicationAdapter extends ArrayAdapter<ApplicationInfo> {
|
||||
private List<ApplicationInfo> appsList = null;
|
||||
private Context context;
|
||||
private PackageManager packageManager;
|
||||
public ArrayList arrayList;
|
||||
public SharedPreferences prefs;
|
||||
|
||||
public ApplicationAdapter(Context context, int textViewResourceId,
|
||||
List<ApplicationInfo> appsList) {
|
||||
super(context, textViewResourceId, appsList);
|
||||
this.context = context;
|
||||
this.appsList = appsList;
|
||||
packageManager = context.getPackageManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCount() {
|
||||
return ((null != appsList) ? appsList.size() : 0);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApplicationInfo getItem(int position) {
|
||||
return ((null != appsList) ? appsList.get(position) : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getItemId(int position) {
|
||||
return position;
|
||||
}
|
||||
|
||||
@Override
|
||||
public View getView(int position, View convertView, ViewGroup parent) {
|
||||
View view = convertView;
|
||||
|
||||
prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
|
||||
if (null == view) {
|
||||
LayoutInflater layoutInflater = (LayoutInflater) context
|
||||
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
view = layoutInflater.inflate(R.layout.app_list_row, null);
|
||||
}
|
||||
|
||||
ApplicationInfo applicationInfo = appsList.get(position);
|
||||
if (null != applicationInfo) {
|
||||
TextView appName = (TextView) view.findViewById(R.id.app_name);
|
||||
TextView packageName = (TextView) view.findViewById(R.id.app_paackage);
|
||||
ImageView iconview = (ImageView) view.findViewById(R.id.app_icon);
|
||||
ImageView statusview = (ImageView) view.findViewById(R.id.app_status);
|
||||
appName.setText(applicationInfo.loadLabel(packageManager));
|
||||
packageName.setText(applicationInfo.packageName);
|
||||
iconview.setImageDrawable(applicationInfo.loadIcon(packageManager));
|
||||
if (CheckApp(applicationInfo.packageName)) {
|
||||
statusview.setImageDrawable(this.context.getDrawable(R.drawable.root));
|
||||
} else {
|
||||
statusview.setImageDrawable(null);
|
||||
}
|
||||
}
|
||||
return view;
|
||||
}
|
||||
|
||||
public void UpdateRootStatusView(int position, View convertView) {
|
||||
View view = convertView;
|
||||
if (null == view) {
|
||||
LayoutInflater layoutInflater = (LayoutInflater) context
|
||||
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
view = layoutInflater.inflate(R.layout.app_list_row, null);
|
||||
}
|
||||
ApplicationInfo applicationInfo = appsList.get(position);
|
||||
if (null != applicationInfo) {
|
||||
ImageView statusview = (ImageView) view.findViewById(R.id.app_status);
|
||||
if (CheckApp(applicationInfo.packageName)) {
|
||||
statusview.setImageDrawable(this.context.getDrawable(R.drawable.root));
|
||||
} else {
|
||||
statusview.setImageDrawable(null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private boolean CheckApp(String appToCheck) {
|
||||
Set<String> set = prefs.getStringSet("autoapps", null);
|
||||
arrayList = new ArrayList<>(set);
|
||||
return arrayList.toString().contains(appToCheck);
|
||||
|
||||
}
|
||||
|
||||
|
||||
};
|
Reference in New Issue
Block a user