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

130 lines
3.1 KiB
Java
Raw Normal View History

2012-04-03 13:07:33 -06:00
package org.woozle.penaltytimer;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;
2012-04-05 19:37:15 -06:00
import android.widget.*;
import android.os.*;
import java.lang.Math;
import java.util.*;
2012-04-03 13:07:33 -06:00
public class PenaltyTimer extends Activity
{
2012-04-05 19:37:15 -06:00
private Handler mHandler = new Handler();
private JammerButton[] jbs = new JammerButton[2];
private TimerButton[] tbs = new TimerButton[8];
private Runnable pulse = new Runnable() {
public void run() {
long now = SystemClock.uptimeMillis();
pulse(now);
mHandler.postAtTime(this, now + 100);
}
};
2012-04-03 13:07:33 -06:00
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
2012-04-05 19:37:15 -06:00
Persistence p = (Persistence)getLastNonConfigurationInstance();
TableLayout tl;
View top;
2012-04-03 13:07:33 -06:00
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
2012-04-05 19:37:15 -06:00
tl = (TableLayout)findViewById(R.id.mainTable);
top = findViewById(R.id.main);
for (int i = 0; i < 4; i += 1) {
TableRow tr = new TableRow(this);
tl.addView(tr, i);
for (int j = 0; j < 2; j += 1) {
TimerButton b;
TextView v;
if (i == 0) {
JammerButton jb = new JammerButton(this);
if (p != null) {
jb.penalized = p.penalized[j];
}
jbs[j] = jb;
b = jb;
} else {
b = new TimerButton(this);
}
if (p != null) {
b.set(p.remain[i*2 + j]);
if (p.paused) {
b.pause();
} else {
b.resume();
}
}
v = b.getButton();
tr.addView(v);
tbs[i*2 + j] = b;
}
}
jbs[0].setOther(jbs[1]);
jbs[1].setOther(jbs[0]);
mHandler.postAtTime(pulse, 100);
2012-04-03 13:07:33 -06:00
}
2012-04-05 19:37:15 -06:00
class Persistence {
public long[] remain = new long[8];
public boolean paused = false;
public boolean[] penalized = new boolean[2];
}
public Object onRetainNonConfigurationInstance() {
Persistence p = new Persistence();
int i;
for (i = 0; i < 8; i += 1) {
TimerButton b = tbs[i];
if (b.paused) {
p.paused = true;
}
p.remain[i] = b.remaining();
if (i < 2) {
JammerButton j = (JammerButton)b;
p.penalized[i] = j.penalized;
}
}
return p;
}
private void pulse(long now) {
for (int i = 0; i < 8; i += 1) {
tbs[i].pulse(now);
}
}
public void pauseButton(View v) {
for (int i = 0; i < 8; i += 1) {
tbs[i].pause();
}
}
public void resumeButton(View v) {
for (int i = 0; i < 8; i += 1) {
tbs[i].resume();
}
2012-04-03 13:07:33 -06:00
}
}
2012-04-05 19:37:15 -06:00