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