private static void migrateConversation(
      Context context,
      MasterSecret masterSecret,
      Handler handler,
      long theirThreadId,
      long ourThreadId) {
    SmsDatabase ourSmsDatabase = DatabaseFactory.getSmsDatabase(context);
    Cursor cursor = null;

    try {
      Uri uri = Uri.parse("content://sms/conversations/" + theirThreadId);
      cursor = context.getContentResolver().query(uri, null, null, null, null);
      SQLiteDatabase transaction = ourSmsDatabase.beginTransaction();
      SQLiteStatement statement = ourSmsDatabase.createInsertStatement(transaction);

      while (cursor != null && cursor.moveToNext()) {
        getContentValuesForRow(context, masterSecret, cursor, ourThreadId, statement);
        statement.execute();

        Message msg =
            handler.obtainMessage(SECONDARY_PROGRESS_UPDATE, 10000 / cursor.getCount(), 0);
        handler.sendMessage(msg);
      }

      ourSmsDatabase.endTransaction(transaction);
      DatabaseFactory.getThreadDatabase(context).update(ourThreadId);
      DatabaseFactory.getThreadDatabase(context).notifyConversationListeners(ourThreadId);

    } finally {
      if (cursor != null) cursor.close();
    }
  }
Example #2
0
  private void handleSentMessage(Intent intent) {
    long messageId = intent.getLongExtra("message_id", -1);
    int result = intent.getIntExtra("ResultCode", -31337);
    boolean upgraded = intent.getBooleanExtra("upgraded", false);
    boolean push = intent.getBooleanExtra("push", false);

    Log.w("SMSReceiverService", "Intent resultcode: " + result);
    Log.w("SMSReceiverService", "Running sent callback: " + messageId);

    if (result == Activity.RESULT_OK) {
      SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
      Cursor cursor = database.getMessage(messageId);
      SmsDatabase.Reader reader = database.readerFor(cursor);

      database.markAsSent(messageId);

      if (upgraded) database.markAsSecure(messageId);
      if (push) database.markAsPush(messageId);

      SmsMessageRecord record = reader.getNext();

      if (record != null && record.isEndSession()) {
        Log.w("SmsSender", "Ending session...");
        Session.abortSessionFor(context, record.getIndividualRecipient());
        KeyExchangeProcessor.broadcastSecurityUpdateEvent(context, record.getThreadId());
      }

      unregisterForRadioChanges();
    } else if (result == SmsManager.RESULT_ERROR_NO_SERVICE
        || result == SmsManager.RESULT_ERROR_RADIO_OFF) {
      DatabaseFactory.getSmsDatabase(context).markAsOutbox(messageId);
      toastHandler
          .obtainMessage(
              0, context.getString(R.string.SmsReceiver_currently_unable_to_send_your_sms_message))
          .sendToTarget();
      registerForRadioChanges();
    } else {
      long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
      Recipients recipients =
          DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);

      DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
      MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
      unregisterForRadioChanges();
    }
  }