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 
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() {
32             if (hasOnClickListeners() && mIsInLongpress && isEnabled()) {
33                 callOnClick();
34                 mHandler.postDelayed(this, mZoomSpeed);
35             }
36         }
37     };
38 
39     private long mZoomSpeed = 1000;
40     private boolean mIsInLongpress;
41 
ZoomButton(Context context)42     public ZoomButton(Context context) {
43         this(context, null);
44     }
45 
ZoomButton(Context context, AttributeSet attrs)46     public ZoomButton(Context context, AttributeSet attrs) {
47         this(context, attrs, 0);
48     }
49 
ZoomButton(Context context, AttributeSet attrs, int defStyleAttr)50     public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr) {
51         this(context, attrs, defStyleAttr, 0);
52     }
53 
ZoomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
55         super(context, attrs, defStyleAttr, defStyleRes);
56         mHandler = new Handler();
57         setOnLongClickListener(this);
58     }
59 
60     @Override
onTouchEvent(MotionEvent event)61     public boolean onTouchEvent(MotionEvent event) {
62         if ((event.getAction() == MotionEvent.ACTION_CANCEL)
63                 || (event.getAction() == MotionEvent.ACTION_UP)) {
64             mIsInLongpress = false;
65         }
66         return super.onTouchEvent(event);
67     }
68 
setZoomSpeed(long speed)69     public void setZoomSpeed(long speed) {
70         mZoomSpeed = speed;
71     }
72 
onLongClick(View v)73     public boolean onLongClick(View v) {
74         mIsInLongpress = true;
75         mHandler.post(mRunnable);
76         return true;
77     }
78 
79     @Override
onKeyUp(int keyCode, KeyEvent event)80     public boolean onKeyUp(int keyCode, KeyEvent event) {
81         mIsInLongpress = false;
82         return super.onKeyUp(keyCode, event);
83     }
84 
85     @Override
setEnabled(boolean enabled)86     public void setEnabled(boolean enabled) {
87         if (!enabled) {
88 
89             /* If we're being disabled reset the state back to unpressed
90              * as disabled views don't get events and therefore we won't
91              * get the up event to reset the state.
92              */
93             setPressed(false);
94         }
95         super.setEnabled(enabled);
96     }
97 
98     @Override
dispatchUnhandledMove(View focused, int direction)99     public boolean dispatchUnhandledMove(View focused, int direction) {
100         clearFocus();
101         return super.dispatchUnhandledMove(focused, direction);
102     }
103 
104     @Override
getAccessibilityClassName()105     public CharSequence getAccessibilityClassName() {
106         return ZoomButton.class.getName();
107     }
108 }
109