1 /* <lambda>null2 * Copyright (C) 2022 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 17 package com.android.systemui.statusbar.phone 18 19 import android.graphics.Rect 20 import android.view.View 21 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentComponent 22 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentModule.END_SIDE_CONTENT 23 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentModule.START_SIDE_CONTENT 24 import com.android.systemui.statusbar.phone.fragment.dagger.StatusBarFragmentScope 25 import com.android.systemui.util.ListenerSet 26 import com.android.systemui.util.boundsOnScreen 27 import javax.inject.Inject 28 import javax.inject.Named 29 30 /** 31 * Provides the bounds of the **content** on each side of the status bar. 32 * 33 * This is distinct from [StatusBarContentInsetsProvider], which provides the bounds of full status 34 * bar after accounting for system insets. 35 */ 36 @StatusBarFragmentScope 37 class StatusBarBoundsProvider 38 @Inject 39 constructor( 40 @Named(START_SIDE_CONTENT) private val startSideContent: View, 41 @Named(END_SIDE_CONTENT) private val endSideContent: View, 42 ) : StatusBarFragmentComponent.Startable { 43 44 interface BoundsChangeListener { 45 fun onStatusBarBoundsChanged(bounds: BoundsPair) 46 } 47 48 private val changeListeners = ListenerSet<BoundsChangeListener>() 49 50 fun addChangeListener(listener: BoundsChangeListener) { 51 changeListeners.addIfAbsent(listener) 52 listener.onStatusBarBoundsChanged(previousBounds) 53 } 54 55 private var previousBounds = 56 BoundsPair(start = startSideContent.boundsOnScreen, end = endSideContent.boundsOnScreen) 57 58 private val layoutListener = 59 View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> 60 val newBounds = BoundsPair(start = visibleStartSideBounds, end = visibleEndSideBounds) 61 if (previousBounds != newBounds) { 62 previousBounds = newBounds 63 changeListeners.forEach { it.onStatusBarBoundsChanged(newBounds) } 64 } 65 } 66 67 override fun start() { 68 startSideContent.addOnLayoutChangeListener(layoutListener) 69 endSideContent.addOnLayoutChangeListener(layoutListener) 70 } 71 72 override fun stop() { 73 startSideContent.removeOnLayoutChangeListener(layoutListener) 74 endSideContent.removeOnLayoutChangeListener(layoutListener) 75 } 76 77 /** 78 * Returns the bounds of the end side of the status bar that are visible to the user. The end 79 * side is right when in LTR and is left when in RTL. 80 * 81 * Note that even though the layout might be larger, here we only return the bounds for what is 82 * visible to the user. 83 * 84 * The end side of the status bar contains the multi-user switcher and status icons such as 85 * wi-fi, battery, etc 86 */ 87 val visibleEndSideBounds: Rect 88 get() = endSideContent.boundsOnScreen 89 90 /** 91 * Returns the bounds of the start side of the status bar that are visible to the user. The 92 * start side is left when in LTR and is right when in RTL. 93 * 94 * Note that even though the layout might be larger, here we only return the bounds for what is 95 * visible to the user. 96 * 97 * The start side of the status bar contains the operator name, clock, on-going call chip, and 98 * notifications. 99 */ 100 val visibleStartSideBounds: Rect 101 get() = startSideContent.boundsOnScreen 102 } 103 104 /** 105 * Stores bounds of the status content. 106 * 107 * @property start the bounds of the status bar content on the start side (clock & notif icons). 108 * @property end the bounds of the status bar content on the end side (system icons & battery). 109 */ 110 data class BoundsPair(val start: Rect, val end: Rect) 111