blob: ab0b135980d6f42120d7f6a0c9f4bbf86394b75a [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
19import android.app.SearchManager;
Sunny Goyale755d462014-07-22 13:48:29 -070020import android.content.ComponentName;
21import android.content.ContentResolver;
22import 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;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040027import android.database.ContentObserver;
28import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040029import android.util.Log;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040030
Kenny Guyed131872014-04-30 03:02:21 +010031import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070032import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070033import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010034
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070036import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037
Winson Chung6e1c0d32013-10-25 15:24:24 -070038public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070039 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040040 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
41
Chris Wrenee523362014-09-09 10:09:02 -040042 private static final boolean DEBUG = false;
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;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046 private LauncherModel mModel;
47 private IconCache mIconCache;
48 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040049 private boolean mIsScreenLarge;
50 private float mScreenDensity;
51 private int mLongPressTimeout = 300;
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;
60
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040062 if (INSTANCE == null) {
63 INSTANCE = new LauncherAppState();
64 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 return INSTANCE;
66 }
67
Chris Wrend8fe6de2013-10-04 10:42:14 -040068 public static LauncherAppState getInstanceNoCreate() {
69 return INSTANCE;
70 }
71
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040073 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 }
75
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 public static void setApplicationContext(Context context) {
77 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040078 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 }
80 sContext = context.getApplicationContext();
81 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 private LauncherAppState() {
84 if (sContext == null) {
85 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040086 }
87
Daniel Sandlere4f98912013-06-25 15:13:26 -040088 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040089
Daniel Sandlere4f98912013-06-25 15:13:26 -040090 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
91 MemoryTracker.startTrackingMe(sContext, "L");
92 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093
Daniel Sandlere4f98912013-06-25 15:13:26 -040094 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070095 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
97
Michael Jurka6e27f642013-12-10 13:40:30 +010098 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 mIconCache = new IconCache(sContext);
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);
Kenny Guyed131872014-04-30 03:02:21 +0100104 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100105 launcherApps.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 Sandlere4f98912013-06-25 15:13:26 -0400111 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112 filter = new IntentFilter();
113 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400114 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400115 filter = new IntentFilter();
116 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400117 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118
119 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400120 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
122 mFavoritesObserver);
123 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100124
Michael Jurka6e27f642013-12-10 13:40:30 +0100125 public void recreateWidgetPreviewDb() {
126 if (mWidgetPreviewCacheDb != null) {
127 mWidgetPreviewCacheDb.close();
128 }
129 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
130 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131
132 /**
133 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
134 */
135 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400136 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100137 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100138 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700139 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140
Daniel Sandlere4f98912013-06-25 15:13:26 -0400141 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142 resolver.unregisterContentObserver(mFavoritesObserver);
143 }
144
145 /**
146 * Receives notifications whenever the user favorites have changed.
147 */
148 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
149 @Override
150 public void onChange(boolean selfChange) {
151 // If the database has ever changed, then we really need to force a reload of the
152 // workspace on the next load
153 mModel.resetLoadedState(false, true);
154 mModel.startLoaderFromBackground();
155 }
156 };
157
158 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400159 if (mModel == null) {
160 throw new IllegalStateException("setLauncher() called before init()");
161 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 mModel.initialize(launcher);
163 return mModel;
164 }
165
Sunny Goyal34942622014-08-29 17:20:55 -0700166 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400167 return mIconCache;
168 }
169
170 LauncherModel getModel() {
171 return mModel;
172 }
173
Bjorn Bringert1307f632013-10-03 22:31:03 +0100174 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
175 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
176 }
177
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400178 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
179 return mWidgetPreviewCacheDb;
180 }
181
Daniel Sandlere4f98912013-06-25 15:13:26 -0400182 static void setLauncherProvider(LauncherProvider provider) {
183 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400184 }
185
Daniel Sandlere4f98912013-06-25 15:13:26 -0400186 static LauncherProvider getLauncherProvider() {
187 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400188 }
189
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400191 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400192 }
193
Winson Chung892c74d2013-08-22 16:15:50 -0700194 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
195 int width, int height,
196 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700197 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700198 mDynamicGrid = new DynamicGrid(context,
199 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700200 minWidth, minHeight, width, height,
201 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700202 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700203 }
204
Winson Chung5f8afe62013-08-12 16:19:28 -0700205 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700206 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700207 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700208 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700209 return grid;
210 }
Winson Chungb3800242013-10-24 11:01:54 -0700211 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700212 return mDynamicGrid;
213 }
214
Daniel Sandlere4f98912013-06-25 15:13:26 -0400215 public boolean isScreenLarge() {
216 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400217 }
218
Michael Jurka104c4562013-07-08 18:03:46 -0700219 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
220 public static boolean isScreenLarge(Resources res) {
221 return res.getBoolean(R.bool.is_large_tablet);
222 }
223
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400224 public static boolean isScreenLandscape(Context context) {
225 return context.getResources().getConfiguration().orientation ==
226 Configuration.ORIENTATION_LANDSCAPE;
227 }
228
Daniel Sandlere4f98912013-06-25 15:13:26 -0400229 public float getScreenDensity() {
230 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400231 }
232
Daniel Sandlere4f98912013-06-25 15:13:26 -0400233 public int getLongPressTimeout() {
234 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400235 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700236
Michael Jurkaa6a05472013-11-13 17:59:46 +0100237 public void onWallpaperChanged() {
238 mWallpaperChangedSinceLastCheck = true;
239 }
240
241 public boolean hasWallpaperChangedSinceLastCheck() {
242 boolean result = mWallpaperChangedSinceLastCheck;
243 mWallpaperChangedSinceLastCheck = false;
244 return result;
245 }
246
Winson Chung6e1c0d32013-10-25 15:24:24 -0700247 @Override
248 public void onAvailableSizeChanged(DeviceProfile grid) {
249 Utilities.setIconSize(grid.iconSizePx);
250 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800251
252 public static boolean isDisableAllApps() {
253 // Returns false on non-dogfood builds.
254 return getInstance().mBuildInfo.isDogfoodBuild() &&
255 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
256 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800257
258 public static boolean isDogfoodBuild() {
259 return getInstance().mBuildInfo.isDogfoodBuild();
260 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500261
Sunny Goyale755d462014-07-22 13:48:29 -0700262 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
263 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500264 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700265
266 /**
267 * Updates the icons and label of all icons for the provided package name.
268 */
269 public void updatePackageBadge(String packageName) {
270 mModel.updatePackageBadge(packageName);
271 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400272}