blob: af17c9488445d440ff4faae682fe68b9a621212c [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.widget;
18
19import android.content.Context;
20import android.os.Handler;
21import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.view.KeyEvent;
23import android.view.MotionEvent;
24import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.view.View.OnLongClickListener;
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -080026import android.view.accessibility.AccessibilityEvent;
27import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029public class ZoomButton extends ImageButton implements OnLongClickListener {
30
31 private final Handler mHandler;
32 private final Runnable mRunnable = new Runnable() {
33 public void run() {
Dianne Hackborn0500b3c2011-11-01 15:28:43 -070034 if (hasOnClickListeners() && mIsInLongpress && isEnabled()) {
35 callOnClick();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 mHandler.postDelayed(this, mZoomSpeed);
37 }
38 }
39 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
41 private long mZoomSpeed = 1000;
42 private boolean mIsInLongpress;
43
44 public ZoomButton(Context context) {
45 this(context, null);
46 }
47
48 public ZoomButton(Context context, AttributeSet attrs) {
49 this(context, attrs, 0);
50 }
51
52 public ZoomButton(Context context, AttributeSet attrs, int defStyle) {
53 super(context, attrs, defStyle);
54 mHandler = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055 setOnLongClickListener(this);
56 }
57
58 @Override
59 public boolean onTouchEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 if ((event.getAction() == MotionEvent.ACTION_CANCEL)
61 || (event.getAction() == MotionEvent.ACTION_UP)) {
62 mIsInLongpress = false;
63 }
64 return super.onTouchEvent(event);
65 }
66
67 public void setZoomSpeed(long speed) {
68 mZoomSpeed = speed;
69 }
70
71 public boolean onLongClick(View v) {
72 mIsInLongpress = true;
73 mHandler.post(mRunnable);
74 return true;
75 }
76
77 @Override
78 public boolean onKeyUp(int keyCode, KeyEvent event) {
79 mIsInLongpress = false;
80 return super.onKeyUp(keyCode, event);
81 }
82
83 @Override
84 public void setEnabled(boolean enabled) {
85 if (!enabled) {
86
87 /* If we're being disabled reset the state back to unpressed
88 * as disabled views don't get events and therefore we won't
89 * get the up event to reset the state.
90 */
91 setPressed(false);
92 }
93 super.setEnabled(enabled);
94 }
95
96 @Override
97 public boolean dispatchUnhandledMove(View focused, int direction) {
98 clearFocus();
99 return super.dispatchUnhandledMove(focused, direction);
100 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800101
102 @Override
103 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
104 super.onInitializeAccessibilityEvent(event);
105 event.setClassName(ZoomButton.class.getName());
106 }
107
108 @Override
109 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
110 super.onInitializeAccessibilityNodeInfo(info);
111 info.setClassName(ZoomButton.class.getName());
112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113}