LauncherAppState.java revision e060b0b3c72b93b47031f0f33bf18b121fb99333
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
19import android.app.SearchManager;
20import android.content.*;
21import android.content.res.Configuration;
22import android.database.ContentObserver;
23import android.os.Handler;
24import android.util.Log;
25
26import java.lang.ref.WeakReference;
27
28public class LauncherAppState {
29    private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
30
31    private LauncherModel mModel;
32    private IconCache mIconCache;
33    private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
34    private boolean mIsScreenLarge;
35    private float mScreenDensity;
36    private int mLongPressTimeout = 300;
37
38    private static WeakReference<LauncherProvider> sLauncherProvider;
39    private static Context sContext;
40
41    private static Object mLock = new Object();
42    private static LauncherAppState INSTANCE;
43
44    public static LauncherAppState getInstance() {
45        if (INSTANCE == null) {
46            INSTANCE = new LauncherAppState();
47        }
48        return INSTANCE;
49    }
50
51    public Context getContext() {
52        return sContext;
53    }
54
55    public static void setApplicationContext(Context context) {
56        if (sContext != null) {
57            Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
58        }
59        sContext = context.getApplicationContext();
60    }
61
62    private LauncherAppState() {
63        if (sContext == null) {
64            throw new IllegalStateException("LauncherAppState inited before app context set");
65        }
66
67        Log.v(Launcher.TAG, "LauncherAppState inited");
68
69        if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
70            MemoryTracker.startTrackingMe(sContext, "L");
71        }
72
73        // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
74        mIsScreenLarge = sContext.getResources().getBoolean(R.bool.is_large_screen);
75        mScreenDensity = sContext.getResources().getDisplayMetrics().density;
76
77        mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
78        mIconCache = new IconCache(sContext);
79        mModel = new LauncherModel(this, mIconCache);
80
81        // Register intent receivers
82        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
83        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
84        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
85        filter.addDataScheme("package");
86        sContext.registerReceiver(mModel, filter);
87        filter = new IntentFilter();
88        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
89        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
90        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
91        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
92        sContext.registerReceiver(mModel, filter);
93        filter = new IntentFilter();
94        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
95        sContext.registerReceiver(mModel, filter);
96        filter = new IntentFilter();
97        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
98        sContext.registerReceiver(mModel, filter);
99
100        // Register for changes to the favorites
101        ContentResolver resolver = sContext.getContentResolver();
102        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
103                mFavoritesObserver);
104    }
105
106    /**
107     * Call from Application.onTerminate(), which is not guaranteed to ever be called.
108     */
109    public void onTerminate() {
110        sContext.unregisterReceiver(mModel);
111
112        ContentResolver resolver = sContext.getContentResolver();
113        resolver.unregisterContentObserver(mFavoritesObserver);
114    }
115
116    /**
117     * Receives notifications whenever the user favorites have changed.
118     */
119    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
120        @Override
121        public void onChange(boolean selfChange) {
122            // If the database has ever changed, then we really need to force a reload of the
123            // workspace on the next load
124            mModel.resetLoadedState(false, true);
125            mModel.startLoaderFromBackground();
126        }
127    };
128
129    LauncherModel setLauncher(Launcher launcher) {
130        if (mModel == null) {
131            throw new IllegalStateException("setLauncher() called before init()");
132        }
133        mModel.initialize(launcher);
134        return mModel;
135    }
136
137    IconCache getIconCache() {
138        return mIconCache;
139    }
140
141    LauncherModel getModel() {
142        return mModel;
143    }
144
145    WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
146        return mWidgetPreviewCacheDb;
147    }
148
149    static void setLauncherProvider(LauncherProvider provider) {
150        sLauncherProvider = new WeakReference<LauncherProvider>(provider);
151    }
152
153    static LauncherProvider getLauncherProvider() {
154        return sLauncherProvider.get();
155    }
156
157    public static String getSharedPreferencesKey() {
158        return SHARED_PREFERENCES_KEY;
159    }
160
161    public boolean isScreenLarge() {
162        return mIsScreenLarge;
163    }
164
165    public static boolean isScreenLandscape(Context context) {
166        return context.getResources().getConfiguration().orientation ==
167            Configuration.ORIENTATION_LANDSCAPE;
168    }
169
170    public float getScreenDensity() {
171        return mScreenDensity;
172    }
173
174    public int getLongPressTimeout() {
175        return mLongPressTimeout;
176    }
177}
178