X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fuk%2Fme%2Fnjae%2Fsunshine%2Fdata%2FWeatherContract.java;h=6afd5f3cdafffbd4902495f2eb3bc371c5783aa8;hb=HEAD;hp=99dd2d1b9168aa2d8397cd0cfb946ada82c21e36;hpb=b529b0b07fd94118aff6add8b80ae53947819982;p=Sunshine.git diff --git a/app/src/main/java/uk/me/njae/sunshine/data/WeatherContract.java b/app/src/main/java/uk/me/njae/sunshine/data/WeatherContract.java index 99dd2d1..6afd5f3 100644 --- a/app/src/main/java/uk/me/njae/sunshine/data/WeatherContract.java +++ b/app/src/main/java/uk/me/njae/sunshine/data/WeatherContract.java @@ -1,14 +1,79 @@ package uk.me.njae.sunshine.data; +import android.content.ContentUris; +import android.net.Uri; import android.provider.BaseColumns; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + /** * Created by neil on 09/11/14. */ public class WeatherContract { + // The "Content authority" is a name for the entire content provider, similar to the + // relationship between a domain name and its website. A convenient string to use for the + // content authority is the package name for the app, which is guaranteed to be unique on the + // device. + public static final String CONTENT_AUTHORITY = "uk.me.njae.sunshine"; + + // Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact + // the content provider. + public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY); + + + // Format used for storing dates in the database. ALso used for converting those strings + // back into date objects for comparison/processing. + public static final String DATE_FORMAT = "yyyyMMdd"; + + /** + * Converts Date class to a string representation, used for easy comparison and database lookup. + * @param date The input date + * @return a DB-friendly representation of the date, using the format defined in DATE_FORMAT. + */ + public static String getDbDateString(Date date){ + // Because the API returns a unix timestamp (measured in seconds), + // it must be converted to milliseconds in order to be converted to valid date. + SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT); + return sdf.format(date); + } + + /** + * Converts a dateText to a long Unix time representation + * @param dateText the input date string + * @return the Date object + */ + public static Date getDateFromDb(String dateText) { + SimpleDateFormat dbDateFormat = new SimpleDateFormat(DATE_FORMAT); + try { + return dbDateFormat.parse(dateText); + } catch ( ParseException e ) { + e.printStackTrace(); + return null; + } + } + + + // Possible paths (appended to base content URI for possible URI's) + // For instance, content://com.example.android.sunshine.app/weather/ is a valid path for + // looking at weather data. content://com.example.android.sunshine.app/givemeroot/ will fail, + // as the ContentProvider hasn't been given any information on what to do with "givemeroot". + // At least, let's hope not. Don't be that dev, reader. Don't be that dev. + public static final String PATH_WEATHER = "weather"; + public static final String PATH_LOCATION = "location"; + /* Inner class that defines the table contents of the location table */ public static final class LocationEntry implements BaseColumns { + public static final Uri CONTENT_URI = + BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build(); + + public static final String CONTENT_TYPE = + "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; + public static final String CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION; + // Table name public static final String TABLE_NAME = "location"; @@ -24,11 +89,24 @@ public class WeatherContract { // map intent, we store the latitude and longitude as returned by openweathermap. public static final String COLUMN_COORD_LAT = "coord_lat"; public static final String COLUMN_COORD_LONG = "coord_long"; + + public static Uri buildLocationUri(long id) { + return ContentUris.withAppendedId(CONTENT_URI, id); + } } /* Inner class that defines the table contents of the weather table */ public static final class WeatherEntry implements BaseColumns { + public static final Uri CONTENT_URI = + BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build(); + + public static final String CONTENT_TYPE = + "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; + public static final String CONTENT_ITEM_TYPE = + "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER; + + public static final String TABLE_NAME = "weather"; // Column with the foreign key into the location table. @@ -57,5 +135,35 @@ public class WeatherContract { // Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats. public static final String COLUMN_DEGREES = "degrees"; + + public static Uri buildWeatherUri(long id) { + return ContentUris.withAppendedId(CONTENT_URI, id); + } + + public static Uri buildWeatherLocation(String locationSetting) { + return CONTENT_URI.buildUpon().appendPath(locationSetting).build(); + } + + public static Uri buildWeatherLocationWithStartDate( + String locationSetting, String startDate) { + return CONTENT_URI.buildUpon().appendPath(locationSetting) + .appendQueryParameter(COLUMN_DATETEXT, startDate).build(); + } + + public static Uri buildWeatherLocationWithDate(String locationSetting, String date) { + return CONTENT_URI.buildUpon().appendPath(locationSetting).appendPath(date).build(); + } + + public static String getLocationSettingFromUri(Uri uri) { + return uri.getPathSegments().get(1); + } + + public static String getDateFromUri(Uri uri) { + return uri.getPathSegments().get(2); + } + + public static String getStartDateFromUri(Uri uri) { + return uri.getQueryParameter(COLUMN_DATETEXT); + } } }