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