blob: d51df32b010eb12d076372657ff8e7019730cdfc [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);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700115 // For handling managed profiles
116 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
117 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
118
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100121
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400122 /**
123 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
124 */
125 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400126 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100128 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 }
131
132 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700133 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
134 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700136 public void reloadWorkspace() {
137 mModel.resetLoadedState(false, true);
138 mModel.startLoaderFromBackground();
139 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140
141 LauncherModel setLauncher(Launcher launcher) {
142 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800143 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
144 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 return mModel;
146 }
147
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800149 return mAccessibilityDelegate;
150 }
151
Sunny Goyal34942622014-08-29 17:20:55 -0700152 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400153 return mIconCache;
154 }
155
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700156 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 return mModel;
158 }
159
Bjorn Bringert1307f632013-10-03 22:31:03 +0100160 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
161 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
162 }
163
Daniel Sandlere4f98912013-06-25 15:13:26 -0400164 static void setLauncherProvider(LauncherProvider provider) {
165 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 }
167
Daniel Sandlere4f98912013-06-25 15:13:26 -0400168 static LauncherProvider getLauncherProvider() {
169 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400170 }
171
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100173 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 }
175
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800176 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700177 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500178 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
179 mDynamicGrid.getDeviceProfile().addCallback(this);
180 return mDynamicGrid.getDeviceProfile();
181 }
182
183 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
184 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700185 // Determine the dynamic grid properties
186 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
187 Display display = wm.getDefaultDisplay();
188
189 Point realSize = new Point();
190 display.getRealSize(realSize);
191 DisplayMetrics dm = new DisplayMetrics();
192 display.getMetrics(dm);
193
Chris Wrenb02e6112014-11-24 16:57:54 -0500194 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700195 Point smallestSize = new Point();
196 Point largestSize = new Point();
197 display.getCurrentSizeRange(smallestSize, largestSize);
198
Chris Wrenb02e6112014-11-24 16:57:54 -0500199 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700200 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700201 Math.min(smallestSize.x, smallestSize.y),
202 Math.min(largestSize.x, largestSize.y),
203 realSize.x, realSize.y,
204 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 }
206
Winson Chung5f8afe62013-08-12 16:19:28 -0700207 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500208 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700209 grid.updateFromConfiguration(context, context.getResources(),
210 realSize.x, realSize.y,
211 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500212 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700213 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500214
Winson Chungb3800242013-10-24 11:01:54 -0700215 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700216 return mDynamicGrid;
217 }
218
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700219 public WidgetPreviewLoader getWidgetCache() {
220 return mWidgetCache;
221 }
222
Daniel Sandlere4f98912013-06-25 15:13:26 -0400223 public boolean isScreenLarge() {
224 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400225 }
226
Michael Jurka104c4562013-07-08 18:03:46 -0700227 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
228 public static boolean isScreenLarge(Resources res) {
229 return res.getBoolean(R.bool.is_large_tablet);
230 }
231
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400232 public static boolean isScreenLandscape(Context context) {
233 return context.getResources().getConfiguration().orientation ==
234 Configuration.ORIENTATION_LANDSCAPE;
235 }
236
Daniel Sandlere4f98912013-06-25 15:13:26 -0400237 public float getScreenDensity() {
238 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400239 }
240
Daniel Sandlere4f98912013-06-25 15:13:26 -0400241 public int getLongPressTimeout() {
242 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400243 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700244
Michael Jurkaa6a05472013-11-13 17:59:46 +0100245 public void onWallpaperChanged() {
246 mWallpaperChangedSinceLastCheck = true;
247 }
248
249 public boolean hasWallpaperChangedSinceLastCheck() {
250 boolean result = mWallpaperChangedSinceLastCheck;
251 mWallpaperChangedSinceLastCheck = false;
252 return result;
253 }
254
Winson Chung6e1c0d32013-10-25 15:24:24 -0700255 @Override
256 public void onAvailableSizeChanged(DeviceProfile grid) {
257 Utilities.setIconSize(grid.iconSizePx);
258 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800259
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800260 public static boolean isDogfoodBuild() {
261 return getInstance().mBuildInfo.isDogfoodBuild();
262 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400263}