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 | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.Utilities.getDevicePrefs; |
| 20 | import static com.android.launcher3.config.FeatureFlags.ENABLE_THEMED_ICONS; |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 21 | import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; |
Jon Miranda | ec1277e | 2021-03-25 10:41:54 -0400 | [diff] [blame] | 22 | import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 23 | |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 27 | import android.content.pm.LauncherApps; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 28 | import android.os.UserHandle; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 29 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 30 | |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 31 | import androidx.annotation.Nullable; |
| 32 | |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 33 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 34 | import com.android.launcher3.graphics.IconShape; |
Sunny Goyal | f840f10 | 2018-09-21 14:41:05 -0700 | [diff] [blame] | 35 | import com.android.launcher3.icons.IconCache; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 36 | import com.android.launcher3.icons.IconProvider; |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 37 | import com.android.launcher3.icons.LauncherIcons; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 38 | import com.android.launcher3.notification.NotificationListener; |
Sunny Goyal | 73b5a27 | 2019-12-09 14:55:56 -0800 | [diff] [blame] | 39 | import com.android.launcher3.pm.InstallSessionHelper; |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 40 | import com.android.launcher3.pm.InstallSessionTracker; |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 41 | import com.android.launcher3.pm.UserCache; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 42 | import com.android.launcher3.util.MainThreadInitializedObject; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 43 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 44 | import com.android.launcher3.util.RunnableList; |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 45 | import com.android.launcher3.util.SafeCloseable; |
Jon Miranda | ec1277e | 2021-03-25 10:41:54 -0400 | [diff] [blame] | 46 | import com.android.launcher3.util.SettingsCache; |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 47 | import com.android.launcher3.util.SimpleBroadcastReceiver; |
Pinyao Ting | c7a6c29 | 2019-08-26 14:36:02 -0700 | [diff] [blame] | 48 | import com.android.launcher3.widget.custom.CustomWidgetManager; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 49 | |
Sunny Goyal | c620560 | 2015-05-21 20:46:33 -0700 | [diff] [blame] | 50 | public class LauncherAppState { |
Chris Wren | aeff7ea | 2014-02-14 16:59:24 -0500 | [diff] [blame] | 51 | |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 52 | public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher"; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 53 | private static final String KEY_ICON_STATE = "pref_icon_shape_path"; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 54 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 55 | // We do not need any synchronization for this variable as its only written on UI thread. |
Sunny Goyal | cf845f0 | 2019-09-25 09:08:16 -0700 | [diff] [blame] | 56 | public static final MainThreadInitializedObject<LauncherAppState> INSTANCE = |
Sunny Goyal | e529a86 | 2019-08-06 09:48:36 -0700 | [diff] [blame] | 57 | new MainThreadInitializedObject<>(LauncherAppState::new); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 58 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 59 | private final Context mContext; |
| 60 | private final LauncherModel mModel; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 61 | private final IconProvider mIconProvider; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 62 | private final IconCache mIconCache; |
| 63 | private final WidgetPreviewLoader mWidgetCache; |
| 64 | private final InvariantDeviceProfile mInvariantDeviceProfile; |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 65 | private final RunnableList mOnTerminateCallback = new RunnableList(); |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 66 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 67 | public static LauncherAppState getInstance(final Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 68 | return INSTANCE.get(context); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 71 | public static LauncherAppState getInstanceNoCreate() { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 72 | return INSTANCE.getNoCreate(); |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 73 | } |
| 74 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 75 | public Context getContext() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 76 | return mContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 77 | } |
| 78 | |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 79 | public LauncherAppState(Context context) { |
| 80 | this(context, LauncherFiles.APP_ICONS_DB); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 81 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 82 | mInvariantDeviceProfile.addOnChangeListener(idp -> refreshAndReloadLauncher()); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 83 | |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 84 | mContext.getSystemService(LauncherApps.class).registerCallback(mModel); |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 85 | |
| 86 | SimpleBroadcastReceiver modelChangeReceiver = |
| 87 | new SimpleBroadcastReceiver(mModel::onBroadcastIntent); |
| 88 | modelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED, |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 89 | Intent.ACTION_MANAGED_PROFILE_AVAILABLE, |
| 90 | Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE, |
| 91 | Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
Zak Cohen | 3eeb41d | 2020-02-14 14:15:13 -0800 | [diff] [blame] | 92 | if (FeatureFlags.IS_STUDIO_BUILD) { |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 93 | modelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD); |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 94 | } |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 95 | mOnTerminateCallback.add(() -> mContext.unregisterReceiver(modelChangeReceiver)); |
Sunny Goyal | 1416843 | 2019-10-24 15:59:49 -0700 | [diff] [blame] | 96 | |
Sunny Goyal | 01615a6 | 2019-09-20 12:00:07 -0700 | [diff] [blame] | 97 | // TODO: remove listener on terminate |
Hyunyoung Song | e17a699 | 2019-09-09 23:32:17 -0700 | [diff] [blame] | 98 | FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload); |
Sunny Goyal | 01615a6 | 2019-09-20 12:00:07 -0700 | [diff] [blame] | 99 | CustomWidgetManager.INSTANCE.get(mContext) |
| 100 | .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts); |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 101 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 102 | SafeCloseable userChangeListener = UserCache.INSTANCE.get(mContext) |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 103 | .addUserChangeListener(mModel::forceReload); |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 104 | mOnTerminateCallback.add(userChangeListener::close); |
Sunny Goyal | 337c81f | 2019-12-10 12:19:13 -0800 | [diff] [blame] | 105 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 106 | IconObserver observer = new IconObserver(); |
| 107 | SafeCloseable iconChangeTracker = mIconProvider.registerIconChangeListener( |
| 108 | observer, MODEL_EXECUTOR.getHandler()); |
| 109 | mOnTerminateCallback.add(iconChangeTracker::close); |
| 110 | MODEL_EXECUTOR.execute(observer::verifyIconChanged); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 111 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 112 | InstallSessionTracker installSessionTracker = |
| 113 | InstallSessionHelper.INSTANCE.get(context).registerInstallTracker(mModel); |
| 114 | mOnTerminateCallback.add(installSessionTracker::unregister); |
Sunny Goyal | 045b4fa | 2019-09-20 12:51:37 -0700 | [diff] [blame] | 115 | |
Sunny Goyal | eaf7a95 | 2020-07-29 16:54:20 -0700 | [diff] [blame] | 116 | // Register an observer to rebind the notification listener when dots are re-enabled. |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 117 | SettingsCache settingsCache = SettingsCache.INSTANCE.get(mContext); |
| 118 | SettingsCache.OnChangeListener notificationLister = this::onNotificationSettingsChanged; |
| 119 | settingsCache.register(NOTIFICATION_BADGING_URI, notificationLister); |
| 120 | onNotificationSettingsChanged(settingsCache.getValue(NOTIFICATION_BADGING_URI)); |
| 121 | mOnTerminateCallback.add(() -> |
| 122 | settingsCache.unregister(NOTIFICATION_BADGING_URI, notificationLister)); |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 125 | public LauncherAppState(Context context, @Nullable String iconCacheFileName) { |
| 126 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
| 127 | Preconditions.assertUIThread(); |
| 128 | mContext = context; |
| 129 | |
| 130 | mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context); |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 131 | mIconProvider = new IconProvider(context, ENABLE_THEMED_ICONS.get()); |
| 132 | mIconCache = new IconCache(mContext, mInvariantDeviceProfile, |
| 133 | iconCacheFileName, mIconProvider); |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 134 | mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); |
Sunny Goyal | 4484015 | 2020-08-25 23:22:18 -0700 | [diff] [blame] | 135 | mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext)); |
Tracy Zhou | be13d10 | 2020-01-12 01:07:59 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 138 | private void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 139 | if (areNotificationDotsEnabled) { |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 140 | NotificationListener.requestRebind(new ComponentName( |
| 141 | mContext, NotificationListener.class)); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 142 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 143 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 144 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 145 | private void refreshAndReloadLauncher() { |
| 146 | LauncherIcons.clearPool(); |
| 147 | mIconCache.updateIconParams( |
| 148 | mInvariantDeviceProfile.fillResIconDpi, mInvariantDeviceProfile.iconBitmapSize); |
| 149 | mWidgetCache.refresh(); |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 150 | mModel.forceReload(); |
| 151 | } |
| 152 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 153 | /** |
| 154 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 155 | */ |
| 156 | public void onTerminate() { |
Sunny Goyal | 8b74cc7 | 2020-07-27 17:50:33 -0700 | [diff] [blame] | 157 | mModel.destroy(); |
Sunny Goyal | e7b0012 | 2019-10-02 16:13:34 -0700 | [diff] [blame] | 158 | mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel); |
Sunny Goyal | 01615a6 | 2019-09-20 12:00:07 -0700 | [diff] [blame] | 159 | CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null); |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 160 | mOnTerminateCallback.executeAllAndDestroy(); |
| 161 | } |
Sunny Goyal | 01615a6 | 2019-09-20 12:00:07 -0700 | [diff] [blame] | 162 | |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 163 | public IconProvider getIconProvider() { |
| 164 | return mIconProvider; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 165 | } |
| 166 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 167 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 168 | return mIconCache; |
| 169 | } |
| 170 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 171 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 172 | return mModel; |
| 173 | } |
| 174 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 175 | public WidgetPreviewLoader getWidgetCache() { |
| 176 | return mWidgetCache; |
| 177 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 178 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 179 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 180 | return mInvariantDeviceProfile; |
| 181 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 182 | |
| 183 | /** |
| 184 | * Shorthand for {@link #getInvariantDeviceProfile()} |
| 185 | */ |
| 186 | public static InvariantDeviceProfile getIDP(Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 187 | return InvariantDeviceProfile.INSTANCE.get(context); |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 188 | } |
Sunny Goyal | b47172b | 2021-05-03 19:59:51 -0700 | [diff] [blame] | 189 | |
| 190 | private class IconObserver implements IconProvider.IconChangeListener { |
| 191 | |
| 192 | @Override |
| 193 | public void onAppIconChanged(String packageName, UserHandle user) { |
| 194 | mModel.onAppIconChanged(packageName, user); |
| 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public void onSystemIconStateChanged(String iconState) { |
| 199 | IconShape.init(mContext); |
| 200 | refreshAndReloadLauncher(); |
| 201 | getDevicePrefs(mContext).edit().putString(KEY_ICON_STATE, iconState).apply(); |
| 202 | } |
| 203 | |
| 204 | void verifyIconChanged() { |
| 205 | String iconState = mIconProvider.getSystemIconState(); |
| 206 | if (!iconState.equals(getDevicePrefs(mContext).getString(KEY_ICON_STATE, ""))) { |
| 207 | onSystemIconStateChanged(iconState); |
| 208 | } |
| 209 | } |
| 210 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 211 | } |