blob: 79bc084248e1b8a321c9a0ed85566c89d5150e45 [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
Kenny Guyed131872014-04-30 03:02:21 +010027import com.android.launcher3.compat.LauncherAppsCompat;
28
Daniel Sandlercc8befa2013-06-11 14:45:48 -040029import java.lang.ref.WeakReference;
30
Winson Chung6e1c0d32013-10-25 15:24:24 -070031public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070032 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040033 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
34
Chris Wrenb358f812014-04-16 13:37:00 -040035 private static final boolean DEBUG = true; // STOPSHIP(cwren) temporary for debugging
Chris Wrenaeff7ea2014-02-14 16:59:24 -050036
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080037 private final AppFilter mAppFilter;
38 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040039 private LauncherModel mModel;
40 private IconCache mIconCache;
41 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040042 private boolean mIsScreenLarge;
43 private float mScreenDensity;
44 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010045 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Daniel Sandlere4f98912013-06-25 15:13:26 -040047 private static WeakReference<LauncherProvider> sLauncherProvider;
48 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Daniel Sandlere4f98912013-06-25 15:13:26 -040050 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051
Winson Chung5f8afe62013-08-12 16:19:28 -070052 private DynamicGrid mDynamicGrid;
53
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 if (INSTANCE == null) {
56 INSTANCE = new LauncherAppState();
57 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 return INSTANCE;
59 }
60
Chris Wrend8fe6de2013-10-04 10:42:14 -040061 public static LauncherAppState getInstanceNoCreate() {
62 return INSTANCE;
63 }
64
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040066 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 }
68
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 public static void setApplicationContext(Context context) {
70 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040071 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 }
73 sContext = context.getApplicationContext();
74 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 private LauncherAppState() {
77 if (sContext == null) {
78 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040079 }
80
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
84 MemoryTracker.startTrackingMe(sContext, "L");
85 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070088 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
90
Michael Jurka6e27f642013-12-10 13:40:30 +010091 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010093
94 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080095 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010096 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +010097 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
98 launcherApps.addOnAppsChangedListener(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
100 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100101 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
103 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400104 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105 filter = new IntentFilter();
106 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_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_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111
112 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
115 mFavoritesObserver);
116 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100117
Michael Jurka6e27f642013-12-10 13:40:30 +0100118 public void recreateWidgetPreviewDb() {
119 if (mWidgetPreviewCacheDb != null) {
120 mWidgetPreviewCacheDb.close();
121 }
122 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
123 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124
125 /**
126 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
127 */
128 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400129 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100130 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
131 launcherApps.removeOnAppsChangedListener(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132
Daniel Sandlere4f98912013-06-25 15:13:26 -0400133 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134 resolver.unregisterContentObserver(mFavoritesObserver);
135 }
136
137 /**
138 * Receives notifications whenever the user favorites have changed.
139 */
140 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
141 @Override
142 public void onChange(boolean selfChange) {
143 // If the database has ever changed, then we really need to force a reload of the
144 // workspace on the next load
145 mModel.resetLoadedState(false, true);
146 mModel.startLoaderFromBackground();
147 }
148 };
149
150 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400151 if (mModel == null) {
152 throw new IllegalStateException("setLauncher() called before init()");
153 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400154 mModel.initialize(launcher);
155 return mModel;
156 }
157
158 IconCache getIconCache() {
159 return mIconCache;
160 }
161
162 LauncherModel getModel() {
163 return mModel;
164 }
165
Bjorn Bringert1307f632013-10-03 22:31:03 +0100166 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
167 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
168 }
169
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
171 return mWidgetPreviewCacheDb;
172 }
173
Daniel Sandlere4f98912013-06-25 15:13:26 -0400174 static void setLauncherProvider(LauncherProvider provider) {
175 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176 }
177
Daniel Sandlere4f98912013-06-25 15:13:26 -0400178 static LauncherProvider getLauncherProvider() {
179 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400180 }
181
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400183 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 }
185
Winson Chung892c74d2013-08-22 16:15:50 -0700186 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
187 int width, int height,
188 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700189 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700190 mDynamicGrid = new DynamicGrid(context,
191 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700192 minWidth, minHeight, width, height,
193 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700194 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700195 }
196
Winson Chung5f8afe62013-08-12 16:19:28 -0700197 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700198 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700199 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700200 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 return grid;
202 }
Winson Chungb3800242013-10-24 11:01:54 -0700203 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 return mDynamicGrid;
205 }
206
Daniel Sandlere4f98912013-06-25 15:13:26 -0400207 public boolean isScreenLarge() {
208 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400209 }
210
Michael Jurka104c4562013-07-08 18:03:46 -0700211 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
212 public static boolean isScreenLarge(Resources res) {
213 return res.getBoolean(R.bool.is_large_tablet);
214 }
215
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400216 public static boolean isScreenLandscape(Context context) {
217 return context.getResources().getConfiguration().orientation ==
218 Configuration.ORIENTATION_LANDSCAPE;
219 }
220
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221 public float getScreenDensity() {
222 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400223 }
224
Daniel Sandlere4f98912013-06-25 15:13:26 -0400225 public int getLongPressTimeout() {
226 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400227 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700228
Michael Jurkaa6a05472013-11-13 17:59:46 +0100229 public void onWallpaperChanged() {
230 mWallpaperChangedSinceLastCheck = true;
231 }
232
233 public boolean hasWallpaperChangedSinceLastCheck() {
234 boolean result = mWallpaperChangedSinceLastCheck;
235 mWallpaperChangedSinceLastCheck = false;
236 return result;
237 }
238
Winson Chung6e1c0d32013-10-25 15:24:24 -0700239 @Override
240 public void onAvailableSizeChanged(DeviceProfile grid) {
241 Utilities.setIconSize(grid.iconSizePx);
242 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800243
244 public static boolean isDisableAllApps() {
245 // Returns false on non-dogfood builds.
246 return getInstance().mBuildInfo.isDogfoodBuild() &&
247 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
248 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800249
250 public static boolean isDogfoodBuild() {
251 return getInstance().mBuildInfo.isDogfoodBuild();
252 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500253
254 public void setPackageState(String pkgName, int state) {
255 if (DEBUG) Log.d(TAG, "setPackageState(" + pkgName + ", " + state + ")");
256 mModel.setPackageState(pkgName, state);
257 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400258}