blob: 0725a652f7aa1aef664aa69ddd880e6299c4031a [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;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040020import android.content.*;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040021import android.content.res.Configuration;
Michael Jurka104c4562013-07-08 18:03:46 -070022import android.content.res.Resources;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040023import android.database.ContentObserver;
24import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040025import android.util.Log;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040026
Kenny Guyed131872014-04-30 03:02:21 +010027import com.android.launcher3.compat.LauncherAppsCompat;
28
Daniel Sandlercc8befa2013-06-11 14:45:48 -040029import java.lang.ref.WeakReference;
30
Winson Chung6e1c0d32013-10-25 15:24:24 -070031public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070032 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040033 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
34
Chris Wrenb358f812014-04-16 13:37:00 -040035 private static final boolean DEBUG = true; // STOPSHIP(cwren) temporary for debugging
Chris Wrenaeff7ea2014-02-14 16:59:24 -050036
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080037 private final AppFilter mAppFilter;
38 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040039 private LauncherModel mModel;
40 private IconCache mIconCache;
41 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040042 private boolean mIsScreenLarge;
43 private float mScreenDensity;
44 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010045 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Daniel Sandlere4f98912013-06-25 15:13:26 -040047 private static WeakReference<LauncherProvider> sLauncherProvider;
48 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Daniel Sandlere4f98912013-06-25 15:13:26 -040050 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051
Winson Chung5f8afe62013-08-12 16:19:28 -070052 private DynamicGrid mDynamicGrid;
53
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040055 if (INSTANCE == null) {
56 INSTANCE = new LauncherAppState();
57 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 return INSTANCE;
59 }
60
Chris Wrend8fe6de2013-10-04 10:42:14 -040061 public static LauncherAppState getInstanceNoCreate() {
62 return INSTANCE;
63 }
64
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040066 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040067 }
68
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 public static void setApplicationContext(Context context) {
70 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040071 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 }
73 sContext = context.getApplicationContext();
74 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040075
Daniel Sandlere4f98912013-06-25 15:13:26 -040076 private LauncherAppState() {
77 if (sContext == null) {
78 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040079 }
80
Daniel Sandlere4f98912013-06-25 15:13:26 -040081 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
84 MemoryTracker.startTrackingMe(sContext, "L");
85 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040086
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070088 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
90
Michael Jurka6e27f642013-12-10 13:40:30 +010091 recreateWidgetPreviewDb();
Daniel Sandlere4f98912013-06-25 15:13:26 -040092 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010093
94 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080095 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010096 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Kenny Guyed131872014-04-30 03:02:21 +010097 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext);
98 launcherApps.addOnAppsChangedListener(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099
100 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100101 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400102 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
103 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400104 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400105 filter = new IntentFilter();
106 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400107 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400108 filter = new IntentFilter();
109 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400110 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400111
112 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400113 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
115 mFavoritesObserver);
116 }
Michael Jurka6e27f642013-12-10 13:40:30 +0100117
118 public void recreateWidgetPreviewDb() {
119 if (mWidgetPreviewCacheDb != null) {
120 mWidgetPreviewCacheDb.close();
121 }
122 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
123 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124
125 /**
126 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
127 */
128 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400129 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130
Daniel Sandlere4f98912013-06-25 15:13:26 -0400131 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 resolver.unregisterContentObserver(mFavoritesObserver);
133 }
134
135 /**
136 * Receives notifications whenever the user favorites have changed.
137 */
138 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
139 @Override
140 public void onChange(boolean selfChange) {
141 // If the database has ever changed, then we really need to force a reload of the
142 // workspace on the next load
143 mModel.resetLoadedState(false, true);
144 mModel.startLoaderFromBackground();
145 }
146 };
147
148 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400149 if (mModel == null) {
150 throw new IllegalStateException("setLauncher() called before init()");
151 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 mModel.initialize(launcher);
153 return mModel;
154 }
155
156 IconCache getIconCache() {
157 return mIconCache;
158 }
159
160 LauncherModel getModel() {
161 return mModel;
162 }
163
Bjorn Bringert1307f632013-10-03 22:31:03 +0100164 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
165 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
166 }
167
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
169 return mWidgetPreviewCacheDb;
170 }
171
Daniel Sandlere4f98912013-06-25 15:13:26 -0400172 static void setLauncherProvider(LauncherProvider provider) {
173 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 }
175
Daniel Sandlere4f98912013-06-25 15:13:26 -0400176 static LauncherProvider getLauncherProvider() {
177 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400178 }
179
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400181 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 }
183
Winson Chung892c74d2013-08-22 16:15:50 -0700184 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
185 int width, int height,
186 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700187 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700188 mDynamicGrid = new DynamicGrid(context,
189 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700190 minWidth, minHeight, width, height,
191 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700192 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700193 }
194
Winson Chung5f8afe62013-08-12 16:19:28 -0700195 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700196 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700197 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700198 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700199 return grid;
200 }
Winson Chungb3800242013-10-24 11:01:54 -0700201 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700202 return mDynamicGrid;
203 }
204
Daniel Sandlere4f98912013-06-25 15:13:26 -0400205 public boolean isScreenLarge() {
206 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400207 }
208
Michael Jurka104c4562013-07-08 18:03:46 -0700209 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
210 public static boolean isScreenLarge(Resources res) {
211 return res.getBoolean(R.bool.is_large_tablet);
212 }
213
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400214 public static boolean isScreenLandscape(Context context) {
215 return context.getResources().getConfiguration().orientation ==
216 Configuration.ORIENTATION_LANDSCAPE;
217 }
218
Daniel Sandlere4f98912013-06-25 15:13:26 -0400219 public float getScreenDensity() {
220 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400221 }
222
Daniel Sandlere4f98912013-06-25 15:13:26 -0400223 public int getLongPressTimeout() {
224 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400225 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700226
Michael Jurkaa6a05472013-11-13 17:59:46 +0100227 public void onWallpaperChanged() {
228 mWallpaperChangedSinceLastCheck = true;
229 }
230
231 public boolean hasWallpaperChangedSinceLastCheck() {
232 boolean result = mWallpaperChangedSinceLastCheck;
233 mWallpaperChangedSinceLastCheck = false;
234 return result;
235 }
236
Winson Chung6e1c0d32013-10-25 15:24:24 -0700237 @Override
238 public void onAvailableSizeChanged(DeviceProfile grid) {
239 Utilities.setIconSize(grid.iconSizePx);
240 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800241
242 public static boolean isDisableAllApps() {
243 // Returns false on non-dogfood builds.
244 return getInstance().mBuildInfo.isDogfoodBuild() &&
245 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
246 }
Jorim Jaggieedb00a2014-01-13 13:45:07 -0800247
248 public static boolean isDogfoodBuild() {
249 return getInstance().mBuildInfo.isDogfoodBuild();
250 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -0500251
252 public void setPackageState(String pkgName, int state) {
253 if (DEBUG) Log.d(TAG, "setPackageState(" + pkgName + ", " + state + ")");
254 mModel.setPackageState(pkgName, state);
255 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400256}