1 /*
2  * 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.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 
31 /**
32  * Responsible for creating the status bar window and initializing the root components of that
33  * window (see [CollapsedStatusBarFragment])
34  */
35 @SysUISingleton
36 class StatusBarInitializer @Inject constructor(
37     private val windowController: StatusBarWindowController,
38     private val creationListeners: Set<@JvmSuppressWildcards OnStatusBarViewInitializedListener>,
39 ) {
40 
41     var statusBarViewUpdatedListener: OnStatusBarViewUpdatedListener? = null
42 
43     /**
44      * Creates the status bar window and root views, and initializes the component.
45      *
46      * TODO(b/277762009): Inject StatusBarFragmentCreator and make this class a CoreStartable.
47      */
48     fun initializeStatusBar(
49         statusBarFragmentCreator: () -> CollapsedStatusBarFragment,
50     ) {
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                 }).fragmentManager
71                 .beginTransaction()
72                 .replace(R.id.status_bar_container,
73                         statusBarFragmentCreator.invoke(),
74                         CollapsedStatusBarFragment.TAG)
75                 .commit()
76     }
77 
78     interface OnStatusBarViewInitializedListener {
79 
80         /**
81          * The status bar view has been initialized.
82          *
83          * @param component Dagger component that is created when the status bar view is created.
84          * Can be used to retrieve dependencies from that scope, including the status bar root view.
85          */
86         fun onStatusBarViewInitialized(component: StatusBarFragmentComponent)
87     }
88 
89     interface OnStatusBarViewUpdatedListener {
90         fun onStatusBarViewUpdated(
91             statusBarView: PhoneStatusBarView,
92             statusBarViewController: PhoneStatusBarViewController,
93             statusBarTransitions: PhoneStatusBarTransitions
94         )
95     }
96 }
97