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 com.googlecode.android_scripting.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.widget.ImageButton;
     24 
     25 import com.googlecode.android_scripting.R;
     26 
     27 /**
     28  * This class exists purely to cancel long click events.
     29  */
     30 public class NumberPickerButton extends ImageButton {
     31 
     32   private NumberPicker mNumberPicker;
     33 
     34   public NumberPickerButton(Context context, AttributeSet attrs, int defStyle) {
     35     super(context, attrs, defStyle);
     36   }
     37 
     38   public NumberPickerButton(Context context, AttributeSet attrs) {
     39     super(context, attrs);
     40   }
     41 
     42   public NumberPickerButton(Context context) {
     43     super(context);
     44   }
     45 
     46   public void setNumberPicker(NumberPicker picker) {
     47     mNumberPicker = picker;
     48   }
     49 
     50   @Override
     51   public boolean onTouchEvent(MotionEvent event) {
     52     cancelLongpressIfRequired(event);
     53     return super.onTouchEvent(event);
     54   }
     55 
     56   @Override
     57   public boolean onTrackballEvent(MotionEvent event) {
     58     cancelLongpressIfRequired(event);
     59     return super.onTrackballEvent(event);
     60   }
     61 
     62   @Override
     63   public boolean onKeyUp(int keyCode, KeyEvent event) {
     64     if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) || (keyCode == KeyEvent.KEYCODE_ENTER)) {
     65       cancelLongpress();
     66     }
     67     return super.onKeyUp(keyCode, event);
     68   }
     69 
     70   private void cancelLongpressIfRequired(MotionEvent event) {
     71     if ((event.getAction() == MotionEvent.ACTION_CANCEL)
     72         || (event.getAction() == MotionEvent.ACTION_UP)) {
     73       cancelLongpress();
     74     }
     75   }
     76 
     77   private void cancelLongpress() {
     78     if (R.id.increment == getId()) {
     79       mNumberPicker.cancelIncrement();
     80     } else if (R.id.decrement == getId()) {
     81       mNumberPicker.cancelDecrement();
     82     }
     83   }
     84 }
     85