blob: 84a1d04118245d7fc6e79321de242283c1caaf72 [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
86 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
87 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 }
116
117 /**
118 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
119 */
120 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400121 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122
Daniel Sandlere4f98912013-06-25 15:13:26 -0400123 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 resolver.unregisterContentObserver(mFavoritesObserver);
125 }
126
127 /**
128 * Receives notifications whenever the user favorites have changed.
129 */
130 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
131 @Override
132 public void onChange(boolean selfChange) {
133 // If the database has ever changed, then we really need to force a reload of the
134 // workspace on the next load
135 mModel.resetLoadedState(false, true);
136 mModel.startLoaderFromBackground();
137 }
138 };
139
140 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400141 if (mModel == null) {
142 throw new IllegalStateException("setLauncher() called before init()");
143 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 mModel.initialize(launcher);
145 return mModel;
146 }
147
148 IconCache getIconCache() {
149 return mIconCache;
150 }
151
152 LauncherModel getModel() {
153 return mModel;
154 }
155
Bjorn Bringert1307f632013-10-03 22:31:03 +0100156 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
157 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
158 }
159
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400160 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
161 return mWidgetPreviewCacheDb;
162 }
163
Daniel Sandlere4f98912013-06-25 15:13:26 -0400164 static void setLauncherProvider(LauncherProvider provider) {
165 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 }
167
Daniel Sandlere4f98912013-06-25 15:13:26 -0400168 static LauncherProvider getLauncherProvider() {
169 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400170 }
171
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400173 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 }
175
Winson Chung892c74d2013-08-22 16:15:50 -0700176 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
177 int width, int height,
178 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700179 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700180 mDynamicGrid = new DynamicGrid(context,
181 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700182 minWidth, minHeight, width, height,
183 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700184 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700185 }
186
Winson Chung5f8afe62013-08-12 16:19:28 -0700187 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700188 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700189 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700190 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 return grid;
192 }
Winson Chungb3800242013-10-24 11:01:54 -0700193 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700194 return mDynamicGrid;
195 }
196
Daniel Sandlere4f98912013-06-25 15:13:26 -0400197 public boolean isScreenLarge() {
198 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400199 }
200
Michael Jurka104c4562013-07-08 18:03:46 -0700201 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
202 public static boolean isScreenLarge(Resources res) {
203 return res.getBoolean(R.bool.is_large_tablet);
204 }
205
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 public static boolean isScreenLandscape(Context context) {
207 return context.getResources().getConfiguration().orientation ==
208 Configuration.ORIENTATION_LANDSCAPE;
209 }
210
Daniel Sandlere4f98912013-06-25 15:13:26 -0400211 public float getScreenDensity() {
212 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 }
214
Daniel Sandlere4f98912013-06-25 15:13:26 -0400215 public int getLongPressTimeout() {
216 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400217 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700218
Michael Jurkaa6a05472013-11-13 17:59:46 +0100219 public void onWallpaperChanged() {
220 mWallpaperChangedSinceLastCheck = true;
221 }
222
223 public boolean hasWallpaperChangedSinceLastCheck() {
224 boolean result = mWallpaperChangedSinceLastCheck;
225 mWallpaperChangedSinceLastCheck = false;
226 return result;
227 }
228
Winson Chung6e1c0d32013-10-25 15:24:24 -0700229 @Override
230 public void onAvailableSizeChanged(DeviceProfile grid) {
231 Utilities.setIconSize(grid.iconSizePx);
232 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400233}