blob: 2a08b8176f419ac1a54b662ac746bfbdd06e2292 [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;
Sunny Goyale755d462014-07-22 13:48:29 -070022import 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;
Sunny Goyal33d44382014-10-16 09:24:19 -070027import android.graphics.Point;
Sunny Goyal4bbf4192014-11-11 12:23:59 -080028import android.os.Build;
Sunny Goyal33d44382014-10-16 09:24:19 -070029import android.util.DisplayMetrics;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040030import android.util.Log;
Sunny Goyal33d44382014-10-16 09:24:19 -070031import android.view.Display;
32import android.view.WindowManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080033
Kenny Guyed131872014-04-30 03:02:21 +010034import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070035import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070036import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070037import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080038
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 {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050043
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080044 private final AppFilter mAppFilter;
45 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070046 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080047 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070048 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080049
50 private final boolean mIsScreenLarge;
51 private final float mScreenDensity;
52 private final int mLongPressTimeout = 300;
53
Michael Jurkaa6a05472013-11-13 17:59:46 +010054 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 private static WeakReference<LauncherProvider> sLauncherProvider;
57 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058
Daniel Sandlere4f98912013-06-25 15:13:26 -040059 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060
Winson Chung5f8afe62013-08-12 16:19:28 -070061 private DynamicGrid mDynamicGrid;
Adam Cohenc9735cf2015-01-23 16:11:55 -080062 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070063
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040065 if (INSTANCE == null) {
66 INSTANCE = new LauncherAppState();
67 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040068 return INSTANCE;
69 }
70
Chris Wrend8fe6de2013-10-04 10:42:14 -040071 public static LauncherAppState getInstanceNoCreate() {
72 return INSTANCE;
73 }
74
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077 }
78
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 public static void setApplicationContext(Context context) {
80 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040081 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 }
83 sContext = context.getApplicationContext();
84 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 private LauncherAppState() {
87 if (sContext == null) {
88 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040089 }
90
Daniel Sandlere4f98912013-06-25 15:13:26 -040091 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040092
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
94 MemoryTracker.startTrackingMe(sContext, "L");
95 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040096
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070098 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
Daniel Sandlere4f98912013-06-25 15:13:26 -0400100 mIconCache = new IconCache(sContext);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700101 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100102
103 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800104 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100105 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700106
107 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108
109 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100110 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
112 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400115 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100117
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 /**
119 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
120 */
121 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400122 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100123 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100124 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 }
127
128 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700129 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
130 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700132 public void reloadWorkspace() {
133 mModel.resetLoadedState(false, true);
134 mModel.startLoaderFromBackground();
135 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400136
137 LauncherModel setLauncher(Launcher launcher) {
138 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800139 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
140 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 return mModel;
142 }
143
Adam Cohenc9735cf2015-01-23 16:11:55 -0800144 LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800145 return mAccessibilityDelegate;
146 }
147
Sunny Goyal34942622014-08-29 17:20:55 -0700148 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400149 return mIconCache;
150 }
151
152 LauncherModel getModel() {
153 return mModel;
154 }
155
Bjorn Bringert1307f632013-10-03 22:31:03 +0100156 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
157 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
158 }
159
Daniel Sandlere4f98912013-06-25 15:13:26 -0400160 static void setLauncherProvider(LauncherProvider provider) {
161 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 }
163
Daniel Sandlere4f98912013-06-25 15:13:26 -0400164 static LauncherProvider getLauncherProvider() {
165 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400166 }
167
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100169 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 }
171
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800172 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700173 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500174 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
175 mDynamicGrid.getDeviceProfile().addCallback(this);
176 return mDynamicGrid.getDeviceProfile();
177 }
178
179 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
180 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700181 // Determine the dynamic grid properties
182 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
183 Display display = wm.getDefaultDisplay();
184
185 Point realSize = new Point();
186 display.getRealSize(realSize);
187 DisplayMetrics dm = new DisplayMetrics();
188 display.getMetrics(dm);
189
Chris Wrenb02e6112014-11-24 16:57:54 -0500190 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700191 Point smallestSize = new Point();
192 Point largestSize = new Point();
193 display.getCurrentSizeRange(smallestSize, largestSize);
194
Chris Wrenb02e6112014-11-24 16:57:54 -0500195 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700196 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700197 Math.min(smallestSize.x, smallestSize.y),
198 Math.min(largestSize.x, largestSize.y),
199 realSize.x, realSize.y,
200 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 }
202
Winson Chung5f8afe62013-08-12 16:19:28 -0700203 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500204 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700205 grid.updateFromConfiguration(context, context.getResources(),
206 realSize.x, realSize.y,
207 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500208 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700209 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500210
Winson Chungb3800242013-10-24 11:01:54 -0700211 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700212 return mDynamicGrid;
213 }
214
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700215 public WidgetPreviewLoader getWidgetCache() {
216 return mWidgetCache;
217 }
218
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219 public boolean isScreenLarge() {
220 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400221 }
222
Michael Jurka104c4562013-07-08 18:03:46 -0700223 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
224 public static boolean isScreenLarge(Resources res) {
225 return res.getBoolean(R.bool.is_large_tablet);
226 }
227
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400228 public static boolean isScreenLandscape(Context context) {
229 return context.getResources().getConfiguration().orientation ==
230 Configuration.ORIENTATION_LANDSCAPE;
231 }
232
Daniel Sandlere4f98912013-06-25 15:13:26 -0400233 public float getScreenDensity() {
234 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400235 }
236
Daniel Sandlere4f98912013-06-25 15:13:26 -0400237 public int getLongPressTimeout() {
238 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400239 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700240
Michael Jurkaa6a05472013-11-13 17:59:46 +0100241 public void onWallpaperChanged() {
242 mWallpaperChangedSinceLastCheck = true;
243 }
244
245 public boolean hasWallpaperChangedSinceLastCheck() {
246 boolean result = mWallpaperChangedSinceLastCheck;
247 mWallpaperChangedSinceLastCheck = false;
248 return result;
249 }
250
Winson Chung6e1c0d32013-10-25 15:24:24 -0700251 @Override
252 public void onAvailableSizeChanged(DeviceProfile grid) {
253 Utilities.setIconSize(grid.iconSizePx);
254 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800255
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800256 public static boolean isDogfoodBuild() {
257 return getInstance().mBuildInfo.isDogfoodBuild();
258 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500259
Sunny Goyale755d462014-07-22 13:48:29 -0700260 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
261 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500262 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700263
264 /**
265 * Updates the icons and label of all icons for the provided package name.
266 */
267 public void updatePackageBadge(String packageName) {
268 mModel.updatePackageBadge(packageName);
269 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400270}