blob: bde54c335d5768a1492d1249bfd21addb536802b [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;
Adam Cohen091440a2015-03-18 14:16:05 -070036import com.android.launcher3.util.Thunk;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080037
Daniel Sandlercc8befa2013-06-11 14:45:48 -040038import java.lang.ref.WeakReference;
39
Winson Chung6e1c0d32013-10-25 15:24:24 -070040public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050041
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080042 private final AppFilter mAppFilter;
43 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070044 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080045 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070046 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080047
48 private final boolean mIsScreenLarge;
49 private final float mScreenDensity;
50 private final int mLongPressTimeout = 300;
51
Michael Jurkaa6a05472013-11-13 17:59:46 +010052 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053
Daniel Sandlere4f98912013-06-25 15:13:26 -040054 private static WeakReference<LauncherProvider> sLauncherProvider;
55 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056
Daniel Sandlere4f98912013-06-25 15:13:26 -040057 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058
Winson Chung5f8afe62013-08-12 16:19:28 -070059 private DynamicGrid mDynamicGrid;
Adam Cohenc9735cf2015-01-23 16:11:55 -080060 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070061
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040063 if (INSTANCE == null) {
64 INSTANCE = new LauncherAppState();
65 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066 return INSTANCE;
67 }
68
Chris Wrend8fe6de2013-10-04 10:42:14 -040069 public static LauncherAppState getInstanceNoCreate() {
70 return INSTANCE;
71 }
72
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075 }
76
Daniel Sandlere4f98912013-06-25 15:13:26 -040077 public static void setApplicationContext(Context context) {
78 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040079 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 }
81 sContext = context.getApplicationContext();
82 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 private LauncherAppState() {
85 if (sContext == null) {
86 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040087 }
88
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040090
Daniel Sandlere4f98912013-06-25 15:13:26 -040091 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
92 MemoryTracker.startTrackingMe(sContext, "L");
93 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040094
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070096 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mIconCache = new IconCache(sContext);
Sunny Goyal5b0e6692015-03-19 14:31:19 -070099 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100100
101 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800102 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100103 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700104
105 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106
107 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100108 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400109 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
110 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700113 // For handling managed profiles
114 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
115 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
116
Daniel Sandlere4f98912013-06-25 15:13:26 -0400117 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100119
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 /**
121 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
122 */
123 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400124 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100125 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100126 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700127 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400128 }
129
130 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700131 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
132 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700134 public void reloadWorkspace() {
135 mModel.resetLoadedState(false, true);
136 mModel.startLoaderFromBackground();
137 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400138
139 LauncherModel setLauncher(Launcher launcher) {
140 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800141 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
142 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 return mModel;
144 }
145
Hyunyoung Song3f471442015-04-08 19:01:34 -0700146 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800147 return mAccessibilityDelegate;
148 }
149
Sunny Goyal34942622014-08-29 17:20:55 -0700150 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400151 return mIconCache;
152 }
153
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700154 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 return mModel;
156 }
157
Bjorn Bringert1307f632013-10-03 22:31:03 +0100158 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
159 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
160 }
161
Daniel Sandlere4f98912013-06-25 15:13:26 -0400162 static void setLauncherProvider(LauncherProvider provider) {
163 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 }
165
Daniel Sandlere4f98912013-06-25 15:13:26 -0400166 static LauncherProvider getLauncherProvider() {
167 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400168 }
169
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100171 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400172 }
173
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800174 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700175 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500176 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
177 mDynamicGrid.getDeviceProfile().addCallback(this);
178 return mDynamicGrid.getDeviceProfile();
179 }
180
181 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
182 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700183 // Determine the dynamic grid properties
184 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
185 Display display = wm.getDefaultDisplay();
186
187 Point realSize = new Point();
188 display.getRealSize(realSize);
189 DisplayMetrics dm = new DisplayMetrics();
190 display.getMetrics(dm);
191
Chris Wrenb02e6112014-11-24 16:57:54 -0500192 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700193 Point smallestSize = new Point();
194 Point largestSize = new Point();
195 display.getCurrentSizeRange(smallestSize, largestSize);
196
Chris Wrenb02e6112014-11-24 16:57:54 -0500197 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700198 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700199 Math.min(smallestSize.x, smallestSize.y),
200 Math.min(largestSize.x, largestSize.y),
201 realSize.x, realSize.y,
202 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700203 }
204
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500206 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700207 grid.updateFromConfiguration(context, context.getResources(),
208 realSize.x, realSize.y,
209 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500210 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700211 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500212
Winson Chungb3800242013-10-24 11:01:54 -0700213 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700214 return mDynamicGrid;
215 }
216
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700217 public WidgetPreviewLoader getWidgetCache() {
218 return mWidgetCache;
219 }
220
Daniel Sandlere4f98912013-06-25 15:13:26 -0400221 public boolean isScreenLarge() {
222 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400223 }
224
Michael Jurka104c4562013-07-08 18:03:46 -0700225 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
226 public static boolean isScreenLarge(Resources res) {
227 return res.getBoolean(R.bool.is_large_tablet);
228 }
229
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400230 public static boolean isScreenLandscape(Context context) {
231 return context.getResources().getConfiguration().orientation ==
232 Configuration.ORIENTATION_LANDSCAPE;
233 }
234
Daniel Sandlere4f98912013-06-25 15:13:26 -0400235 public float getScreenDensity() {
236 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400237 }
238
Daniel Sandlere4f98912013-06-25 15:13:26 -0400239 public int getLongPressTimeout() {
240 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400241 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700242
Michael Jurkaa6a05472013-11-13 17:59:46 +0100243 public void onWallpaperChanged() {
244 mWallpaperChangedSinceLastCheck = true;
245 }
246
247 public boolean hasWallpaperChangedSinceLastCheck() {
248 boolean result = mWallpaperChangedSinceLastCheck;
249 mWallpaperChangedSinceLastCheck = false;
250 return result;
251 }
252
Winson Chung6e1c0d32013-10-25 15:24:24 -0700253 @Override
254 public void onAvailableSizeChanged(DeviceProfile grid) {
255 Utilities.setIconSize(grid.iconSizePx);
256 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800257
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800258 public static boolean isDogfoodBuild() {
259 return getInstance().mBuildInfo.isDogfoodBuild();
260 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400261}