6555465a0b59f7b8cd7c6f7b1013a6dd7bc8a498
[Sunshine.git] / app / src / main / java / uk / me / njae / sunshine / data / WeatherContract.java
1 package uk.me.njae.sunshine.data;
2
3 import android.content.ContentUris;
4 import android.net.Uri;
5 import android.provider.BaseColumns;
6
7 import java.text.SimpleDateFormat;
8 import java.util.Date;
9
10 /**
11 * Created by neil on 09/11/14.
12 */
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
17 // device.
18 public static final String CONTENT_AUTHORITY = "uk.me.njae.sunshine";
19
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);
23
24
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";
28
29 /**
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.
33 */
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);
39 }
40
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";
48
49 /* Inner class that defines the table contents of the location table */
50 public static final class LocationEntry implements BaseColumns {
51
52 public static final Uri CONTENT_URI =
53 BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build();
54
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;
59
60 // Table name
61 public static final String TABLE_NAME = "location";
62
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";
66
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";
70
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";
75
76 public static Uri buildLocationUri(long id) {
77 return ContentUris.withAppendedId(CONTENT_URI, id);
78 }
79 }
80
81 /* Inner class that defines the table contents of the weather table */
82 public static final class WeatherEntry implements BaseColumns {
83
84 public static final Uri CONTENT_URI =
85 BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build();
86
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;
91
92
93 public static final String TABLE_NAME = "weather";
94
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";
101
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";
105
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";
109
110 // Humidity is stored as a float representing percentage
111 public static final String COLUMN_HUMIDITY = "humidity";
112
113 // Humidity is stored as a float representing percentage
114 public static final String COLUMN_PRESSURE = "pressure";
115
116 // Windspeed is stored as a float representing windspeed mph
117 public static final String COLUMN_WIND_SPEED = "wind";
118
119 // Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats.
120 public static final String COLUMN_DEGREES = "degrees";
121
122 public static Uri buildWeatherUri(long id) {
123 return ContentUris.withAppendedId(CONTENT_URI, id);
124 }
125
126 public static Uri buildWeatherLocation(String locationSetting) {
127 return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
128 }
129
130 public static Uri buildWeatherLocationWithStartDate(
131 String locationSetting, String startDate) {
132 return CONTENT_URI.buildUpon().appendPath(locationSetting)
133 .appendQueryParameter(COLUMN_DATETEXT, startDate).build();
134 }
135
136 public static Uri buildWeatherLocationWithDate(String locationSetting, String date) {
137 return CONTENT_URI.buildUpon().appendPath(locationSetting).appendPath(date).build();
138 }
139
140 public static String getLocationSettingFromUri(Uri uri) {
141 return uri.getPathSegments().get(1);
142 }
143
144 public static String getDateFromUri(Uri uri) {
145 return uri.getPathSegments().get(2);
146 }
147
148 public static String getStartDateFromUri(Uri uri) {
149 return uri.getQueryParameter(COLUMN_DATETEXT);
150 }
151 }
152 }