Launcher3 workspace 加载默认的布局

本文深入解析了Launcher3在AOSP Q版本中加载默认布局的过程。首先,InvariantDeviceProfile在Launcher启动时初始化,从device_profiles.xml加载设备配置。接着,LoaderTask的loadWorkspace函数加载default_workspace_n*n.xml布局文件,根据初始化的配置添加快捷方式、文件夹和小部件到工作区。默认图标加载由LauncherProvider的loadDefaultFavoritesIfNecessary方法完成,该方法根据InvariantDeviceProfile的defaultLayoutId加载默认布局,并在首次完成后移除EMPTY_DATABASE_CREATED标志。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文主要分析launcher3 如何加载默认的icon ,基于AOSP Q Launcher3 code

1、 launcher首先会初始化设备配置属性,比如加载哪些图标,图标布局n * n等等,这些配置定义在device_profiles.xml 中,device_profiles.xml 会关联到default_workspace_n*n.xml,
2、 在LoaderTask.java 的loadworkspace函数中会去load默认的布局,其实就是去加载 1中初始化后的配置

1、InvariantDeviceProfile初始化

Launcher在oncreate的时候会通过单例初始化LauncherAppState类

LauncherAppState app = LauncherAppState.getInstance(this);

在LauncherAppState的构造中会初始化InvariantDeviceProfile,先看一下InvariantDeviceProfile构造函数,

  public InvariantDeviceProfile(Context context) {
   
   
		......
        // This guarantees that width < height
        minWidthDps = Utilities.dpiFromPx(Math.min(smallestSize.x, smallestSize.y), dm);
        minHeightDps = Utilities.dpiFromPx(Math.min(largestSize.x, largestSize.y), dm);
        
        ArrayList<InvariantDeviceProfile> closestProfiles = findClosestDeviceProfiles(
                minWidthDps, minHeightDps, getPredefinedDeviceProfiles(context));
                
        InvariantDeviceProfile interpolatedDeviceProfileOut =
                invDistWeightedInterpolate(minWidthDps,  minHeightDps, closestProfiles);

        InvariantDeviceProfile closestProfile = closestProfiles.get(0);
        numRows = closestProfile.numRows;
        numColumns = closestProfile.numColumns;
        numHotseatIcons = closestProfile.numHotseatIcons;
        //这个布局就是后面load default favorites加载所需要的布局id
        defaultLayoutId = closestProfile.defaultLayoutId;
        Log.d("InvariantDeviceProfile", "InvariantDeviceProfile: defaultLayoutId: " + defaultLayoutId);
        demoModeLayoutId = closestProfile.demoModeLayoutId;
        numFolderRows = closestProfile.numFolderRows;
        numFolderColumns = closestProfile.numFolderColumns;
       ......

通过getPredefinedDeviceProfiles 去load device_profiles.xml中定义的所有profile,放到List中,然后通过findClosestDeviceProfiles从 list中找出最接近的profile。

    /**
     * Returns the closest device profiles order
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值