Пример #1
0
  /**
   * Returns a cursor based on the data in the attachments table, or null if the attachment is not
   * recorded in the table.
   *
   * <p>Supports REST Uri only, for a single row - selection, selection args, and sortOrder are
   * ignored (non-null values should probably throw an exception....)
   */
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    if (projection == null) {
      projection =
          new String[] {
            AttachmentProviderColumns._ID, AttachmentProviderColumns.DATA,
          };
    }

    List<String> segments = uri.getPathSegments();
    String accountId = segments.get(0);
    String id = segments.get(1);
    String format = segments.get(2);
    String name = null;
    int size = -1;
    String contentUri = null;

    uri = ContentUris.withAppendedId(Attachment.CONTENT_URI, Long.parseLong(id));
    Cursor c = getContext().getContentResolver().query(uri, PROJECTION_QUERY, null, null, null);
    try {
      if (c.moveToFirst()) {
        name = c.getString(0);
        size = c.getInt(1);
        contentUri = c.getString(2);
      } else {
        return null;
      }
    } finally {
      c.close();
    }

    MatrixCursor ret = new MatrixCursor(projection);
    Object[] values = new Object[projection.length];
    for (int i = 0, count = projection.length; i < count; i++) {
      String column = projection[i];
      if (AttachmentProviderColumns._ID.equals(column)) {
        values[i] = id;
      } else if (AttachmentProviderColumns.DATA.equals(column)) {
        values[i] = contentUri;
      } else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
        values[i] = name;
      } else if (AttachmentProviderColumns.SIZE.equals(column)) {
        values[i] = size;
      }
    }
    ret.addRow(values);
    return ret;
  }
Пример #2
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

    String[] columnNames = (projection == null) ? DEFAULT_PROJECTION : projection;

    List<String> segments = uri.getPathSegments();
    String dbName = segments.get(0);
    String id = segments.get(1);

    // Versions of K-9 before 3.400 had a database name here, not an
    // account UID, so implement a bit of backcompat
    if (dbName.endsWith(".db")) {
      dbName = dbName.substring(0, dbName.length() - 3);
    }

    final AttachmentInfo attachmentInfo;
    try {
      final Account account = Preferences.getPreferences(getContext()).getAccount(dbName);
      attachmentInfo = LocalStore.getInstance(account, getContext()).getAttachmentInfo(id);
    } catch (MessagingException e) {
      Log.e(K9.LOG_TAG, "Unable to retrieve attachment info from local store for ID: " + id, e);
      return null;
    }

    if (attachmentInfo == null) {
      if (K9.DEBUG) {
        Log.d(K9.LOG_TAG, "No attachment info for ID: " + id);
      }
      return null;
    }

    MatrixCursor ret = new MatrixCursor(columnNames);
    Object[] values = new Object[columnNames.length];
    for (int i = 0, count = columnNames.length; i < count; i++) {
      String column = columnNames[i];
      if (AttachmentProviderColumns._ID.equals(column)) {
        values[i] = id;
      } else if (AttachmentProviderColumns.DATA.equals(column)) {
        values[i] = uri.toString();
      } else if (AttachmentProviderColumns.DISPLAY_NAME.equals(column)) {
        values[i] = attachmentInfo.name;
      } else if (AttachmentProviderColumns.SIZE.equals(column)) {
        values[i] = attachmentInfo.size;
      }
    }
    ret.addRow(values);
    return ret;
  }