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