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 * 32 * @param view component that will be expanded 33 * @param minTouchTargetSizeResource required minimum touch target size 34 */ 35 @JvmStatic configureSizenull36 fun configureSize(view: View, @DimenRes minTouchTargetSizeResource: Int) { 37 val parent = view.parent as View 38 val res = view.context.resources 39 val minTouchTargetSize = res.getDimensionPixelSize(minTouchTargetSizeResource) 40 41 // Defer getHitRect so that it's called after the parent's children are laid out. 42 parent.post { 43 val hitRect = Rect() 44 view.getHitRect(hitRect) 45 val currentTouchTargetWidth = hitRect.width() 46 if (currentTouchTargetWidth < minTouchTargetSize) { 47 // Divide width difference by two to get adjustment 48 val adjustInsetBy = (minTouchTargetSize - currentTouchTargetWidth) / 2 49 50 // Inset adjustment is applied to top, bottom, left, right 51 hitRect.inset(-adjustInsetBy, -adjustInsetBy) 52 parent.touchDelegate = TouchDelegate(hitRect, view) 53 } 54 } 55 } 56 } 57