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