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 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 19 | import static com.android.launcher3.InvariantDeviceProfile.CHANGE_FLAG_ICON_PARAMS; |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 20 | import static com.android.launcher3.util.SecureSettingsObserver.newNotificationSettingsObserver; |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 21 | |
Tony | 573c3e1 | 2019-02-22 13:10:59 -0800 | [diff] [blame] | 22 | import android.app.KeyguardManager; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 24 | import android.content.ContentProviderClient; |
Sunny Goyal | e755d46 | 2014-07-22 13:48:29 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
| 27 | import android.content.IntentFilter; |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 28 | import android.os.Handler; |
Tony | 573c3e1 | 2019-02-22 13:10:59 -0800 | [diff] [blame] | 29 | import android.os.Process; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 30 | import android.util.Log; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 31 | |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 32 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 4390ace | 2014-10-13 11:33:11 -0700 | [diff] [blame] | 33 | import com.android.launcher3.compat.PackageInstallerCompat; |
Sunny Goyal | 823fd50 | 2015-08-04 11:40:13 -0700 | [diff] [blame] | 34 | import com.android.launcher3.compat.UserManagerCompat; |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 35 | import com.android.launcher3.config.FeatureFlags; |
Sunny Goyal | f840f10 | 2018-09-21 14:41:05 -0700 | [diff] [blame] | 36 | import com.android.launcher3.icons.IconCache; |
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 | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 39 | import com.android.launcher3.util.MainThreadInitializedObject; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 40 | import com.android.launcher3.util.Preconditions; |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 41 | import com.android.launcher3.util.SecureSettingsObserver; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 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 | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 45 | public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher"; |
| 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. |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 48 | private static final MainThreadInitializedObject<LauncherAppState> INSTANCE = |
| 49 | new MainThreadInitializedObject<>((c) -> new LauncherAppState(c)); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 50 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 51 | private final Context mContext; |
| 52 | private final LauncherModel mModel; |
| 53 | private final IconCache mIconCache; |
| 54 | private final WidgetPreviewLoader mWidgetCache; |
| 55 | private final InvariantDeviceProfile mInvariantDeviceProfile; |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 56 | private final SecureSettingsObserver mNotificationDotsObserver; |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 57 | |
| 58 | public static LauncherAppState getInstance(final Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 59 | return INSTANCE.get(context); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 60 | } |
| 61 | |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 62 | public static LauncherAppState getInstanceNoCreate() { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 63 | return INSTANCE.getNoCreate(); |
Chris Wren | d8fe6de | 2013-10-04 10:42:14 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 66 | public Context getContext() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 67 | return mContext; |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 70 | private LauncherAppState(Context context) { |
| 71 | if (getLocalProvider(context) == null) { |
| 72 | throw new RuntimeException( |
| 73 | "Initializing LauncherAppState in the absence of LauncherProvider"); |
Daniel Sandler | e4f9891 | 2013-06-25 15:13:26 -0400 | [diff] [blame] | 74 | } |
Hyunyoung Song | 0de0117 | 2016-10-05 16:27:48 -0700 | [diff] [blame] | 75 | Log.v(Launcher.TAG, "LauncherAppState initiated"); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 76 | Preconditions.assertUIThread(); |
| 77 | mContext = context; |
Daniel Sandler | b9eb286 | 2013-06-14 20:17:30 -0400 | [diff] [blame] | 78 | |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 79 | mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 80 | mIconCache = new IconCache(mContext, mInvariantDeviceProfile); |
| 81 | mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache); |
Sunny Goyal | c6e9769 | 2017-06-02 13:46:55 -0700 | [diff] [blame] | 82 | mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext)); |
Sunny Goyal | 2759579 | 2015-03-19 13:18:44 -0700 | [diff] [blame] | 83 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 84 | LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 85 | |
| 86 | // Register intent receivers |
Kenny Guy | ed13187 | 2014-04-30 03:02:21 +0100 | [diff] [blame] | 87 | IntentFilter filter = new IntentFilter(); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 88 | filter.addAction(Intent.ACTION_LOCALE_CHANGED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 89 | // For handling managed profiles |
Sunny Goyal | d3b87ef | 2016-07-28 12:11:54 -0700 | [diff] [blame] | 90 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 91 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 92 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE); |
| 93 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE); |
| 94 | filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED); |
Sunny Goyal | 957c13f | 2015-05-01 13:02:20 -0700 | [diff] [blame] | 95 | |
Sunny Goyal | 29947f0 | 2017-12-18 13:49:44 -0800 | [diff] [blame] | 96 | if (FeatureFlags.IS_DOGFOOD_BUILD) { |
| 97 | filter.addAction(ACTION_FORCE_ROLOAD); |
| 98 | } |
| 99 | |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 100 | mContext.registerReceiver(mModel, filter); |
| 101 | UserManagerCompat.getInstance(mContext).enableAndResetCache(); |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 102 | mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged); |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 103 | new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context)); |
Tony Wickham | 827cef2 | 2016-03-17 15:39:39 -0700 | [diff] [blame] | 104 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 105 | if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) { |
| 106 | mNotificationDotsObserver = null; |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 107 | } else { |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 108 | // Register an observer to rebind the notification listener when dots are re-enabled. |
| 109 | mNotificationDotsObserver = |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 110 | newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged); |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 111 | mNotificationDotsObserver.register(); |
| 112 | mNotificationDotsObserver.dispatchOnChange(); |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 116 | protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) { |
| 117 | if (areNotificationDotsEnabled) { |
Sunny Goyal | 420d545 | 2018-10-17 10:25:14 -0700 | [diff] [blame] | 118 | NotificationListener.requestRebind(new ComponentName( |
| 119 | mContext, NotificationListener.class)); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 120 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 121 | } |
Kenny Guy | 1317e2d | 2014-05-08 18:52:50 +0100 | [diff] [blame] | 122 | |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 123 | private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) { |
| 124 | if (changeFlags == 0) { |
| 125 | return; |
| 126 | } |
| 127 | |
Hyunyoung Song | c55a350 | 2018-12-04 15:43:16 -0800 | [diff] [blame] | 128 | if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) { |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 129 | LauncherIcons.clearPool(); |
| 130 | mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize); |
Hyunyoung Song | 2bf3f37 | 2019-04-02 10:45:57 -0700 | [diff] [blame] | 131 | mWidgetCache.refresh(); |
Sunny Goyal | 87dc48b | 2018-10-12 11:42:33 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | mModel.forceReload(); |
| 135 | } |
| 136 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 137 | /** |
| 138 | * Call from Application.onTerminate(), which is not guaranteed to ever be called. |
| 139 | */ |
| 140 | public void onTerminate() { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 141 | mContext.unregisterReceiver(mModel); |
| 142 | final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext); |
Kenny Guy | c2bd810 | 2014-06-30 12:30:31 +0100 | [diff] [blame] | 143 | launcherApps.removeOnAppsChangedCallback(mModel); |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 144 | PackageInstallerCompat.getInstance(mContext).onStop(); |
Tony Wickham | f34bee8 | 2018-12-03 18:11:39 -0800 | [diff] [blame] | 145 | if (mNotificationDotsObserver != null) { |
| 146 | mNotificationDotsObserver.unregister(); |
Tony | d48710c | 2017-07-27 23:23:58 -0700 | [diff] [blame] | 147 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 148 | } |
| 149 | |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 150 | LauncherModel setLauncher(Launcher launcher) { |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 151 | getLocalProvider(mContext).setLauncherProviderChangeListener(launcher); |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 152 | mModel.initialize(launcher); |
| 153 | return mModel; |
| 154 | } |
| 155 | |
Sunny Goyal | 3494262 | 2014-08-29 17:20:55 -0700 | [diff] [blame] | 156 | public IconCache getIconCache() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 157 | return mIconCache; |
| 158 | } |
| 159 | |
Sunny Goyal | 18bf8e2 | 2015-04-08 18:13:46 -0700 | [diff] [blame] | 160 | public LauncherModel getModel() { |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 161 | return mModel; |
| 162 | } |
| 163 | |
Sunny Goyal | 5b0e669 | 2015-03-19 14:31:19 -0700 | [diff] [blame] | 164 | public WidgetPreviewLoader getWidgetCache() { |
| 165 | return mWidgetCache; |
| 166 | } |
Michael Jurka | a6a0547 | 2013-11-13 17:59:46 +0100 | [diff] [blame] | 167 | |
Adam Cohen | 2e6da15 | 2015-05-06 11:42:25 -0700 | [diff] [blame] | 168 | public InvariantDeviceProfile getInvariantDeviceProfile() { |
| 169 | return mInvariantDeviceProfile; |
| 170 | } |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * Shorthand for {@link #getInvariantDeviceProfile()} |
| 174 | */ |
| 175 | public static InvariantDeviceProfile getIDP(Context context) { |
Sunny Goyal | d0e360a | 2018-06-29 14:40:18 -0700 | [diff] [blame] | 176 | return InvariantDeviceProfile.INSTANCE.get(context); |
Sunny Goyal | 87f784c | 2017-01-11 10:48:34 -0800 | [diff] [blame] | 177 | } |
Sunny Goyal | fdbef27 | 2017-02-01 12:52:54 -0800 | [diff] [blame] | 178 | |
| 179 | private static LauncherProvider getLocalProvider(Context context) { |
| 180 | try (ContentProviderClient cl = context.getContentResolver() |
| 181 | .acquireContentProviderClient(LauncherProvider.AUTHORITY)) { |
| 182 | return (LauncherProvider) cl.getLocalContentProvider(); |
| 183 | } |
| 184 | } |
Daniel Sandler | cc8befa | 2013-06-11 14:45:48 -0400 | [diff] [blame] | 185 | } |