blob: e5fa03208293ac86e5af0a2c7315f6cd1e737219 [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;
22import android.content.ContentResolver;
23import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040026import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070027import android.content.res.Resources;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040028import android.database.ContentObserver;
Sunny Goyal33d44382014-10-16 09:24:19 -070029import android.graphics.Point;
Sunny Goyal4bbf4192014-11-11 12:23:59 -080030import android.os.Build;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040031import android.os.Handler;
Sunny Goyal33d44382014-10-16 09:24:19 -070032import android.util.DisplayMetrics;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040033import android.util.Log;
Sunny Goyal33d44382014-10-16 09:24:19 -070034import android.view.Display;
35import android.view.WindowManager;
Kenny Guyed131872014-04-30 03:02:21 +010036import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070037import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyale755d462014-07-22 13:48:29 -070038import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
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 {
Winson Chung5f8afe62013-08-12 16:19:28 -070043 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040044
Chris Wrenee523362014-09-09 10:09:02 -040045 private static final boolean DEBUG = false;
Chris Wrenaeff7ea2014-02-14 16:59:24 -050046
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080047 private final AppFilter mAppFilter;
48 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049 private LauncherModel mModel;
50 private IconCache mIconCache;
51 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040052 private boolean mIsScreenLarge;
53 private float mScreenDensity;
54 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010055 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056
Daniel Sandlere4f98912013-06-25 15:13:26 -040057 private static WeakReference<LauncherProvider> sLauncherProvider;
58 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040059
Daniel Sandlere4f98912013-06-25 15:13:26 -040060 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061
Winson Chung5f8afe62013-08-12 16:19:28 -070062 private DynamicGrid mDynamicGrid;
63
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;
100
Michael Jurka6e27f642013-12-10 13:40:30 +0100101 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -0400102 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +0100103
104 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800105 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +0100106 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +0100107 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100108 launcherApps.addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400109
110 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100111 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
113 filter.addAction(Intent.ACTION_CONFIGURATION_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_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400117 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 filter = new IntentFilter();
119 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400120 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400121
122 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400123 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
125 mFavoritesObserver);
126 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127
Michael Jurka6e27f642013-12-10 13:40:30 +0100128 public void recreateWidgetPreviewDb() {
129 if (mWidgetPreviewCacheDb != null) {
130 mWidgetPreviewCacheDb.close();
131 }
132 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
133 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400134
135 /**
136 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
137 */
138 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400139 sContext.unregisterReceiver(mModel);
Kenny Guy1317e2d2014-05-08 18:52:50 +0100140 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100141 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyal4390ace2014-10-13 11:33:11 -0700142 PackageInstallerCompat.getInstance(sContext).onStop();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143
Daniel Sandlere4f98912013-06-25 15:13:26 -0400144 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 resolver.unregisterContentObserver(mFavoritesObserver);
146 }
147
148 /**
149 * Receives notifications whenever the user favorites have changed.
150 */
151 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
152 @Override
153 public void onChange(boolean selfChange) {
154 // If the database has ever changed, then we really need to force a reload of the
155 // workspace on the next load
156 mModel.resetLoadedState(false, true);
157 mModel.startLoaderFromBackground();
158 }
159 };
160
161 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400162 if (mModel == null) {
163 throw new IllegalStateException("setLauncher() called before init()");
164 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400165 mModel.initialize(launcher);
166 return mModel;
167 }
168
Sunny Goyal34942622014-08-29 17:20:55 -0700169 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400170 return mIconCache;
171 }
172
173 LauncherModel getModel() {
174 return mModel;
175 }
176
Bjorn Bringert1307f632013-10-03 22:31:03 +0100177 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
178 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
179 }
180
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400181 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
182 return mWidgetPreviewCacheDb;
183 }
184
Daniel Sandlere4f98912013-06-25 15:13:26 -0400185 static void setLauncherProvider(LauncherProvider provider) {
186 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400187 }
188
Daniel Sandlere4f98912013-06-25 15:13:26 -0400189 static LauncherProvider getLauncherProvider() {
190 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400191 }
192
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400193 public static String getSharedPreferencesKey() {
Helena Josol28db2802014-10-09 17:04:09 +0100194 return LauncherFiles.SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400195 }
196
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800197 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Sunny Goyal33d44382014-10-16 09:24:19 -0700198 DeviceProfile initDynamicGrid(Context context) {
199 // Determine the dynamic grid properties
200 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
201 Display display = wm.getDefaultDisplay();
202
203 Point realSize = new Point();
204 display.getRealSize(realSize);
205 DisplayMetrics dm = new DisplayMetrics();
206 display.getMetrics(dm);
207
Winson Chung5f8afe62013-08-12 16:19:28 -0700208 if (mDynamicGrid == null) {
Sunny Goyal33d44382014-10-16 09:24:19 -0700209 Point smallestSize = new Point();
210 Point largestSize = new Point();
211 display.getCurrentSizeRange(smallestSize, largestSize);
212
Winson Chungf7d45852013-10-10 18:57:15 -0700213 mDynamicGrid = new DynamicGrid(context,
214 context.getResources(),
Sunny Goyal33d44382014-10-16 09:24:19 -0700215 Math.min(smallestSize.x, smallestSize.y),
216 Math.min(largestSize.x, largestSize.y),
217 realSize.x, realSize.y,
218 dm.widthPixels, dm.heightPixels);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700219 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700220 }
221
Winson Chung5f8afe62013-08-12 16:19:28 -0700222 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700223 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Sunny Goyal33d44382014-10-16 09:24:19 -0700224 grid.updateFromConfiguration(context, context.getResources(),
225 realSize.x, realSize.y,
226 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700227 return grid;
228 }
Winson Chungb3800242013-10-24 11:01:54 -0700229 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700230 return mDynamicGrid;
231 }
232
Daniel Sandlere4f98912013-06-25 15:13:26 -0400233 public boolean isScreenLarge() {
234 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400235 }
236
Michael Jurka104c4562013-07-08 18:03:46 -0700237 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
238 public static boolean isScreenLarge(Resources res) {
239 return res.getBoolean(R.bool.is_large_tablet);
240 }
241
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400242 public static boolean isScreenLandscape(Context context) {
243 return context.getResources().getConfiguration().orientation ==
244 Configuration.ORIENTATION_LANDSCAPE;
245 }
246
Daniel Sandlere4f98912013-06-25 15:13:26 -0400247 public float getScreenDensity() {
248 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400249 }
250
Daniel Sandlere4f98912013-06-25 15:13:26 -0400251 public int getLongPressTimeout() {
252 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400253 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700254
Michael Jurkaa6a05472013-11-13 17:59:46 +0100255 public void onWallpaperChanged() {
256 mWallpaperChangedSinceLastCheck = true;
257 }
258
259 public boolean hasWallpaperChangedSinceLastCheck() {
260 boolean result = mWallpaperChangedSinceLastCheck;
261 mWallpaperChangedSinceLastCheck = false;
262 return result;
263 }
264
Winson Chung6e1c0d32013-10-25 15:24:24 -0700265 @Override
266 public void onAvailableSizeChanged(DeviceProfile grid) {
267 Utilities.setIconSize(grid.iconSizePx);
268 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800269
270 public static boolean isDisableAllApps() {
271 // Returns false on non-dogfood builds.
272 return getInstance().mBuildInfo.isDogfoodBuild() &&
Sunny Goyal4bbf4192014-11-11 12:23:59 -0800273 Utilities.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800274 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800275
276 public static boolean isDogfoodBuild() {
277 return getInstance().mBuildInfo.isDogfoodBuild();
278 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500279
Sunny Goyale755d462014-07-22 13:48:29 -0700280 public void setPackageState(ArrayList<PackageInstallInfo> installInfo) {
281 mModel.setPackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500282 }
Sunny Goyala22666f2014-09-18 13:25:15 -0700283
284 /**
285 * Updates the icons and label of all icons for the provided package name.
286 */
287 public void updatePackageBadge(String packageName) {
288 mModel.updatePackageBadge(packageName);
289 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400290}