[app] Only allow setting modules' scope when enabled (#1455)

This commit is contained in:
LoveSy 2021-11-29 19:43:40 +08:00 committed by GitHub
parent ed4d230821
commit 62667a4e05
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 21 deletions

View File

@ -109,6 +109,10 @@ public class ScopeAdapter extends EmptyStateRecyclerView.EmptyStateAdapter<Scope
if (!moduleUtil.setModuleEnabled(module.packageName, isChecked)) { if (!moduleUtil.setModuleEnabled(module.packageName, isChecked)) {
return false; return false;
} }
var tmpChkList = new HashSet<>(checkedList);
if (isChecked && !tmpChkList.isEmpty() && !ConfigManager.setModuleScope(module.packageName, tmpChkList)) {
return false;
}
enabled = isChecked; enabled = isChecked;
notifyDataSetChanged(); notifyDataSetChanged();
return true; return true;
@ -200,6 +204,10 @@ public class ScopeAdapter extends EmptyStateRecyclerView.EmptyStateAdapter<Scope
} }
private void checkRecommended() { private void checkRecommended() {
if (!fragment.binding.masterSwitch.isChecked()) {
fragment.showHint(R.string.module_is_not_activated_yet, false);
return;
}
fragment.runAsync(() -> { fragment.runAsync(() -> {
var tmpChkList = new HashSet<>(checkedList); var tmpChkList = new HashSet<>(checkedList);
tmpChkList.removeIf(i -> i.userId == module.userId); tmpChkList.removeIf(i -> i.userId == module.userId);
@ -514,9 +522,6 @@ public class ScopeAdapter extends EmptyStateRecyclerView.EmptyStateAdapter<Scope
} }
}); });
tmpChkList.retainAll(installedList); tmpChkList.retainAll(installedList);
if (emptyCheckedList) {
ConfigManager.setModuleScope(module.packageName, tmpChkList);
}
checkedList = tmpChkList; checkedList = tmpChkList;
recommendedList = tmpRecList; recommendedList = tmpRecList;
searchList = tmpList.parallelStream().sorted(this::sortApps).collect(Collectors.toList()); searchList = tmpList.parallelStream().sorted(this::sortApps).collect(Collectors.toList());

View File

@ -96,7 +96,9 @@ public class BackupUtils {
} }
ModuleUtil.InstalledModule module = ModuleUtil.getInstance().getModule(name); ModuleUtil.InstalledModule module = ModuleUtil.getInstance().getModule(name);
if (module != null) { if (module != null) {
ModuleUtil.getInstance().setModuleEnabled(name, moduleObject.getBoolean("enable")); var enabled = moduleObject.getBoolean("enable");
ModuleUtil.getInstance().setModuleEnabled(name, enabled);
if (!enabled) continue;
JSONArray scopeArray = moduleObject.getJSONArray("scope"); JSONArray scopeArray = moduleObject.getJSONArray("scope");
HashSet<ScopeAdapter.ApplicationWithEquals> scope = new HashSet<>(); HashSet<ScopeAdapter.ApplicationWithEquals> scope = new HashSet<>();
for (int j = 0; j < scopeArray.length(); j++) { for (int j = 0; j < scopeArray.length(); j++) {

View File

@ -133,7 +133,7 @@
<string name="menu_show_games">Games</string> <string name="menu_show_games">Games</string>
<string name="menu_show_modules">Modules</string> <string name="menu_show_modules">Modules</string>
<string name="menu_show_denylist">Denylist</string> <string name="menu_show_denylist">Denylist</string>
<string name="failed_to_save_scope_list">Failed save scope list</string> <string name="failed_to_save_scope_list">Failed to save scope list</string>
<string name="app_description">%1$s\nVersion %2$s</string> <string name="app_description">%1$s\nVersion %2$s</string>
<string name="use_recommended">Recommended</string> <string name="use_recommended">Recommended</string>
<string name="no_scope_selected_has_recommended">You did not select any app. Select recommended apps?</string> <string name="no_scope_selected_has_recommended">You did not select any app. Select recommended apps?</string>

View File

@ -711,21 +711,21 @@ public class ConfigManager {
} }
} }
public boolean setModuleScope(String packageName, List<Application> scopes) { public boolean setModuleScope(String packageName, List<Application> scopes) throws RemoteException {
if (scopes == null) return false; if (scopes == null) return false;
enableModule(packageName);
int mid = getModuleId(packageName); int mid = getModuleId(packageName);
if (mid == -1) return false; if (mid == -1) return false;
Application self = new Application(); Application self = new Application();
self.packageName = packageName; self.packageName = packageName;
self.userId = 0; self.userId = 0;
scopes.add(self); scopes.add(self);
int finalMid = mid;
executeInTransaction(() -> { executeInTransaction(() -> {
db.delete("scope", "mid = ?", new String[]{String.valueOf(finalMid)}); db.delete("scope", "mid = ?", new String[]{String.valueOf(mid)});
for (Application app : scopes) { for (Application app : scopes) {
if (app.packageName.equals("android") && app.userId != 0) continue; if (app.packageName.equals("android") && app.userId != 0) continue;
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put("mid", finalMid); values.put("mid", mid);
values.put("app_pkg_name", app.packageName); values.put("app_pkg_name", app.packageName);
values.put("user_id", app.userId); values.put("user_id", app.userId);
db.insertWithOnConflict("scope", null, values, SQLiteDatabase.CONFLICT_IGNORE); db.insertWithOnConflict("scope", null, values, SQLiteDatabase.CONFLICT_IGNORE);
@ -810,8 +810,12 @@ public class ConfigManager {
} }
} }
public boolean enableModule(String packageName, ApplicationInfo info) { public boolean enableModule(String packageName) throws RemoteException {
if (packageName.equals("lspd") || !updateModuleApkPath(packageName, getModuleApkPath(info), false)) PackageInfo pkgInfo = PackageService.getPackageInfo(packageName, PackageService.MATCH_ALL_FLAGS, 0);
if (pkgInfo == null || pkgInfo.applicationInfo == null) {
return false;
}
if (packageName.equals("lspd") || !updateModuleApkPath(packageName, getModuleApkPath(pkgInfo.applicationInfo), false))
return false; return false;
boolean changed = executeInTransaction(() -> { boolean changed = executeInTransaction(() -> {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();

View File

@ -521,16 +521,11 @@ public class LSPManagerService extends ILSPManagerService.Stub {
@Override @Override
public boolean enableModule(String packageName) throws RemoteException { public boolean enableModule(String packageName) throws RemoteException {
PackageInfo pkgInfo = PackageService.getPackageInfo(packageName, PackageService.MATCH_ALL_FLAGS, 0); return ConfigManager.getInstance().enableModule(packageName);
if (pkgInfo != null && pkgInfo.applicationInfo != null) {
return ConfigManager.getInstance().enableModule(packageName, pkgInfo.applicationInfo);
} else {
return false;
}
} }
@Override @Override
public boolean setModuleScope(String packageName, ParceledListSlice<Application> scope) { public boolean setModuleScope(String packageName, ParceledListSlice<Application> scope) throws RemoteException {
return ConfigManager.getInstance().setModuleScope(packageName, scope.getList()); return ConfigManager.getInstance().setModuleScope(packageName, scope.getList());
} }

View File

@ -115,9 +115,9 @@ public class LSPosedService extends ILSPosedService.Stub {
} }
if (isXposedModule) { if (isXposedModule) {
broadcastOrShowNotification(moduleName, userId, intentAction); broadcastOrShowNotification(moduleName, userId, intentAction);
// When installing a new Xposed module, we update the apk path to prepare for // When installing a new Xposed module, we update the apk path to mark it as a
// the first activation of a module with a recommended scope or for a user to // module to send a broadcast when modules that have not been activated are
// uninstall a module that has not been activated before. // uninstalled.
ConfigManager.getInstance().updateModuleApkPath(moduleName, ConfigManager.getInstance().getModuleApkPath(applicationInfo), true); ConfigManager.getInstance().updateModuleApkPath(moduleName, ConfigManager.getInstance().getModuleApkPath(applicationInfo), true);
// when package is changed, we may need to update cache (module cache or process cache) // when package is changed, we may need to update cache (module cache or process cache)
ConfigManager.getInstance().updateCache(); ConfigManager.getInstance().updateCache();