1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.systemui;
16 
17 import android.annotation.Nullable;
18 import android.app.AlarmManager;
19 import android.app.INotificationManager;
20 import android.app.IWallpaperManager;
21 import android.hardware.SensorPrivacyManager;
22 import android.hardware.display.NightDisplayListener;
23 import android.os.Handler;
24 import android.os.Looper;
25 import android.util.ArrayMap;
26 import android.util.DisplayMetrics;
27 import android.view.IWindowManager;
28 
29 import com.android.internal.annotations.VisibleForTesting;
30 import com.android.internal.logging.MetricsLogger;
31 import com.android.internal.statusbar.IStatusBarService;
32 import com.android.internal.util.Preconditions;
33 import com.android.keyguard.KeyguardSecurityModel;
34 import com.android.keyguard.KeyguardUpdateMonitor;
35 import com.android.keyguard.clock.ClockManager;
36 import com.android.settingslib.bluetooth.LocalBluetoothManager;
37 import com.android.systemui.appops.AppOpsController;
38 import com.android.systemui.assist.AssistManager;
39 import com.android.systemui.broadcast.BroadcastDispatcher;
40 import com.android.systemui.bubbles.BubbleController;
41 import com.android.systemui.colorextraction.SysuiColorExtractor;
42 import com.android.systemui.dagger.qualifiers.Background;
43 import com.android.systemui.dagger.qualifiers.Main;
44 import com.android.systemui.dock.DockManager;
45 import com.android.systemui.dump.DumpManager;
46 import com.android.systemui.fragments.FragmentService;
47 import com.android.systemui.keyguard.ScreenLifecycle;
48 import com.android.systemui.keyguard.WakefulnessLifecycle;
49 import com.android.systemui.model.SysUiState;
50 import com.android.systemui.plugins.ActivityStarter;
51 import com.android.systemui.plugins.DarkIconDispatcher;
52 import com.android.systemui.plugins.PluginDependencyProvider;
53 import com.android.systemui.plugins.VolumeDialogController;
54 import com.android.systemui.plugins.statusbar.StatusBarStateController;
55 import com.android.systemui.power.EnhancedEstimates;
56 import com.android.systemui.power.PowerUI;
57 import com.android.systemui.recents.OverviewProxyService;
58 import com.android.systemui.recents.Recents;
59 import com.android.systemui.screenrecord.RecordingController;
60 import com.android.systemui.shared.plugins.PluginManager;
61 import com.android.systemui.shared.system.ActivityManagerWrapper;
62 import com.android.systemui.shared.system.DevicePolicyManagerWrapper;
63 import com.android.systemui.shared.system.PackageManagerWrapper;
64 import com.android.systemui.stackdivider.Divider;
65 import com.android.systemui.statusbar.CommandQueue;
66 import com.android.systemui.statusbar.NavigationBarController;
67 import com.android.systemui.statusbar.NotificationListener;
68 import com.android.systemui.statusbar.NotificationLockscreenUserManager;
69 import com.android.systemui.statusbar.NotificationMediaManager;
70 import com.android.systemui.statusbar.NotificationRemoteInputManager;
71 import com.android.systemui.statusbar.NotificationViewHierarchyManager;
72 import com.android.systemui.statusbar.SmartReplyController;
73 import com.android.systemui.statusbar.VibratorHelper;
74 import com.android.systemui.statusbar.notification.NotificationEntryManager;
75 import com.android.systemui.statusbar.notification.NotificationEntryManager.KeyguardEnvironment;
76 import com.android.systemui.statusbar.notification.NotificationFilter;
77 import com.android.systemui.statusbar.notification.VisualStabilityManager;
78 import com.android.systemui.statusbar.notification.logging.NotificationLogger;
79 import com.android.systemui.statusbar.notification.row.NotificationBlockingHelperManager;
80 import com.android.systemui.statusbar.notification.row.NotificationGutsManager;
81 import com.android.systemui.statusbar.phone.AutoHideController;
82 import com.android.systemui.statusbar.phone.DozeParameters;
83 import com.android.systemui.statusbar.phone.KeyguardDismissUtil;
84 import com.android.systemui.statusbar.phone.LightBarController;
85 import com.android.systemui.statusbar.phone.LockscreenGestureLogger;
86 import com.android.systemui.statusbar.phone.ManagedProfileController;
87 import com.android.systemui.statusbar.phone.NavigationModeController;
88 import com.android.systemui.statusbar.phone.NotificationGroupAlertTransferHelper;
89 import com.android.systemui.statusbar.phone.NotificationGroupManager;
90 import com.android.systemui.statusbar.phone.NotificationShadeWindowController;
91 import com.android.systemui.statusbar.phone.ShadeController;
92 import com.android.systemui.statusbar.phone.StatusBar;
93 import com.android.systemui.statusbar.phone.StatusBarIconController;
94 import com.android.systemui.statusbar.phone.StatusBarWindowController;
95 import com.android.systemui.statusbar.policy.AccessibilityController;
96 import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper;
97 import com.android.systemui.statusbar.policy.BatteryController;
98 import com.android.systemui.statusbar.policy.BluetoothController;
99 import com.android.systemui.statusbar.policy.CastController;
100 import com.android.systemui.statusbar.policy.ConfigurationController;
101 import com.android.systemui.statusbar.policy.DataSaverController;
102 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
103 import com.android.systemui.statusbar.policy.ExtensionController;
104 import com.android.systemui.statusbar.policy.FlashlightController;
105 import com.android.systemui.statusbar.policy.HotspotController;
106 import com.android.systemui.statusbar.policy.KeyguardStateController;
107 import com.android.systemui.statusbar.policy.LocationController;
108 import com.android.systemui.statusbar.policy.NetworkController;
109 import com.android.systemui.statusbar.policy.NextAlarmController;
110 import com.android.systemui.statusbar.policy.RemoteInputQuickSettingsDisabler;
111 import com.android.systemui.statusbar.policy.RotationLockController;
112 import com.android.systemui.statusbar.policy.SecurityController;
113 import com.android.systemui.statusbar.policy.SensorPrivacyController;
114 import com.android.systemui.statusbar.policy.SmartReplyConstants;
115 import com.android.systemui.statusbar.policy.UserInfoController;
116 import com.android.systemui.statusbar.policy.UserSwitcherController;
117 import com.android.systemui.statusbar.policy.ZenModeController;
118 import com.android.systemui.tracing.ProtoTracer;
119 import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
120 import com.android.systemui.tuner.TunerService;
121 import com.android.systemui.util.leak.GarbageMonitor;
122 import com.android.systemui.util.leak.LeakDetector;
123 import com.android.systemui.util.leak.LeakReporter;
124 import com.android.systemui.util.sensors.AsyncSensorManager;
125 import com.android.systemui.wm.DisplayController;
126 import com.android.systemui.wm.DisplayImeController;
127 import com.android.systemui.wm.SystemWindows;
128 
129 import java.util.function.Consumer;
130 
131 import javax.inject.Inject;
132 import javax.inject.Named;
133 
134 import dagger.Lazy;
135 import dagger.Subcomponent;
136 
137 /**
138  * Class to handle ugly dependencies throughout sysui until we determine the
139  * long-term dependency injection solution.
140  *
141  * Classes added here should be things that are expected to live the lifetime of sysui,
142  * and are generally applicable to many parts of sysui. They will be lazily
143  * initialized to ensure they aren't created on form factors that don't need them
144  * (e.g. HotspotController on TV). Despite being lazily initialized, it is expected
145  * that all dependencies will be gotten during sysui startup, and not during runtime
146  * to avoid jank.
147  *
148  * All classes used here are expected to manage their own lifecycle, meaning if
149  * they have no clients they should not have any registered resources like bound
150  * services, registered receivers, etc.
151  */
152 public class Dependency {
153     /**
154      * Key for getting a the main looper.
155      */
156     private static final String MAIN_LOOPER_NAME = "main_looper";
157 
158     /**
159      * Key for getting a background Looper for background work.
160      */
161     private static final String BG_LOOPER_NAME = "background_looper";
162     /**
163      * Key for getting a Handler for receiving time tick broadcasts on.
164      */
165     public static final String TIME_TICK_HANDLER_NAME = "time_tick_handler";
166     /**
167      * Generic handler on the main thread.
168      */
169     private static final String MAIN_HANDLER_NAME = "main_handler";
170 
171     /**
172      * An email address to send memory leak reports to by default.
173      */
174     public static final String LEAK_REPORT_EMAIL_NAME = "leak_report_email";
175 
176     /**
177      * Whether this platform supports long-pressing notifications to show notification channel
178      * settings.
179      */
180     public static final String ALLOW_NOTIFICATION_LONG_PRESS_NAME = "allow_notif_longpress";
181 
182     /**
183      * Key for getting a background Looper for background work.
184      */
185     public static final DependencyKey<Looper> BG_LOOPER = new DependencyKey<>(BG_LOOPER_NAME);
186     /**
187      * Key for getting a mainer Looper.
188      */
189     public static final DependencyKey<Looper> MAIN_LOOPER = new DependencyKey<>(MAIN_LOOPER_NAME);
190     /**
191      * Key for getting a Handler for receiving time tick broadcasts on.
192      */
193     public static final DependencyKey<Handler> TIME_TICK_HANDLER =
194             new DependencyKey<>(TIME_TICK_HANDLER_NAME);
195     /**
196      * Generic handler on the main thread.
197      */
198     public static final DependencyKey<Handler> MAIN_HANDLER =
199             new DependencyKey<>(MAIN_HANDLER_NAME);
200 
201     /**
202      * An email address to send memory leak reports to by default.
203      */
204     public static final DependencyKey<String> LEAK_REPORT_EMAIL =
205             new DependencyKey<>(LEAK_REPORT_EMAIL_NAME);
206 
207     private final ArrayMap<Object, Object> mDependencies = new ArrayMap<>();
208     private final ArrayMap<Object, LazyDependencyCreator> mProviders = new ArrayMap<>();
209 
210     @Inject DumpManager mDumpManager;
211 
212     @Inject Lazy<ActivityStarter> mActivityStarter;
213     @Inject Lazy<BroadcastDispatcher> mBroadcastDispatcher;
214     @Inject Lazy<AsyncSensorManager> mAsyncSensorManager;
215     @Inject Lazy<BluetoothController> mBluetoothController;
216     @Inject Lazy<LocationController> mLocationController;
217     @Inject Lazy<RotationLockController> mRotationLockController;
218     @Inject Lazy<NetworkController> mNetworkController;
219     @Inject Lazy<ZenModeController> mZenModeController;
220     @Inject Lazy<HotspotController> mHotspotController;
221     @Inject Lazy<CastController> mCastController;
222     @Inject Lazy<FlashlightController> mFlashlightController;
223     @Inject Lazy<UserSwitcherController> mUserSwitcherController;
224     @Inject Lazy<UserInfoController> mUserInfoController;
225     @Inject Lazy<KeyguardStateController> mKeyguardMonitor;
226     @Inject Lazy<KeyguardUpdateMonitor> mKeyguardUpdateMonitor;
227     @Inject Lazy<BatteryController> mBatteryController;
228     @Inject Lazy<NightDisplayListener> mNightDisplayListener;
229     @Inject Lazy<ManagedProfileController> mManagedProfileController;
230     @Inject Lazy<NextAlarmController> mNextAlarmController;
231     @Inject Lazy<DataSaverController> mDataSaverController;
232     @Inject Lazy<AccessibilityController> mAccessibilityController;
233     @Inject Lazy<DeviceProvisionedController> mDeviceProvisionedController;
234     @Inject Lazy<PluginManager> mPluginManager;
235     @Inject Lazy<AssistManager> mAssistManager;
236     @Inject Lazy<SecurityController> mSecurityController;
237     @Inject Lazy<LeakDetector> mLeakDetector;
238     @Inject Lazy<LeakReporter> mLeakReporter;
239     @Inject Lazy<GarbageMonitor> mGarbageMonitor;
240     @Inject Lazy<TunerService> mTunerService;
241     @Inject Lazy<NotificationShadeWindowController> mNotificationShadeWindowController;
242     @Inject Lazy<StatusBarWindowController> mTempStatusBarWindowController;
243     @Inject Lazy<DarkIconDispatcher> mDarkIconDispatcher;
244     @Inject Lazy<ConfigurationController> mConfigurationController;
245     @Inject Lazy<StatusBarIconController> mStatusBarIconController;
246     @Inject Lazy<ScreenLifecycle> mScreenLifecycle;
247     @Inject Lazy<WakefulnessLifecycle> mWakefulnessLifecycle;
248     @Inject Lazy<FragmentService> mFragmentService;
249     @Inject Lazy<ExtensionController> mExtensionController;
250     @Inject Lazy<PluginDependencyProvider> mPluginDependencyProvider;
251     @Nullable
252     @Inject Lazy<LocalBluetoothManager> mLocalBluetoothManager;
253     @Inject Lazy<VolumeDialogController> mVolumeDialogController;
254     @Inject Lazy<MetricsLogger> mMetricsLogger;
255     @Inject Lazy<AccessibilityManagerWrapper> mAccessibilityManagerWrapper;
256     @Inject Lazy<SysuiColorExtractor> mSysuiColorExtractor;
257     @Inject Lazy<TunablePaddingService> mTunablePaddingService;
258     @Inject Lazy<ForegroundServiceController> mForegroundServiceController;
259     @Inject Lazy<UiOffloadThread> mUiOffloadThread;
260     @Inject Lazy<PowerUI.WarningsUI> mWarningsUI;
261     @Inject Lazy<LightBarController> mLightBarController;
262     @Inject Lazy<IWindowManager> mIWindowManager;
263     @Inject Lazy<OverviewProxyService> mOverviewProxyService;
264     @Inject Lazy<NavigationModeController> mNavBarModeController;
265     @Inject Lazy<EnhancedEstimates> mEnhancedEstimates;
266     @Inject Lazy<VibratorHelper> mVibratorHelper;
267     @Inject Lazy<IStatusBarService> mIStatusBarService;
268     @Inject Lazy<DisplayMetrics> mDisplayMetrics;
269     @Inject Lazy<LockscreenGestureLogger> mLockscreenGestureLogger;
270     @Inject Lazy<KeyguardEnvironment> mKeyguardEnvironment;
271     @Inject Lazy<ShadeController> mShadeController;
272     @Inject Lazy<NotificationRemoteInputManager.Callback> mNotificationRemoteInputManagerCallback;
273     @Inject Lazy<AppOpsController> mAppOpsController;
274     @Inject Lazy<NavigationBarController> mNavigationBarController;
275     @Inject Lazy<StatusBarStateController> mStatusBarStateController;
276     @Inject Lazy<NotificationLockscreenUserManager> mNotificationLockscreenUserManager;
277     @Inject Lazy<NotificationGroupAlertTransferHelper> mNotificationGroupAlertTransferHelper;
278     @Inject Lazy<NotificationGroupManager> mNotificationGroupManager;
279     @Inject Lazy<VisualStabilityManager> mVisualStabilityManager;
280     @Inject Lazy<NotificationGutsManager> mNotificationGutsManager;
281     @Inject Lazy<NotificationMediaManager> mNotificationMediaManager;
282     @Inject Lazy<NotificationBlockingHelperManager> mNotificationBlockingHelperManager;
283     @Inject Lazy<NotificationRemoteInputManager> mNotificationRemoteInputManager;
284     @Inject Lazy<SmartReplyConstants> mSmartReplyConstants;
285     @Inject Lazy<NotificationListener> mNotificationListener;
286     @Inject Lazy<NotificationLogger> mNotificationLogger;
287     @Inject Lazy<NotificationViewHierarchyManager> mNotificationViewHierarchyManager;
288     @Inject Lazy<NotificationFilter> mNotificationFilter;
289     @Inject Lazy<KeyguardDismissUtil> mKeyguardDismissUtil;
290     @Inject Lazy<SmartReplyController> mSmartReplyController;
291     @Inject Lazy<RemoteInputQuickSettingsDisabler> mRemoteInputQuickSettingsDisabler;
292     @Inject Lazy<BubbleController> mBubbleController;
293     @Inject Lazy<NotificationEntryManager> mNotificationEntryManager;
294     @Inject Lazy<SensorPrivacyManager> mSensorPrivacyManager;
295     @Inject Lazy<AutoHideController> mAutoHideController;
296     @Inject Lazy<ForegroundServiceNotificationListener> mForegroundServiceNotificationListener;
297     @Inject @Background Lazy<Looper> mBgLooper;
298     @Inject @Background Lazy<Handler> mBgHandler;
299     @Inject @Main Lazy<Looper> mMainLooper;
300     @Inject @Main Lazy<Handler> mMainHandler;
301     @Inject @Named(TIME_TICK_HANDLER_NAME) Lazy<Handler> mTimeTickHandler;
302     @Nullable
303     @Inject @Named(LEAK_REPORT_EMAIL_NAME) Lazy<String> mLeakReportEmail;
304     @Inject Lazy<ClockManager> mClockManager;
305     @Inject Lazy<ActivityManagerWrapper> mActivityManagerWrapper;
306     @Inject Lazy<DevicePolicyManagerWrapper> mDevicePolicyManagerWrapper;
307     @Inject Lazy<PackageManagerWrapper> mPackageManagerWrapper;
308     @Inject Lazy<SensorPrivacyController> mSensorPrivacyController;
309     @Inject Lazy<DockManager> mDockManager;
310     @Inject Lazy<INotificationManager> mINotificationManager;
311     @Inject Lazy<SysUiState> mSysUiStateFlagsContainer;
312     @Inject Lazy<AlarmManager> mAlarmManager;
313     @Inject Lazy<KeyguardSecurityModel> mKeyguardSecurityModel;
314     @Inject Lazy<DozeParameters> mDozeParameters;
315     @Inject Lazy<IWallpaperManager> mWallpaperManager;
316     @Inject Lazy<CommandQueue> mCommandQueue;
317     @Inject Lazy<Recents> mRecents;
318     @Inject Lazy<StatusBar> mStatusBar;
319     @Inject Lazy<DisplayController> mDisplayController;
320     @Inject Lazy<SystemWindows> mSystemWindows;
321     @Inject Lazy<DisplayImeController> mDisplayImeController;
322     @Inject Lazy<RecordingController> mRecordingController;
323     @Inject Lazy<ProtoTracer> mProtoTracer;
324     @Inject Lazy<Divider> mDivider;
325 
326     @Inject
Dependency()327     public Dependency() {
328     }
329 
330     /**
331      * Initialize Depenency.
332      */
start()333     protected void start() {
334         // TODO: Think about ways to push these creation rules out of Dependency to cut down
335         // on imports.
336         mProviders.put(TIME_TICK_HANDLER, mTimeTickHandler::get);
337         mProviders.put(BG_LOOPER, mBgLooper::get);
338         mProviders.put(MAIN_LOOPER, mMainLooper::get);
339         mProviders.put(MAIN_HANDLER, mMainHandler::get);
340         mProviders.put(ActivityStarter.class, mActivityStarter::get);
341         mProviders.put(BroadcastDispatcher.class, mBroadcastDispatcher::get);
342 
343         mProviders.put(AsyncSensorManager.class, mAsyncSensorManager::get);
344 
345         mProviders.put(BluetoothController.class, mBluetoothController::get);
346         mProviders.put(SensorPrivacyManager.class, mSensorPrivacyManager::get);
347 
348         mProviders.put(LocationController.class, mLocationController::get);
349 
350         mProviders.put(RotationLockController.class, mRotationLockController::get);
351 
352         mProviders.put(NetworkController.class, mNetworkController::get);
353 
354         mProviders.put(ZenModeController.class, mZenModeController::get);
355 
356         mProviders.put(HotspotController.class, mHotspotController::get);
357 
358         mProviders.put(CastController.class, mCastController::get);
359 
360         mProviders.put(FlashlightController.class, mFlashlightController::get);
361 
362         mProviders.put(KeyguardStateController.class, mKeyguardMonitor::get);
363 
364         mProviders.put(KeyguardUpdateMonitor.class, mKeyguardUpdateMonitor::get);
365 
366         mProviders.put(UserSwitcherController.class, mUserSwitcherController::get);
367 
368         mProviders.put(UserInfoController.class, mUserInfoController::get);
369 
370         mProviders.put(BatteryController.class, mBatteryController::get);
371 
372         mProviders.put(NightDisplayListener.class, mNightDisplayListener::get);
373 
374         mProviders.put(ManagedProfileController.class, mManagedProfileController::get);
375 
376         mProviders.put(NextAlarmController.class, mNextAlarmController::get);
377 
378         mProviders.put(DataSaverController.class, mDataSaverController::get);
379 
380         mProviders.put(AccessibilityController.class, mAccessibilityController::get);
381 
382         mProviders.put(DeviceProvisionedController.class, mDeviceProvisionedController::get);
383 
384         mProviders.put(PluginManager.class, mPluginManager::get);
385 
386         mProviders.put(AssistManager.class, mAssistManager::get);
387 
388         mProviders.put(SecurityController.class, mSecurityController::get);
389 
390         mProviders.put(LeakDetector.class, mLeakDetector::get);
391 
392         mProviders.put(LEAK_REPORT_EMAIL, mLeakReportEmail::get);
393 
394         mProviders.put(LeakReporter.class, mLeakReporter::get);
395 
396         mProviders.put(GarbageMonitor.class, mGarbageMonitor::get);
397 
398         mProviders.put(TunerService.class, mTunerService::get);
399 
400         mProviders.put(NotificationShadeWindowController.class,
401                 mNotificationShadeWindowController::get);
402 
403         mProviders.put(StatusBarWindowController.class, mTempStatusBarWindowController::get);
404 
405         mProviders.put(DarkIconDispatcher.class, mDarkIconDispatcher::get);
406 
407         mProviders.put(ConfigurationController.class, mConfigurationController::get);
408 
409         mProviders.put(StatusBarIconController.class, mStatusBarIconController::get);
410 
411         mProviders.put(ScreenLifecycle.class, mScreenLifecycle::get);
412 
413         mProviders.put(WakefulnessLifecycle.class, mWakefulnessLifecycle::get);
414 
415         mProviders.put(FragmentService.class, mFragmentService::get);
416 
417         mProviders.put(ExtensionController.class, mExtensionController::get);
418 
419         mProviders.put(PluginDependencyProvider.class, mPluginDependencyProvider::get);
420 
421         mProviders.put(LocalBluetoothManager.class, mLocalBluetoothManager::get);
422 
423         mProviders.put(VolumeDialogController.class, mVolumeDialogController::get);
424 
425         mProviders.put(MetricsLogger.class, mMetricsLogger::get);
426 
427         mProviders.put(AccessibilityManagerWrapper.class, mAccessibilityManagerWrapper::get);
428 
429         mProviders.put(SysuiColorExtractor.class, mSysuiColorExtractor::get);
430 
431         mProviders.put(TunablePaddingService.class, mTunablePaddingService::get);
432 
433         mProviders.put(ForegroundServiceController.class, mForegroundServiceController::get);
434 
435         mProviders.put(UiOffloadThread.class, mUiOffloadThread::get);
436 
437         mProviders.put(PowerUI.WarningsUI.class, mWarningsUI::get);
438 
439         mProviders.put(LightBarController.class, mLightBarController::get);
440 
441         mProviders.put(IWindowManager.class, mIWindowManager::get);
442 
443         mProviders.put(OverviewProxyService.class, mOverviewProxyService::get);
444 
445         mProviders.put(NavigationModeController.class, mNavBarModeController::get);
446 
447         mProviders.put(EnhancedEstimates.class, mEnhancedEstimates::get);
448 
449         mProviders.put(VibratorHelper.class, mVibratorHelper::get);
450 
451         mProviders.put(IStatusBarService.class, mIStatusBarService::get);
452 
453         mProviders.put(DisplayMetrics.class, mDisplayMetrics::get);
454 
455         mProviders.put(LockscreenGestureLogger.class, mLockscreenGestureLogger::get);
456 
457         mProviders.put(KeyguardEnvironment.class, mKeyguardEnvironment::get);
458         mProviders.put(ShadeController.class, mShadeController::get);
459         mProviders.put(NotificationRemoteInputManager.Callback.class,
460                 mNotificationRemoteInputManagerCallback::get);
461 
462         mProviders.put(AppOpsController.class, mAppOpsController::get);
463 
464         mProviders.put(NavigationBarController.class, mNavigationBarController::get);
465 
466         mProviders.put(StatusBarStateController.class, mStatusBarStateController::get);
467         mProviders.put(NotificationLockscreenUserManager.class,
468                 mNotificationLockscreenUserManager::get);
469         mProviders.put(VisualStabilityManager.class, mVisualStabilityManager::get);
470         mProviders.put(NotificationGroupManager.class, mNotificationGroupManager::get);
471         mProviders.put(NotificationGroupAlertTransferHelper.class,
472                 mNotificationGroupAlertTransferHelper::get);
473         mProviders.put(NotificationMediaManager.class, mNotificationMediaManager::get);
474         mProviders.put(NotificationGutsManager.class, mNotificationGutsManager::get);
475         mProviders.put(NotificationBlockingHelperManager.class,
476                 mNotificationBlockingHelperManager::get);
477         mProviders.put(NotificationRemoteInputManager.class,
478                 mNotificationRemoteInputManager::get);
479         mProviders.put(SmartReplyConstants.class, mSmartReplyConstants::get);
480         mProviders.put(NotificationListener.class, mNotificationListener::get);
481         mProviders.put(NotificationLogger.class, mNotificationLogger::get);
482         mProviders.put(NotificationViewHierarchyManager.class,
483                 mNotificationViewHierarchyManager::get);
484         mProviders.put(NotificationFilter.class, mNotificationFilter::get);
485         mProviders.put(KeyguardDismissUtil.class, mKeyguardDismissUtil::get);
486         mProviders.put(SmartReplyController.class, mSmartReplyController::get);
487         mProviders.put(RemoteInputQuickSettingsDisabler.class,
488                 mRemoteInputQuickSettingsDisabler::get);
489         mProviders.put(BubbleController.class, mBubbleController::get);
490         mProviders.put(NotificationEntryManager.class, mNotificationEntryManager::get);
491         mProviders.put(ForegroundServiceNotificationListener.class,
492                 mForegroundServiceNotificationListener::get);
493         mProviders.put(ClockManager.class, mClockManager::get);
494         mProviders.put(ActivityManagerWrapper.class, mActivityManagerWrapper::get);
495         mProviders.put(DevicePolicyManagerWrapper.class, mDevicePolicyManagerWrapper::get);
496         mProviders.put(PackageManagerWrapper.class, mPackageManagerWrapper::get);
497         mProviders.put(SensorPrivacyController.class, mSensorPrivacyController::get);
498         mProviders.put(DockManager.class, mDockManager::get);
499         mProviders.put(INotificationManager.class, mINotificationManager::get);
500         mProviders.put(SysUiState.class, mSysUiStateFlagsContainer::get);
501         mProviders.put(AlarmManager.class, mAlarmManager::get);
502         mProviders.put(KeyguardSecurityModel.class, mKeyguardSecurityModel::get);
503         mProviders.put(DozeParameters.class, mDozeParameters::get);
504         mProviders.put(IWallpaperManager.class, mWallpaperManager::get);
505         mProviders.put(CommandQueue.class, mCommandQueue::get);
506         mProviders.put(Recents.class, mRecents::get);
507         mProviders.put(StatusBar.class, mStatusBar::get);
508         mProviders.put(DisplayController.class, mDisplayController::get);
509         mProviders.put(SystemWindows.class, mSystemWindows::get);
510         mProviders.put(DisplayImeController.class, mDisplayImeController::get);
511         mProviders.put(ProtoTracer.class, mProtoTracer::get);
512 
513         // TODO(b/118592525): to support multi-display , we start to add something which is
514         //                    per-display, while others may be global. I think it's time to add
515         //                    a new class maybe named DisplayDependency to solve per-display
516         //                    Dependency problem.
517         mProviders.put(AutoHideController.class, mAutoHideController::get);
518 
519         mProviders.put(RecordingController.class, mRecordingController::get);
520         mProviders.put(Divider.class, mDivider::get);
521 
522         sDependency = this;
523     }
524 
getDependency(Class<T> cls)525     protected final <T> T getDependency(Class<T> cls) {
526         return getDependencyInner(cls);
527     }
528 
getDependency(DependencyKey<T> key)529     protected final <T> T getDependency(DependencyKey<T> key) {
530         return getDependencyInner(key);
531     }
532 
getDependencyInner(Object key)533     private synchronized <T> T getDependencyInner(Object key) {
534         @SuppressWarnings("unchecked")
535         T obj = (T) mDependencies.get(key);
536         if (obj == null) {
537             obj = createDependency(key);
538             mDependencies.put(key, obj);
539 
540             // TODO: Get dependencies to register themselves instead
541             if (autoRegisterModulesForDump() && obj instanceof Dumpable) {
542                 mDumpManager.registerDumpable(obj.getClass().getName(), (Dumpable) obj);
543             }
544         }
545         return obj;
546     }
547 
548     @VisibleForTesting
createDependency(Object cls)549     protected <T> T createDependency(Object cls) {
550         Preconditions.checkArgument(cls instanceof DependencyKey<?> || cls instanceof Class<?>);
551 
552         @SuppressWarnings("unchecked")
553         LazyDependencyCreator<T> provider = mProviders.get(cls);
554         if (provider == null) {
555             throw new IllegalArgumentException("Unsupported dependency " + cls
556                     + ". " + mProviders.size() + " providers known.");
557         }
558         return provider.createDependency();
559     }
560 
561     // Currently, there are situations in tests where we might create more than one instance of a
562     // thing that should be a singleton: the "real" one (created by Dagger, usually as a result of
563     // inflating a view), and a mocked one (injected into Dependency). If we register the mocked
564     // one, the DumpManager will throw an exception complaining (rightly) that we have too many
565     // things registered with that name. So in tests, we disable the auto-registration until the
566     // root cause is fixed, i.e. inflated views in tests with Dagger dependencies.
567     @VisibleForTesting
autoRegisterModulesForDump()568     protected boolean autoRegisterModulesForDump() {
569         return true;
570     }
571 
572     private static Dependency sDependency;
573 
574     /**
575      * Interface for a class that can create a dependency. Used to implement laziness
576      * @param <T> The type of the dependency being created
577      */
578     private interface LazyDependencyCreator<T> {
createDependency()579         T createDependency();
580     }
581 
destroyDependency(Class<T> cls, Consumer<T> destroy)582     private <T> void destroyDependency(Class<T> cls, Consumer<T> destroy) {
583         T dep = (T) mDependencies.remove(cls);
584         if (dep instanceof Dumpable) {
585             mDumpManager.unregisterDumpable(dep.getClass().getName());
586         }
587         if (dep != null && destroy != null) {
588             destroy.accept(dep);
589         }
590     }
591 
592     /**
593      * Used in separate process teardown to ensure the context isn't leaked.
594      *
595      * TODO: Remove once PreferenceFragment doesn't reference getActivity()
596      * anymore and these context hacks are no longer needed.
597      */
clearDependencies()598     public static void clearDependencies() {
599         sDependency = null;
600     }
601 
602     /**
603      * Checks to see if a dependency is instantiated, if it is it removes it from
604      * the cache and calls the destroy callback.
605      */
destroy(Class<T> cls, Consumer<T> destroy)606     public static <T> void destroy(Class<T> cls, Consumer<T> destroy) {
607         sDependency.destroyDependency(cls, destroy);
608     }
609 
610     /**
611      * @deprecated see docs/dagger.md
612      */
613     @Deprecated
get(Class<T> cls)614     public static <T> T get(Class<T> cls) {
615         return sDependency.getDependency(cls);
616     }
617 
618     /**
619      * @deprecated see docs/dagger.md
620      */
621     @Deprecated
get(DependencyKey<T> cls)622     public static <T> T get(DependencyKey<T> cls) {
623         return sDependency.getDependency(cls);
624     }
625 
626     public static final class DependencyKey<V> {
627         private final String mDisplayName;
628 
DependencyKey(String displayName)629         public DependencyKey(String displayName) {
630             mDisplayName = displayName;
631         }
632 
633         @Override
toString()634         public String toString() {
635             return mDisplayName;
636         }
637     }
638 
639     @Subcomponent
640     public interface DependencyInjector {
createSystemUI(Dependency dependency)641         void createSystemUI(Dependency dependency);
642     }
643 }
644