blob: 03ab94babccacebc05a03907036ee823e65cc84f [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;
Sunny Goyale755d462014-07-22 13:48:29 -070020import android.content.ComponentName;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040025import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070026import android.content.res.Resources;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040027import android.database.ContentObserver;
28import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040029import android.util.Log;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040030
Kenny Guyed131872014-04-30 03:02:21 +010031import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070032import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070033import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010034
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070036import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037
Winson Chung6e1c0d32013-10-25 15:24:24 -070038public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070039 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040040
Chris Wrenee523362014-09-09 10:09:02 -040041 private static final boolean DEBUG = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050042
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080043 private final AppFilter mAppFilter;
44 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045 private LauncherModel mModel;
46 private IconCache mIconCache;
47 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private boolean mIsScreenLarge;
49 private float mScreenDensity;
50 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010051 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 private static WeakReference<LauncherProvider> sLauncherProvider;
54 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057
Winson Chung5f8afe62013-08-12 16:19:28 -070058 private DynamicGrid mDynamicGrid;
59
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040061 if (INSTANCE == null) {
62 INSTANCE = new LauncherAppState();
63 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 return INSTANCE;
65 }
66
Chris Wrend8fe6de2013-10-04 10:42:14 -040067 public static LauncherAppState getInstanceNoCreate() {
68 return INSTANCE;
69 }
70
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 }
74
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 public static void setApplicationContext(Context context) {
76 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040077 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 }
79 sContext = context.getApplicationContext();
80 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 private LauncherAppState() {
83 if (sContext == null) {
84 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040085 }
86
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
90 MemoryTracker.startTrackingMe(sContext, "L");
91 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070094 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
96
Michael Jurka6e27f642013-12-10 13:40:30 +010097 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010099
100 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800101 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100102 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +0100103 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100104 launcherApps.addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105
106 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100107 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
109 filter.addAction(Intent.ACTION_CONFIGURATION_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_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 filter = new IntentFilter();
115 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400116 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117
118 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
121 mFavoritesObserver);
122 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100123
Michael Jurka6e27f642013-12-10 13:40:30 +0100124 public void recreateWidgetPreviewDb() {
125 if (mWidgetPreviewCacheDb != null) {
126 mWidgetPreviewCacheDb.close();
127 }
128 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
129 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130
131 /**
132 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
133 */
134 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400135 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100136 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100137 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700138 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139
Daniel Sandlere4f98912013-06-25 15:13:26 -0400140 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 resolver.unregisterContentObserver(mFavoritesObserver);
142 }
143
144 /**
145 * Receives notifications whenever the user favorites have changed.
146 */
147 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
148 @Override
149 public void onChange(boolean selfChange) {
150 // If the database has ever changed, then we really need to force a reload of the
151 // workspace on the next load
152 mModel.resetLoadedState(false, true);
153 mModel.startLoaderFromBackground();
154 }
155 };
156
157 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400158 if (mModel == null) {
159 throw new IllegalStateException("setLauncher() called before init()");
160 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400161 mModel.initialize(launcher);
162 return mModel;
163 }
164
Sunny Goyal34942622014-08-29 17:20:55 -0700165 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 return mIconCache;
167 }
168
169 LauncherModel getModel() {
170 return mModel;
171 }
172
Bjorn Bringert1307f632013-10-03 22:31:03 +0100173 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
174 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
175 }
176
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
178 return mWidgetPreviewCacheDb;
179 }
180
Daniel Sandlere4f98912013-06-25 15:13:26 -0400181 static void setLauncherProvider(LauncherProvider provider) {
182 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 }
184
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 static LauncherProvider getLauncherProvider() {
186 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400187 }
188
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100190 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400191 }
192
Winson Chung892c74d2013-08-22 16:15:50 -0700193 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
194 int width, int height,
195 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700197 mDynamicGrid = new DynamicGrid(context,
198 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700199 minWidth, minHeight, width, height,
200 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700201 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700202 }
203
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700205 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700206 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700207 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700208 return grid;
209 }
Winson Chungb3800242013-10-24 11:01:54 -0700210 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700211 return mDynamicGrid;
212 }
213
Daniel Sandlere4f98912013-06-25 15:13:26 -0400214 public boolean isScreenLarge() {
215 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400216 }
217
Michael Jurka104c4562013-07-08 18:03:46 -0700218 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
219 public static boolean isScreenLarge(Resources res) {
220 return res.getBoolean(R.bool.is_large_tablet);
221 }
222
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400223 public static boolean isScreenLandscape(Context context) {
224 return context.getResources().getConfiguration().orientation ==
225 Configuration.ORIENTATION_LANDSCAPE;
226 }
227
Daniel Sandlere4f98912013-06-25 15:13:26 -0400228 public float getScreenDensity() {
229 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400230 }
231
Daniel Sandlere4f98912013-06-25 15:13:26 -0400232 public int getLongPressTimeout() {
233 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400234 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700235
Michael Jurkaa6a05472013-11-13 17:59:46 +0100236 public void onWallpaperChanged() {
237 mWallpaperChangedSinceLastCheck = true;
238 }
239
240 public boolean hasWallpaperChangedSinceLastCheck() {
241 boolean result = mWallpaperChangedSinceLastCheck;
242 mWallpaperChangedSinceLastCheck = false;
243 return result;
244 }
245
Winson Chung6e1c0d32013-10-25 15:24:24 -0700246 @Override
247 public void onAvailableSizeChanged(DeviceProfile grid) {
248 Utilities.setIconSize(grid.iconSizePx);
249 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800250
251 public static boolean isDisableAllApps() {
252 // Returns false on non-dogfood builds.
253 return getInstance().mBuildInfo.isDogfoodBuild() &&
254 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
255 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800256
257 public static boolean isDogfoodBuild() {
258 return getInstance().mBuildInfo.isDogfoodBuild();
259 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500260
Sunny Goyale755d462014-07-22 13:48:29 -0700261 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
262 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500263 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700264
265 /**
266 * Updates the icons and label of all icons for the provided package name.
267 */
268 public void updatePackageBadge(String packageName) {
269 mModel.updatePackageBadge(packageName);
270 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400271}