1 /* 2 * Copyright (C) 2019 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.annotation.NonNull; 20 import android.content.Context; 21 import android.content.res.Configuration; 22 import android.content.res.Resources; 23 import android.graphics.Rect; 24 import android.view.View; 25 import android.view.ViewTreeObserver; 26 import android.view.ViewTreeObserver.OnComputeInternalInsetsListener; 27 28 import com.android.systemui.Dependency; 29 import com.android.systemui.bubbles.BubbleController; 30 import com.android.systemui.statusbar.policy.ConfigurationController; 31 import com.android.systemui.statusbar.policy.ConfigurationController.ConfigurationListener; 32 33 /** 34 * Manages what parts of the status bar are touchable. Clients are primarily UI that displays in the 35 * status bar even though the UI doesn't look like part of the status bar. 36 */ 37 public final class StatusBarTouchableRegionManager implements 38 OnComputeInternalInsetsListener, ConfigurationListener { 39 40 private final BubbleController mBubbleController = Dependency.get(BubbleController.class); 41 private final Context mContext; 42 private final HeadsUpManagerPhone mHeadsUpManager; 43 private boolean mIsStatusBarExpanded = false; 44 private boolean mShouldAdjustInsets = false; 45 private final StatusBar mStatusBar; 46 private int mStatusBarHeight; 47 private final View mStatusBarWindowView; 48 private boolean mForceCollapsedUntilLayout = false; 49 private final StatusBarWindowController mStatusBarWindowController; 50 StatusBarTouchableRegionManager(@onNull Context context, HeadsUpManagerPhone headsUpManager, @NonNull StatusBar statusBar, @NonNull View statusBarWindowView)51 public StatusBarTouchableRegionManager(@NonNull Context context, 52 HeadsUpManagerPhone headsUpManager, 53 @NonNull StatusBar statusBar, 54 @NonNull View statusBarWindowView) { 55 mContext = context; 56 mHeadsUpManager = headsUpManager; 57 mStatusBar = statusBar; 58 mStatusBarWindowView = statusBarWindowView; 59 mStatusBarWindowController = Dependency.get(StatusBarWindowController.class); 60 61 initResources(); 62 63 mBubbleController.setBubbleStateChangeListener((hasBubbles) -> { 64 updateTouchableRegion(); 65 }); 66 67 mStatusBarWindowController.setForcePluginOpenListener((forceOpen) -> { 68 updateTouchableRegion(); 69 }); 70 Dependency.get(ConfigurationController.class).addCallback(this); 71 } 72 73 /** 74 * Set the touchable portion of the status bar based on what elements are visible. 75 */ updateTouchableRegion()76 public void updateTouchableRegion() { 77 boolean hasCutoutInset = (mStatusBarWindowView != null) 78 && (mStatusBarWindowView.getRootWindowInsets() != null) 79 && (mStatusBarWindowView.getRootWindowInsets().getDisplayCutout() != null); 80 boolean shouldObserve = 81 mHeadsUpManager.hasPinnedHeadsUp() || mHeadsUpManager.isHeadsUpGoingAway() 82 || mBubbleController.hasBubbles() 83 || mForceCollapsedUntilLayout 84 || hasCutoutInset 85 || mStatusBarWindowController.getForcePluginOpen(); 86 if (shouldObserve == mShouldAdjustInsets) { 87 return; 88 } 89 90 if (shouldObserve) { 91 mStatusBarWindowView.getViewTreeObserver().addOnComputeInternalInsetsListener(this); 92 mStatusBarWindowView.requestLayout(); 93 } else { 94 mStatusBarWindowView.getViewTreeObserver().removeOnComputeInternalInsetsListener(this); 95 } 96 mShouldAdjustInsets = shouldObserve; 97 } 98 99 /** 100 * Calls {@code updateTouchableRegion()} after a layout pass completes. 101 */ updateTouchableRegionAfterLayout()102 public void updateTouchableRegionAfterLayout() { 103 mForceCollapsedUntilLayout = true; 104 mStatusBarWindowView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 105 @Override 106 public void onLayoutChange(View v, int left, int top, int right, int bottom, 107 int oldLeft, 108 int oldTop, int oldRight, int oldBottom) { 109 if (mStatusBarWindowView.getHeight() <= mStatusBarHeight) { 110 mStatusBarWindowView.removeOnLayoutChangeListener(this); 111 mForceCollapsedUntilLayout = false; 112 updateTouchableRegion(); 113 } 114 } 115 }); 116 } 117 118 /** 119 * Notify that the status bar panel gets expanded or collapsed. 120 * 121 * @param isExpanded True to notify expanded, false to notify collapsed. 122 */ setIsStatusBarExpanded(boolean isExpanded)123 public void setIsStatusBarExpanded(boolean isExpanded) { 124 if (isExpanded != mIsStatusBarExpanded) { 125 mIsStatusBarExpanded = isExpanded; 126 if (isExpanded) { 127 // make sure our state is sane 128 mForceCollapsedUntilLayout = false; 129 } 130 updateTouchableRegion(); 131 } 132 } 133 134 @Override onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo info)135 public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo info) { 136 if (mIsStatusBarExpanded || mStatusBar.isBouncerShowing()) { 137 // The touchable region is always the full area when expanded 138 return; 139 } 140 141 mHeadsUpManager.updateTouchableRegion(info); 142 143 Rect bubbleRect = mBubbleController.getTouchableRegion(); 144 if (bubbleRect != null) { 145 info.touchableRegion.union(bubbleRect); 146 } 147 } 148 149 @Override onConfigChanged(Configuration newConfig)150 public void onConfigChanged(Configuration newConfig) { 151 initResources(); 152 } 153 154 @Override onDensityOrFontScaleChanged()155 public void onDensityOrFontScaleChanged() { 156 initResources(); 157 } 158 159 @Override onOverlayChanged()160 public void onOverlayChanged() { 161 initResources(); 162 } 163 initResources()164 private void initResources() { 165 Resources resources = mContext.getResources(); 166 mStatusBarHeight = resources.getDimensionPixelSize( 167 com.android.internal.R.dimen.status_bar_height); 168 } 169 } 170