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