Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.launcher3; |
| 18 | |
| 19 | import android.app.SearchManager; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 20 | import android.content.*; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 21 | import android.content.res.Configuration; |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 22 | import android.content.res.Resources; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 23 | import android.database.ContentObserver; |
| 24 | import android.os.Handler; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 25 | import android.util.Log; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 26 | |
| 27 | import java.lang.ref.WeakReference; |
| 28 | |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 29 | public class LauncherAppState implements DeviceProfile.DeviceProfileCallbacks { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 30 | private static final String TAG = "LauncherAppState"; |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 31 | private static final String SHARED_PREFERENCES_KEY = "com.android.launcher3.prefs"; |
| 32 | |
Chris Wren | 1a1fdf4 | 2014-04-15 13:57:15 -0400 | [diff] [blame] | 33 | private static final boolean DEBUG = false; |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 34 | |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 35 | private final AppFilter mAppFilter; |
| 36 | private final BuildInfo mBuildInfo; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 37 | private LauncherModel mModel; |
| 38 | private IconCache mIconCache; |
| 39 | private WidgetPreviewLoader.CacheDb mWidgetPreviewCacheDb; |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 40 | private boolean mIsScreenLarge; |
| 41 | private float mScreenDensity; |
| 42 | private int mLongPressTimeout = 300; |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 43 | private boolean mWallpaperChangedSinceLastCheck; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 44 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 45 | private static WeakReference<LauncherProvider> sLauncherProvider; |
| 46 | private static Context sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 47 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 48 | private static LauncherAppState INSTANCE; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 49 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 50 | private DynamicGrid mDynamicGrid; |
| 51 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 52 | public static LauncherAppState getInstance() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 53 | if (INSTANCE == null) { |
| 54 | INSTANCE = new LauncherAppState(); |
| 55 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 56 | return INSTANCE; |
| 57 | } |
| 58 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 59 | public static LauncherAppState getInstanceNoCreate() { |
| 60 | return INSTANCE; |
| 61 | } |
| 62 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 63 | public Context getContext() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 64 | return sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 65 | } |
| 66 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 67 | public static void setApplicationContext(Context context) { |
| 68 | if (sContext != null) { |
Daniel Sandler | e060b0b | 2013-06-27 21:47:55 -0400 | [diff] [blame] | 69 | Log.w(Launcher.TAG, "setApplicationContext called twice! old=" + sContext + " new=" + context); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 70 | } |
| 71 | sContext = context.getApplicationContext(); |
| 72 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 73 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 74 | private LauncherAppState() { |
| 75 | if (sContext == null) { |
| 76 | throw new IllegalStateException("LauncherAppState inited before app context set"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 79 | Log.v(Launcher.TAG, "LauncherAppState inited"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 80 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 81 | if (sContext.getResources().getBoolean(R.bool.debug_memory_enabled)) { |
| 82 | MemoryTracker.startTrackingMe(sContext, "L"); |
| 83 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 84 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 85 | // set sIsScreenXLarge and mScreenDensity *before* creating icon cache |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 86 | mIsScreenLarge = isScreenLarge(sContext.getResources()); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 87 | mScreenDensity = sContext.getResources().getDisplayMetrics().density; |
| 88 | |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 89 | recreateWidgetPreviewDb(); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 90 | mIconCache = new IconCache(sContext); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 91 | |
| 92 | mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 93 | mBuildInfo = BuildInfo.loadByName(sContext.getString(R.string.build_info_class)); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 94 | mModel = new LauncherModel(this, mIconCache, mAppFilter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 95 | |
| 96 | // Register intent receivers |
| 97 | IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 98 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 99 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 100 | filter.addDataScheme("package"); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 101 | sContext.registerReceiver(mModel, filter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 102 | filter = new IntentFilter(); |
| 103 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 104 | filter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| 105 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
| 106 | filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 107 | sContext.registerReceiver(mModel, filter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 108 | filter = new IntentFilter(); |
| 109 | filter.addAction(SearchManager.INTENT_GLOBAL_SEARCH_ACTIVITY_CHANGED); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 110 | sContext.registerReceiver(mModel, filter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 111 | filter = new IntentFilter(); |
| 112 | filter.addAction(SearchManager.INTENT_ACTION_SEARCHABLES_CHANGED); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 113 | sContext.registerReceiver(mModel, filter); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 114 | |
| 115 | // Register for changes to the favorites |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 116 | ContentResolver resolver = sContext.getContentResolver(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 117 | resolver.registerContentObserver(LauncherSettings.Favorites.CONTENT_URI, true, |
| 118 | mFavoritesObserver); |
| 119 | } |
Michael Jurka | 6e27f64 | 2013-12-10 13:40:30 +0100 | [diff] [blame] | 120 | |
| 121 | public void recreateWidgetPreviewDb() { |
| 122 | if (mWidgetPreviewCacheDb != null) { |
| 123 | mWidgetPreviewCacheDb.close(); |
| 124 | } |
| 125 | mWidgetPreviewCacheDb = new WidgetPreviewLoader.CacheDb(sContext); |
| 126 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 130 | */ |
| 131 | public void onTerminate() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 132 | sContext.unregisterReceiver(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 133 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 134 | ContentResolver resolver = sContext.getContentResolver(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 135 | resolver.unregisterContentObserver(mFavoritesObserver); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Receives notifications whenever the user favorites have changed. |
| 140 | */ |
| 141 | private final ContentObserver mFavoritesObserver = new ContentObserver(new Handler()) { |
| 142 | @Override |
| 143 | public void onChange(boolean selfChange) { |
| 144 | // If the database has ever changed, then we really need to force a reload of the |
| 145 | // workspace on the next load |
| 146 | mModel.resetLoadedState(false, true); |
| 147 | mModel.startLoaderFromBackground(); |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | LauncherModel setLauncher(Launcher launcher) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 152 | if (mModel == null) { |
| 153 | throw new IllegalStateException("setLauncher() called before init()"); |
| 154 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 155 | mModel.initialize(launcher); |
| 156 | return mModel; |
| 157 | } |
| 158 | |
| 159 | IconCache getIconCache() { |
| 160 | return mIconCache; |
| 161 | } |
| 162 | |
| 163 | LauncherModel getModel() { |
| 164 | return mModel; |
| 165 | } |
| 166 | |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 167 | boolean shouldShowAppOrWidgetProvider(ComponentName componentName) { |
| 168 | return mAppFilter == null || mAppFilter.shouldShowApp(componentName); |
| 169 | } |
| 170 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 171 | WidgetPreviewLoader.CacheDb getWidgetPreviewCacheDb() { |
| 172 | return mWidgetPreviewCacheDb; |
| 173 | } |
| 174 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 175 | static void setLauncherProvider(LauncherProvider provider) { |
| 176 | sLauncherProvider = new WeakReference<LauncherProvider>(provider); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 177 | } |
| 178 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 179 | static LauncherProvider getLauncherProvider() { |
| 180 | return sLauncherProvider.get(); |
Daniel Sandler | 924b993 | 2013-06-12 00:38:25 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 183 | public static String getSharedPreferencesKey() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 184 | return SHARED_PREFERENCES_KEY; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 187 | DeviceProfile initDynamicGrid(Context context, int minWidth, int minHeight, |
| 188 | int width, int height, |
| 189 | int availableWidth, int availableHeight) { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 190 | if (mDynamicGrid == null) { |
Winson Chung | f7d4585 | 2013-10-10 18:57:15 -0700 | [diff] [blame] | 191 | mDynamicGrid = new DynamicGrid(context, |
| 192 | context.getResources(), |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 193 | minWidth, minHeight, width, height, |
| 194 | availableWidth, availableHeight); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 195 | mDynamicGrid.getDeviceProfile().addCallback(this); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 196 | } |
| 197 | |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 198 | // Update the icon size |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 199 | DeviceProfile grid = mDynamicGrid.getDeviceProfile(); |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 200 | grid.updateFromConfiguration(context, context.getResources(), width, height, |
Winson Chung | 892c74d | 2013-08-22 16:15:50 -0700 | [diff] [blame] | 201 | availableWidth, availableHeight); |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 202 | return grid; |
| 203 | } |
Winson Chung | b380024 | 2013-10-24 11:01:54 -0700 | [diff] [blame] | 204 | public DynamicGrid getDynamicGrid() { |
Winson Chung | 5f8afe6 | 2013-08-12 16:19:28 -0700 | [diff] [blame] | 205 | return mDynamicGrid; |
| 206 | } |
| 207 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 208 | public boolean isScreenLarge() { |
| 209 | return mIsScreenLarge; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 210 | } |
| 211 | |
Michael Jurka | 104c456 | 2013-07-08 18:03:46 -0700 | [diff] [blame] | 212 | // Need a version that doesn't require an instance of LauncherAppState for the wallpaper picker |
| 213 | public static boolean isScreenLarge(Resources res) { |
| 214 | return res.getBoolean(R.bool.is_large_tablet); |
| 215 | } |
| 216 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 217 | public static boolean isScreenLandscape(Context context) { |
| 218 | return context.getResources().getConfiguration().orientation == |
| 219 | Configuration.ORIENTATION_LANDSCAPE; |
| 220 | } |
| 221 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 222 | public float getScreenDensity() { |
| 223 | return mScreenDensity; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 226 | public int getLongPressTimeout() { |
| 227 | return mLongPressTimeout; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 228 | } |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 229 | |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 230 | public void onWallpaperChanged() { |
| 231 | mWallpaperChangedSinceLastCheck = true; |
| 232 | } |
| 233 | |
| 234 | public boolean hasWallpaperChangedSinceLastCheck() { |
| 235 | boolean result = mWallpaperChangedSinceLastCheck; |
| 236 | mWallpaperChangedSinceLastCheck = false; |
| 237 | return result; |
| 238 | } |
| 239 | |
Winson Chung | 6e1c0d3 | 2013-10-25 15:24:24 -0700 | [diff] [blame] | 240 | @Override |
| 241 | public void onAvailableSizeChanged(DeviceProfile grid) { |
| 242 | Utilities.setIconSize(grid.iconSizePx); |
| 243 | } |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 244 | |
| 245 | public static boolean isDisableAllApps() { |
| 246 | // Returns false on non-dogfood builds. |
| 247 | return getInstance().mBuildInfo.isDogfoodBuild() && |
| 248 | Launcher.isPropertyEnabled(Launcher.DISABLE_ALL_APPS_PROPERTY); |
| 249 | } |
Jorim Jaggi | eedb00a | 2014-01-13 13:45:07 -0800 | [diff] [blame] | 250 | |
| 251 | public static boolean isDogfoodBuild() { |
| 252 | return getInstance().mBuildInfo.isDogfoodBuild(); |
| 253 | } |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 254 | |
| 255 | public void setPackageState(String pkgName, int state) { |
| 256 | if (DEBUG) Log.d(TAG, "setPackageState(" + pkgName + ", " + state + ")"); |
| 257 | mModel.setPackageState(pkgName, state); |
| 258 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 259 | } |