Android-Launcher-LauncherApplication解析

因为这个app是Launcher,所以这个类是Android系统中的第一个Application的启动,主要做了这些事情

  1. 各各私有变量的初始化工作
  2. 为LauncherModel注册广播接收者
  3. 初始化LauncherProvider
public class LauncherApplication extends Application {
    static final String TAG = "LauncherApplication";
    /*
    这个是为整个Launcerapp 提供数据的,相当于mvp 中的m作用,很重要的类
    LauncherModel 本身是个广播接收者,是继承BroadcastReceiver
    提供数据支持,数据保存状态,刷新屏幕app的状态的作用
    */
    private LauncherModel mModel;
    //对launcer中的图片提供缓存作用的
    private IconCache mIconCache;
    //存储着串口widget 小部件信息的数据库
    private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
    //标记当前的设备是否是大屏设备,比如平板就是大屏的,这个laucnher是可以适配平板的
    private static boolean sIsScreenLarge;
    private static float sScreenDensity;
    //长按事件
    private static int sLongPressTimeout = 300;
    //launcher 这个app下关于SharedPreference的key 统一的
    private static final String sSharedPreferencesKey = "com.android.launcher2.prefs";
    //这个是关于launcher中的app的数据信息的数据提供者,供LauncherModel 查询 还有插入
    WeakReference<LauncherProvider> mLauncherProvider;

    @Override
    public void onCreate() {
        super.onCreate();

        // set sIsScreenXLarge and sScreenDensity *before* creating icon cache
        sIsScreenLarge = getResources().getBoolean(R.bool.is_large_screen);
        sScreenDensity = getResources().getDisplayMetrics().density;

        recreateWidgetPreviewDb();
        mIconCache = new IconCache(this);
        mModel = new LauncherModel(this, mIconCache);
        //LauncherApps 是通过系统服务LauncherAppsService查询 系统中app的数据集合的作用
        //LauncherAppsService最终是通过像PackageManager向PackageManagerService查询
        LauncherApps launcherApps = (LauncherApps)
                getSystemService(Context.LAUNCHER_APPS_SERVICE);
        //注册查询的结果回调
        launcherApps.registerCallback(mModel.getLauncherAppsCallback());

        // Register intent receivers
        //为LauncherModel 注册广播
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
        registerReceiver(mModel, filter);
        filter = new IntentFilter();
        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
        registerReceiver(mModel, filter);
        filter = new IntentFilter();
        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
        registerReceiver(mModel, filter);

        // Register for changes to the favoritesLan
        ContentResolver resolver = getContentResolver();
        //注册LauncherProvider更新的观察者,然后通知LaucherModel 做刷新
        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
                mFavoritesObserver);
    }

    public void recreateWidgetPreviewDb() {
        mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(this);
    }

    /**
     * There's no guarantee that this function is ever called.
     */
    @Override
    public void onTerminate() {
        super.onTerminate();

        unregisterReceiver(mModel);

        ContentResolver resolver = getContentResolver();
        resolver.unregisterContentObserver(mFavoritesObserver);
    }

    /**
     * Receives notifications whenever the user favorites have changed.
     */
    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
        @Override
        public void onChange(boolean selfChange) {
            // If the database has ever changed, then we really need to force a reload of the
            // workspace on the next load
            mModel.resetLoadedState(false, true);
            mModel.startLoaderFromBackground();
        }
    };

    LauncherModel setLauncher(Launcher launcher) {
        mModel.initialize(launcher);
        return mModel;
    }

    IconCache getIconCache() {
        return mIconCache;
    }

    LauncherModel getModel() {
        return mModel;
    }

    WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
        return mWidgetPreviewCacheDb;
    }

    void setLauncherProvider(LauncherProvider provider) {
        mLauncherProvider = new WeakReference<LauncherProvider>(provider);
    }

    LauncherProvider getLauncherProvider() {
        return mLauncherProvider.get();
    }

    public static String getSharedPreferencesKey() {
        return sSharedPreferencesKey;
    }

    public static boolean isScreenLarge() {
        return sIsScreenLarge;
    }

    public static boolean isScreenLandscape(Context context) {
        return context.getResources().getConfiguration().orientation ==
            Configuration.ORIENTATION_LANDSCAPE;
    }

    public static float getScreenDensity() {
        return sScreenDensity;
    }

    public static int getLongPressTimeout() {
        return sLongPressTimeout;
    }
}

你可能感兴趣的:(android源码分析,Android技术分享,android,java,apache)