blob: 555b1ccadc43389bdd74a18a57259c3f8965757f [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;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080036
Kenny Guyed131872014-04-30 03:02:21 +010037import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070038import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070039import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070040import com.android.launcher3.util.Thunk;
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;
Adam Cohen091440a2015-03-18 14:16:05 -070049 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080050 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070051 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080052
53 private final boolean mIsScreenLarge;
54 private final float mScreenDensity;
55 private final int mLongPressTimeout = 300;
56
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;
Daniel Sandlere4f98912013-06-25 15:13:26 -0400103 mIconCache = new IconCache(sContext);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700104 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
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);
Sunny Goyal27595792015-03-19 13:18:44 -0700109
110 LauncherAppsCompat.getInstance(sContext).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 Sandlercc8befa2013-06-11 14:45:48 -0400116 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400118 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400119
120 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400121 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
123 mFavoritesObserver);
124 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100125
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 /**
127 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
128 */
129 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400130 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100131 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100132 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700133 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134
Daniel Sandlere4f98912013-06-25 15:13:26 -0400135 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400136 resolver.unregisterContentObserver(mFavoritesObserver);
137 }
138
139 /**
140 * Receives notifications whenever the user favorites have changed.
141 */
142 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
143 @Override
144 public void onChange(boolean selfChange) {
145 // If the database has ever changed, then we really need to force a reload of the
146 // workspace on the next load
147 mModel.resetLoadedState(false, true);
148 mModel.startLoaderFromBackground();
149 }
150 };
151
152 LauncherModel setLauncher(Launcher launcher) {
153 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800154 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
155 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400156 return mModel;
157 }
158
Adam Cohenc9735cf2015-01-23 16:11:55 -0800159 LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800160 return mAccessibilityDelegate;
161 }
162
Sunny Goyal34942622014-08-29 17:20:55 -0700163 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 return mIconCache;
165 }
166
167 LauncherModel getModel() {
168 return mModel;
169 }
170
Bjorn Bringert1307f632013-10-03 22:31:03 +0100171 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
172 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
173 }
174
Daniel Sandlere4f98912013-06-25 15:13:26 -0400175 static void setLauncherProvider(LauncherProvider provider) {
176 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400177 }
178
Daniel Sandlere4f98912013-06-25 15:13:26 -0400179 static LauncherProvider getLauncherProvider() {
180 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400181 }
182
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100184 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400185 }
186
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800187 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700188 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500189 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
190 mDynamicGrid.getDeviceProfile().addCallback(this);
191 return mDynamicGrid.getDeviceProfile();
192 }
193
194 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
195 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700196 // Determine the dynamic grid properties
197 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
198 Display display = wm.getDefaultDisplay();
199
200 Point realSize = new Point();
201 display.getRealSize(realSize);
202 DisplayMetrics dm = new DisplayMetrics();
203 display.getMetrics(dm);
204
Chris Wrenb02e6112014-11-24 16:57:54 -0500205 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700206 Point smallestSize = new Point();
207 Point largestSize = new Point();
208 display.getCurrentSizeRange(smallestSize, largestSize);
209
Chris Wrenb02e6112014-11-24 16:57:54 -0500210 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700211 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700212 Math.min(smallestSize.x, smallestSize.y),
213 Math.min(largestSize.x, largestSize.y),
214 realSize.x, realSize.y,
215 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 }
217
Winson Chung5f8afe62013-08-12 16:19:28 -0700218 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500219 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700220 grid.updateFromConfiguration(context, context.getResources(),
221 realSize.x, realSize.y,
222 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500223 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700224 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500225
Winson Chungb3800242013-10-24 11:01:54 -0700226 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700227 return mDynamicGrid;
228 }
229
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700230 public WidgetPreviewLoader getWidgetCache() {
231 return mWidgetCache;
232 }
233
Daniel Sandlere4f98912013-06-25 15:13:26 -0400234 public boolean isScreenLarge() {
235 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400236 }
237
Michael Jurka104c4562013-07-08 18:03:46 -0700238 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
239 public static boolean isScreenLarge(Resources res) {
240 return res.getBoolean(R.bool.is_large_tablet);
241 }
242
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 public static boolean isScreenLandscape(Context context) {
244 return context.getResources().getConfiguration().orientation ==
245 Configuration.ORIENTATION_LANDSCAPE;
246 }
247
Daniel Sandlere4f98912013-06-25 15:13:26 -0400248 public float getScreenDensity() {
249 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400250 }
251
Daniel Sandlere4f98912013-06-25 15:13:26 -0400252 public int getLongPressTimeout() {
253 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400254 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700255
Michael Jurkaa6a05472013-11-13 17:59:46 +0100256 public void onWallpaperChanged() {
257 mWallpaperChangedSinceLastCheck = true;
258 }
259
260 public boolean hasWallpaperChangedSinceLastCheck() {
261 boolean result = mWallpaperChangedSinceLastCheck;
262 mWallpaperChangedSinceLastCheck = false;
263 return result;
264 }
265
Winson Chung6e1c0d32013-10-25 15:24:24 -0700266 @Override
267 public void onAvailableSizeChanged(DeviceProfile grid) {
268 Utilities.setIconSize(grid.iconSizePx);
269 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800270
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800271 public static boolean isDogfoodBuild() {
272 return getInstance().mBuildInfo.isDogfoodBuild();
273 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500274
Sunny Goyale755d462014-07-22 13:48:29 -0700275 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
276 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500277 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700278
279 /**
280 * Updates the icons and label of all icons for the provided package name.
281 */
282 public void updatePackageBadge(String packageName) {
283 mModel.updatePackageBadge(packageName);
284 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400285}