blob: 8c6fc609fe1ea4b087c91dd65d4ab663daefa8a4 [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;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080035import android.view.View.AccessibilityDelegate;
Sunny Goyal33d44382014-10-16 09:24:19 -070036import android.view.WindowManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080037
Kenny Guyed131872014-04-30 03:02:21 +010038import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070039import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070040import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080041
Daniel Sandlercc8befa2013-06-11 14:45:48 -040042import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070043import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Winson Chung6e1c0d32013-10-25 15:24:24 -070045public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
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;
Adam Cohenc9735cf2015-01-23 16:11:55 -080065 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070066
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 if (INSTANCE == null) {
69 INSTANCE = new LauncherAppState();
70 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 return INSTANCE;
72 }
73
Chris Wrend8fe6de2013-10-04 10:42:14 -040074 public static LauncherAppState getInstanceNoCreate() {
75 return INSTANCE;
76 }
77
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080 }
81
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 public static void setApplicationContext(Context context) {
83 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040084 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 }
86 sContext = context.getApplicationContext();
87 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040088
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 private LauncherAppState() {
90 if (sContext == null) {
91 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040092 }
93
Daniel Sandlere4f98912013-06-25 15:13:26 -040094 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040095
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
97 MemoryTracker.startTrackingMe(sContext, "L");
98 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
Daniel Sandlere4f98912013-06-25 15:13:26 -0400100 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -0700101 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -0400102 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
103
Michael Jurka6e27f642013-12-10 13:40:30 +0100104 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400105 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100106
107 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800108 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100109 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700110
111 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112
113 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100114 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400115 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
116 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120
121 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400122 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
124 mFavoritesObserver);
125 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100126
Michael Jurka6e27f642013-12-10 13:40:30 +0100127 public void recreateWidgetPreviewDb() {
128 if (mWidgetPreviewCacheDb != null) {
129 mWidgetPreviewCacheDb.close();
130 }
131 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
132 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133
134 /**
135 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
136 */
137 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400138 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100139 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100140 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700141 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142
Daniel Sandlere4f98912013-06-25 15:13:26 -0400143 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 resolver.unregisterContentObserver(mFavoritesObserver);
145 }
146
147 /**
148 * Receives notifications whenever the user favorites have changed.
149 */
150 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
151 @Override
152 public void onChange(boolean selfChange) {
153 // If the database has ever changed, then we really need to force a reload of the
154 // workspace on the next load
155 mModel.resetLoadedState(false, true);
156 mModel.startLoaderFromBackground();
157 }
158 };
159
160 LauncherModel setLauncher(Launcher launcher) {
161 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800162 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
163 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 return mModel;
165 }
166
Adam Cohenc9735cf2015-01-23 16:11:55 -0800167 LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800168 return mAccessibilityDelegate;
169 }
170
Sunny Goyal34942622014-08-29 17:20:55 -0700171 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 return mIconCache;
173 }
174
175 LauncherModel getModel() {
176 return mModel;
177 }
178
Bjorn Bringert1307f632013-10-03 22:31:03 +0100179 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
180 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
181 }
182
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
184 return mWidgetPreviewCacheDb;
185 }
186
Daniel Sandlere4f98912013-06-25 15:13:26 -0400187 static void setLauncherProvider(LauncherProvider provider) {
188 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 }
190
Daniel Sandlere4f98912013-06-25 15:13:26 -0400191 static LauncherProvider getLauncherProvider() {
192 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400193 }
194
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400195 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100196 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400197 }
198
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800199 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700200 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500201 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
202 mDynamicGrid.getDeviceProfile().addCallback(this);
203 return mDynamicGrid.getDeviceProfile();
204 }
205
206 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
207 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700208 // Determine the dynamic grid properties
209 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
210 Display display = wm.getDefaultDisplay();
211
212 Point realSize = new Point();
213 display.getRealSize(realSize);
214 DisplayMetrics dm = new DisplayMetrics();
215 display.getMetrics(dm);
216
Chris Wrenb02e6112014-11-24 16:57:54 -0500217 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700218 Point smallestSize = new Point();
219 Point largestSize = new Point();
220 display.getCurrentSizeRange(smallestSize, largestSize);
221
Chris Wrenb02e6112014-11-24 16:57:54 -0500222 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700223 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700224 Math.min(smallestSize.x, smallestSize.y),
225 Math.min(largestSize.x, largestSize.y),
226 realSize.x, realSize.y,
227 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700228 }
229
Winson Chung5f8afe62013-08-12 16:19:28 -0700230 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500231 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700232 grid.updateFromConfiguration(context, context.getResources(),
233 realSize.x, realSize.y,
234 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500235 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700236 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500237
Winson Chungb3800242013-10-24 11:01:54 -0700238 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700239 return mDynamicGrid;
240 }
241
Daniel Sandlere4f98912013-06-25 15:13:26 -0400242 public boolean isScreenLarge() {
243 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400244 }
245
Michael Jurka104c4562013-07-08 18:03:46 -0700246 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
247 public static boolean isScreenLarge(Resources res) {
248 return res.getBoolean(R.bool.is_large_tablet);
249 }
250
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400251 public static boolean isScreenLandscape(Context context) {
252 return context.getResources().getConfiguration().orientation ==
253 Configuration.ORIENTATION_LANDSCAPE;
254 }
255
Daniel Sandlere4f98912013-06-25 15:13:26 -0400256 public float getScreenDensity() {
257 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400258 }
259
Daniel Sandlere4f98912013-06-25 15:13:26 -0400260 public int getLongPressTimeout() {
261 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400262 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700263
Michael Jurkaa6a05472013-11-13 17:59:46 +0100264 public void onWallpaperChanged() {
265 mWallpaperChangedSinceLastCheck = true;
266 }
267
268 public boolean hasWallpaperChangedSinceLastCheck() {
269 boolean result = mWallpaperChangedSinceLastCheck;
270 mWallpaperChangedSinceLastCheck = false;
271 return result;
272 }
273
Winson Chung6e1c0d32013-10-25 15:24:24 -0700274 @Override
275 public void onAvailableSizeChanged(DeviceProfile grid) {
276 Utilities.setIconSize(grid.iconSizePx);
277 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800278
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800279 public static boolean isDogfoodBuild() {
280 return getInstance().mBuildInfo.isDogfoodBuild();
281 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500282
Sunny Goyale755d462014-07-22 13:48:29 -0700283 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
284 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500285 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700286
287 /**
288 * Updates the icons and label of all icons for the provided package name.
289 */
290 public void updatePackageBadge(String packageName) {
291 mModel.updatePackageBadge(packageName);
292 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400293}