blob: 8b34ffbe0be5fee5048a81d366f1ad19e48891eb [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
31public class LauncherAppState {
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;
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));
91 mModel = new LauncherModel(this, mIconCache, mAppFilter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092
93 // Register intent receivers
94 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
95 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
96 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
97 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -040098 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040099 filter = new IntentFilter();
100 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
101 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
102 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 }
117
118 /**
119 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
120 */
121 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400122 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400123
Daniel Sandlere4f98912013-06-25 15:13:26 -0400124 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400125 resolver.unregisterContentObserver(mFavoritesObserver);
126 }
127
128 /**
129 * Receives notifications whenever the user favorites have changed.
130 */
131 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
132 @Override
133 public void onChange(boolean selfChange) {
134 // If the database has ever changed, then we really need to force a reload of the
135 // workspace on the next load
136 mModel.resetLoadedState(false, true);
137 mModel.startLoaderFromBackground();
138 }
139 };
140
141 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400142 if (mModel == null) {
143 throw new IllegalStateException("setLauncher() called before init()");
144 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 mModel.initialize(launcher);
146 return mModel;
147 }
148
149 IconCache getIconCache() {
150 return mIconCache;
151 }
152
153 LauncherModel getModel() {
154 return mModel;
155 }
156
Bjorn Bringert1307f632013-10-03 22:31:03 +0100157 boolean shouldShowAppOrWidgetProvider(ComponentName componentName) {
158 return mAppFilter == null || mAppFilter.shouldShowApp(componentName);
159 }
160
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400161 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
162 return mWidgetPreviewCacheDb;
163 }
164
Daniel Sandlere4f98912013-06-25 15:13:26 -0400165 static void setLauncherProvider(LauncherProvider provider) {
166 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400167 }
168
Daniel Sandlere4f98912013-06-25 15:13:26 -0400169 static LauncherProvider getLauncherProvider() {
170 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400171 }
172
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400173 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400174 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400175 }
176
Winson Chung892c74d2013-08-22 16:15:50 -0700177 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
178 int width, int height,
179 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700180 if (mDynamicGrid == null) {
181 mDynamicGrid = new DynamicGrid(context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700182 minWidth, minHeight, width, height,
183 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700184 }
185
Winson Chung5f8afe62013-08-12 16:19:28 -0700186 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700187 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -0700188 Utilities.setIconSize(grid.iconSizePx);
Winson Chung892c74d2013-08-22 16:15:50 -0700189 grid.updateFromConfiguration(context.getResources(), width, height,
190 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700191 return grid;
192 }
193 DynamicGrid getDynamicGrid() {
194 return mDynamicGrid;
195 }
196
Daniel Sandlere4f98912013-06-25 15:13:26 -0400197 public boolean isScreenLarge() {
198 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400199 }
200
Michael Jurka104c4562013-07-08 18:03:46 -0700201 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
202 public static boolean isScreenLarge(Resources res) {
203 return res.getBoolean(R.bool.is_large_tablet);
204 }
205
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 public static boolean isScreenLandscape(Context context) {
207 return context.getResources().getConfiguration().orientation ==
208 Configuration.ORIENTATION_LANDSCAPE;
209 }
210
Daniel Sandlere4f98912013-06-25 15:13:26 -0400211 public float getScreenDensity() {
212 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 }
214
Daniel Sandlere4f98912013-06-25 15:13:26 -0400215 public int getLongPressTimeout() {
216 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400217 }
218}