blob: a46692b0b500d12ebd08e9d3b0419e9e97c56836 [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
Tonyd48710c2017-07-27 23:23:58 -070019import android.content.ComponentName;
Sunny Goyalfdbef272017-02-01 12:52:54 -080020import android.content.ContentProviderClient;
Sunny Goyale755d462014-07-22 13:48:29 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
Sunny Goyalfdbef272017-02-01 12:52:54 -080024import android.os.Looper;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040025import android.util.Log;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080026
Kenny Guyed131872014-04-30 03:02:21 +010027import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal4390ace2014-10-13 11:33:11 -070028import com.android.launcher3.compat.PackageInstallerCompat;
Sunny Goyal823fd502015-08-04 11:40:13 -070029import com.android.launcher3.compat.UserManagerCompat;
Sunny Goyal29947f02017-12-18 13:49:44 -080030import com.android.launcher3.config.FeatureFlags;
Tonyd48710c2017-07-27 23:23:58 -070031import com.android.launcher3.notification.NotificationListener;
Sunny Goyalae502842016-06-17 08:43:56 -070032import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfdbef272017-02-01 12:52:54 -080033import com.android.launcher3.util.Preconditions;
Tonyd48710c2017-07-27 23:23:58 -070034import com.android.launcher3.util.SettingsObserver;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080035
Sunny Goyalfdbef272017-02-01 12:52:54 -080036import java.util.concurrent.Callable;
37import java.util.concurrent.ExecutionException;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040038
Tonyd48710c2017-07-27 23:23:58 -070039import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING;
40
Sunny Goyalc6205602015-05-21 20:46:33 -070041public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050042
Sunny Goyal29947f02017-12-18 13:49:44 -080043 public static final String ACTION_FORCE_ROLOAD = "force-reload-launcher";
44
Sunny Goyalfdbef272017-02-01 12:52:54 -080045 // We do not need any synchronization for this variable as its only written on UI thread.
Daniel Sandlere4f98912013-06-25 15:13:26 -040046 private static LauncherAppState INSTANCE;
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;
Tonyd48710c2017-07-27 23:23:58 -070053 private final SettingsObserver mNotificationBadgingObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080054
55 public static LauncherAppState getInstance(final Context context) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040056 if (INSTANCE == null) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080057 if (Looper.myLooper() == Looper.getMainLooper()) {
58 INSTANCE = new LauncherAppState(context.getApplicationContext());
59 } else {
60 try {
61 return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
62 @Override
63 public LauncherAppState call() throws Exception {
64 return LauncherAppState.getInstance(context);
65 }
66 }).get();
67 } catch (InterruptedException|ExecutionException e) {
68 throw new RuntimeException(e);
69 }
70 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040071 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040072 return INSTANCE;
73 }
74
Chris Wrend8fe6de2013-10-04 10:42:14 -040075 public static LauncherAppState getInstanceNoCreate() {
76 return INSTANCE;
77 }
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
Sunny Goyalfdbef272017-02-01 12:52:54 -080083 private LauncherAppState(Context context) {
84 if (getLocalProvider(context) == null) {
85 throw new RuntimeException(
86 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040087 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070088 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080089 Preconditions.assertUIThread();
90 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040091
Sunny Goyalfdbef272017-02-01 12:52:54 -080092 mInvariantDeviceProfile = new InvariantDeviceProfile(mContext);
93 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
94 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070095 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070096
Sunny Goyalfdbef272017-02-01 12:52:54 -080097 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098
99 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100100 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400101 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700102 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700103 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
104 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
105 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
106 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
107 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700108
Sunny Goyal29947f02017-12-18 13:49:44 -0800109 if (FeatureFlags.IS_DOGFOOD_BUILD) {
110 filter.addAction(ACTION_FORCE_ROLOAD);
111 }
112
Sunny Goyalfdbef272017-02-01 12:52:54 -0800113 mContext.registerReceiver(mModel, filter);
114 UserManagerCompat.getInstance(mContext).enableAndResetCache();
115 new ConfigMonitor(mContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700116
Tonyd48710c2017-07-27 23:23:58 -0700117 if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
118 mNotificationBadgingObserver = null;
119 } else {
120 // Register an observer to rebind the notification listener when badging is re-enabled.
121 mNotificationBadgingObserver = new SettingsObserver.Secure(
122 mContext.getContentResolver()) {
123 @Override
124 public void onSettingChanged(boolean isNotificationBadgingEnabled) {
125 if (isNotificationBadgingEnabled) {
126 NotificationListener.requestRebind(new ComponentName(
127 mContext, NotificationListener.class));
128 }
129 }
130 };
131 mNotificationBadgingObserver.register(NOTIFICATION_BADGING);
132 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400133 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100134
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400135 /**
136 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
137 */
138 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800139 mContext.unregisterReceiver(mModel);
140 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100141 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800142 PackageInstallerCompat.getInstance(mContext).onStop();
Tonyd48710c2017-07-27 23:23:58 -0700143 if (mNotificationBadgingObserver != null) {
144 mNotificationBadgingObserver.unregister();
145 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400146 }
147
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400148 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800149 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400150 mModel.initialize(launcher);
151 return mModel;
152 }
153
Sunny Goyal34942622014-08-29 17:20:55 -0700154 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 return mIconCache;
156 }
157
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700158 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 return mModel;
160 }
161
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700162 public WidgetPreviewLoader getWidgetCache() {
163 return mWidgetCache;
164 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100165
Adam Cohen2e6da152015-05-06 11:42:25 -0700166 public InvariantDeviceProfile getInvariantDeviceProfile() {
167 return mInvariantDeviceProfile;
168 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800169
170 /**
171 * Shorthand for {@link #getInvariantDeviceProfile()}
172 */
173 public static InvariantDeviceProfile getIDP(Context context) {
174 return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
175 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800176
177 private static LauncherProvider getLocalProvider(Context context) {
178 try (ContentProviderClient cl = context.getContentResolver()
179 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
180 return (LauncherProvider) cl.getLocalContentProvider();
181 }
182 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400183}