I have followed Anthony's advices, implementation using TimerTask and ButtonField.
Be carfull with CONSUME_CLICK style, somehow it excludes navigationClick event.
Also, I have expirienced troubles with Bold 9000/8900 simulators, navigationClick is fired after trackwheel is unclicked. But sample works fine with Storm.
Interface:
public interface LongClickListener {
public void longClickEvent(Field field, int eventNumber);
}
Control itself:
public class LongClickButtonField extends ButtonField {
boolean mContinuousMode = false;
Timer mLongClickTimer = null;
long mLongClickDelay = 1000;
long mContinuousPeriod = 500;
LongClickListener mLongClickListener = null;
public LongClickButtonField(String label) {
super(label);
}
public LongClickButtonField(String label, boolean continuousMode) {
this(label);
mContinuousMode = continuousMode;
}
public void setLongClickListener(LongClickListener longClickListener) {
mLongClickListener = longClickListener;
}
protected boolean navigationClick(int status, int time) {
mLongClickTimer = new Timer();
final Field eventArg = this;
if(!mContinuousMode)
{
mLongClickTimer.schedule(new TimerTask() {public void run() {
mLongClickListener.longClickEvent(eventArg, 0);
}}, mLongClickDelay);
}
else
{
mLongClickTimer.schedule(new TimerTask(){
int eventNum= 0;
public void run() {
mLongClickListener.longClickEvent(eventArg, eventNum);
eventNum++;
}}, mLongClickDelay, mContinuousPeriod);
}
return true;
}
protected boolean navigationUnclick(int status, int time) {
mLongClickTimer.cancel();
return true;
}
}
Use:
public class Scr extends MainScreen implements LongClickListener {
LabelField mStatusLabel = new LabelField("status") {
protected void paint(Graphics graphics) {
graphics.setColor(Color.BLACK);
super.paint(graphics);
};
};
public Scr() {
LongClickButtonField button = new LongClickButtonField(
"click & hold 5 s");
LongClickButtonField buttonContinuous = new LongClickButtonField(
"click & hold > 5 s", true);
button.setLongClickListener(this);
buttonContinuous.setLongClickListener(this);
add(mStatusLabel);
add(button);
add(buttonContinuous);
}
public void longClickEvent(final Field field, final int eventNumber) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
mStatusLabel.setText(
((ButtonField) field).getLabel()
+ " event #"
+ String.valueOf(eventNumber));
}
});
}
}