blob: 8351bbfaa43073d5038a7d10b00bcb57b5b27cbb [file] [log] [blame]
Evan Lairdb0506ca2018-03-15 10:34:31 -04001/*
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;
Evan Lairdb0506ca2018-03-15 10:34:31 -040024import android.view.ViewTreeObserver.InternalInsetsInfo;
25import android.view.ViewTreeObserver.OnComputeInternalInsetsListener;
26import android.widget.FrameLayout;
27
28/**
29 * Frame layout that will intercept the touches of children if they want to
30 */
31public 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
Issei Suzuki43190bd2018-08-20 17:28:41 +020078 internalInsetsInfo.touchableRegion.op(unionRegion, Op.UNION);
Evan Lairdb0506ca2018-03-15 10:34:31 -040079 }
80 };
81
82 public interface RegionInterceptableView {
83 default public boolean shouldInterceptTouch() {
84 return false;
85 }
86
87 public Region getInterceptRegion();
88 }
89}