本文主要分析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。