Android Open Source - UK-Weather-repo Weather Content Provider






From Project

Back to project page UK-Weather-repo.

License

The source code is released under:

Apache License

If you think the Android project UK-Weather-repo listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.haringeymobile.ukweather.database;
/*from  w w w . j  a  v a2 s.  com*/
import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class WeatherContentProvider extends ContentProvider {

  private DatabaseHelper databaseHelper;

  // Used for the UriMacher
  private static final int CITIES_ALL_ROWS = 1;
  private static final int CITIES_SINGLE_ROW = 2;

  public static final String AUTHORITY = "com.haringeymobile.ukweather.provider";

  private static final String PATH_CITY_RECORDS = CityTable.TABLE_CITIES;

  public static final Uri CONTENT_URI_CITY_RECORDS = Uri.parse("content://"
      + AUTHORITY + "/" + PATH_CITY_RECORDS);

  private static final String PROVIDER_SPECIFIC_SUBTYPE_FOR_CITY_RECORDS = "/vnd."
      + AUTHORITY + "." + CityTable.TABLE_CITIES;

  public static final String CONTENT_TYPE_CITY_RECORDS = ContentResolver.CURSOR_DIR_BASE_TYPE
      + PROVIDER_SPECIFIC_SUBTYPE_FOR_CITY_RECORDS;
  public static final String CONTENT_ITEM_TYPE_CITY_RECORDS = ContentResolver.CURSOR_ITEM_BASE_TYPE
      + PROVIDER_SPECIFIC_SUBTYPE_FOR_CITY_RECORDS;

  private static final UriMatcher myURIMatcher = new UriMatcher(
      UriMatcher.NO_MATCH);

  static {
    myURIMatcher.addURI(AUTHORITY, PATH_CITY_RECORDS, CITIES_ALL_ROWS);
    myURIMatcher.addURI(AUTHORITY, PATH_CITY_RECORDS + "/#",
        CITIES_SINGLE_ROW);
  }

  @Override
  public boolean onCreate() {
    databaseHelper = new DatabaseHelper(getContext());
    return true;
  }

  @Override
  public Cursor query(Uri uri, String[] projection, String selection,
      String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db;
    try {
      db = databaseHelper.getWritableDatabase();
    } catch (SQLiteException ex) {
      db = databaseHelper.getReadableDatabase();
    }

    String groupBy = null;
    String having = null;
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

    switch (myURIMatcher.match(uri)) {
    case CITIES_SINGLE_ROW:
      queryBuilder.appendWhere(CityTable._ID + "="
          + uri.getLastPathSegment());
      // fall through
    case CITIES_ALL_ROWS:
      queryBuilder.setTables(CityTable.TABLE_CITIES);
      break;
    default:
      throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    Cursor cursor = queryBuilder.query(db, projection, selection,
        selectionArgs, groupBy, having, sortOrder);
    cursor.setNotificationUri(getContext().getContentResolver(), uri);
    return cursor;
  }

  @Override
  public String getType(Uri uri) {
    switch (myURIMatcher.match(uri)) {
    case CITIES_SINGLE_ROW:
      return CONTENT_ITEM_TYPE_CITY_RECORDS;
    case CITIES_ALL_ROWS:
      return CONTENT_TYPE_CITY_RECORDS;
    default:
      throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
  }

  @Override
  public Uri insert(Uri uri, ContentValues values) {
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    String nullColumnHack = null;
    long id;
    switch (myURIMatcher.match(uri)) {
    case CITIES_ALL_ROWS:
      id = db.insert(CityTable.TABLE_CITIES, nullColumnHack, values);
      break;
    default:
      throw new IllegalArgumentException("Unsupported URI: " + uri);
    }

    if (id > -1) {
      Uri insertedIdUri = ContentUris.withAppendedId(uri, id);
      getContext().getContentResolver().notifyChange(insertedIdUri, null);
      return insertedIdUri;
    }

    throw new SQLException("Could not insert into table for uri: " + uri);
  }

  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    int rowsDeleted = 0;
    switch (myURIMatcher.match(uri)) {
    case CITIES_SINGLE_ROW:
      String id_rec = uri.getLastPathSegment();
      if (TextUtils.isEmpty(selection)) {
        rowsDeleted = db.delete(CityTable.TABLE_CITIES, CityTable._ID
            + "=" + id_rec, null);
      } else {
        rowsDeleted = db.delete(CityTable.TABLE_CITIES, CityTable._ID
            + "=" + id_rec + " and " + selection, selectionArgs);
      }
      break;
    case CITIES_ALL_ROWS:
      rowsDeleted = db.delete(CityTable.TABLE_CITIES, selection,
          selectionArgs);
      break;
    default:
      throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsDeleted;
  }

  @Override
  public int update(Uri uri, ContentValues values, String selection,
      String[] selectionArgs) {
    SQLiteDatabase db = databaseHelper.getWritableDatabase();
    int rowsUpdated = 0;
    switch (myURIMatcher.match(uri)) {
    case CITIES_SINGLE_ROW:
      String id_rec = uri.getLastPathSegment();
      if (TextUtils.isEmpty(selection)) {
        rowsUpdated = db.update(CityTable.TABLE_CITIES, values,
            CityTable._ID + "=" + id_rec, null);
      } else {
        rowsUpdated = db.update(CityTable.TABLE_CITIES, values,
            CityTable._ID + "=" + id_rec + " and " + selection,
            selectionArgs);
      }
      break;
    case CITIES_ALL_ROWS:
      rowsUpdated = db.update(CityTable.TABLE_CITIES, values, selection,
          selectionArgs);
      break;
    default:
      throw new IllegalArgumentException("Unsupported URI: " + uri);
    }
    getContext().getContentResolver().notifyChange(uri, null);
    return rowsUpdated;
  }

  /**
   * A test package can call this to get a handle to the database underlying
   * WeatherContentProvider, so it can insert test data into the database. The
   * test case class is responsible for instantiating the provider in a test
   * context; {@link android.test.ProviderTestCase2} does this during the call
   * to setUp()
   * 
   * @return a handle to the database helper object for the provider's data.
   */
  public DatabaseHelper getOpenHelperForTest() {
    return databaseHelper;
  }

}




Java Source Code List

com.astuetz.PagerSlidingTabStrip.java
com.haringeymobile.ukweather.AboutActivity.java
com.haringeymobile.ukweather.BaseCityCursorAdapter.java
com.haringeymobile.ukweather.BaseCityListFragmentWithButtons.java
com.haringeymobile.ukweather.CityListFragmentWithUtilityButtons.java
com.haringeymobile.ukweather.CityListFragmentWithWeatherButtons.java
com.haringeymobile.ukweather.CityManagementActivity.java
com.haringeymobile.ukweather.CitySearchResultsDialog.java
com.haringeymobile.ukweather.CityUtilitiesCursorAdapter.java
com.haringeymobile.ukweather.CityWeatherCursorAdapter.java
com.haringeymobile.ukweather.DeleteCityDialog.java
com.haringeymobile.ukweather.GetAvailableCitiesTask.java
com.haringeymobile.ukweather.MainActivity.java
com.haringeymobile.ukweather.SettingsActivityPreHoneycomb.java
com.haringeymobile.ukweather.SettingsActivity.java
com.haringeymobile.ukweather.SettingsFragment.java
com.haringeymobile.ukweather.WeatherCurrentInfoFragment.java
com.haringeymobile.ukweather.WeatherDailyWeatherForecastChildFragment.java
com.haringeymobile.ukweather.WeatherForecastParentFragment.java
com.haringeymobile.ukweather.WeatherInfoActivity.java
com.haringeymobile.ukweather.WeatherInfoFragment.java
com.haringeymobile.ukweather.WeatherInfoType.java
com.haringeymobile.ukweather.WeatherThreeHourlyForecastChildFragment.java
com.haringeymobile.ukweather.WorkerFragmentToRetrieveJsonString.java
com.haringeymobile.ukweather.data.InitialCity.java
com.haringeymobile.ukweather.data.JsonFetcher.java
com.haringeymobile.ukweather.data.OpenWeatherMapUrl.java
com.haringeymobile.ukweather.data.objects.CityCurrentWeather.java
com.haringeymobile.ukweather.data.objects.CityDailyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.CityInfo.java
com.haringeymobile.ukweather.data.objects.CityThreeHourlyWeatherForecast.java
com.haringeymobile.ukweather.data.objects.Clouds.java
com.haringeymobile.ukweather.data.objects.Coordinates.java
com.haringeymobile.ukweather.data.objects.NumericParameters.java
com.haringeymobile.ukweather.data.objects.Rain.java
com.haringeymobile.ukweather.data.objects.SearchResponseForDailyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForFindQuery.java
com.haringeymobile.ukweather.data.objects.SearchResponseForThreeHourlyForecastQuery.java
com.haringeymobile.ukweather.data.objects.SystemParameters.java
com.haringeymobile.ukweather.data.objects.TemperatureScale.java
com.haringeymobile.ukweather.data.objects.Temperature.java
com.haringeymobile.ukweather.data.objects.WeatherInformation.java
com.haringeymobile.ukweather.data.objects.Weather.java
com.haringeymobile.ukweather.data.objects.WindSpeedMeasurementUnit.java
com.haringeymobile.ukweather.data.objects.Wind.java
com.haringeymobile.ukweather.database.CityTable.java
com.haringeymobile.ukweather.database.DatabaseHelper.java
com.haringeymobile.ukweather.database.GeneralDatabaseService.java
com.haringeymobile.ukweather.database.SqlOperation.java
com.haringeymobile.ukweather.database.WeatherContentProvider.java
com.haringeymobile.ukweather.utils.AsyncTaskWithProgressBar.java
com.haringeymobile.ukweather.utils.GlobalConstants.java
com.haringeymobile.ukweather.utils.MiscMethods.java
com.haringeymobile.ukweather.utils.SharedPrefsHelper.java