Code cleanups

This commit is contained in:
topjohnwu 2017-07-21 05:08:39 +08:00
parent 89e5b8d057
commit 72820b162c
8 changed files with 71 additions and 154 deletions

View File

@ -1,12 +1,10 @@
package com.topjohnwu.magisk; package com.topjohnwu.magisk;
import android.content.pm.PackageManager;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.view.MenuItemCompat; import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -15,7 +13,6 @@ import android.view.ViewGroup;
import android.widget.SearchView; import android.widget.SearchView;
import com.topjohnwu.magisk.adapters.ApplicationAdapter; import com.topjohnwu.magisk.adapters.ApplicationAdapter;
import com.topjohnwu.magisk.asyncs.MagiskHide;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.utils.CallbackEvent; import com.topjohnwu.magisk.utils.CallbackEvent;
import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Logger;
@ -46,13 +43,12 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_magisk_hide, container, false); View view = inflater.inflate(R.layout.fragment_magisk_hide, container, false);
unbinder = ButterKnife.bind(this, view); unbinder = ButterKnife.bind(this, view);
lastFilter = "";
PackageManager packageManager = getActivity().getPackageManager();
mSwipeRefreshLayout.setRefreshing(true); mSwipeRefreshLayout.setRefreshing(true);
mSwipeRefreshLayout.setOnRefreshListener(() -> new MagiskHide(getActivity()).list()); mSwipeRefreshLayout.setOnRefreshListener(() -> appAdapter.refresh());
appAdapter = new ApplicationAdapter(packageManager); appAdapter = new ApplicationAdapter(getActivity());
recyclerView.setAdapter(appAdapter); recyclerView.setAdapter(appAdapter);
searchListener = new SearchView.OnQueryTextListener() { searchListener = new SearchView.OnQueryTextListener() {
@ -71,10 +67,6 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
} }
}; };
if (getApplication().magiskHideDone.isTriggered) {
onTrigger(getApplication().magiskHideDone);
}
return view; return view;
} }
@ -107,10 +99,7 @@ public class MagiskHideFragment extends Fragment implements CallbackEvent.Listen
@Override @Override
public void onTrigger(CallbackEvent<Void> event) { public void onTrigger(CallbackEvent<Void> event) {
Logger.dev("MagiskHideFragment: UI refresh"); Logger.dev("MagiskHideFragment: UI refresh");
appAdapter.setLists(getApplication().appList, getApplication().magiskHideList);
mSwipeRefreshLayout.setRefreshing(false); mSwipeRefreshLayout.setRefreshing(false);
if (!TextUtils.isEmpty(lastFilter)) { appAdapter.filter(lastFilter);
appAdapter.filter(lastFilter);
}
} }
} }

View File

@ -5,7 +5,6 @@ import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.os.Build; import android.os.Build;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
@ -66,8 +65,6 @@ public class MagiskManager extends Application {
// Data // Data
public Map<String, Module> moduleMap; public Map<String, Module> moduleMap;
public List<String> blockList; public List<String> blockList;
public List<ApplicationInfo> appList;
public List<String> magiskHideList;
// Configurations // Configurations
public static boolean shellLogging; public static boolean shellLogging;

View File

@ -13,9 +13,7 @@ import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.widget.Toast; import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.MagiskHide;
import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.utils.Logger; import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
@ -158,18 +156,9 @@ public class SettingsActivity extends Activity {
case "magiskhide": case "magiskhide":
enabled = prefs.getBoolean("magiskhide", false); enabled = prefs.getBoolean("magiskhide", false);
if (enabled) { if (enabled) {
if (!magiskManager.isSuClient) { Utils.enableMagiskHide(magiskManager.shell);
new AlertDialogBuilder(getActivity())
.setTitle(R.string.no_magisksu_title)
.setMessage(R.string.no_magisksu_msg)
.setPositiveButton(R.string.understand, (dialog, which) -> new MagiskHide(getActivity()).enable())
.setCancelable(false)
.show();
} else {
new MagiskHide(getActivity()).enable();
}
} else { } else {
new MagiskHide(getActivity()).disable(); Utils.disableMagiskHide(magiskManager.shell);
} }
break; break;
case "hosts": case "hosts":

View File

@ -8,7 +8,6 @@ import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import com.topjohnwu.magisk.asyncs.LoadApps;
import com.topjohnwu.magisk.asyncs.LoadModules; import com.topjohnwu.magisk.asyncs.LoadModules;
import com.topjohnwu.magisk.asyncs.UpdateRepos; import com.topjohnwu.magisk.asyncs.UpdateRepos;
import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.Activity;
@ -53,7 +52,6 @@ public class SplashActivity extends Activity{
// Now fire all async tasks // Now fire all async tasks
loadModuleTask.exec(); loadModuleTask.exec();
new LoadApps(this).exec();
Intent intent = new Intent(this, MainActivity.class); Intent intent = new Intent(this, MainActivity.class);
String section = getIntent().getStringExtra(MagiskManager.INTENT_SECTION); String section = getIntent().getStringExtra(MagiskManager.INTENT_SECTION);

View File

@ -1,6 +1,6 @@
package com.topjohnwu.magisk.adapters; package com.topjohnwu.magisk.adapters;
import android.app.Activity; import android.content.Context;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
@ -14,13 +14,16 @@ import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.MagiskHide; import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.CallbackEvent;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator;
import java.util.List; import java.util.List;
import butterknife.BindView; import butterknife.BindView;
@ -41,20 +44,19 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
private List<ApplicationInfo> mOriginalList, mList; private List<ApplicationInfo> mOriginalList, mList;
private List<String> mHideList; private List<String> mHideList;
private PackageManager packageManager; private PackageManager pm;
private ApplicationFilter filter; private ApplicationFilter filter;
private CallbackEvent<Void> magiskHideDone;
private Shell shell;
public ApplicationAdapter(PackageManager packageManager) { public ApplicationAdapter(Context context) {
mOriginalList = mList = Collections.emptyList(); mOriginalList = mList = Collections.emptyList();
mHideList = Collections.emptyList(); mHideList = Collections.emptyList();
this.packageManager = packageManager;
filter = new ApplicationFilter(); filter = new ApplicationFilter();
} pm = context.getPackageManager();
magiskHideDone = Utils.getMagiskManager(context).magiskHideDone;
public void setLists(List<ApplicationInfo> listApps, List<String> hideList) { shell = Utils.getMagiskManager(context).shell;
mOriginalList = mList = listApps; new LoadApps().exec();
mHideList = hideList;
notifyDataSetChanged();
} }
@Override @Override
@ -67,8 +69,8 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
public void onBindViewHolder(final ViewHolder holder, int position) { public void onBindViewHolder(final ViewHolder holder, int position) {
ApplicationInfo info = mList.get(position); ApplicationInfo info = mList.get(position);
holder.appIcon.setImageDrawable(info.loadIcon(packageManager)); holder.appIcon.setImageDrawable(info.loadIcon(pm));
holder.appName.setText(info.loadLabel(packageManager)); holder.appName.setText(info.loadLabel(pm));
holder.appPackage.setText(info.packageName); holder.appPackage.setText(info.packageName);
// Remove all listeners // Remove all listeners
@ -87,10 +89,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
holder.checkBox.setChecked(mHideList.contains(info.packageName)); holder.checkBox.setChecked(mHideList.contains(info.packageName));
holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> { holder.checkBox.setOnCheckedChangeListener((v, isChecked) -> {
if (isChecked) { if (isChecked) {
new MagiskHide((Activity) holder.itemView.getContext()).add(info.packageName); Utils.addMagiskHide(shell, info.packageName);
mHideList.add(info.packageName); mHideList.add(info.packageName);
} else { } else {
new MagiskHide((Activity) holder.itemView.getContext()).rm(info.packageName); Utils.rmMagiskHide(shell, info.packageName);
mHideList.remove(info.packageName); mHideList.remove(info.packageName);
} }
}); });
@ -106,6 +108,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
filter.filter(constraint); filter.filter(constraint);
} }
public void refresh() {
new LoadApps().exec();
}
static class ViewHolder extends RecyclerView.ViewHolder { static class ViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.app_icon) ImageView appIcon; @BindView(R.id.app_icon) ImageView appIcon;
@ -130,7 +136,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
filteredApps = new ArrayList<>(); filteredApps = new ArrayList<>();
String filter = constraint.toString().toLowerCase(); String filter = constraint.toString().toLowerCase();
for (ApplicationInfo info : mOriginalList) { for (ApplicationInfo info : mOriginalList) {
if (Utils.lowercaseContains(info.loadLabel(packageManager), filter) if (Utils.lowercaseContains(info.loadLabel(pm), filter)
|| Utils.lowercaseContains(info.packageName, filter)) { || Utils.lowercaseContains(info.packageName, filter)) {
filteredApps.add(info); filteredApps.add(info);
} }
@ -150,4 +156,27 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
notifyDataSetChanged(); notifyDataSetChanged();
} }
} }
private class LoadApps extends ParallelTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
mOriginalList = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = mOriginalList.iterator(); i.hasNext(); ) {
ApplicationInfo info = i.next();
if (ApplicationAdapter.BLACKLIST.contains(info.packageName) || !info.enabled) {
i.remove();
}
}
Collections.sort(mOriginalList, (a, b) -> a.loadLabel(pm).toString().toLowerCase()
.compareTo(b.loadLabel(pm).toString().toLowerCase()));
mHideList = Utils.listMagiskHide(shell);
return null;
}
@Override
protected void onPostExecute(Void v) {
magiskHideDone.trigger();
}
}
} }

View File

@ -1,44 +0,0 @@
package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.adapters.ApplicationAdapter;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class LoadApps extends ParallelTask<Void, Void, Void> {
public LoadApps(Context context) {
super(context);
}
@Override
protected Void doInBackground(Void... voids) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
PackageManager pm = magiskManager.getPackageManager();
List<ApplicationInfo> list = pm.getInstalledApplications(0);
for (Iterator<ApplicationInfo> i = list.iterator(); i.hasNext(); ) {
ApplicationInfo info = i.next();
if (ApplicationAdapter.BLACKLIST.contains(info.packageName) || !info.enabled) {
i.remove();
}
}
Collections.sort(list, (a, b) -> a.loadLabel(pm).toString().toLowerCase()
.compareTo(b.loadLabel(pm).toString().toLowerCase()));
magiskManager.appList = Collections.unmodifiableList(list);
return null;
}
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
new MagiskHide(magiskManager).list();
}
}

View File

@ -1,61 +0,0 @@
package com.topjohnwu.magisk.asyncs;
import android.content.Context;
import com.topjohnwu.magisk.MagiskManager;
import java.util.List;
public class MagiskHide extends ParallelTask<Object, Void, Void> {
private boolean isList = false;
public MagiskHide(Context context) {
super(context);
}
@Override
protected Void doInBackground(Object... params) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return null;
String command = (String) params[0];
List<String> ret = magiskManager.shell.su("magiskhide --" + command);
if (isList) {
magiskManager.magiskHideList = ret;
}
return null;
}
@Override
protected void onPostExecute(Void v) {
MagiskManager magiskManager = getMagiskManager();
if (magiskManager == null) return;
if (isList) {
magiskManager.magiskHideDone.trigger();
}
super.onPostExecute(v);
}
public void add(CharSequence packageName) {
exec("add " + packageName);
}
public void rm(CharSequence packageName) {
exec("rm " + packageName);
}
public void enable() {
exec("enable");
}
public void disable() {
exec("disable");
}
public void list() {
isList = true;
if (getMagiskManager() == null) return;
exec("ls");
}
}

View File

@ -226,4 +226,24 @@ public class Utils {
(NotificationManager) magiskManager.getSystemService(Context.NOTIFICATION_SERVICE); (NotificationManager) magiskManager.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(APK_UPDATE_NOTIFICATION_ID, builder.build()); notificationManager.notify(APK_UPDATE_NOTIFICATION_ID, builder.build());
} }
public static void enableMagiskHide(Shell shell) {
shell.su_raw("magiskhide --enable");
}
public static void disableMagiskHide(Shell shell) {
shell.su_raw("magiskhide --disable");
}
public static List<String> listMagiskHide(Shell shell) {
return shell.su("magiskhide --ls");
}
public static void addMagiskHide(Shell shell, String pkg) {
shell.su_raw("magiskhide --add " + pkg);
}
public static void rmMagiskHide(Shell shell, String pkg) {
shell.su_raw("magiskhide --rm " + pkg);
}
} }