1/* 2 * Copyright (C) 2017 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 17package com.android.systemui; 18 19import android.content.Context; 20import android.graphics.Region; 21import android.graphics.Region.Op; 22import android.util.AttributeSet; 23import android.view.View; 24import android.view.ViewTreeObserver; 25import android.view.ViewTreeObserver.InternalInsetsInfo; 26import android.view.ViewTreeObserver.OnComputeInternalInsetsListener; 27import android.widget.FrameLayout; 28 29/** 30 * Frame layout that will intercept the touches of children if they want to 31 */ 32public class RegionInterceptingFrameLayout extends FrameLayout { 33 public RegionInterceptingFrameLayout(Context context) { 34 super(context); 35 } 36 37 public RegionInterceptingFrameLayout(Context context, AttributeSet attrs) { 38 super(context, attrs); 39 } 40 41 public RegionInterceptingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 42 super(context, attrs, defStyleAttr); 43 } 44 45 public RegionInterceptingFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, 46 int defStyleRes) { 47 super(context, attrs, defStyleAttr, defStyleRes); 48 } 49 50 @Override 51 protected void onAttachedToWindow() { 52 super.onAttachedToWindow(); 53 getViewTreeObserver().addOnComputeInternalInsetsListener(mInsetsListener); 54 } 55 56 @Override 57 protected void onDetachedFromWindow() { 58 super.onDetachedFromWindow(); 59 getViewTreeObserver().removeOnComputeInternalInsetsListener(mInsetsListener); 60 } 61 62 private final OnComputeInternalInsetsListener mInsetsListener = internalInsetsInfo -> { 63 internalInsetsInfo.setTouchableInsets(InternalInsetsInfo.TOUCHABLE_INSETS_REGION); 64 internalInsetsInfo.touchableRegion.setEmpty(); 65 for (int i = 0; i < getChildCount(); i++) { 66 View child = getChildAt(i); 67 if (!(child instanceof RegionInterceptableView)) { 68 continue; 69 } 70 RegionInterceptableView riv = (RegionInterceptableView) child; 71 if (!riv.shouldInterceptTouch()) { 72 continue; 73 } 74 Region unionRegion = riv.getInterceptRegion(); 75 if (unionRegion == null) { 76 continue; 77 } 78 79 internalInsetsInfo.touchableRegion.op(riv.getInterceptRegion(), Op.UNION); 80 } 81 }; 82 83 public interface RegionInterceptableView { 84 default public boolean shouldInterceptTouch() { 85 return false; 86 } 87 88 public Region getInterceptRegion(); 89 } 90} 91