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