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>
</activity>
</application>
<uses-permission android:name="android.permission.VIBRATE"/>
</manifest>

View File

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

View File

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

View File

@ -14,6 +14,7 @@ public class PenaltyTimer extends Activity
private Handler mHandler = new Handler();
private JammerButton[] jbs = new JammerButton[2];
private TimerButton[] tbs = new TimerButton[8];
private boolean paused = false;
private Runnable pulse = new Runnable() {
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. */
@Override
public void onCreate(Bundle savedInstanceState) {
Persistence p = (Persistence)getLastNonConfigurationInstance();
TableLayout tl;
View top;
long now = SystemClock.uptimeMillis();
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@ -46,7 +56,7 @@ public class PenaltyTimer extends Activity
TextView v;
if (i == 0) {
JammerButton jb = new JammerButton(this);
JammerButton jb = new JammerButton(this, now);
if (p != null) {
jb.penalized = p.penalized[j];
@ -54,16 +64,15 @@ public class PenaltyTimer extends Activity
jbs[j] = jb;
b = jb;
} else {
b = new TimerButton(this);
b = new TimerButton(this, now);
}
if (p != null) {
b.set(p.remain[i*2 + j]);
if (p.paused) {
b.pause();
} else {
b.resume();
}
int idx = i*2 + j;
b.startTime = p.startTime[idx];
b.duration = p.duration[idx];
b.running = p.running[idx];
}
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[1].setOther(jbs[0]);
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() {
Persistence p = new Persistence();
@ -93,10 +104,10 @@ public class PenaltyTimer extends Activity
for (i = 0; i < 8; i += 1) {
TimerButton b = tbs[i];
if (b.paused) {
p.paused = true;
}
p.remain[i] = b.remaining();
p.paused = paused;
p.startTime[i] = b.startTime;
p.duration[i] = b.duration;
p.running[i] = b.running;
if (i < 2) {
JammerButton j = (JammerButton)b;
@ -115,15 +126,21 @@ public class PenaltyTimer extends Activity
public void pauseButton(View v) {
for (int i = 0; i < 8; i += 1) {
tbs[i].pause();
}
}
Button e = (Button) findViewById(R.id.pause);
public void resumeButton(View v) {
paused = !paused;
for (int i = 0; i < 8; i += 1) {
if (paused) {
tbs[i].pause();
} else {
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.graphics.Color;
import android.os.SystemClock;
import android.os.Vibrator;
import java.util.*;
public class TimerButton
@ -14,26 +15,30 @@ public class TimerButton
{
Button b;
Timer updateTimer;
long startTime = 0;
long duration = 0;
public long startTime = 0;
public long duration = 0;
public boolean running = false;
public boolean paused = false;
boolean shook = false;
Vibrator vibr;
long now;
static int releaseColor = Color.rgb(0, 127, 0);
static int standColor = Color.rgb(127, 127, 0);
static int normalColor = Color.BLACK;
static int pauseColor = Color.GRAY;
public TimerButton(Context context) {
public TimerButton(Context context, long now) {
b = new Button(context);
vibr = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
b.setText("--:--");
b.setTextSize(24);
b.setGravity(Gravity.CENTER);
updateTimer = new Timer();
b.setOnClickListener(this);
b.setOnLongClickListener(this);
this.now = now;
}
public long remaining() {
@ -54,7 +59,6 @@ public class TimerButton
return "--:--";
}
ret = min + ":";
if (sec < 10) {
ret += "0";
@ -104,14 +108,17 @@ public class TimerButton
if ((duration > 0) && (r <= 0)) {
duration = 0;
running = false;
shook = false;
expireHook();
}
if (paused) {
b.setTextColor(pauseColor);
} else if (r <= 0) {
if (r <= 0) {
b.setTextColor(releaseColor);
} else if (r < 10000) {
if (! shook) {
vibr.vibrate(200);
shook = true;
}
b.setTextColor(standColor);
} else {
b.setTextColor(normalColor);
@ -125,9 +132,6 @@ public class TimerButton
}
public void onClick(View v) {
if (paused) {
return;
}
set((remaining() + 60000) % (60 * 8 * 1000));
start();
}
@ -140,12 +144,12 @@ public class TimerButton
}
public void pause() {
paused = true;
stop();
b.setEnabled(false);
}
public void resume() {
paused = false;
b.setEnabled(true);
if (remaining() > 0) {
start();
}