-
-
Save cahyowhy/f83d39751e8ea7cdc7822607716e4aa5 to your computer and use it in GitHub Desktop.
WeatherProvider
This file contains 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.cahyo.wibowo.jalretrovit.db; | |
import android.annotation.TargetApi; | |
import android.content.ContentProvider; | |
import android.content.ContentValues; | |
import android.content.UriMatcher; | |
import android.database.Cursor; | |
import android.database.sqlite.SQLiteDatabase; | |
import android.database.sqlite.SQLiteQueryBuilder; | |
import android.net.Uri; | |
import android.support.annotation.Nullable; | |
/** | |
* Created by cahyo on 17/08/16. | |
*/ | |
public class WeatherProvider extends ContentProvider { | |
// The URI Matcher used by this content provider. | |
private static final UriMatcher sUriMatcher = buildUriMatcher(); | |
private DbHelper mOpenHelper; | |
static final int DAILY_WEATHER = 100; | |
static final int DAILY_WEATHER_WITH_LOCATION = 101; | |
static final int DAILY_WEATHER_WITH_LOCATION_AND_DATE = 102; | |
static final int CURRENTLY_WEATHER = 103; | |
static final int CURRENTLY_WEATHER_WITHLOCATION = 104; | |
static final int HOURLY_WEATHER = 105; | |
static final int HOURLY_WEATHER_WITHLOCATION = 106; | |
static final int LOCATION = 300; | |
private static final SQLiteQueryBuilder sWeatherHourlyByGPSBuilder; | |
static{ | |
sWeatherHourlyByGPSBuilder = new SQLiteQueryBuilder(); | |
//This is an inner join which looks like | |
//weather INNER JOIN location ON weather.location_id(foreignkey) = location._id | |
sWeatherHourlyByGPSBuilder.setTables( | |
WeatherContract.WeatherHourly_entry.TABLE_NAME + " INNER JOIN " + | |
WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
" ON " + WeatherContract.WeatherHourly_entry.TABLE_NAME + | |
"." + WeatherContract.WeatherHourly_entry.COLUMN_foreign + | |
" = " + WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
"." + WeatherContract.WeatherLocation_entry._ID); | |
} | |
private static final SQLiteQueryBuilder sWeatherDailyByGPSBuilder; | |
static{ | |
sWeatherDailyByGPSBuilder = new SQLiteQueryBuilder(); | |
//This is an inner join which looks like | |
//weather INNER JOIN location ON weather.location_id(foreignkey) = location._id | |
sWeatherDailyByGPSBuilder.setTables( | |
WeatherContract.Weatherdaily_entry.TABLE_NAME + " INNER JOIN " + | |
WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
" ON " + WeatherContract.Weatherdaily_entry.TABLE_NAME + | |
"." + WeatherContract.Weatherdaily_entry.COLUMN_foreign + | |
" = " + WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
"." + WeatherContract.WeatherLocation_entry._ID); | |
} | |
private static final SQLiteQueryBuilder sWeatherCurrentlyByGPSBuilder; | |
static{ | |
sWeatherCurrentlyByGPSBuilder = new SQLiteQueryBuilder(); | |
//This is an inner join which looks like | |
//weather INNER JOIN location ON weather.location_id(foreignkey) = location._id | |
sWeatherCurrentlyByGPSBuilder.setTables( | |
WeatherContract.Weathercurrently_entry.TABLE_NAME + " INNER JOIN " + | |
WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
" ON " + WeatherContract.Weathercurrently_entry.TABLE_NAME + | |
"." + WeatherContract.Weathercurrently_entry.COLUMN_foreign + | |
" = " + WeatherContract.WeatherLocation_entry.TABLE_NAME + | |
"." + WeatherContract.WeatherLocation_entry._ID); | |
} | |
//weather_location.latlon = ? | |
private static final String sLocationGpsSelection = | |
WeatherContract.WeatherLocation_entry.TABLE_NAME+ "." + WeatherContract.WeatherLocation_entry.COLUMN_LAT_LON + | |
" = ? "; | |
//weather_location.latlon = ? and time = ? | |
private static final String sLocationGpsDayDateSelection = | |
WeatherContract.WeatherLocation_entry.TABLE_NAME+ "." + WeatherContract.WeatherLocation_entry.COLUMN_LAT_LON + | |
" = ? AND "+ WeatherContract.Weatherdaily_entry.COLUMN_TIME + " = ? "; | |
private Cursor getHourlyWeatherByGPS(Uri uri, String[] projection, String sortOrder) { | |
String locationSetting = WeatherContract.WeatherHourly_entry.getGPSLocationFromUri(uri); | |
String[] selectionArgs; | |
String selection; | |
selection = sLocationGpsSelection; | |
selectionArgs = new String[]{locationSetting}; | |
return sWeatherHourlyByGPSBuilder.query(mOpenHelper.getReadableDatabase(), | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder); | |
} | |
private Cursor getCurrentlyWeatherByGPS(Uri uri, String[] projection, String sortOrder) { | |
String locationSetting = WeatherContract.Weathercurrently_entry.getGPSLocationFromUri(uri); | |
String[] selectionArgs; | |
String selection; | |
selection = sLocationGpsSelection; | |
selectionArgs = new String[]{locationSetting}; | |
return sWeatherCurrentlyByGPSBuilder.query(mOpenHelper.getReadableDatabase(), | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder); | |
} | |
private Cursor getDailyWeatherByGPS(Uri uri, String[] projection, String sortOrder) { | |
String locationSetting = WeatherContract.Weatherdaily_entry.getGPSLocationFromUri(uri); | |
String[] selectionArgs; | |
String selection; | |
selection = sLocationGpsSelection; | |
selectionArgs = new String[]{locationSetting}; | |
return sWeatherDailyByGPSBuilder.query(mOpenHelper.getReadableDatabase(), | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder); | |
} | |
private Cursor getDailyWeatherByGPSwithDate(Uri uri, String[] projection, String sortOrder) { | |
String locationSetting = WeatherContract.Weatherdaily_entry.getGPSLocationFromUri(uri); | |
long date = WeatherContract.Weatherdaily_entry.getDateFromUri(uri); | |
return sWeatherDailyByGPSBuilder.query(mOpenHelper.getReadableDatabase(), | |
projection, | |
sLocationGpsDayDateSelection, | |
new String[]{locationSetting, Long.toString(date)}, | |
null, | |
null, | |
sortOrder | |
); | |
} | |
static UriMatcher buildUriMatcher() { | |
// I know what you're thinking. Why create a UriMatcher when you can use regular | |
// expressions instead? Because you're not crazy, that's why. | |
// All paths added to the UriMatcher have a corresponding code to return when a match is | |
// found. The code passed into the constructor represents the code to return for the root | |
// URI. It's common to use NO_MATCH as the code for this case. | |
final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); | |
final String authority = WeatherContract.CONTENT_AUTHORITY; | |
// For each type of URI you want to add, create a corresponding code. | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_DAILY, DAILY_WEATHER); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_DAILY + "/*", DAILY_WEATHER_WITH_LOCATION); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_DAILY + "/*/#", DAILY_WEATHER_WITH_LOCATION_AND_DATE); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_NOW, CURRENTLY_WEATHER); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_NOW + "/*", CURRENTLY_WEATHER_WITHLOCATION); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_HOURLY, HOURLY_WEATHER); | |
matcher.addURI(authority, WeatherContract.PATH_WEATHER_HOURLY + "/*", HOURLY_WEATHER_WITHLOCATION); | |
matcher.addURI(authority, WeatherContract.PATH_LOCATION, LOCATION); | |
return matcher; | |
} | |
@Override | |
public boolean onCreate() { | |
mOpenHelper = new DbHelper(getContext()); | |
return true; | |
} | |
@Nullable | |
@Override | |
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { | |
Cursor retCursor; | |
switch (sUriMatcher.match(uri)) { | |
// "hourly_weather/*" | |
case HOURLY_WEATHER_WITHLOCATION: { | |
retCursor = getHourlyWeatherByGPS(uri, projection, sortOrder); | |
break; | |
} | |
//"hourly_weather" | |
case HOURLY_WEATHER: { | |
retCursor = mOpenHelper.getReadableDatabase().query( | |
WeatherContract.WeatherHourly_entry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
// "currently_weather/*" | |
case CURRENTLY_WEATHER_WITHLOCATION: { | |
retCursor = getCurrentlyWeatherByGPS(uri, projection, sortOrder); | |
break; | |
} | |
//"currently_weather" | |
case CURRENTLY_WEATHER: { | |
retCursor = mOpenHelper.getReadableDatabase().query( | |
WeatherContract.Weathercurrently_entry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
// "daily_weather/*/*" | |
case DAILY_WEATHER_WITH_LOCATION_AND_DATE: | |
{ | |
retCursor = getDailyWeatherByGPSwithDate(uri, projection, sortOrder); | |
break; | |
} | |
// "daily_weather/*" | |
case DAILY_WEATHER_WITH_LOCATION: { | |
retCursor = getDailyWeatherByGPS(uri, projection, sortOrder); | |
break; | |
} | |
// "daily_weather" | |
case DAILY_WEATHER: { | |
retCursor = mOpenHelper.getReadableDatabase().query( | |
WeatherContract.Weatherdaily_entry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
// "location" | |
case LOCATION: { | |
retCursor = mOpenHelper.getReadableDatabase().query( | |
WeatherContract.WeatherLocation_entry.TABLE_NAME, | |
projection, | |
selection, | |
selectionArgs, | |
null, | |
null, | |
sortOrder | |
); | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
retCursor.setNotificationUri(getContext().getContentResolver(), uri); | |
return retCursor; | |
} | |
@Nullable | |
@Override | |
public String getType(Uri uri) { | |
// Use the Uri Matcher to determine what kind of URI this is. | |
final int match = sUriMatcher.match(uri); | |
switch (match) { | |
// Student: Uncomment and fill out these two cases | |
case HOURLY_WEATHER_WITHLOCATION: | |
return WeatherContract.WeatherHourly_entry.CONTENT_TYPE; | |
case HOURLY_WEATHER: | |
return WeatherContract.WeatherHourly_entry.CONTENT_TYPE; | |
case CURRENTLY_WEATHER_WITHLOCATION: | |
return WeatherContract.Weathercurrently_entry.CONTENT_TYPE; | |
case CURRENTLY_WEATHER: | |
return WeatherContract.Weathercurrently_entry.CONTENT_TYPE; | |
case DAILY_WEATHER_WITH_LOCATION_AND_DATE: | |
return WeatherContract.Weatherdaily_entry.CONTENT_ITEM_TYPE; | |
case DAILY_WEATHER_WITH_LOCATION: | |
return WeatherContract.Weatherdaily_entry.CONTENT_TYPE; | |
case DAILY_WEATHER: | |
return WeatherContract.Weatherdaily_entry.CONTENT_TYPE; | |
case LOCATION: | |
return WeatherContract.Weatherdaily_entry.CONTENT_TYPE; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
} | |
private void normalizeDate(ContentValues values) { | |
// normalize the date value | |
if (values.containsKey(WeatherContract.Weatherdaily_entry.COLUMN_TIME)) { | |
long dateValue = values.getAsLong(WeatherContract.Weatherdaily_entry.COLUMN_TIME); | |
values.put(WeatherContract.Weatherdaily_entry.COLUMN_TIME, WeatherContract.normalizeDate(dateValue)); | |
} | |
if (values.containsKey(WeatherContract.Weathercurrently_entry.COLUMN_TIME)) { | |
long dateValue = values.getAsLong(WeatherContract.Weathercurrently_entry.COLUMN_TIME); | |
values.put(WeatherContract.Weathercurrently_entry.COLUMN_TIME, WeatherContract.normalizeDate(dateValue)); | |
} | |
if (values.containsKey(WeatherContract.WeatherHourly_entry.COLUMN_TIME)) { | |
long dateValue = values.getAsLong(WeatherContract.WeatherHourly_entry.COLUMN_TIME); | |
values.put(WeatherContract.WeatherHourly_entry.COLUMN_TIME, WeatherContract.normalizeDate(dateValue)); | |
} | |
} | |
@Nullable | |
@Override | |
public Uri insert(Uri uri, ContentValues values) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
Uri returnUri; | |
switch (match) { | |
case HOURLY_WEATHER: { | |
normalizeDate(values); | |
long _id = db.insert(WeatherContract.WeatherHourly_entry.TABLE_NAME, null, values); | |
if ( _id > 0 ) | |
returnUri = WeatherContract.WeatherHourly_entry.buildWeatherHourlyUri(_id); | |
else | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case CURRENTLY_WEATHER: { | |
normalizeDate(values); | |
long _id = db.insert(WeatherContract.Weathercurrently_entry.TABLE_NAME, null, values); | |
if ( _id > 0 ) | |
returnUri = WeatherContract.Weathercurrently_entry.buildWeatherCurrentlyUri(_id); | |
else | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case DAILY_WEATHER: { | |
normalizeDate(values); | |
long _id = db.insert(WeatherContract.Weatherdaily_entry.TABLE_NAME, null, values); | |
if ( _id > 0 ) | |
returnUri = WeatherContract.Weatherdaily_entry.buildWeatherDailyUri(_id); | |
else | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
case LOCATION: { | |
long _id = db.insert(WeatherContract.WeatherLocation_entry.TABLE_NAME, null, values); | |
if ( _id > 0 ) | |
returnUri = WeatherContract.WeatherLocation_entry.buildWeatherLocationUri(_id); | |
else | |
throw new android.database.SQLException("Failed to insert row into " + uri); | |
break; | |
} | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return returnUri; | |
} | |
@Override | |
public int delete(Uri uri, String selection, String[] selectionArgs) { | |
return 0; | |
} | |
@Override | |
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
int rowsUpdated; | |
switch (match) { | |
case HOURLY_WEATHER: | |
normalizeDate(values); | |
rowsUpdated = db.update(WeatherContract.WeatherHourly_entry.TABLE_NAME, values, selection, | |
selectionArgs); | |
break; | |
case CURRENTLY_WEATHER: | |
normalizeDate(values); | |
rowsUpdated = db.update(WeatherContract.Weathercurrently_entry.TABLE_NAME, values, selection, | |
selectionArgs); | |
break; | |
case DAILY_WEATHER: | |
normalizeDate(values); | |
rowsUpdated = db.update(WeatherContract.Weatherdaily_entry.TABLE_NAME, values, selection, | |
selectionArgs); | |
break; | |
case LOCATION: | |
rowsUpdated = db.update(WeatherContract.WeatherLocation_entry.TABLE_NAME, values, selection, | |
selectionArgs); | |
break; | |
default: | |
throw new UnsupportedOperationException("Unknown uri: " + uri); | |
} | |
if (rowsUpdated != 0) { | |
getContext().getContentResolver().notifyChange(uri, null); | |
} | |
return rowsUpdated; | |
} | |
@Override | |
public int bulkInsert(Uri uri, ContentValues[] values) { | |
final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); | |
final int match = sUriMatcher.match(uri); | |
switch (match) { | |
case HOURLY_WEATHER: | |
db.beginTransaction(); | |
int returnCount2 = 0; | |
try { | |
for (ContentValues value : values) { | |
normalizeDate(value); | |
long _id = db.insert(WeatherContract.WeatherHourly_entry.TABLE_NAME, null, value); | |
if (_id != -1) { | |
returnCount2++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return returnCount2; | |
case DAILY_WEATHER: | |
db.beginTransaction(); | |
int returnCount = 0; | |
try { | |
for (ContentValues value : values) { | |
normalizeDate(value); | |
long _id = db.insert(WeatherContract.Weatherdaily_entry.TABLE_NAME, null, value); | |
if (_id != -1) { | |
returnCount++; | |
} | |
} | |
db.setTransactionSuccessful(); | |
} finally { | |
db.endTransaction(); | |
} | |
getContext().getContentResolver().notifyChange(uri, null); | |
return returnCount; | |
default: | |
return super.bulkInsert(uri, values); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment