- 【源码解析】Launcher 8.0源码(5)---Launch
- 【源码解析】Launcher 8.0源码(4)---Launch
- 【源码解析】Launcher 8.0源码(8)---Launch
- 【源码解析】Launcher 8.0源码(7)---Launch
- 【源码解析】Launcher 8.0源码(1)---Launch
- 【源码解析】Launcher 8.0源码(3)---Launch
- 【源码解析】Launcher 8.0源码(2)---Launch
- 【源码解析】Launcher 8.0源码(6)---Launch
- flask/odoo/werkzeug的url mapping
- Launcher3导入Android Studio
我们上篇讲解了Launcher的oncreate方法的一个基本流程是如何创建桌面UI的,这篇我们将一步一步进行详细的讲解
第一步是创建LauncherAppState,他是一个全局的管理类,初始化一些对象和注册广播,获取硬件设备信息。
在Launcher的oncreate方法中是通过
LauncherAppState app = LauncherAppState.getInstance(this);
来获得LauncherAppState的实例,从方法名可以看出是采用的单例的模式创建的,顾名思义就是创建且只创建一次对象,于是整个代码的核心是创建对象,我们就来看一下这段代码
public static LauncherAppState getInstance(final Context context) {
if (INSTANCE == null) {
if (Looper.myLooper() == Looper.getMainLooper()) {
INSTANCE = new LauncherAppState(context.getApplicationContext());
} else {
try {
return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
@Override
public LauncherAppState call() throws Exception {
return LauncherAppState.getInstance(context);
}
}).get();
} catch (InterruptedException|ExecutionException e) {
throw new RuntimeException(e);
}
}
}
return INSTANCE;
}
我们从代码中看到一处写的很严谨的地方就是在创建LauncherAppState的时候传入的Context是通过context.getApplicationContext(),来获取的,而不是传入的Activity,这样一来可以减少内存泄漏。
new LauncherAppState
我们来看一些LauncherAppState的构造方法
private LauncherAppState(Context context) {
if (getLocalProvider(context) == null) {
throw new RuntimeException(
"Initializing LauncherAppState in the absence of LauncherProvider");
}
Log.v(Launcher.TAG, "LauncherAppState initiated");
Preconditions.assertUIThread();
mContext = context;
if (TestingUtils.MEMORY_DUMP_ENABLED) {
TestingUtils.startTrackingMemory(mContext);
}
mInvariantDeviceProfile = new InvariantDeviceProfile(mContext);
mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
// Register intent receivers
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_LOCALE_CHANGED);
// For handling managed profiles
filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
filter.addAction(OGesturesDataService.ACTION_GESTURE_ASSIGNMENTS_UPDATED);
// For extracting colors from the wallpaper
if (Utilities.ATLEAST_NOUGAT) {
// TODO: add a broadcast entry to the manifest for pre-N.
filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
}
mContext.registerReceiver(mModel, filter);
UserManagerCompat.getInstance(mContext).enableAndResetCache();
new ConfigMonitor(mContext).register();
}
在构造方法中最关键的一点就是创建了InvariantDeviceProfile,这个类就是用来获取设备硬件信息的。,紧接着还创建了InconCache图标管理工具,初始化了Weight加载缓存工具mWidgetCache ,初始化广播接收器LauncherModel,处理数据,最后还注册了广播 mContext.registerReceiver(mModel, filter);下一篇我们将一一进行分析这几个类。
网友评论