Пример #1
0
  /**
   * Returns an ArrayList containing all the Addresses stored in the application's database
   *
   * @return An ArrayList containing one Address object for each record in the Addresses table.
   */
  public ArrayList<Address> getAllAddresses() {
    ArrayList<Address> addresses = new ArrayList<Address>();

    // Specify which columns from the table we are interested in
    String[] projection = {
      AddressesTable.COLUMN_ID,
      AddressesTable.COLUMN_CORRESPONDING_PUBKEY_ID,
      AddressesTable.COLUMN_LABEL,
      AddressesTable.COLUMN_ADDRESS,
      AddressesTable.COLUMN_PRIVATE_SIGNING_KEY,
      AddressesTable.COLUMN_PRIVATE_ENCRYPTION_KEY,
      AddressesTable.COLUMN_RIPE_HASH,
      AddressesTable.COLUMN_TAG
    };

    // Query the database via the ContentProvider
    Cursor cursor =
        mContentResolver.query(
            DatabaseContentProvider.CONTENT_URI_ADDRESSES, projection, null, null, null);

    if (cursor.moveToFirst()) {
      do {
        long id = cursor.getLong(0);
        long correspondingPubkeyId = cursor.getLong(1);
        String label = cursor.getString(2);
        String address = cursor.getString(3);
        String privateSigningKey = cursor.getString(4);
        String privateEncryptionKey = cursor.getString(5);
        byte[] ripeHash = Base64.decode(cursor.getString(6), Base64.DEFAULT);
        byte[] tag = Base64.decode(cursor.getString(7), Base64.DEFAULT);

        Address a = new Address();
        a.setId(id);
        a.setCorrespondingPubkeyId(correspondingPubkeyId);
        a.setLabel(label);
        a.setAddress(address);
        a.setPrivateSigningKey(privateSigningKey);
        a.setPrivateEncryptionKey(privateEncryptionKey);
        a.setRipeHash(ripeHash);
        a.setTag(tag);

        addresses.add(a);
      } while (cursor.moveToNext());
    }

    cursor.close();
    return addresses;
  }
Пример #2
0
  /**
   * Finds all Addresses in the application's database that match the given field
   *
   * @param columnName - A String specifying the name of the column in the database that should be
   *     used to find matching records. See the AddressesTable class to find the relevant column
   *     name.
   * @param searchString - A String specifying the value to search for. There are 4 use cases for
   *     this:<br>
   *     1) The value to search for is a String (e.g. A label from the UI). In this case the value
   *     can be passed in directly.<br>
   *     2) The value to search for is an int or long. In this case you should use String.valueOf(x)
   *     and pass in the resulting String.<br>
   *     3) The value to search for is a boolean. In this case you should pass in the String "0" for
   *     false or the String "1" for true. <br>
   *     4) The value to search for is a byte[]. In this case you should encode the byte[] into a
   *     Base64 encoded String using the class android.util.Base64 and pass in the resulting String.
   *     <br>
   *     <br>
   *     <b>NOTE:</b> The above String conversion is very clumsy, but seems to be necessary. See
   *     https://stackoverflow.com/questions/20911760/android-how-to-query-sqlitedatabase-with-non-string-selection-args
   * @return An ArrayList containing Address objects populated with the data from the database
   *     search
   */
  public ArrayList<Address> searchAddresses(String columnName, String searchString) {
    ArrayList<Address> matchingRecords = new ArrayList<Address>();

    // Specify which columns from the table we are interested in
    String[] projection = {
      AddressesTable.COLUMN_ID,
      AddressesTable.COLUMN_CORRESPONDING_PUBKEY_ID,
      AddressesTable.COLUMN_LABEL,
      AddressesTable.COLUMN_ADDRESS,
      AddressesTable.COLUMN_PRIVATE_SIGNING_KEY,
      AddressesTable.COLUMN_PRIVATE_ENCRYPTION_KEY,
      AddressesTable.COLUMN_RIPE_HASH,
      AddressesTable.COLUMN_TAG
    };

    // Query the database via the ContentProvider
    Cursor cursor =
        mContentResolver.query(
            DatabaseContentProvider.CONTENT_URI_ADDRESSES,
            projection,
            AddressesTable.TABLE_ADDRESSES + "." + columnName + " = ? ",
            new String[] {searchString},
            null);

    if (cursor.moveToFirst()) {
      do {
        long id = cursor.getLong(0);
        long correspondingPubkeyId = cursor.getLong(1);
        String label = cursor.getString(2);
        String address = cursor.getString(3);
        String privateSigningKey = cursor.getString(4);
        String privateEncryptionKey = cursor.getString(5);
        byte[] ripeHash = Base64.decode(cursor.getString(6), Base64.DEFAULT);
        byte[] tag = Base64.decode(cursor.getString(7), Base64.DEFAULT);

        Address a = new Address();
        a.setId(id);
        a.setCorrespondingPubkeyId(correspondingPubkeyId);
        a.setLabel(label);
        a.setAddress(address);
        a.setPrivateSigningKey(privateSigningKey);
        a.setPrivateEncryptionKey(privateEncryptionKey);
        a.setRipeHash(ripeHash);
        a.setTag(tag);

        matchingRecords.add(a);
      } while (cursor.moveToNext());
    } else {
      Log.i(
          TAG,
          "Unable to find any Addresses with the value "
              + searchString
              + " in the "
              + columnName
              + " column");
      cursor.close();
      return matchingRecords;
    }

    cursor.close();
    return matchingRecords;
  }