Home | History | Annotate | Download | only in widget
      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 import android.view.accessibility.AccessibilityEvent;
     27 import android.view.accessibility.AccessibilityNodeInfo;
     28 
     29 public class ZoomButton extends ImageButton implements OnLongClickListener {
     30 
     31     private final Handler mHandler;
     32     private final Runnable mRunnable = new Runnable() {
     33         public void run() {
     34             if (hasOnClickListeners() && mIsInLongpress && isEnabled()) {
     35                 callOnClick();
     36                 mHandler.postDelayed(this, mZoomSpeed);
     37             }
     38         }
     39     };
     40 
     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 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);
     58         mHandler = new Handler();
     59         setOnLongClickListener(this);
     60     }
     61 
     62     @Override
     63     public boolean onTouchEvent(MotionEvent event) {
     64         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     }
    105 
    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     }
    117 }
    118