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