示例#1
0
  /** Deletes a row in the database */
  @Override
  public int delete(final Uri uri, final String where, final String[] whereArgs) {

    Helpers.validateSelection(where, sAppReadableColumnsSet);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();
    int count;
    int match = sURIMatcher.match(uri);
    switch (match) {
      case MY_DOWNLOADS:
      case MY_DOWNLOADS_ID:
      case ALL_DOWNLOADS:
      case ALL_DOWNLOADS_ID:
        SqlSelection selection = getWhereClause(uri, where, whereArgs, match);
        deleteRequestHeaders(db, selection.getSelection(), selection.getParameters());
        count = db.delete(DB_TABLE, selection.getSelection(), selection.getParameters());
        break;

      default:
        Log.d(Constants.TAG, "deleting unknown/invalid URI: " + uri);
        throw new UnsupportedOperationException("Cannot delete URI: " + uri);
    }
    notifyContentChanged(uri, match);
    return count;
  }
示例#2
0
  /** Updates a row in the database */
  @Override
  public int update(
      final Uri uri, final ContentValues values, final String where, final String[] whereArgs) {

    Helpers.validateSelection(where, sAppReadableColumnsSet);

    SQLiteDatabase db = mOpenHelper.getWritableDatabase();

    int count;
    boolean startService = false;

    if (values.containsKey(Downloads.COLUMN_DELETED)) {
      if (values.getAsInteger(Downloads.COLUMN_DELETED) == 1) {
        // some rows are to be 'deleted'. need to start DownloadService.
        startService = true;
      }
    }

    ContentValues filteredValues;
    if (Binder.getCallingPid() != Process.myPid()) {
      filteredValues = new ContentValues();
      copyString(Downloads.COLUMN_APP_DATA, values, filteredValues);
      copyInteger(Downloads.COLUMN_VISIBILITY, values, filteredValues);
      Integer i = values.getAsInteger(Downloads.COLUMN_CONTROL);
      if (i != null) {
        filteredValues.put(Downloads.COLUMN_CONTROL, i);
        startService = true;
      }

      copyInteger(Downloads.COLUMN_CONTROL, values, filteredValues);
      copyString(Downloads.COLUMN_TITLE, values, filteredValues);
      copyString(Downloads.COLUMN_DESCRIPTION, values, filteredValues);
      copyInteger(Downloads.COLUMN_DELETED, values, filteredValues);
    } else {
      filteredValues = values;
      String filename = values.getAsString(Downloads._DATA);
      if (filename != null) {
        Cursor c = query(uri, new String[] {Downloads.COLUMN_TITLE}, null, null, null);
        if (!c.moveToFirst() || c.getString(0).length() == 0) {
          values.put(Downloads.COLUMN_TITLE, new File(filename).getName());
        }
        c.close();
      }

      Integer status = values.getAsInteger(Downloads.COLUMN_STATUS);
      boolean isRestart = status != null && status == Downloads.STATUS_PENDING;
      boolean isUserBypassingSizeLimit =
          values.containsKey(Downloads.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT);
      if (isRestart || isUserBypassingSizeLimit) {
        startService = true;
      }
    }

    int match = sURIMatcher.match(uri);
    switch (match) {
      case MY_DOWNLOADS:
      case MY_DOWNLOADS_ID:
      case ALL_DOWNLOADS:
      case ALL_DOWNLOADS_ID:
        SqlSelection selection = getWhereClause(uri, where, whereArgs, match);
        if (filteredValues.size() > 0) {
          count =
              db.update(
                  DB_TABLE, filteredValues, selection.getSelection(), selection.getParameters());
        } else {
          count = 0;
        }
        break;

      default:
        Log.d(Constants.TAG, "updating unknown/invalid URI: " + uri);
        throw new UnsupportedOperationException("Cannot update URI: " + uri);
    }

    notifyContentChanged(uri, match);
    if (startService) {
      Context context = getContext();
      context.startService(new Intent(context, DownloadService.class));
    }
    return count;
  }
示例#3
0
  /** Starts a database query */
  @Override
  public Cursor query(
      final Uri uri,
      String[] projection,
      final String selection,
      final String[] selectionArgs,
      final String sort) {

    Helpers.validateSelection(selection, sAppReadableColumnsSet);

    SQLiteDatabase db = mOpenHelper.getReadableDatabase();

    int match = sURIMatcher.match(uri);
    if (match == -1) {
      if (Constants.LOGV) {
        Log.v(Constants.TAG, "querying unknown URI: " + uri);
      }
      throw new IllegalArgumentException("Unknown URI: " + uri);
    }

    if (match == REQUEST_HEADERS_URI) {
      if (projection != null || selection != null || sort != null) {
        throw new UnsupportedOperationException(
            "Request header queries do not support " + "projections, selections or sorting");
      }
      return queryRequestHeaders(db, uri);
    }

    SqlSelection fullSelection = getWhereClause(uri, selection, selectionArgs, match);

    if (Constants.LOGVV) {
      logVerboseQueryInfo(projection, selection, selectionArgs, sort, db);
    }

    Cursor ret =
        db.query(
            DB_TABLE,
            projection,
            fullSelection.getSelection(),
            fullSelection.getParameters(),
            null,
            null,
            sort);

    if (ret != null) {
      ret = new ReadOnlyCursorWrapper(ret);
    }

    if (ret != null) {
      ret.setNotificationUri(getContext().getContentResolver(), uri);
      if (Constants.LOGVV) {
        Log.v(Constants.TAG, "created cursor " + ret + " on behalf of " + Binder.getCallingPid());
      }
    } else {
      if (Constants.LOGV) {
        Log.v(Constants.TAG, "query failed in downloads database");
      }
    }

    return ret;
  }