blob: f540eb47d1736cf34812fc8df69a242605176f43 [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
Sunny Goyal83a8f042015-05-19 12:52:12 -070034import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Kenny Guyed131872014-04-30 03:02:21 +010035import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070036import com.android.launcher3.compat.PackageInstallerCompat;
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;
40
Winson Chung6e1c0d32013-10-25 15:24:24 -070041public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050042
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080043 private final AppFilter mAppFilter;
44 private final BuildInfo mBuildInfo;
Adam Cohen091440a2015-03-18 14:16:05 -070045 @Thunk final LauncherModel mModel;
Sunny Goyale0f58d72014-11-10 18:05:31 -080046 private final IconCache mIconCache;
Sunny Goyal5b0e6692015-03-19 14:31:19 -070047 private final WidgetPreviewLoader mWidgetCache;
Sunny Goyale0f58d72014-11-10 18:05:31 -080048
49 private final boolean mIsScreenLarge;
50 private final float mScreenDensity;
51 private final int mLongPressTimeout = 300;
52
Michael Jurkaa6a05472013-11-13 17:59:46 +010053 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 private static WeakReference<LauncherProvider> sLauncherProvider;
56 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057
Daniel Sandlere4f98912013-06-25 15:13:26 -040058 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040059
Winson Chung5f8afe62013-08-12 16:19:28 -070060 private DynamicGrid mDynamicGrid;
Adam Cohenc9735cf2015-01-23 16:11:55 -080061 private LauncherAccessibilityDelegate mAccessibilityDelegate;
Winson Chung5f8afe62013-08-12 16:19:28 -070062
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040064 if (INSTANCE == null) {
65 INSTANCE = new LauncherAppState();
66 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 return INSTANCE;
68 }
69
Chris Wrend8fe6de2013-10-04 10:42:14 -040070 public static LauncherAppState getInstanceNoCreate() {
71 return INSTANCE;
72 }
73
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040076 }
77
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 public static void setApplicationContext(Context context) {
79 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040080 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 }
82 sContext = context.getApplicationContext();
83 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040084
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 private LauncherAppState() {
86 if (sContext == null) {
87 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088 }
89
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040091
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
93 MemoryTracker.startTrackingMe(sContext, "L");
94 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070097 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 mIconCache = new IconCache(sContext);
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700100 mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100101
102 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800103 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100104 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Sunny Goyal27595792015-03-19 13:18:44 -0700105
106 LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107
108 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100109 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400110 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
111 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700114 // For handling managed profiles
115 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
116 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
117
Daniel Sandlere4f98912013-06-25 15:13:26 -0400118 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400119 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100120
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121 /**
122 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
123 */
124 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400125 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100126 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100127 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400129 }
130
131 /**
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700132 * Reloads the workspace items from the DB and re-binds the workspace. This should generally
133 * not be called as DB updates are automatically followed by UI update
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134 */
Sunny Goyal1d4a2df2015-03-30 11:11:46 -0700135 public void reloadWorkspace() {
136 mModel.resetLoadedState(false, true);
137 mModel.startLoaderFromBackground();
138 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139
140 LauncherModel setLauncher(Launcher launcher) {
141 mModel.initialize(launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800142 mAccessibilityDelegate = ((launcher != null) && Utilities.isLmpOrAbove()) ?
143 new LauncherAccessibilityDelegate(launcher) : null;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 return mModel;
145 }
146
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147 public LauncherAccessibilityDelegate getAccessibilityDelegate() {
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800148 return mAccessibilityDelegate;
149 }
150
Sunny Goyal34942622014-08-29 17:20:55 -0700151 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 return mIconCache;
153 }
154
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700155 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400156 return mModel;
157 }
158
Bjorn Bringert1307f632013-10-03 22:31:03 +0100159 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
160 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
161 }
162
Daniel Sandlere4f98912013-06-25 15:13:26 -0400163 static void setLauncherProvider(LauncherProvider provider) {
164 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 }
166
Daniel Sandlere4f98912013-06-25 15:13:26 -0400167 static LauncherProvider getLauncherProvider() {
168 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400169 }
170
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400171 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100172 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 }
174
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800175 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700176 DeviceProfile initDynamicGrid(Context context) {
Chris Wrenb02e6112014-11-24 16:57:54 -0500177 mDynamicGrid = createDynamicGrid(context, mDynamicGrid);
178 mDynamicGrid.getDeviceProfile().addCallback(this);
179 return mDynamicGrid.getDeviceProfile();
180 }
181
182 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
183 static DynamicGrid createDynamicGrid(Context context, DynamicGrid dynamicGrid) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700184 // Determine the dynamic grid properties
185 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
186 Display display = wm.getDefaultDisplay();
187
188 Point realSize = new Point();
189 display.getRealSize(realSize);
190 DisplayMetrics dm = new DisplayMetrics();
191 display.getMetrics(dm);
192
Chris Wrenb02e6112014-11-24 16:57:54 -0500193 if (dynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700194 Point smallestSize = new Point();
195 Point largestSize = new Point();
196 display.getCurrentSizeRange(smallestSize, largestSize);
197
Chris Wrenb02e6112014-11-24 16:57:54 -0500198 dynamicGrid = new DynamicGrid(context,
Winson Chungf7d45852013-10-10 18:57:15 -0700199 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700200 Math.min(smallestSize.x, smallestSize.y),
201 Math.min(largestSize.x, largestSize.y),
202 realSize.x, realSize.y,
203 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700204 }
205
Winson Chung5f8afe62013-08-12 16:19:28 -0700206 // Update the icon size
Chris Wrenb02e6112014-11-24 16:57:54 -0500207 DeviceProfile grid = dynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700208 grid.updateFromConfiguration(context, context.getResources(),
209 realSize.x, realSize.y,
210 dm.widthPixels, dm.heightPixels);
Chris Wrenb02e6112014-11-24 16:57:54 -0500211 return dynamicGrid;
Winson Chung5f8afe62013-08-12 16:19:28 -0700212 }
Chris Wrenb02e6112014-11-24 16:57:54 -0500213
Winson Chungb3800242013-10-24 11:01:54 -0700214 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700215 return mDynamicGrid;
216 }
217
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700218 public WidgetPreviewLoader getWidgetCache() {
219 return mWidgetCache;
220 }
221
Daniel Sandlere4f98912013-06-25 15:13:26 -0400222 public boolean isScreenLarge() {
223 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400224 }
225
Michael Jurka104c4562013-07-08 18:03:46 -0700226 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
227 public static boolean isScreenLarge(Resources res) {
228 return res.getBoolean(R.bool.is_large_tablet);
229 }
230
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400231 public static boolean isScreenLandscape(Context context) {
232 return context.getResources().getConfiguration().orientation ==
233 Configuration.ORIENTATION_LANDSCAPE;
234 }
235
Daniel Sandlere4f98912013-06-25 15:13:26 -0400236 public float getScreenDensity() {
237 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400238 }
239
Daniel Sandlere4f98912013-06-25 15:13:26 -0400240 public int getLongPressTimeout() {
241 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400242 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700243
Michael Jurkaa6a05472013-11-13 17:59:46 +0100244 public void onWallpaperChanged() {
245 mWallpaperChangedSinceLastCheck = true;
246 }
247
248 public boolean hasWallpaperChangedSinceLastCheck() {
249 boolean result = mWallpaperChangedSinceLastCheck;
250 mWallpaperChangedSinceLastCheck = false;
251 return result;
252 }
253
Winson Chung6e1c0d32013-10-25 15:24:24 -0700254 @Override
255 public void onAvailableSizeChanged(DeviceProfile grid) {
256 Utilities.setIconSize(grid.iconSizePx);
257 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800258
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800259 public static boolean isDogfoodBuild() {
260 return getInstance().mBuildInfo.isDogfoodBuild();
261 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400262}