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