blob: 47553b6f93e72b3f84ca380f5951887cf83a093f [file] [log] [blame]
Eric Erfanianccca3152017-02-22 16:32:36 -08001/*
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
17package com.android.dialer.dialpadview;
18
19import android.content.Context;
20import android.graphics.RectF;
21import android.os.Bundle;
calderwoodrada777162018-04-04 17:24:11 -070022import android.text.TextUtils;
Eric Erfanianccca3152017-02-22 16:32:36 -080023import android.util.AttributeSet;
24import android.view.MotionEvent;
25import android.view.View;
Eric Erfanianccca3152017-02-22 16:32:36 -080026import android.view.accessibility.AccessibilityEvent;
27import android.view.accessibility.AccessibilityManager;
28import android.view.accessibility.AccessibilityNodeInfo;
calderwoodrada777162018-04-04 17:24:11 -070029import android.view.accessibility.AccessibilityNodeInfo.AccessibilityAction;
Eric Erfanianccca3152017-02-22 16:32:36 -080030import 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 */
47public class DialpadKeyButton extends FrameLayout {
48
Eric Erfanianccca3152017-02-22 16:32:36 -080049 /** Accessibility manager instance used to check touch exploration state. */
linyuh183cb712017-12-27 17:02:37 -080050 private AccessibilityManager accessibilityManager;
Eric Erfanianccca3152017-02-22 16:32:36 -080051
52 /** Bounds used to filter HOVER_EXIT events. */
linyuh183cb712017-12-27 17:02:37 -080053 private RectF hoverBounds = new RectF();
Eric Erfanianccca3152017-02-22 16:32:36 -080054
Eric Erfanianccca3152017-02-22 16:32:36 -080055 /** Alternate content description for long-hover state. */
linyuh183cb712017-12-27 17:02:37 -080056 private CharSequence longHoverContentDesc;
Eric Erfanianccca3152017-02-22 16:32:36 -080057
Eric Erfanianccca3152017-02-22 16:32:36 -080058 /** Backup of clickable property. Used for accessibility. */
linyuh183cb712017-12-27 17:02:37 -080059 private boolean wasClickable;
Eric Erfanianccca3152017-02-22 16:32:36 -080060
61 /** Backup of long-clickable property. Used for accessibility. */
linyuh183cb712017-12-27 17:02:37 -080062 private boolean wasLongClickable;
Eric Erfanianccca3152017-02-22 16:32:36 -080063
linyuh183cb712017-12-27 17:02:37 -080064 private OnPressedListener onPressedListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080065
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) {
linyuh183cb712017-12-27 17:02:37 -080077 this.onPressedListener = onPressedListener;
Eric Erfanianccca3152017-02-22 16:32:36 -080078 }
79
80 private void initForAccessibility(Context context) {
linyuh183cb712017-12-27 17:02:37 -080081 accessibilityManager =
Eric Erfanianccca3152017-02-22 16:32:36 -080082 (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);
83 }
84
85 public void setLongHoverContentDescription(CharSequence contentDescription) {
linyuh183cb712017-12-27 17:02:37 -080086 longHoverContentDesc = contentDescription;
Eric Erfanianccca3152017-02-22 16:32:36 -080087 }
88
89 @Override
90 public void setPressed(boolean pressed) {
91 super.setPressed(pressed);
linyuh183cb712017-12-27 17:02:37 -080092 if (onPressedListener != null) {
93 onPressedListener.onPressed(this, pressed);
Eric Erfanianccca3152017-02-22 16:32:36 -080094 }
95 }
96
97 @Override
98 public void onSizeChanged(int w, int h, int oldw, int oldh) {
99 super.onSizeChanged(w, h, oldw, oldh);
100
linyuh183cb712017-12-27 17:02:37 -0800101 hoverBounds.left = getPaddingLeft();
102 hoverBounds.right = w - getPaddingRight();
103 hoverBounds.top = getPaddingTop();
104 hoverBounds.bottom = h - getPaddingBottom();
Eric Erfanianccca3152017-02-22 16:32:36 -0800105 }
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
calderwoodrada777162018-04-04 17:24:11 -0700118 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 Erfanianccca3152017-02-22 16:32:36 -0800130 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.
linyuh183cb712017-12-27 17:02:37 -0800133 if (accessibilityManager.isEnabled() && accessibilityManager.isTouchExplorationEnabled()) {
Eric Erfanianccca3152017-02-22 16:32:36 -0800134 switch (event.getActionMasked()) {
135 case MotionEvent.ACTION_HOVER_ENTER:
136 // Lift-to-type temporarily disables double-tap activation.
linyuh183cb712017-12-27 17:02:37 -0800137 wasClickable = isClickable();
138 wasLongClickable = isLongClickable();
Eric Erfanianccca3152017-02-22 16:32:36 -0800139 setClickable(false);
140 setLongClickable(false);
141 break;
142 case MotionEvent.ACTION_HOVER_EXIT:
linyuh183cb712017-12-27 17:02:37 -0800143 if (hoverBounds.contains(event.getX(), event.getY())) {
calderwoodraa9eae7e2017-09-15 17:00:28 -0700144 simulateClickForAccessibility();
Eric Erfanianccca3152017-02-22 16:32:36 -0800145 }
146
linyuh183cb712017-12-27 17:02:37 -0800147 setClickable(wasClickable);
148 setLongClickable(wasLongClickable);
Eric Erfanianccca3152017-02-22 16:32:36 -0800149 break;
calderwoodraa9eae7e2017-09-15 17:00:28 -0700150 default: // No-op
151 break;
Eric Erfanianccca3152017-02-22 16:32:36 -0800152 }
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 Erfanianccca3152017-02-22 16:32:36 -0800177 public interface OnPressedListener {
178
179 void onPressed(View view, boolean pressed);
180 }
181}