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