1 /*
2  * Copyright (C) 2024 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.car.customization.tool.ui
18 
19 import android.content.Context
20 import android.util.AttributeSet
21 import android.view.MotionEvent
22 import android.widget.LinearLayout
23 
24 /**
25  * Represents the main layout view of the tool. A [LinearLayout] with an additional touch listener
26  * that returns true or false when touches are triggered inside or outside the layout.
27  */
28 class MainLayoutView : LinearLayout {
29 
30     constructor(context: Context) : super(context)
31     constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
32     constructor(
33         context: Context,
34         attrs: AttributeSet,
35         defStyleAttr: Int
36     ) : super(context, attrs, defStyleAttr)
37 
38     private var touchListener: ((inside: Boolean) -> Unit)? = null
39 
setTouchListenernull40     fun setTouchListener(listener: (inside: Boolean) -> Unit) {
41         touchListener = listener
42     }
43 
onTouchEventnull44     override fun onTouchEvent(event: MotionEvent): Boolean {
45         if (event.action == MotionEvent.ACTION_DOWN) touchListener?.invoke(/* inside = */true)
46         if (event.action == MotionEvent.ACTION_OUTSIDE) touchListener?.invoke(/* inside = */false)
47         return super.onTouchEvent(event)
48     }
49 
onInterceptTouchEventnull50     override fun onInterceptTouchEvent(event: MotionEvent): Boolean {
51         if (event.action == MotionEvent.ACTION_DOWN) touchListener?.invoke(/* inside = */true)
52         if (event.action == MotionEvent.ACTION_OUTSIDE) touchListener?.invoke(/* inside = */false)
53         return super.onInterceptTouchEvent(event)
54     }
55 
56 }