1 package uk
.me
.njae
.sunshine
.data
;
3 import android
.content
.ContentUris
;
4 import android
.net
.Uri
;
5 import android
.provider
.BaseColumns
;
7 import java
.text
.SimpleDateFormat
;
11 * Created by neil on 09/11/14.
13 public class WeatherContract
{
14 // The "Content authority" is a name for the entire content provider, similar to the
15 // relationship between a domain name and its website. A convenient string to use for the
16 // content authority is the package name for the app, which is guaranteed to be unique on the
18 public static final String CONTENT_AUTHORITY
= "uk.me.njae.sunshine";
20 // Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact
21 // the content provider.
22 public static final Uri BASE_CONTENT_URI
= Uri
.parse("content://" + CONTENT_AUTHORITY
);
25 // Format used for storing dates in the database. ALso used for converting those strings
26 // back into date objects for comparison/processing.
27 public static final String DATE_FORMAT
= "yyyyMMdd";
30 * Converts Date class to a string representation, used for easy comparison and database lookup.
31 * @param date The input date
32 * @return a DB-friendly representation of the date, using the format defined in DATE_FORMAT.
34 public static String
getDbDateString(Date date
){
35 // Because the API returns a unix timestamp (measured in seconds),
36 // it must be converted to milliseconds in order to be converted to valid date.
37 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT
);
38 return sdf
.format(date
);
41 // Possible paths (appended to base content URI for possible URI's)
42 // For instance, content://com.example.android.sunshine.app/weather/ is a valid path for
43 // looking at weather data. content://com.example.android.sunshine.app/givemeroot/ will fail,
44 // as the ContentProvider hasn't been given any information on what to do with "givemeroot".
45 // At least, let's hope not. Don't be that dev, reader. Don't be that dev.
46 public static final String PATH_WEATHER
= "weather";
47 public static final String PATH_LOCATION
= "location";
49 /* Inner class that defines the table contents of the location table */
50 public static final class LocationEntry
implements BaseColumns
{
52 public static final Uri CONTENT_URI
=
53 BASE_CONTENT_URI
.buildUpon().appendPath(PATH_LOCATION
).build();
55 public static final String CONTENT_TYPE
=
56 "vnd.android.cursor.dir/" + CONTENT_AUTHORITY
+ "/" + PATH_LOCATION
;
57 public static final String CONTENT_ITEM_TYPE
=
58 "vnd.android.cursor.item/" + CONTENT_AUTHORITY
+ "/" + PATH_LOCATION
;
61 public static final String TABLE_NAME
= "location";
63 // The location setting string is what will be sent to openweathermap
64 // as the location query.
65 public static final String COLUMN_LOCATION_SETTING
= "location_setting";
67 // Human readable location string, provided by the API. Because for styling,
68 // "Mountain View" is more recognizable than 94043.
69 public static final String COLUMN_CITY_NAME
= "city_name";
71 // In order to uniquely pinpoint the location on the map when we launch the
72 // map intent, we store the latitude and longitude as returned by openweathermap.
73 public static final String COLUMN_COORD_LAT
= "coord_lat";
74 public static final String COLUMN_COORD_LONG
= "coord_long";
76 public static Uri
buildLocationUri(long id
) {
77 return ContentUris
.withAppendedId(CONTENT_URI
, id
);
81 /* Inner class that defines the table contents of the weather table */
82 public static final class WeatherEntry
implements BaseColumns
{
84 public static final Uri CONTENT_URI
=
85 BASE_CONTENT_URI
.buildUpon().appendPath(PATH_WEATHER
).build();
87 public static final String CONTENT_TYPE
=
88 "vnd.android.cursor.dir/" + CONTENT_AUTHORITY
+ "/" + PATH_WEATHER
;
89 public static final String CONTENT_ITEM_TYPE
=
90 "vnd.android.cursor.item/" + CONTENT_AUTHORITY
+ "/" + PATH_WEATHER
;
93 public static final String TABLE_NAME
= "weather";
95 // Column with the foreign key into the location table.
96 public static final String COLUMN_LOC_KEY
= "location_id";
97 // Date, stored as Text with format yyyy-MM-dd
98 public static final String COLUMN_DATETEXT
= "date";
99 // Weather id as returned by API, to identify the icon to be used
100 public static final String COLUMN_WEATHER_ID
= "weather_id";
102 // Short description and long description of the weather, as provided by API.
103 // e.g "clear" vs "sky is clear".
104 public static final String COLUMN_SHORT_DESC
= "short_desc";
106 // Min and max temperatures for the day (stored as floats)
107 public static final String COLUMN_MIN_TEMP
= "min";
108 public static final String COLUMN_MAX_TEMP
= "max";
110 // Humidity is stored as a float representing percentage
111 public static final String COLUMN_HUMIDITY
= "humidity";
113 // Humidity is stored as a float representing percentage
114 public static final String COLUMN_PRESSURE
= "pressure";
116 // Windspeed is stored as a float representing windspeed mph
117 public static final String COLUMN_WIND_SPEED
= "wind";
119 // Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats.
120 public static final String COLUMN_DEGREES
= "degrees";
122 public static Uri
buildWeatherUri(long id
) {
123 return ContentUris
.withAppendedId(CONTENT_URI
, id
);
126 public static Uri
buildWeatherLocation(String locationSetting
) {
127 return CONTENT_URI
.buildUpon().appendPath(locationSetting
).build();
130 public static Uri
buildWeatherLocationWithStartDate(
131 String locationSetting
, String startDate
) {
132 return CONTENT_URI
.buildUpon().appendPath(locationSetting
)
133 .appendQueryParameter(COLUMN_DATETEXT
, startDate
).build();
136 public static Uri
buildWeatherLocationWithDate(String locationSetting
, String date
) {
137 return CONTENT_URI
.buildUpon().appendPath(locationSetting
).appendPath(date
).build();
140 public static String
getLocationSettingFromUri(Uri uri
) {
141 return uri
.getPathSegments().get(1);
144 public static String
getDateFromUri(Uri uri
) {
145 return uri
.getPathSegments().get(2);
148 public static String
getStartDateFromUri(Uri uri
) {
149 return uri
.getQueryParameter(COLUMN_DATETEXT
);