예제 #1
0
  private void insertInboxRecords() throws Exception {
    // Try and insert inbox messages
    Cursor cursor = getContentResolver().query(SYSTEM_SMS_INBOX_URI, INBOX_PROJ, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
      do {
        ContentValues values = new ContentValues();

        Contact contact = ContactUtils.getContactFromPhone(this, cursor.getString(0));

        if (contact == null || contact.name == null) {
          values.put(DbField.DISPLAY_NAME.getName(), cursor.getString(0));
        } else {
          values.put(DbField.DISPLAY_NAME.getName(), contact.name);
        }

        values.put(DbField.NUMBER.getName(), cursor.getString(0));
        values.put(DbField.TIME.getName(), cursor.getLong(1));
        values.put(DbField.READ.getName(), cursor.getInt(2));
        values.put(DbField.MESSAGE.getName(), cursor.getString(3));

        if (contact != null && contact.lookupId != null) {
          values.put(DbField.CONTACT_LOOKUP_ID.getName(), contact.lookupId);
        }

        if (contact != null && contact.photoUri != null) {
          values.put(DbField.PHOTO_URI.getName(), contact.photoUri);
        }

        if (DatabaseService.doInsert(this, MessagesProvider.INBOX_URI, values) == null) {
          throw new SQLException("No record was inserted for sms: " + cursor.getString(4));
        }
      } while (cursor.moveToNext());
    }
  }
예제 #2
0
  @Override
  public void onReceive(Context context, Intent intent) {
    // get the Bundle map from the Intent parameter to onReceive()
    Bundle bundle = intent.getExtras();

    boolean isFromDraft = bundle.getBoolean(Extra.IS_DRAFT, false);
    int replyToId = bundle.getInt(Extra.ID, -1);
    String address = bundle.getString(Extra.ADDRESS);
    String message = bundle.getString(Extra.TEXT);
    Contact contact = ContactUtils.getContactFromPhone(context, address);

    ContentValues values = new ContentValues();
    values.put(DbField.TIME.getName(), System.currentTimeMillis());
    values.put(DbField.MESSAGE.getName(), message);
    values.put(DbField.NUMBER.getName(), address);

    if (contact != null) {
      values.put(DbField.CONTACT_LOOKUP_ID.getName(), contact.lookupId);
      values.put(DbField.PHOTO_URI.getName(), contact.photoUri);
    }

    values.put(
        DbField.DISPLAY_NAME.getName(),
        (contact == null || contact.name == null) ? address : contact.name);

    if (replyToId != -1) {
      values.put(DbField.IN_REPLY_TO_ID.getName(), replyToId);
    }

    if (BuildConfig.DEBUG) {
      Log.i(TAG, "Inserting: " + values);
    }

    switch (getResultCode()) {
      case Activity.RESULT_OK:
        // Sms was sent successfully! Put it into our 'sent messages'
        // table
        if (BuildConfig.DEBUG) {
          Log.i(TAG, "Message was sent successfully!");
        }
        values.put(DbField.IS_DRAFT.getName(), "0");

        DatabaseService.requestInsertSent(context, values);

        if (isFromDraft) {
          DatabaseService.requestRemoveDraft(context, address, message);
        }

        Toast.makeText(context, "Message Sent!", Toast.LENGTH_SHORT).show();
        break;
      default:
        // Some sort of error occured! Notify the user and save in
        // 'drafts' table
        if (BuildConfig.DEBUG) {
          Log.w(TAG, "Error sending message");
        }

        values.put(DbField.IS_DRAFT.getName(), "1");

        if (!isFromDraft) {
          Toast.makeText(context, "Error sending message. Saved in drafts", Toast.LENGTH_LONG)
              .show();
          // Dont want to insert mulitple records if the draft fails
          // more than once..
          DatabaseService.requestInsertDraft(context, values);
        }

        break;
    }
  }