blob: 9082276e45261d9ff82c8137d6bc915f62fe8391 [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;
Adam Cohen091440a2015-03-18 14:16:05 -070041import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080042
Daniel Sandlercc8befa2013-06-11 14:45:48 -040043import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070044import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045
Winson Chung6e1c0d32013-10-25 15:24:24 -070046public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050047
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080048 private final AppFilter mAppFilter;
49 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070050 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080051 private final IconCache mIconCache;
52
53 private final boolean mIsScreenLarge;
54 private final float mScreenDensity;
55 private final int mLongPressTimeout = 300;
56
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Michael Jurkaa6a05472013-11-13 17:59:46 +010058 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040059
Daniel Sandlere4f98912013-06-25 15:13:26 -040060 private static WeakReference<LauncherProvider> sLauncherProvider;
61 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062
Daniel Sandlere4f98912013-06-25 15:13:26 -040063 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064
Winson Chung5f8afe62013-08-12 16:19:28 -070065 private DynamicGrid mDynamicGrid;
Adam Cohenc9735cf2015-01-23 16:11:55 -080066 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070067
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 if (INSTANCE == null) {
70 INSTANCE = new LauncherAppState();
71 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072 return INSTANCE;
73 }
74
Chris Wrend8fe6de2013-10-04 10:42:14 -040075 public static LauncherAppState getInstanceNoCreate() {
76 return INSTANCE;
77 }
78
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 }
82
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 public static void setApplicationContext(Context context) {
84 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040085 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 }
87 sContext = context.getApplicationContext();
88 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 private LauncherAppState() {
91 if (sContext == null) {
92 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040093 }
94
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040096
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
98 MemoryTracker.startTrackingMe(sContext, "L");
99 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400100
Daniel Sandlere4f98912013-06-25 15:13:26 -0400101 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -0700102 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -0400103 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
104
Michael Jurka6e27f642013-12-10 13:40:30 +0100105 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400106 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100107
108 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800109 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100110 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700111
112 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113
114 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100115 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
117 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400119 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400120 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121
122 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400123 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
125 mFavoritesObserver);
126 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127
Michael Jurka6e27f642013-12-10 13:40:30 +0100128 public void recreateWidgetPreviewDb() {
129 if (mWidgetPreviewCacheDb != null) {
130 mWidgetPreviewCacheDb.close();
131 }
132 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
133 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134
135 /**
136 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
137 */
138 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400139 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100140 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100141 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700142 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143
Daniel Sandlere4f98912013-06-25 15:13:26 -0400144 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 resolver.unregisterContentObserver(mFavoritesObserver);
146 }
147
148 /**
149 * Receives notifications whenever the user favorites have changed.
150 */
151 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
152 @Override
153 public void onChange(boolean selfChange) {
154 // If the database has ever changed, then we really need to force a reload of the
155 // workspace on the next load
156 mModel.resetLoadedState(false, true);
157 mModel.startLoaderFromBackground();
158 }
159 };
160
161 LauncherModel setLauncher(Launcher launcher) {
162 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800163 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
164 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 return mModel;
166 }
167
Adam Cohenc9735cf2015-01-23 16:11:55 -0800168 LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800169 return mAccessibilityDelegate;
170 }
171
Sunny Goyal34942622014-08-29 17:20:55 -0700172 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 return mIconCache;
174 }
175
176 LauncherModel getModel() {
177 return mModel;
178 }
179
Bjorn Bringert1307f632013-10-03 22:31:03 +0100180 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
181 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
182 }
183
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
185 return mWidgetPreviewCacheDb;
186 }
187
Daniel Sandlere4f98912013-06-25 15:13:26 -0400188 static void setLauncherProvider(LauncherProvider provider) {
189 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 }
191
Daniel Sandlere4f98912013-06-25 15:13:26 -0400192 static LauncherProvider getLauncherProvider() {
193 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400194 }
195
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400196 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100197 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400198 }
199
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800200 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700201 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500202 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
203 mDynamicGrid.getDeviceProfile().addCallback(this);
204 return mDynamicGrid.getDeviceProfile();
205 }
206
207 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
208 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700209 // Determine the dynamic grid properties
210 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
211 Display display = wm.getDefaultDisplay();
212
213 Point realSize = new Point();
214 display.getRealSize(realSize);
215 DisplayMetrics dm = new DisplayMetrics();
216 display.getMetrics(dm);
217
Chris Wrenb02e6112014-11-24 16:57:54 -0500218 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700219 Point smallestSize = new Point();
220 Point largestSize = new Point();
221 display.getCurrentSizeRange(smallestSize, largestSize);
222
Chris Wrenb02e6112014-11-24 16:57:54 -0500223 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700224 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700225 Math.min(smallestSize.x, smallestSize.y),
226 Math.min(largestSize.x, largestSize.y),
227 realSize.x, realSize.y,
228 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700229 }
230
Winson Chung5f8afe62013-08-12 16:19:28 -0700231 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500232 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700233 grid.updateFromConfiguration(context, context.getResources(),
234 realSize.x, realSize.y,
235 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500236 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700237 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500238
Winson Chungb3800242013-10-24 11:01:54 -0700239 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700240 return mDynamicGrid;
241 }
242
Daniel Sandlere4f98912013-06-25 15:13:26 -0400243 public boolean isScreenLarge() {
244 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400245 }
246
Michael Jurka104c4562013-07-08 18:03:46 -0700247 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
248 public static boolean isScreenLarge(Resources res) {
249 return res.getBoolean(R.bool.is_large_tablet);
250 }
251
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400252 public static boolean isScreenLandscape(Context context) {
253 return context.getResources().getConfiguration().orientation ==
254 Configuration.ORIENTATION_LANDSCAPE;
255 }
256
Daniel Sandlere4f98912013-06-25 15:13:26 -0400257 public float getScreenDensity() {
258 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400259 }
260
Daniel Sandlere4f98912013-06-25 15:13:26 -0400261 public int getLongPressTimeout() {
262 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400263 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700264
Michael Jurkaa6a05472013-11-13 17:59:46 +0100265 public void onWallpaperChanged() {
266 mWallpaperChangedSinceLastCheck = true;
267 }
268
269 public boolean hasWallpaperChangedSinceLastCheck() {
270 boolean result = mWallpaperChangedSinceLastCheck;
271 mWallpaperChangedSinceLastCheck = false;
272 return result;
273 }
274
Winson Chung6e1c0d32013-10-25 15:24:24 -0700275 @Override
276 public void onAvailableSizeChanged(DeviceProfile grid) {
277 Utilities.setIconSize(grid.iconSizePx);
278 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800279
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800280 public static boolean isDogfoodBuild() {
281 return getInstance().mBuildInfo.isDogfoodBuild();
282 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500283
Sunny Goyale755d462014-07-22 13:48:29 -0700284 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
285 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500286 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700287
288 /**
289 * Updates the icons and label of all icons for the provided package name.
290 */
291 public void updatePackageBadge(String packageName) {
292 mModel.updatePackageBadge(packageName);
293 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400294}