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];
|
2012-05-26 13:50:57 -06:00
|
|
|
private boolean paused = false;
|
2012-04-05 19:37:15 -06:00
|
|
|
|
|
|
|
private Runnable pulse = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
long now = SystemClock.uptimeMillis();
|
|
|
|
pulse(now);
|
|
|
|
|
|
|
|
mHandler.postAtTime(this, now + 100);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-26 13:50:57 -06:00
|
|
|
class Persistence {
|
|
|
|
public boolean paused = false;
|
|
|
|
public long[] startTime = new long[8];
|
|
|
|
public long[] duration = new long[8];
|
|
|
|
public boolean[] running = new boolean[8];
|
|
|
|
public boolean[] penalized = new boolean[2];
|
|
|
|
}
|
|
|
|
|
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();
|
2012-05-26 13:50:57 -06:00
|
|
|
long now = SystemClock.uptimeMillis();
|
2012-12-08 16:45:58 -07:00
|
|
|
LinearLayout top;
|
2012-04-05 19:37:15 -06:00
|
|
|
|
2012-04-03 13:07:33 -06:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.main);
|
2012-12-08 16:45:58 -07:00
|
|
|
top = (LinearLayout)findViewById(R.id.main);
|
2012-04-05 19:37:15 -06:00
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
// Create all the buttons
|
2012-04-05 19:37:15 -06:00
|
|
|
for (int i = 0; i < 4; i += 1) {
|
2012-12-08 16:45:58 -07:00
|
|
|
LinearLayout tr = new LinearLayout(this);
|
2012-04-05 19:37:15 -06:00
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
top.addView(tr, i);
|
2012-04-05 19:37:15 -06:00
|
|
|
|
|
|
|
for (int j = 0; j < 2; j += 1) {
|
|
|
|
TimerButton b;
|
|
|
|
TextView v;
|
|
|
|
|
|
|
|
if (i == 0) {
|
2012-05-26 13:50:57 -06:00
|
|
|
JammerButton jb = new JammerButton(this, now);
|
2012-12-08 16:45:58 -07:00
|
|
|
|
2012-04-05 19:37:15 -06:00
|
|
|
if (p != null) {
|
|
|
|
jb.penalized = p.penalized[j];
|
|
|
|
}
|
|
|
|
jbs[j] = jb;
|
|
|
|
b = jb;
|
|
|
|
} else {
|
2012-05-26 13:50:57 -06:00
|
|
|
b = new TimerButton(this, now);
|
2012-04-05 19:37:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p != null) {
|
2012-05-26 13:50:57 -06:00
|
|
|
int idx = i*2 + j;
|
|
|
|
|
|
|
|
b.startTime = p.startTime[idx];
|
|
|
|
b.duration = p.duration[idx];
|
|
|
|
b.running = p.running[idx];
|
2012-04-05 19:37:15 -06:00
|
|
|
}
|
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
tr.addView(b.getButton());
|
2012-04-05 19:37:15 -06:00
|
|
|
|
|
|
|
tbs[i*2 + j] = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
|
2012-05-26 13:50:57 -06:00
|
|
|
// Invert value, then simulate a button press
|
|
|
|
if (p != null) {
|
|
|
|
paused = ! p.paused;
|
|
|
|
} else {
|
|
|
|
paused = true;
|
|
|
|
}
|
2012-12-08 16:45:58 -07:00
|
|
|
pauseButton();
|
2012-05-26 13:50:57 -06:00
|
|
|
|
2012-04-05 19:37:15 -06:00
|
|
|
jbs[0].setOther(jbs[1]);
|
|
|
|
jbs[1].setOther(jbs[0]);
|
|
|
|
|
|
|
|
mHandler.postAtTime(pulse, 100);
|
2012-12-08 16:45:58 -07:00
|
|
|
|
2012-04-03 13:07:33 -06:00
|
|
|
}
|
|
|
|
|
2012-04-05 19:37:15 -06:00
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
|
2012-04-05 19:37:15 -06:00
|
|
|
public Object onRetainNonConfigurationInstance() {
|
|
|
|
Persistence p = new Persistence();
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i += 1) {
|
|
|
|
TimerButton b = tbs[i];
|
|
|
|
|
2012-05-26 13:50:57 -06:00
|
|
|
p.paused = paused;
|
|
|
|
p.startTime[i] = b.startTime;
|
|
|
|
p.duration[i] = b.duration;
|
|
|
|
p.running[i] = b.running;
|
2012-04-05 19:37:15 -06:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-08 16:45:58 -07:00
|
|
|
public void pauseButton() {
|
2012-05-26 13:50:57 -06:00
|
|
|
Button e = (Button) findViewById(R.id.pause);
|
2012-04-05 19:37:15 -06:00
|
|
|
|
2012-05-26 13:50:57 -06:00
|
|
|
paused = !paused;
|
2012-04-05 19:37:15 -06:00
|
|
|
for (int i = 0; i < 8; i += 1) {
|
2012-05-26 13:50:57 -06:00
|
|
|
if (paused) {
|
|
|
|
tbs[i].pause();
|
|
|
|
} else {
|
|
|
|
tbs[i].resume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (paused) {
|
2012-05-26 16:22:14 -06:00
|
|
|
e.setText(R.string.jam_begin);
|
2012-05-26 13:50:57 -06:00
|
|
|
} else {
|
2012-05-26 16:22:14 -06:00
|
|
|
e.setText(R.string.jam_end);
|
2012-04-05 19:37:15 -06:00
|
|
|
}
|
2012-04-03 13:07:33 -06:00
|
|
|
}
|
2012-12-08 16:45:58 -07:00
|
|
|
|
|
|
|
public void pauseButton(View v) {
|
|
|
|
pauseButton();
|
|
|
|
}
|
2012-04-03 13:07:33 -06:00
|
|
|
}
|
2012-04-05 19:37:15 -06:00
|
|
|
|