Created
September 26, 2013 02:14
-
-
Save danpoltawski/6708932 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.shouldirun.android; | |
| import android.content.ContentProvider; | |
| import android.content.ContentValues; | |
| import android.content.Context; | |
| import android.content.UriMatcher; | |
| import android.database.Cursor; | |
| import android.database.sqlite.SQLiteDatabase; | |
| import android.net.Uri; | |
| import android.text.format.Time; | |
| import com.readystatesoftware.sqliteasset.SQLiteAssetHelper; | |
| /** | |
| * Provides access to a database of notes. Each note has a title, the note | |
| * itself, a creation date and a modified data. | |
| */ | |
| public class ShouldiRunProvider extends ContentProvider { | |
| // Used for debugging and logging | |
| private static final String TAG = "ShouldiRunProvider"; | |
| private DatabaseHelper mOpenHelper; | |
| private UriMatcher matcher; | |
| private static final String AUTHORITY = "com.shouldirun.android.timetabledata"; | |
| private static final String TRAINS_PATH = "trainslist"; | |
| private static final String STATIONSERVICES_PATH = "staionservices"; | |
| public static final Uri TRAINLIST_URI = Uri.parse("content://" + AUTHORITY + "/" + TRAINS_PATH); | |
| public static final Uri STATIONSERVICES_URI = Uri.parse("content://" + AUTHORITY + "/" + STATIONSERVICES_PATH); | |
| private static final int TRAINLIST = 1; | |
| private static final int STATIONSERVICES = 2; | |
| /** | |
| * | |
| * This class helps open, create, and upgrade the database file. Set to package visibility | |
| * for testing purposes. | |
| */ | |
| static class DatabaseHelper extends SQLiteAssetHelper { | |
| private static final String DATABASE_NAME = "perth"; | |
| private static final int DATABASE_VERSION = 2; | |
| public DatabaseHelper(Context context) { | |
| // calls the super constructor, requesting the default cursor factory. | |
| super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
| } | |
| } | |
| public ShouldiRunProvider() { | |
| matcher = new UriMatcher(UriMatcher.NO_MATCH); | |
| matcher.addURI(AUTHORITY, TRAINS_PATH, TRAINLIST); | |
| matcher.addURI(AUTHORITY, STATIONSERVICES_PATH, STATIONSERVICES); | |
| } | |
| /** | |
| * | |
| * Initializes the provider by creating a new DatabaseHelper. onCreate() is called | |
| * automatically when Android creates the provider in response to a resolver request from a | |
| * client. | |
| */ | |
| @Override | |
| public boolean onCreate() { | |
| // Creates a new helper object. Note that the database itself isn't opened until | |
| // something tries to access it, and it's only created if it doesn't already exist. | |
| mOpenHelper = new DatabaseHelper(getContext()); | |
| // Assumes that any failures will be reported by a thrown exception. | |
| return true; | |
| } | |
| /** | |
| * This is called when a client calls {@link android.content.ContentResolver#getType(Uri)}. | |
| * Returns the MIME data type of the URI given as a parameter. | |
| * | |
| * @param uri The URI whose MIME type is desired. | |
| * @return The MIME type of the URI. | |
| * @throws IllegalArgumentException if the incoming URI pattern is invalid. | |
| */ | |
| @Override | |
| public String getType(Uri uri) { | |
| switch (matcher.match(uri)) { | |
| case TRAINLIST: | |
| return "vnd.android.cursor.dir/"; | |
| default: | |
| throw new IllegalArgumentException("Unknown URI " + uri); | |
| } | |
| } | |
| /** | |
| * This method is called when a client calls | |
| * {@link android.content.ContentResolver#query(Uri, String[], String, String[], String)}. | |
| * Queries the database and returns a cursor containing the results. | |
| * | |
| * @return A cursor containing the results of the query. The cursor exists but is empty if | |
| * the query returns no results or an exception occurs. | |
| * @throws IllegalArgumentException if the incoming URI pattern is invalid. | |
| */ | |
| @Override | |
| public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, | |
| String sortOrder) { | |
| return null; | |
| // Opens the database object in "read" mode, since no writes need to be done. | |
| SQLiteDatabase db = mOpenHelper.getReadableDatabase(); | |
| Cursor c; | |
| switch (matcher.match(uri)) { | |
| case TRAINLIST: | |
| String query = "SELECT s.stop_id AS _id, stop_name FROM gtfs_stops s JOIN danp_computed_stops d " | |
| +"ON d.stop_id = s.stop_id WHERE "; | |
| c = db.rawQuery(query + selection, selectionArgs); | |
| break; | |
| case STATIONSERVICES: | |
| Time now = new Time(); | |
| now.setToNow(); | |
| String weekday = getWeekDayFromTime(now); | |
| String datestring = getDateSQLStringFromDate(now); | |
| int seconds = ((now.hour * 3600) + (now.minute * 60) + now.second); | |
| String stationID = selection; | |
| String calenddartext = "SELECT c.service_id FROM gtfs_calendar c " | |
| +"LEFT JOIN gtfs_calendar_dates d ON d.service_id = c.service_id AND d.date = ? " | |
| +"WHERE c.start_date <='"+datestring+"' AND c.end_date >='"+datestring+"' AND c."+weekday+" = 1 " | |
| +"AND d.service_id IS NULL " | |
| +"UNION SELECT service_id FROM gtfs_calendar_dates d WHERE d.date = '"+datestring+"' AND d.exception_type = 1"; | |
| // Only 'LIMIT BY TIME' if asked for | |
| String timeSQL = ""; | |
| if (seconds > 0) { | |
| timeSQL+= " AND s.departure_time_seconds >= "+seconds; | |
| } | |
| String lastStop = ""; | |
| if (!stationID.equals("99999")) { | |
| lastStop = "AND s.danp_last_stop IS NULL"; | |
| } | |
| query = "SELECT t.trip_id AS _id, r.route_short_name, s.departure_time, " | |
| +"s.departure_time_seconds, t.trip_headsign, r.route_long_name, s.stop_id " | |
| +"FROM gtfs_stop_times s " | |
| +"JOIN gtfs_trips t ON s.trip_id = t.trip_id " | |
| +"JOIN gtfs_routes r ON r.route_id = t.route_id " | |
| +"WHERE s.stop_id IN ("+stationID+") " | |
| + timeSQL+" "+lastStop+" " | |
| +"AND t.service_id IN ("+calenddartext+") ORDER BY s.departure_time LIMIT 1"; | |
| c = db.rawQuery(query, null); | |
| break; | |
| default: | |
| throw new IllegalArgumentException("Unknown URI " + uri); | |
| /*String query = "SELECT p.stop_id AS _id, p.stop_name FROM gtfs_stops p WHERE p.stop_id IN " + | |
| "(SELECT DISTINCT parent_station FROM gtfs_stops s JOIN danp_computed_stops d " | |
| +"ON d.stop_id = s.stop_id WHERE d.route_type = 2) " | |
| +"ORDER BY abs(stop_lat - ?)+ abs(stop_lon - ?)"; | |
| */ | |
| } | |
| // Tells the Cursor what URI to watch, so it knows when its source data changes | |
| c.setNotificationUri(getContext().getContentResolver(), uri); | |
| return c; | |
| } | |
| @Override | |
| public Uri insert(Uri uri, ContentValues initialValues) { | |
| throw new IllegalArgumentException("This content provider doesn't support insertion."); | |
| } | |
| @Override | |
| public int delete(Uri uri, String where, String[] whereArgs) { | |
| throw new IllegalArgumentException("This content provider doesn't support deletion."); | |
| } | |
| @Override | |
| public int update(Uri uri, ContentValues values, String where, String[] whereArgs) { | |
| throw new IllegalArgumentException("This content provider doesn't support updates."); | |
| } | |
| private String getWeekDayFromTime(Time time) { | |
| switch (time.weekDay) { | |
| case Time.MONDAY: | |
| return "monday"; | |
| case Time.TUESDAY: | |
| return "tuesday"; | |
| case Time.WEDNESDAY: | |
| return "wednesday"; | |
| case Time.THURSDAY: | |
| return "thursday"; | |
| case Time.FRIDAY: | |
| return "friday"; | |
| case Time.SATURDAY: | |
| return "saturday"; | |
| case Time.SUNDAY: | |
| return"sunday"; | |
| default: | |
| throw new IllegalArgumentException("Incorrect weekday!"); | |
| } | |
| } | |
| private String getDateSQLStringFromDate(Time date) { | |
| return date.format("%Y%m%d"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment