1 /* <lambda>null2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.systemui.statusbar.core 17 18 import android.app.Fragment 19 import com.android.systemui.R 20 import com.android.systemui.fragments.FragmentHostManager 21 import com.android.systemui.statusbar.phone.PhoneStatusBarTransitions 22 import com.android.systemui.statusbar.phone.PhoneStatusBarView 23 import com.android.systemui.statusbar.phone.PhoneStatusBarViewController 24 import com.android.systemui.statusbar.phone.dagger.CentralSurfacesComponent 25 import com.android.systemui.statusbar.phone.dagger.CentralSurfacesComponent.CentralSurfacesScope 26 import com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment 27 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentComponent 28 import com.android.systemui.statusbar.window.StatusBarWindowController 29 import java.lang.IllegalStateException 30 import javax.inject.Inject 31 32 /** 33 * Responsible for creating the status bar window and initializing the root components of that 34 * window (see [CollapsedStatusBarFragment]) 35 */ 36 @CentralSurfacesScope 37 class StatusBarInitializer @Inject constructor( 38 private val windowController: StatusBarWindowController, 39 private val creationListeners: Set<@JvmSuppressWildcards OnStatusBarViewInitializedListener>, 40 ) { 41 42 var statusBarViewUpdatedListener: OnStatusBarViewUpdatedListener? = null 43 44 /** 45 * Creates the status bar window and root views, and initializes the component 46 */ 47 fun initializeStatusBar( 48 centralSurfacesComponent: CentralSurfacesComponent 49 ) { 50 windowController.fragmentHostManager.addTagListener( 51 CollapsedStatusBarFragment.TAG, 52 object : FragmentHostManager.FragmentListener { 53 override fun onFragmentViewCreated(tag: String, fragment: Fragment) { 54 val statusBarFragmentComponent = (fragment as CollapsedStatusBarFragment) 55 .statusBarFragmentComponent ?: throw IllegalStateException() 56 statusBarViewUpdatedListener?.onStatusBarViewUpdated( 57 statusBarFragmentComponent.phoneStatusBarView, 58 statusBarFragmentComponent.phoneStatusBarViewController, 59 statusBarFragmentComponent.phoneStatusBarTransitions 60 ) 61 creationListeners.forEach { listener -> 62 listener.onStatusBarViewInitialized(statusBarFragmentComponent) 63 } 64 } 65 66 override fun onFragmentViewDestroyed(tag: String?, fragment: Fragment?) { 67 // nop 68 } 69 }).fragmentManager 70 .beginTransaction() 71 .replace(R.id.status_bar_container, 72 centralSurfacesComponent.createCollapsedStatusBarFragment(), 73 CollapsedStatusBarFragment.TAG) 74 .commit() 75 } 76 77 interface OnStatusBarViewInitializedListener { 78 79 /** 80 * The status bar view has been initialized. 81 * 82 * @param component Dagger component that is created when the status bar view is created. 83 * Can be used to retrieve dependencies from that scope, including the status bar root view. 84 */ 85 fun onStatusBarViewInitialized(component: StatusBarFragmentComponent) 86 } 87 88 interface OnStatusBarViewUpdatedListener { 89 fun onStatusBarViewUpdated( 90 statusBarView: PhoneStatusBarView, 91 statusBarViewController: PhoneStatusBarViewController, 92 statusBarTransitions: PhoneStatusBarTransitions 93 ) 94 } 95 } 96