blob: 182a4ee1f07a95d0e6e7c845f772d0cf07b8e87d [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
Sunny Goyal420d5452018-10-17 10:25:14 -070019import static com.android.launcher3.util.SecureSettingsObserver.newNotificationSettingsObserver;
Sunny Goyal87dc48b2018-10-12 11:42:33 -070020import static com.android.launcher3.InvariantDeviceProfile.CHANGE_FLAG_ICON_SIZE;
Sunny Goyald0e360a2018-06-29 14:40:18 -070021
Tonyd48710c2017-07-27 23:23:58 -070022import android.content.ComponentName;
Sunny Goyalfdbef272017-02-01 12:52:54 -080023import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070024import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040027import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080028
Kenny Guyed131872014-04-30 03:02:21 +010029import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070030import com.android.launcher3.compat.PackageInstallerCompat;
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 Goyald0e360a2018-06-29 14:40:18 -070036import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080037import com.android.launcher3.util.Preconditions;
Sunny Goyal420d5452018-10-17 10:25:14 -070038import com.android.launcher3.util.SecureSettingsObserver;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080039
Sunny Goyalc6205602015-05-21 20:46:33 -070040public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050041
Sunny Goyal29947f02017-12-18 13:49:44 -080042 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
43
Sunny Goyalfdbef272017-02-01 12:52:54 -080044 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyald0e360a2018-06-29 14:40:18 -070045 private static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
46 new MainThreadInitializedObject<>((c) -> new LauncherAppState(c));
Daniel Sandlercc8befa2013-06-11 14:45:48 -040047
Sunny Goyalfdbef272017-02-01 12:52:54 -080048 private final Context mContext;
49 private final LauncherModel mModel;
50 private final IconCache mIconCache;
51 private final WidgetPreviewLoader mWidgetCache;
52 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tony Wickhamf34bee82018-12-03 18:11:39 -080053 private final SecureSettingsObserver mNotificationDotsObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080054
55 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070056 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040057 }
58
Chris Wrend8fe6de2013-10-04 10:42:14 -040059 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070060 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040061 }
62
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080064 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040065 }
66
Sunny Goyalfdbef272017-02-01 12:52:54 -080067 private LauncherAppState(Context context) {
68 if (getLocalProvider(context) == null) {
69 throw new RuntimeException(
70 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040071 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070072 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080073 Preconditions.assertUIThread();
74 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040075
Sunny Goyald0e360a2018-06-29 14:40:18 -070076 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080077 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
78 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070079 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070080
Sunny Goyalfdbef272017-02-01 12:52:54 -080081 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082
83 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010084 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040085 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070086 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -070087 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
88 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
89 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
90 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
91 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070092
Sunny Goyal29947f02017-12-18 13:49:44 -080093 if (FeatureFlags.IS_DOGFOOD_BUILD) {
94 filter.addAction(ACTION_FORCE_ROLOAD);
95 }
96
Sunny Goyalfdbef272017-02-01 12:52:54 -080097 mContext.registerReceiver(mModel, filter);
98 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Sunny Goyal87dc48b2018-10-12 11:42:33 -070099 mInvariantDeviceProfile.addOnChangeListener(this::onIdpChanged);
Tony Wickham827cef22016-03-17 15:39:39 -0700100
Tony Wickhamf34bee82018-12-03 18:11:39 -0800101 if (!mContext.getResources().getBoolean(R.bool.notification_dots_enabled)) {
102 mNotificationDotsObserver = null;
Tonyd48710c2017-07-27 23:23:58 -0700103 } else {
Tony Wickhamf34bee82018-12-03 18:11:39 -0800104 // Register an observer to rebind the notification listener when dots are re-enabled.
105 mNotificationDotsObserver =
Sunny Goyal420d5452018-10-17 10:25:14 -0700106 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
Tony Wickhamf34bee82018-12-03 18:11:39 -0800107 mNotificationDotsObserver.register();
108 mNotificationDotsObserver.dispatchOnChange();
Sunny Goyal420d5452018-10-17 10:25:14 -0700109 }
110 }
111
Tony Wickhamf34bee82018-12-03 18:11:39 -0800112 protected void onNotificationSettingsChanged(boolean areNotificationDotsEnabled) {
113 if (areNotificationDotsEnabled) {
Sunny Goyal420d5452018-10-17 10:25:14 -0700114 NotificationListener.requestRebind(new ComponentName(
115 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700116 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400117 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100118
Sunny Goyal87dc48b2018-10-12 11:42:33 -0700119 private void onIdpChanged(int changeFlags, InvariantDeviceProfile idp) {
120 if (changeFlags == 0) {
121 return;
122 }
123
124 if ((changeFlags & CHANGE_FLAG_ICON_SIZE) != 0) {
125 LauncherIcons.clearPool();
126 mIconCache.updateIconParams(idp.fillResIconDpi, idp.iconBitmapSize);
127 }
128
129 mModel.forceReload();
130 }
131
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400132 /**
133 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
134 */
135 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800136 mContext.unregisterReceiver(mModel);
137 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100138 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800139 PackageInstallerCompat.getInstance(mContext).onStop();
Tony Wickhamf34bee82018-12-03 18:11:39 -0800140 if (mNotificationDotsObserver != null) {
141 mNotificationDotsObserver.unregister();
Tonyd48710c2017-07-27 23:23:58 -0700142 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 }
144
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400145 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800146 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400147 mModel.initialize(launcher);
148 return mModel;
149 }
150
Sunny Goyal34942622014-08-29 17:20:55 -0700151 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 return mIconCache;
153 }
154
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700155 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400156 return mModel;
157 }
158
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700159 public WidgetPreviewLoader getWidgetCache() {
160 return mWidgetCache;
161 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100162
Adam Cohen2e6da152015-05-06 11:42:25 -0700163 public InvariantDeviceProfile getInvariantDeviceProfile() {
164 return mInvariantDeviceProfile;
165 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800166
167 /**
168 * Shorthand for {@link #getInvariantDeviceProfile()}
169 */
170 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700171 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800172 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800173
174 private static LauncherProvider getLocalProvider(Context context) {
175 try (ContentProviderClient cl = context.getContentResolver()
176 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
177 return (LauncherProvider) cl.getLocalContentProvider();
178 }
179 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400180}