blob: 5e41fcad054cb0c530b5b47eb2581762de72f662 [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
27import java.lang.ref.WeakReference;
28
Winson Chung6e1c0d32013-10-25 15:24:24 -070029public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks {
Winson Chung5f8afe62013-08-12 16:19:28 -070030 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040031 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
32
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080033 private final AppFilter mAppFilter;
34 private final BuildInfo mBuildInfo;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035 private LauncherModel mModel;
36 private IconCache mIconCache;
37 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040038 private boolean mIsScreenLarge;
39 private float mScreenDensity;
40 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010041 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040042
Daniel Sandlere4f98912013-06-25 15:13:26 -040043 private static WeakReference<LauncherProvider> sLauncherProvider;
44 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045
Daniel Sandlere4f98912013-06-25 15:13:26 -040046 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Winson Chung5f8afe62013-08-12 16:19:28 -070048 private DynamicGrid mDynamicGrid;
49
Daniel Sandlercc8befa2013-06-11 14:45:48 -040050 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040051 if (INSTANCE == null) {
52 INSTANCE = new LauncherAppState();
53 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 return INSTANCE;
55 }
56
Chris Wrend8fe6de2013-10-04 10:42:14 -040057 public static LauncherAppState getInstanceNoCreate() {
58 return INSTANCE;
59 }
60
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040062 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 }
64
Daniel Sandlere4f98912013-06-25 15:13:26 -040065 public static void setApplicationContext(Context context) {
66 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040067 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 }
69 sContext = context.getApplicationContext();
70 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040071
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 private LauncherAppState() {
73 if (sContext == null) {
74 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040075 }
76
Daniel Sandlere4f98912013-06-25 15:13:26 -040077 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040078
Daniel Sandlere4f98912013-06-25 15:13:26 -040079 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
80 MemoryTracker.startTrackingMe(sContext, "L");
81 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082
Daniel Sandlere4f98912013-06-25 15:13:26 -040083 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070084 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040085 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
86
87 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
88 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010089
90 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -080091 mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class));
Bjorn Bringert1307f632013-10-03 22:31:03 +010092 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040093
94 // Register intent receivers
95 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
96 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
97 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
98 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -040099 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400100 filter = new IntentFilter();
101 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
102 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
103 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
104 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400105 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400106 filter = new IntentFilter();
107 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400108 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400109 filter = new IntentFilter();
110 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400111 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400112
113 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400114 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400115 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
116 mFavoritesObserver);
117 }
118
119 /**
120 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
121 */
122 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400123 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400124
Daniel Sandlere4f98912013-06-25 15:13:26 -0400125 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 resolver.unregisterContentObserver(mFavoritesObserver);
127 }
128
129 /**
130 * Receives notifications whenever the user favorites have changed.
131 */
132 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
133 @Override
134 public void onChange(boolean selfChange) {
135 // If the database has ever changed, then we really need to force a reload of the
136 // workspace on the next load
137 mModel.resetLoadedState(false, true);
138 mModel.startLoaderFromBackground();
139 }
140 };
141
142 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400143 if (mModel == null) {
144 throw new IllegalStateException("setLauncher() called before init()");
145 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400146 mModel.initialize(launcher);
147 return mModel;
148 }
149
150 IconCache getIconCache() {
151 return mIconCache;
152 }
153
154 LauncherModel getModel() {
155 return mModel;
156 }
157
Bjorn Bringert1307f632013-10-03 22:31:03 +0100158 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
159 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
160 }
161
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
163 return mWidgetPreviewCacheDb;
164 }
165
Daniel Sandlere4f98912013-06-25 15:13:26 -0400166 static void setLauncherProvider(LauncherProvider provider) {
167 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 }
169
Daniel Sandlere4f98912013-06-25 15:13:26 -0400170 static LauncherProvider getLauncherProvider() {
171 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400172 }
173
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400174 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400175 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176 }
177
Winson Chung892c74d2013-08-22 16:15:50 -0700178 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
179 int width, int height,
180 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700181 if (mDynamicGrid == null) {
Winson Chungf7d45852013-10-10 18:57:15 -0700182 mDynamicGrid = new DynamicGrid(context,
183 context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700184 minWidth, minHeight, width, height,
185 availableWidth, availableHeight);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700186 mDynamicGrid.getDeviceProfile().addCallback(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700187 }
188
Winson Chung5f8afe62013-08-12 16:19:28 -0700189 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700190 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700191 grid.updateFromConfiguration(context, context.getResources(), width, height,
Winson Chung892c74d2013-08-22 16:15:50 -0700192 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700193 return grid;
194 }
Winson Chungb3800242013-10-24 11:01:54 -0700195 public DynamicGrid getDynamicGrid() {
Winson Chung5f8afe62013-08-12 16:19:28 -0700196 return mDynamicGrid;
197 }
198
Daniel Sandlere4f98912013-06-25 15:13:26 -0400199 public boolean isScreenLarge() {
200 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400201 }
202
Michael Jurka104c4562013-07-08 18:03:46 -0700203 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
204 public static boolean isScreenLarge(Resources res) {
205 return res.getBoolean(R.bool.is_large_tablet);
206 }
207
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400208 public static boolean isScreenLandscape(Context context) {
209 return context.getResources().getConfiguration().orientation ==
210 Configuration.ORIENTATION_LANDSCAPE;
211 }
212
Daniel Sandlere4f98912013-06-25 15:13:26 -0400213 public float getScreenDensity() {
214 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400215 }
216
Daniel Sandlere4f98912013-06-25 15:13:26 -0400217 public int getLongPressTimeout() {
218 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400219 }
Winson Chung6e1c0d32013-10-25 15:24:24 -0700220
Michael Jurkaa6a05472013-11-13 17:59:46 +0100221 public void onWallpaperChanged() {
222 mWallpaperChangedSinceLastCheck = true;
223 }
224
225 public boolean hasWallpaperChangedSinceLastCheck() {
226 boolean result = mWallpaperChangedSinceLastCheck;
227 mWallpaperChangedSinceLastCheck = false;
228 return result;
229 }
230
Winson Chung6e1c0d32013-10-25 15:24:24 -0700231 @Override
232 public void onAvailableSizeChanged(DeviceProfile grid) {
233 Utilities.setIconSize(grid.iconSizePx);
234 }
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800235
236 public static boolean isDisableAllApps() {
237 // Returns false on non-dogfood builds.
238 return getInstance().mBuildInfo.isDogfoodBuild() &&
239 Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY);
240 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400241}