blob: ad010197908b0b3c3fbb77b0d6ca02c9cd3efc26 [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
Chris Wrenb358f812014-04-16 13:37:00 -040033 private static final boolean DEBUG = true; // STOPSHIP(cwren) temporary for debugging
Chris Wrenaeff7ea2014-02-14 16:59:24 -050034
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080035 private final AppFilter mAppFilter;
36 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037 private LauncherModel mModel;
38 private IconCache mIconCache;
39 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040040 private boolean mIsScreenLarge;
41 private float mScreenDensity;
42 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010043 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Daniel Sandlere4f98912013-06-25 15:13:26 -040045 private static WeakReference<LauncherProvider> sLauncherProvider;
46 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Winson Chung5f8afe62013-08-12 16:19:28 -070050 private DynamicGrid mDynamicGrid;
51
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 if (INSTANCE == null) {
54 INSTANCE = new LauncherAppState();
55 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 return INSTANCE;
57 }
58
Chris Wrend8fe6de2013-10-04 10:42:14 -040059 public static LauncherAppState getInstanceNoCreate() {
60 return INSTANCE;
61 }
62
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040064 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 }
66
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 public static void setApplicationContext(Context context) {
68 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040069 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040070 }
71 sContext = context.getApplicationContext();
72 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 private LauncherAppState() {
75 if (sContext == null) {
76 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040077 }
78
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040080
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
82 MemoryTracker.startTrackingMe(sContext, "L");
83 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070086 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
88
Michael Jurka6e27f642013-12-10 13:40:30 +010089 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010091
92 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080093 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010094 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095
96 // Register intent receivers
97 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
98 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
99 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
100 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -0400101 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter = new IntentFilter();
103 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
104 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
105 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
106 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400107 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 filter = new IntentFilter();
109 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111 filter = new IntentFilter();
112 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114
115 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400116 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
118 mFavoritesObserver);
119 }
Michael Jurka6e27f642013-12-10 13:40:30 +0100120
121 public void recreateWidgetPreviewDb() {
122 if (mWidgetPreviewCacheDb != null) {
123 mWidgetPreviewCacheDb.close();
124 }
125 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
126 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400127
128 /**
129 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
130 */
131 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400132 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133
Daniel Sandlere4f98912013-06-25 15:13:26 -0400134 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 resolver.unregisterContentObserver(mFavoritesObserver);
136 }
137
138 /**
139 * Receives notifications whenever the user favorites have changed.
140 */
141 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
142 @Override
143 public void onChange(boolean selfChange) {
144 // If the database has ever changed, then we really need to force a reload of the
145 // workspace on the next load
146 mModel.resetLoadedState(false, true);
147 mModel.startLoaderFromBackground();
148 }
149 };
150
151 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400152 if (mModel == null) {
153 throw new IllegalStateException("setLauncher() called before init()");
154 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 mModel.initialize(launcher);
156 return mModel;
157 }
158
159 IconCache getIconCache() {
160 return mIconCache;
161 }
162
163 LauncherModel getModel() {
164 return mModel;
165 }
166
Bjorn Bringert1307f632013-10-03 22:31:03 +0100167 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
168 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
169 }
170
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400171 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
172 return mWidgetPreviewCacheDb;
173 }
174
Daniel Sandlere4f98912013-06-25 15:13:26 -0400175 static void setLauncherProvider(LauncherProvider provider) {
176 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 }
178
Daniel Sandlere4f98912013-06-25 15:13:26 -0400179 static LauncherProvider getLauncherProvider() {
180 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400181 }
182
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400185 }
186
Winson Chung892c74d2013-08-22 16:15:50 -0700187 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
188 int width, int height,
189 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700190 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700191 mDynamicGrid = new DynamicGrid(context,
192 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700193 minWidth, minHeight, width, height,
194 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700195 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 }
197
Winson Chung5f8afe62013-08-12 16:19:28 -0700198 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700199 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700200 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700201 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700202 return grid;
203 }
Winson Chungb3800242013-10-24 11:01:54 -0700204 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 return mDynamicGrid;
206 }
207
Daniel Sandlere4f98912013-06-25 15:13:26 -0400208 public boolean isScreenLarge() {
209 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400210 }
211
Michael Jurka104c4562013-07-08 18:03:46 -0700212 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
213 public static boolean isScreenLarge(Resources res) {
214 return res.getBoolean(R.bool.is_large_tablet);
215 }
216
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400217 public static boolean isScreenLandscape(Context context) {
218 return context.getResources().getConfiguration().orientation ==
219 Configuration.ORIENTATION_LANDSCAPE;
220 }
221
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222 public float getScreenDensity() {
223 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400224 }
225
Daniel Sandlere4f98912013-06-25 15:13:26 -0400226 public int getLongPressTimeout() {
227 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400228 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700229
Michael Jurkaa6a05472013-11-13 17:59:46 +0100230 public void onWallpaperChanged() {
231 mWallpaperChangedSinceLastCheck = true;
232 }
233
234 public boolean hasWallpaperChangedSinceLastCheck() {
235 boolean result = mWallpaperChangedSinceLastCheck;
236 mWallpaperChangedSinceLastCheck = false;
237 return result;
238 }
239
Winson Chung6e1c0d32013-10-25 15:24:24 -0700240 @Override
241 public void onAvailableSizeChanged(DeviceProfile grid) {
242 Utilities.setIconSize(grid.iconSizePx);
243 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800244
245 public static boolean isDisableAllApps() {
246 // Returns false on non-dogfood builds.
247 return getInstance().mBuildInfo.isDogfoodBuild() &&
248 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
249 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800250
251 public static boolean isDogfoodBuild() {
252 return getInstance().mBuildInfo.isDogfoodBuild();
253 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500254
255 public void setPackageState(String pkgName, int state) {
256 if (DEBUG) Log.d(TAG, "setPackageState(" + pkgName + ", " + state + ")");
257 mModel.setPackageState(pkgName, state);
258 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400259}