1 /* 2 * Copyright (C) 2008 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 android.widget; 18 19 import android.content.Context; 20 import android.os.Handler; 21 import android.util.AttributeSet; 22 import android.view.KeyEvent; 23 import android.view.MotionEvent; 24 import android.view.View; 25 import android.view.View.OnLongClickListener; 26 import android.view.accessibility.AccessibilityEvent; 27 import android.view.accessibility.AccessibilityNodeInfo; 28 29 public class ZoomButton extends ImageButton implements OnLongClickListener { 30 31 private final Handler mHandler; 32 private final Runnable mRunnable = new Runnable() { 33 public void run() { 34 if (hasOnClickListeners() && mIsInLongpress && isEnabled()) { 35 callOnClick(); 36 mHandler.postDelayed(this, mZoomSpeed); 37 } 38 } 39 }; 40 41 private long mZoomSpeed = 1000; 42 private boolean mIsInLongpress; 43 44 public ZoomButton(Context context) { 45 this(context, null); 46 } 47 48 public ZoomButton(Context context, AttributeSet attrs) { 49 this(context, attrs, 0); 50 } 51 52 public ZoomButton(Context context, AttributeSet attrs, int defStyle) { 53 super(context, attrs, defStyle); 54 mHandler = new Handler(); 55 setOnLongClickListener(this); 56 } 57 58 @Override 59 public boolean onTouchEvent(MotionEvent event) { 60 if ((event.getAction() == MotionEvent.ACTION_CANCEL) 61 || (event.getAction() == MotionEvent.ACTION_UP)) { 62 mIsInLongpress = false; 63 } 64 return super.onTouchEvent(event); 65 } 66 67 public void setZoomSpeed(long speed) { 68 mZoomSpeed = speed; 69 } 70 71 public boolean onLongClick(View v) { 72 mIsInLongpress = true; 73 mHandler.post(mRunnable); 74 return true; 75 } 76 77 @Override 78 public boolean onKeyUp(int keyCode, KeyEvent event) { 79 mIsInLongpress = false; 80 return super.onKeyUp(keyCode, event); 81 } 82 83 @Override 84 public void setEnabled(boolean enabled) { 85 if (!enabled) { 86 87 /* If we're being disabled reset the state back to unpressed 88 * as disabled views don't get events and therefore we won't 89 * get the up event to reset the state. 90 */ 91 setPressed(false); 92 } 93 super.setEnabled(enabled); 94 } 95 96 @Override 97 public boolean dispatchUnhandledMove(View focused, int direction) { 98 clearFocus(); 99 return super.dispatchUnhandledMove(focused, direction); 100 } 101 102 @Override 103 public void onInitializeAccessibilityEvent(AccessibilityEvent event) { 104 super.onInitializeAccessibilityEvent(event); 105 event.setClassName(ZoomButton.class.getName()); 106 } 107 108 @Override 109 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) { 110 super.onInitializeAccessibilityNodeInfo(info); 111 info.setClassName(ZoomButton.class.getName()); 112 } 113 } 114