The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame^] | 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.GestureDetector; |
| 23 | import android.view.HapticFeedbackConstants; |
| 24 | import android.view.KeyEvent; |
| 25 | import android.view.MotionEvent; |
| 26 | import android.view.View; |
| 27 | import android.view.GestureDetector.SimpleOnGestureListener; |
| 28 | import android.view.View.OnLongClickListener; |
| 29 | |
| 30 | |
| 31 | public class ZoomButton extends ImageButton implements OnLongClickListener { |
| 32 | |
| 33 | private final Handler mHandler; |
| 34 | private final Runnable mRunnable = new Runnable() { |
| 35 | public void run() { |
| 36 | if ((mOnClickListener != null) && mIsInLongpress && isEnabled()) { |
| 37 | mOnClickListener.onClick(ZoomButton.this); |
| 38 | mHandler.postDelayed(this, mZoomSpeed); |
| 39 | } |
| 40 | } |
| 41 | }; |
| 42 | private final GestureDetector mGestureDetector; |
| 43 | |
| 44 | private long mZoomSpeed = 1000; |
| 45 | private boolean mIsInLongpress; |
| 46 | |
| 47 | public ZoomButton(Context context) { |
| 48 | this(context, null); |
| 49 | } |
| 50 | |
| 51 | public ZoomButton(Context context, AttributeSet attrs) { |
| 52 | this(context, attrs, 0); |
| 53 | } |
| 54 | |
| 55 | public ZoomButton(Context context, AttributeSet attrs, int defStyle) { |
| 56 | super(context, attrs, defStyle); |
| 57 | mHandler = new Handler(); |
| 58 | mGestureDetector = new GestureDetector(context, new SimpleOnGestureListener() { |
| 59 | @Override |
| 60 | public void onLongPress(MotionEvent e) { |
| 61 | performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); |
| 62 | onLongClick(ZoomButton.this); |
| 63 | } |
| 64 | }); |
| 65 | setOnLongClickListener(this); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean onTouchEvent(MotionEvent event) { |
| 70 | mGestureDetector.onTouchEvent(event); |
| 71 | if ((event.getAction() == MotionEvent.ACTION_CANCEL) |
| 72 | || (event.getAction() == MotionEvent.ACTION_UP)) { |
| 73 | mIsInLongpress = false; |
| 74 | } |
| 75 | return super.onTouchEvent(event); |
| 76 | } |
| 77 | |
| 78 | public void setZoomSpeed(long speed) { |
| 79 | mZoomSpeed = speed; |
| 80 | } |
| 81 | |
| 82 | public boolean onLongClick(View v) { |
| 83 | mIsInLongpress = true; |
| 84 | mHandler.post(mRunnable); |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 90 | mIsInLongpress = false; |
| 91 | return super.onKeyUp(keyCode, event); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void setEnabled(boolean enabled) { |
| 96 | if (!enabled) { |
| 97 | |
| 98 | /* If we're being disabled reset the state back to unpressed |
| 99 | * as disabled views don't get events and therefore we won't |
| 100 | * get the up event to reset the state. |
| 101 | */ |
| 102 | setPressed(false); |
| 103 | } |
| 104 | super.setEnabled(enabled); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 109 | clearFocus(); |
| 110 | return super.dispatchUnhandledMove(focused, direction); |
| 111 | } |
| 112 | } |