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