android-rollerderby-penalty.../src/org/woozle/penaltytimer/TimerButton.java

163 lines
3.6 KiB
Java
Raw Normal View History

2012-04-05 19:37:15 -06:00
package org.woozle.penaltytimer;
import android.content.Context;
import android.widget.*;
import android.view.View;
import android.view.View.*;
import android.view.Gravity;
2012-05-26 16:22:14 -06:00
import android.graphics.ColorFilter;
import android.graphics.PorterDuffColorFilter;
import android.graphics.PorterDuff;
2012-04-05 19:37:15 -06:00
import android.os.SystemClock;
import android.os.Vibrator;
2012-04-05 19:37:15 -06:00
import java.util.*;
public class TimerButton
implements OnClickListener, OnLongClickListener
{
Button b;
Timer updateTimer;
public long startTime = 0;
public long duration = 0;
2012-04-05 19:37:15 -06:00
public boolean running = false;
boolean shook = false;
Vibrator vibr;
2012-04-05 19:37:15 -06:00
long now;
2012-05-26 16:22:14 -06:00
static ColorFilter releaseColor = null;
static ColorFilter standColor = new PorterDuffColorFilter(0xffffff88, PorterDuff.Mode.MULTIPLY);
static ColorFilter normalColor = new PorterDuffColorFilter(0xffff8888, PorterDuff.Mode.MULTIPLY);
2012-04-05 19:37:15 -06:00
public TimerButton(Context context, long now) {
2012-04-05 19:37:15 -06:00
b = new Button(context);
vibr = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
2012-04-05 19:37:15 -06:00
b.setText("--:--");
b.setTextSize(24);
b.setGravity(Gravity.CENTER);
updateTimer = new Timer();
b.setOnClickListener(this);
b.setOnLongClickListener(this);
this.now = now;
2012-04-05 19:37:15 -06:00
}
public long remaining() {
if (running) {
return duration - (now - startTime);
} else {
return duration;
}
}
public String bstr(long remain, boolean tenths) {
long min = Math.abs(remain / 60000);
long sec = Math.abs(remain / 1000) % 60;
String ret;
// Has the timer run out?
if (remain <= 0) {
return "--:--";
}
ret = min + ":";
if (sec < 10) {
ret += "0";
}
ret += sec;
if (tenths) {
ret += ".";
ret += Math.abs(remain / 100) % 10;
}
return ret;
}
public String str(long remain, boolean tenths) {
return bstr(remain, tenths) + " B";
}
public void set(long t) {
startTime = now;
duration = t;
}
public void start() {
if (! running) {
startTime = now;
}
running = true;
}
public void stop() {
if (running) {
duration = remaining();
}
running = false;
}
public TextView getButton() {
return b;
}
public void expireHook() {
}
public void update() {
long r = remaining();
2012-05-26 16:22:14 -06:00
ColorFilter cf;
2012-04-05 19:37:15 -06:00
if ((duration > 0) && (r <= 0)) {
duration = 0;
running = false;
shook = false;
2012-04-05 19:37:15 -06:00
expireHook();
}
if (r <= 0) {
2012-05-26 16:22:14 -06:00
cf = releaseColor;
2012-04-05 19:37:15 -06:00
} else if (r < 10000) {
if (! shook) {
vibr.vibrate(200);
shook = true;
}
2012-05-26 16:22:14 -06:00
cf = standColor;
2012-04-05 19:37:15 -06:00
} else {
2012-05-26 16:22:14 -06:00
cf = normalColor;
2012-04-05 19:37:15 -06:00
}
2012-05-26 16:22:14 -06:00
b.getBackground().setColorFilter(cf);
2012-04-05 19:37:15 -06:00
b.setText(str(r, true));
}
public void pulse(long now) {
this.now = now;
update();
}
public void onClick(View v) {
set((remaining() + 60000) % (60 * 8 * 1000));
start();
}
public boolean onLongClick(View v) {
set(0);
stop();
expireHook();
return true;
}
public void pause() {
stop();
b.setEnabled(false);
2012-04-05 19:37:15 -06:00
}
public void resume() {
b.setEnabled(true);
2012-04-05 19:37:15 -06:00
if (remaining() > 0) {
start();
}
}
}