Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.dialer.dialpadview; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.graphics.RectF; |
| 21 | import android.os.Bundle; |
calderwoodra | da77716 | 2018-04-04 17:24:11 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
| 24 | import android.view.MotionEvent; |
| 25 | import android.view.View; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 26 | import android.view.accessibility.AccessibilityEvent; |
| 27 | import android.view.accessibility.AccessibilityManager; |
| 28 | import android.view.accessibility.AccessibilityNodeInfo; |
calderwoodra | da77716 | 2018-04-04 17:24:11 -0700 | [diff] [blame] | 29 | import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 30 | import android.widget.FrameLayout; |
| 31 | |
| 32 | /** |
| 33 | * Custom class for dialpad buttons. |
| 34 | * |
| 35 | * <p>When touch exploration mode is enabled for accessibility, this class implements the |
| 36 | * lift-to-type interaction model: |
| 37 | * |
| 38 | * <ul> |
| 39 | * <li>Hovering over the button will cause it to gain accessibility focus |
| 40 | * <li>Removing the hover pointer while inside the bounds of the button will perform a click action |
| 41 | * <li>If long-click is supported, hovering over the button for a longer period of time will switch |
| 42 | * to the long-click action |
| 43 | * <li>Moving the hover pointer outside of the bounds of the button will restore to the normal click |
| 44 | * action |
| 45 | * <ul> |
| 46 | */ |
| 47 | public class DialpadKeyButton extends FrameLayout { |
| 48 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 49 | /** Accessibility manager instance used to check touch exploration state. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 50 | private AccessibilityManager accessibilityManager; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 51 | |
| 52 | /** Bounds used to filter HOVER_EXIT events. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 53 | private RectF hoverBounds = new RectF(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 54 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 55 | /** Alternate content description for long-hover state. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 56 | private CharSequence longHoverContentDesc; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 57 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 58 | /** Backup of clickable property. Used for accessibility. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 59 | private boolean wasClickable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 60 | |
| 61 | /** Backup of long-clickable property. Used for accessibility. */ |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 62 | private boolean wasLongClickable; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 63 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 64 | private OnPressedListener onPressedListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 65 | |
| 66 | public DialpadKeyButton(Context context, AttributeSet attrs) { |
| 67 | super(context, attrs); |
| 68 | initForAccessibility(context); |
| 69 | } |
| 70 | |
| 71 | public DialpadKeyButton(Context context, AttributeSet attrs, int defStyle) { |
| 72 | super(context, attrs, defStyle); |
| 73 | initForAccessibility(context); |
| 74 | } |
| 75 | |
| 76 | public void setOnPressedListener(OnPressedListener onPressedListener) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 77 | this.onPressedListener = onPressedListener; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private void initForAccessibility(Context context) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 81 | accessibilityManager = |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 82 | (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE); |
| 83 | } |
| 84 | |
| 85 | public void setLongHoverContentDescription(CharSequence contentDescription) { |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 86 | longHoverContentDesc = contentDescription; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public void setPressed(boolean pressed) { |
| 91 | super.setPressed(pressed); |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 92 | if (onPressedListener != null) { |
| 93 | onPressedListener.onPressed(this, pressed); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 99 | super.onSizeChanged(w, h, oldw, oldh); |
| 100 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 101 | hoverBounds.left = getPaddingLeft(); |
| 102 | hoverBounds.right = w - getPaddingRight(); |
| 103 | hoverBounds.top = getPaddingTop(); |
| 104 | hoverBounds.bottom = h - getPaddingBottom(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean performAccessibilityAction(int action, Bundle arguments) { |
| 109 | if (action == AccessibilityNodeInfo.ACTION_CLICK) { |
| 110 | simulateClickForAccessibility(); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | return super.performAccessibilityAction(action, arguments); |
| 115 | } |
| 116 | |
| 117 | @Override |
calderwoodra | da77716 | 2018-04-04 17:24:11 -0700 | [diff] [blame] | 118 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { |
| 119 | super.onInitializeAccessibilityNodeInfo(info); |
| 120 | // If the button has a long hover description, ask talkback to announce the action follow by |
| 121 | // the description (for example "double tap and hold to call voicemail"). |
| 122 | if (!TextUtils.isEmpty(longHoverContentDesc)) { |
| 123 | AccessibilityAction longClickAction = |
| 124 | new AccessibilityAction(AccessibilityNodeInfo.ACTION_LONG_CLICK, longHoverContentDesc); |
| 125 | info.addAction(longClickAction); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | @Override |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 130 | public boolean onHoverEvent(MotionEvent event) { |
| 131 | // When touch exploration is turned on, lifting a finger while inside |
| 132 | // the button's hover target bounds should perform a click action. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 133 | if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) { |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 134 | switch (event.getActionMasked()) { |
| 135 | case MotionEvent.ACTION_HOVER_ENTER: |
| 136 | // Lift-to-type temporarily disables double-tap activation. |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 137 | wasClickable = isClickable(); |
| 138 | wasLongClickable = isLongClickable(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 139 | setClickable(false); |
| 140 | setLongClickable(false); |
| 141 | break; |
| 142 | case MotionEvent.ACTION_HOVER_EXIT: |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 143 | if (hoverBounds.contains(event.getX(), event.getY())) { |
calderwoodra | a9eae7e | 2017-09-15 17:00:28 -0700 | [diff] [blame] | 144 | simulateClickForAccessibility(); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 145 | } |
| 146 | |
linyuh | 183cb71 | 2017-12-27 17:02:37 -0800 | [diff] [blame] | 147 | setClickable(wasClickable); |
| 148 | setLongClickable(wasLongClickable); |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 149 | break; |
calderwoodra | a9eae7e | 2017-09-15 17:00:28 -0700 | [diff] [blame] | 150 | default: // No-op |
| 151 | break; |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | return super.onHoverEvent(event); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * When accessibility is on, simulate press and release to preserve the semantic meaning of |
| 160 | * performClick(). Required for Braille support. |
| 161 | */ |
| 162 | private void simulateClickForAccessibility() { |
| 163 | // Checking the press state prevents double activation. |
| 164 | if (isPressed()) { |
| 165 | return; |
| 166 | } |
| 167 | |
| 168 | setPressed(true); |
| 169 | |
| 170 | // Stay consistent with performClick() by sending the event after |
| 171 | // setting the pressed state but before performing the action. |
| 172 | sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED); |
| 173 | |
| 174 | setPressed(false); |
| 175 | } |
| 176 | |
Eric Erfanian | ccca315 | 2017-02-22 16:32:36 -0800 | [diff] [blame] | 177 | public interface OnPressedListener { |
| 178 | |
| 179 | void onPressed(View view, boolean pressed); |
| 180 | } |
| 181 | } |