首页 > 其他分享 >Settings里面切换不同Launcher的代码流程

Settings里面切换不同Launcher的代码流程

时间:2024-05-25 15:36:51浏览次数:31  
标签:java Settings Launcher packageName package role 切换 roleName android

1.Android\packages\modules\Permission\PermissionController中的DefaultAppActivity中接收,根据packagename进行追踪路径如下:
DefaultAppActivity.java 
--->HandheldDefaultAppFragment.java  
--->DefaultAppChildFragment.java:setDefaultApp()
--->ManageRoleHolderStateLiveData.java
--->RoleService.java addRoleHolderAsUser()
--->RoleControllerServiceImpl.java:onAddRoleHolder()
--->RoleManager.java:addRoleHolderFromController()
--->RoleService.java:addRoleHolderFromController()
--->RoleUserState.java:addRoleHolder():readFile()
--->RolesPersistenceImpl.java:ROLES_FILE_NAME = "roles.xml"

位于Android\packages\modules\Permission\PermissionController\res\xml\roles.xml
可以通过修改roles.xml,来修改默认应用,但是我需要的是动态的修改:


2.vendor/mediatek/proprietary/packages/apps/PermissionController/src/com/android/permissioncontroller/role/ui/ManageRoleHolderStateLiveData.java

通过setRoleHolderAsUser函数实现,传入roleName和packageName,最终调用
android.app.role.RoleManager->addRoleHolderAsUser(...);

public void setRoleHolderAsUser(@NonNull String roleName, @NonNull String packageName,
            boolean add, int flags, @NonNull UserHandle user, @NonNull Context context) {
        if (getValue() != STATE_IDLE) {
            Log.e(LOG_TAG, "Already (tried) managing role holders, requested role: " + roleName
                    + ", requested package: " + packageName);
            return;
        }
        if (DEBUG) {
            Log.i(LOG_TAG, (add ? "Adding" : "Removing") + " package as role holder, role: "
                    + roleName + ", package: " + packageName);
        }
        mLastPackageName = packageName;
        mLastAdd = add;
        mLastFlags = flags;
        mLastUser = user;
        setValue(STATE_WORKING);

        RoleManager roleManager = context.getSystemService(RoleManager.class);
        Executor executor = context.getMainExecutor();
        Consumer<Boolean> callback = successful -> {
            if (successful) {
                if (DEBUG) {
                    Log.i(LOG_TAG, "Package " + (add ? "added" : "removed")
                            + " as role holder, role: " + roleName + ", package: " + packageName);
                }
                setValue(STATE_SUCCESS);
            } else {
                if (DEBUG) {
                    Log.i(LOG_TAG, "Failed to " + (add ? "add" : "remove")
                            + " package as role holder, role: " + roleName + ", package: "
                            + packageName);
                }
                setValue(STATE_FAILURE);
            }
        };
        if (add) {
            roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
        } else {
            roleManager.removeRoleHolderAsUser(roleName, packageName, flags, user, executor,
                    callback);
        }
    }

各默认应用对应的 roleName 如下:
电话
role: android.app.role.DIALER, package: com.android.dialer
短信
role: android.app.role.SMS, package: com.android.mms
主屏幕
role: android.app.role.HOME, package: com.android.launcher3
浏览器
role: android.app.role.BROWSER, package: com.android.browser

可以通过RoleManager->addRoleHolderAsUser(...),在某个服务中重写默认的app包名!


// 默认浏览器
./frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java

import android.app.role.RoleManager;
import android.os.UserHandle;
import android.os.Process;
import java.util.concurrent.Executor;
import java.util.function.Consumer;

public void setDefaultBrowser(Context context, String packageName) {
    String roleName = "android.app.role.BROWSER";
    boolean add = true;
    int flags = 0;
    UserHandle user = Process.myUserHandle();
    Log.d("TAG", "role: "+ roleName + ", package: " + packageName);
    RoleManager roleManager = context.getSystemService(RoleManager.class);
    Executor executor = context.getMainExecutor();
    Consumer<Boolean> callback = successful -> {
        if (successful) {
            Log.d("TAG", "Success , role: " + roleName + ", package: " + packageName);
        } else {
            Log.d("TAG", "Failed , role: " + roleName + ", package: "+ packageName);
        }
    };
    roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
}

@Override
public void systemReady() {
	...
	synchronized (mLock) {
            mSystemReady = true;
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    updateSettings();
                }
            });
            // If this happens, for whatever reason, systemReady came later than systemBooted.
            // And keyguard should be already bound from systemBooted
            if (mSystemBooted) {
                mKeyguardDelegate.onBootCompleted();
            }
        }
        mAutofillManagerInternal = LocalServices.getService(AutofillManagerInternal.class);
	setDefaultBrowser(mContext, "com.xx");//add
	...
}

2.系统给与的pm命令,选中setHomeActivity
frameworks/base/services/core/java/com/android/server/pm/PackageManagerShellCommand.java

PS C:\Users\Aaaa>  adb shell -x
rk3566_r:/ $ pm set-home-activity com.xxx.mylauncher
Success


import android.app.role.IRoleManager;
import android.app.role.RoleManager;
import android.os.ServiceManager;
import java.util.concurrent.CompletableFuture;
import android.os.RemoteCallback;
import android.content.Context;

...

        String default_home_package = SystemProperties.get("persist.sys.xxxxx", null);
        final CompletableFuture<Boolean> future = new CompletableFuture<>();
        final RemoteCallback callback = new RemoteCallback(res -> future.complete(res != null));
        try {
            IRoleManager roleManager = android.app.role.IRoleManager.Stub.asInterface(
                    ServiceManager.getServiceOrThrow(Context.ROLE_SERVICE));
            roleManager.addRoleHolderAsUser(RoleManager.ROLE_HOME, default_home_package,
                    0, 0, callback);
            boolean success = future.get();
            if (success) {
                android.util.Log.v("tww", "DefaultActivity success");
            } else {
                android.util.Log.v("tww", "DefaultActivity Error: Failed to set default home.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
...

标签:java,Settings,Launcher,packageName,package,role,切换,roleName,android
From: https://www.cnblogs.com/kato-T/p/18212468

相关文章

  • nvm介绍、下载、安装、配置及使用,(Node Version Manager)nodejs版本管理切换工具
    1、介绍nvm在Web前端项目开发过程中,由于各种前端框架、插件以及Nodejs、Npm的飞速更新,在项目新开发或对老项目进行更新维护时,有些项目版本的配置和当前Node、Npm环境不匹配,导致运行报错,甚至都无法启动。nvm的出现就是为了解决以上问题的,nvm是一个Node.js版本管理器,......
  • 解决tiktok切换前后置虚拟摄像头卡住问题
    背景介绍:本人原先是android逆向工程师,后来因为工作变动,离开了协议分析这类的岗位,目前在做直播机与第三方应用兼容性分析相关分析。所以就有了这篇兼容性分析文章。本文篇幅较长,需要跟着思路一直往下看,否则肯定云里雾里,第一次发表长文,有很多不足的地方,请各位大佬指正,小弟在此谢......
  • 10-3-前后台进程切换- nice进程优先级-实战screen后台执行命令
    10.3.1Linux后台进程与前台进程的区别前台进程:是在终端中运行的命令,那么该终端就为进程的控制终端,一旦这个终端关闭,这个进程也随着消失。后台进程:也叫守护进程(Daemon),是运行在后台的一种特殊进程,不受终端控制,它不需要与终端交互;Linux的大多数服务器就是用守护进程实现......
  • go多版本切换
    VSCode中如何快速切换golang版本而无影响?如何安装多版本参考链接:https://go.dev/doc/manage-install当已安装某个版本后,执行如下命令goinstallgolang.org/dl/[email protected]执行第一行命令后,会在GOPATH中新增go1.19.7。执行第二行命令后,会在sdk目......
  • vue路由切换滑动效果
    1.增加页面路由{path:'/feedbackList',name:'feedbackList',component:()=>import('../views/feedbackList/index'),meta:{title:"反馈列表",index:1}},{path:'/feedback......
  • Biwen.Settings添加对IConfiguration&IOptions的集成支持
    Biwen.Settings是一个简易的配置项管理模块,主要的作用就是可以校验并持久化配置项,比如将自己的配置存储到数据库中,JSON文件中等使用上也是很简单,只需要在服务中注入配置,比如我们有一个GithubSetting的配置项,我们只需要定义好对象然后注入到Service中即可:[Descripti......
  • Zemax Optics Studio启动软件弹出Ansys Licensing Settings Utility
    博主在安装ZemaxOpticsStudio的过程中遇到了一个问题:安装完成后弹出AnsysLicensingSettingsUtility.解决方法:1.打开任务管理器,点击服务,确保“ANSYS,Inc.LicenseManagerCVD”服务和“ANSYSLicensingTomcat”服务状态是“正在运行”2.确保AnsysLicenseManagerRele......
  • 页面切换保存怎么实现数据不丢失且记忆滚动条位置,常规的方案会闪一下,不如原生性能,怎么
    要实现在页面切换时保存数据状态且记忆滚动条位置,同时避免闪烁现象,可以采用以下几种策略来优化用户体验,这些策略尤其适用于单页面应用(SPA)如Vue或React应用中:1.使用Keep-Alive(Vue中)在Vue中,可以利用<keep-alive>组件来缓存组件实例,这样在切换页面时,组件不会被销毁和重新创建,从而......
  • CSS3随机背景图片切换特效
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`CSS3随机背景图片切换特效日期:2018-5-16阿珏css浏览:4572次评论:16条css3的随机背景图片淡入淡出切换特效 演示效果如本篇文章背景所示......
  • git切换连接方式(ssh-https)
      要将git仓库的ssh链接切换为https链接,可以按照以下步骤操作: 1.打开终端,并进入到本地的git仓库目录中。 2.使用以下命令查看当前git仓库的远程链接:gitremote-v3.使用以下命令移除当前的ssh远程链接:gitremoteremoveorigin4.使用以下命令添加https远程链......