blob: 246278fa250c2514634d107975cd5216b396ee1a [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 Goyale755d462014-07-22 13:48:29 -070032import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +010033
Daniel Sandlercc8befa2013-06-11 14:45:48 -040034import java.lang.ref.WeakReference;
Sunny Goyale755d462014-07-22 13:48:29 -070035import java.util.ArrayList;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040036
Winson Chung6e1c0d32013-10-25 15:24:24 -070037public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070038 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040039 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
40
Chris Wrenee523362014-09-09 10:09:02 -040041 private static final boolean DEBUG = false;
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;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045 private LauncherModel mModel;
46 private IconCache mIconCache;
47 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private boolean mIsScreenLarge;
49 private float mScreenDensity;
50 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010051 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040052
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 private static WeakReference<LauncherProvider> sLauncherProvider;
54 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057
Winson Chung5f8afe62013-08-12 16:19:28 -070058 private DynamicGrid mDynamicGrid;
59
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040061 if (INSTANCE == null) {
62 INSTANCE = new LauncherAppState();
63 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 return INSTANCE;
65 }
66
Chris Wrend8fe6de2013-10-04 10:42:14 -040067 public static LauncherAppState getInstanceNoCreate() {
68 return INSTANCE;
69 }
70
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 }
74
Daniel Sandlere4f98912013-06-25 15:13:26 -040075 public static void setApplicationContext(Context context) {
76 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040077 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 }
79 sContext = context.getApplicationContext();
80 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081
Daniel Sandlere4f98912013-06-25 15:13:26 -040082 private LauncherAppState() {
83 if (sContext == null) {
84 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040085 }
86
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
90 MemoryTracker.startTrackingMe(sContext, "L");
91 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092
Daniel Sandlere4f98912013-06-25 15:13:26 -040093 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070094 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040095 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
96
Michael Jurka6e27f642013-12-10 13:40:30 +010097 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010099
100 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800101 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100102 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +0100103 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100104 launcherApps.addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105
106 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100107 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
109 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111 filter = new IntentFilter();
112 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 filter = new IntentFilter();
115 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400116 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117
118 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400119 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400120 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
121 mFavoritesObserver);
122 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100123
Michael Jurka6e27f642013-12-10 13:40:30 +0100124 public void recreateWidgetPreviewDb() {
125 if (mWidgetPreviewCacheDb != null) {
126 mWidgetPreviewCacheDb.close();
127 }
128 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
129 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130
131 /**
132 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
133 */
134 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400135 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100136 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100137 launcherApps.removeOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400138
Daniel Sandlere4f98912013-06-25 15:13:26 -0400139 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140 resolver.unregisterContentObserver(mFavoritesObserver);
141 }
142
143 /**
144 * Receives notifications whenever the user favorites have changed.
145 */
146 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
147 @Override
148 public void onChange(boolean selfChange) {
149 // If the database has ever changed, then we really need to force a reload of the
150 // workspace on the next load
151 mModel.resetLoadedState(false, true);
152 mModel.startLoaderFromBackground();
153 }
154 };
155
156 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400157 if (mModel == null) {
158 throw new IllegalStateException("setLauncher() called before init()");
159 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400160 mModel.initialize(launcher);
161 return mModel;
162 }
163
Sunny Goyal34942622014-08-29 17:20:55 -0700164 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 return mIconCache;
166 }
167
168 LauncherModel getModel() {
169 return mModel;
170 }
171
Bjorn Bringert1307f632013-10-03 22:31:03 +0100172 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
173 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
174 }
175
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
177 return mWidgetPreviewCacheDb;
178 }
179
Daniel Sandlere4f98912013-06-25 15:13:26 -0400180 static void setLauncherProvider(LauncherProvider provider) {
181 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 }
183
Daniel Sandlere4f98912013-06-25 15:13:26 -0400184 static LauncherProvider getLauncherProvider() {
185 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400186 }
187
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400189 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190 }
191
Winson Chung892c74d2013-08-22 16:15:50 -0700192 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
193 int width, int height,
194 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700195 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700196 mDynamicGrid = new DynamicGrid(context,
197 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700198 minWidth, minHeight, width, height,
199 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700200 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700201 }
202
Winson Chung5f8afe62013-08-12 16:19:28 -0700203 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700204 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700205 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700206 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700207 return grid;
208 }
Winson Chungb3800242013-10-24 11:01:54 -0700209 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700210 return mDynamicGrid;
211 }
212
Daniel Sandlere4f98912013-06-25 15:13:26 -0400213 public boolean isScreenLarge() {
214 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400215 }
216
Michael Jurka104c4562013-07-08 18:03:46 -0700217 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
218 public static boolean isScreenLarge(Resources res) {
219 return res.getBoolean(R.bool.is_large_tablet);
220 }
221
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400222 public static boolean isScreenLandscape(Context context) {
223 return context.getResources().getConfiguration().orientation ==
224 Configuration.ORIENTATION_LANDSCAPE;
225 }
226
Daniel Sandlere4f98912013-06-25 15:13:26 -0400227 public float getScreenDensity() {
228 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400229 }
230
Daniel Sandlere4f98912013-06-25 15:13:26 -0400231 public int getLongPressTimeout() {
232 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400233 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700234
Michael Jurkaa6a05472013-11-13 17:59:46 +0100235 public void onWallpaperChanged() {
236 mWallpaperChangedSinceLastCheck = true;
237 }
238
239 public boolean hasWallpaperChangedSinceLastCheck() {
240 boolean result = mWallpaperChangedSinceLastCheck;
241 mWallpaperChangedSinceLastCheck = false;
242 return result;
243 }
244
Winson Chung6e1c0d32013-10-25 15:24:24 -0700245 @Override
246 public void onAvailableSizeChanged(DeviceProfile grid) {
247 Utilities.setIconSize(grid.iconSizePx);
248 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800249
250 public static boolean isDisableAllApps() {
251 // Returns false on non-dogfood builds.
252 return getInstance().mBuildInfo.isDogfoodBuild() &&
253 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
254 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800255
256 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}