• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.permissioncontroller.safetycenter.ui
18 
19 import android.graphics.Rect
20 import android.os.Build
21 import android.view.TouchDelegate
22 import android.view.View
23 import androidx.annotation.DimenRes
24 import androidx.annotation.RequiresApi
25 
26 /** Class to configure touch targets for Safety Center. */
27 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
28 object SafetyCenterTouchTarget {
29     /**
30      * Resizes the touch target of views by delegating to the parent component.
31      * @param view component that will be expanded
32      * @param minTouchTargetSizeResource required minimum touch target size
33      */
34     @JvmStatic
configureSizenull35     fun configureSize(view: View, @DimenRes minTouchTargetSizeResource: Int) {
36         val parent = view.parent as View
37         val res = view.context.resources
38         val minTouchTargetSize = res.getDimensionPixelSize(minTouchTargetSizeResource)
39 
40         // Defer getHitRect so that it's called after the parent's children are laid out.
41         parent.post {
42             val hitRect = Rect()
43             view.getHitRect(hitRect)
44             val currentTouchTargetWidth = hitRect.width()
45             if (currentTouchTargetWidth < minTouchTargetSize) {
46                 // Divide width difference by two to get adjustment
47                 val adjustInsetBy = (minTouchTargetSize - currentTouchTargetWidth) / 2
48 
49                 // Inset adjustment is applied to top, bottom, left, right
50                 hitRect.inset(-adjustInsetBy, -adjustInsetBy)
51                 parent.touchDelegate = TouchDelegate(hitRect, view)
52             }
53         }
54     }
55 }
56