Commit c036f6d5 authored by topjohnwu's avatar topjohnwu

Cleanup Utils

parent 6f457c0c
...@@ -27,6 +27,7 @@ import com.topjohnwu.magisk.components.ExpandableView; ...@@ -27,6 +27,7 @@ import com.topjohnwu.magisk.components.ExpandableView;
import com.topjohnwu.magisk.components.Fragment; import com.topjohnwu.magisk.components.Fragment;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.ShowUI;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
...@@ -123,18 +124,18 @@ public class MagiskFragment extends Fragment ...@@ -123,18 +124,18 @@ public class MagiskFragment extends Fragment
// Show Manager update first // Show Manager update first
if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) { if (mm.remoteManagerVersionCode > BuildConfig.VERSION_CODE) {
Utils.showManagerInstallDialog(getActivity()); ShowUI.showManagerInstallDialog(getActivity());
return; return;
} }
((NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll(); ((NotificationManager) mm.getSystemService(Context.NOTIFICATION_SERVICE)).cancelAll();
Utils.showMagiskInstallDialog(this, ShowUI.showMagiskInstallDialog(this,
keepEncChkbox.isChecked(), keepVerityChkbox.isChecked()); keepEncChkbox.isChecked(), keepVerityChkbox.isChecked());
} }
@OnClick(R.id.uninstall_button) @OnClick(R.id.uninstall_button)
void uninstall() { void uninstall() {
Utils.showUninstallDialog(this); ShowUI.showUninstallDialog(this);
} }
@Nullable @Nullable
......
...@@ -17,6 +17,7 @@ import android.widget.Toast; ...@@ -17,6 +17,7 @@ import android.widget.Toast;
import com.topjohnwu.magisk.asyncs.CheckUpdates; import com.topjohnwu.magisk.asyncs.CheckUpdates;
import com.topjohnwu.magisk.asyncs.HideManager; import com.topjohnwu.magisk.asyncs.HideManager;
import com.topjohnwu.magisk.asyncs.UpdateRepos;
import com.topjohnwu.magisk.components.Activity; import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.utils.Shell; import com.topjohnwu.magisk.utils.Shell;
...@@ -118,7 +119,9 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { ...@@ -118,7 +119,9 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
} }
findPreference("clear").setOnPreferenceClickListener((pref) -> { findPreference("clear").setOnPreferenceClickListener((pref) -> {
Utils.clearRepoCache(getActivity()); mm.prefs.edit().remove(UpdateRepos.ETAG_KEY).apply();
mm.repoDB.clearRepo();
mm.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
return true; return true;
}); });
...@@ -204,9 +207,9 @@ public class SettingsActivity extends Activity implements Topic.Subscriber { ...@@ -204,9 +207,9 @@ public class SettingsActivity extends Activity implements Topic.Subscriber {
case "magiskhide": case "magiskhide":
enabled = prefs.getBoolean("magiskhide", false); enabled = prefs.getBoolean("magiskhide", false);
if (enabled) { if (enabled) {
Utils.enableMagiskHide(); Shell.su_raw("magiskhide --enable");
} else { } else {
Utils.disableMagiskHide(); Shell.su_raw("magiskhide --disable");
} }
break; break;
case "hosts": case "hosts":
......
...@@ -5,6 +5,7 @@ import android.content.pm.ApplicationInfo; ...@@ -5,6 +5,7 @@ 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;
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.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
...@@ -16,6 +17,7 @@ import android.widget.TextView; ...@@ -16,6 +17,7 @@ import android.widget.TextView;
import com.topjohnwu.magisk.R; import com.topjohnwu.magisk.R;
import com.topjohnwu.magisk.asyncs.ParallelTask; import com.topjohnwu.magisk.asyncs.ParallelTask;
import com.topjohnwu.magisk.components.SnackbarMaker; import com.topjohnwu.magisk.components.SnackbarMaker;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Topic; import com.topjohnwu.magisk.utils.Topic;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Utils;
...@@ -56,6 +58,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter. ...@@ -56,6 +58,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
new LoadApps().exec(); new LoadApps().exec();
} }
private boolean lowercaseContains(CharSequence string, CharSequence nonNullLowercaseSearch) {
return !TextUtils.isEmpty(string) && string.toString().toLowerCase().contains(nonNullLowercaseSearch);
}
@Override @Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false); View mView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_app, parent, false);
...@@ -86,10 +92,10 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter. ...@@ -86,10 +92,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) {
Utils.addMagiskHide(info.packageName); Shell.su_raw("magiskhide --add " + info.packageName);
mHideList.add(info.packageName); mHideList.add(info.packageName);
} else { } else {
Utils.rmMagiskHide(info.packageName); Shell.su_raw("magiskhide --rm " + info.packageName);
mHideList.remove(info.packageName); mHideList.remove(info.packageName);
} }
}); });
...@@ -132,8 +138,8 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter. ...@@ -132,8 +138,8 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
mList = new ArrayList<>(); mList = 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(pm), filter) if (lowercaseContains(info.loadLabel(pm), filter)
|| Utils.lowercaseContains(info.packageName, filter)) { || lowercaseContains(info.packageName, filter)) {
mList.add(info); mList.add(info);
} }
} }
...@@ -160,7 +166,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter. ...@@ -160,7 +166,7 @@ public class ApplicationAdapter extends RecyclerView.Adapter<ApplicationAdapter.
} }
Collections.sort(mOriginalList, (a, b) -> a.loadLabel(pm).toString().toLowerCase() Collections.sort(mOriginalList, (a, b) -> a.loadLabel(pm).toString().toLowerCase()
.compareTo(b.loadLabel(pm).toString().toLowerCase())); .compareTo(b.loadLabel(pm).toString().toLowerCase()));
mHideList = Utils.listMagiskHide(); mHideList = Shell.su("magiskhide --ls");
return null; return null;
} }
......
...@@ -4,7 +4,7 @@ import android.content.Context; ...@@ -4,7 +4,7 @@ import android.content.Context;
import com.topjohnwu.magisk.BuildConfig; import com.topjohnwu.magisk.BuildConfig;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.ShowUI;
import com.topjohnwu.magisk.utils.WebService; import com.topjohnwu.magisk.utils.WebService;
import org.json.JSONException; import org.json.JSONException;
...@@ -65,9 +65,9 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> { ...@@ -65,9 +65,9 @@ public class CheckUpdates extends ParallelTask<Void, Void, Void> {
if (mm == null) return; if (mm == null) return;
if (showNotification && mm.updateNotification) { if (showNotification && mm.updateNotification) {
if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) { if (BuildConfig.VERSION_CODE < mm.remoteManagerVersionCode) {
Utils.showManagerUpdateNotification(mm); ShowUI.showManagerUpdateNotification(mm);
} else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) { } else if (mm.magiskVersionCode < mm.remoteMagiskVersionCode) {
Utils.showMagiskUpdateNotification(mm); ShowUI.showMagiskUpdateNotification(mm);
} }
} }
mm.updateCheckDone.publish(); mm.updateCheckDone.publish();
......
...@@ -14,6 +14,7 @@ import com.topjohnwu.magisk.utils.Utils; ...@@ -14,6 +14,7 @@ import com.topjohnwu.magisk.utils.Utils;
import com.topjohnwu.magisk.utils.ZipUtils; import com.topjohnwu.magisk.utils.ZipUtils;
import java.io.File; import java.io.File;
import java.security.SecureRandom;
import java.util.List; import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
...@@ -27,6 +28,27 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> { ...@@ -27,6 +28,27 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
super(context); super(context);
} }
private String genPackageName(String prefix, int length) {
StringBuilder builder = new StringBuilder(length);
builder.append(prefix);
length -= prefix.length();
SecureRandom random = new SecureRandom();
String base = "abcdefghijklmnopqrstuvwxyz";
String alpha = base + base.toUpperCase();
String full = alpha + "0123456789..........";
char next, prev = '\0';
for (int i = 0; i < length; ++i) {
if (prev == '.' || i == length - 1 || i == 0) {
next = alpha.charAt(random.nextInt(alpha.length()));
} else {
next = full.charAt(random.nextInt(full.length()));
}
builder.append(next);
prev = next;
}
return builder.toString();
}
@Override @Override
protected void onPreExecute() { protected void onPreExecute() {
getMagiskManager().toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT); getMagiskManager().toast(R.string.hide_manager_toast, Toast.LENGTH_SHORT);
...@@ -67,7 +89,7 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> { ...@@ -67,7 +89,7 @@ public class HideManager extends ParallelTask<Void, Void, Boolean> {
return false; return false;
// Patch binary XML with new package name // Patch binary XML with new package name
pkg = Utils.genPackageName("com.", UNHIDE_PKG_NAME.length - 1); pkg = genPackageName("com.", UNHIDE_PKG_NAME.length - 1);
System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length()); System.arraycopy(pkg.getBytes(), 0, xml, offset, pkg.length());
asset.getOutputStream(je).write(xml); asset.getOutputStream(je).write(xml);
......
...@@ -5,7 +5,9 @@ import android.content.Context; ...@@ -5,7 +5,9 @@ import android.content.Context;
import com.topjohnwu.magisk.MagiskManager; import com.topjohnwu.magisk.MagiskManager;
import com.topjohnwu.magisk.container.Module; import com.topjohnwu.magisk.container.Module;
import com.topjohnwu.magisk.container.ValueSortedMap; import com.topjohnwu.magisk.container.ValueSortedMap;
import com.topjohnwu.magisk.utils.Utils; import com.topjohnwu.magisk.utils.Shell;
import java.util.List;
public class LoadModules extends ParallelTask<Void, Void, Void> { public class LoadModules extends ParallelTask<Void, Void, Void> {
...@@ -13,13 +15,18 @@ public class LoadModules extends ParallelTask<Void, Void, Void> { ...@@ -13,13 +15,18 @@ public class LoadModules extends ParallelTask<Void, Void, Void> {
super(context); super(context);
} }
private List<String> getModList() {
String command = "ls -d " + MagiskManager.MAGISK_PATH + "/* | grep -v lost+found";
return Shell.su(command);
}
@Override @Override
protected Void doInBackground(Void... voids) { protected Void doInBackground(Void... voids) {
MagiskManager mm = getMagiskManager(); MagiskManager mm = getMagiskManager();
if (mm == null) return null; if (mm == null) return null;
mm.moduleMap = new ValueSortedMap<>(); mm.moduleMap = new ValueSortedMap<>();
for (String path : Utils.getModList(MagiskManager.MAGISK_PATH)) { for (String path : getModList()) {
Module module = new Module(path); Module module = new Module(path);
mm.moduleMap.put(module.getId(), module); mm.moduleMap.put(module.getId(), module);
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment