Rahul Ravikumar | 0533600 | 2019-10-14 15:04:32 -0700 | [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.util.AttributeSet; |
| 21 | import android.view.KeyEvent; |
| 22 | import android.view.MotionEvent; |
| 23 | import android.view.View; |
| 24 | import android.view.View.OnLongClickListener; |
| 25 | |
| 26 | /** |
| 27 | * This widget provides a simple utility for turning a continued long-press event |
| 28 | * into a series of clicks at some set frequency. There is no actual 'zoom' functionality |
| 29 | * handled by this widget directly. Instead, clients of this API should set up an |
| 30 | * {@link View#setOnClickListener(OnClickListener) onClickListener} to handle |
| 31 | * zoom functionality. That click listener is called on a frequency |
| 32 | * determined by {@link #setZoomSpeed(long)} whenever the user long-presses |
| 33 | * on the ZoomButton. |
| 34 | * |
| 35 | * @deprecated Use other means to handle this functionality. This widget is merely a |
| 36 | * simple wrapper around a long-press handler. |
| 37 | */ |
| 38 | @Deprecated |
| 39 | public class ZoomButton extends ImageButton implements OnLongClickListener { |
| 40 | |
| 41 | private final Runnable mRunnable = new Runnable() { |
| 42 | public void run() { |
| 43 | if (hasOnClickListeners() && mIsInLongpress && isEnabled()) { |
| 44 | callOnClick(); |
| 45 | postDelayed(this, mZoomSpeed); |
| 46 | } |
| 47 | } |
| 48 | }; |
| 49 | |
| 50 | private long mZoomSpeed = 1000; |
| 51 | private boolean mIsInLongpress; |
| 52 | |
| 53 | public ZoomButton(Context context) { |
| 54 | this(context, null); |
| 55 | } |
| 56 | |
| 57 | public ZoomButton(Context context, AttributeSet attrs) { |
| 58 | this(context, attrs, 0); |
| 59 | } |
| 60 | |
| 61 | public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr) { |
| 62 | this(context, attrs, defStyleAttr, 0); |
| 63 | } |
| 64 | |
| 65 | public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 66 | super(context, attrs, defStyleAttr, defStyleRes); |
| 67 | setOnLongClickListener(this); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public boolean onTouchEvent(MotionEvent event) { |
| 72 | if ((event.getAction() == MotionEvent.ACTION_CANCEL) |
| 73 | || (event.getAction() == MotionEvent.ACTION_UP)) { |
| 74 | mIsInLongpress = false; |
| 75 | } |
| 76 | return super.onTouchEvent(event); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sets the delay between calls to the widget's {@link View#setOnClickListener(OnClickListener) |
| 81 | * onClickListener}. |
| 82 | * |
| 83 | * @param speed The delay between calls to the click listener, in milliseconds |
| 84 | */ |
| 85 | public void setZoomSpeed(long speed) { |
| 86 | mZoomSpeed = speed; |
| 87 | } |
| 88 | |
| 89 | @Override |
| 90 | public boolean onLongClick(View v) { |
| 91 | mIsInLongpress = true; |
| 92 | post(mRunnable); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public boolean onKeyUp(int keyCode, KeyEvent event) { |
| 98 | mIsInLongpress = false; |
| 99 | return super.onKeyUp(keyCode, event); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public void setEnabled(boolean enabled) { |
| 104 | if (!enabled) { |
| 105 | |
| 106 | /* If we're being disabled reset the state back to unpressed |
| 107 | * as disabled views don't get events and therefore we won't |
| 108 | * get the up event to reset the state. |
| 109 | */ |
| 110 | setPressed(false); |
| 111 | } |
| 112 | super.setEnabled(enabled); |
| 113 | } |
| 114 | |
| 115 | @Override |
| 116 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 117 | clearFocus(); |
| 118 | return super.dispatchUnhandledMove(focused, direction); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public CharSequence getAccessibilityClassName() { |
| 123 | return ZoomButton.class.getName(); |
| 124 | } |
| 125 | } |