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