1 /* 2 * Copyright (C) 2021 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.settingslib.widget; 18 19 import android.content.Context; 20 import android.graphics.Rect; 21 import android.util.AttributeSet; 22 import android.view.TouchDelegate; 23 import android.view.View; 24 import android.widget.LinearLayout; 25 26 import androidx.annotation.Nullable; 27 28 /** 29 * The view providing {@link BannerMessagePreference}. 30 * 31 * <p>Callers should not instantiate this view directly but rather through adding a 32 * {@link BannerMessagePreference} to a {@code PreferenceScreen}. 33 */ 34 public class BannerMessageView extends LinearLayout { 35 private Rect mTouchTargetForDismissButton; 36 BannerMessageView(Context context)37 public BannerMessageView(Context context) { 38 super(context); 39 } 40 BannerMessageView(Context context, @Nullable AttributeSet attrs)41 public BannerMessageView(Context context, 42 @Nullable AttributeSet attrs) { 43 super(context, attrs); 44 } 45 BannerMessageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)46 public BannerMessageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 47 super(context, attrs, defStyleAttr); 48 } 49 BannerMessageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)50 public BannerMessageView(Context context, AttributeSet attrs, int defStyleAttr, 51 int defStyleRes) { 52 super(context, attrs, defStyleAttr, defStyleRes); 53 } 54 55 @Override onLayout(boolean changed, int l, int t, int r, int b)56 protected void onLayout(boolean changed, int l, int t, int r, int b) { 57 super.onLayout(changed, l, t, r, b); 58 setupIncreaseTouchTargetForDismissButton(); 59 } 60 setupIncreaseTouchTargetForDismissButton()61 private void setupIncreaseTouchTargetForDismissButton() { 62 if (mTouchTargetForDismissButton != null) { 63 // Already set up 64 return; 65 } 66 67 // The dismiss button is in the 'top row' RelativeLayout for positioning, but this element 68 // does not have enough space to provide large touch targets. We therefore set the top 69 // target on this view. 70 View topRow = findViewById(R.id.top_row); 71 View dismissButton = findViewById(R.id.banner_dismiss_btn); 72 if (topRow == null || dismissButton == null || dismissButton.getVisibility() != VISIBLE) { 73 return; 74 } 75 76 int minimum = 77 getResources() 78 .getDimensionPixelSize(R.dimen.settingslib_preferred_minimum_touch_target); 79 int width = dismissButton.getWidth(); 80 int height = dismissButton.getHeight(); 81 int widthIncrease = width < minimum ? minimum - width : 0; 82 int heightIncrease = height < minimum ? minimum - height : 0; 83 84 // Compute the hit rect of dismissButton within the local co-orindate reference of this view 85 // (rather than it's direct parent topRow). 86 Rect hitRectWithinTopRow = new Rect(); 87 dismissButton.getHitRect(hitRectWithinTopRow); 88 Rect hitRectOfTopRowWithinThis = new Rect(); 89 topRow.getHitRect(hitRectOfTopRowWithinThis); 90 mTouchTargetForDismissButton = new Rect(); 91 mTouchTargetForDismissButton.left = 92 hitRectOfTopRowWithinThis.left + hitRectWithinTopRow.left; 93 mTouchTargetForDismissButton.right = 94 hitRectOfTopRowWithinThis.left + hitRectWithinTopRow.right; 95 mTouchTargetForDismissButton.top = 96 hitRectOfTopRowWithinThis.top + hitRectWithinTopRow.top; 97 mTouchTargetForDismissButton.bottom = 98 hitRectOfTopRowWithinThis.top + hitRectWithinTopRow.bottom; 99 100 // Adjust the touch target rect to apply the necessary increase in width and height. 101 mTouchTargetForDismissButton.left -= 102 widthIncrease % 2 == 1 ? (widthIncrease / 2) + 1 : widthIncrease / 2; 103 mTouchTargetForDismissButton.top -= 104 heightIncrease % 2 == 1 ? (heightIncrease / 2) + 1 : heightIncrease / 2; 105 mTouchTargetForDismissButton.right += widthIncrease / 2; 106 mTouchTargetForDismissButton.bottom += heightIncrease / 2; 107 108 setTouchDelegate(new TouchDelegate(mTouchTargetForDismissButton, dismissButton)); 109 } 110 111 } 112