1 /*
2  * Copyright (C) 2023 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  */
18 
19 package com.android.healthconnect.controller.utils
20 
21 import android.content.Context
22 import android.graphics.Rect
23 import android.view.TouchDelegate
24 import android.view.View
25 import com.android.healthconnect.controller.R
26 import kotlin.math.max
27 
28 /** Increases a view's touch target size and uses the parentView as the touch delegate. */
increaseViewTouchTargetSizenull29 fun increaseViewTouchTargetSize(context: Context, childView: View, parentView: View) {
30     val largerTouchBounds = Rect()
31     childView.getHitRect(largerTouchBounds)
32     val margin =
33         getTouchTargetMargin(
34             context.resources.getDimension(R.dimen.button_size), largerTouchBounds.height())
35     largerTouchBounds.top -= margin
36     largerTouchBounds.left -= margin
37     largerTouchBounds.bottom += margin
38     largerTouchBounds.right += margin
39     parentView.touchDelegate = TouchDelegate(largerTouchBounds, childView)
40 }
41 
getTouchTargetMarginnull42 fun getTouchTargetMargin(desiredSize: Float, actualSize: Int): Int {
43     return max(((desiredSize - actualSize) / 2) * 1.0f, 0f).toInt()
44 }
45