blob: 5daa845f34ff712d7270cb750e59a28b0e018583 [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;
Winson Chung5f8afe62013-08-12 16:19:28 -070025import android.provider.Settings;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040026import android.util.Log;
Winson Chung5f8afe62013-08-12 16:19:28 -070027import android.view.Display;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040028
29import 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
Daniel Sandlercc8befa2013-06-11 14:45:48 -040035 private LauncherModel mModel;
36 private IconCache mIconCache;
Bjorn Bringert1307f632013-10-03 22:31:03 +010037 private AppFilter mAppFilter;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040038 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040039 private boolean mIsScreenLarge;
40 private float mScreenDensity;
41 private int mLongPressTimeout = 300;
Michael Jurkaa6a05472013-11-13 17:59:46 +010042 private boolean mWallpaperChangedSinceLastCheck;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040043
Daniel Sandlere4f98912013-06-25 15:13:26 -040044 private static WeakReference<LauncherProvider> sLauncherProvider;
45 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Daniel Sandlere4f98912013-06-25 15:13:26 -040047 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040048
Winson Chung5f8afe62013-08-12 16:19:28 -070049 private DynamicGrid mDynamicGrid;
50
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040052 if (INSTANCE == null) {
53 INSTANCE = new LauncherAppState();
54 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055 return INSTANCE;
56 }
57
Chris Wrend8fe6de2013-10-04 10:42:14 -040058 public static LauncherAppState getInstanceNoCreate() {
59 return INSTANCE;
60 }
61
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040063 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 }
65
Daniel Sandlere4f98912013-06-25 15:13:26 -040066 public static void setApplicationContext(Context context) {
67 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040068 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 }
70 sContext = context.getApplicationContext();
71 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072
Daniel Sandlere4f98912013-06-25 15:13:26 -040073 private LauncherAppState() {
74 if (sContext == null) {
75 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040076 }
77
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040079
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
81 MemoryTracker.startTrackingMe(sContext, "L");
82 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070085 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040086 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
87
88 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
89 mIconCache = new IconCache(sContext);
Bjorn Bringert1307f632013-10-03 22:31:03 +010090
91 mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class));
92 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 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400235}