一、Launcher加载布局的基本流程
Launcher界面的内容展现是由default_workspace.xml决定的,它决定了每个屏幕显示的内容和布局.位于res\xml目录下.它是在LauncherProvider里的loadDefaultFavoritesIfNecessary方法进行解析,然后存储到数据库中.LauncherProvider继承自ContentProvider.看下这个loadDefaultFavoritesIfNecessary方法.
synchronized public void loadDefaultFavoritesIfNecessary(int origWorkspaceResId) {
String spKey = LauncherAppState.getSharedPreferencesKey();
//获取SharedPreferences实例
SharedPreferences sp = getContext().getSharedPreferences(spKey, Context.MODE_PRIVATE);
if (sp.getBoolean(EMPTY_DATABASE_CREATED, false)) {
//初始化workspaceResId
int workspaceResId = origWorkspaceResId;
// 如果没有提供就使用默认的布局
if (workspaceResId == 0) {
workspaceResId = sp.getInt(DEFAULT_WORKSPACE_RESOURCE_ID, R.xml.default_workspace);
}
// Populate favorites table with initial favorites
SharedPreferences.Editor editor = sp.edit();
editor.remove(EMPTY_DATABASE_CREATED);
if (origWorkspaceResId != 0) {
editor.putInt(DEFAULT_WORKSPACE_RESOURCE_ID, origWorkspaceResId);
}
//解析布局数据,加载到数据库中
mOpenHelper.loadFavorites(mOpenHelper.getWritableDatabase(), workspaceResId);
mOpenHelper.setFlagJustLoadedOldDb();
editor.commit();
}
}
这个方法里先初始化WorkSpaceId,然后作为参数传递到loadFavorites中,在loadFavorites方法里进行布局的的解析,然后保存数据到数据库中.代码我就不贴了,有点多.
那么Launcher是如何加载这个布局的呢,Launcher是调用LauncherModel的startLoader方法实现的.
if (!mRestoring) {
if (sPausedFromUserAction) {
// If the user leaves launcher, then we should just load items
// asynchronously when
// they return.
mModel.startLoader(true, -1);
} else {
// We only load the page synchronously if the user rotates (or
// triggers a
// configuration change) while launcher is in the foreground
mModel.startLoader(true, mWorkspace.getCurrentPage());
}
}
LauncherModel是数据库的操作类,在这个类里加载数据.
二、Launcher布局分析