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 | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 19 | import android.content.ContentProviderClient; |
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; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 23 | import android.os.Looper; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 24 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 25 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 26 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 27 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 28 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 29 | import com.android.launcher3.config.ProviderConfig; |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 30 | import com.android.launcher3.dynamicui.ExtractionUtils; |
Sunny Goyal | ae50284 | 2016-06-17 08:43:56 -0700 | [diff] [blame] | 31 | import com.android.launcher3.util.ConfigMonitor; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 32 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 322d556 | 2015-06-25 19:35:49 -0700 | [diff] [blame] | 33 | import com.android.launcher3.util.TestingUtils; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 34 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 35 | import java.util.concurrent.Callable; |
| 36 | import java.util.concurrent.ExecutionException; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 37 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 38 | public class LauncherAppState { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 39 | |
Sunny Goyal | e26d100 | 2016-06-20 14:52:14 -0700 | [diff] [blame] | 40 | public static final boolean PROFILE_STARTUP = ProviderConfig.IS_DOGFOOD_BUILD; |
| 41 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 42 | // We do not need any synchronization for this variable as its only written on UI thread. |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 43 | private static LauncherAppState INSTANCE; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 44 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 45 | private final Context mContext; |
| 46 | private final LauncherModel mModel; |
| 47 | private final IconCache mIconCache; |
| 48 | private final WidgetPreviewLoader mWidgetCache; |
| 49 | private final InvariantDeviceProfile mInvariantDeviceProfile; |
Sunny Goyal | 7779d62 | 2015-06-11 16:18:39 -0700 | [diff] [blame] | 50 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 51 | |
| 52 | public static LauncherAppState getInstance(final Context context) { |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 53 | if (INSTANCE == null) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 54 | if (Looper.myLooper() == Looper.getMainLooper()) { |
| 55 | INSTANCE = new LauncherAppState(context.getApplicationContext()); |
| 56 | } else { |
| 57 | try { |
| 58 | return new MainThreadExecutor().submit(new Callable<LauncherAppState>() { |
| 59 | @Override |
| 60 | public LauncherAppState call() throws Exception { |
| 61 | return LauncherAppState.getInstance(context); |
| 62 | } |
| 63 | }).get(); |
| 64 | } catch (InterruptedException|ExecutionException e) { |
| 65 | throw new RuntimeException(e); |
| 66 | } |
| 67 | } |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 68 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 69 | return INSTANCE; |
| 70 | } |
| 71 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 72 | public static LauncherAppState getInstanceNoCreate() { |
| 73 | return INSTANCE; |
| 74 | } |
| 75 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 76 | public Context getContext() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 77 | return mContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 78 | } |
| 79 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 80 | private LauncherAppState(Context context) { |
| 81 | if (getLocalProvider(context) == null) { |
| 82 | throw new RuntimeException( |
| 83 | "Initializing LauncherAppState in the absence of LauncherProvider"); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 84 | } |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 85 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 86 | Preconditions.assertUIThread(); |
| 87 | mContext = context; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 88 | |
Sunny Goyal | 322d556 | 2015-06-25 19:35:49 -0700 | [diff] [blame] | 89 | if (TestingUtils.MEMORY_DUMP_ENABLED) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 90 | TestingUtils.startTrackingMemory(mContext); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 91 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 92 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 93 | mInvariantDeviceProfile = new InvariantDeviceProfile(mContext); |
| 94 | mIconCache = new IconCache(mContext, mInvariantDeviceProfile); |
| 95 | mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); |
Bjorn Bringert | 1307f63 | 2013-10-03 22:31:03 +0100 | [diff] [blame] | 96 | |
Sunny Goyal | ab121c1 | 2016-12-02 19:29:43 +0530 | [diff] [blame] | 97 | mModel = new LauncherModel(this, mIconCache, |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 98 | Utilities.getOverrideObject(AppFilter.class, mContext, R.string.app_filter_class)); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 99 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 100 | LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 101 | |
| 102 | // Register intent receivers |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 103 | IntentFilter filter = new IntentFilter(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 104 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 105 | // For handling managed profiles |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 106 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 107 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 108 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 109 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 110 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 111 | // For extracting colors from the wallpaper |
Sunny Goyal | f5e3744 | 2016-11-02 10:31:24 -0700 | [diff] [blame] | 112 | if (Utilities.ATLEAST_NOUGAT) { |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 113 | // TODO: add a broadcast entry to the manifest for pre-N. |
| 114 | filter.addAction(Intent.ACTION_WALLPAPER_CHANGED); |
| 115 | } |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 116 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 117 | mContext.registerReceiver(mModel, filter); |
| 118 | UserManagerCompat.getInstance(mContext).enableAndResetCache(); |
| 119 | new ConfigMonitor(mContext).register(); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 120 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 121 | ExtractionUtils.startColorExtractionServiceIfNecessary(mContext); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 122 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 123 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 124 | /** |
| 125 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 126 | */ |
| 127 | public void onTerminate() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 128 | mContext.unregisterReceiver(mModel); |
| 129 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 130 | launcherApps.removeOnAppsChangedCallback(mModel); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 131 | PackageInstallerCompat.getInstance(mContext).onStop(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 135 | * Reloads the workspace items from the DB and re-binds the workspace. This should generally |
| 136 | * not be called as DB updates are automatically followed by UI update |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 137 | */ |
Sunny Goyal | 1d4a2df | 2015-03-30 11:11:46 -0700 | [diff] [blame] | 138 | public void reloadWorkspace() { |
| 139 | mModel.resetLoadedState(false, true); |
| 140 | mModel.startLoaderFromBackground(); |
| 141 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 142 | |
| 143 | LauncherModel setLauncher(Launcher launcher) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 144 | getLocalProvider(mContext).setLauncherProviderChangeListener(launcher); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 145 | mModel.initialize(launcher); |
| 146 | return mModel; |
| 147 | } |
| 148 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 149 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 150 | return mIconCache; |
| 151 | } |
| 152 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 153 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 154 | return mModel; |
| 155 | } |
| 156 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 157 | public WidgetPreviewLoader getWidgetCache() { |
| 158 | return mWidgetCache; |
| 159 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 160 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 161 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 162 | return mInvariantDeviceProfile; |
| 163 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
| 166 | * Shorthand for {@link #getInvariantDeviceProfile()} |
| 167 | */ |
| 168 | public static InvariantDeviceProfile getIDP(Context context) { |
| 169 | return LauncherAppState.getInstance(context).getInvariantDeviceProfile(); |
| 170 | } |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame^] | 171 | |
| 172 | private static LauncherProvider getLocalProvider(Context context) { |
| 173 | try (ContentProviderClient cl = context.getContentResolver() |
| 174 | .acquireContentProviderClient(LauncherProvider.AUTHORITY)) { |
| 175 | return (LauncherProvider) cl.getLocalContentProvider(); |
| 176 | } |
| 177 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 178 | } |