Added alarm indicator to Your Train card.

Fixed mysterious "freeze" of notifications (turned out to be unexpected gc of weak references).
Departure countdown now reads "Leaving" at the end of the countdown.
This commit is contained in:
Doug Keen 2012-10-10 17:21:30 -07:00
parent 699a40e93b
commit 5836ccb89c
6 changed files with 192 additions and 82 deletions

View File

@ -2,6 +2,18 @@
<merge xmlns:android="http://schemas.android.com/apk/res/android" <merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:bart="http://schemas.android.com/apk/res/com.dougkeen.bart" > xmlns:bart="http://schemas.android.com/apk/res/com.dougkeen.bart" >
<TextView
android:id="@+id/alarmText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:drawableLeft="@drawable/ic_action_alarm"
android:gravity="center_vertical"
android:textSize="16dp"
android:visibility="gone" >
</TextView>
<TextView <TextView
android:id="@+id/yourTrainHeader" android:id="@+id/yourTrainHeader"
android:layout_width="wrap_content" android:layout_width="wrap_content"

View File

@ -43,5 +43,5 @@
<string name="cancel_alarm">Cancel alarm</string> <string name="cancel_alarm">Cancel alarm</string>
<string name="set_alarm">Set alarm</string> <string name="set_alarm">Set alarm</string>
<string name="leaving">Leaving</string> <string name="leaving">Leaving</string>
<string name="departed">Departed</string>
</resources> </resources>

View File

@ -176,7 +176,7 @@ public class ViewDeparturesActivity extends SActivity implements
&& mSelectedDeparture != null) { && mSelectedDeparture != null) {
((Checkable) findViewById(R.id.yourTrainSection)) ((Checkable) findViewById(R.id.yourTrainSection))
.setChecked(true); .setChecked(true);
startYourTrainActionMode(bartRunnerApplication); startYourTrainActionMode();
} }
} }
setListAdapter(mDeparturesAdapter); setListAdapter(mDeparturesAdapter);
@ -245,9 +245,7 @@ public class ViewDeparturesActivity extends SActivity implements
tryToPlayRingtone(alarmSound); tryToPlayRingtone(alarmSound);
} }
final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (vibrator.hasVibrator()) { vibrator.vibrate(new long[] { 0, 500, 500 }, 1);
vibrator.vibrate(new long[] { 0, 500, 500 }, 1);
}
mHandler.postDelayed(new Runnable() { mHandler.postDelayed(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -276,9 +274,7 @@ public class ViewDeparturesActivity extends SActivity implements
application.setAlarmSounding(false); application.setAlarmSounding(false);
application.setAlarmMediaPlayer(null); application.setAlarmMediaPlayer(null);
final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
if (vibrator.hasVibrator()) { vibrator.cancel();
vibrator.cancel();
}
try { try {
if (mediaPlayer != null && mediaPlayer.isPlaying()) { if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop(); mediaPlayer.stop();
@ -363,7 +359,7 @@ public class ViewDeparturesActivity extends SActivity implements
@Override @Override
public void onClick(View v) { public void onClick(View v) {
((Checkable) v).setChecked(true); ((Checkable) v).setChecked(true);
startYourTrainActionMode((BartRunnerApplication) getApplication()); startYourTrainActionMode();
} }
}; };
@ -472,28 +468,28 @@ public class ViewDeparturesActivity extends SActivity implements
} }
private void refreshBoardedDeparture(boolean animate) { private void refreshBoardedDeparture(boolean animate) {
final Departure boardedDeparture = ((BartRunnerApplication) getApplication()) final Departure boardedDeparture = getBoardedDeparture();
.getBoardedDeparture();
int currentVisibility = mYourTrainSection.getVisibility(); int currentVisibility = mYourTrainSection.getVisibility();
final boolean boardedDepartureDoesNotApply = boardedDeparture == null if (!doesDepartureApply(boardedDeparture)) {
|| boardedDeparture.getStationPair() == null
|| !boardedDeparture.getStationPair().equals(getStationPair());
if (boardedDepartureDoesNotApply) {
if (currentVisibility != View.GONE) { if (currentVisibility != View.GONE) {
hideYourTrainSection(); hideYourTrainSection();
} }
return; return;
} }
mYourTrainSection.updateFromDeparture(boardedDeparture); mYourTrainSection.updateFromBoardedDeparture();
if (currentVisibility != View.VISIBLE) { if (currentVisibility != View.VISIBLE) {
showYourTrainSection(animate); showYourTrainSection(animate);
} }
} }
private boolean doesDepartureApply(final Departure departure) {
return departure != null && departure.getStationPair() != null
&& departure.getStationPair().equals(getStationPair());
}
private void setBoardedDeparture(Departure selectedDeparture) { private void setBoardedDeparture(Departure selectedDeparture) {
final BartRunnerApplication application = (BartRunnerApplication) getApplication(); final BartRunnerApplication application = (BartRunnerApplication) getApplication();
selectedDeparture.setPassengerDestination(mDestination); selectedDeparture.setPassengerDestination(mDestination);
@ -546,14 +542,13 @@ public class ViewDeparturesActivity extends SActivity implements
} }
private void startYourTrainActionMode(BartRunnerApplication application) { private void startYourTrainActionMode() {
if (mActionMode == null) if (mActionMode == null)
mActionMode = startActionMode(new YourTrainActionMode()); mActionMode = startActionMode(new YourTrainActionMode());
mActionMode.setTitle(R.string.your_train); mActionMode.setTitle(R.string.your_train);
if (application.getBoardedDeparture() != null Departure boardedDeparture = getBoardedDeparture();
&& application.getBoardedDeparture().isAlarmPending()) { if (boardedDeparture != null && boardedDeparture.isAlarmPending()) {
int leadTime = application.getBoardedDeparture() int leadTime = boardedDeparture.getAlarmLeadTimeMinutes();
.getAlarmLeadTimeMinutes();
mActionMode.setSubtitle(getAlarmSubtitle(leadTime)); mActionMode.setSubtitle(getAlarmSubtitle(leadTime));
} else { } else {
mActionMode.setSubtitle(null); mActionMode.setSubtitle(null);
@ -579,9 +574,7 @@ public class ViewDeparturesActivity extends SActivity implements
.findItem(R.id.cancel_alarm_button); .findItem(R.id.cancel_alarm_button);
final MenuItem setAlarmButton = menu final MenuItem setAlarmButton = menu
.findItem(R.id.set_alarm_button); .findItem(R.id.set_alarm_button);
final BartRunnerApplication application = (BartRunnerApplication) getApplication(); final Departure boardedDeparture = getBoardedDeparture();
final Departure boardedDeparture = application
.getBoardedDeparture();
if (boardedDeparture == null) { if (boardedDeparture == null) {
mode.finish(); mode.finish();
@ -652,10 +645,8 @@ public class ViewDeparturesActivity extends SActivity implements
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
final int itemId = item.getItemId(); final int itemId = item.getItemId();
if (itemId == R.id.set_alarm_button) { if (itemId == R.id.set_alarm_button) {
BartRunnerApplication application = (BartRunnerApplication) getApplication();
// Don't prompt for alarm if train is about to leave // Don't prompt for alarm if train is about to leave
if (application.getBoardedDeparture().getMeanSecondsLeft() > 60) { if (getBoardedDeparture().getMeanSecondsLeft() > 60) {
new TrainAlarmDialogFragment() new TrainAlarmDialogFragment()
.show(getSupportFragmentManager() .show(getSupportFragmentManager()
.beginTransaction()); .beginTransaction());
@ -680,9 +671,7 @@ public class ViewDeparturesActivity extends SActivity implements
public void onDestroyActionMode(ActionMode mode) { public void onDestroyActionMode(ActionMode mode) {
((Checkable) findViewById(R.id.yourTrainSection)).setChecked(false); ((Checkable) findViewById(R.id.yourTrainSection)).setChecked(false);
final BartRunnerApplication application = (BartRunnerApplication) getApplication(); final Departure boardedDeparture = getBoardedDeparture();
final Departure boardedDeparture = application
.getBoardedDeparture();
if (boardedDeparture != null) { if (boardedDeparture != null) {
boardedDeparture.getAlarmPendingObservable() boardedDeparture.getAlarmPendingObservable()
.unregisterObserver(mAlarmPendingObserver); .unregisterObserver(mAlarmPendingObserver);
@ -719,10 +708,18 @@ public class ViewDeparturesActivity extends SActivity implements
Ticker.getInstance().startTicking( Ticker.getInstance().startTicking(
ViewDeparturesActivity.this); ViewDeparturesActivity.this);
Departure boardedDeparture = getBoardedDeparture();
boolean boardedDepartureFound = false;
// Merge lists // Merge lists
if (mDeparturesAdapter.getCount() > 0) { if (mDeparturesAdapter.getCount() > 0) {
int adapterIndex = -1; int adapterIndex = -1;
for (Departure departure : departures) { for (Departure departure : departures) {
if (!boardedDepartureFound
&& departure.equals(boardedDeparture)) {
boardedDepartureFound = true;
}
adapterIndex++; adapterIndex++;
Departure existingDeparture = null; Departure existingDeparture = null;
if (adapterIndex < mDeparturesAdapter.getCount()) { if (adapterIndex < mDeparturesAdapter.getCount()) {
@ -755,6 +752,11 @@ public class ViewDeparturesActivity extends SActivity implements
} }
} }
if (doesDepartureApply(boardedDeparture)
&& !boardedDepartureFound) {
boardedDeparture.setListedInETDs(false);
}
refreshBoardedDeparture(true); refreshBoardedDeparture(true);
getListAdapter().notifyDataSetChanged(); getListAdapter().notifyDataSetChanged();
@ -771,8 +773,7 @@ public class ViewDeparturesActivity extends SActivity implements
if (isDepartureActionModeActive() && mSelectedDeparture != null) { if (isDepartureActionModeActive() && mSelectedDeparture != null) {
targetDeparture = mSelectedDeparture; targetDeparture = mSelectedDeparture;
} else { } else {
targetDeparture = ((BartRunnerApplication) getApplication()) targetDeparture = getBoardedDeparture();
.getBoardedDeparture();
} }
for (int i = getListAdapter().getCount() - 1; i >= 0; i--) { for (int i = getListAdapter().getCount() - 1; i >= 0; i--) {
if (getListAdapter().getItem(i).equals(targetDeparture)) { if (getListAdapter().getItem(i).equals(targetDeparture)) {
@ -851,4 +852,8 @@ public class ViewDeparturesActivity extends SActivity implements
&& !mActionMode.getTitle().equals( && !mActionMode.getTitle().equals(
getString(R.string.your_train)); getString(R.string.your_train));
} }
private Departure getBoardedDeparture() {
return ((BartRunnerApplication) getApplication()).getBoardedDeparture();
}
} }

View File

@ -1,5 +1,6 @@
package com.dougkeen.bart.controls; package com.dougkeen.bart.controls;
import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.drawable.GradientDrawable; import android.graphics.drawable.GradientDrawable;
@ -11,9 +12,11 @@ import android.widget.ImageView;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import com.dougkeen.bart.BartRunnerApplication;
import com.dougkeen.bart.R; import com.dougkeen.bart.R;
import com.dougkeen.bart.model.Departure; import com.dougkeen.bart.model.Departure;
import com.dougkeen.bart.model.TextProvider; import com.dougkeen.bart.model.TextProvider;
import com.dougkeen.util.Observer;
public class YourTrainLayout extends RelativeLayout implements Checkable { public class YourTrainLayout extends RelativeLayout implements Checkable {
@ -38,6 +41,28 @@ public class YourTrainLayout extends RelativeLayout implements Checkable {
private boolean mChecked; private boolean mChecked;
private Departure mDeparture;
private final Observer<Integer> mAlarmLeadObserver = new Observer<Integer>() {
@Override
public void onUpdate(Integer newValue) {
final Activity context = (Activity) getContext();
if (context != null) {
context.runOnUiThread(mUpdateAlarmIndicatorRunnable);
}
}
};
private final Observer<Boolean> mAlarmPendingObserver = new Observer<Boolean>() {
@Override
public void onUpdate(Boolean newValue) {
final Activity context = (Activity) getContext();
if (context != null) {
context.runOnUiThread(mUpdateAlarmIndicatorRunnable);
}
}
};
@Override @Override
public boolean isChecked() { public boolean isChecked() {
return mChecked; return mChecked;
@ -64,7 +89,27 @@ public class YourTrainLayout extends RelativeLayout implements Checkable {
setChecked(!isChecked()); setChecked(!isChecked());
} }
public void updateFromDeparture(final Departure boardedDeparture) { public void updateFromBoardedDeparture() {
final Departure boardedDeparture = ((BartRunnerApplication) ((Activity) getContext())
.getApplication()).getBoardedDeparture();
if (boardedDeparture == null)
return;
if (!boardedDeparture.equals(mDeparture)) {
if (mDeparture != null) {
mDeparture.getAlarmLeadTimeMinutesObservable()
.unregisterObserver(mAlarmLeadObserver);
mDeparture.getAlarmPendingObservable().unregisterObserver(
mAlarmPendingObserver);
}
boardedDeparture.getAlarmLeadTimeMinutesObservable()
.registerObserver(mAlarmLeadObserver);
boardedDeparture.getAlarmPendingObservable().registerObserver(
mAlarmPendingObserver);
}
mDeparture = boardedDeparture;
((TextView) findViewById(R.id.yourTrainDestinationText)) ((TextView) findViewById(R.id.yourTrainDestinationText))
.setText(boardedDeparture.getTrainDestination().toString()); .setText(boardedDeparture.getTrainDestination().toString());
@ -88,6 +133,9 @@ public class YourTrainLayout extends RelativeLayout implements Checkable {
((ImageView) findViewById(R.id.yourTrainXferIcon)) ((ImageView) findViewById(R.id.yourTrainXferIcon))
.setVisibility(View.INVISIBLE); .setVisibility(View.INVISIBLE);
} }
updateAlarmIndicator();
CountdownTextView departureCountdown = (CountdownTextView) findViewById(R.id.yourTrainDepartureCountdown); CountdownTextView departureCountdown = (CountdownTextView) findViewById(R.id.yourTrainDepartureCountdown);
CountdownTextView arrivalCountdown = (CountdownTextView) findViewById(R.id.yourTrainArrivalCountdown); CountdownTextView arrivalCountdown = (CountdownTextView) findViewById(R.id.yourTrainArrivalCountdown);
@ -95,7 +143,7 @@ public class YourTrainLayout extends RelativeLayout implements Checkable {
@Override @Override
public String getText(long tickNumber) { public String getText(long tickNumber) {
if (boardedDeparture.hasDeparted()) { if (boardedDeparture.hasDeparted()) {
return getContext().getString(R.string.leaving); return boardedDeparture.getCountdownText();
} else { } else {
return "Leaves in " + boardedDeparture.getCountdownText() return "Leaves in " + boardedDeparture.getCountdownText()
+ " " + boardedDeparture.getUncertaintyText(); + " " + boardedDeparture.getUncertaintyText();
@ -117,4 +165,21 @@ public class YourTrainLayout extends RelativeLayout implements Checkable {
setBackground(); setBackground();
} }
private void updateAlarmIndicator() {
if (!mDeparture.isAlarmPending()) {
findViewById(R.id.alarmText).setVisibility(GONE);
} else {
findViewById(R.id.alarmText).setVisibility(VISIBLE);
((TextView) findViewById(R.id.alarmText)).setText(String
.valueOf(mDeparture.getAlarmLeadTimeMinutes()));
}
}
private final Runnable mUpdateAlarmIndicatorRunnable = new Runnable() {
@Override
public void run() {
updateAlarmIndicator();
}
};
} }

View File

@ -74,6 +74,8 @@ public class Departure implements Parcelable, Comparable<Departure> {
0); 0);
private Observable<Boolean> alarmPending = new Observable<Boolean>(false); private Observable<Boolean> alarmPending = new Observable<Boolean>(false);
private boolean listedInETDs = true;
public Station getOrigin() { public Station getOrigin() {
return origin; return origin;
} }
@ -463,9 +465,12 @@ public class Departure implements Parcelable, Comparable<Departure> {
if (hasDeparted()) { if (hasDeparted()) {
if (origin != null && origin.longStationLinger && beganAsDeparted) { if (origin != null && origin.longStationLinger && beganAsDeparted) {
builder.append("At station"); builder.append("At station");
} else { } else if (isListedInETDs()) {
builder.append(BartRunnerApplication.getAppContext().getString( builder.append(BartRunnerApplication.getAppContext().getString(
R.string.leaving)); R.string.leaving));
} else {
builder.append(BartRunnerApplication.getAppContext().getString(
R.string.departed));
} }
} else { } else {
builder.append(secondsLeft / 60); builder.append(secondsLeft / 60);
@ -484,6 +489,14 @@ public class Departure implements Parcelable, Comparable<Departure> {
} }
} }
public boolean isListedInETDs() {
return listedInETDs;
}
public void setListedInETDs(boolean listedInETDs) {
this.listedInETDs = listedInETDs;
}
public int getAlarmLeadTimeMinutes() { public int getAlarmLeadTimeMinutes() {
return alarmLeadTimeMinutes.getValue(); return alarmLeadTimeMinutes.getValue();
} }

View File

@ -136,6 +136,8 @@ public class BoardedDepartureService extends Service implements
if (intent.getBooleanExtra("clearBoardedDeparture", false)) { if (intent.getBooleanExtra("clearBoardedDeparture", false)) {
application.setBoardedDeparture(null); application.setBoardedDeparture(null);
shutDown(false); shutDown(false);
} else {
updateNotification();
} }
return; return;
} }
@ -232,6 +234,19 @@ public class BoardedDepartureService extends Service implements
return; return;
} }
if (mEtdService != null) {
/*
* Make sure we're still listening for ETD changes (in case weak ref
* was garbage collected). Not a huge fan of this approach, but I
* think I'd rather keep the weak references to avoid memory leaks
* than move to soft references or some other form of stronger
* reference. Besides, registerListener() should only result in a
* few constant-time map operations, so there shouldn't be a big
* performance hit.
*/
mEtdService.registerListener(this, false);
}
boardedDeparture.updateAlarm(getApplicationContext(), mAlarmManager); boardedDeparture.updateAlarm(getApplicationContext(), mAlarmManager);
updateNotification(); updateNotification();