blob: 4754558aa163295aed80dbf40009ec9f814eefa4 [file] [log] [blame]
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001/*
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
17package com.android.launcher3;
18
Hyunyoung Songc55a3502018-12-04 15:43:16 -080019import static com.android.launcher3.InvariantDeviceProfile.CHANGE_FLAG_ICON_PARAMS;
Jon Mirandaec1277e2021-03-25 10:41:54 -040020import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_HOTSEAT_COUNT;
21import static com.android.launcher3.InvariantDeviceProfile.KEY_MIGRATION_SRC_WORKSPACE_SIZE;
22import static com.android.launcher3.config.FeatureFlags.ENABLE_FOUR_COLUMNS;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070023import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Jon Mirandaec1277e2021-03-25 10:41:54 -040024import static com.android.launcher3.util.SettingsCache.NOTIFICATION_BADGING_URI;
Sunny Goyald0e360a2018-06-29 14:40:18 -070025
Tonyd48710c2017-07-27 23:23:58 -070026import android.content.ComponentName;
Sunny Goyale755d462014-07-22 13:48:29 -070027import android.content.Context;
28import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070029import android.content.pm.LauncherApps;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080030import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040031import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080032
Tracy Zhoube13d102020-01-12 01:07:59 -080033import androidx.annotation.Nullable;
34
Sunny Goyal29947f02017-12-18 13:49:44 -080035import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070036import com.android.launcher3.icons.IconCache;
Sunny Goyal14168432019-10-24 15:59:49 -070037import com.android.launcher3.icons.IconProvider;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070038import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070039import com.android.launcher3.notification.NotificationListener;
Sunny Goyal73b5a272019-12-09 14:55:56 -080040import com.android.launcher3.pm.InstallSessionHelper;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070041import com.android.launcher3.pm.InstallSessionTracker;
Sunny Goyal337c81f2019-12-10 12:19:13 -080042import com.android.launcher3.pm.UserCache;
Sunny Goyald0e360a2018-06-29 14:40:18 -070043import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080044import com.android.launcher3.util.Preconditions;
Sunny Goyal14168432019-10-24 15:59:49 -070045import com.android.launcher3.util.SafeCloseable;
Jon Mirandaec1277e2021-03-25 10:41:54 -040046import com.android.launcher3.util.SettingsCache;
Sunny Goyale7b00122019-10-02 16:13:34 -070047import com.android.launcher3.util.SimpleBroadcastReceiver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070048import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049
Sunny Goyalc6205602015-05-21 20:46:33 -070050public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050051
Sunny Goyal29947f02017-12-18 13:49:44 -080052 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
53
Sunny Goyalfdbef272017-02-01 12:52:54 -080054 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070055 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070056 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057
Sunny Goyalfdbef272017-02-01 12:52:54 -080058 private final Context mContext;
59 private final LauncherModel mModel;
60 private final IconCache mIconCache;
61 private final WidgetPreviewLoader mWidgetCache;
62 private final InvariantDeviceProfile mInvariantDeviceProfile;
Vinit Nayaka34af182021-02-11 15:48:54 -080063 private SettingsCache.OnChangeListener mNotificationSettingsChangedListener;
Sunny Goyalfdbef272017-02-01 12:52:54 -080064
Vinit Nayaka34af182021-02-11 15:48:54 -080065 private SettingsCache mSettingsCache;
Tracy Zhoube13d102020-01-12 01:07:59 -080066 private InstallSessionTracker mInstallSessionTracker;
67 private SimpleBroadcastReceiver mModelChangeReceiver;
68 private SafeCloseable mCalendarChangeTracker;
69 private SafeCloseable mUserChangeListener;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070070
Sunny Goyalfdbef272017-02-01 12:52:54 -080071 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070072 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040073 }
74
Chris Wrend8fe6de2013-10-04 10:42:14 -040075 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070076 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040077 }
78
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080080 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 }
82
Tracy Zhoube13d102020-01-12 01:07:59 -080083 public LauncherAppState(Context context) {
84 this(context, LauncherFiles.APP_ICONS_DB);
Sunny Goyal27595792015-03-19 13:18:44 -070085
Sunny Goyale7b00122019-10-02 16:13:34 -070086 mModelChangeReceiver = new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040087
Sunny Goyale7b00122019-10-02 16:13:34 -070088 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
89 mModelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
Sunny Goyale7b00122019-10-02 16:13:34 -070090 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
91 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
92 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Zak Cohen3eeb41d2020-02-14 14:15:13 -080093 if (FeatureFlags.IS_STUDIO_BUILD) {
Sunny Goyale7b00122019-10-02 16:13:34 -070094 mModelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080095 }
Sunny Goyal14168432019-10-24 15:59:49 -070096
97 mCalendarChangeTracker = IconProvider.registerIconChangeListener(mContext,
98 mModel::onAppIconChanged, MODEL_EXECUTOR.getHandler());
99
Sunny Goyal01615a62019-09-20 12:00:07 -0700100 // TODO: remove listener on terminate
Hyunyoung Songe17a6992019-09-09 23:32:17 -0700101 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal01615a62019-09-20 12:00:07 -0700102 CustomWidgetManager.INSTANCE.get(mContext)
103 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Sunny Goyal29947f02017-12-18 13:49:44 -0800104
Sunny Goyal337c81f2019-12-10 12:19:13 -0800105 mUserChangeListener = UserCache.INSTANCE.get(mContext)
106 .addUserChangeListener(mModel::forceReload);
107
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700108 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800109 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700110
Sunny Goyal73b5a272019-12-09 14:55:56 -0800111 mInstallSessionTracker = InstallSessionHelper.INSTANCE.get(context)
Winson Chungf9935182020-10-23 09:26:44 -0700112 .registerInstallTracker(mModel);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700113
Sunny Goyaleaf7a952020-07-29 16:54:20 -0700114 // Register an observer to rebind the notification listener when dots are re-enabled.
Vinit Nayaka34af182021-02-11 15:48:54 -0800115 mSettingsCache = SettingsCache.INSTANCE.get(mContext);
116 mNotificationSettingsChangedListener = this::onNotificationSettingsChanged;
117 mSettingsCache.register(NOTIFICATION_BADGING_URI,
118 mNotificationSettingsChangedListener);
Sunny Goyal58530bd2021-03-31 15:56:11 -0700119 onNotificationSettingsChanged(mSettingsCache.getValue(NOTIFICATION_BADGING_URI));
Sunny Goyal420d5452018-10-17 10:25:14 -0700120 }
121
Tracy Zhoube13d102020-01-12 01:07:59 -0800122 public LauncherAppState(Context context, @Nullable String iconCacheFileName) {
123 Log.v(Launcher.TAG, "LauncherAppState initiated");
124 Preconditions.assertUIThread();
125 mContext = context;
126
127 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(context);
Jon Mirandaec1277e2021-03-25 10:41:54 -0400128
129 // b/175329686 Temporary logic to gracefully migrate group of users to the new 4x5 grid.
130 String gridName = InvariantDeviceProfile.getCurrentGridName(context);
131 if (ENABLE_FOUR_COLUMNS.get()
132 || "reasonable".equals(gridName)
133 || ENABLE_FOUR_COLUMNS.key.equals(gridName)) {
134 // Reset flag and remove it from developer options to prevent it from being enabled
135 // again.
136 ENABLE_FOUR_COLUMNS.reset(context);
137 FeatureFlags.removeFlag(ENABLE_FOUR_COLUMNS);
138
139 // Force migration code to run
140 Utilities.getPrefs(context).edit()
141 .remove(KEY_MIGRATION_SRC_HOTSEAT_COUNT)
142 .remove(KEY_MIGRATION_SRC_WORKSPACE_SIZE)
143 .apply();
144
145 // We make an empty call here to ensure the database is created with the old IDP grid,
146 // so that when we set the new grid the migration can proceeds as expected.
147 LauncherSettings.Settings.call(context.getContentResolver(), "");
148
149 String newGridName = "practical";
150 Utilities.getPrefs(mContext).edit().putString("idp_grid_name", newGridName).commit();
151 mInvariantDeviceProfile.setCurrentGrid(context, "practical");
152 } else {
153 FeatureFlags.removeFlag(ENABLE_FOUR_COLUMNS);
154 }
155
Tracy Zhoube13d102020-01-12 01:07:59 -0800156 mIconCache = new IconCache(mContext, mInvariantDeviceProfile, iconCacheFileName);
157 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyal44840152020-08-25 23:22:18 -0700158 mModel = new LauncherModel(context, this, mIconCache, new AppFilter(mContext));
Tracy Zhoube13d102020-01-12 01:07:59 -0800159 }
160
Tony Wickhamf34bee82018-12-03 18:11:39 -0800161 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
162 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700163 NotificationListener.requestRebind(new ComponentName(
164 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700165 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100167
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700168 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
169 if (changeFlags == 0) {
170 return;
171 }
172
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800173 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700174 LauncherIcons.clearPool();
175 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700176 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700177 }
178
179 mModel.forceReload();
180 }
181
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400182 /**
183 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
184 */
185 public void onTerminate() {
Sunny Goyal8b74cc72020-07-27 17:50:33 -0700186 mModel.destroy();
Tracy Zhoube13d102020-01-12 01:07:59 -0800187 if (mModelChangeReceiver != null) {
188 mContext.unregisterReceiver(mModelChangeReceiver);
189 }
Sunny Goyale7b00122019-10-02 16:13:34 -0700190 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Tracy Zhoube13d102020-01-12 01:07:59 -0800191 if (mInstallSessionTracker != null) {
192 mInstallSessionTracker.unregister();
193 }
194 if (mCalendarChangeTracker != null) {
195 mCalendarChangeTracker.close();
196 }
197 if (mUserChangeListener != null) {
198 mUserChangeListener.close();
199 }
Sunny Goyal01615a62019-09-20 12:00:07 -0700200 CustomWidgetManager.INSTANCE.get(mContext).setWidgetRefreshCallback(null);
201
Vinit Nayaka34af182021-02-11 15:48:54 -0800202 if (mSettingsCache != null) {
203 mSettingsCache.unregister(NOTIFICATION_BADGING_URI,
204 mNotificationSettingsChangedListener);
Tonyd48710c2017-07-27 23:23:58 -0700205 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400206 }
207
Sunny Goyal34942622014-08-29 17:20:55 -0700208 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400209 return mIconCache;
210 }
211
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700212 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400213 return mModel;
214 }
215
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700216 public WidgetPreviewLoader getWidgetCache() {
217 return mWidgetCache;
218 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100219
Adam Cohen2e6da152015-05-06 11:42:25 -0700220 public InvariantDeviceProfile getInvariantDeviceProfile() {
221 return mInvariantDeviceProfile;
222 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800223
224 /**
225 * Shorthand for {@link #getInvariantDeviceProfile()}
226 */
227 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700228 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800229 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400230}