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 17package com.android.incallui; 18 19import android.view.MotionEvent; 20import android.view.View; 21 22/** 23 * OnTouchListener used to shrink the "hit target" of some onscreen buttons. 24 * 25 * We do this for a few specific buttons which are vulnerable to 26 * "false touches" because either (1) they're near the edge of the 27 * screen and might be unintentionally touched while holding the 28 * device in your hand, (2) they're in the upper corners and might 29 * be touched by the user's ear before the prox sensor has a chance to 30 * kick in, or (3) they are close to other buttons. 31 */ 32public class SmallerHitTargetTouchListener implements View.OnTouchListener { 33 private static final String TAG = "SmallerHitTargetTouchListener"; 34 35 /** 36 * Edge dimensions where a touch does not register an action (in DIP). 37 */ 38 private static final int HIT_TARGET_EDGE_IGNORE_DP_X = 30; 39 private static final int HIT_TARGET_EDGE_IGNORE_DP_Y = 10; 40 private static final int HIT_TARGET_MIN_SIZE_DP_X = HIT_TARGET_EDGE_IGNORE_DP_X * 3; 41 private static final int HIT_TARGET_MIN_SIZE_DP_Y = HIT_TARGET_EDGE_IGNORE_DP_Y * 3; 42 43 // True if the most recent DOWN event was a "hit". 44 boolean mDownEventHit; 45 46 /** 47 * Called when a touch event is dispatched to a view. This allows listeners to 48 * get a chance to respond before the target view. 49 * 50 * @return True if the listener has consumed the event, false otherwise. 51 * (In other words, we return true when the touch is *outside* 52 * the "smaller hit target", which will prevent the actual 53 * button from handling these events.) 54 */ 55 @Override 56 public boolean onTouch(View v, MotionEvent event) { 57 // if (DBG) log("SmallerHitTargetTouchListener: " + v + ", event " + event); 58 59 if (event.getAction() == MotionEvent.ACTION_DOWN) { 60 // Note that event.getX() and event.getY() are already 61 // translated into the View's coordinates. (In other words, 62 // "0,0" is a touch on the upper-left-most corner of the view.) 63 final int touchX = (int) event.getX(); 64 final int touchY = (int) event.getY(); 65 66 final int viewWidth = v.getWidth(); 67 final int viewHeight = v.getHeight(); 68 69 final float pixelDensity = v.getResources().getDisplayMetrics().density; 70 final int targetMinSizeX = (int) (HIT_TARGET_MIN_SIZE_DP_X * pixelDensity); 71 final int targetMinSizeY = (int) (HIT_TARGET_MIN_SIZE_DP_Y * pixelDensity); 72 73 int edgeIgnoreX = (int) (HIT_TARGET_EDGE_IGNORE_DP_X * pixelDensity); 74 int edgeIgnoreY = (int) (HIT_TARGET_EDGE_IGNORE_DP_Y * pixelDensity); 75 76 // If we are dealing with smaller buttons where the dead zone defined by 77 // HIT_TARGET_EDGE_IGNORE_DP_[X|Y] is too large. 78 if (viewWidth < targetMinSizeX || viewHeight < targetMinSizeY) { 79 // This really should not happen given our two use cases (as of this writing) 80 // in the call edge button and secondary calling card. However, we leave 81 // this is as a precautionary measure. 82 Log.w(TAG, "onTouch: view is too small for SmallerHitTargetTouchListener"); 83 edgeIgnoreX = 0; 84 edgeIgnoreY = 0; 85 } 86 87 final int minTouchX = edgeIgnoreX; 88 final int maxTouchX = viewWidth - edgeIgnoreX; 89 final int minTouchY = edgeIgnoreY; 90 final int maxTouchY = viewHeight - edgeIgnoreY; 91 92 if (touchX < minTouchX || touchX > maxTouchX || 93 touchY < minTouchY || touchY > maxTouchY) { 94 // Missed! 95 // if (DBG) log(" -> MISSED!"); 96 mDownEventHit = false; 97 return true; // Consume this event; don't let the button see it 98 } else { 99 // Hit! 100 // if (DBG) log(" -> HIT!"); 101 mDownEventHit = true; 102 return false; // Let this event through to the actual button 103 } 104 } else { 105 // This is a MOVE, UP or CANCEL event. 106 // 107 // We only do the "smaller hit target" check on DOWN events. 108 // For the subsequent MOVE/UP/CANCEL events, we let them 109 // through to the actual button IFF the previous DOWN event 110 // got through to the actual button (i.e. it was a "hit".) 111 return !mDownEventHit; 112 } 113 } 114} 115