XPOSED源码研究之XposedInstaller(5)ModulesFragment

来源:互联网 发布:网络舆情重要性 编辑:程序博客网 时间:2024/05/18 03:29
由于安装的模块是一个列表,ModulesFragment继承自ListFragment方便管理,ListFragment的使用可以参考:ListFragment 使用ListView and 自定义Adapter。

当有安装的模块被加载时,接受通知并做处理:
@Override public void onSingleInstalledModuleReloaded(ModuleUtil moduleUtil, String packageName, InstalledModule module) { getActivity().runOnUiThread(reloadModules); } @Override public void onInstalledModulesReloaded(ModuleUtil moduleUtil) { getActivity().runOnUiThread(reloadModules); }
对模块按名称进行排序:
private Runnable reloadModules = new Runnable() { public void run() { mAdapter.setNotifyOnChange(false); mAdapter.clear(); mAdapter.addAll(mModuleUtil.getModules().values()); final Collator col =Collator.getInstance(Locale.getDefault()); mAdapter.sort(newComparator<InstalledModule>() { @Override public int compare(InstalledModule lhs, InstalledModule rhs) { return col.compare(lhs.getAppName(), rhs.getAppName()); } }); mAdapter.notifyDataSetChanged(); } };
长按item项的菜单选择:
@Override public boolean onContextItemSelected(MenuItem item) { InstalledModule module = getItemFromContextMenuInfo(item.getMenuInfo()); if (module == nullreturnfalseswitch (item.getItemId()) { case R.id.menu_launch: startActivity(getSettingsIntent(module.packageName)); return truecaseR.id.menu_download_updates: Intent detailsIntent new Intent(getActivity(), DownloadDetailsActivity.class); detailsIntent.setData(Uri.fromParts("package", module.packageName, null)); startActivity(detailsIntent); return truecaseR.id.menu_support: NavUtil.startURL(getActivity(), RepoDb.getModuleSupport(module.packageName)); return truecase R.id.menu_play_store: Intent i new Intent(android.content.Intent.ACTION_VIEW); i.setData(Uri.parse(String.format(PLAY_STORE_LINK, module.packageName))); i.setPackage(PLAY_STORE_PACKAGE); try { startActivity(i); } catch(ActivityNotFoundException e) { i.setPackage(null); startActivity(i); } return true;case R.id.menu_app_info: startActivity(newIntent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.fromParts("package", module.packageName, null))); return truecaseR.id.menu_uninstall: startActivity(new Intent(Intent.ACTION_UNINSTALL_PACKAGE, Uri.fromParts("package", module.packageName, null))); return true; } return false; }
分别是:运行app,下载更新、获取支持、访问appstore,程序信息、卸载。
其中运行app功能,getSettingsIntent用以获取指定包名的启动Intent:
private Intent getSettingsIntent(String packageName) { // taken from ApplicationPackageManager.getLaunchIntentForPackage(String) // first looks for an Xposed-specific category, falls back to getLaunchIntentForPackage PackageManager pm =getActivity().getPackageManager(); Intent intentToResolve newIntent(Intent.ACTION_MAIN); intentToResolve.addCategory(SETTINGS_CATEGORY); intentToResolve.setPackage(packageName); List<ResolveInfo> ris = pm.queryIntentActivities(intentToResolve, 0); if (ris == null || ris.size() <= 0) {return pm.getLaunchIntentForPackage(packageName); } Intent intent newIntent(intentToResolve); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClassName(ris.get(0).activityInfo.packageName, ris.get(0).activityInfo.name); return intent; }
下载更新创建路径package:包名”启动DownloadDetailsActivity。
获取支持:通过包名查询出url然后打开访问:
public static void startURL(Context context, Uri uri) { Intent intent newIntent(Intent.ACTION_VIEW, uri); intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); if ("http".equals(uri.getScheme()) && "repo.xposed.info".equals(uri.getHost())) { Intent browser newIntent(Intent.ACTION_VIEW, EXAMPLE_URI); ComponentName browserApp =browser.resolveActivity(context.getPackageManager()); intent.setComponent(browserApp); } context.startActivity(intent); } public staticvoid startURL(Context context, String url) { startURL(context, parseURL(url)); }

访问appstore的方式直接访问https://play.google.com/store/apps/details?id=包名

程序信息直接通过打开系统设置来显示:
startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
     Uri.fromParts("package", module.packageName, null)));

卸载程序也是直接调用系统的卸载:
startActivity(new Intent(Intent.ACTION_UNINSTALL_PACKAGE,
     Uri.fromParts("package", module.packageName, null)));

0 0
原创粉丝点击