blob: 6bf581277e34427b2d8db8bcac7578ffaa3e689d [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 Goyald0e360a2018-06-29 14:40:18 -070020
Tonyd48710c2017-07-27 23:23:58 -070021import android.content.ComponentName;
Sunny Goyalfdbef272017-02-01 12:52:54 -080022import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070023import android.content.Context;
24import android.content.Intent;
25import android.content.IntentFilter;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040026import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080027
Kenny Guyed131872014-04-30 03:02:21 +010028import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070029import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070030import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal29947f02017-12-18 13:49:44 -080031import com.android.launcher3.config.FeatureFlags;
Sunny Goyalf840f102018-09-21 14:41:05 -070032import com.android.launcher3.icons.IconCache;
Tonyd48710c2017-07-27 23:23:58 -070033import com.android.launcher3.notification.NotificationListener;
Sunny Goyald0e360a2018-06-29 14:40:18 -070034import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080035import com.android.launcher3.util.Preconditions;
Sunny Goyal420d5452018-10-17 10:25:14 -070036import com.android.launcher3.util.SecureSettingsObserver;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080037
Sunny Goyalc6205602015-05-21 20:46:33 -070038public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050039
Sunny Goyal29947f02017-12-18 13:49:44 -080040 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
41
Sunny Goyalfdbef272017-02-01 12:52:54 -080042 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyald0e360a2018-06-29 14:40:18 -070043 private static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
44 new MainThreadInitializedObject<>((c) -> new LauncherAppState(c));
Daniel Sandlercc8befa2013-06-11 14:45:48 -040045
Sunny Goyalfdbef272017-02-01 12:52:54 -080046 private final Context mContext;
47 private final LauncherModel mModel;
48 private final IconCache mIconCache;
49 private final WidgetPreviewLoader mWidgetCache;
50 private final InvariantDeviceProfile mInvariantDeviceProfile;
Sunny Goyal420d5452018-10-17 10:25:14 -070051 private final SecureSettingsObserver mNotificationBadgingObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080052
53 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070054 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040055 }
56
Chris Wrend8fe6de2013-10-04 10:42:14 -040057 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070058 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040059 }
60
Daniel Sandlercc8befa2013-06-11 14:45:48 -040061 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080062 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040063 }
64
Sunny Goyalfdbef272017-02-01 12:52:54 -080065 private LauncherAppState(Context context) {
66 if (getLocalProvider(context) == null) {
67 throw new RuntimeException(
68 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040069 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070070 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080071 Preconditions.assertUIThread();
72 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040073
Sunny Goyald0e360a2018-06-29 14:40:18 -070074 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080075 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
76 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070077 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070078
Sunny Goyalfdbef272017-02-01 12:52:54 -080079 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040080
81 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010082 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070084 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -070085 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
86 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
87 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
88 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
89 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070090
Sunny Goyal29947f02017-12-18 13:49:44 -080091 if (FeatureFlags.IS_DOGFOOD_BUILD) {
92 filter.addAction(ACTION_FORCE_ROLOAD);
93 }
94
Sunny Goyalfdbef272017-02-01 12:52:54 -080095 mContext.registerReceiver(mModel, filter);
96 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Tony Wickham827cef22016-03-17 15:39:39 -070097
Tonyd48710c2017-07-27 23:23:58 -070098 if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
99 mNotificationBadgingObserver = null;
100 } else {
101 // Register an observer to rebind the notification listener when badging is re-enabled.
Sunny Goyal420d5452018-10-17 10:25:14 -0700102 mNotificationBadgingObserver =
103 newNotificationSettingsObserver(mContext, this::onNotificationSettingsChanged);
104 mNotificationBadgingObserver.register();
105 mNotificationBadgingObserver.dispatchOnChange();
106 }
107 }
108
109 protected void onNotificationSettingsChanged(boolean isNotificationBadgingEnabled) {
110 if (isNotificationBadgingEnabled) {
111 NotificationListener.requestRebind(new ComponentName(
112 mContext, NotificationListener.class));
Tonyd48710c2017-07-27 23:23:58 -0700113 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400114 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100115
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400116 /**
117 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
118 */
119 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800120 mContext.unregisterReceiver(mModel);
121 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100122 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800123 PackageInstallerCompat.getInstance(mContext).onStop();
Tonyd48710c2017-07-27 23:23:58 -0700124 if (mNotificationBadgingObserver != null) {
125 mNotificationBadgingObserver.unregister();
126 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400127 }
128
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400129 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800130 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400131 mModel.initialize(launcher);
132 return mModel;
133 }
134
Sunny Goyal34942622014-08-29 17:20:55 -0700135 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400136 return mIconCache;
137 }
138
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700139 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400140 return mModel;
141 }
142
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700143 public WidgetPreviewLoader getWidgetCache() {
144 return mWidgetCache;
145 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100146
Adam Cohen2e6da152015-05-06 11:42:25 -0700147 public InvariantDeviceProfile getInvariantDeviceProfile() {
148 return mInvariantDeviceProfile;
149 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800150
151 /**
152 * Shorthand for {@link #getInvariantDeviceProfile()}
153 */
154 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700155 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800156 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800157
158 private static LauncherProvider getLocalProvider(Context context) {
159 try (ContentProviderClient cl = context.getContentResolver()
160 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
161 return (LauncherProvider) cl.getLocalContentProvider();
162 }
163 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164}