[app] Fix crash when module no longer exists (#536)

This commit is contained in:
tehcneko 2021-05-04 16:37:38 +08:00 committed by GitHub
parent 20c2f872ad
commit 11801e0145
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 28 deletions

View File

@ -94,9 +94,9 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
private final PackageManager pm;
private final SharedPreferences preferences;
private final Handler loadAppListHandler;
private final ModuleUtil moduleUtil;
private final String modulePackageName;
private final String moduleName;
private final ModuleUtil.InstalledModule module;
private final HashSet<ApplicationWithEquals> recommendedList = new HashSet<>();
private final HashSet<ApplicationWithEquals> checkedList = new HashSet<>();
@ -106,7 +106,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
private final SwitchBar.OnCheckedChangeListener switchBarOnCheckedChangeListener = new SwitchBar.OnCheckedChangeListener() {
@Override
public boolean onCheckedChanged(SwitchBar view, boolean isChecked) {
if (!ModuleUtil.getInstance().setModuleEnabled(modulePackageName, isChecked)) {
if (!moduleUtil.setModuleEnabled(module.packageName, isChecked)) {
return false;
}
enabled = isChecked;
@ -131,16 +131,15 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
private boolean refreshing = false;
private boolean enabled = true;
public ScopeAdapter(AppListActivity activity, String moduleName, String modulePackageName) {
public ScopeAdapter(AppListActivity activity, ModuleUtil.InstalledModule module) {
this.activity = activity;
this.moduleName = moduleName;
this.modulePackageName = modulePackageName;
this.module = module;
moduleUtil = ModuleUtil.getInstance();
HandlerThread handlerThread = new HandlerThread("appList");
handlerThread.start();
loadAppListHandler = new Handler(handlerThread.getLooper(), this);
preferences = App.getPreferences();
pm = activity.getPackageManager();
refresh(false);
}
@NonNull
@ -151,7 +150,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
}
private boolean shouldHideApp(PackageInfo info, ApplicationWithEquals app) {
if (info.packageName.equals(this.modulePackageName)) {
if (info.packageName.equals(this.module.packageName)) {
return true;
}
if (info.packageName.equals(BuildConfig.APPLICATION_ID)) {
@ -221,7 +220,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
private void checkRecommended() {
checkedList.clear();
checkedList.addAll(recommendedList);
ConfigManager.setModuleScope(modulePackageName, checkedList);
ConfigManager.setModuleScope(module.packageName, checkedList);
}
public boolean onOptionsItemSelected(MenuItem item) {
@ -251,7 +250,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
item.setChecked(!item.isChecked());
preferences.edit().putBoolean("filter_modules", item.isChecked()).apply();
} else if (itemId == R.id.menu_launch) {
Intent launchIntent = AppHelper.getSettingsIntent(modulePackageName, pm);
Intent launchIntent = AppHelper.getSettingsIntent(module.packageName, pm);
if (launchIntent != null) {
activity.startActivity(launchIntent);
} else {
@ -262,7 +261,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
Calendar now = Calendar.getInstance();
activity.backupLauncher.launch(String.format(Locale.US,
"%s_%04d%02d%02d_%02d%02d%02d.lsp",
moduleName,
module.getAppName(),
now.get(Calendar.YEAR), now.get(Calendar.MONTH) + 1,
now.get(Calendar.DAY_OF_MONTH), now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE), now.get(Calendar.SECOND)));
@ -320,11 +319,11 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
inflater.inflate(R.menu.menu_app_list, menu);
Intent intent = AppHelper.getSettingsIntent(modulePackageName, pm);
Intent intent = AppHelper.getSettingsIntent(module.packageName, pm);
if (intent == null) {
menu.removeItem(R.id.menu_launch);
}
List<String> scopeList = ModuleUtil.getInstance().getModule(modulePackageName).getScopeList();
List<String> scopeList = module.getScopeList();
if (scopeList == null || scopeList.isEmpty()) {
menu.removeItem(R.id.use_recommended);
}
@ -470,7 +469,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
activity.binding.progress.setIndeterminate(true);
activity.binding.progress.setVisibility(View.VISIBLE);
}
enabled = ModuleUtil.getInstance().isModuleEnabled(modulePackageName);
enabled = moduleUtil.isModuleEnabled(module.packageName);
activity.binding.masterSwitch.setOnCheckedChangeListener(null);
activity.binding.masterSwitch.setChecked(enabled);
activity.binding.masterSwitch.setOnCheckedChangeListener(switchBarOnCheckedChangeListener);
@ -483,7 +482,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
} else {
checkedList.remove(appInfo.application);
}
if (!ConfigManager.setModuleScope(modulePackageName, checkedList)) {
if (!ConfigManager.setModuleScope(module.packageName, checkedList)) {
activity.makeSnackBar(R.string.failed_to_save_scope_list, Snackbar.LENGTH_SHORT);
if (!isChecked) {
checkedList.add(appInfo.application);
@ -509,9 +508,9 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
recommendedList.clear();
searchList.clear();
checkedList.addAll(ConfigManager.getModuleScope(modulePackageName));
checkedList.addAll(ConfigManager.getModuleScope(module.packageName));
HashSet<ApplicationWithEquals> installedList = new HashSet<>();
List<String> scopeList = ModuleUtil.getInstance().getModule(modulePackageName).getScopeList();
List<String> scopeList = module.getScopeList();
boolean emptyCheckedList = checkedList.isEmpty();
for (PackageInfo info : appList) {
int uid = info.applicationInfo.uid;
@ -544,7 +543,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
}
checkedList.retainAll(installedList);
if (emptyCheckedList) {
ConfigManager.setModuleScope(modulePackageName, checkedList);
ConfigManager.setModuleScope(module.packageName, checkedList);
}
sortApps(searchList);
synchronized (dataReadyRunnable) {
@ -628,8 +627,8 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
builder.setPositiveButton(android.R.string.cancel, null);
}
builder.setNegativeButton(!recommendedList.isEmpty() ? android.R.string.cancel : android.R.string.ok, (dialog, which) -> {
ModuleUtil.getInstance().setModuleEnabled(modulePackageName, false);
Toast.makeText(activity, activity.getString(R.string.module_disabled_no_selection, moduleName), Toast.LENGTH_LONG).show();
moduleUtil.setModuleEnabled(module.packageName, false);
Toast.makeText(activity, activity.getString(R.string.module_disabled_no_selection, module.getAppName()), Toast.LENGTH_LONG).show();
activity.finish();
});
builder.show();

View File

@ -44,6 +44,7 @@ import org.lsposed.manager.databinding.ActivityAppListBinding;
import org.lsposed.manager.ui.activity.base.BaseActivity;
import org.lsposed.manager.util.BackupUtils;
import org.lsposed.manager.util.LinearLayoutManagerFix;
import org.lsposed.manager.util.ModuleUtil;
import rikka.recyclerview.RecyclerViewKt;
@ -60,18 +61,23 @@ public class AppListActivity extends BaseActivity {
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String modulePackageName = getIntent().getStringExtra("modulePackageName");
String moduleName = getIntent().getStringExtra("moduleName");
binding = ActivityAppListBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setAppBar(binding.appBar, binding.toolbar);
binding.appBar.setRaised(true);
binding.toolbar.setNavigationOnClickListener(view -> onBackPressed());
ModuleUtil.InstalledModule module = ModuleUtil.getInstance().getModule(modulePackageName);
if (module == null) {
finish();
return;
}
ActionBar bar = getSupportActionBar();
assert bar != null;
bar.setDisplayHomeAsUpEnabled(true);
bar.setTitle(moduleName);
bar.setSubtitle(modulePackageName);
scopeAdapter = new ScopeAdapter(this, moduleName, modulePackageName);
if (bar != null) {
bar.setDisplayHomeAsUpEnabled(true);
bar.setTitle(module.getAppName());
bar.setSubtitle(module.packageName);
}
scopeAdapter = new ScopeAdapter(this, module);
scopeAdapter.setHasStableIds(true);
binding.recyclerView.setAdapter(scopeAdapter);
binding.recyclerView.setHasFixedSize(true);
@ -149,6 +155,12 @@ public class AppListActivity extends BaseActivity {
});
}
@Override
protected void onResume() {
super.onResume();
scopeAdapter.refresh(false);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (scopeAdapter.onOptionsItemSelected(item)) {

View File

@ -278,7 +278,6 @@ public class ModulesActivity extends ListActivity implements ModuleUtil.ModuleLi
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(ModulesActivity.this, AppListActivity.class);
intent.putExtra("modulePackageName", item.packageName);
intent.putExtra("moduleName", item.getAppName());
startActivity(intent);
});

View File

@ -53,7 +53,6 @@ public final class NotificationUtil {
Intent intent = new Intent(context, AppListActivity.class)
.putExtra("modulePackageName", modulePackageName)
.putExtra("moduleName", moduleName)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, PENDING_INTENT_OPEN_APP_LIST, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);