Bugfixes, add vibrator, single begin/end button

* Handles rotation when timers are running
* Sets buttons to disabled when paused
* Vibrates at 10s remaining on any timer
* One button for jam begin and jam end

I still need help figuring out how to get the buttons to change
color.
This commit is contained in:
Neale Pickett 2012-05-26 13:50:57 -06:00
parent 8e8ea4c913
commit f2b0fc27d3
5 changed files with 65 additions and 43 deletions

View File

@ -12,4 +12,5 @@
</intent-filter> </intent-filter>
</activity> </activity>
</application> </application>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest> </manifest>

View File

@ -12,9 +12,9 @@
android:stretchColumns="*"> android:stretchColumns="*">
<TableRow> <TableRow>
<Button android:text="Jam End" <Button android:text="Jam End"
android:id="@+id/pause"
android:layout_span="2"
android:onClick="pauseButton" /> android:onClick="pauseButton" />
<Button android:text="Jam Begin"
android:onClick="resumeButton" />
</TableRow> </TableRow>
</TableLayout> </TableLayout>
</ScrollView> </ScrollView>

View File

@ -10,8 +10,8 @@ public class JammerButton extends TimerButton
public boolean penalized = false; public boolean penalized = false;
private JammerButton peer; private JammerButton peer;
public JammerButton(Context context) { public JammerButton(Context context, long now) {
super(context); super(context, now);
} }
public String str(long remain, boolean tenths) { public String str(long remain, boolean tenths) {
@ -27,7 +27,7 @@ public class JammerButton extends TimerButton
} }
public void onClick(View v) { public void onClick(View v) {
if (! paused && peer.running) { if (peer.running) {
penalized = true; penalized = true;
if (peer.penalized) { if (peer.penalized) {

View File

@ -14,6 +14,7 @@ public class PenaltyTimer extends Activity
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private JammerButton[] jbs = new JammerButton[2]; private JammerButton[] jbs = new JammerButton[2];
private TimerButton[] tbs = new TimerButton[8]; private TimerButton[] tbs = new TimerButton[8];
private boolean paused = false;
private Runnable pulse = new Runnable() { private Runnable pulse = new Runnable() {
public void run() { public void run() {
@ -24,12 +25,21 @@ public class PenaltyTimer extends Activity
} }
}; };
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];
}
/** Called when the activity is first created. */ /** Called when the activity is first created. */
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
Persistence p = (Persistence)getLastNonConfigurationInstance(); Persistence p = (Persistence)getLastNonConfigurationInstance();
TableLayout tl; TableLayout tl;
View top; View top;
long now = SystemClock.uptimeMillis();
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.main); setContentView(R.layout.main);
@ -46,7 +56,7 @@ public class PenaltyTimer extends Activity
TextView v; TextView v;
if (i == 0) { if (i == 0) {
JammerButton jb = new JammerButton(this); JammerButton jb = new JammerButton(this, now);
if (p != null) { if (p != null) {
jb.penalized = p.penalized[j]; jb.penalized = p.penalized[j];
@ -54,16 +64,15 @@ public class PenaltyTimer extends Activity
jbs[j] = jb; jbs[j] = jb;
b = jb; b = jb;
} else { } else {
b = new TimerButton(this); b = new TimerButton(this, now);
} }
if (p != null) { if (p != null) {
b.set(p.remain[i*2 + j]); int idx = i*2 + j;
if (p.paused) {
b.pause(); b.startTime = p.startTime[idx];
} else { b.duration = p.duration[idx];
b.resume(); b.running = p.running[idx];
}
} }
v = b.getButton(); v = b.getButton();
@ -73,18 +82,20 @@ public class PenaltyTimer extends Activity
} }
} }
// Invert value, then simulate a button press
if (p != null) {
paused = ! p.paused;
} else {
paused = true;
}
pauseButton(top);
jbs[0].setOther(jbs[1]); jbs[0].setOther(jbs[1]);
jbs[1].setOther(jbs[0]); jbs[1].setOther(jbs[0]);
mHandler.postAtTime(pulse, 100); mHandler.postAtTime(pulse, 100);
} }
class Persistence {
public long[] remain = new long[8];
public boolean paused = false;
public boolean[] penalized = new boolean[2];
}
public Object onRetainNonConfigurationInstance() { public Object onRetainNonConfigurationInstance() {
Persistence p = new Persistence(); Persistence p = new Persistence();
@ -93,10 +104,10 @@ public class PenaltyTimer extends Activity
for (i = 0; i < 8; i += 1) { for (i = 0; i < 8; i += 1) {
TimerButton b = tbs[i]; TimerButton b = tbs[i];
if (b.paused) { p.paused = paused;
p.paused = true; p.startTime[i] = b.startTime;
} p.duration[i] = b.duration;
p.remain[i] = b.remaining(); p.running[i] = b.running;
if (i < 2) { if (i < 2) {
JammerButton j = (JammerButton)b; JammerButton j = (JammerButton)b;
@ -115,15 +126,21 @@ public class PenaltyTimer extends Activity
public void pauseButton(View v) { public void pauseButton(View v) {
for (int i = 0; i < 8; i += 1) { Button e = (Button) findViewById(R.id.pause);
tbs[i].pause();
}
}
public void resumeButton(View v) { paused = !paused;
for (int i = 0; i < 8; i += 1) { for (int i = 0; i < 8; i += 1) {
if (paused) {
tbs[i].pause();
} else {
tbs[i].resume(); tbs[i].resume();
} }
} }
if (paused) {
e.setText("Jam Begin");
} else {
e.setText("Jam End");
}
}
} }

View File

@ -7,6 +7,7 @@ import android.view.View.*;
import android.view.Gravity; import android.view.Gravity;
import android.graphics.Color; import android.graphics.Color;
import android.os.SystemClock; import android.os.SystemClock;
import android.os.Vibrator;
import java.util.*; import java.util.*;
public class TimerButton public class TimerButton
@ -14,26 +15,30 @@ public class TimerButton
{ {
Button b; Button b;
Timer updateTimer; Timer updateTimer;
long startTime = 0; public long startTime = 0;
long duration = 0; public long duration = 0;
public boolean running = false; public boolean running = false;
public boolean paused = false; boolean shook = false;
Vibrator vibr;
long now; long now;
static int releaseColor = Color.rgb(0, 127, 0); static int releaseColor = Color.rgb(0, 127, 0);
static int standColor = Color.rgb(127, 127, 0); static int standColor = Color.rgb(127, 127, 0);
static int normalColor = Color.BLACK; static int normalColor = Color.BLACK;
static int pauseColor = Color.GRAY;
public TimerButton(Context context) { public TimerButton(Context context, long now) {
b = new Button(context); b = new Button(context);
vibr = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
b.setText("--:--"); b.setText("--:--");
b.setTextSize(24); b.setTextSize(24);
b.setGravity(Gravity.CENTER); b.setGravity(Gravity.CENTER);
updateTimer = new Timer(); updateTimer = new Timer();
b.setOnClickListener(this); b.setOnClickListener(this);
b.setOnLongClickListener(this); b.setOnLongClickListener(this);
this.now = now;
} }
public long remaining() { public long remaining() {
@ -54,7 +59,6 @@ public class TimerButton
return "--:--"; return "--:--";
} }
ret = min + ":"; ret = min + ":";
if (sec < 10) { if (sec < 10) {
ret += "0"; ret += "0";
@ -104,14 +108,17 @@ public class TimerButton
if ((duration > 0) && (r <= 0)) { if ((duration > 0) && (r <= 0)) {
duration = 0; duration = 0;
running = false; running = false;
shook = false;
expireHook(); expireHook();
} }
if (paused) { if (r <= 0) {
b.setTextColor(pauseColor);
} else if (r <= 0) {
b.setTextColor(releaseColor); b.setTextColor(releaseColor);
} else if (r < 10000) { } else if (r < 10000) {
if (! shook) {
vibr.vibrate(200);
shook = true;
}
b.setTextColor(standColor); b.setTextColor(standColor);
} else { } else {
b.setTextColor(normalColor); b.setTextColor(normalColor);
@ -125,9 +132,6 @@ public class TimerButton
} }
public void onClick(View v) { public void onClick(View v) {
if (paused) {
return;
}
set((remaining() + 60000) % (60 * 8 * 1000)); set((remaining() + 60000) % (60 * 8 * 1000));
start(); start();
} }
@ -140,12 +144,12 @@ public class TimerButton
} }
public void pause() { public void pause() {
paused = true;
stop(); stop();
b.setEnabled(false);
} }
public void resume() { public void resume() {
paused = false; b.setEnabled(true);
if (remaining() > 0) { if (remaining() > 0) {
start(); start();
} }