Mostly split the interface to use the database
[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.ParseException;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10
11 /**
12 * Created by neil on 09/11/14.
13 */
14 public class WeatherContract {
15 // The "Content authority" is a name for the entire content provider, similar to the
16 // relationship between a domain name and its website. A convenient string to use for the
17 // content authority is the package name for the app, which is guaranteed to be unique on the
18 // device.
19 public static final String CONTENT_AUTHORITY = "uk.me.njae.sunshine";
20
21 // Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact
22 // the content provider.
23 public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
24
25
26 // Format used for storing dates in the database. ALso used for converting those strings
27 // back into date objects for comparison/processing.
28 public static final String DATE_FORMAT = "yyyyMMdd";
29
30 /**
31 * Converts Date class to a string representation, used for easy comparison and database lookup.
32 * @param date The input date
33 * @return a DB-friendly representation of the date, using the format defined in DATE_FORMAT.
34 */
35 public static String getDbDateString(Date date){
36 // Because the API returns a unix timestamp (measured in seconds),
37 // it must be converted to milliseconds in order to be converted to valid date.
38 SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
39 return sdf.format(date);
40 }
41
42 /**
43 * Converts a dateText to a long Unix time representation
44 * @param dateText the input date string
45 * @return the Date object
46 */
47 public static Date getDateFromDb(String dateText) {
48 SimpleDateFormat dbDateFormat = new SimpleDateFormat(DATE_FORMAT);
49 try {
50 return dbDateFormat.parse(dateText);
51 } catch ( ParseException e ) {
52 e.printStackTrace();
53 return null;
54 }
55 }
56
57
58 // Possible paths (appended to base content URI for possible URI's)
59 // For instance, content://com.example.android.sunshine.app/weather/ is a valid path for
60 // looking at weather data. content://com.example.android.sunshine.app/givemeroot/ will fail,
61 // as the ContentProvider hasn't been given any information on what to do with "givemeroot".
62 // At least, let's hope not. Don't be that dev, reader. Don't be that dev.
63 public static final String PATH_WEATHER = "weather";
64 public static final String PATH_LOCATION = "location";
65
66 /* Inner class that defines the table contents of the location table */
67 public static final class LocationEntry implements BaseColumns {
68
69 public static final Uri CONTENT_URI =
70 BASE_CONTENT_URI.buildUpon().appendPath(PATH_LOCATION).build();
71
72 public static final String CONTENT_TYPE =
73 "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;
74 public static final String CONTENT_ITEM_TYPE =
75 "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_LOCATION;
76
77 // Table name
78 public static final String TABLE_NAME = "location";
79
80 // The location setting string is what will be sent to openweathermap
81 // as the location query.
82 public static final String COLUMN_LOCATION_SETTING = "location_setting";
83
84 // Human readable location string, provided by the API. Because for styling,
85 // "Mountain View" is more recognizable than 94043.
86 public static final String COLUMN_CITY_NAME = "city_name";
87
88 // In order to uniquely pinpoint the location on the map when we launch the
89 // map intent, we store the latitude and longitude as returned by openweathermap.
90 public static final String COLUMN_COORD_LAT = "coord_lat";
91 public static final String COLUMN_COORD_LONG = "coord_long";
92
93 public static Uri buildLocationUri(long id) {
94 return ContentUris.withAppendedId(CONTENT_URI, id);
95 }
96 }
97
98 /* Inner class that defines the table contents of the weather table */
99 public static final class WeatherEntry implements BaseColumns {
100
101 public static final Uri CONTENT_URI =
102 BASE_CONTENT_URI.buildUpon().appendPath(PATH_WEATHER).build();
103
104 public static final String CONTENT_TYPE =
105 "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER;
106 public static final String CONTENT_ITEM_TYPE =
107 "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_WEATHER;
108
109
110 public static final String TABLE_NAME = "weather";
111
112 // Column with the foreign key into the location table.
113 public static final String COLUMN_LOC_KEY = "location_id";
114 // Date, stored as Text with format yyyy-MM-dd
115 public static final String COLUMN_DATETEXT = "date";
116 // Weather id as returned by API, to identify the icon to be used
117 public static final String COLUMN_WEATHER_ID = "weather_id";
118
119 // Short description and long description of the weather, as provided by API.
120 // e.g "clear" vs "sky is clear".
121 public static final String COLUMN_SHORT_DESC = "short_desc";
122
123 // Min and max temperatures for the day (stored as floats)
124 public static final String COLUMN_MIN_TEMP = "min";
125 public static final String COLUMN_MAX_TEMP = "max";
126
127 // Humidity is stored as a float representing percentage
128 public static final String COLUMN_HUMIDITY = "humidity";
129
130 // Humidity is stored as a float representing percentage
131 public static final String COLUMN_PRESSURE = "pressure";
132
133 // Windspeed is stored as a float representing windspeed mph
134 public static final String COLUMN_WIND_SPEED = "wind";
135
136 // Degrees are meteorological degrees (e.g, 0 is north, 180 is south). Stored as floats.
137 public static final String COLUMN_DEGREES = "degrees";
138
139 public static Uri buildWeatherUri(long id) {
140 return ContentUris.withAppendedId(CONTENT_URI, id);
141 }
142
143 public static Uri buildWeatherLocation(String locationSetting) {
144 return CONTENT_URI.buildUpon().appendPath(locationSetting).build();
145 }
146
147 public static Uri buildWeatherLocationWithStartDate(
148 String locationSetting, String startDate) {
149 return CONTENT_URI.buildUpon().appendPath(locationSetting)
150 .appendQueryParameter(COLUMN_DATETEXT, startDate).build();
151 }
152
153 public static Uri buildWeatherLocationWithDate(String locationSetting, String date) {
154 return CONTENT_URI.buildUpon().appendPath(locationSetting).appendPath(date).build();
155 }
156
157 public static String getLocationSettingFromUri(Uri uri) {
158 return uri.getPathSegments().get(1);
159 }
160
161 public static String getDateFromUri(Uri uri) {
162 return uri.getPathSegments().get(2);
163 }
164
165 public static String getStartDateFromUri(Uri uri) {
166 return uri.getQueryParameter(COLUMN_DATETEXT);
167 }
168 }
169 }