2011-05-23 18:59:34 +00:00
|
|
|
package com.dougkeen.bart;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import android.app.ListActivity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Bundle;
|
2011-05-27 21:06:58 +00:00
|
|
|
import android.os.Parcelable;
|
2011-05-23 18:59:34 +00:00
|
|
|
import android.os.PowerManager;
|
2011-05-27 21:06:58 +00:00
|
|
|
import android.text.format.DateFormat;
|
2011-06-05 18:13:21 +00:00
|
|
|
import android.text.util.Linkify;
|
2011-05-27 21:06:58 +00:00
|
|
|
import android.util.Log;
|
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuInflater;
|
|
|
|
import android.view.MenuItem;
|
2012-02-20 01:46:11 +00:00
|
|
|
import android.view.View;
|
2011-05-23 18:59:34 +00:00
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.TextView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
import com.dougkeen.bart.GetRealTimeDeparturesTask.Params;
|
|
|
|
import com.dougkeen.bart.data.Departure;
|
|
|
|
import com.dougkeen.bart.data.RealTimeDepartures;
|
2011-06-01 03:42:32 +00:00
|
|
|
import com.dougkeen.bart.data.RoutesColumns;
|
2011-05-23 18:59:34 +00:00
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
public class ViewDeparturesActivity extends ListActivity {
|
2011-05-23 18:59:34 +00:00
|
|
|
|
2011-05-27 21:06:58 +00:00
|
|
|
private static final int UNCERTAINTY_THRESHOLD = 17;
|
|
|
|
|
2011-05-23 18:59:34 +00:00
|
|
|
private Uri mUri;
|
|
|
|
|
|
|
|
private Station mOrigin;
|
|
|
|
private Station mDestination;
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
private ArrayAdapter<Departure> mDeparturesAdapter;
|
2011-05-23 18:59:34 +00:00
|
|
|
|
|
|
|
private TextView mListTitleView;
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
private AsyncTask<Params, Integer, RealTimeDepartures> mGetDeparturesTask;
|
2011-05-23 18:59:34 +00:00
|
|
|
|
|
|
|
private boolean mIsAutoUpdating = false;
|
|
|
|
|
|
|
|
private final Runnable AUTO_UPDATE_RUNNABLE = new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
runAutoUpdate();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private PowerManager.WakeLock mWakeLock;
|
|
|
|
|
2011-06-17 21:35:20 +00:00
|
|
|
private boolean mDataFetchIsPending;
|
2011-05-27 21:06:58 +00:00
|
|
|
|
2011-05-23 18:59:34 +00:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setContentView(R.layout.main);
|
|
|
|
|
|
|
|
final Intent intent = getIntent();
|
|
|
|
|
|
|
|
String action = intent.getAction();
|
|
|
|
|
|
|
|
if (Intent.ACTION_VIEW.equals(action)) {
|
|
|
|
mUri = intent.getData();
|
|
|
|
}
|
|
|
|
|
|
|
|
Cursor cursor = managedQuery(mUri, new String[] {
|
2011-05-27 21:06:58 +00:00
|
|
|
RoutesColumns.FROM_STATION.string,
|
|
|
|
RoutesColumns.TO_STATION.string }, null, null, null);
|
2011-05-23 18:59:34 +00:00
|
|
|
|
|
|
|
if (!cursor.moveToFirst()) {
|
|
|
|
throw new IllegalStateException("URI not found: " + mUri.toString());
|
|
|
|
}
|
|
|
|
mOrigin = Station.getByAbbreviation(cursor.getString(0));
|
|
|
|
mDestination = Station.getByAbbreviation(cursor.getString(1));
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
String header = "Departures:\n" + mOrigin.name + " to "
|
|
|
|
+ mDestination.name;
|
2011-05-23 18:59:34 +00:00
|
|
|
|
|
|
|
mListTitleView = (TextView) findViewById(R.id.listTitle);
|
|
|
|
mListTitleView.setText(header);
|
|
|
|
((TextView) findViewById(android.R.id.empty))
|
2011-06-05 18:13:21 +00:00
|
|
|
.setText(R.string.departure_wait_message);
|
2011-05-23 18:59:34 +00:00
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
mDeparturesAdapter = new DepartureArrayAdapter(this,
|
|
|
|
R.layout.departure_listing);
|
2011-05-27 21:06:58 +00:00
|
|
|
if (savedInstanceState != null
|
2011-06-05 18:13:21 +00:00
|
|
|
&& savedInstanceState.containsKey("departures")) {
|
|
|
|
for (Parcelable departure : savedInstanceState
|
|
|
|
.getParcelableArray("departures")) {
|
|
|
|
mDeparturesAdapter.add((Departure) departure);
|
2011-05-27 21:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
setListAdapter(mDeparturesAdapter);
|
2012-02-20 01:46:11 +00:00
|
|
|
|
|
|
|
findViewById(R.id.missingDepartureText).setVisibility(View.VISIBLE);
|
2011-05-27 21:06:58 +00:00
|
|
|
}
|
|
|
|
|
2011-06-20 22:34:09 +00:00
|
|
|
@Override
|
|
|
|
protected void onPause() {
|
|
|
|
cancelDataFetch();
|
|
|
|
super.onPause();
|
|
|
|
}
|
|
|
|
|
2011-05-27 21:06:58 +00:00
|
|
|
@Override
|
|
|
|
protected void onDestroy() {
|
2011-06-20 22:34:09 +00:00
|
|
|
cancelDataFetch();
|
|
|
|
super.onDestroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cancelDataFetch() {
|
2011-06-05 18:13:21 +00:00
|
|
|
if (mGetDeparturesTask != null) {
|
|
|
|
mGetDeparturesTask.cancel(true);
|
2011-06-17 21:35:20 +00:00
|
|
|
mDataFetchIsPending = false;
|
2011-05-27 21:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
|
super.onSaveInstanceState(outState);
|
2011-06-05 18:13:21 +00:00
|
|
|
Departure[] departures = new Departure[mDeparturesAdapter.getCount()];
|
|
|
|
for (int i = mDeparturesAdapter.getCount() - 1; i >= 0; i--) {
|
|
|
|
departures[i] = mDeparturesAdapter.getItem(i);
|
2011-05-27 21:06:58 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
outState.putParcelableArray("departures", departures);
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
|
|
super.onWindowFocusChanged(hasFocus);
|
|
|
|
if (hasFocus) {
|
2011-06-17 21:35:20 +00:00
|
|
|
if (!mDataFetchIsPending) {
|
2011-06-05 18:13:21 +00:00
|
|
|
fetchLatestDepartures();
|
2011-05-27 21:06:58 +00:00
|
|
|
}
|
2011-06-20 22:34:09 +00:00
|
|
|
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
|
|
|
mWakeLock = powerManager
|
2011-11-05 18:51:20 +00:00
|
|
|
.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,
|
2011-06-05 18:13:21 +00:00
|
|
|
"ViewDeparturesActivity");
|
2011-05-23 18:59:34 +00:00
|
|
|
mWakeLock.acquire();
|
2011-06-05 18:13:21 +00:00
|
|
|
if (mDeparturesAdapter != null && !mDeparturesAdapter.isEmpty()) {
|
2011-05-27 21:06:58 +00:00
|
|
|
mIsAutoUpdating = true;
|
|
|
|
}
|
|
|
|
runAutoUpdate();
|
2011-05-23 18:59:34 +00:00
|
|
|
} else if (mWakeLock != null) {
|
|
|
|
mWakeLock.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
private void fetchLatestDepartures() {
|
2011-05-27 21:06:58 +00:00
|
|
|
if (!hasWindowFocus())
|
|
|
|
return;
|
2011-06-05 18:13:21 +00:00
|
|
|
if (mGetDeparturesTask != null
|
2011-11-05 18:51:20 +00:00
|
|
|
&& mGetDeparturesTask.getStatus().equals(
|
|
|
|
AsyncTask.Status.RUNNING)) {
|
2011-06-01 03:42:32 +00:00
|
|
|
// Don't overlap fetches
|
|
|
|
return;
|
|
|
|
}
|
2011-05-27 21:06:58 +00:00
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
mGetDeparturesTask = new GetRealTimeDeparturesTask() {
|
2011-05-23 18:59:34 +00:00
|
|
|
@Override
|
2011-06-05 18:13:21 +00:00
|
|
|
public void onResult(RealTimeDepartures result) {
|
2011-06-17 21:35:20 +00:00
|
|
|
mDataFetchIsPending = false;
|
2011-06-01 03:42:32 +00:00
|
|
|
Log.i(Constants.TAG, "Processing data from server");
|
2011-06-05 18:13:21 +00:00
|
|
|
processLatestDepartures(result);
|
2011-06-01 03:42:32 +00:00
|
|
|
Log.i(Constants.TAG, "Done processing data from server");
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2011-06-17 21:35:20 +00:00
|
|
|
public void onError(Exception e) {
|
|
|
|
mDataFetchIsPending = false;
|
|
|
|
Log.w(Constants.TAG, e.getMessage(), e);
|
2011-06-05 18:13:21 +00:00
|
|
|
Toast.makeText(ViewDeparturesActivity.this,
|
2011-11-05 18:51:20 +00:00
|
|
|
R.string.could_not_connect, Toast.LENGTH_LONG).show();
|
2011-06-01 03:42:32 +00:00
|
|
|
((TextView) findViewById(android.R.id.empty))
|
|
|
|
.setText(R.string.could_not_connect);
|
2011-07-03 01:11:10 +00:00
|
|
|
// Try again in 60s
|
|
|
|
scheduleDataFetch(60000);
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
};
|
2011-06-01 03:42:32 +00:00
|
|
|
Log.i(Constants.TAG, "Fetching data from server");
|
2011-06-05 18:13:21 +00:00
|
|
|
mGetDeparturesTask.execute(new GetRealTimeDeparturesTask.Params(
|
2011-11-05 18:51:20 +00:00
|
|
|
mOrigin, mDestination));
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
protected void processLatestDepartures(RealTimeDepartures result) {
|
|
|
|
if (result.getDepartures().isEmpty()) {
|
|
|
|
final TextView textView = (TextView) findViewById(android.R.id.empty);
|
|
|
|
textView.setText(R.string.no_data_message);
|
|
|
|
Linkify.addLinks(textView, Linkify.WEB_URLS);
|
2011-05-25 15:31:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-27 21:06:58 +00:00
|
|
|
boolean needsBetterAccuracy = false;
|
2011-06-05 18:13:21 +00:00
|
|
|
Departure firstDeparture = null;
|
|
|
|
final List<Departure> departures = result.getDepartures();
|
|
|
|
if (mDeparturesAdapter.getCount() > 0) {
|
2011-05-23 18:59:34 +00:00
|
|
|
int adapterIndex = -1;
|
2011-06-05 18:13:21 +00:00
|
|
|
for (Departure departure : departures) {
|
2011-05-23 18:59:34 +00:00
|
|
|
adapterIndex++;
|
2011-06-05 18:13:21 +00:00
|
|
|
Departure existingDeparture = null;
|
|
|
|
if (adapterIndex < mDeparturesAdapter.getCount()) {
|
|
|
|
existingDeparture = mDeparturesAdapter
|
|
|
|
.getItem(adapterIndex);
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
while (existingDeparture != null
|
|
|
|
&& !departure.equals(existingDeparture)) {
|
|
|
|
mDeparturesAdapter.remove(existingDeparture);
|
|
|
|
if (adapterIndex < mDeparturesAdapter.getCount()) {
|
|
|
|
existingDeparture = mDeparturesAdapter
|
2011-05-23 18:59:34 +00:00
|
|
|
.getItem(adapterIndex);
|
|
|
|
} else {
|
2011-06-05 18:13:21 +00:00
|
|
|
existingDeparture = null;
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
if (existingDeparture != null) {
|
|
|
|
existingDeparture.mergeEstimate(departure);
|
2011-05-23 18:59:34 +00:00
|
|
|
} else {
|
2011-06-05 18:13:21 +00:00
|
|
|
mDeparturesAdapter.add(departure);
|
|
|
|
existingDeparture = departure;
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
if (firstDeparture == null) {
|
|
|
|
firstDeparture = existingDeparture;
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
if (existingDeparture.getUncertaintySeconds() > UNCERTAINTY_THRESHOLD) {
|
2011-05-27 21:06:58 +00:00
|
|
|
needsBetterAccuracy = true;
|
|
|
|
}
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-06-05 18:13:21 +00:00
|
|
|
for (Departure departure : departures) {
|
|
|
|
if (firstDeparture == null) {
|
|
|
|
firstDeparture = departure;
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
mDeparturesAdapter.add(departure);
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-05-27 21:06:58 +00:00
|
|
|
needsBetterAccuracy = true;
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
2011-06-05 18:13:21 +00:00
|
|
|
mDeparturesAdapter.notifyDataSetChanged();
|
2011-05-23 18:59:34 +00:00
|
|
|
|
2011-06-05 18:13:21 +00:00
|
|
|
if (hasWindowFocus() && firstDeparture != null) {
|
2011-11-05 18:51:20 +00:00
|
|
|
if (needsBetterAccuracy || firstDeparture.hasDeparted()) {
|
2011-05-23 18:59:34 +00:00
|
|
|
// Get more data in 20s
|
2011-06-17 21:35:20 +00:00
|
|
|
scheduleDataFetch(20000);
|
2011-05-23 18:59:34 +00:00
|
|
|
} else {
|
2011-06-05 18:13:21 +00:00
|
|
|
// Get more 90 seconds before next train arrives, right when
|
|
|
|
// next train arrives, or 3 minutes, whichever is sooner
|
2011-06-06 15:42:11 +00:00
|
|
|
final int intervalUntilNextDeparture = firstDeparture
|
|
|
|
.getMinSecondsLeft() * 1000;
|
2011-06-05 18:13:21 +00:00
|
|
|
final int alternativeInterval = 3 * 60 * 1000;
|
|
|
|
|
|
|
|
int interval = intervalUntilNextDeparture;
|
|
|
|
if (intervalUntilNextDeparture > 95000
|
|
|
|
&& intervalUntilNextDeparture < alternativeInterval) {
|
|
|
|
interval = interval - 90 * 1000;
|
|
|
|
} else if (intervalUntilNextDeparture > alternativeInterval) {
|
|
|
|
interval = alternativeInterval;
|
|
|
|
}
|
2011-06-17 21:35:20 +00:00
|
|
|
|
|
|
|
if (interval < 0) {
|
|
|
|
interval = 20000;
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduleDataFetch(interval);
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
if (!mIsAutoUpdating) {
|
|
|
|
mIsAutoUpdating = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mIsAutoUpdating = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-17 21:35:20 +00:00
|
|
|
private void scheduleDataFetch(int millisUntilExecute) {
|
|
|
|
if (!mDataFetchIsPending) {
|
|
|
|
mListTitleView.postDelayed(new Runnable() {
|
|
|
|
public void run() {
|
|
|
|
fetchLatestDepartures();
|
|
|
|
}
|
|
|
|
}, millisUntilExecute);
|
|
|
|
mDataFetchIsPending = true;
|
|
|
|
Log.i(Constants.TAG, "Scheduled another data fetch in "
|
|
|
|
+ millisUntilExecute / 1000 + "s");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-23 18:59:34 +00:00
|
|
|
private void runAutoUpdate() {
|
2011-06-05 18:13:21 +00:00
|
|
|
if (mIsAutoUpdating && mDeparturesAdapter != null) {
|
|
|
|
mDeparturesAdapter.notifyDataSetChanged();
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|
|
|
|
if (hasWindowFocus()) {
|
|
|
|
mListTitleView.postDelayed(AUTO_UPDATE_RUNNABLE, 1000);
|
|
|
|
} else {
|
|
|
|
mIsAutoUpdating = false;
|
|
|
|
}
|
|
|
|
}
|
2011-05-27 21:06:58 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(Menu menu) {
|
|
|
|
MenuInflater inflater = getMenuInflater();
|
|
|
|
inflater.inflate(R.menu.route_menu, menu);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
int itemId = item.getItemId();
|
|
|
|
if (itemId == R.id.view_on_bart_site_button) {
|
|
|
|
startActivity(new Intent(
|
|
|
|
Intent.ACTION_VIEW,
|
|
|
|
Uri.parse("http://m.bart.gov/schedules/qp_results.aspx?type=departure&date=today&time="
|
|
|
|
+ DateFormat.format("h:mmaa",
|
2011-11-05 18:51:20 +00:00
|
|
|
System.currentTimeMillis())
|
|
|
|
+ "&orig="
|
2011-05-27 21:06:58 +00:00
|
|
|
+ mOrigin.abbreviation
|
|
|
|
+ "&dest="
|
|
|
|
+ mDestination.abbreviation)));
|
|
|
|
return true;
|
2011-07-16 18:01:02 +00:00
|
|
|
} else if (itemId == R.id.view_system_map_button) {
|
2011-11-05 18:51:20 +00:00
|
|
|
startActivity(new Intent(this, ViewMapActivity.class));
|
2011-07-16 18:01:02 +00:00
|
|
|
return true;
|
2011-05-27 21:06:58 +00:00
|
|
|
} else {
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
}
|
2011-05-23 18:59:34 +00:00
|
|
|
}
|