private String getContentLocation(Context context, Uri uri) throws MmsException { Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), uri, PROJECTION, null, null, null); mLocked = false; if (cursor != null) { try { if ((cursor.getCount() == 1) && cursor.moveToFirst()) { // Get the locked flag from the M-Notification.ind so it can be transferred // to the real message after the download. mLocked = cursor.getInt(COLUMN_LOCKED) == 1; return cursor.getString(COLUMN_CONTENT_LOCATION); } } finally { cursor.close(); } } throw new MmsException("Cannot get X-Mms-Content-Location from: " + uri); }
private void markMmsFailedToSend(Context context, Uri msgUri) { // https://github.com/qklabs/aosp-messenger/blob/master/src/com/android/mms/data/WorkingMessage.java#L1476-1476 try { PduPersister p = PduPersister.getPduPersister(context); // Move the message into MMS Outbox. A trigger will create an entry in // the "pending_msgs" table. p.move(msgUri, Telephony.Mms.Outbox.CONTENT_URI); // Now update the pending_msgs table with an error for that new item. ContentValues values = new ContentValues(1); values.put(Telephony.MmsSms.PendingMessages.ERROR_TYPE, Telephony.MmsSms.ERR_TYPE_GENERIC_PERMANENT); long msgId = ContentUris.parseId(msgUri); SqliteWrapper.update(context, mContentResolver, Telephony.MmsSms.PendingMessages.CONTENT_URI, values, Telephony.MmsSms.PendingMessages.MSG_ID + "=" + msgId, null); } catch (MmsException e) { // Not much we can do here. If the p.move throws an exception, we'll just // leave the message in the draft box. Log.e(TAG, "Failed to move message to outbox and mark as error: " + msgUri, e); } }
private int getResponseStatus(long msgID) { int respStatus = 0; Cursor cursor = SqliteWrapper.query(mContext, mContentResolver, Mms.Outbox.CONTENT_URI, null, Mms._ID + "=" + msgID, null, null); try { if (cursor.moveToFirst()) { respStatus = cursor.getInt(cursor.getColumnIndexOrThrow(Mms.RESPONSE_STATUS)); } } finally { cursor.close(); } if (respStatus != 0) { Log.e(TAG, "Response status is: " + respStatus); } return respStatus; }
private int getRetrieveStatus(long msgID) { int retrieveStatus = 0; Cursor cursor = SqliteWrapper.query(mContext, mContentResolver, Mms.Inbox.CONTENT_URI, null, Mms._ID + "=" + msgID, null, null); try { if (cursor.moveToFirst()) { retrieveStatus = cursor.getInt(cursor.getColumnIndexOrThrow( Mms.RESPONSE_STATUS)); } } finally { cursor.close(); } if (retrieveStatus != 0) { if (LOCAL_LOGV) Log.v(TAG, "Retrieve status is: " + retrieveStatus); } return retrieveStatus; }
public void markAllPendingTransactionsAsFailed() { synchronized (mProcessing) { while (!mPending.isEmpty()) { Transaction transaction = mPending.remove(0); transaction.mTransactionState.setState(TransactionState.FAILED); if (transaction instanceof SendTransaction) { Uri uri = ((SendTransaction)transaction).mSendReqURI; transaction.mTransactionState.setContentUri(uri); int respStatus = PduHeaders.RESPONSE_STATUS_ERROR_NETWORK_PROBLEM; ContentValues values = new ContentValues(1); values.put("resp_st", respStatus); SqliteWrapper.update(TransactionService.this, TransactionService.this.getContentResolver(), uri, values, null, null); } transaction.notifyObservers(); } } }
public final boolean isLimitSurpassed() { long oneHourAgo = System.currentTimeMillis() - ONE_HOUR; Cursor c = SqliteWrapper.query(mContext, mContext.getContentResolver(), Uri.withAppendedPath( Uri.parse("content://mms"), "rate"), new String[] { "COUNT(*) AS rate" }, "sent_time" + ">" + oneHourAgo, null, null); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(0) >= RATE_LIMIT; } } finally { c.close(); } } return false; }
private static boolean isDuplicateNotification( Context context, NotificationInd nInd) { byte[] rawLocation = nInd.getContentLocation(); if (rawLocation != null) { String location = new String(rawLocation); // TODO do not use the sdk > 19 sms apis for this String selection = "ct_l = ?"; String[] selectionArgs = new String[] { location }; Cursor cursor = SqliteWrapper.query( context, context.getContentResolver(), Uri.parse("content://mms"), new String[] { "_id" }, selection, selectionArgs, null); if (cursor != null) { try { if (cursor.getCount() > 0) { // We already received the same notification before. return true; } } finally { cursor.close(); } } } return false; }
public static void canonicalTableDump() { Log.d(TAG, "**** Dump of canoncial_addresses table ****"); Context context = sInstance.mContext; Cursor c = SqliteWrapper.query(context, context.getContentResolver(), sAllCanonical, null, null, null, null); if (c == null) { Log.w(TAG, "null Cursor in content://mms-sms/canonical-addresses"); } try { while (c.moveToNext()) { // TODO: don't hardcode the column indices long id = c.getLong(0); String number = c.getString(1); Log.d(TAG, "id: " + id + " number: " + number); } } finally { c.close(); } }
/** * getSingleNumberFromCanonicalAddresses looks up the recipientId in the canonical_addresses * table and returns the associated number or email address. * @param context needed for the ContentResolver * @param recipientId of the contact to look up * @return phone number or email address of the recipientId */ public static String getSingleAddressFromCanonicalAddressInDb(final Context context, final String recipientId) { Cursor c = SqliteWrapper.query(context, context.getContentResolver(), ContentUris.withAppendedId(sSingleCanonicalAddressUri, Long.parseLong(recipientId)), null, null, null, null); if (c == null) { LogTag.warn(TAG, "null Cursor looking up recipient: " + recipientId); return null; } try { if (c.moveToFirst()) { String number = c.getString(0); return number; } } finally { c.close(); } return null; }
public static Uri getContactUriForEmail(Context context, String emailAddress) { Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), Uri.withAppendedPath(ContactsContract.CommonDataKinds.Email.CONTENT_LOOKUP_URI, Uri.encode(emailAddress)), new String[]{ContactsContract.CommonDataKinds.Email.CONTACT_ID, ContactsContract.Contacts.DISPLAY_NAME}, null, null, null); if (cursor != null) { try { while (cursor.moveToNext()) { String name = cursor.getString(1); if (!TextUtils.isEmpty(name)) { return ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, cursor.getLong(0)); } } } finally { cursor.close(); } } return null; }
/** * Get the service center to use for a reply. * * The rule from TS 23.040 D.6 is that we send reply messages to * the service center of the message to which we're replying, but * only if we haven't already replied to that message and only if * <code>TP-Reply-Path</code> was set in that message. * * Therefore, return the service center from the most recent * message in the conversation, but only if it is a message from * the other party, and only if <code>TP-Reply-Path</code> is set. * Otherwise, return null. */ private String getOutgoingServiceCenter(long threadId) { Cursor cursor = null; try { cursor = SqliteWrapper.query(mContext, mContext.getContentResolver(), Inbox.CONTENT_URI, SERVICE_CENTER_PROJECTION, "thread_id = " + threadId, null, "date DESC"); if ((cursor == null) || !cursor.moveToFirst()) { return null; } boolean replyPathPresent = (1 == cursor.getInt(COLUMN_REPLY_PATH_PRESENT)); return replyPathPresent ? cursor.getString(COLUMN_SERVICE_CENTER) : null; } finally { if (cursor != null) { cursor.close(); } } }
private int getRetrieveStatus(long msgID) { int retrieveStatus = 0; Cursor cursor = SqliteWrapper.query(mContext, mContentResolver, Mms.Inbox.CONTENT_URI, null, Mms._ID + "=" + msgID, null, null); try { if (cursor.moveToFirst()) { retrieveStatus = cursor.getInt(cursor.getColumnIndexOrThrow( Mms.RESPONSE_STATUS)); } } finally { cursor.close(); } if (retrieveStatus != 0) { if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE)) { Log.v(TAG, "Retrieve status is: " + retrieveStatus); } } return retrieveStatus; }
private static boolean isDuplicateNotification( Context context, NotificationInd nInd) { byte[] rawLocation = nInd.getContentLocation(); if (rawLocation != null) { String location = new String(rawLocation); String selection = Mms.CONTENT_LOCATION + " = ?"; String[] selectionArgs = new String[] { location }; Cursor cursor = SqliteWrapper.query( context, context.getContentResolver(), Mms.CONTENT_URI, new String[] { Mms._ID }, selection, selectionArgs, null); if (cursor != null) { try { if (cursor.getCount() > 0) { // We already received the same notification before. return true; } } finally { cursor.close(); } } } return false; }
private static int getDownloadFailedMessageCount(Context context) { // Look for any messages in the MMS Inbox that are of the type // NOTIFICATION_IND (i.e. not already downloaded) and in the // permanent failure state. If there are none, cancel any // failed download notification. Cursor c = SqliteWrapper.query(context, context.getContentResolver(), Mms.Inbox.CONTENT_URI, null, Mms.MESSAGE_TYPE + "=" + String.valueOf(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND) + " AND " + Mms.STATUS + "=" + String.valueOf(DownloadManager.STATE_PERMANENT_FAILURE), null, null); if (c == null) { return 0; } int count = c.getCount(); c.close(); return count; }
/** * Get the thread ID of the SMS message with the given URI * @param context The context * @param uri The URI of the SMS message * @return The thread ID, or THREAD_NONE if the URI contains no entries */ public static long getSmsThreadId(Context context, Uri uri) { Cursor cursor = SqliteWrapper.query( context, context.getContentResolver(), uri, SMS_THREAD_ID_PROJECTION, null, null, null); if (cursor == null) { return THREAD_NONE; } try { if (cursor.moveToFirst()) { return cursor.getLong(cursor.getColumnIndex(Sms.THREAD_ID)); } else { return THREAD_NONE; } } finally { cursor.close(); } }
/** * Get the thread ID of the MMS message with the given URI * @param context The context * @param uri The URI of the SMS message * @return The thread ID, or THREAD_NONE if the URI contains no entries */ public static long getThreadId(Context context, Uri uri) { Cursor cursor = SqliteWrapper.query( context, context.getContentResolver(), uri, MMS_THREAD_ID_PROJECTION, null, null, null); if (cursor == null) { return THREAD_NONE; } try { if (cursor.moveToFirst()) { return cursor.getLong(cursor.getColumnIndex(Mms.THREAD_ID)); } else { return THREAD_NONE; } } finally { cursor.close(); } }
/** * Move all messages that are in the outbox to the failed state and set them to unread. * @return The number of messages that were actually moved */ private int moveOutboxMessagesToFailedBox() { ContentValues values = new ContentValues(3); values.put(Sms.TYPE, Sms.MESSAGE_TYPE_FAILED); values.put(Sms.ERROR_CODE, SmsManager.RESULT_ERROR_GENERIC_FAILURE); values.put(Sms.READ, Integer.valueOf(0)); int messageCount = SqliteWrapper.update( getApplicationContext(), getContentResolver(), Outbox.CONTENT_URI, values, "type = " + Sms.MESSAGE_TYPE_OUTBOX, null); if (Log.isLoggable(LogTag.TRANSACTION, Log.VERBOSE) || LogTag.DEBUG_SEND) { Log.v(TAG, "moveOutboxMessagesToFailedBox messageCount: " + messageCount); } return messageCount; }
private void markMmsMessageWithError(Uri mmsUri) { try { PduPersister p = PduPersister.getPduPersister(mActivity); // Move the message into MMS Outbox. A trigger will create an entry // in // the "pending_msgs" table. p.move(mmsUri, Mms.Outbox.CONTENT_URI); // Now update the pending_msgs table with an error for that new // item. ContentValues values = new ContentValues(1); values.put(PendingMessages.ERROR_TYPE, MmsSms.ERR_TYPE_GENERIC_PERMANENT); long msgId = ContentUris.parseId(mmsUri); SqliteWrapper.update(mActivity, mContentResolver, PendingMessages.CONTENT_URI, values, PendingMessages.MSG_ID + "=" + msgId, null); } catch (MmsException e) { // Not much we can do here. If the p.move throws an exception, we'll // just // leave the message in the draft box. Log.e(TAG, "Failed to move message to outbox and mark as error: " + mmsUri, e); } }
private void updateDraftSmsMessage(final Conversation conv, String contents) { final long threadId = conv.getThreadId(); if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) { LogTag.debug("updateDraftSmsMessage tid=%d, contents=\"%s\"", threadId, contents); } // If we don't have a valid thread, there's nothing to do. if (threadId <= 0) { return; } ContentValues values = new ContentValues(3); values.put(Sms.THREAD_ID, threadId); values.put(Sms.BODY, contents); values.put(Sms.TYPE, Sms.MESSAGE_TYPE_DRAFT); SqliteWrapper.insert(mActivity, mContentResolver, Sms.CONTENT_URI, values); asyncDeleteDraftMmsMessage(conv); mMessageUri = null; }
private Uri getContactUriForEmail(String emailAddress) { Cursor cursor = SqliteWrapper.query( this, getContentResolver(), Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(emailAddress)), new String[] { Email.CONTACT_ID, Contacts.DISPLAY_NAME }, null, null, null); if (cursor != null) { try { while (cursor.moveToNext()) { String name = cursor.getString(1); if (!TextUtils.isEmpty(name)) { return ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0)); } } } finally { cursor.close(); } } return null; }
private void startMsgListQuery(int token) { Uri conversationUri = mConversation.getUri(); if (conversationUri == null) { log("##### startMsgListQuery: conversationUri is null, bail!"); return; } long threadId = mConversation.getThreadId(); if (LogTag.VERBOSE || Log.isLoggable(LogTag.APP, Log.VERBOSE)) { log("startMsgListQuery for " + conversationUri + ", threadId=" + threadId + " token: " + token + " mConversation: " + mConversation); } // Cancel any pending queries mBackgroundQueryHandler.cancelOperation(token); try { // Kick off the new query mBackgroundQueryHandler.startQuery(token, threadId /* cookie */, conversationUri, PROJECTION, null, null, null); } catch (SQLiteException e) { SqliteWrapper.checkSQLiteException(this, e); } }
private long getMessageDate(Uri uri) { if (uri != null) { Cursor cursor = SqliteWrapper.query(this, mContentResolver, uri, new String[] { Mms.DATE }, null, null, null); if (cursor != null) { try { if ((cursor.getCount() == 1) && cursor.moveToFirst()) { return cursor.getLong(0) * 1000L; } } finally { cursor.close(); } } } return NO_DATE_FOR_DIALOG; }
public final boolean isLimitSurpassed() { long oneHourAgo = System.currentTimeMillis() - ONE_HOUR; Cursor c = SqliteWrapper.query(mContext, mContext.getContentResolver(), Rate.CONTENT_URI, new String[] { "COUNT(*) AS rate" }, Rate.SENT_TIME + ">" + oneHourAgo, null, null); if (c != null) { try { if (c.moveToFirst()) { return c.getInt(0) >= RATE_LIMIT; } } finally { c.close(); } } return false; }
public void markAllPendingTransactionsAsFailed() { synchronized (mProcessing) { while (mPending.size() != 0) { Transaction transaction = mPending.remove(0); transaction.mTransactionState.setState(TransactionState.FAILED); if (transaction instanceof SendTransaction) { Uri uri = ((SendTransaction)transaction).mSendReqURI; transaction.mTransactionState.setContentUri(uri); int respStatus = PduHeaders.RESPONSE_STATUS_ERROR_NETWORK_PROBLEM; ContentValues values = new ContentValues(1); values.put(Mms.RESPONSE_STATUS, respStatus); SqliteWrapper.update(TransactionService.this, TransactionService.this.getContentResolver(), uri, values, null, null); } transaction.notifyObservers(); } } }
private void markMmsMessageWithError(Uri mmsUri) { try { PduPersister p = PduPersister.getPduPersister(mActivity); // Move the message into MMS Outbox. A trigger will create an entry in // the "pending_msgs" table. p.move(mmsUri, Mms.Outbox.CONTENT_URI); // Now update the pending_msgs table with an error for that new item. ContentValues values = new ContentValues(1); values.put(PendingMessages.ERROR_TYPE, MmsSms.ERR_TYPE_GENERIC_PERMANENT); long msgId = ContentUris.parseId(mmsUri); SqliteWrapper.update(mActivity, mContentResolver, PendingMessages.CONTENT_URI, values, PendingMessages.MSG_ID + "=" + msgId, null); } catch (MmsException e) { // Not much we can do here. If the p.move throws an exception, we'll just // leave the message in the draft box. Log.e(TAG, "Failed to move message to outbox and mark as error: " + mmsUri, e); } }
private Uri getContactUriForEmail(String emailAddress) { Cursor cursor = SqliteWrapper.query(this, getContentResolver(), Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode(emailAddress)), new String[] { Email.CONTACT_ID, Contacts.DISPLAY_NAME }, null, null, null); if (cursor != null) { try { while (cursor.moveToNext()) { String name = cursor.getString(1); if (!TextUtils.isEmpty(name)) { return ContentUris.withAppendedId(Contacts.CONTENT_URI, cursor.getLong(0)); } } } finally { cursor.close(); } } return null; }