blob: f687c9df9c0b3ff6f092c0cab62235554c4ef232 [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;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070020import static com.android.launcher3.util.Executors.MODEL_EXECUTOR;
Sunny Goyal420d5452018-10-17 10:25:14 -070021import static com.android.launcher3.util.SecureSettingsObserver.newNotificationSettingsObserver;
Sunny Goyald0e360a2018-06-29 14:40:18 -070022
Tonyd48710c2017-07-27 23:23:58 -070023import android.content.ComponentName;
Sunny Goyalfdbef272017-02-01 12:52:54 -080024import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070025import android.content.Context;
26import android.content.Intent;
Sunny Goyale7b00122019-10-02 16:13:34 -070027import android.content.pm.LauncherApps;
Hyunyoung Songc55a3502018-12-04 15:43:16 -080028import android.os.Handler;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040029import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080030
Sunny Goyal823fd502015-08-04 11:40:13 -070031import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal29947f02017-12-18 13:49:44 -080032import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070033import com.android.launcher3.icons.IconCache;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070034import com.android.launcher3.icons.LauncherIcons;
Tonyd48710c2017-07-27 23:23:58 -070035import com.android.launcher3.notification.NotificationListener;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070036import com.android.launcher3.pm.InstallSessionTracker;
37import com.android.launcher3.pm.PackageInstallerCompat;
Sunny Goyald0e360a2018-06-29 14:40:18 -070038import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080039import com.android.launcher3.util.Preconditions;
Sunny Goyal420d5452018-10-17 10:25:14 -070040import com.android.launcher3.util.SecureSettingsObserver;
Sunny Goyale7b00122019-10-02 16:13:34 -070041import com.android.launcher3.util.SimpleBroadcastReceiver;
Pinyao Tingc7a6c292019-08-26 14:36:02 -070042import com.android.launcher3.widget.custom.CustomWidgetManager;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080043
Sunny Goyalc6205602015-05-21 20:46:33 -070044public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050045
Sunny Goyal29947f02017-12-18 13:49:44 -080046 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
47
Sunny Goyalfdbef272017-02-01 12:52:54 -080048 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyalcf845f02019-09-25 09:08:16 -070049 public static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
Sunny Goyale529a862019-08-06 09:48:36 -070050 new MainThreadInitializedObject<>(LauncherAppState::new);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040051
Sunny Goyalfdbef272017-02-01 12:52:54 -080052 private final Context mContext;
53 private final LauncherModel mModel;
54 private final IconCache mIconCache;
55 private final WidgetPreviewLoader mWidgetCache;
56 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tony Wickhamf34bee82018-12-03 18:11:39 -080057 private final SecureSettingsObserver mNotificationDotsObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080058
Sunny Goyal045b4fa2019-09-20 12:51:37 -070059 private final InstallSessionTracker mInstallSessionTracker;
Sunny Goyale7b00122019-10-02 16:13:34 -070060 private final SimpleBroadcastReceiver mModelChangeReceiver;
Sunny Goyal045b4fa2019-09-20 12:51:37 -070061
Sunny Goyalfdbef272017-02-01 12:52:54 -080062 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070063 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040064 }
65
Chris Wrend8fe6de2013-10-04 10:42:14 -040066 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070067 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040068 }
69
Daniel Sandlercc8befa2013-06-11 14:45:48 -040070 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080071 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072 }
73
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 private LauncherAppState(Context context) {
75 if (getLocalProvider(context) == null) {
76 throw new RuntimeException(
77 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040078 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070079 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080080 Preconditions.assertUIThread();
81 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040082
Sunny Goyald0e360a2018-06-29 14:40:18 -070083 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080084 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
85 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070086 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070087
Sunny Goyale7b00122019-10-02 16:13:34 -070088 mModelChangeReceiver = new SimpleBroadcastReceiver(mModel::onBroadcastIntent);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040089
Sunny Goyale7b00122019-10-02 16:13:34 -070090 mContext.getSystemService(LauncherApps.class).registerCallback(mModel);
91 mModelChangeReceiver.register(mContext, Intent.ACTION_LOCALE_CHANGED,
92 Intent.ACTION_MANAGED_PROFILE_ADDED,
93 Intent.ACTION_MANAGED_PROFILE_REMOVED,
94 Intent.ACTION_MANAGED_PROFILE_AVAILABLE,
95 Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE,
96 Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal29947f02017-12-18 13:49:44 -080097 if (FeatureFlags.IS_DOGFOOD_BUILD) {
Sunny Goyale7b00122019-10-02 16:13:34 -070098 mModelChangeReceiver.register(mContext, ACTION_FORCE_ROLOAD);
Sunny Goyal29947f02017-12-18 13:49:44 -080099 }
Hyunyoung Songe17a6992019-09-09 23:32:17 -0700100 FeatureFlags.APP_SEARCH_IMPROVEMENTS.addChangeListener(context, mModel::forceReload);
Sunny Goyal29947f02017-12-18 13:49:44 -0800101
Sunny Goyalfdbef272017-02-01 12:52:54 -0800102 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700103 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800104 new Handler().post( () -> mInvariantDeviceProfile.verifyConfigChangedInBackground(context));
Tony Wickham827cef22016-03-17 15:39:39 -0700105
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700106 mInstallSessionTracker = PackageInstallerCompat.getInstance(context)
107 .registerInstallTracker(mModel, MODEL_EXECUTOR);
108
Tony Wickhamf34bee82018-12-03 18:11:39 -0800109 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
110 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700111 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800112 // Register an observer to rebind the notification listener when dots are re-enabled.
113 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700114 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800115 mNotificationDotsObserver.register();
116 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700117 }
118 }
119
Tony Wickhamf34bee82018-12-03 18:11:39 -0800120 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
121 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700122 NotificationListener.requestRebind(new ComponentName(
123 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700124 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400125 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100126
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700127 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
128 if (changeFlags == 0) {
129 return;
130 }
131
Hyunyoung Songc55a3502018-12-04 15:43:16 -0800132 if ((changeFlags & CHANGE_FLAG_ICON_PARAMS) != 0) {
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700133 LauncherIcons.clearPool();
134 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
Hyunyoung Song2bf3f372019-04-02 10:45:57 -0700135 mWidgetCache.refresh();
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700136 }
137
138 mModel.forceReload();
139 }
140
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 /**
142 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
143 */
144 public void onTerminate() {
Sunny Goyale7b00122019-10-02 16:13:34 -0700145 mContext.unregisterReceiver(mModelChangeReceiver);
146 mContext.getSystemService(LauncherApps.class).unregisterCallback(mModel);
Sunny Goyal045b4fa2019-09-20 12:51:37 -0700147 mInstallSessionTracker.unregister();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800148 if (mNotificationDotsObserver != null) {
149 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700150 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400151 }
152
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400153 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800154 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 mModel.initialize(launcher);
Pinyao Tingc7a6c292019-08-26 14:36:02 -0700156 CustomWidgetManager.INSTANCE.get(launcher)
157 .setWidgetRefreshCallback(mModel::refreshAndBindWidgetsAndShortcuts);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400158 return mModel;
159 }
160
Sunny Goyal34942622014-08-29 17:20:55 -0700161 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400162 return mIconCache;
163 }
164
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700165 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400166 return mModel;
167 }
168
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700169 public WidgetPreviewLoader getWidgetCache() {
170 return mWidgetCache;
171 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100172
Adam Cohen2e6da152015-05-06 11:42:25 -0700173 public InvariantDeviceProfile getInvariantDeviceProfile() {
174 return mInvariantDeviceProfile;
175 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800176
177 /**
178 * Shorthand for {@link #getInvariantDeviceProfile()}
179 */
180 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700181 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800182 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800183
184 private static LauncherProvider getLocalProvider(Context context) {
185 try (ContentProviderClient cl = context.getContentResolver()
186 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
187 return (LauncherProvider) cl.getLocalContentProvider();
188 }
189 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400190}