You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tasks/astrid/common-src/com/todoroo/andlib/widget/NumberPickerWidgetButton.java

99 lines
3.1 KiB
Java

/*
* ASTRID: Android's Simple Task Recording Dashboard
*
* Copyright (c) 2009 Tim Su
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.todoroo.andlib.widget;
import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.ImageButton;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.DependencyInjectionService;
/**
* This class exists purely to cancel long click events.
*/
public class NumberPickerWidgetButton extends ImageButton {
private NumberPickerWidget mNumberPicker;
@Autowired
private Integer numberPickerIncrementId;
@Autowired
private Integer numberPickerDecrementId;
public NumberPickerWidgetButton(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
DependencyInjectionService.getInstance().inject(this);
}
public NumberPickerWidgetButton(Context context, AttributeSet attrs) {
super(context, attrs);
DependencyInjectionService.getInstance().inject(this);
}
public NumberPickerWidgetButton(Context context) {
super(context);
DependencyInjectionService.getInstance().inject(this);
}
public void setNumberPicker(NumberPickerWidget picker) {
mNumberPicker = picker;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
cancelLongpressIfRequired(event);
return super.onTouchEvent(event);
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
cancelLongpressIfRequired(event);
return super.onTrackballEvent(event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER)
|| (keyCode == KeyEvent.KEYCODE_ENTER)) {
cancelLongpress();
}
return super.onKeyUp(keyCode, event);
}
private void cancelLongpressIfRequired(MotionEvent event) {
if ((event.getAction() == MotionEvent.ACTION_CANCEL)
|| (event.getAction() == MotionEvent.ACTION_UP)) {
cancelLongpress();
}
}
private void cancelLongpress() {
if (numberPickerIncrementId == getId()) {
mNumberPicker.cancelIncrement();
} else if (numberPickerDecrementId == getId()) {
mNumberPicker.cancelDecrement();
}
}
}