[app] Fix opening modules and apps for other users (#584)
This commit is contained in:
parent
fe17834fa3
commit
64efa50685
|
|
@ -20,12 +20,14 @@
|
|||
|
||||
package org.lsposed.manager.adapters;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.UserHandle;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import org.lsposed.manager.ConfigManager;
|
||||
|
|
@ -49,6 +51,16 @@ public class AppHelper {
|
|||
return getIntentForCategory(packageName, userId, packageManager, Intent.CATEGORY_LAUNCHER);
|
||||
}
|
||||
|
||||
public static void startActivityAsUser(Activity activity, Intent intent, UserHandle user) {
|
||||
try {
|
||||
//noinspection JavaReflectionMemberAccess
|
||||
var startActivityAsUserMethod = Activity.class.getMethod("startActivityAsUser", Intent.class, UserHandle.class);
|
||||
startActivityAsUserMethod.invoke(activity, intent, user);
|
||||
} catch (Throwable t) {
|
||||
t.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Intent getIntentForCategory(String packageName, int userId, PackageManager packageManager, String category) {
|
||||
Intent intentToResolve = new Intent(Intent.ACTION_MAIN);
|
||||
intentToResolve.addCategory(category);
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ import android.os.Build;
|
|||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Message;
|
||||
import android.os.UserHandle;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.TextUtils;
|
||||
|
|
@ -98,6 +99,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
private final ModuleUtil moduleUtil;
|
||||
|
||||
private final ModuleUtil.InstalledModule module;
|
||||
private final UserHandle userHandle;
|
||||
|
||||
private final HashSet<ApplicationWithEquals> recommendedList = new HashSet<>();
|
||||
private final HashSet<ApplicationWithEquals> checkedList = new HashSet<>();
|
||||
|
|
@ -132,9 +134,10 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
private boolean refreshing = false;
|
||||
private boolean enabled = true;
|
||||
|
||||
public ScopeAdapter(AppListActivity activity, ModuleUtil.InstalledModule module) {
|
||||
public ScopeAdapter(AppListActivity activity, ModuleUtil.InstalledModule module, UserHandle userHandle) {
|
||||
this.activity = activity;
|
||||
this.module = module;
|
||||
this.userHandle = userHandle;
|
||||
moduleUtil = ModuleUtil.getInstance();
|
||||
HandlerThread handlerThread = new HandlerThread("appList");
|
||||
handlerThread.start();
|
||||
|
|
@ -256,7 +259,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
} else if (itemId == R.id.menu_launch) {
|
||||
Intent launchIntent = AppHelper.getSettingsIntent(module.packageName, module.userId, pm);
|
||||
if (launchIntent != null) {
|
||||
activity.startActivity(launchIntent);
|
||||
AppHelper.startActivityAsUser(activity, launchIntent, userHandle);
|
||||
} else {
|
||||
activity.makeSnackBar(R.string.module_no_ui, Snackbar.LENGTH_LONG);
|
||||
}
|
||||
|
|
@ -289,7 +292,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
if (itemId == R.id.menu_launch) {
|
||||
Intent launchIntent = pm.getLaunchIntentForPackage(info.packageName);
|
||||
if (launchIntent != null) {
|
||||
activity.startActivity(launchIntent);
|
||||
AppHelper.startActivityAsUser(activity, launchIntent, userHandle);
|
||||
}
|
||||
} else if (itemId == R.id.menu_compile_speed) {
|
||||
CompileDialogFragment.speed(activity.getSupportFragmentManager(), info);
|
||||
|
|
@ -303,7 +306,7 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
e.printStackTrace();
|
||||
}
|
||||
} else if (itemId == R.id.menu_app_info) {
|
||||
activity.startActivity(new Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", info.packageName, null)));
|
||||
AppHelper.startActivityAsUser(activity, new Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", info.packageName, null)), userHandle);
|
||||
} else if (itemId == R.id.menu_force_stop) {
|
||||
if (info.packageName.equals("android")) {
|
||||
ConfigManager.reboot(false, null, false);
|
||||
|
|
@ -412,14 +415,10 @@ public class ScopeAdapter extends RecyclerView.Adapter<ScopeAdapter.ViewHolder>
|
|||
holder.itemView.setOnCreateContextMenuListener((menu, v, menuInfo) -> {
|
||||
activity.getMenuInflater().inflate(R.menu.menu_app_item, menu);
|
||||
menu.setHeaderTitle(appName);
|
||||
Intent launchIntent = pm.getLaunchIntentForPackage(appInfo.packageName);
|
||||
Intent launchIntent = AppHelper.getIntentForCategory(appInfo.packageName, userId, pm, Intent.CATEGORY_LAUNCHER);
|
||||
if (launchIntent == null) {
|
||||
menu.removeItem(R.id.menu_launch);
|
||||
}
|
||||
if (userId != 0) {
|
||||
menu.removeItem(R.id.menu_launch);
|
||||
menu.removeItem(R.id.menu_app_info);
|
||||
}
|
||||
if (android) {
|
||||
menu.findItem(R.id.menu_force_stop).setTitle(R.string.reboot);
|
||||
menu.removeItem(R.id.menu_compile_speed);
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ package org.lsposed.manager.ui.activity;
|
|||
import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.UserHandle;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
|
||||
|
|
@ -46,6 +47,8 @@ import org.lsposed.manager.util.BackupUtils;
|
|||
import org.lsposed.manager.util.LinearLayoutManagerFix;
|
||||
import org.lsposed.manager.util.ModuleUtil;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import rikka.recyclerview.RecyclerViewKt;
|
||||
|
||||
public class AppListActivity extends BaseActivity {
|
||||
|
|
@ -62,6 +65,7 @@ public class AppListActivity extends BaseActivity {
|
|||
super.onCreate(savedInstanceState);
|
||||
String modulePackageName = getIntent().getStringExtra("modulePackageName");
|
||||
int moduleUserId = getIntent().getIntExtra("moduleUserId", -1);
|
||||
UserHandle userHandle = getIntent().getParcelableExtra("userHandle");
|
||||
binding = ActivityAppListBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
setAppBar(binding.appBar, binding.toolbar);
|
||||
|
|
@ -75,10 +79,14 @@ public class AppListActivity extends BaseActivity {
|
|||
ActionBar bar = getSupportActionBar();
|
||||
if (bar != null) {
|
||||
bar.setDisplayHomeAsUpEnabled(true);
|
||||
bar.setTitle(module.getAppName());
|
||||
if (module.userId != 0) {
|
||||
bar.setTitle(String.format(Locale.US, "%s (%d)", module.getAppName(), module.userId));
|
||||
} else {
|
||||
bar.setTitle(module.getAppName());
|
||||
}
|
||||
bar.setSubtitle(module.packageName);
|
||||
}
|
||||
scopeAdapter = new ScopeAdapter(this, module);
|
||||
scopeAdapter = new ScopeAdapter(this, module, userHandle);
|
||||
scopeAdapter.setHasStableIds(true);
|
||||
binding.recyclerView.setAdapter(scopeAdapter);
|
||||
binding.recyclerView.setHasFixedSize(true);
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@ import android.os.Build;
|
|||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.UserHandle;
|
||||
import android.os.UserManager;
|
||||
import android.text.Spannable;
|
||||
import android.text.SpannableStringBuilder;
|
||||
import android.text.TextUtils;
|
||||
|
|
@ -79,6 +81,7 @@ import org.lsposed.manager.util.ModuleUtil;
|
|||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
|
@ -99,6 +102,8 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
private PackageManager pm;
|
||||
private ModuleUtil moduleUtil;
|
||||
private ModuleUtil.InstalledModule selectedModule;
|
||||
private UserHandle selectedModuleUser;
|
||||
private UserManager userManager;
|
||||
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
|
|
@ -108,6 +113,7 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
moduleUtil = ModuleUtil.getInstance();
|
||||
pm = getPackageManager();
|
||||
moduleUtil.addListener(this);
|
||||
userManager = getSystemService(UserManager.class);
|
||||
super.onCreate(savedInstanceState);
|
||||
binding = ActivityModuleDetailBinding.inflate(getLayoutInflater());
|
||||
setContentView(binding.getRoot());
|
||||
|
|
@ -171,15 +177,20 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
int[] users = ConfigManager.getUsers();
|
||||
if (users != null) {
|
||||
if (users.length != adapters.size()) {
|
||||
int[] userIds = ConfigManager.getUsers();
|
||||
if (userIds != null) {
|
||||
List<UserHandle> users = userManager.getUserProfiles();
|
||||
HashMap<Integer, UserHandle> handles = new HashMap<>();
|
||||
for (UserHandle handle : users) {
|
||||
handles.put(handle.hashCode(), handle);
|
||||
}
|
||||
if (userIds.length != adapters.size()) {
|
||||
adapters.clear();
|
||||
if (users.length != 1) {
|
||||
if (users.size() != 1) {
|
||||
binding.viewPager.setUserInputEnabled(true);
|
||||
ArrayList<String> titles = new ArrayList<>();
|
||||
for (int userId : users) {
|
||||
var adapter = new ModuleAdapter(userId);
|
||||
for (int userId : userIds) {
|
||||
var adapter = new ModuleAdapter(userId, handles.get(userId));
|
||||
adapter.setHasStableIds(true);
|
||||
adapters.add(adapter);
|
||||
titles.add(getString(R.string.user_title, userId));
|
||||
|
|
@ -188,7 +199,7 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
binding.tabLayout.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
binding.viewPager.setUserInputEnabled(false);
|
||||
var adapter = new ModuleAdapter(0);
|
||||
var adapter = new ModuleAdapter(0, users.get(0));
|
||||
adapter.setHasStableIds(true);
|
||||
adapters.add(adapter);
|
||||
binding.tabLayout.setVisibility(View.GONE);
|
||||
|
|
@ -243,7 +254,7 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
}
|
||||
Intent intent = AppHelper.getSettingsIntent(packageName, module.userId, pm);
|
||||
if (intent != null) {
|
||||
startActivity(intent);
|
||||
AppHelper.startActivityAsUser(this, intent, selectedModuleUser);
|
||||
} else {
|
||||
Snackbar.make(binding.snackbar, R.string.module_no_ui, Snackbar.LENGTH_LONG).show();
|
||||
}
|
||||
|
|
@ -259,7 +270,7 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
}
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_app_info) {
|
||||
startActivity(new Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", module.packageName, null)));
|
||||
AppHelper.startActivityAsUser(this, (new Intent(ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", module.packageName, null))), selectedModuleUser);
|
||||
return true;
|
||||
} else if (itemId == R.id.menu_uninstall) {
|
||||
new AlertDialog.Builder(this)
|
||||
|
|
@ -332,10 +343,12 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
private final List<ModuleUtil.InstalledModule> searchList = new ArrayList<>();
|
||||
private final List<ModuleUtil.InstalledModule> showList = new ArrayList<>();
|
||||
private final int userId;
|
||||
private final UserHandle userHandle;
|
||||
private boolean isLoaded;
|
||||
|
||||
ModuleAdapter(int userId) {
|
||||
ModuleAdapter(int userId, UserHandle userHandle) {
|
||||
this.userId = userId;
|
||||
this.userHandle = userHandle;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
|
|
@ -418,11 +431,13 @@ public class ModulesActivity extends BaseActivity implements ModuleUtil.ModuleLi
|
|||
Intent intent = new Intent(ModulesActivity.this, AppListActivity.class);
|
||||
intent.putExtra("modulePackageName", item.packageName);
|
||||
intent.putExtra("moduleUserId", item.userId);
|
||||
intent.putExtra("userHandle", userHandle);
|
||||
startActivity(intent);
|
||||
});
|
||||
|
||||
holder.itemView.setOnLongClickListener(v -> {
|
||||
selectedModule = item;
|
||||
selectedModuleUser = userHandle;
|
||||
return false;
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue