Launcher是一个特殊的App,属于系统软件,在按home键时会启动的App,在你的Activity中加入如下intent-fliter 的category之后就会被系统当作Launcher应用。
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
一般我们在给视图绑定数据的时候会把它写在主线程onCreate中,如果需要加载时间,我们会用线程去辅助加载数据,Launcher启动时需要加载好App,shortcut,Folder等一系列的item,所以Launcher采用了Runable来加载数据,并且定义了一个规范的LauncherModel.Callback接口来定义具体的加载过程,步骤。
Launcher本身就是一个框架,从首次启动读取自定义的配置文件开始,操作的过程不断的更新db中存储的信息。下面来简单介绍启动流程。
那么具体到代码是如何启动并绑定数据的呢?
首先定义了接口:LauncherModel.Callback 看名字就知道是什么意思了。
public interface Callbacks {
public boolean setLoadOnResume();
public int getCurrentWorkspaceScreen();
public void startBinding();
public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end);
public void bindFolders(HashMap<Long, FolderInfo> folders);
public void finishBindingItems();
public void bindAppWidget(LauncherAppWidgetInfo info);
public void bindAllApplications(ArrayList<ApplicationInfo> apps);
public void bindAppsAdded(ArrayList<ApplicationInfo> apps);
public void bindAppsUpdated(ArrayList<ApplicationInfo> apps);
public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent);
public void bindPackagesUpdated();
public boolean isAllAppsVisible();
public boolean isAllAppsButtonRank(int rank);
public void bindSearchablesChanged();
public void onPageBoundSynchronously(int page);
}
该类中还定义了一个线程:该类就负责读书default_workspace.xml文件,读取数据库等其他的耗时操作。
private class LoaderTask implements Runnable {}
private static class DatabaseHelper extends SQLiteOpenHelper {}
private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ContentValues values = new ContentValues();
PackageManager packageManager = mContext.getPackageManager();
int allAppsButtonRank = mContext.getResources().getInteger(
R.integer.hotseat_all_apps_index);
int i = 0;
try {
XmlResourceParser parser = mContext.getResources().getXml(
workspaceResourceId);
AttributeSet attrs = Xml.asAttributeSet(parser);
beginDocument(parser, TAG_FAVORITES);
final int depth = parser.getDepth();
int type;
while (((type = parser.next()) != XmlPullParser.