blob: 1ffe41bc6c188b64e2463132c8b17cae816dd11a [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 Goyal3d706ad2017-03-06 16:56:39 -080030import com.android.launcher3.config.FeatureFlags;
Tony Wickham827cef22016-03-17 15:39:39 -070031import com.android.launcher3.dynamicui.ExtractionUtils;
Tonyd48710c2017-07-27 23:23:58 -070032import com.android.launcher3.notification.NotificationListener;
Sunny Goyalae502842016-06-17 08:43:56 -070033import com.android.launcher3.util.ConfigMonitor;
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 Goyal322d5562015-06-25 19:35:49 -070036import com.android.launcher3.util.TestingUtils;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080037
Sunny Goyalfdbef272017-02-01 12:52:54 -080038import java.util.concurrent.Callable;
39import java.util.concurrent.ExecutionException;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040040
Tonyd48710c2017-07-27 23:23:58 -070041import static com.android.launcher3.SettingsActivity.NOTIFICATION_BADGING;
42
Sunny Goyalc6205602015-05-21 20:46:33 -070043public class LauncherAppState {
Chris Wrenaeff7ea2014-02-14 16:59:24 -050044
Sunny Goyal3d706ad2017-03-06 16:56:39 -080045 public static final boolean PROFILE_STARTUP = FeatureFlags.IS_DOGFOOD_BUILD;
Sunny Goyale26d1002016-06-20 14:52:14 -070046
Sunny Goyalfdbef272017-02-01 12:52:54 -080047 // We do not need any synchronization for this variable as its only written on UI thread.
Daniel Sandlere4f98912013-06-25 15:13:26 -040048 private static LauncherAppState INSTANCE;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040049
Sunny Goyalfdbef272017-02-01 12:52:54 -080050 private final Context mContext;
51 private final LauncherModel mModel;
52 private final IconCache mIconCache;
53 private final WidgetPreviewLoader mWidgetCache;
54 private final InvariantDeviceProfile mInvariantDeviceProfile;
Tonyd48710c2017-07-27 23:23:58 -070055 private final SettingsObserver mNotificationBadgingObserver;
Sunny Goyalfdbef272017-02-01 12:52:54 -080056
57 public static LauncherAppState getInstance(final Context context) {
Daniel Sandlere4f98912013-06-25 15:13:26 -040058 if (INSTANCE == null) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080059 if (Looper.myLooper() == Looper.getMainLooper()) {
60 INSTANCE = new LauncherAppState(context.getApplicationContext());
61 } else {
62 try {
63 return new MainThreadExecutor().submit(new Callable<LauncherAppState>() {
64 @Override
65 public LauncherAppState call() throws Exception {
66 return LauncherAppState.getInstance(context);
67 }
68 }).get();
69 } catch (InterruptedException|ExecutionException e) {
70 throw new RuntimeException(e);
71 }
72 }
Daniel Sandlere4f98912013-06-25 15:13:26 -040073 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040074 return INSTANCE;
75 }
76
Chris Wrend8fe6de2013-10-04 10:42:14 -040077 public static LauncherAppState getInstanceNoCreate() {
78 return INSTANCE;
79 }
80
Daniel Sandlercc8befa2013-06-11 14:45:48 -040081 public Context getContext() {
Sunny Goyalfdbef272017-02-01 12:52:54 -080082 return mContext;
Daniel Sandlercc8befa2013-06-11 14:45:48 -040083 }
84
Sunny Goyalfdbef272017-02-01 12:52:54 -080085 private LauncherAppState(Context context) {
86 if (getLocalProvider(context) == null) {
87 throw new RuntimeException(
88 "Initializing LauncherAppState in the absence of LauncherProvider");
Daniel Sandlere4f98912013-06-25 15:13:26 -040089 }
Hyunyoung Song0de01172016-10-05 16:27:48 -070090 Log.v(Launcher.TAG, "LauncherAppState initiated");
Sunny Goyalfdbef272017-02-01 12:52:54 -080091 Preconditions.assertUIThread();
92 mContext = context;
Daniel Sandlerb9eb2862013-06-14 20:17:30 -040093
Sunny Goyal322d5562015-06-25 19:35:49 -070094 if (TestingUtils.MEMORY_DUMP_ENABLED) {
Sunny Goyalfdbef272017-02-01 12:52:54 -080095 TestingUtils.startTrackingMemory(mContext);
Daniel Sandlere4f98912013-06-25 15:13:26 -040096 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -040097
Sunny Goyalfdbef272017-02-01 12:52:54 -080098 mInvariantDeviceProfile = new InvariantDeviceProfile(mContext);
99 mIconCache = new IconCache(mContext, mInvariantDeviceProfile);
100 mWidgetCache = new WidgetPreviewLoader(mContext, mIconCache);
Sunny Goyalc6e97692017-06-02 13:46:55 -0700101 mModel = new LauncherModel(this, mIconCache, AppFilter.newInstance(mContext));
Sunny Goyal27595792015-03-19 13:18:44 -0700102
Sunny Goyalfdbef272017-02-01 12:52:54 -0800103 LauncherAppsCompat.getInstance(mContext).addOnAppsChangedCallback(mModel);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400104
105 // Register intent receivers
Kenny Guyed131872014-04-30 03:02:21 +0100106 IntentFilter filter = new IntentFilter();
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400107 filter.addAction(Intent.ACTION_LOCALE_CHANGED);
Sunny Goyal957c13f2015-05-01 13:02:20 -0700108 // For handling managed profiles
Sunny Goyald3b87ef2016-07-28 12:11:54 -0700109 filter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED);
110 filter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED);
111 filter.addAction(Intent.ACTION_MANAGED_PROFILE_AVAILABLE);
112 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNAVAILABLE);
113 filter.addAction(Intent.ACTION_MANAGED_PROFILE_UNLOCKED);
Tony Wickham827cef22016-03-17 15:39:39 -0700114 // For extracting colors from the wallpaper
Sunny Goyalf5e37442016-11-02 10:31:24 -0700115 if (Utilities.ATLEAST_NOUGAT) {
Tony Wickham827cef22016-03-17 15:39:39 -0700116 // TODO: add a broadcast entry to the manifest for pre-N.
117 filter.addAction(Intent.ACTION_WALLPAPER_CHANGED);
118 }
Sunny Goyal957c13f2015-05-01 13:02:20 -0700119
Sunny Goyalfdbef272017-02-01 12:52:54 -0800120 mContext.registerReceiver(mModel, filter);
121 UserManagerCompat.getInstance(mContext).enableAndResetCache();
122 new ConfigMonitor(mContext).register();
Tony Wickham827cef22016-03-17 15:39:39 -0700123
Sunny Goyalfdbef272017-02-01 12:52:54 -0800124 ExtractionUtils.startColorExtractionServiceIfNecessary(mContext);
Tonyd48710c2017-07-27 23:23:58 -0700125
126 if (!mContext.getResources().getBoolean(R.bool.notification_badging_enabled)) {
127 mNotificationBadgingObserver = null;
128 } else {
129 // Register an observer to rebind the notification listener when badging is re-enabled.
130 mNotificationBadgingObserver = new SettingsObserver.Secure(
131 mContext.getContentResolver()) {
132 @Override
133 public void onSettingChanged(boolean isNotificationBadgingEnabled) {
134 if (isNotificationBadgingEnabled) {
135 NotificationListener.requestRebind(new ComponentName(
136 mContext, NotificationListener.class));
137 }
138 }
139 };
140 mNotificationBadgingObserver.register(NOTIFICATION_BADGING);
141 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400142 }
Kenny Guy1317e2d2014-05-08 18:52:50 +0100143
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400144 /**
145 * Call from Application.onTerminate(), which is not guaranteed to ever be called.
146 */
147 public void onTerminate() {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800148 mContext.unregisterReceiver(mModel);
149 final LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(mContext);
Kenny Guyc2bd8102014-06-30 12:30:31 +0100150 launcherApps.removeOnAppsChangedCallback(mModel);
Sunny Goyalfdbef272017-02-01 12:52:54 -0800151 PackageInstallerCompat.getInstance(mContext).onStop();
Tonyd48710c2017-07-27 23:23:58 -0700152 if (mNotificationBadgingObserver != null) {
153 mNotificationBadgingObserver.unregister();
154 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400155 }
156
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400157 LauncherModel setLauncher(Launcher launcher) {
Sunny Goyalfdbef272017-02-01 12:52:54 -0800158 getLocalProvider(mContext).setLauncherProviderChangeListener(launcher);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400159 mModel.initialize(launcher);
160 return mModel;
161 }
162
Sunny Goyal34942622014-08-29 17:20:55 -0700163 public IconCache getIconCache() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400164 return mIconCache;
165 }
166
Sunny Goyal18bf8e22015-04-08 18:13:46 -0700167 public LauncherModel getModel() {
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400168 return mModel;
169 }
170
Sunny Goyal5b0e6692015-03-19 14:31:19 -0700171 public WidgetPreviewLoader getWidgetCache() {
172 return mWidgetCache;
173 }
Michael Jurkaa6a05472013-11-13 17:59:46 +0100174
Adam Cohen2e6da152015-05-06 11:42:25 -0700175 public InvariantDeviceProfile getInvariantDeviceProfile() {
176 return mInvariantDeviceProfile;
177 }
Sunny Goyal87f784c2017-01-11 10:48:34 -0800178
179 /**
180 * Shorthand for {@link #getInvariantDeviceProfile()}
181 */
182 public static InvariantDeviceProfile getIDP(Context context) {
183 return LauncherAppState.getInstance(context).getInvariantDeviceProfile();
184 }
Sunny Goyalfdbef272017-02-01 12:52:54 -0800185
186 private static LauncherProvider getLocalProvider(Context context) {
187 try (ContentProviderClient cl = context.getContentResolver()
188 .acquireContentProviderClient(LauncherProvider.AUTHORITY)) {
189 return (LauncherProvider) cl.getLocalContentProvider();
190 }
191 }
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400192}