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