Android4.4-Launcher源碼分析系列之Launcher啓動簡介

一、Launcher的啓動

首先啓動LauncherApplication,這裏面只有兩行代碼,初始化了LauncherAppState,LauncherAppState主要是初始化一些對象,註冊廣播和內容觀察者等.比如你安裝程序和卸載程序,裏面都會監聽這個廣播.

之後啓動Launcher這個Activity,看它的oncreate方法.

        @Override
	protected void onCreate(Bundle savedInstanceState) {
		if (DEBUG_STRICT_MODE) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork() // or
					.penaltyLog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
		}
		super.onCreate(savedInstanceState);
		// 初始化Context,獲取LauncherAppState實例
		LauncherAppState.setApplicationContext(getApplicationContext());
		LauncherAppState app = LauncherAppState.getInstance();

		// 確定grid屬性
		Point smallestSize = new Point();
		Point largestSize = new Point();
		Point realSize = new Point();
		Display display = getWindowManager().getDefaultDisplay();
		display.getCurrentSizeRange(smallestSize, largestSize);
		display.getRealSize(realSize);
		DisplayMetrics dm = new DisplayMetrics();
		display.getMetrics(dm);
		// Lazy-initialize the dynamic grid
		DeviceProfile grid = app.initDynamicGrid(this, Math.min(smallestSize.x, smallestSize.y), Math.min(largestSize.x, largestSize.y), realSize.x, realSize.y, dm.widthPixels, dm.heightPixels);

		//獲取SharedPreferences實例
		mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(), Context.MODE_PRIVATE);
		// 獲取LauncherModel實例
		mModel = app.setLauncher(this);
		// 獲取IconCache實例
		mIconCache = app.getIconCache();
		mIconCache.flushInvalidIcons(grid);
		// 獲取DragController實例
		mDragController = new DragController(this);
		mInflater = getLayoutInflater();

		mStats = new Stats(this);
		// 獲取AppWidgetManager實例
		mAppWidgetManager = AppWidgetManager.getInstance(this);
		// 獲取AppWidgetHost實例並且監聽
		mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
		mAppWidgetHost.startListening();

		// If we are getting an onCreate, we can actually preempt onResume and
		// unset mPaused here,
		// this also ensures that any synchronous binding below doesn't
		// re-trigger another
		// LauncherModel load.
		mPaused = false;

		if (PROFILE_STARTUP) {
			android.os.Debug.startMethodTracing(Environment.getExternalStorageDirectory() + "/launcher");
		}

		checkForLocaleChange();
		setContentView(R.layout.launcher); // 設置佈局

		setupViews();
		grid.layout(this);
		//註冊內容觀察者
		registerContentObservers();

		lockAllApps();

		mSavedState = savedInstanceState;
		restoreState(mSavedState);

		// 更新抽屜界面狀態
		if (mAppsCustomizeContent != null) {
			mAppsCustomizeContent.onPackagesUpdated(LauncherModel.getSortedWidgetsAndShortcuts(this));
		}

		if (PROFILE_STARTUP) {
			android.os.Debug.stopMethodTracing();
		}
		// 加載桌面的圖標,widget等
		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());
			}
		}

		// 處理默認鍵
		mDefaultKeySsb = new SpannableStringBuilder();
		Selection.setSelection(mDefaultKeySsb, 0);
		// 註冊廣播,監聽關閉系統Dialog
		IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
		registerReceiver(mCloseSystemDialogsReceiver, filter);
		// 更新所有的icon
		updateGlobalIcons();
		// 開啓自動旋轉
		unlockScreenOrientation(true);

		// 第一次運行的時候提示幫助
		showFirstRunCling();
	}<span style="font-size:14px;">
</span>

1.初始化LauncherAppState.


2.初始化DeviceProfile,DeviceProfile裏保存了很多Launcher的屬性.比如Icon的大小,Icon標題的大小等.


3.獲取SharedPreferences實例mSharedPrefs,mSharedPrefs保存的都是第一次開機時的提示信息.


4.獲取LauncherModel實例..是操作數據有關的,保存了桌面運行的狀態.像加載widget和快捷方式到桌面都是通過它.


5.獲取IconCache實例,從字面看就知道是保存了圖標的信息.


6.獲取DragController實例,前面文章介紹了,一個拖動控制接口.


7.獲取AppWidgetManager實例,是用來管理widget的.


8.setContentView(R.layout.launcher)    設置佈局.


9.setupViews     控件的初始化,註冊點擊監聽等.


10.註冊內容觀察者.


11.加載桌面的圖標,widget等,調用LauncherModel的startLoader方法進行加載.


12.處理默認鍵.


13.註冊廣播,監聽關閉系統Dialog.


14.更新所有的icon.


15.開啓自動旋轉屏幕.


16.第一次運行的時候提示幫助,就是那些手指點哪裏的提示信息.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章