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