1,如何加载数据:·LauncherProvider: loadFavorites(); 这是加载默认的workspace 到数据库。如下是仿iphone把所有app都默认添加到桌面的load代码。
private void loadIphoneFavorites(SQLiteDatabase db) {
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ContentValues values = new ContentValues();
//load all applications
final PackageManager packageManager = mContext.getPackageManager();
final List<ResolveInfo> apps = packageManager.queryIntentActivities(intent, 0);
int count = 0;
if (apps != null) {
count = apps.size();
int screenIndex = 1;
int cellXIndex = 0, cellYIndex = 0;
for (int i = 0; i < count; i++) {
screenIndex = i/16 + 1;
cellXIndex = i%4;
cellYIndex = i/4 - ((screenIndex-1) * 4);
ResolveInfo resolveInfo = apps.get(i);
ActivityInfo info = resolveInfo.activityInfo;
values.clear();
boolean isUninstall = false;
int flags = info.applicationInfo.flags;
if ((flags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
isUninstall = true;
} else if ((flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0) {
isUninstall = true;
}
values.put(LauncherSettings.Favorites.CONTAINER, LauncherSettings.Favorites.CONTAINER_DESKTOP);
values.put(LauncherSettings.Favorites.SCREEN, screenIndex);
values.put(LauncherSettings.Favorites.CELLX, cellXIndex);
values.put(LauncherSettings.Favorites.CELLY, cellYIndex);
if (isUninstall) {
values.put(LauncherSettings.Favorites.ISUNINSTALL, LauncherSettings.BaseLauncherColumns.ISUNINSTALL_TRUE);
} else {
values.put(LauncherSettings.Favorites.ISUNINSTALL, LauncherSettings.BaseLauncherColumns.ISUNINSTALL_FALSE);
}
addIphoneShortCut(db, values, info, packageManager, intent);
}
}
// add search widget to database
values.clear();
values.put(LauncherSettings.Favorites.CONTAINER, LauncherSettings.Favorites.CONTAINER_DESKTOP);
values.put(LauncherSettings.Favorites.SCREEN, 0);
values.put(LauncherSettings.Favorites.CELLX, 0);
values.put(LauncherSettings.Favorites.CELLY, 0);
//values.put(LauncherSettings.Favorites.ISUNINSTALL, false);
addSearchWidget(db, values);
}
2,这些数据是如何转化成view的:
Launcher.java 中的binditems 函数来实现:大致调用关系为:LoaderTask ---> loadAndBindWorkspace() --->loadWorkspace( )( 这里就是查询数据库里,导出iteminfo) ---> bindWorkspace( ) --->binditems( 导出的iteminfo为参数).
例如,shorcut,在binditems 中,关键的语句为:
final view shortcut = createShortcut ( (Shortcutinfo )item); (这一句就是实现客制化 view的关键所在,根据xml文件来定制 cellview 并添加到celllayout 中)
workspace.addInScreen( ..............).
3,app 的图片是怎样加载显示的:
app 的图片是交给 Iconcache 的类来管理的,Iconcache 在LauncherApplication 中初始化,然后传入LauncherModel 中,LauncherModel在 loadAllAppsByBatch ( ) 中有这样一句:
for( int j = 0; i<N &&j<batchsize; j++)
{
mAllAppsList.add(new ApplicationInfo(apps.get(i),mIconCache);
i++;
}
跟踪进 new ApplicationInfo ,有如下的调用关系: iconCache.getTitleAndIcon( ) ----> cacheLocked( ...),这个函数就是真正把app 的信息作为CacheEntry 存入 mCache 的哈希表中的。创建 shortcut的时候,就会根据shortcutinfo 来查找相应的图片,然后传入:
favorite.setCompoundDrawablesWithIntrinsicBounds(null,
new FastBitmapDrawable(info.getIcon(mIconCache)),
null, null);
这个函数就是设置textview 的上,下,左,右,四张图片,以便显示。