blob: 5159de17af4989527c2575a3bde6b621fdc83ea2 [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 Goyald0e360a2018-06-29 14:40:18 -070019import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING;
20
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;
Tonyd48710c2017-07-27 23:23:58 -070032import com.android.launcher3.notification.NotificationListener;
Sunny Goyald0e360a2018-06-29 14:40:18 -070033import com.android.launcher3.util.MainThreadInitializedObject;
Sunny Goyalfdbef272017-02-01 12:52:54 -080034import com.android.launcher3.util.Preconditions;
Tonyd48710c2017-07-27 23:23:58 -070035import com.android.launcher3.util.SettingsObserver;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080036
Sunny Goyalc6205602015-05-21 20:46:33 -070037public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050038
Sunny Goyal29947f02017-12-18 13:49:44 -080039 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
40
Sunny Goyalfdbef272017-02-01 12:52:54 -080041 // We do not need any synchronization for this variable as its only written on UI thread.
Sunny Goyald0e360a2018-06-29 14:40:18 -070042 private static final MainThreadInitializedObject<LauncherAppState> INSTANCE =
43 new MainThreadInitializedObject<>((c) -> new LauncherAppState(c));
Daniel Sandlercc8befa2013-06-11 14:45:48 -040044
Sunny Goyalfdbef272017-02-01 12:52:54 -080045 private final Context mContext;
46 private final LauncherModel mModel;
47 private final IconCache mIconCache;
48 private final WidgetPreviewLoader mWidgetCache;
49 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tonyd48710c2017-07-27 23:23:58 -070050 private final SettingsObserver mNotificationBadgingObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080051
52 public static LauncherAppState getInstance(final Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -070053 return INSTANCE.get(context);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040054 }
55
Chris Wrend8fe6de2013-10-04 10:42:14 -040056 public static LauncherAppState getInstanceNoCreate() {
Sunny Goyald0e360a2018-06-29 14:40:18 -070057 return INSTANCE.getNoCreate();
Chris Wrend8fe6de2013-10-04 10:42:14 -040058 }
59
Daniel Sandlercc8befa2013-06-11 14:45:48 -040060 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080061 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040062 }
63
Sunny Goyalfdbef272017-02-01 12:52:54 -080064 private LauncherAppState(Context context) {
65 if (getLocalProvider(context) == null) {
66 throw new RuntimeException(
67 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070069 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080070 Preconditions.assertUIThread();
71 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040072
Sunny Goyald0e360a2018-06-29 14:40:18 -070073 mInvariantDeviceProfile = InvariantDeviceProfile.INSTANCE.get(mContext);
Sunny Goyalfdbef272017-02-01 12:52:54 -080074 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
75 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070076 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070077
Sunny Goyalfdbef272017-02-01 12:52:54 -080078 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040079
80 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010081 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040082 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070083 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -070084 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
85 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
86 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
87 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
88 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070089
Sunny Goyal29947f02017-12-18 13:49:44 -080090 if (FeatureFlags.IS_DOGFOOD_BUILD) {
91 filter.addAction(ACTION_FORCE_ROLOAD);
92 }
93
Sunny Goyalfdbef272017-02-01 12:52:54 -080094 mContext.registerReceiver(mModel, filter);
95 UserManagerCompat.getInstance(mContext).enableAndResetCache();
Tony Wickham827cef22016-03-17 15:39:39 -070096
Tonyd48710c2017-07-27 23:23:58 -070097 if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
98 mNotificationBadgingObserver = null;
99 } else {
100 // Register an observer to rebind the notification listener when badging is re-enabled.
101 mNotificationBadgingObserver = new SettingsObserver.Secure(
102 mContext.getContentResolver()) {
103 @Override
104 public void onSettingChanged(boolean isNotificationBadgingEnabled) {
105 if (isNotificationBadgingEnabled) {
106 NotificationListener.requestRebind(new ComponentName(
107 mContext, NotificationListener.class));
108 }
109 }
110 };
111 mNotificationBadgingObserver.register(NOTIFICATION_BADGING);
112 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400113 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100114
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400115 /**
116 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
117 */
118 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800119 mContext.unregisterReceiver(mModel);
120 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100121 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800122 PackageInstallerCompat.getInstance(mContext).onStop();
Tonyd48710c2017-07-27 23:23:58 -0700123 if (mNotificationBadgingObserver != null) {
124 mNotificationBadgingObserver.unregister();
125 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 }
127
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400128 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800129 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400130 mModel.initialize(launcher);
131 return mModel;
132 }
133
Sunny Goyal34942622014-08-29 17:20:55 -0700134 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 return mIconCache;
136 }
137
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700138 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 return mModel;
140 }
141
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700142 public WidgetPreviewLoader getWidgetCache() {
143 return mWidgetCache;
144 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100145
Adam Cohen2e6da152015-05-06 11:42:25 -0700146 public InvariantDeviceProfile getInvariantDeviceProfile() {
147 return mInvariantDeviceProfile;
148 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800149
150 /**
151 * Shorthand for {@link #getInvariantDeviceProfile()}
152 */
153 public static InvariantDeviceProfile getIDP(Context context) {
Sunny Goyald0e360a2018-06-29 14:40:18 -0700154 return InvariantDeviceProfile.INSTANCE.get(context);
Sunny Goyal87f784c2017-01-11 10:48:34 -0800155 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800156
157 private static LauncherProvider getLocalProvider(Context context) {
158 try (ContentProviderClient cl = context.getContentResolver()
159 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
160 return (LauncherProvider) cl.getLocalContentProvider();
161 }
162 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400163}