本文主要是對(duì)自己分析MTK 6.0 Launcher3源碼的筆記株憾,如有發(fā)現(xiàn)不對(duì),請及時(shí)指正
啟動(dòng)流程
app啟動(dòng)都是由application開始啟動(dòng)的。
LauncherApplication.java
public void onCreate() {
super.onCreate();
...
LauncherAppState.setApplicationContext(this);
LauncherAppState.getInstance().setLauncehrApplication(this);
..
}
上面最主要的就是獲取了 LauncherAppState 這個(gè)實(shí)例嗤瞎,然后將自己傳到LauncherAppState的LauncherApplication對(duì)象 讓
LauncherAppState是用于存儲(chǔ)全局變量墙歪,比如緩存(各種cache),維護(hù)內(nèi)存數(shù)據(jù)的類(LauncherModel)贝奇,下面是LauncherAppState的類結(jié)構(gòu):
private final AppFilter mAppFilter;
private final LauncherModel mModel;
private final IconCache mIconCache;
private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
private static WeakReference<LauncherProvider> sLauncherProvider;
mAppFilter:用于存儲(chǔ)app文件夾的一些信息
mModel:用于維護(hù)Launcher在內(nèi)存中的數(shù)據(jù)虹菲,比如app信息列表和widget信息列表,同時(shí)提供了更新數(shù)據(jù)庫的操作
mIconCache:應(yīng)用程序icon和title的緩存
mWidgetPreviewCacheDb:存儲(chǔ)widget預(yù)覽信息的數(shù)據(jù)庫
sLauncherProvide:r app和widget的ContentProvider掉瞳,用數(shù)據(jù)庫存儲(chǔ)信息
private LauncherAppState() {
...
mIsScreenLarge = isScreenLarge(sContext.getResources());
mScreenDensity = sContext.getResources().getDisplayMetrics().density;
recreateWidgetPreviewDb();
mIconCache = new IconCache(sContext);
mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
mModel = new LauncherModel(this, mIconCache, mAppFilter);
final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
launcherApps.addOnAppsChangedCallback(mModel);
// Register intent receivers
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
sContext.registerReceiver(mModel, filter);
filter = new IntentFilter();
filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
sContext.registerReceiver(mModel, filter);
filter = new IntentFilter();
filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
sContext.registerReceiver(mModel, filter);
.....
// Register for changes to the favorites
ContentResolver resolver = sContext.getContentResolver();
resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
mFavoritesObserver);
mPowerManager = (PowerManager)sContext.getSystemService(Context.POWER_SERVICE);
LauncherAppState.getInstance()方法實(shí)例化了以上的數(shù)據(jù)毕源,同時(shí)對(duì)Launcher中使用到的Receiver和Observer進(jìn)行了注冊。
在執(zhí)行完Application的onCreate方法后陕习,接下來就開始執(zhí)行Launcher(Main Activity)的onCreate()方法霎褐。
Launcher.java
看onCreate()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
...此處省略部分代碼
super.onCreate(savedInstanceState);
...
LauncherAppState.setApplicationContext(getApplicationContext());
LauncherAppState app = LauncherAppState.getInstance();
LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this); //監(jiān)聽 LauncherAppState 里面的provider
...
DeviceProfile grid = app.initDynamicGrid(this, isDatabaseIdChanged); //設(shè)備的配置文件
...
/**@}**/
mIconCache = app.getIconCache(); /*獲取Icon的緩存之類*/
mIconCache.flushInvalidIcons(grid);
mDragController = new DragController(this);
mInflater = getLayoutInflater();
...
mAppWidgetManager = AppWidgetManagerCompat.getInstance(this); //獲取Widget的實(shí)例
...
mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();
checkForLocaleChange(); //檢測語言的變化 隨之變化
setContentView(R.layout.launcher);
setupViews(); //實(shí)例化Luncher.XML 全部引用進(jìn)來
grid.layout(this); //配置網(wǎng)格
...
// 數(shù)據(jù)加載
if (!mRestoring) {
/// M: Add for smart book feature. Reset load state if database changed before.
if (isDatabaseIdChanged) { //false
mModel.resetLoadedState(true, true);
} else {
/**M: Added to reset the loader state, to resolve the timing state issue.@{*/
mModel.resetLoadedState(false, false);
/**@}**/
}
if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) { //false
// If the user leaves launcher, then we should just load items asynchronously when
// they return.
mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
} 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.getRestorePage());
}
}
registerContentObservers();
lockAllApps();
......
}
mModel.startLoader(true, mWorkspace.getRestorePage()); 這句話很重要 開始進(jìn)行加載數(shù)據(jù)模型等
在分析加載之前我們先看看luncher.xml都有什么
luncher.xml###
<com.android.launcher3.LauncherRootView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:launcher="http://schemas.android.com/apk/res-auto/com.android.launcher3"
android:id="@+id/launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/workspace_bg"
android:fitsSystemWindows="true">
<com.android.launcher3.DragLayer
android:id="@+id/drag_layer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.android.launcher3.FocusIndicatorView
android:id="@+id/focus_indicator"
android:layout_width="52dp"
android:layout_height="52dp" />
<!-- The workspace contains 5 screens of cells -->
<com.android.launcher3.Workspace
android:id="@+id/workspace"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
launcher:defaultScreen="@integer/config_workspaceDefaultScreen" />
<include layout="@layout/hotseat"
android:id="@+id/hotseat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end" />
<include
android:id="@+id/search_drop_target_bar"
layout="@layout/search_drop_target_bar" />
<include layout="@layout/overview_panel"
android:id="@+id/overview_panel"
android:visibility="gone" />
<include layout="@layout/apps_customize_pane"
android:id="@+id/apps_customize_pane"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
</com.android.launcher3.DragLayer>
<ViewStub
android:id="@+id/launcher_overlay_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inflatedId="@+id/launcher_overlay"
android:layout="@layout/launcher_overlay" />
</com.android.launcher3.LauncherRootView>
DragLayer :這是個(gè)對(duì)Launcher的所有事件處理
Workspace:就是可以左右滑動(dòng)的區(qū)域
![Paste_Image.png](http://upload-images.jianshu.io/upload_images/2687780-2437085bb3712940.png?
imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
FocusIndicatorView:和Icon焦點(diǎn)變化有關(guān)
hotseat:
overview_panel:就是長按顯示下面的圖標(biāo)
apps_customize_pane:
下面貼兩張Launcher常用的類
到此 launcher3的啟動(dòng)流程已經(jīng)完成了
接下來會(huì)分析Launcher3的數(shù)據(jù)加載流程
數(shù)據(jù)加載
剛看到一篇總結(jié)比較好的流程圖
數(shù)據(jù)加載 load和bindWorkspace()
數(shù)據(jù)加載 獲取和bind apps