刚才分析完A部分,也就是super.onCreate(savedInstanceState); 之前的部分。如今我们来分析B部分
@Override
protected void onCreate(Bundle savedInstanceState) {
// ================================= A 部分
super.onCreate(savedInstanceState);
// ================================= B 部分
setContentView(R.layout.launcher);
// ================================= C 部分
}
B部分主要做以下几个事情,看代码:
// 單例模式獲取 LauncherAppState 對象
LauncherAppState app = LauncherAppState.getInstance();
// Load configuration-specific DeviceProfile
// 設備的輪廓 肖像還是景觀?
mDeviceProfile = getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE ?
app.getInvariantDeviceProfile().landscapeProfile
: app.getInvariantDeviceProfile().portraitProfile;
mSharedPrefs = Utilities.getPrefs(this);
// 根據包管理者判斷是否是安全模式
mIsSafeModeEnabled = getPackageManager().isSafeMode();
mModel = app.setLauncher(this);//設置啓動器
mIconCache = app.getIconCache();//Icon的緩存
mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
mDragController = new DragController(this);
// 所有的app 控制器
mAllAppsController = new AllAppsTransitionController(this);
// 狀態過渡動畫
mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
// mAppWidgetManager.setBindAppWidgetPermission();
mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();//開始監聽
mPaused = false;
分析开始了:
第一部分:单例模式获取对象。
LauncherAppState app = LauncherAppState.getInstance();这个类的代码在如下路径:
rk3288\packages\apps\Launcher3\src\com\android\launcher3\LauncherAppState.java
public static LauncherAppState getInstance() {
if (INSTANCE == null) {
INSTANCE = new LauncherAppState();
}
return INSTANCE;
}
唯一只有一个实例!
第二部分:private DeviceProfile mDeviceProfile;
mDeviceProfile = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ?
app.getInvariantDeviceProfile().landscapeProfile: app.getInvariantDeviceProfile().portraitProfile;
找到这个类如下:
rk3288\packages\apps\Launcher3\src\com\android\launcher3\DeviceProfile.java
Resources res = context.getResources();之后可以获取Resources的对象。
然后res。getConfiguration().orientation就可以获取到方向了。
public InvariantDeviceProfile getInvariantDeviceProfile() {
return mInvariantDeviceProfile;
}
rk3288\packages\apps\Launcher3\src\com\android\launcher3\InvariantDeviceProfile.java 文件里面有两个变量
DeviceProfile landscapeProfile;
DeviceProfile portraitProfil
landscapeProfile = new DeviceProfile(context, this, smallestSize, largestSize,largeSide, smallSide, true /* isLandscape */);
portraitProfile = new DeviceProfile(context, this, smallestSize, largestSize,smallSide, largeSide, false /* isLandscape */);
getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE 这个条件是否成立?
成立就是选择 landscapeProfile
不成立就是选择 portraitProfil
也就是mDeviceProfile的实例化对象。
C:\Users\Administrator>adb logcat -s -vtime InvariantDeviceProfile
--------- beginning of main
--------- beginning of system
01-18 14:26:14.970 E/InvariantDeviceProfile( 1148): jhczz : InvariantDeviceProfile smallSide 1536
01-18 14:26:14.970 E/InvariantDeviceProfile( 1148): jhczz : InvariantDeviceProfile largeSide 2048
--------- beginning of crash
第三部分:获取共享参数 SharedPreferences
private SharedPreferences mSharedPrefs;
mSharedPrefs = Utilities.getPrefs(this);
第四部分:根据包管理者判断是否是安全模式
mIsSafeModeEnabled = getPackageManager().isSafeMode();
第五部分:设置启动器
private LauncherModel mModel;
mModel = app.setLauncher(this);
rk3288\packages\apps\Launcher3\src\com\android\launcher3\LauncherAppState.java
LauncherModel setLauncher(Launcher launcher) {
sLauncherProvider.get().setLauncherProviderChangeListener(launcher);
mModel.initialize(launcher);
return mModel;
}
第六部分:Icon的缓存
mIconCache = app.getIconCache();
private final IconCache mIconCache;
mIconCache = new IconCache(sContext, mInvariantDeviceProfile);
public IconCache getIconCache() {
return mIconCache;
}
第七部分:一些相关的初始化,需要传入一个Launcher上下文来实例化对象。以第一个举例子。
private LauncherAccessibilityDelegate mAccessibilityDelegate;
public LauncherAccessibilityDelegate(Launcher launcher) {
mLauncher = launcher;
mActions.put(REMOVE, new AccessibilityAction(REMOVE,launcher.getText(R.string.remove_drop_target_label)));
mActions.put(INFO, new AccessibilityAction(INFO,launcher.getText(R.string.app_info_drop_target_label)));
mActions.put(UNINSTALL, new AccessibilityAction(UNINSTALL,launcher.getText(R.string.uninstall_drop_target_label)));
mActions.put(ADD_TO_WORKSPACE, new AccessibilityAction(ADD_TO_WORKSPACE,launcher.getText(R.string.action_add_to_workspace)));
mActions.put(MOVE, new AccessibilityAction(MOVE,launcher.getText(R.string.action_move)));
mActions.put(MOVE_TO_WORKSPACE, new AccessibilityAction(MOVE_TO_WORKSPACE,launcher.getText(R.string.action_move_to_workspace)));
mActions.put(RESIZE, new AccessibilityAction(RESIZE, launcher.getText(R.string.action_resize)));
mActions.put(DEEP_SHORTCUTS, new AccessibilityAction(DEEP_SHORTCUTS,launcher.getText(R.string.action_deep_shortcut)));
}
其他就不再赘述;如下:
mAccessibilityDelegate = new LauncherAccessibilityDelegate(this);
mDragController = new DragController(this);
// 所有的app 控制器
mAllAppsController = new AllAppsTransitionController(this);
// 狀態過渡動畫
mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, mAllAppsController);
mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
// mAppWidgetManager.setBindAppWidgetPermission();
mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
mAppWidgetHost.startListening();//開始監聽
mPaused = false;
至此,B部分已经结束。