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

The following examples show how to use net.sqlcipher.database.SQLiteDatabase#insert() . 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
private long createThreadForRecipient(@NonNull RecipientId recipientId, boolean group, int distributionType) {
  if (recipientId.isUnknown()) {
    throw new AssertionError("Cannot create a thread for an unknown recipient!");
  }

  ContentValues contentValues = new ContentValues(4);
  long date                   = System.currentTimeMillis();

  contentValues.put(DATE, date - date % 1000);
  contentValues.put(RECIPIENT_ID, recipientId.serialize());

  if (group)
    contentValues.put(TYPE, distributionType);

  contentValues.put(MESSAGE_COUNT, 0);

  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  return db.insert(TABLE_NAME, null, contentValues);
}
 
Example 2
private @NonNull Pair<Long, Long> insertCallLog(@NonNull RecipientId recipientId, long type, boolean unread) {
  Recipient recipient = Recipient.resolved(recipientId);
  long      threadId  = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);

  ContentValues values = new ContentValues(6);
  values.put(RECIPIENT_ID, recipientId.serialize());
  values.put(ADDRESS_DEVICE_ID,  1);
  values.put(DATE_RECEIVED, System.currentTimeMillis());
  values.put(DATE_SENT, System.currentTimeMillis());
  values.put(READ, unread ? 0 : 1);
  values.put(TYPE, type);
  values.put(THREAD_ID, threadId);

  SQLiteDatabase db = databaseHelper.getWritableDatabase();
  long messageId    = db.insert(TABLE_NAME, null, values);

  DatabaseFactory.getThreadDatabase(context).update(threadId, true);
  notifyConversationListeners(threadId);
  ApplicationDependencies.getJobManager().add(new TrimThreadJob(threadId));

  if (unread) {
    DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
  }

  return new Pair<>(messageId, threadId);
}
 
Example 3
public Pair<Long, Long> insertMessageInbox(@NonNull NotificationInd notification, int subscriptionId) {
  SQLiteDatabase       db             = databaseHelper.getWritableDatabase();
  long                 threadId       = getThreadIdFor(notification);
  ContentValues        contentValues  = new ContentValues();
  ContentValuesBuilder contentBuilder = new ContentValuesBuilder(contentValues);

  Log.i(TAG, "Message received type: " + notification.getMessageType());

  contentBuilder.add(CONTENT_LOCATION, notification.getContentLocation());
  contentBuilder.add(DATE_SENT, System.currentTimeMillis());
  contentBuilder.add(EXPIRY, notification.getExpiry());
  contentBuilder.add(MESSAGE_SIZE, notification.getMessageSize());
  contentBuilder.add(TRANSACTION_ID, notification.getTransactionId());
  contentBuilder.add(MESSAGE_TYPE, notification.getMessageType());

  if (notification.getFrom() != null) {
    Recipient recipient = Recipient.external(context, Util.toIsoString(notification.getFrom().getTextString()));
    contentValues.put(RECIPIENT_ID, recipient.getId().serialize());
  } else {
    contentValues.put(RECIPIENT_ID, RecipientId.UNKNOWN.serialize());
  }

  contentValues.put(MESSAGE_BOX, Types.BASE_INBOX_TYPE);
  contentValues.put(THREAD_ID, threadId);
  contentValues.put(STATUS, Status.DOWNLOAD_INITIALIZED);
  contentValues.put(DATE_RECEIVED, generatePduCompatTimestamp());
  contentValues.put(READ, Util.isDefaultSmsProvider(context) ? 0 : 1);
  contentValues.put(SUBSCRIPTION_ID, subscriptionId);

  if (!contentValues.containsKey(DATE_SENT))
    contentValues.put(DATE_SENT, contentValues.getAsLong(DATE_RECEIVED));

  long messageId = db.insert(TABLE_NAME, null, contentValues);

  return new Pair<>(messageId, threadId);
}
 
Example 4
public void insertDrafts(long threadId, List<Draft> drafts) {
  SQLiteDatabase db    = databaseHelper.getWritableDatabase();

  for (Draft draft : drafts) {
    ContentValues values = new ContentValues(3);
    values.put(THREAD_ID, threadId);
    values.put(DRAFT_TYPE, draft.getType());
    values.put(DRAFT_VALUE, draft.getValue());

    db.insert(TABLE_NAME, null, values);
  }
}
 
Example 5
public void applyStorageSyncUpdates(@NonNull Collection<SignalStorageRecord> inserts,
                                    @NonNull Collection<SignalStorageRecord> deletes)
{
  SQLiteDatabase db = databaseHelper.getWritableDatabase();

  db.beginTransaction();
  try {
    for (SignalStorageRecord insert : inserts) {
      ContentValues values = new ContentValues();
      values.put(TYPE, insert.getType());
      values.put(STORAGE_ID, Base64.encodeBytes(insert.getId().getRaw()));

      db.insert(TABLE_NAME, null, values);
    }

    String deleteQuery = STORAGE_ID + " = ?";

    for (SignalStorageRecord delete : deletes) {
      String[] args = new String[] { Base64.encodeBytes(delete.getId().getRaw()) };
      db.delete(TABLE_NAME, deleteQuery, args);
    }

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

}
 
Example 6
public void insert(Collection<RecipientId> recipientIds, long mmsId, int status, long timestamp) {
  SQLiteDatabase db = databaseHelper.getWritableDatabase();

  for (RecipientId recipientId : recipientIds) {
    ContentValues values = new ContentValues(4);
    values.put(MMS_ID, mmsId);
    values.put(RECIPIENT_ID, recipientId.serialize());
    values.put(STATUS, status);
    values.put(TIMESTAMP, timestamp);

    db.insert(TABLE_NAME, null, values);
  }
}
 
Example 7
public boolean insertPerson(String firstName, String lastName, String address) {
    SQLiteDatabase db = this.getWritableDatabase(DB_PASSWORD);
    ContentValues contentValues = new ContentValues();
    contentValues.put("first_name", firstName);
    contentValues.put("last_name", lastName);
    contentValues.put("address", address);
    db.insert("person", null, contentValues);
    db.close();
    return true;
}
 
Example 8
protected Optional<InsertResult> insertMessageInbox(IncomingTextMessage message, long type) {
  if (message.isJoined()) {
    type = (type & (Types.TOTAL_MASK - Types.BASE_TYPE_MASK)) | Types.JOINED_TYPE;
  } else if (message.isPreKeyBundle()) {
    type |= Types.KEY_EXCHANGE_BIT | Types.KEY_EXCHANGE_BUNDLE_BIT;
  } else if (message.isSecureMessage()) {
    type |= Types.SECURE_MESSAGE_BIT;
  } else if (message.isGroup()) {
    IncomingGroupUpdateMessage incomingGroupUpdateMessage = (IncomingGroupUpdateMessage) message;

    type |= Types.SECURE_MESSAGE_BIT;

    if      (incomingGroupUpdateMessage.isGroupV2()) type |= Types.GROUP_V2_BIT | Types.GROUP_UPDATE_BIT;
    else if (incomingGroupUpdateMessage.isUpdate())  type |= Types.GROUP_UPDATE_BIT;
    else if (incomingGroupUpdateMessage.isQuit())    type |= Types.GROUP_QUIT_BIT;

  } else if (message.isEndSession()) {
    type |= Types.SECURE_MESSAGE_BIT;
    type |= Types.END_SESSION_BIT;
  }

  if (message.isPush())                type |= Types.PUSH_MESSAGE_BIT;
  if (message.isIdentityUpdate())      type |= Types.KEY_EXCHANGE_IDENTITY_UPDATE_BIT;
  if (message.isContentPreKeyBundle()) type |= Types.KEY_EXCHANGE_CONTENT_FORMAT;

  if      (message.isIdentityVerified())    type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
  else if (message.isIdentityDefault())     type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;

  Recipient recipient = Recipient.resolved(message.getSender());

  Recipient groupRecipient;

  if (message.getGroupId() == null) {
    groupRecipient = null;
  } else {
    RecipientId id = DatabaseFactory.getRecipientDatabase(context).getOrInsertFromGroupId(message.getGroupId());
    groupRecipient = Recipient.resolved(id);
  }

  boolean    unread     = (org.thoughtcrime.securesms.util.Util.isDefaultSmsProvider(context) ||
                          message.isSecureMessage() || message.isGroup() || message.isPreKeyBundle()) &&
                          !message.isIdentityUpdate() && !message.isIdentityDefault() && !message.isIdentityVerified();

  long       threadId;

  if (groupRecipient == null) threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipient);
  else                        threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(groupRecipient);

  ContentValues values = new ContentValues(6);
  values.put(RECIPIENT_ID, message.getSender().serialize());
  values.put(ADDRESS_DEVICE_ID,  message.getSenderDeviceId());
  values.put(DATE_RECEIVED, System.currentTimeMillis());
  values.put(DATE_SENT, message.getSentTimestampMillis());
  values.put(DATE_SERVER, message.getServerTimestampMillis());
  values.put(PROTOCOL, message.getProtocol());
  values.put(READ, unread ? 0 : 1);
  values.put(SUBSCRIPTION_ID, message.getSubscriptionId());
  values.put(EXPIRES_IN, message.getExpiresIn());
  values.put(UNIDENTIFIED, message.isUnidentified());

  if (!TextUtils.isEmpty(message.getPseudoSubject()))
    values.put(SUBJECT, message.getPseudoSubject());

  values.put(REPLY_PATH_PRESENT, message.isReplyPathPresent());
  values.put(SERVICE_CENTER, message.getServiceCenterAddress());
  values.put(BODY, message.getMessageBody());
  values.put(TYPE, type);
  values.put(THREAD_ID, threadId);

  if (message.isPush() && isDuplicate(message, threadId)) {
    Log.w(TAG, "Duplicate message (" + message.getSentTimestampMillis() + "), ignoring...");
    return Optional.absent();
  } else {
    SQLiteDatabase db        = databaseHelper.getWritableDatabase();
    long           messageId = db.insert(TABLE_NAME, null, values);

    if (unread) {
      DatabaseFactory.getThreadDatabase(context).incrementUnread(threadId, 1);
    }

    if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
      DatabaseFactory.getThreadDatabase(context).update(threadId, true);
    }

    if (message.getSubscriptionId() != -1) {
      DatabaseFactory.getRecipientDatabase(context).setDefaultSubscriptionId(recipient.getId(), message.getSubscriptionId());
    }

    notifyConversationListeners(threadId);

    if (!message.isIdentityUpdate() && !message.isIdentityVerified() && !message.isIdentityDefault()) {
      ApplicationDependencies.getJobManager().add(new TrimThreadJob(threadId));
    }

    return Optional.of(new InsertResult(messageId, threadId));
  }
}
 
Example 9
public long insertMessageOutbox(long threadId, OutgoingTextMessage message,
                                boolean forceSms, long date, InsertListener insertListener)
{
  long type = Types.BASE_SENDING_TYPE;

  if      (message.isKeyExchange())   type |= Types.KEY_EXCHANGE_BIT;
  else if (message.isSecureMessage()) type |= (Types.SECURE_MESSAGE_BIT | Types.PUSH_MESSAGE_BIT);
  else if (message.isEndSession())    type |= Types.END_SESSION_BIT;
  if      (forceSms)                  type |= Types.MESSAGE_FORCE_SMS_BIT;

  if      (message.isIdentityVerified()) type |= Types.KEY_EXCHANGE_IDENTITY_VERIFIED_BIT;
  else if (message.isIdentityDefault())  type |= Types.KEY_EXCHANGE_IDENTITY_DEFAULT_BIT;

  RecipientId            recipientId           = message.getRecipient().getId();
  Map<RecipientId, Long> earlyDeliveryReceipts = earlyDeliveryReceiptCache.remove(date);

  ContentValues contentValues = new ContentValues(6);
  contentValues.put(RECIPIENT_ID, recipientId.serialize());
  contentValues.put(THREAD_ID, threadId);
  contentValues.put(BODY, message.getMessageBody());
  contentValues.put(DATE_RECEIVED, System.currentTimeMillis());
  contentValues.put(DATE_SENT, date);
  contentValues.put(READ, 1);
  contentValues.put(TYPE, type);
  contentValues.put(SUBSCRIPTION_ID, message.getSubscriptionId());
  contentValues.put(EXPIRES_IN, message.getExpiresIn());
  contentValues.put(DELIVERY_RECEIPT_COUNT, Stream.of(earlyDeliveryReceipts.values()).mapToLong(Long::longValue).sum());

  SQLiteDatabase db        = databaseHelper.getWritableDatabase();
  long           messageId = db.insert(TABLE_NAME, null, contentValues);

  if (insertListener != null) {
    insertListener.onComplete();
  }

  if (!message.isIdentityVerified() && !message.isIdentityDefault()) {
    DatabaseFactory.getThreadDatabase(context).update(threadId, true);
    DatabaseFactory.getThreadDatabase(context).setLastSeen(threadId);
  }

  DatabaseFactory.getThreadDatabase(context).setHasSent(threadId, true);

  notifyConversationListeners(threadId);

  if (!message.isIdentityVerified() && !message.isIdentityDefault()) {
    ApplicationDependencies.getJobManager().add(new TrimThreadJob(threadId));
  }

  return messageId;
}