高通Android 11/12/13 通过包名设置默认launcher

背景:最近在封装供第三应用系统SDK 接口,遇到一个无法通过包名设置主launcher代码坑所以记录下。
 

涉及类roles.xml # <!---~ @see com.android.settings.applications.defaultapps.DefaultHomePreferenceController~ @see com.android.settings.applications.defaultapps.DefaultHomePicker~ @see com.android.server.pm.PackageManagerService#setHomeActivity(ComponentName, int)-->DeaultAppActivity.java#onCreateDefaultAppChildFragment.java # onRoleChanged # addPreferenceAutoDefaultAppListFragment#onActivityCreatedManageRoleHolderStateLiveData.java #setRoleHolderAsUserHandheldDefaultAppFragment.java(packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/role/ui/handheld)#newInstanceRoleManager.java #addRoleHolderAsUserIRoleManager.aidl #addRoleHolderAsUserRole.java  #getDefaultHolders TwoTargetPreference.java #OnSecondTargetClickListenerPackageManagerShellCommand.java#runSetHomeActivityResolverActivity.java # onCreateParseActivityUtils #private static ParseResult<ParsedActivity> parseActivityOrAlias(ParsedActivity activity,ParsingPackage pkg, String tag, XmlResourceParser parser, Resources resources,TypedArray array, boolean isReceiver, boolean isAlias, boolean visibleToEphemeral,ParseInput input, int parentActivityNameAttr, int permissionAttr,int exportedAttr)

1、一开始我是这样写的,代码如下图所示。

private void setDefaultLauncher3(Context context,String packageName,String className) {try {PackageManager pm = getPackageManager();Log.i("deflauncher", "deflauncher : PackageName = " + packageName + " ClassName = " + className);IntentFilter filter = new IntentFilter();filter.addAction("android.intent.action.MAIN");filter.addCategory("android.intent.category.HOME");filter.addCategory("android.intent.category.DEFAULT");Intent intent = new Intent(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME);List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);final int N = list.size();ComponentName[] set = new ComponentName[N];int bestMatch = 0;for (int i = 0; i < N; i++) {ResolveInfo r = list.get(i);set[i] = new ComponentName(r.activityInfo.packageName,r.activityInfo.name);if (r.match > bestMatch) bestMatch = r.match;}ComponentName preActivity = new ComponentName(packageName, className);pm.addPreferredActivity(filter, bestMatch, set, preActivity);} catch (Exception e) {e.printStackTrace();}}

2、具体添加位置参考在frameworks/base/core/java/android/content/pm/parsing/component/ParsedActivityUtils.java

3、写死launcher包名主activity类名方法如下代码所示 app.olauncher.MainActivity

/*** This method shares parsing logic between Activity/Receiver/alias instances, but requires* passing in booleans for isReceiver/isAlias, since there's no indicator in the other* parameters.** They're used to filter the parsed tags and their behavior. This makes the method rather* messy, but it's more maintainable than writing 3 separate methods for essentially the same* type of logic.*/@NonNullprivate static ParseResult<ParsedActivity> parseActivityOrAlias(ParsedActivity activity,ParsingPackage pkg, String tag, XmlResourceParser parser, Resources resources,TypedArray array, boolean isReceiver, boolean isAlias, boolean visibleToEphemeral,ParseInput input, int parentActivityNameAttr, int permissionAttr,int exportedAttr) throws IOException, XmlPullParserException {String parentActivityName = array.getNonConfigurationString(parentActivityNameAttr, Configuration.NATIVE_CONFIG_VERSION);if (parentActivityName != null) {String packageName = pkg.getPackageName();String parentClassName = ParsingUtils.buildClassName(packageName, parentActivityName);if (parentClassName == null) {Log.e(TAG, "Activity " + activity.getName()+ " specified invalid parentActivityName " + parentActivityName);} else {activity.setParentActivity(parentClassName);}}String permission = array.getNonConfigurationString(permissionAttr, 0);if (isAlias) {// An alias will override permissions to allow referencing an Activity through its alias// without needing the original permission. If an alias needs the same permission,// it must be re-declared.activity.setPermission(permission);} else {activity.setPermission(permission != null ? permission : pkg.getPermission());}final boolean setExported = array.hasValue(exportedAttr);if (setExported) {activity.exported = array.getBoolean(exportedAttr, false);}final int depth = parser.getDepth();int type;while ((type = parser.next()) != XmlPullParser.END_DOCUMENT&& (type != XmlPullParser.END_TAG|| parser.getDepth() > depth)) {if (type != XmlPullParser.START_TAG) {continue;}final ParseResult result;if (parser.getName().equals("intent-filter")) {ParseResult<ParsedIntentInfo> intentResult = parseIntentFilter(pkg, activity,!isReceiver, visibleToEphemeral, resources, parser, input);if (intentResult.isSuccess()) {ParsedIntentInfo intent = intentResult.getResult();if (intent != null) {Log.e(TAG,"ZM activityName="+activity.getName());if("app.olauncher.MainActivity".equals(activity.getName())){intent.addCategory("android.intent.category.HOME");intent.addCategory("android.intent.category.DEFAULT");intent.setPriority(1000);}activity.order = Math.max(intent.getOrder(), activity.order);         activity.addIntent(intent);if (LOG_UNSAFE_BROADCASTS && isReceiver&& pkg.getTargetSdkVersion() >= Build.VERSION_CODES.O) {int actionCount = intent.countActions();for (int i = 0; i < actionCount; i++) {final String action = intent.getAction(i);if (action == null || !action.startsWith("android.")) {continue;}if (!SAFE_BROADCASTS.contains(action)) {Slog.w(TAG,"Broadcast " + action + " may never be delivered to "+ pkg.getPackageName() + " as requested at: "+ parser.getPositionDescription());}}}}}result = intentResult;} else if (parser.getName().equals("meta-data")) {result = ParsedComponentUtils.addMetaData(activity, pkg, resources, parser, input);} else if (parser.getName().equals("property")) {result = ParsedComponentUtils.addProperty(activity, pkg, resources, parser, input);} else if (!isReceiver && !isAlias && parser.getName().equals("preferred")) {ParseResult<ParsedIntentInfo> intentResult = parseIntentFilter(pkg, activity,true /*allowImplicitEphemeralVisibility*/, visibleToEphemeral,resources, parser, input);if (intentResult.isSuccess()) {ParsedIntentInfo intent = intentResult.getResult();if (intent != null) {pkg.addPreferredActivityFilter(activity.getClassName(), intent);}}result = intentResult;} else if (!isReceiver && !isAlias && parser.getName().equals("layout")) {ParseResult<ActivityInfo.WindowLayout> layoutResult =parseActivityWindowLayout(resources, parser, input);if (layoutResult.isSuccess()) {activity.windowLayout = layoutResult.getResult();}result = layoutResult;} else {result = ParsingUtils.unknownTag(tag, pkg, parser, input);}if (result.isError()) {return input.error(result);}}if (!isAlias && activity.launchMode != LAUNCH_SINGLE_INSTANCE_PER_TASK&& activity.metaData != null && activity.metaData.containsKey(ParsingPackageUtils.METADATA_ACTIVITY_LAUNCH_MODE)) {final String launchMode = activity.metaData.getString(ParsingPackageUtils.METADATA_ACTIVITY_LAUNCH_MODE);if (launchMode != null && launchMode.equals("singleInstancePerTask")) {activity.launchMode = LAUNCH_SINGLE_INSTANCE_PER_TASK;}}ParseResult<ActivityInfo.WindowLayout> layoutResult =resolveActivityWindowLayout(activity, input);if (layoutResult.isError()) {return input.error(layoutResult);}activity.windowLayout = layoutResult.getResult();if (!setExported) {boolean hasIntentFilters = activity.getIntents().size() > 0;if (hasIntentFilters) {final ParseResult exportedCheckResult = input.deferError(activity.getName() + ": Targeting S+ (version " + Build.VERSION_CODES.S+ " and above) requires that an explicit value for android:exported be"+ " defined when intent filters are present",DeferredError.MISSING_EXPORTED_FLAG);if (exportedCheckResult.isError()) {return input.error(exportedCheckResult);}}activity.exported = hasIntentFilters;}return input.success(activity);}

4、在ResolverActivity.java 中onCreate方法中 执行以下代码,代码路径 /frameworks/base/core/java/com/android/internal/app/ResolverActivity.java

protected void onCreate(Bundle savedInstanceState, Intent intent,CharSequence title, int defaultTitleRes, Intent[] initialIntents,List<ResolveInfo> rList, boolean supportsAlwaysUseOption) {setTheme(appliedThemeResId());super.onCreate(savedInstanceState);if (mResolvingHome) {setDefaultLauncher3();finish();return;}

5、灵活一点如果动态设置launcher流程又不一样,下图是Setttings默认主屏幕应用  launcher列表选项(这个界面radiobutton控件通过preference动态添加 这个addPreference(preference):

6、点击事件位置代码路径packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/role/ui/DefaultAppChildFragment.java

 private void addPreference(@NonNull String key, @NonNull Drawable icon,@NonNull CharSequence title, boolean checked, @Nullable ApplicationInfo applicationInfo,@NonNull ArrayMap<String, Preference> oldPreferences,@NonNull PreferenceScreen preferenceScreen, @NonNull Context context) {TwoStatePreference preference = (TwoStatePreference) oldPreferences.get(key);if (preference == null) {preference = requirePreferenceFragment().createApplicationPreference(context);preference.setKey(key);preference.setIcon(icon);preference.setTitle(title);preference.setPersistent(false);preference.setOnPreferenceChangeListener((preference2, newValue) -> false);preference.setOnPreferenceClickListener(this);}Log.e("DefaultAppChildFragment","addPreference");preference.setChecked(checked);if (applicationInfo != null) {mRole.prepareApplicationPreferenceAsUser(preference, applicationInfo, mUser, context);}preferenceScreen.addPreference(preference);}

logcat日志

 DefaultAppChildFragment com.android.permissioncontroller     E  addPreference

7、另外一种通过指令去设置 adb shell pm set-home-activity  app.olauncher.debug (主launcher包名),验证过是没问题的。

8、实际调用还是通过RoleManager#addRoleHolderAsUser方法去添加为主Launcher

代码路径packages\modules\Permission\framework-s\java\android\app\role\RoleManager.java

  /*** Add a specific application to the holders of a role. If the role is exclusive, the previous* holder will be replaced.* <p>* <strong>Note:</strong> Using this API requires holding* {@code android.permission.MANAGE_ROLE_HOLDERS} and if the user id is not the current user* {@code android.permission.INTERACT_ACROSS_USERS_FULL}.** @param roleName the name of the role to add the role holder for* @param packageName the package name of the application to add to the role holders* @param flags optional behavior flags* @param user the user to add the role holder for* @param executor the {@code Executor} to run the callback on.* @param callback the callback for whether this call is successful** @see #getRoleHoldersAsUser(String, UserHandle)* @see #removeRoleHolderAsUser(String, String, int, UserHandle, Executor, Consumer)* @see #clearRoleHoldersAsUser(String, int, UserHandle, Executor, Consumer)** @hide*/@RequiresPermission(Manifest.permission.MANAGE_ROLE_HOLDERS)@SystemApipublic void addRoleHolderAsUser(@NonNull String roleName, @NonNull String packageName,@ManageHoldersFlags int flags, @NonNull UserHandle user,@CallbackExecutor @NonNull Executor executor, @NonNull Consumer<Boolean> callback) {Preconditions.checkStringNotEmpty(roleName, "roleName cannot be null or empty");Preconditions.checkStringNotEmpty(packageName, "packageName cannot be null or empty");Objects.requireNonNull(user, "user cannot be null");Objects.requireNonNull(executor, "executor cannot be null");Objects.requireNonNull(callback, "callback cannot be null");try {mService.addRoleHolderAsUser(roleName, packageName, flags, user.getIdentifier(),createRemoteCallback(executor, callback));} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

打印logcat日志如下所示

2024-05-14 01:18:43.314  1653-1653  RoleManager             pid-1653                             D  Package added as role holder, role: android.app.role.HOME, package: com.android.launcher3
2024-05-14 01:47:11.673  2854-23939 RoleContro...erviceImpl com.android.permissioncontroller     I  Package is already a role holder, package: com.android.launcher3, role: android.app.role.HOME
2024-05-14 01:47:11.674  1653-1653  RoleManager             pid-1653                             D  Package added as role holder, role: android.app.role.HOME, package: com.android.launcher32024-05-14 01:18:43.319  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=app.olauncher.debugtitle=Olauncher
2024-05-14 01:18:43.324  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=com.android.launcher3title=Quickstep
2024-05-14 01:18:43.332  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=app.olauncher.debugtitle=Olauncher
2024-05-14 01:18:43.338  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=com.android.launcher3title=Quickstep
2024-05-14 01:47:10.880  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=app.olauncher.debugtitle=Olauncher
2024-05-14 01:47:10.885  2854-2854  DefaultApp...ragment ZM com.android.permissioncontroller     E  key=com.android.launcher3title=Quickstep

9、代码路径 packages/modules/Permission/PermissionController/src/com/android/permissioncontroller/role/ui/ManageRoleHolderStateLiveData.java

10、代码路径frameworks\base\services\core\java\com/android\server\pm\PackageManagerShellCommand.java

private int runSetHomeActivity() {final PrintWriter pw = getOutPrintWriter();int userId = UserHandle.USER_SYSTEM;String opt;while ((opt = getNextOption()) != null) {switch (opt) {case "--user":userId = UserHandle.parseUserArg(getNextArgRequired());break;default:pw.println("Error: Unknown option: " + opt);return 1;}}String pkgName;String component = getNextArg();if (component.indexOf('/') < 0) {// No component specified, so assume it's just a package name.pkgName = component;} else {ComponentName componentName =component != null ? ComponentName.unflattenFromString(component) : null;if (componentName == null) {pw.println("Error: invalid component name");return 1;}pkgName = componentName.getPackageName();}final int translatedUserId =translateUserId(userId, UserHandle.USER_NULL, "runSetHomeActivity");final CompletableFuture<Boolean> future = new CompletableFuture<>();try {RoleManager roleManager = mContext.getSystemService(RoleManager.class);roleManager.addRoleHolderAsUser(RoleManager.ROLE_HOME, pkgName, 0,UserHandle.of(translatedUserId), FgThread.getExecutor(), future::complete);boolean success = future.get();if (success) {pw.println("Success");return 0;} else {pw.println("Error: Failed to set default home.");return 1;}} catch (Exception e) {pw.println(e.toString());return 1;}}

11、最后可以把这些代码添加自己自定义系统服务AIDL接口 ,然后在Android.bp中添加源码编译路径(不知道怎么添加AIDL源码编译路径看我之前这篇文章高通 Android 12 源码编译aidl接口_安卓12 怎么写aidl-CSDN博客)

12、在自己app应用调用通过 如下代码 进行设置即可(Process导入android.os包切记哈)

/*** 设置当前Launcher** @param packageName 传入第三方launcher包名*/public void setCurrentLauncher(String packageName) {setRoleHolderAsUser(RoleManager.ROLE_HOME, packageName, 0, Process.myUserHandle(), mContext);}

13、最后别忘记如果你是app调用代码的时候记得加系统签名哈 AndroidManifest.xml中 ,否则也不会生效。

 android:sharedUserId="android.uid.system"

到这里基本结束了,转载请注明出处高通Android 11/12/13 通过包名设置默认launcher-CSDN博客,谢谢!

感谢

Android R设置默认桌面_setroleholderasuser-CSDN博客

Android10.0(Q) 默认应用设置(电话、短信、浏览器、主屏幕应用)_android.app.role.browser-CSDN博客

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/702644.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

你了解黑龙江等保测评么?

等保测评的全称是信息安全等级保护测评&#xff0c;是信息安全等级保护工作中的一项重要内容。 具体来说&#xff0c;等保测评是指按照国家相关标准和技术规范&#xff0c;对信息系统安全等级保护状况进行检测评估的活动。 其主要目的是发现信息系统存在的安全隐患和不足&…

Leetcode—3146. 两个字符串的排列差【简单】

2024每日刷题&#xff08;135&#xff09; Leetcode—3146. 两个字符串的排列差 实现代码 class Solution { public:int findPermutationDifference(string s, string t) {int maps[26];int mapt[26];for(int i 0; i < s.size(); i) {int idxs s[i] - a;int idxt t[i] …

一部手机、一段视频,快速开展自动无人直播获取潜在客户

​​直播已经成为一种全新的营销方式。对于实体门店而言&#xff0c;直播具有吸引潜在客户、提升品牌知名度以及促进销售的巨大潜能。然而&#xff0c;很多门店因缺乏专业的直播设备和人员而无法轻松实现直播。为此&#xff0c;我们隆重介绍一款手机自动直播门店助手&#xff0…

机械手避障如何选择激光雷达?

在选择用于机械手避障的激光雷达时&#xff0c;应该考虑以下主要技术参数&#xff1a; 测量范围&#xff1a;激光雷达的测量范围决定了它能够检测到的最大距离。您需要根据机械手的应用场景和工作环境来选择合适的测量范围。 精度&#xff1a;精度是激光雷达测量结果的重要参数…

高效前端工程化:Monorepo、pnpm与Vue3集成实战指南

引言 在当今快速发展的前端开发领域&#xff0c;高效地管理和组织代码库成为提升开发效率的关键。随着项目规模的扩大&#xff0c;传统的单体仓库逐渐显露出局限性&#xff0c;而新兴的包管理工具如 PNPM、项目结构模式如 Monorepo 和 Turborepo 开始受到广泛关注。将教会大家…

数据生命周期管理:从提取到治理再到安全保障的全面策略

在大数据的时代背景下&#xff0c;数据已经成为企业运营不可或缺的资源。然而&#xff0c;数据的管理并非易事&#xff0c;特别是在数据的整个生命周期中——从数据的提取、治理到安全保障&#xff0c;每一个环节都至关重要。本文将探讨如何制定一个全面的数据生命周期管理策略…

java AOP环绕通知记录操作日志

一.创建数据库日志表 CREATE TABLE uc_system_log (id bigint(20) NOT NULL AUTO_INCREMENT COMMENT 主键ID,user_code varchar(64) DEFAULT NULL COMMENT 用户编码,user_name varchar(128) DEFAULT NULL COMMENT 用户名称,is_login tinyint(4) NOT NULL DEFAULT 0 COMMENT 是…

电脑录屏软件有哪些?这3款神器必须要知道

在当今现代社会&#xff0c;电脑录屏软件已经成为人们日常生活中不可或缺的一部分。无论是录制游戏精彩瞬间、制作教程、还是在线会议记录&#xff0c;一款好用的电脑录屏软件都能帮助我们更高效地完成任务。可是电脑录屏软件有哪些呢&#xff1f;接下来&#xff0c;我们将介绍…

通过windows远程桌面,远程连接CentOS系统

1.配置阿里云的YUM仓库 1.1 备份当前的YUM仓库配置文件 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup1.2 下载阿里云的CentOS仓库配置文件 对于CentOS 7&#xff1a; sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirr…

不懂数字后端Box List、Polygon的意思?

什么是BOX&#xff1f; 景芯SoC做design planning的第一步就是确定floorplan的box&#xff0c;也就是设计的区域。这个区域可以划分为三个边界&#xff0c;如下图所示&#xff1a; Die Box 最外面一圈&#xff0c;我们称为 Die Box&#xff0c;也就是用来放置 IO 单元&#x…

Matlab进阶绘图第54期—密度散点图(概率密度版)

在之前的文章中&#xff0c;分享过Matlab密度散点图的绘制方法&#xff1a; 此版内容用到了一些点云数据处理中求取密度的知识&#xff0c;对部分人来说&#xff0c;可能有些不好理解。 于是&#xff0c;本期内容使用Matlab自带的ksdensity函数进行密度散点图(概率密度版)的绘…

Mixtral

文章目录 一、关于 MixtralMistral AI、 La PlateformeMistral AI LLMs 二、Mistral AI API账户设置 三、Mixtral 说明通过稀疏架构推动开放模型的前沿表现Instructed 模型使用开源部署堆栈部署 Mixtral在我们的平台上使用 Mixtral。 一、关于 Mixtral 官网&#xff1a;https:…