public void onLocationChanged(String newLocation) { Uri uri = mUri; if (null != uri) { long date = WeatherContract.WeatherEntry.getDateFromUri(uri); Uri updateUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(newLocation, date); mUri = updateUri; getLoaderManager().restartLoader(DETAIL_LOADER, null, this); } }
private Cursor getWeatherByLocationSettingAndDate( Uri uri, String[] projection, String sortOrder) { String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); long date = WeatherContract.WeatherEntry.getDateFromUri(uri); return sWeatherByLocationSettingQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, sLocationSettingAndDaySelection, new String[] {locationSetting, Long.toString(date)}, null, null, sortOrder); }
/* Student: Add the ability to insert Locations to the implementation of this function. */ @Override public Uri insert(Uri uri, ContentValues values) { final SQLiteDatabase db = mOpenHelper.getWritableDatabase(); final int match = sUriMatcher.match(uri); Uri returnUri; switch (match) { case WEATHER: { normalizeDate(values); long _id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = WeatherContract.WeatherEntry.buildWeatherUri(_id); else throw new android.database.SQLException("Failed to insert row into " + uri); break; } case LOCATION: { long _id = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, values); if (_id > 0) returnUri = WeatherContract.LocationEntry.buildLocationUri(_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 protected void onHandleIntent(Intent intent) { if (intent != null) { final String action = intent.getAction(); if (SunshineSyncAdapter.ACTION_DATA_UPDATED.equals(action)) { String location = Utility.getPreferredLocation(this); Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( location, System.currentTimeMillis()); Cursor cursor = getContentResolver() .query( weatherForLocationUri, FORECAST_COLUMNS, null, null, WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"); if (cursor.moveToFirst()) { int weatherId = cursor.getInt(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_WEATHER_ID)); String desc = cursor.getString( cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_SHORT_DESC)); double high = cursor.getDouble(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MAX_TEMP)); double low = cursor.getDouble(cursor.getColumnIndex(WeatherContract.WeatherEntry.COLUMN_MIN_TEMP)); handleWatchfaceUpdate(weatherId, desc, high, low); } cursor.close(); } } }
/** * This method uses existing code from the sunshine app to query for weather data that already * exists and is being displayed to the user's in the mobile app. * * @param context used for retrieving shared perferences * @return Cursor containing data for Today's weather */ private Cursor getWearableWeatherData(Context context) { // Sort order: Ascending, by date. String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; String locationSetting = Utility.getPreferredLocation(context); Uri locationUri = WeatherContract.WeatherEntry.buildWearableWeatherLocation( locationSetting, System.currentTimeMillis()); return getContentResolver().query(locationUri, FORECAST_COLUMNS, null, null, sortOrder); }
private Cursor getWeatherByLocationSetting(Uri uri, String[] projection, String sortOrder) { String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri); long startDate = WeatherContract.WeatherEntry.getStartDateFromUri(uri); String[] selectionArgs; String selection; if (startDate == 0) { selection = sLocationSettingSelection; selectionArgs = new String[] {locationSetting}; } else { selectionArgs = new String[] {locationSetting, Long.toString(startDate)}; selection = sLocationSettingWithStartDateSelection; } return sWeatherByLocationSettingQueryBuilder.query( mOpenHelper.getReadableDatabase(), projection, selection, selectionArgs, null, null, sortOrder); }
@Override public Loader<Cursor> onCreateLoader(int i, Bundle bundle) { // This is called when a new Loader needs to be created. This // fragment only uses one loader, so we don't care about checking the id. // To only show current and future dates, filter the query to return weather only for // dates after or including today. // Sort order: Ascending, by date. String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC"; String locationSetting = Utility.getPreferredLocation(getActivity()); Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate( locationSetting, System.currentTimeMillis()); return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }
@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { Log.v(LOG_TAG, "In onCreateLoader"); Intent intent = getActivity().getIntent(); if (intent == null || !intent.hasExtra(DATE_KEY)) { return null; } String forecastDate = intent.getStringExtra(DATE_KEY); // Sort order: Ascending, by date. String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATETEXT + " ASC"; mLocation = Utility.getPreferredLocation(getActivity()); Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithDate(mLocation, forecastDate); Log.v(LOG_TAG, weatherForLocationUri.toString()); // Now create and return a CursorLoader that will take care of // creating a Cursor for the data being displayed. return new CursorLoader( getActivity(), weatherForLocationUri, FORECAST_COLUMNS, null, null, sortOrder); }