diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 3447d3a..ea5adc1 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -53,6 +53,18 @@ + + + + + + + + + @@ -62,6 +74,7 @@ + - - - - - - - - - - - diff --git a/res/layout/main.xml b/res/layout/main.xml index 4e07c8f..e42e494 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -15,4 +15,19 @@ android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 2ada87c..3b3ae05 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -30,4 +30,6 @@ View system map System map Departures + OK + Quick departure lookup diff --git a/res/values/styles.xml b/res/values/styles.xml index 886faa6..f6a8dcb 100644 --- a/res/values/styles.xml +++ b/res/values/styles.xml @@ -125,5 +125,9 @@ 87dp 5dp + + \ No newline at end of file diff --git a/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java b/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java new file mode 100644 index 0000000..7ac80a2 --- /dev/null +++ b/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java @@ -0,0 +1,78 @@ +package com.dougkeen.bart; + +import android.app.Activity; +import android.os.Bundle; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.Spinner; +import android.widget.SpinnerAdapter; +import android.widget.Toast; + +import com.dougkeen.bart.model.Station; + +public abstract class AbstractRouteSelectionActivity extends Activity { + + public AbstractRouteSelectionActivity() { + super(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.route_form); + + SpinnerAdapter originSpinnerAdapter = new ArrayAdapter(this, + R.layout.simple_spinner_item, Station.getStationList()); + ((Spinner) findViewById(R.id.origin_spinner)) + .setAdapter(originSpinnerAdapter); + + SpinnerAdapter destinationSpinnerAdapter = new ArrayAdapter( + this, R.layout.simple_spinner_item, Station.getStationList()); + ((Spinner) findViewById(R.id.destination_spinner)) + .setAdapter(destinationSpinnerAdapter); + + Button okButton = (Button) findViewById(R.id.okButton); + okButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + Station origin = (Station) ((Spinner) findViewById(R.id.origin_spinner)) + .getSelectedItem(); + Station destination = (Station) ((Spinner) findViewById(R.id.destination_spinner)) + .getSelectedItem(); + if (origin == null) { + Toast.makeText(v.getContext(), + com.dougkeen.bart.R.string.error_null_origin, + Toast.LENGTH_LONG); + return; + } + if (destination == null) { + Toast.makeText(v.getContext(), + com.dougkeen.bart.R.string.error_null_destination, + Toast.LENGTH_LONG); + return; + } + if (origin.equals(destination)) { + Toast.makeText( + v.getContext(), + com.dougkeen.bart.R.string.error_matching_origin_and_destination, + Toast.LENGTH_LONG); + return; + } + onOkButtonClick(origin, destination); + } + + }); + + Button cancelButton = (Button) findViewById(R.id.cancelButton); + cancelButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + setResult(RESULT_CANCELED); + finish(); + } + }); + + } + + abstract protected void onOkButtonClick(Station origin, Station destination); +} \ No newline at end of file diff --git a/src/com/dougkeen/bart/AddRouteActivity.java b/src/com/dougkeen/bart/AddRouteActivity.java index b2e70b2..49b7510 100644 --- a/src/com/dougkeen/bart/AddRouteActivity.java +++ b/src/com/dougkeen/bart/AddRouteActivity.java @@ -1,83 +1,25 @@ package com.dougkeen.bart; -import com.dougkeen.bart.data.RoutesColumns; -import com.dougkeen.bart.model.Constants; -import com.dougkeen.bart.model.Station; - -import android.app.Activity; import android.content.ContentValues; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.Button; import android.widget.CheckBox; -import android.widget.Spinner; -import android.widget.SpinnerAdapter; -import android.widget.Toast; -public class AddRouteActivity extends Activity { +import com.dougkeen.bart.data.RoutesColumns; +import com.dougkeen.bart.model.Constants; +import com.dougkeen.bart.model.Station; +public class AddRouteActivity extends AbstractRouteSelectionActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - - setContentView(R.layout.add_favorite); - - SpinnerAdapter originSpinnerAdapter = new ArrayAdapter(this, - R.layout.simple_spinner_item, Station.getStationList()); - ((Spinner) findViewById(R.id.origin_spinner)) - .setAdapter(originSpinnerAdapter); - - SpinnerAdapter destinationSpinnerAdapter = new ArrayAdapter( - this, R.layout.simple_spinner_item, Station.getStationList()); - ((Spinner) findViewById(R.id.destination_spinner)) - .setAdapter(destinationSpinnerAdapter); - - Button saveButton = (Button) findViewById(R.id.saveButton); - saveButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - onSaveButtonClick(); - } - - }); - - Button cancelButton = (Button) findViewById(R.id.cancelButton); - cancelButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View v) { - setResult(RESULT_CANCELED); - finish(); - } - }); - + findViewById(R.id.return_checkbox).setVisibility(View.VISIBLE); } - protected void onSaveButtonClick() { - Station origin = (Station) ((Spinner) findViewById(R.id.origin_spinner)) - .getSelectedItem(); - Station destination = (Station) ((Spinner) findViewById(R.id.destination_spinner)) - .getSelectedItem(); - - if (origin == null) { - Toast.makeText(this, com.dougkeen.bart.R.string.error_null_origin, - Toast.LENGTH_LONG); - return; - } - if (destination == null) { - Toast.makeText(this, - com.dougkeen.bart.R.string.error_null_destination, - Toast.LENGTH_LONG); - return; - } - if (origin.equals(destination)) { - Toast.makeText( - this, - com.dougkeen.bart.R.string.error_matching_origin_and_destination, - Toast.LENGTH_LONG); - return; - } - + @Override + protected void onOkButtonClick(Station origin, Station destination) { ContentValues values = new ContentValues(); values.put(RoutesColumns.FROM_STATION.string, origin.abbreviation); values.put(RoutesColumns.TO_STATION.string, destination.abbreviation); @@ -97,5 +39,4 @@ public class AddRouteActivity extends Activity { setResult(RESULT_OK, (new Intent()).setAction(newUri.toString())); finish(); } - } diff --git a/src/com/dougkeen/bart/QuickRouteActivity.java b/src/com/dougkeen/bart/QuickRouteActivity.java new file mode 100644 index 0000000..605194c --- /dev/null +++ b/src/com/dougkeen/bart/QuickRouteActivity.java @@ -0,0 +1,17 @@ +package com.dougkeen.bart; + +import android.content.Intent; + +import com.dougkeen.bart.model.Constants; +import com.dougkeen.bart.model.Station; + +public class QuickRouteActivity extends AbstractRouteSelectionActivity { + + @Override + protected void onOkButtonClick(Station origin, Station destination) { + startActivity(new Intent(Intent.ACTION_VIEW, + Constants.ARBITRARY_ROUTE_CONTENT_URI_ROOT.buildUpon() + .appendPath(origin.abbreviation) + .appendPath(destination.abbreviation).build())); + } +} diff --git a/src/com/dougkeen/bart/RoutesListActivity.java b/src/com/dougkeen/bart/RoutesListActivity.java index c720791..abc79c4 100644 --- a/src/com/dougkeen/bart/RoutesListActivity.java +++ b/src/com/dougkeen/bart/RoutesListActivity.java @@ -21,6 +21,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView.AdapterContextMenuInfo; +import android.widget.Button; import android.widget.ListView; import android.widget.SimpleCursorAdapter; import android.widget.SimpleCursorAdapter.ViewBinder; @@ -88,6 +89,15 @@ public class RoutesListActivity extends ActionBarListActivity { setListAdapter(adapter); registerForContextMenu(getListView()); + + ((Button) findViewById(R.id.quickLookupButton)) + .setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + startActivity(new Intent(Intent.ACTION_PICK, + Constants.ARBITRARY_ROUTE_CONTENT_URI_ROOT)); + } + }); } private void refreshFares() { diff --git a/src/com/dougkeen/bart/data/BartContentProvider.java b/src/com/dougkeen/bart/data/BartContentProvider.java index 9894a24..f929fc9 100644 --- a/src/com/dougkeen/bart/data/BartContentProvider.java +++ b/src/com/dougkeen/bart/data/BartContentProvider.java @@ -9,6 +9,8 @@ import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; +import android.database.MatrixCursor; +import android.database.MatrixCursor.RowBuilder; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteQueryBuilder; @@ -22,6 +24,8 @@ public class BartContentProvider extends ContentProvider { private static final int FAVORITES = 1; private static final int FAVORITE_ID = 2; + private static final int ARBITRARY_ROUTE = 3; + private static final int ARBITRARY_ROUTE_UNDEFINED = 4; /** * The default sort order for events @@ -33,6 +37,9 @@ public class BartContentProvider extends ContentProvider { sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); sUriMatcher.addURI(Constants.AUTHORITY, "favorites", FAVORITES); sUriMatcher.addURI(Constants.AUTHORITY, "favorites/#", FAVORITE_ID); + sUriMatcher.addURI(Constants.AUTHORITY, "route/*/*", ARBITRARY_ROUTE); + sUriMatcher.addURI(Constants.AUTHORITY, "route", + ARBITRARY_ROUTE_UNDEFINED); sFavoritesProjectionMap = new HashMap(); sFavoritesProjectionMap.put(RoutesColumns._ID.string, @@ -45,7 +52,8 @@ public class BartContentProvider extends ContentProvider { RoutesColumns.FARE.string); sFavoritesProjectionMap.put(RoutesColumns.FARE_LAST_UPDATED.string, RoutesColumns.FARE_LAST_UPDATED.string); - sFavoritesProjectionMap.put(RoutesColumns.AVERAGE_TRIP_SAMPLE_COUNT.string, + sFavoritesProjectionMap.put( + RoutesColumns.AVERAGE_TRIP_SAMPLE_COUNT.string, RoutesColumns.AVERAGE_TRIP_SAMPLE_COUNT.string); sFavoritesProjectionMap.put(RoutesColumns.AVERAGE_TRIP_LENGTH.string, RoutesColumns.AVERAGE_TRIP_LENGTH.string); @@ -66,6 +74,10 @@ public class BartContentProvider extends ContentProvider { return Constants.FAVORITE_CONTENT_TYPE; } else if (match == FAVORITE_ID) { return Constants.FAVORITE_CONTENT_ITEM_TYPE; + } else if (match == ARBITRARY_ROUTE) { + return Constants.ARBITRARY_ROUTE_TYPE; + } else if (match == ARBITRARY_ROUTE_UNDEFINED) { + return Constants.ARBITRARY_ROUTE_UNDEFINED_TYPE; } else { throw new IllegalArgumentException("Unknown URI " + uri); } @@ -82,14 +94,29 @@ public class BartContentProvider extends ContentProvider { int match = sUriMatcher.match(uri); - if (match == FAVORITES) { - qb.setTables(DatabaseHelper.FAVORITES_TABLE_NAME); - qb.setProjectionMap(sFavoritesProjectionMap); + if (match == ARBITRARY_ROUTE) { + MatrixCursor returnCursor = new MatrixCursor(projection); + RowBuilder newRow = returnCursor.newRow(); + + for (String column : projection) { + if (column.equals(RoutesColumns.FROM_STATION.string)) { + newRow.add(uri.getPathSegments().get(1)); + } else if (column.equals(RoutesColumns.TO_STATION.string)) { + newRow.add(uri.getPathSegments().get(2)); + } else { + newRow.add(null); + } + } + + return returnCursor; } else if (match == FAVORITE_ID) { qb.setTables(DatabaseHelper.FAVORITES_TABLE_NAME); qb.setProjectionMap(sFavoritesProjectionMap); qb.appendWhere(RoutesColumns._ID + " = " + uri.getPathSegments().get(1)); + } else if (match == FAVORITES) { + qb.setTables(DatabaseHelper.FAVORITES_TABLE_NAME); + qb.setProjectionMap(sFavoritesProjectionMap); } else { throw new IllegalArgumentException("Unknown URI " + uri); } diff --git a/src/com/dougkeen/bart/model/Constants.java b/src/com/dougkeen/bart/model/Constants.java index 6134a31..0a47659 100644 --- a/src/com/dougkeen/bart/model/Constants.java +++ b/src/com/dougkeen/bart/model/Constants.java @@ -5,9 +5,13 @@ import android.net.Uri; public class Constants { public static final String AUTHORITY = "com.dougkeen.bart.dataprovider"; public static final String FAVORITE_CONTENT_TYPE = "vnd.android.cursor.dir/com.dougkeen.bart.favorite"; + public static final String ARBITRARY_ROUTE_UNDEFINED_TYPE = "vnd.android.cursor.dir/com.dougkeen.bart.arbitraryroute"; + public static final String ARBITRARY_ROUTE_TYPE = "vnd.android.cursor.item/com.dougkeen.bart.arbitraryroute"; public static final String FAVORITE_CONTENT_ITEM_TYPE = "vnd.android.cursor.item/com.dougkeen.bart.favorite"; public static final Uri FAVORITE_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites"); + public static final Uri ARBITRARY_ROUTE_CONTENT_URI_ROOT = Uri + .parse("content://" + AUTHORITY + "/route"); public static final String MAP_URL = "http://m.bart.gov/images/global/system-map29.gif"; public static final String TAG = "BartRunner";