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 | |
Sunny Goyal | 4b17147 | 2015-08-19 12:43:27 -0700 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 23 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 24 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 25 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 26 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 27 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 28 | import com.android.launcher3.config.ProviderConfig; |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 29 | import com.android.launcher3.dynamicui.ExtractionUtils; |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 30 | import com.android.launcher3.logging.FileLog; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 31 | import com.android.launcher3.shortcuts.DeepShortcutManager; |
| 32 | import com.android.launcher3.shortcuts.ShortcutCache; |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.ConfigMonitor; |
Sunny Goyal | 322d556 | 2015-06-25 19:35:49 -0700 | [diff] [blame] | 34 | import com.android.launcher3.util.TestingUtils; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 35 | import com.android.launcher3.util.Thunk; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 36 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 37 | import java.lang.ref.WeakReference; |
| 38 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 39 | public class LauncherAppState { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 40 | |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 41 | public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD; |
| 42 | |
Nilesh Agrawal | 16f3ea8 | 2014-01-09 17:14:01 -0800 | [diff] [blame] | 43 | private final AppFilter mAppFilter; |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 44 | @Thunk final LauncherModel mModel; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 45 | private final IconCache mIconCache; |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 46 | private final WidgetPreviewLoader mWidgetCache; |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 47 | private final DeepShortcutManager mDeepShortcutManager; |
Sunny Goyal | e0f58d7 | 2014-11-10 18:05:31 -0800 | [diff] [blame] | 48 | |
Sunny Goyal | 4b17147 | 2015-08-19 12:43:27 -0700 | [diff] [blame] | 49 | @Thunk boolean mWallpaperChangedSinceLastCheck; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 50 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 51 | private static WeakReference<LauncherProvider> sLauncherProvider; |
| 52 | private static Context sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 53 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 54 | private static LauncherAppState INSTANCE; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 55 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 56 | private InvariantDeviceProfile mInvariantDeviceProfile; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 57 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 58 | public static LauncherAppState getInstance() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 59 | if (INSTANCE == null) { |
| 60 | INSTANCE = new LauncherAppState(); |
| 61 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 62 | return INSTANCE; |
| 63 | } |
| 64 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 65 | public static LauncherAppState getInstanceNoCreate() { |
| 66 | return INSTANCE; |
| 67 | } |
| 68 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 69 | public Context getContext() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 70 | return sContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 73 | static void setLauncherProvider(LauncherProvider provider) { |
| 74 | if (sLauncherProvider != null) { |
| 75 | Log.w(Launcher.TAG, "setLauncherProvider called twice! old=" + |
| 76 | sLauncherProvider.get() + " new=" + provider); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 77 | } |
Sunny Goyal | d3849d1 | 2015-10-29 10:28:32 -0700 | [diff] [blame] | 78 | sLauncherProvider = new WeakReference<>(provider); |
| 79 | |
| 80 | // The content provider exists for the entire duration of the launcher main process and |
| 81 | // is the first component to get created. Initializing application context here ensures |
| 82 | // that LauncherAppState always exists in the main process. |
| 83 | sContext = provider.getContext().getApplicationContext(); |
Sunny Goyal | 713edfc | 2016-05-06 09:58:34 -0700 | [diff] [blame] | 84 | FileLog.setDir(sContext.getFilesDir()); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 85 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 86 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 87 | private LauncherAppState() { |
| 88 | if (sContext == null) { |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 89 | throw new IllegalStateException("LauncherAppState initiated before app context set"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 90 | } |
| 91 | |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 92 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 93 | |
Sunny Goyal | 322d556 | 2015-06-25 19:35:49 -0700 | [diff] [blame] | 94 | if (TestingUtils.MEMORY_DUMP_ENABLED) { |
| 95 | TestingUtils.startTrackingMemory(sContext); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 96 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 97 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 98 | mInvariantDeviceProfile = new InvariantDeviceProfile(sContext); |
| 99 | mIconCache = new IconCache(sContext, mInvariantDeviceProfile); |
Sunny Goyal | 383c507 | 2015-06-12 21:18:53 -0700 | [diff] [blame] | 100 | mWidgetCache = new WidgetPreviewLoader(sContext, mIconCache); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 101 | mDeepShortcutManager = new DeepShortcutManager(sContext, new ShortcutCache()); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 102 | |
| 103 | mAppFilter = AppFilter.loadByName(sContext.getString(R.string.app_filter_class)); |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 104 | mModel = new LauncherModel(this, mIconCache, mAppFilter, mDeepShortcutManager); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 105 | |
| 106 | LauncherAppsCompat.getInstance(sContext).addOnAppsChangedCallback(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 107 | |
| 108 | // Register intent receivers |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 109 | IntentFilter filter = new IntentFilter(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 110 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 111 | // For handling managed profiles |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 112 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 113 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 114 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 115 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 116 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 117 | // For extracting colors from the wallpaper |
| 118 | if (Utilities.isNycOrAbove()) { |
| 119 | // TODO: add a broadcast entry to the manifest for pre-N. |
| 120 | filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); |
| 121 | } |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 122 | |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 123 | sContext.registerReceiver(mModel, filter); |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 124 | UserManagerCompat.getInstance(sContext).enableAndResetCache(); |
Sunny Goyal | 4b17147 | 2015-08-19 12:43:27 -0700 | [diff] [blame] | 125 | if (!Utilities.ATLEAST_KITKAT) { |
| 126 | sContext.registerReceiver(new BroadcastReceiver() { |
| 127 | |
| 128 | @Override |
| 129 | public void onReceive(Context context, Intent intent) { |
| 130 | mWallpaperChangedSinceLastCheck = true; |
| 131 | } |
| 132 | }, new IntentFilter(Intent.ACTION_WALLPAPER_CHANGED)); |
| 133 | } |
Sunny Goyal | a50a419 | 2015-12-11 09:50:49 -0800 | [diff] [blame] | 134 | new ConfigMonitor(sContext).register(); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 135 | |
| 136 | ExtractionUtils.startColorExtractionServiceIfNecessary(sContext); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 137 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 138 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 139 | /** |
| 140 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 141 | */ |
| 142 | public void onTerminate() { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 143 | sContext.unregisterReceiver(mModel); |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 144 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(sContext); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 145 | launcherApps.removeOnAppsChangedCallback(mModel); |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 146 | PackageInstallerCompat.getInstance(sContext).onStop(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 150 | * Reloads the workspace items from the DB and re-binds the workspace. This should generally |
| 151 | * not be called as DB updates are automatically followed by UI update |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 152 | */ |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 153 | public void reloadWorkspace() { |
| 154 | mModel.resetLoadedState(false, true); |
| 155 | mModel.startLoaderFromBackground(); |
| 156 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 157 | |
| 158 | LauncherModel setLauncher(Launcher launcher) { |
Sunny Goyal | d249748 | 2015-09-22 18:24:19 -0700 | [diff] [blame] | 159 | sLauncherProvider.get().setLauncherProviderChangeListener(launcher); |
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 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 168 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 169 | return mModel; |
| 170 | } |
| 171 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 172 | public WidgetPreviewLoader getWidgetCache() { |
| 173 | return mWidgetCache; |
| 174 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 175 | |
Tony Wickham | bfbf7f9 | 2016-05-19 11:19:39 -0700 | [diff] [blame] | 176 | public DeepShortcutManager getShortcutManager() { |
| 177 | return mDeepShortcutManager; |
| 178 | } |
| 179 | |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 180 | public boolean hasWallpaperChangedSinceLastCheck() { |
| 181 | boolean result = mWallpaperChangedSinceLastCheck; |
| 182 | mWallpaperChangedSinceLastCheck = false; |
| 183 | return result; |
| 184 | } |
| 185 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 186 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 187 | return mInvariantDeviceProfile; |
| 188 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 189 | } |