blob: 715e86803e11fe63548d389c29946a144a7ce067 [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
Alan Viverette617feb92013-09-09 18:09:13 -070052 public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr) {
53 this(context, attrs, defStyleAttr, 0);
54 }
55
56 public ZoomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
57 super(context, attrs, defStyleAttr, defStyleRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 mHandler = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 setOnLongClickListener(this);
60 }
61
62 @Override
63 public boolean onTouchEvent(MotionEvent event) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 if ((event.getAction() == MotionEvent.ACTION_CANCEL)
65 || (event.getAction() == MotionEvent.ACTION_UP)) {
66 mIsInLongpress = false;
67 }
68 return super.onTouchEvent(event);
69 }
70
71 public void setZoomSpeed(long speed) {
72 mZoomSpeed = speed;
73 }
74
75 public boolean onLongClick(View v) {
76 mIsInLongpress = true;
77 mHandler.post(mRunnable);
78 return true;
79 }
80
81 @Override
82 public boolean onKeyUp(int keyCode, KeyEvent event) {
83 mIsInLongpress = false;
84 return super.onKeyUp(keyCode, event);
85 }
86
87 @Override
88 public void setEnabled(boolean enabled) {
89 if (!enabled) {
90
91 /* If we're being disabled reset the state back to unpressed
92 * as disabled views don't get events and therefore we won't
93 * get the up event to reset the state.
94 */
95 setPressed(false);
96 }
97 super.setEnabled(enabled);
98 }
99
100 @Override
101 public boolean dispatchUnhandledMove(View focused, int direction) {
102 clearFocus();
103 return super.dispatchUnhandledMove(focused, direction);
104 }
Svetoslav Ganov8a78fd42012-01-17 14:36:46 -0800105
106 @Override
107 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
108 super.onInitializeAccessibilityEvent(event);
109 event.setClassName(ZoomButton.class.getName());
110 }
111
112 @Override
113 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
114 super.onInitializeAccessibilityNodeInfo(info);
115 info.setClassName(ZoomButton.class.getName());
116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117}