blob: ec914d8733dce54c5572b59e6016f1aac9be5ef4 [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
Sunny Goyal4bbf4192014-11-11 12:23:59 -080019import android.annotation.TargetApi;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040020import android.app.SearchManager;
Sunny Goyale755d462014-07-22 13:48:29 -070021import android.content.ComponentName;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040026import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070027import android.content.res.Resources;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040028import android.database.ContentObserver;
Sunny Goyal33d44382014-10-16 09:24:19 -070029import android.graphics.Point;
Sunny Goyal4bbf4192014-11-11 12:23:59 -080030import android.os.Build;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040031import android.os.Handler;
Sunny Goyal33d44382014-10-16 09:24:19 -070032import android.util.DisplayMetrics;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040033import android.util.Log;
Sunny Goyal33d44382014-10-16 09:24:19 -070034import android.view.Display;
35import android.view.WindowManager;
Kenny Guyed131872014-04-30 03:02:21 +010036import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070037import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070038import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040039import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070040import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040041
Winson Chung6e1c0d32013-10-25 15:24:24 -070042public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070043 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040044
Chris Wrenee523362014-09-09 10:09:02 -040045 private static final boolean DEBUG = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050046
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080047 private final AppFilter mAppFilter;
48 private final BuildInfo mBuildInfo;
Sunny Goyale0f58d72014-11-10 18:05:31 -080049 private final LauncherModel mModel;
50 private final IconCache mIconCache;
51
52 private final boolean mIsScreenLarge;
53 private final float mScreenDensity;
54 private final int mLongPressTimeout = 300;
55
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Michael Jurkaa6a05472013-11-13 17:59:46 +010057 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058
Daniel Sandlere4f98912013-06-25 15:13:26 -040059 private static WeakReference<LauncherProvider> sLauncherProvider;
60 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061
Daniel Sandlere4f98912013-06-25 15:13:26 -040062 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063
Winson Chung5f8afe62013-08-12 16:19:28 -070064 private DynamicGrid mDynamicGrid;
65
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 if (INSTANCE == null) {
68 INSTANCE = new LauncherAppState();
69 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040070 return INSTANCE;
71 }
72
Chris Wrend8fe6de2013-10-04 10:42:14 -040073 public static LauncherAppState getInstanceNoCreate() {
74 return INSTANCE;
75 }
76
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 }
80
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 public static void setApplicationContext(Context context) {
82 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040083 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 }
85 sContext = context.getApplicationContext();
86 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040087
Daniel Sandlere4f98912013-06-25 15:13:26 -040088 private LauncherAppState() {
89 if (sContext == null) {
90 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040091 }
92
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040094
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
96 MemoryTracker.startTrackingMe(sContext, "L");
97 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -0700100 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -0400101 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
102
Michael Jurka6e27f642013-12-10 13:40:30 +0100103 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400104 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100105
106 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800107 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100108 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +0100109 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100110 launcherApps.addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111
112 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100113 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
115 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400116 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 filter = new IntentFilter();
118 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 filter = new IntentFilter();
121 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400122 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123
124 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400125 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
127 mFavoritesObserver);
128 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100129
Michael Jurka6e27f642013-12-10 13:40:30 +0100130 public void recreateWidgetPreviewDb() {
131 if (mWidgetPreviewCacheDb != null) {
132 mWidgetPreviewCacheDb.close();
133 }
134 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
135 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400136
137 /**
138 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
139 */
140 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400141 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100142 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100143 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700144 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145
Daniel Sandlere4f98912013-06-25 15:13:26 -0400146 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 resolver.unregisterContentObserver(mFavoritesObserver);
148 }
149
150 /**
151 * Receives notifications whenever the user favorites have changed.
152 */
153 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
154 @Override
155 public void onChange(boolean selfChange) {
156 // If the database has ever changed, then we really need to force a reload of the
157 // workspace on the next load
158 mModel.resetLoadedState(false, true);
159 mModel.startLoaderFromBackground();
160 }
161 };
162
163 LauncherModel setLauncher(Launcher launcher) {
164 mModel.initialize(launcher);
165 return mModel;
166 }
167
Sunny Goyal34942622014-08-29 17:20:55 -0700168 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400169 return mIconCache;
170 }
171
172 LauncherModel getModel() {
173 return mModel;
174 }
175
Bjorn Bringert1307f632013-10-03 22:31:03 +0100176 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
177 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
178 }
179
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
181 return mWidgetPreviewCacheDb;
182 }
183
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 static void setLauncherProvider(LauncherProvider provider) {
185 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400186 }
187
Daniel Sandlere4f98912013-06-25 15:13:26 -0400188 static LauncherProvider getLauncherProvider() {
189 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400190 }
191
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400192 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100193 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400194 }
195
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800196 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700197 DeviceProfile initDynamicGrid(Context context) {
198 // Determine the dynamic grid properties
199 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
200 Display display = wm.getDefaultDisplay();
201
202 Point realSize = new Point();
203 display.getRealSize(realSize);
204 DisplayMetrics dm = new DisplayMetrics();
205 display.getMetrics(dm);
206
Winson Chung5f8afe62013-08-12 16:19:28 -0700207 if (mDynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700208 Point smallestSize = new Point();
209 Point largestSize = new Point();
210 display.getCurrentSizeRange(smallestSize, largestSize);
211
Winson Chungf7d45852013-10-10 18:57:15 -0700212 mDynamicGrid = new DynamicGrid(context,
213 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700214 Math.min(smallestSize.x, smallestSize.y),
215 Math.min(largestSize.x, largestSize.y),
216 realSize.x, realSize.y,
217 dm.widthPixels, dm.heightPixels);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700218 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700219 }
220
Winson Chung5f8afe62013-08-12 16:19:28 -0700221 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700222 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700223 grid.updateFromConfiguration(context, context.getResources(),
224 realSize.x, realSize.y,
225 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700226 return grid;
227 }
Winson Chungb3800242013-10-24 11:01:54 -0700228 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700229 return mDynamicGrid;
230 }
231
Daniel Sandlere4f98912013-06-25 15:13:26 -0400232 public boolean isScreenLarge() {
233 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400234 }
235
Michael Jurka104c4562013-07-08 18:03:46 -0700236 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
237 public static boolean isScreenLarge(Resources res) {
238 return res.getBoolean(R.bool.is_large_tablet);
239 }
240
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400241 public static boolean isScreenLandscape(Context context) {
242 return context.getResources().getConfiguration().orientation ==
243 Configuration.ORIENTATION_LANDSCAPE;
244 }
245
Daniel Sandlere4f98912013-06-25 15:13:26 -0400246 public float getScreenDensity() {
247 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400248 }
249
Daniel Sandlere4f98912013-06-25 15:13:26 -0400250 public int getLongPressTimeout() {
251 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400252 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700253
Michael Jurkaa6a05472013-11-13 17:59:46 +0100254 public void onWallpaperChanged() {
255 mWallpaperChangedSinceLastCheck = true;
256 }
257
258 public boolean hasWallpaperChangedSinceLastCheck() {
259 boolean result = mWallpaperChangedSinceLastCheck;
260 mWallpaperChangedSinceLastCheck = false;
261 return result;
262 }
263
Winson Chung6e1c0d32013-10-25 15:24:24 -0700264 @Override
265 public void onAvailableSizeChanged(DeviceProfile grid) {
266 Utilities.setIconSize(grid.iconSizePx);
267 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800268
269 public static boolean isDisableAllApps() {
270 // Returns false on non-dogfood builds.
271 return getInstance().mBuildInfo.isDogfoodBuild() &&
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800272 Utilities.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800273 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800274
275 public static boolean isDogfoodBuild() {
276 return getInstance().mBuildInfo.isDogfoodBuild();
277 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500278
Sunny Goyale755d462014-07-22 13:48:29 -0700279 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
280 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500281 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700282
283 /**
284 * Updates the icons and label of all icons for the provided package name.
285 */
286 public void updatePackageBadge(String packageName) {
287 mModel.updatePackageBadge(packageName);
288 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400289}