Home | History | Annotate | Download | only in dialpad
      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.dialpad;
     18 
     19 import android.content.Context;
     20 import android.graphics.Rect;
     21 import android.os.Bundle;
     22 import android.util.AttributeSet;
     23 import android.view.MotionEvent;
     24 import android.view.View;
     25 import android.view.accessibility.AccessibilityEvent;
     26 import android.view.accessibility.AccessibilityManager;
     27 import android.view.accessibility.AccessibilityNodeInfo;
     28 import android.widget.FrameLayout;
     29 
     30 /**
     31  * Custom class for dialpad buttons.
     32  * <p>
     33  * This class implements lift-to-type interaction when touch exploration is
     34  * enabled.
     35  */
     36 public class DialpadKeyButton extends FrameLayout {
     37     /** Accessibility manager instance used to check touch exploration state. */
     38     private AccessibilityManager mAccessibilityManager;
     39 
     40     /** Bounds used to filter HOVER_EXIT events. */
     41     private Rect mHoverBounds = new Rect();
     42 
     43     public interface OnPressedListener {
     44         public void onPressed(View view, boolean pressed);
     45     }
     46 
     47     private OnPressedListener mOnPressedListener;
     48 
     49     public void setOnPressedListener(OnPressedListener onPressedListener) {
     50         mOnPressedListener = onPressedListener;
     51     }
     52 
     53     public DialpadKeyButton(Context context, AttributeSet attrs) {
     54         super(context, attrs);
     55         initForAccessibility(context);
     56     }
     57 
     58     public DialpadKeyButton(Context context, AttributeSet attrs, int defStyle) {
     59         super(context, attrs, defStyle);
     60         initForAccessibility(context);
     61     }
     62 
     63     private void initForAccessibility(Context context) {
     64         mAccessibilityManager = (AccessibilityManager) context.getSystemService(
     65                 Context.ACCESSIBILITY_SERVICE);
     66     }
     67 
     68     @Override
     69     public void setPressed(boolean pressed) {
     70         super.setPressed(pressed);
     71         if (mOnPressedListener != null) {
     72             mOnPressedListener.onPressed(this, pressed);
     73         }
     74     }
     75 
     76     @Override
     77     public void onSizeChanged(int w, int h, int oldw, int oldh) {
     78         super.onSizeChanged(w, h, oldw, oldh);
     79 
     80         mHoverBounds.left = getPaddingLeft();
     81         mHoverBounds.right = w - getPaddingRight();
     82         mHoverBounds.top = getPaddingTop();
     83         mHoverBounds.bottom = h - getPaddingBottom();
     84     }
     85 
     86     @Override
     87     public boolean performAccessibilityAction(int action, Bundle arguments) {
     88         if (action == AccessibilityNodeInfo.ACTION_CLICK) {
     89             simulateClickForAccessibility();
     90             return true;
     91         }
     92 
     93         return super.performAccessibilityAction(action, arguments);
     94     }
     95 
     96     @Override
     97     public boolean onHoverEvent(MotionEvent event) {
     98         // When touch exploration is turned on, lifting a finger while inside
     99         // the button's hover target bounds should perform a click action.
    100         if (mAccessibilityManager.isEnabled()
    101                 && mAccessibilityManager.isTouchExplorationEnabled()) {
    102             switch (event.getActionMasked()) {
    103                 case MotionEvent.ACTION_HOVER_ENTER:
    104                     // Lift-to-type temporarily disables double-tap activation.
    105                     setClickable(false);
    106                     break;
    107                 case MotionEvent.ACTION_HOVER_EXIT:
    108                     if (mHoverBounds.contains((int) event.getX(), (int) event.getY())) {
    109                         simulateClickForAccessibility();
    110                     }
    111                     setClickable(true);
    112                     break;
    113             }
    114         }
    115 
    116         return super.onHoverEvent(event);
    117     }
    118 
    119     /**
    120      * When accessibility is on, simulate press and release to preserve the
    121      * semantic meaning of performClick(). Required for Braille support.
    122      */
    123     private void simulateClickForAccessibility() {
    124         // Checking the press state prevents double activation.
    125         if (isPressed()) {
    126             return;
    127         }
    128 
    129         setPressed(true);
    130 
    131         // Stay consistent with performClick() by sending the event after
    132         // setting the pressed state but before performing the action.
    133         sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
    134 
    135         setPressed(false);
    136     }
    137 }
    138