Launcher可以说是对外在和内涵要求都非常高的一个应用,太丑了用户第一眼映像就不要;操作效果华丽但是不流畅卡顿也会大大影响用户的使用。所以Launcher中对性能的优化也做下了一定的苦工。
一个应用程序的启动,我们通常而言是通过与Intent匹配的Activity启动的,而实际上,在任何应用程序启动前,该应用程序的Application类已经运行了。Launcher在Applcation中为各个功能做了不少的伏笔与优化。
LauncherApplicaton
LauncherApplication属于自定义的Application类,并在AndroidManifest.xml里指定了自定义Application类,如下:
<application android:name="com.android.launcher2.LauncherApplication" android:process="@string/process" android:label="@string/application_name" android:icon="@drawable/ic_launcher_home"> </application>
作用:
- 创建全局使用的应用程序快捷方式的缓存.
IconCache,即在LauncherApplication中创建.
- 创建全局使用的数据库加载类LauncherModel.
- 为LauncherModel注册package remove,update相关的Boradcast.为LauncherModel 注册favorites 修改的observer.
// Register intent receivers IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); filter.addAction(Intent.ACTION_PACKAGE_REMOVED); filter.addAction(Intent.ACTION_PACKAGE_CHANGED); filter.addDataScheme("package"); registerReceiver(mModel, filter); filter = new IntentFilter(); filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); registerReceiver(mModel, filter); // Register for changes to the favorites ContentResolver resolver = getContentResolver(); resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, mFavoritesObserver);
- 提供全局使用的LauncherModel的入口.
LauncherModel getModel() { return mModel; }
- (父类)提供获取PackageManager,Resource,相关资源的方法.
LauncherModel.java app.getPackageManager().getDefaultActivityIcon(); app.getResources().getInteger();
- 提供Context作用.
IconCache
概括: 这个类是提供系统全局使用的应用程序基本信息(title,icon)的缓存信息.这样每次使用的时候都能够从系统里很快得到. 作用:
- 声明内部类对应存储应用程序的基本信息,因为home的作用就是需要展现这些东西。
private static class CacheEntry { public Bitmap icon; public String title; public Bitmap titleBitmap; }
- 提供应用程序的默认图标.
private Bitmap makeDefaultIcon();
- 提供全局的缓存信息.
private final HashMap<ComponentName, CacheEntry> mCache;
LauncherModel
概括: 在程序包,以及程序的快捷方式更改,添加时,更新数据库的表。加载更新workspace,all apps view的程序及快捷方式. 作用:
- 声明接口Callbacks,提供了一系列加载程序及快捷方式的抽象方法.
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<ApplicationInfo> apps, boolean permanent); public boolean isAllAppsVisible(); }