Java Code Examples for net.sqlcipher.database.SQLiteDatabase#update()

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#update() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Override
public void markExpireStarted(Collection<Long> ids, long startedAtTimestamp) {
  SQLiteDatabase db       = databaseHelper.getWritableDatabase();
  long           threadId = -1;

  db.beginTransaction();
  try {
    for (long id : ids) {
      ContentValues contentValues = new ContentValues();
      contentValues.put(EXPIRE_STARTED, startedAtTimestamp);

      db.update(TABLE_NAME, contentValues, ID_WHERE, new String[]{String.valueOf(id)});

      if (threadId < 0) {
        threadId = getThreadIdForMessage(id);
      }
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }

  DatabaseFactory.getThreadDatabase(context).update(threadId, false);
  notifyConversationListeners(threadId);
}
 
Example 2
public void clearDirtyState(@NonNull List<RecipientId> recipients) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  db.beginTransaction();

  try {
    ContentValues values = new ContentValues();
    values.put(DIRTY, DirtyState.CLEAN.getId());

    for (RecipientId id : recipients) {
      db.update(TABLE_NAME, values, ID_WHERE, new String[]{ id.serialize() });
    }

    db.setTransactionSuccessful();
  } finally {
    db.endTransaction();
  }
}
 
Example 3
private void setReactions(@NonNull SQLiteDatabase db, long messageId, @NonNull ReactionList reactionList) {
  ContentValues values       = new ContentValues(1);
  boolean       hasReactions = reactionList.getReactionsCount() != 0;

  values.put(REACTIONS, reactionList.getReactionsList().isEmpty() ? null : reactionList.toByteArray());
  values.put(REACTIONS_UNREAD, hasReactions ? 1 : 0);

  if (hasReactions) {
    values.put(NOTIFIED, 0);
  }

  String   query = ID + " = ?";
  String[] args  = new String[] { String.valueOf(messageId) };

  db.update(getTableName(), values, query, args);
}
 
Example 4
/**
 * Sets the profile key iff currently null.
 * <p>
 * If it sets it, it also clears out the profile key credential and resets the unidentified access mode.
 * @return true iff changed.
 */
public boolean setProfileKeyIfAbsent(@NonNull RecipientId id, @NonNull ProfileKey profileKey) {
  SQLiteDatabase database    = databaseHelper.getWritableDatabase();
  String         selection   = ID + " = ? AND " + PROFILE_KEY + " is NULL";
  String[]       args        = new String[]{id.serialize()};
  ContentValues  valuesToSet = new ContentValues(3);

  valuesToSet.put(PROFILE_KEY, Base64.encodeBytes(profileKey.serialize()));
  valuesToSet.putNull(PROFILE_KEY_CREDENTIAL);
  valuesToSet.put(UNIDENTIFIED_ACCESS_MODE, UnidentifiedAccessMode.UNKNOWN.getMode());

  if (database.update(TABLE_NAME, valuesToSet, selection, args) > 0) {
    markDirty(id, DirtyState.UPDATE);
    Recipient.live(id).refresh();
    return true;
  } else {
    return false;
  }
}
 
Example 5
public void applyStorageSyncUpdates(@NonNull StorageId storageId, SignalAccountRecord update) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();

  ContentValues values      = new ContentValues();
  ProfileName   profileName = ProfileName.fromParts(update.getGivenName().orNull(), update.getFamilyName().orNull());
  String        profileKey  = update.getProfileKey().or(Optional.fromNullable(Recipient.self().getProfileKey())).transform(Base64::encodeBytes).orNull();

  if (!update.getProfileKey().isPresent()) {
    Log.w(TAG, "Got an empty profile key while applying an account record update!");
  }

  values.put(PROFILE_GIVEN_NAME, profileName.getGivenName());
  values.put(PROFILE_FAMILY_NAME, profileName.getFamilyName());
  values.put(PROFILE_JOINED_NAME, profileName.toString());
  values.put(PROFILE_KEY, profileKey);
  values.put(STORAGE_SERVICE_ID, Base64.encodeBytes(update.getId().getRaw()));
  values.put(DIRTY, DirtyState.CLEAN.getId());

  int updateCount = db.update(TABLE_NAME, values, STORAGE_SERVICE_ID + " = ?", new String[]{Base64.encodeBytes(storageId.getRaw())});
  if (updateCount < 1) {
    throw new AssertionError("Account update didn't match any rows!");
  }

  Recipient.self().live().refresh();
}
 
Example 6
public void copyAttachmentData(@NonNull AttachmentId sourceId, @NonNull AttachmentId destinationId)
    throws MmsException
{
  DatabaseAttachment sourceAttachment = getAttachment(sourceId);

  if (sourceAttachment == null) {
    throw new MmsException("Cannot find attachment for source!");
  }

  SQLiteDatabase database       = databaseHelper.getWritableDatabase();
  DataInfo       sourceDataInfo = getAttachmentDataFileInfo(sourceId, DATA);

  if (sourceDataInfo == null) {
    throw new MmsException("No attachment data found for source!");
  }

  ContentValues contentValues = new ContentValues();

  contentValues.put(DATA, sourceDataInfo.file.getAbsolutePath());
  contentValues.put(DATA_HASH, sourceDataInfo.hash);
  contentValues.put(SIZE, sourceDataInfo.length);
  contentValues.put(DATA_RANDOM, sourceDataInfo.random);

  contentValues.put(TRANSFER_STATE, sourceAttachment.getTransferState());
  contentValues.put(CDN_NUMBER, sourceAttachment.getCdnNumber());
  contentValues.put(CONTENT_LOCATION, sourceAttachment.getLocation());
  contentValues.put(DIGEST, sourceAttachment.getDigest());
  contentValues.put(CONTENT_DISPOSITION, sourceAttachment.getKey());
  contentValues.put(NAME, sourceAttachment.getRelay());
  contentValues.put(SIZE, sourceAttachment.getSize());
  contentValues.put(FAST_PREFLIGHT_ID, sourceAttachment.getFastPreflightId());
  contentValues.put(WIDTH, sourceAttachment.getWidth());
  contentValues.put(HEIGHT, sourceAttachment.getHeight());
  contentValues.put(CONTENT_TYPE, sourceAttachment.getContentType());
  contentValues.put(VISUAL_HASH, getVisualHashStringOrNull(sourceAttachment));

  database.update(TABLE_NAME, contentValues, PART_ID_WHERE, destinationId.toStrings());
}
 
Example 7
public void markDownloadState(long messageId, long state) {
  SQLiteDatabase database     = databaseHelper.getWritableDatabase();
  ContentValues contentValues = new ContentValues();
  contentValues.put(STATUS, state);

  database.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {messageId + ""});
  notifyConversationListeners(getThreadIdForMessage(messageId));
}
 
Example 8
private static void updateAttachmentDataHash(@NonNull SQLiteDatabase database,
                                             @NonNull String oldHash,
                                             @NonNull DataInfo newData)
{
  if (oldHash == null) return;

  ContentValues contentValues = new ContentValues();
  contentValues.put(DATA, newData.file.getAbsolutePath());
  contentValues.put(DATA_RANDOM, newData.random);
  contentValues.put(DATA_HASH, newData.hash);
  database.update(TABLE_NAME,
                  contentValues,
                  DATA_HASH + " = ?",
                  new String[]{oldHash});
}
 
Example 9
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
  SQLiteDatabase          database  = databaseHelper.getWritableDatabase();
  List<MarkedMessageInfo> results   = new LinkedList<>();
  Cursor                  cursor    = null;

  database.beginTransaction();
  try {
    cursor = database.query(TABLE_NAME, new String[] {ID, RECIPIENT_ID, DATE_SENT, TYPE, EXPIRES_IN, EXPIRE_STARTED, THREAD_ID}, where, arguments, null, null, null);

    while (cursor != null && cursor.moveToNext()) {
      if (Types.isSecureType(cursor.getLong(cursor.getColumnIndex(TYPE)))) {
        long           threadId       = cursor.getLong(cursor.getColumnIndex(THREAD_ID));
        RecipientId    recipientId    = RecipientId.from(cursor.getLong(cursor.getColumnIndex(RECIPIENT_ID)));
        long           dateSent       = cursor.getLong(cursor.getColumnIndex(DATE_SENT));
        long           messageId      = cursor.getLong(cursor.getColumnIndex(ID));
        long           expiresIn      = cursor.getLong(cursor.getColumnIndex(EXPIRES_IN));
        long           expireStarted  = cursor.getLong(cursor.getColumnIndex(EXPIRE_STARTED));
        SyncMessageId  syncMessageId  = new SyncMessageId(recipientId, dateSent);
        ExpirationInfo expirationInfo = new ExpirationInfo(messageId, expiresIn, expireStarted, false);

        results.add(new MarkedMessageInfo(threadId, syncMessageId, expirationInfo));
      }
    }

    ContentValues contentValues = new ContentValues();
    contentValues.put(READ, 1);

    database.update(TABLE_NAME, contentValues, where, arguments);
    database.setTransactionSuccessful();
  } finally {
    if (cursor != null) cursor.close();
    database.endTransaction();
  }

  return results;
}
 
Example 10
private List<MarkedMessageInfo> setMessagesRead(String where, String[] arguments) {
  SQLiteDatabase          database = databaseHelper.getWritableDatabase();
  List<MarkedMessageInfo> result   = new LinkedList<>();
  Cursor                  cursor   = null;

  database.beginTransaction();

  try {
    cursor = database.query(TABLE_NAME, new String[] {ID, RECIPIENT_ID, DATE_SENT, MESSAGE_BOX, EXPIRES_IN, EXPIRE_STARTED, THREAD_ID}, where, arguments, null, null, null);

    while(cursor != null && cursor.moveToNext()) {
      if (Types.isSecureType(cursor.getLong(cursor.getColumnIndex(MESSAGE_BOX)))) {
        long           threadId       = cursor.getLong(cursor.getColumnIndex(THREAD_ID));
        RecipientId    recipientId    = RecipientId.from(cursor.getLong(cursor.getColumnIndex(RECIPIENT_ID)));
        long           dateSent       = cursor.getLong(cursor.getColumnIndex(DATE_SENT));
        long           messageId      = cursor.getLong(cursor.getColumnIndex(ID));
        long           expiresIn      = cursor.getLong(cursor.getColumnIndex(EXPIRES_IN));
        long           expireStarted  = cursor.getLong(cursor.getColumnIndex(EXPIRE_STARTED));
        SyncMessageId  syncMessageId  = new SyncMessageId(recipientId, dateSent);
        ExpirationInfo expirationInfo = new ExpirationInfo(messageId, expiresIn, expireStarted, true);

        result.add(new MarkedMessageInfo(threadId, syncMessageId, expirationInfo));
      }
    }

    ContentValues contentValues = new ContentValues();
    contentValues.put(READ, 1);

    database.update(TABLE_NAME, contentValues, where, arguments);
    database.setTransactionSuccessful();
  } finally {
    if (cursor != null) cursor.close();
    database.endTransaction();
  }

  return result;
}
 
Example 11
Source Project: commcare-android   File: SqlStorage.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void update(int id, Externalizable e) {
    SQLiteDatabase db = helper.getHandle();
    db.beginTransaction();
    try {
        db.update(table, helper.getContentValues(e), DatabaseHelper.ID_COL + "=?", new String[]{String.valueOf(id)});
        db.setTransactionSuccessful();
    } finally {
        db.endTransaction();
    }
}
 
Example 12
@WorkerThread
public void writeAudioHash(@NonNull AttachmentId attachmentId, @Nullable AudioWaveFormData audioWaveForm) {
  Log.i(TAG, "updating part audio wave form for #" + attachmentId);

  SQLiteDatabase database = databaseHelper.getWritableDatabase();
  ContentValues  values   = new ContentValues(1);

  if (audioWaveForm != null) {
    values.put(VISUAL_HASH, new AudioHash(audioWaveForm).getHash());
  } else {
    values.putNull(VISUAL_HASH);
  }

  database.update(TABLE_NAME, values, PART_ID_WHERE, attachmentId.toStrings());
}
 
Example 13
public void setDistributionType(long threadId, int distributionType) {
  ContentValues contentValues = new ContentValues(1);
  contentValues.put(TYPE, distributionType);

  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {threadId + ""});
  notifyConversationListListeners();
}
 
Example 14
public void setVerified(@NonNull RecipientId recipientId, IdentityKey identityKey, VerifiedStatus verifiedStatus) {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();

  ContentValues contentValues = new ContentValues(1);
  contentValues.put(VERIFIED, verifiedStatus.toInt());

  int updated = database.update(TABLE_NAME, contentValues, RECIPIENT_ID + " = ? AND " + IDENTITY_KEY + " = ?",
                                new String[] {recipientId.serialize(), Base64.encodeBytes(identityKey.serialize())});

  if (updated > 0) {
    Optional<IdentityRecord> record = getIdentity(recipientId);
    if (record.isPresent()) EventBus.getDefault().post(record.get());
    DatabaseFactory.getRecipientDatabase(context).markDirty(recipientId, RecipientDatabase.DirtyState.UPDATE);
  }
}
 
Example 15
public void setLastSeen(long threadId) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  ContentValues contentValues = new ContentValues(1);
  contentValues.put(LAST_SEEN, System.currentTimeMillis());

  db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(threadId)});
  notifyConversationListListeners();
}
 
Example 16
@Override
public void markUnidentified(long id, boolean unidentified) {
  ContentValues contentValues = new ContentValues(1);
  contentValues.put(UNIDENTIFIED, unidentified ? 1 : 0);

  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  db.update(TABLE_NAME, contentValues, ID_WHERE, new String[] {String.valueOf(id)});
}
 
Example 17
private void updateEntryToStoreInDb(Externalizable extObj, boolean objectInDb,
                                    String filename, ByteArrayOutputStream bos,
                                    SQLiteDatabase db, int id) {
    ContentValues updatedContentValues =
            helper.getContentValuesWithCustomData(extObj, bos.toByteArray());
    if (!objectInDb) {
        // was stored in file: remove file and store in db
        updatedContentValues.put(DatabaseHelper.FILE_COL, (String)null);
        updatedContentValues.put(DatabaseHelper.AES_COL, (byte[])null);

        HybridFileBackedSqlHelpers.setFileAsOrphan(db, filename);
    }
    db.update(table, updatedContentValues,
            DatabaseHelper.ID_COL + "=?", new String[]{String.valueOf(id)});
}
 
Example 18
public BulkOperationsHandle beginBulkSystemContactUpdate() {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();
  database.beginTransaction();

  ContentValues contentValues = new ContentValues(1);
  contentValues.put(SYSTEM_INFO_PENDING, 1);

  database.update(TABLE_NAME, contentValues, SYSTEM_CONTACT_URI + " NOT NULL", null);

  return new BulkOperationsHandle(database);
}
 
Example 19
public void deleteAttachmentFilesForViewOnceMessage(long mmsId) {
  Log.d(TAG, "[deleteAttachmentFilesForViewOnceMessage] mmsId: " + mmsId);

  SQLiteDatabase database = databaseHelper.getWritableDatabase();
  Cursor cursor           = null;

  try {
    cursor = database.query(TABLE_NAME, new String[] {DATA, THUMBNAIL, CONTENT_TYPE, ROW_ID, UNIQUE_ID}, MMS_ID + " = ?",
        new String[] {mmsId+""}, null, null, null);

    while (cursor != null && cursor.moveToNext()) {
      deleteAttachmentOnDisk(cursor.getString(cursor.getColumnIndex(DATA)),
                             cursor.getString(cursor.getColumnIndex(THUMBNAIL)),
                             cursor.getString(cursor.getColumnIndex(CONTENT_TYPE)),
                             new AttachmentId(cursor.getLong(cursor.getColumnIndex(ROW_ID)),
                                              cursor.getLong(cursor.getColumnIndex(UNIQUE_ID))));
    }
  } finally {
    if (cursor != null)
      cursor.close();
  }

  ContentValues values = new ContentValues();
  values.put(DATA, (String) null);
  values.put(DATA_RANDOM, (byte[]) null);
  values.put(DATA_HASH, (String) null);
  values.put(THUMBNAIL, (String) null);
  values.put(THUMBNAIL_RANDOM, (byte[]) null);
  values.put(FILE_NAME, (String) null);
  values.put(CAPTION, (String) null);
  values.put(SIZE, 0);
  values.put(WIDTH, 0);
  values.put(HEIGHT, 0);
  values.put(TRANSFER_STATE, TRANSFER_PROGRESS_DONE);
  values.put(VISUAL_HASH, (String) null);
  values.put(CONTENT_TYPE, MediaUtil.VIEW_ONCE);

  database.update(TABLE_NAME, values, MMS_ID + " = ?", new String[] {mmsId + ""});
  notifyAttachmentListeners();

  long threadId = DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(mmsId);
  if (threadId > 0) {
    notifyConversationListeners(threadId);
  }
}
 
Example 20
public void setApproval(@NonNull RecipientId recipientId, boolean nonBlockingApproval) {
  SQLiteDatabase database = databaseHelper.getWritableDatabase();

  ContentValues contentValues = new ContentValues(2);
  contentValues.put(NONBLOCKING_APPROVAL, nonBlockingApproval);

  database.update(TABLE_NAME, contentValues, RECIPIENT_ID + " = ?", new String[] {recipientId.serialize()});

  DatabaseFactory.getRecipientDatabase(context).markDirty(recipientId, RecipientDatabase.DirtyState.UPDATE);
}