blob: 0136c7087f50262f171e88fb4954495da78b493b [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;
Tonyd48710c2017-07-27 23:23:58 -070030import com.android.launcher3.notification.NotificationListener;
Sunny Goyalae502842016-06-17 08:43:56 -070031import com.android.launcher3.util.ConfigMonitor;
Sunny Goyalfdbef272017-02-01 12:52:54 -080032import com.android.launcher3.util.Preconditions;
Tonyd48710c2017-07-27 23:23:58 -070033import com.android.launcher3.util.SettingsObserver;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080034
Sunny Goyalfdbef272017-02-01 12:52:54 -080035import java.util.concurrent.Callable;
36import java.util.concurrent.ExecutionException;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040037
Tonyd48710c2017-07-27 23:23:58 -070038import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING;
39
Sunny Goyalc6205602015-05-21 20:46:33 -070040public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050041
Sunny Goyalfdbef272017-02-01 12:52:54 -080042 // We do not need any synchronization for this variable as its only written on UI thread.
Daniel Sandlere4f98912013-06-25 15:13:26 -040043 private static LauncherAppState INSTANCE;
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) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040053 if (INSTANCE == null) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080054 if (Looper.myLooper() == Looper.getMainLooper()) {
55 INSTANCE = new LauncherAppState(context.getApplicationContext());
56 } else {
57 try {
58 return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
59 @Override
60 public LauncherAppState call() throws Exception {
61 return LauncherAppState.getInstance(context);
62 }
63 }).get();
64 } catch (InterruptedException|ExecutionException e) {
65 throw new RuntimeException(e);
66 }
67 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040068 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040069 return INSTANCE;
70 }
71
Chris Wrend8fe6de2013-10-04 10:42:14 -040072 public static LauncherAppState getInstanceNoCreate() {
73 return INSTANCE;
74 }
75
Daniel Sandlercc8befa2013-06-11 14:45:48 -040076 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080077 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040078 }
79
Sunny Goyalfdbef272017-02-01 12:52:54 -080080 private LauncherAppState(Context context) {
81 if (getLocalProvider(context) == null) {
82 throw new RuntimeException(
83 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040084 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070085 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080086 Preconditions.assertUIThread();
87 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040088
Sunny Goyalfdbef272017-02-01 12:52:54 -080089 mInvariantDeviceProfile = new InvariantDeviceProfile(mContext);
90 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
91 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -070092 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -070093
Sunny Goyalfdbef272017-02-01 12:52:54 -080094 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -040095
96 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +010097 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -040098 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -070099 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700100 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
101 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
102 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
103 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
104 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700105
Sunny Goyalfdbef272017-02-01 12:52:54 -0800106 mContext.registerReceiver(mModel, filter);
107 UserManagerCompat.getInstance(mContext).enableAndResetCache();
108 new ConfigMonitor(mContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700109
Tonyd48710c2017-07-27 23:23:58 -0700110 if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
111 mNotificationBadgingObserver = null;
112 } else {
113 // Register an observer to rebind the notification listener when badging is re-enabled.
114 mNotificationBadgingObserver = new SettingsObserver.Secure(
115 mContext.getContentResolver()) {
116 @Override
117 public void onSettingChanged(boolean isNotificationBadgingEnabled) {
118 if (isNotificationBadgingEnabled) {
119 NotificationListener.requestRebind(new ComponentName(
120 mContext, NotificationListener.class));
121 }
122 }
123 };
124 mNotificationBadgingObserver.register(NOTIFICATION_BADGING);
125 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400126 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100127
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400128 /**
129 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
130 */
131 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800132 mContext.unregisterReceiver(mModel);
133 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100134 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800135 PackageInstallerCompat.getInstance(mContext).onStop();
Tonyd48710c2017-07-27 23:23:58 -0700136 if (mNotificationBadgingObserver != null) {
137 mNotificationBadgingObserver.unregister();
138 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400139 }
140
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400141 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800142 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400143 mModel.initialize(launcher);
144 return mModel;
145 }
146
Sunny Goyal34942622014-08-29 17:20:55 -0700147 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400148 return mIconCache;
149 }
150
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700151 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400152 return mModel;
153 }
154
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700155 public WidgetPreviewLoader getWidgetCache() {
156 return mWidgetCache;
157 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100158
Adam Cohen2e6da152015-05-06 11:42:25 -0700159 public InvariantDeviceProfile getInvariantDeviceProfile() {
160 return mInvariantDeviceProfile;
161 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800162
163 /**
164 * Shorthand for {@link #getInvariantDeviceProfile()}
165 */
166 public static InvariantDeviceProfile getIDP(Context context) {
167 return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
168 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800169
170 private static LauncherProvider getLocalProvider(Context context) {
171 try (ContentProviderClient cl = context.getContentResolver()
172 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
173 return (LauncherProvider) cl.getLocalContentProvider();
174 }
175 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400176}