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