LauncherAppState.java revision 5f8afe6280eae34620067696173e71943e1a30a3
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.provider.Settings;
25import android.util.Log;
26import android.view.Display;
27
28import java.lang.ref.WeakReference;
29
30public class LauncherAppState {
31    private static final String TAG = "LauncherAppState";
32    private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
33
34    private LauncherModel mModel;
35    private IconCache mIconCache;
36    private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
37    private boolean mIsScreenLarge;
38    private float mScreenDensity;
39    private int mLongPressTimeout = 300;
40
41    private static WeakReference<LauncherProvider> sLauncherProvider;
42    private static Context sContext;
43
44    private static Object mLock = new Object();
45    private static LauncherAppState INSTANCE;
46
47    private DynamicGrid mDynamicGrid;
48
49    public static LauncherAppState getInstance() {
50        if (INSTANCE == null) {
51            INSTANCE = new LauncherAppState();
52        }
53        return INSTANCE;
54    }
55
56    public Context getContext() {
57        return sContext;
58    }
59
60    public static void setApplicationContext(Context context) {
61        if (sContext != null) {
62            Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
63        }
64        sContext = context.getApplicationContext();
65    }
66
67    private LauncherAppState() {
68        if (sContext == null) {
69            throw new IllegalStateException("LauncherAppState inited before app context set");
70        }
71
72        Log.v(Launcher.TAG, "LauncherAppState inited");
73
74        if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
75            MemoryTracker.startTrackingMe(sContext, "L");
76        }
77
78        // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
79        mIsScreenLarge = sContext.getResources().getBoolean(R.bool.is_large_tablet);
80        mScreenDensity = sContext.getResources().getDisplayMetrics().density;
81
82        mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
83        mIconCache = new IconCache(sContext);
84        mModel = new LauncherModel(this, mIconCache);
85
86        // Register intent receivers
87        IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
88        filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
89        filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
90        filter.addDataScheme("package");
91        sContext.registerReceiver(mModel, filter);
92        filter = new IntentFilter();
93        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
94        filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
95        filter.addAction(Intent.ACTION_LOCALE_CHANGED);
96        filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
97        sContext.registerReceiver(mModel, filter);
98        filter = new IntentFilter();
99        filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
100        sContext.registerReceiver(mModel, filter);
101        filter = new IntentFilter();
102        filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
103        sContext.registerReceiver(mModel, filter);
104
105        // Register for changes to the favorites
106        ContentResolver resolver = sContext.getContentResolver();
107        resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
108                mFavoritesObserver);
109    }
110
111    /**
112     * Call from Application.onTerminate(), which is not guaranteed to ever be called.
113     */
114    public void onTerminate() {
115        sContext.unregisterReceiver(mModel);
116
117        ContentResolver resolver = sContext.getContentResolver();
118        resolver.unregisterContentObserver(mFavoritesObserver);
119    }
120
121    /**
122     * Receives notifications whenever the user favorites have changed.
123     */
124    private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
125        @Override
126        public void onChange(boolean selfChange) {
127            // If the database has ever changed, then we really need to force a reload of the
128            // workspace on the next load
129            mModel.resetLoadedState(false, true);
130            mModel.startLoaderFromBackground();
131        }
132    };
133
134    LauncherModel setLauncher(Launcher launcher) {
135        if (mModel == null) {
136            throw new IllegalStateException("setLauncher() called before init()");
137        }
138        mModel.initialize(launcher);
139        return mModel;
140    }
141
142    IconCache getIconCache() {
143        return mIconCache;
144    }
145
146    LauncherModel getModel() {
147        return mModel;
148    }
149
150    WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
151        return mWidgetPreviewCacheDb;
152    }
153
154    static void setLauncherProvider(LauncherProvider provider) {
155        sLauncherProvider = new WeakReference<LauncherProvider>(provider);
156    }
157
158    static LauncherProvider getLauncherProvider() {
159        return sLauncherProvider.get();
160    }
161
162    public static String getSharedPreferencesKey() {
163        return SHARED_PREFERENCES_KEY;
164    }
165
166    DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight, int width, int height) {
167        boolean created = false;
168        if (mDynamicGrid == null) {
169            mDynamicGrid = new DynamicGrid(context.getResources(),
170                    minWidth, minHeight, width, height);
171            created = true;
172        }
173
174        DeviceProfile grid = mDynamicGrid.getDeviceProfile();
175        if (created) {
176            LauncherModel.updateWorkspaceLayoutCells((int) grid.numColumns, (int) grid.numRows);
177        }
178        // Update the icon size
179        Utilities.setIconSize(grid.iconSizePx);
180        grid.updateFromConfiguration(context.getResources(), width, height);
181        return grid;
182    }
183    DynamicGrid getDynamicGrid() {
184        return mDynamicGrid;
185    }
186
187    public boolean isScreenLarge() {
188        return mIsScreenLarge;
189    }
190
191    public static boolean isScreenLandscape(Context context) {
192        return context.getResources().getConfiguration().orientation ==
193            Configuration.ORIENTATION_LANDSCAPE;
194    }
195
196    public float getScreenDensity() {
197        return mScreenDensity;
198    }
199
200    public int getLongPressTimeout() {
201        return mLongPressTimeout;
202    }
203}
204