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.res.R 20 import com.android.systemui.dagger.SysUISingleton 21 import com.android.systemui.fragments.FragmentHostManager 22 import com.android.systemui.statusbar.phone.PhoneStatusBarTransitions 23 import com.android.systemui.statusbar.phone.PhoneStatusBarView 24 import com.android.systemui.statusbar.phone.PhoneStatusBarViewController 25 import com.android.systemui.statusbar.phone.fragment.CollapsedStatusBarFragment 26 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentComponent 27 import com.android.systemui.statusbar.window.StatusBarWindowController 28 import java.lang.IllegalStateException 29 import javax.inject.Inject 30 import javax.inject.Provider 31 32 /** 33 * Responsible for creating the status bar window and initializing the root components of that 34 * window (see [CollapsedStatusBarFragment]) 35 */ 36 @SysUISingleton 37 class StatusBarInitializer @Inject constructor( 38 private val windowController: StatusBarWindowController, 39 private val collapsedStatusBarFragmentProvider: Provider<CollapsedStatusBarFragment>, 40 private val creationListeners: Set<@JvmSuppressWildcards OnStatusBarViewInitializedListener>, 41 ) { 42 43 var statusBarViewUpdatedListener: OnStatusBarViewUpdatedListener? = null 44 45 /** 46 * Creates the status bar window and root views, and initializes the component. 47 * 48 * TODO(b/277764509): Initialize the status bar via [CoreStartable#start]. 49 */ 50 fun initializeStatusBar() { 51 windowController.fragmentHostManager.addTagListener( 52 CollapsedStatusBarFragment.TAG, 53 object : FragmentHostManager.FragmentListener { 54 override fun onFragmentViewCreated(tag: String, fragment: Fragment) { 55 val statusBarFragmentComponent = (fragment as CollapsedStatusBarFragment) 56 .statusBarFragmentComponent ?: throw IllegalStateException() 57 statusBarViewUpdatedListener?.onStatusBarViewUpdated( 58 statusBarFragmentComponent.phoneStatusBarView, 59 statusBarFragmentComponent.phoneStatusBarViewController, 60 statusBarFragmentComponent.phoneStatusBarTransitions 61 ) 62 creationListeners.forEach { listener -> 63 listener.onStatusBarViewInitialized(statusBarFragmentComponent) 64 } 65 } 66 67 override fun onFragmentViewDestroyed(tag: String?, fragment: Fragment?) { 68 // nop 69 } 70 } 71 ).fragmentManager 72 .beginTransaction() 73 .replace( 74 R.id.status_bar_container, 75 collapsedStatusBarFragmentProvider.get(), 76 CollapsedStatusBarFragment.TAG 77 ) 78 .commit() 79 } 80 81 interface OnStatusBarViewInitializedListener { 82 83 /** 84 * The status bar view has been initialized. 85 * 86 * @param component Dagger component that is created when the status bar view is created. 87 * Can be used to retrieve dependencies from that scope, including the status bar root view. 88 */ 89 fun onStatusBarViewInitialized(component: StatusBarFragmentComponent) 90 } 91 92 interface OnStatusBarViewUpdatedListener { 93 fun onStatusBarViewUpdated( 94 statusBarView: PhoneStatusBarView, 95 statusBarViewController: PhoneStatusBarViewController, 96 statusBarTransitions: PhoneStatusBarTransitions 97 ) 98 } 99 } 100