blob: 0c577e548ebfebc26da8db6c42ae0a64dbe20486 [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;
22import android.database.ContentObserver;
23import android.os.Handler;
Winson Chung5f8afe62013-08-12 16:19:28 -070024import android.provider.Settings;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040025import android.util.Log;
Winson Chung5f8afe62013-08-12 16:19:28 -070026import android.view.Display;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040027
28import java.lang.ref.WeakReference;
29
30public class LauncherAppState {
Winson Chung5f8afe62013-08-12 16:19:28 -070031 private static final String TAG = "LauncherAppState";
Daniel Sandlere4f98912013-06-25 15:13:26 -040032 private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs";
33
Daniel Sandlercc8befa2013-06-11 14:45:48 -040034 private LauncherModel mModel;
35 private IconCache mIconCache;
36 private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb;
Daniel Sandlere4f98912013-06-25 15:13:26 -040037 private boolean mIsScreenLarge;
38 private float mScreenDensity;
39 private int mLongPressTimeout = 300;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040040
Daniel Sandlere4f98912013-06-25 15:13:26 -040041 private static WeakReference<LauncherProvider> sLauncherProvider;
42 private static Context sContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040043
Daniel Sandlere4f98912013-06-25 15:13:26 -040044 private static Object mLock = new Object();
45 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
Winson Chung5f8afe62013-08-12 16:19:28 -070079 mIsScreenLarge = sContext.getResources().getBoolean(R.bool.is_large_tablet);
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 Chung5f8afe62013-08-12 16:19:28 -0700166 DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight, int width, int height) {
167 boolean created = false;
168 if (mDynamicGrid == null) {
169 mDynamicGrid = new DynamicGrid(context.getResources(),
170 minWidth, minHeight, width, height);
171 created = true;
172 }
173
174 DeviceProfile grid = mDynamicGrid.getDeviceProfile();
175 if (created) {
176 LauncherModel.updateWorkspaceLayoutCells((int) grid.numColumns, (int) grid.numRows);
177 }
178 // Update the icon size
179 Utilities.setIconSize(grid.iconSizePx);
180 grid.updateFromConfiguration(context.getResources(), width, height);
181 return grid;
182 }
183 DynamicGrid getDynamicGrid() {
184 return mDynamicGrid;
185 }
186
Daniel Sandlere4f98912013-06-25 15:13:26 -0400187 public boolean isScreenLarge() {
188 return mIsScreenLarge;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400189 }
190
191 public static boolean isScreenLandscape(Context context) {
192 return context.getResources().getConfiguration().orientation ==
193 Configuration.ORIENTATION_LANDSCAPE;
194 }
195
Daniel Sandlere4f98912013-06-25 15:13:26 -0400196 public float getScreenDensity() {
197 return mScreenDensity;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400198 }
199
Daniel Sandlere4f98912013-06-25 15:13:26 -0400200 public int getLongPressTimeout() {
201 return mLongPressTimeout;
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400202 }
203}