blob: d27cedcfc711a3021b17edba8771b0b86174508a [file] [log] [blame]
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001/*
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;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040020import android.content.*;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040021import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070022import android.content.res.Resources;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040023import android.database.ContentObserver;
24import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040025import android.util.Log;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040026
27import java.lang.ref.WeakReference;
28
Winson Chung6e1c0d32013-10-25 15:24:24 -070029public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070030 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040031 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
32
Daniel Sandlercc8befa2013-06-11 14:45:48 -040033 private LauncherModel mModel;
34 private IconCache mIconCache;
Bjorn Bringert1307f632013-10-03 22:31:03 +010035 private AppFilter mAppFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040036 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040037 private boolean mIsScreenLarge;
38 private float mScreenDensity;
39 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010040 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040041
Daniel Sandlere4f98912013-06-25 15:13:26 -040042 private static WeakReference<LauncherProvider> sLauncherProvider;
43 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Daniel Sandlere4f98912013-06-25 15:13:26 -040045 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Winson Chung5f8afe62013-08-12 16:19:28 -070047 private DynamicGrid mDynamicGrid;
48
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040050 if (INSTANCE == null) {
51 INSTANCE = new LauncherAppState();
52 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053 return INSTANCE;
54 }
55
Chris Wrend8fe6de2013-10-04 10:42:14 -040056 public static LauncherAppState getInstanceNoCreate() {
57 return INSTANCE;
58 }
59
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040061 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 }
63
Daniel Sandlere4f98912013-06-25 15:13:26 -040064 public static void setApplicationContext(Context context) {
65 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040066 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 }
68 sContext = context.getApplicationContext();
69 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040070
Daniel Sandlere4f98912013-06-25 15:13:26 -040071 private LauncherAppState() {
72 if (sContext == null) {
73 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040074 }
75
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040077
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
79 MemoryTracker.startTrackingMe(sContext, "L");
80 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070083 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
85
Michael Jurka6e27f642013-12-10 13:40:30 +010086 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010088
89 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
90 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040091
92 // Register intent receivers
93 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
94 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
95 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
96 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098 filter = new IntentFilter();
99 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
100 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
101 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
102 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400103 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400104 filter = new IntentFilter();
105 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400106 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107 filter = new IntentFilter();
108 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400109 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400110
111 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400112 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
114 mFavoritesObserver);
115 }
Michael Jurka6e27f642013-12-10 13:40:30 +0100116
117 public void recreateWidgetPreviewDb() {
118 if (mWidgetPreviewCacheDb != null) {
119 mWidgetPreviewCacheDb.close();
120 }
121 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
122 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123
124 /**
125 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
126 */
127 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400128 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400129
Daniel Sandlere4f98912013-06-25 15:13:26 -0400130 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 resolver.unregisterContentObserver(mFavoritesObserver);
132 }
133
134 /**
135 * Receives notifications whenever the user favorites have changed.
136 */
137 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
138 @Override
139 public void onChange(boolean selfChange) {
140 // If the database has ever changed, then we really need to force a reload of the
141 // workspace on the next load
142 mModel.resetLoadedState(false, true);
143 mModel.startLoaderFromBackground();
144 }
145 };
146
147 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400148 if (mModel == null) {
149 throw new IllegalStateException("setLauncher() called before init()");
150 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400151 mModel.initialize(launcher);
152 return mModel;
153 }
154
155 IconCache getIconCache() {
156 return mIconCache;
157 }
158
159 LauncherModel getModel() {
160 return mModel;
161 }
162
Bjorn Bringert1307f632013-10-03 22:31:03 +0100163 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
164 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
165 }
166
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400167 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
168 return mWidgetPreviewCacheDb;
169 }
170
Daniel Sandlere4f98912013-06-25 15:13:26 -0400171 static void setLauncherProvider(LauncherProvider provider) {
172 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 }
174
Daniel Sandlere4f98912013-06-25 15:13:26 -0400175 static LauncherProvider getLauncherProvider() {
176 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400177 }
178
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400179 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400181 }
182
Winson Chung892c74d2013-08-22 16:15:50 -0700183 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
184 int width, int height,
185 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700186 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700187 mDynamicGrid = new DynamicGrid(context,
188 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700189 minWidth, minHeight, width, height,
190 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700191 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700192 }
193
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700195 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700196 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700197 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700198 return grid;
199 }
Winson Chungb3800242013-10-24 11:01:54 -0700200 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 return mDynamicGrid;
202 }
203
Daniel Sandlere4f98912013-06-25 15:13:26 -0400204 public boolean isScreenLarge() {
205 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 }
207
Michael Jurka104c4562013-07-08 18:03:46 -0700208 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
209 public static boolean isScreenLarge(Resources res) {
210 return res.getBoolean(R.bool.is_large_tablet);
211 }
212
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 public static boolean isScreenLandscape(Context context) {
214 return context.getResources().getConfiguration().orientation ==
215 Configuration.ORIENTATION_LANDSCAPE;
216 }
217
Daniel Sandlere4f98912013-06-25 15:13:26 -0400218 public float getScreenDensity() {
219 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400220 }
221
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222 public int getLongPressTimeout() {
223 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400224 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700225
Michael Jurkaa6a05472013-11-13 17:59:46 +0100226 public void onWallpaperChanged() {
227 mWallpaperChangedSinceLastCheck = true;
228 }
229
230 public boolean hasWallpaperChangedSinceLastCheck() {
231 boolean result = mWallpaperChangedSinceLastCheck;
232 mWallpaperChangedSinceLastCheck = false;
233 return result;
234 }
235
Winson Chung6e1c0d32013-10-25 15:24:24 -0700236 @Override
237 public void onAvailableSizeChanged(DeviceProfile grid) {
238 Utilities.setIconSize(grid.iconSizePx);
239 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400240}