blob: ed96310ac97e10a6ec2b14fad5c00ff788bfa57b [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;
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;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040041
Daniel Sandlere4f98912013-06-25 15:13:26 -040042 private static WeakReference<LauncherProvider> sLauncherProvider;
43 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Daniel Sandlere4f98912013-06-25 15:13:26 -040045 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040046
Winson Chung5f8afe62013-08-12 16:19:28 -070047 private DynamicGrid mDynamicGrid;
48
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049 public static LauncherAppState getInstance() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040050 if (INSTANCE == null) {
51 INSTANCE = new LauncherAppState();
52 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040053 return INSTANCE;
54 }
55
Daniel Sandlercc8befa2013-06-11 14:45:48 -040056 public Context getContext() {
Daniel Sandlere4f98912013-06-25 15:13:26 -040057 return sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040058 }
59
Daniel Sandlere4f98912013-06-25 15:13:26 -040060 public static void setApplicationContext(Context context) {
61 if (sContext != null) {
Daniel Sandlere060b0b2013-06-27 21:47:55 -040062 Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context);
Daniel Sandlere4f98912013-06-25 15:13:26 -040063 }
64 sContext = context.getApplicationContext();
65 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040066
Daniel Sandlere4f98912013-06-25 15:13:26 -040067 private LauncherAppState() {
68 if (sContext == null) {
69 throw new IllegalStateException("LauncherAppState inited before app context set");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040070 }
71
Daniel Sandlere4f98912013-06-25 15:13:26 -040072 Log.v(Launcher.TAG, "LauncherAppState inited");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040073
Daniel Sandlere4f98912013-06-25 15:13:26 -040074 if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) {
75 MemoryTracker.startTrackingMe(sContext, "L");
76 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040077
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 // set sIsScreenXLarge and mScreenDensity *before* creating icon cache
Michael Jurka104c4562013-07-08 18:03:46 -070079 mIsScreenLarge = isScreenLarge(sContext.getResources());
Daniel Sandlere4f98912013-06-25 15:13:26 -040080 mScreenDensity = sContext.getResources().getDisplayMetrics().density;
81
82 mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext);
83 mIconCache = new IconCache(sContext);
84 mModel = new LauncherModel(this, mIconCache);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085
86 // Register intent receivers
87 IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
88 filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
89 filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
90 filter.addDataScheme("package");
Daniel Sandlere4f98912013-06-25 15:13:26 -040091 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040092 filter = new IntentFilter();
93 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
94 filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
95 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
96 filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -040097 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098 filter = new IntentFilter();
99 filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400100 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400101 filter = new IntentFilter();
102 filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED);
Daniel Sandlere4f98912013-06-25 15:13:26 -0400103 sContext.registerReceiver(mModel, filter);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400104
105 // Register for changes to the favorites
Daniel Sandlere4f98912013-06-25 15:13:26 -0400106 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107 resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true,
108 mFavoritesObserver);
109 }
110
111 /**
112 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
113 */
114 public void onTerminate() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400115 sContext.unregisterReceiver(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116
Daniel Sandlere4f98912013-06-25 15:13:26 -0400117 ContentResolver resolver = sContext.getContentResolver();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400118 resolver.unregisterContentObserver(mFavoritesObserver);
119 }
120
121 /**
122 * Receives notifications whenever the user favorites have changed.
123 */
124 private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) {
125 @Override
126 public void onChange(boolean selfChange) {
127 // If the database has ever changed, then we really need to force a reload of the
128 // workspace on the next load
129 mModel.resetLoadedState(false, true);
130 mModel.startLoaderFromBackground();
131 }
132 };
133
134 LauncherModel setLauncher(Launcher launcher) {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400135 if (mModel == null) {
136 throw new IllegalStateException("setLauncher() called before init()");
137 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400138 mModel.initialize(launcher);
139 return mModel;
140 }
141
142 IconCache getIconCache() {
143 return mIconCache;
144 }
145
146 LauncherModel getModel() {
147 return mModel;
148 }
149
150 WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() {
151 return mWidgetPreviewCacheDb;
152 }
153
Daniel Sandlere4f98912013-06-25 15:13:26 -0400154 static void setLauncherProvider(LauncherProvider provider) {
155 sLauncherProvider = new WeakReference<LauncherProvider>(provider);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400156 }
157
Daniel Sandlere4f98912013-06-25 15:13:26 -0400158 static LauncherProvider getLauncherProvider() {
159 return sLauncherProvider.get();
Daniel Sandler924b9932013-06-12 00:38:25 -0400160 }
161
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 public static String getSharedPreferencesKey() {
Daniel Sandlere4f98912013-06-25 15:13:26 -0400163 return SHARED_PREFERENCES_KEY;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 }
165
Winson Chung892c74d2013-08-22 16:15:50 -0700166 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight,
167 int width, int height,
168 int availableWidth, int availableHeight) {
Winson Chung5f8afe62013-08-12 16:19:28 -0700169 if (mDynamicGrid == null) {
170 mDynamicGrid = new DynamicGrid(context.getResources(),
Winson Chung892c74d2013-08-22 16:15:50 -0700171 minWidth, minHeight, width, height,
172 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700173 }
174
Winson Chung5f8afe62013-08-12 16:19:28 -0700175 // Update the icon size
Winson Chung892c74d2013-08-22 16:15:50 -0700176 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
Winson Chung5f8afe62013-08-12 16:19:28 -0700177 Utilities.setIconSize(grid.iconSizePx);
Winson Chung892c74d2013-08-22 16:15:50 -0700178 grid.updateFromConfiguration(context.getResources(), width, height,
179 availableWidth, availableHeight);
Winson Chung5f8afe62013-08-12 16:19:28 -0700180 return grid;
181 }
182 DynamicGrid getDynamicGrid() {
183 return mDynamicGrid;
184 }
185
Daniel Sandlere4f98912013-06-25 15:13:26 -0400186 public boolean isScreenLarge() {
187 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400188 }
189
Michael Jurka104c4562013-07-08 18:03:46 -0700190 // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker
191 public static boolean isScreenLarge(Resources res) {
192 return res.getBoolean(R.bool.is_large_tablet);
193 }
194
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400195 public static boolean isScreenLandscape(Context context) {
196 return context.getResources().getConfiguration().orientation ==
197 Configuration.ORIENTATION_LANDSCAPE;
198 }
199
Daniel Sandlere4f98912013-06-25 15:13:26 -0400200 public float getScreenDensity() {
201 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400202 }
203
Daniel Sandlere4f98912013-06-25 15:13:26 -0400204 public int getLongPressTimeout() {
205 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 }
207}