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.temporarydisplay
18 
19 import android.graphics.Rect
20 import android.view.View
21 import android.view.ViewTreeObserver
22 import com.android.systemui.util.ViewController
23 
24 /**
25  * A view controller that will notify the [ViewTreeObserver] about the touchable region for this
26  * view. This will be used by WindowManager to decide which touch events go to the view and which
27  * pass through to the window below.
28  *
29  * @param touchableRegionSetter a function that, given the view and an out rect, fills the rect with
30  *   the touchable region of this view.
31  */
32 class TouchableRegionViewController(
33     view: View,
34     touchableRegionSetter: (View, Rect) -> Unit,
35 ) : ViewController<View>(view) {
36 
37     private val tempRect = Rect()
38 
39     private val internalInsetsListener =
40         ViewTreeObserver.OnComputeInternalInsetsListener { inoutInfo ->
41             inoutInfo.setTouchableInsets(
42                 ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION
43             )
44 
45             tempRect.setEmpty()
46             touchableRegionSetter.invoke(mView, tempRect)
47             inoutInfo.touchableRegion.set(tempRect)
48         }
49 
50     public override fun onViewAttached() {
51         mView.viewTreeObserver.addOnComputeInternalInsetsListener(internalInsetsListener)
52     }
53 
54     public override fun onViewDetached() {
55         mView.viewTreeObserver.removeOnComputeInternalInsetsListener(internalInsetsListener)
56     }
57 }
58