示例#1
0
  @Override
  public int bulkInsert(Uri uri, ContentValues[] values) {
    SQLiteDatabase db = weatherDbHelper.getWritableDatabase();

    final int match = uriMatcher.match(uri);

    switch (match) {
      case WEATHER:
        db.beginTransaction();
        int returnCount = 0;
        try {
          for (ContentValues value : values) {
            long id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, value);
            if (-1 != id) {
              ++returnCount;
            }
          }
          db.setTransactionSuccessful();
        } finally {
          db.endTransaction();
        }

        getContext().getContentResolver().notifyChange(uri, null);

        return returnCount;

      default:
        return super.bulkInsert(uri, values);
    }
  }
示例#2
0
  private Cursor getWeatherByLocationAndDay(Uri uri, String[] projection, String sortOrder) {
    String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri);
    String day = WeatherContract.WeatherEntry.getDateFromUri(uri);

    return weatherByLocationSettingQueryBuilder.query(
        weatherDbHelper.getReadableDatabase(),
        projection,
        locationSettingWithDaySelection,
        new String[] {locationSetting, day},
        null,
        null,
        sortOrder);
  }
示例#3
0
  @Override
  public int delete(Uri uri, String selection, String[] selectionArgs) {

    final String tableName = getTableName(uri);

    // do the actual deletion
    int affectedRows =
        weatherDbHelper.getWritableDatabase().delete(tableName, selection, selectionArgs);

    // notify any registered observers of this change
    if (selection == null || affectedRows > 0) {
      getContext().getContentResolver().notifyChange(uri, null);
    }

    return affectedRows;
  }
示例#4
0
  @Override
  public Uri insert(Uri uri, ContentValues contentValues) {

    final int match = uriMatcher.match(uri);
    Uri returnUri = null;
    SQLiteDatabase db = weatherDbHelper.getWritableDatabase();

    switch (match) {
      case WEATHER:
        {
          long id = db.insert(WeatherContract.WeatherEntry.TABLE_NAME, null, contentValues);
          if (id > 0) {
            returnUri = WeatherContract.WeatherEntry.buildWeatherUri(id);
          } else {
            throw new SQLException("Failed to insert row into " + uri);
          }
          break;
        }

      case LOCATION:
        {
          long id = db.insert(WeatherContract.LocationEntry.TABLE_NAME, null, contentValues);
          if (id > 0) {
            returnUri = WeatherContract.LocationEntry.buildLocationUri(id);
          } else {
            throw new SQLException("Failed to insert row into " + uri);
          }
          break;
        }

      default:
        throw new UnsupportedOperationException("Unknown uri " + uri);
    }

    // notify any registered observers of this change
    getContext().getContentResolver().notifyChange(uri, null);

    return returnUri;
  }
示例#5
0
  private Cursor getWeatherByLocationSetting(Uri uri, String[] projection, String sortOrder) {
    String locationSetting = WeatherContract.WeatherEntry.getLocationSettingFromUri(uri);
    String startDate = WeatherContract.WeatherEntry.getStartDateFromUri(uri);

    String[] selectionArgs;
    String selection;

    if (startDate == null) {
      selection = locationSettingSelection;
      selectionArgs = new String[] {locationSetting};
    } else {
      selection = locationSettingWithStartDateSelection;
      selectionArgs = new String[] {locationSetting, startDate};
    }

    return weatherByLocationSettingQueryBuilder.query(
        weatherDbHelper.getReadableDatabase(),
        projection,
        selection,
        selectionArgs,
        null,
        null,
        sortOrder);
  }
示例#6
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    // Here's the switch statement that, given a URI, will determine what kind of request it is,
    // and query the database accordingly.
    Cursor retCursor;
    switch (uriMatcher.match(uri)) {
        // "weather/*/*"
      case WEATHER_WITH_LOCATION_AND_DATE:
        {
          retCursor = getWeatherByLocationAndDay(uri, projection, sortOrder);
          break;
        }
        // "weather/*"
      case WEATHER_WITH_LOCATION:
        {
          retCursor = getWeatherByLocationSetting(uri, projection, sortOrder);
          break;
        }
        // "weather"
      case WEATHER:
        {
          retCursor =
              weatherDbHelper
                  .getReadableDatabase()
                  .query(
                      WeatherContract.WeatherEntry.TABLE_NAME,
                      projection,
                      selection,
                      selectionArgs,
                      null,
                      null,
                      sortOrder);
          break;
        }
        // "location/*"
      case LOCATION_ID:
        {
          long locationId = ContentUris.parseId(uri);

          String modifiedSelection = WeatherContract.LocationEntry._ID + " = ?";

          retCursor =
              weatherDbHelper
                  .getReadableDatabase()
                  .query(
                      WeatherContract.LocationEntry.TABLE_NAME,
                      projection,
                      modifiedSelection,
                      new String[] {Long.toString(locationId)},
                      null,
                      null,
                      sortOrder);
          break;
        }
        // "location"
      case LOCATION:
        {
          retCursor =
              weatherDbHelper
                  .getReadableDatabase()
                  .query(
                      WeatherContract.LocationEntry.TABLE_NAME,
                      projection,
                      selection,
                      selectionArgs,
                      null,
                      null,
                      sortOrder);
          break;
        }

      default:
        throw new UnsupportedOperationException("Unknown uri: " + uri);
    }

    retCursor.setNotificationUri(getContext().getContentResolver(), uri);

    return retCursor;
  }