Example usage for android.database.sqlite SqliteWrapper query

List of usage examples for android.database.sqlite SqliteWrapper query

Introduction

In this page you can find the example usage for android.database.sqlite SqliteWrapper query.

Prototype

@UnsupportedAppUsage
    public static Cursor query(Context context, ContentResolver resolver, Uri uri, String[] projection,
            String selection, String[] selectionArgs, String sortOrder) 

Source Link

Usage

From source file:com.android.mms.transaction.RetrieveTransaction.java

private String getContentLocation(Context context, Uri uri) throws MmsException {
    Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), uri, PROJECTION, null, null,
            null);//ww w  .j  a  v  a 2  s  . c  o m
    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);
}

From source file:com.android.mms.ui.MailBoxMessageContent.java

private void resendShortMessage(long threadId, Uri uri) {
    Cursor cursor = SqliteWrapper.query(this, getContentResolver(), uri,
            new String[] { Sms.ADDRESS, Sms.BODY, Sms.SUBSCRIPTION_ID }, null, null, null);

    if (cursor != null) {
        try {//from  ww  w  .j a va2  s .co m
            if (cursor.moveToFirst()) {
                MessageSender sender = new SmsMessageSender(this,
                        new String[] { cursor.getString(SMS_ADDRESS_INDEX) }, cursor.getString(SMS_BODY_INDEX),
                        threadId, cursor.getInt(SMS_SUB_ID_INDEX));
                sender.sendMessage(threadId);

                // Delete the undelivered message since the sender will
                // save a new one into database.
                SqliteWrapper.delete(this, getContentResolver(), uri, null, null);
            }
        } catch (MmsException e) {
            Log.e(TAG, e.getMessage());
        } finally {
            cursor.close();
        }
    } else {
        Toast.makeText(MailBoxMessageContent.this, R.string.send_failure, Toast.LENGTH_SHORT).show();
    }
}

From source file:com.android.mms.ui.MailBoxMessageContent.java

private boolean isLockMessage() {
    boolean locked = false;

    Cursor c = SqliteWrapper.query(MailBoxMessageContent.this, mContentResolver, mMessageUri,
            SMS_LOCK_PROJECTION, null, null, null);

    try {//w  w  w .  ja  va 2s.c o  m
        if (c != null && c.moveToFirst()) {
            locked = c.getInt(1) != 0;
        }
    } finally {
        if (c != null)
            c.close();
    }
    return locked;
}

From source file:com.android.mms.transaction.MessagingNotification.java

private static final void addMmsNotificationInfos(Context context, Set<Long> threads,
        SortedSet<NotificationInfo> notificationSet) {
    ContentResolver resolver = context.getContentResolver();

    // This query looks like this when logged:
    // I/Database(  147): elapsedTime4Sql|/data/data/com.android.providers.telephony/databases/
    // mmssms.db|0.362 ms|SELECT thread_id, date, _id, sub, sub_cs FROM pdu WHERE ((msg_box=1
    // AND seen=0 AND (m_type=130 OR m_type=132))) ORDER BY date desc

    Cursor cursor = SqliteWrapper.query(context, resolver, Mms.CONTENT_URI, MMS_STATUS_PROJECTION,
            NEW_INCOMING_MM_CONSTRAINT, null, Mms.DATE + " desc");

    if (cursor == null) {
        return;/*from w  w w .j av  a2s  . c  om*/
    }

    try {
        while (cursor.moveToNext()) {

            long msgId = cursor.getLong(COLUMN_MMS_ID);
            Uri msgUri = Mms.CONTENT_URI.buildUpon().appendPath(Long.toString(msgId)).build();
            String address = AddressUtils.getFrom(context, msgUri);

            Contact contact = Contact.get(address, false);
            if (contact.getSendToVoicemail()) {
                // don't notify, skip this one
                continue;
            }

            String subject = getMmsSubject(cursor.getString(COLUMN_MMS_SUBJECT),
                    cursor.getInt(COLUMN_MMS_SUBJECT_CS));
            subject = MessageUtils.cleanseMmsSubject(context, subject);

            long threadId = cursor.getLong(COLUMN_MMS_THREAD_ID);
            long timeMillis = cursor.getLong(COLUMN_MMS_DATE) * 1000;
            int subId = cursor.getInt(COLUMN_MMS_SUB_ID);

            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.d(TAG, "addMmsNotificationInfos: count=" + cursor.getCount() + ", addr = " + address
                        + ", thread_id=" + threadId);
            }

            // Extract the message and/or an attached picture from the first slide
            Bitmap attachedPicture = null;
            String messageBody = null;
            int attachmentType = WorkingMessage.TEXT;
            try {
                GenericPdu pdu = sPduPersister.load(msgUri);
                if (pdu != null && pdu instanceof MultimediaMessagePdu) {
                    SlideshowModel slideshow = SlideshowModel.createFromPduBody(context,
                            ((MultimediaMessagePdu) pdu).getBody());
                    attachmentType = getAttachmentType(slideshow);
                    SlideModel firstSlide = slideshow.get(0);
                    if (firstSlide != null) {
                        if (firstSlide.hasImage()) {
                            int maxDim = dp2Pixels(MAX_BITMAP_DIMEN_DP);
                            attachedPicture = firstSlide.getImage().getBitmap(maxDim, maxDim);
                        }
                        if (firstSlide.hasText()) {
                            messageBody = firstSlide.getText().getText();
                        }
                    }
                }
            } catch (final MmsException e) {
                Log.e(TAG, "MmsException loading uri: " + msgUri, e);
                continue; // skip this bad boy -- don't generate an empty notification
            }

            NotificationInfo info = getNewMessageNotificationInfo(context, false /* isSms */, address,
                    messageBody, subject, threadId, subId, timeMillis, attachedPicture, contact,
                    attachmentType);
            if (MessageUtils.isMailboxMode()) {
                info.mClickIntent.setData(msgUri);
            }
            notificationSet.add(info);

            threads.add(threadId);
        }
    } finally {
        cursor.close();
    }
}

From source file:com.android.mms.transaction.MessagingNotification.java

private static final MmsSmsDeliveryInfo getSmsNewDeliveryInfo(Context context) {
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = SqliteWrapper.query(context, resolver, Sms.CONTENT_URI, SMS_STATUS_PROJECTION,
            NEW_DELIVERY_SM_CONSTRAINT, null, Sms.DATE);

    if (cursor == null) {
        return null;
    }//from   ww  w  .j  a  v  a 2 s.c  o m

    try {
        if (!cursor.moveToLast()) {
            return null;
        }

        String address = cursor.getString(COLUMN_SMS_ADDRESS);
        long timeMillis = 3000;

        Contact contact = Contact.get(address, false);
        String name = contact.getNameAndNumber();

        return new MmsSmsDeliveryInfo(context.getString(R.string.delivery_toast_body, name), timeMillis);

    } finally {
        cursor.close();
    }
}

From source file:com.android.mms.transaction.MessagingNotification.java

private static final void addSmsNotificationInfos(Context context, Set<Long> threads,
        SortedSet<NotificationInfo> notificationSet) {
    ContentResolver resolver = context.getContentResolver();
    Cursor cursor = SqliteWrapper.query(context, resolver, Sms.CONTENT_URI, SMS_STATUS_PROJECTION,
            NEW_INCOMING_SM_CONSTRAINT, null, Sms.DATE + " desc");

    if (cursor == null) {
        return;/*from   w w  w.  ja  v  a 2  s.  c  om*/
    }

    try {
        while (cursor.moveToNext()) {
            String address = cursor.getString(COLUMN_SMS_ADDRESS);
            if (MessageUtils.isWapPushNumber(address)) {
                String[] mAddresses = address.split(":");
                address = mAddresses[context.getResources().getInteger(R.integer.wap_push_address_index)];
            }

            Contact contact = Contact.get(address, false);
            if (contact.getSendToVoicemail()) {
                // don't notify, skip this one
                continue;
            }

            String message = cursor.getString(COLUMN_SMS_BODY);
            long threadId = cursor.getLong(COLUMN_SMS_THREAD_ID);
            long timeMillis = cursor.getLong(COLUMN_SMS_DATE);
            int subId = cursor.getInt(COLUMN_SMS_SUB_ID);
            String msgId = cursor.getString(COLUMN_SMS_ID);

            if (Log.isLoggable(LogTag.APP, Log.VERBOSE)) {
                Log.d(TAG, "addSmsNotificationInfos: count=" + cursor.getCount() + ", addr=" + address
                        + ", thread_id=" + threadId);
            }

            NotificationInfo info = getNewMessageNotificationInfo(context, true /* isSms */, address, message,
                    null /* subject */, threadId, subId, timeMillis, null /* attachmentBitmap */, contact,
                    WorkingMessage.TEXT);
            if (MessageUtils.isMailboxMode()) {
                info.mClickIntent.setData(Uri.withAppendedPath(Sms.CONTENT_URI, msgId));
            }
            notificationSet.add(info);

            threads.add(threadId);
            threads.add(cursor.getLong(COLUMN_SMS_THREAD_ID));
        }
    } finally {
        cursor.close();
    }
}

From source file:com.android.mms.ui.ComposeMessageActivity.java

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 {/*from  ww w  .  java  2  s.  c  om*/
            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;
}

From source file:com.android.mms.transaction.MessagingNotification.java

/**
 * Return the pending intent for failed messages in conversation mode.
 * @param context The context// www.j av  a  2  s  .c  om
 * @param isDownload Whether the message is failed to download
 * @param threadId The thread if of the  message failed to download
 */
private static Intent getFailedIntentFromConversationMode(Context context, boolean isDownload, long threadId) {
    Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), UNDELIVERED_URI,
            MMS_THREAD_ID_PROJECTION, "read=0", null, null);
    if (cursor == null) {
        return null;
    }
    try {
        Intent failedIntent;
        if (isFailedMessagesInSameThread(cursor)) {
            failedIntent = new Intent(context, ComposeMessageActivity.class);
            if (isDownload) {
                // When isDownload is true, the valid threadId is passed into this function.
                failedIntent.putExtra(FAILED_DOWNLOAD_FLAG, true);
                failedIntent.putExtra(ComposeMessageActivity.THREAD_ID, threadId);
            } else {
                // For send failed case, get the thread id from the cursor.
                failedIntent.putExtra(UNDELIVERED_FLAG, true);
                failedIntent.putExtra(ComposeMessageActivity.THREAD_ID, getUndeliveredMessageThreadId(cursor));
            }
        } else {
            failedIntent = new Intent(context, ConversationList.class);
        }
        return failedIntent;
    } finally {
        cursor.close();
    }
}

From source file:com.android.mms.transaction.MessagingNotification.java

/**
 * Return the pending intent for failed messages in folder mode.
 * @param context The context//ww  w .  j a  v  a  2 s.  c  om
 * @param failedCount The failed messages' count
 * @param isDownload Whether the messages is for received
 */
private static Intent getFailedIntentFromFolderMode(Context context, int failedCount, boolean isDownload) {
    // Query the DB and return the cursor of the  undelivered messages
    Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(), UNDELIVERED_URI,
            MAILBOX_PROJECTION, "read=0", null, null);
    if (cursor == null) {
        return null;
    }

    try {
        int mailboxId = MailBoxMessageList.TYPE_INVALID;
        Intent failedIntent;

        if (failedCount > 1) {
            if (isFailedMessagesInSameBox(cursor)) {
                mailboxId = getUndeliveredMessageBoxId(cursor);
            } else {
                mailboxId = MailBoxMessageList.TYPE_INBOX;
            }

            failedIntent = new Intent(context, MailBoxMessageList.class);
            failedIntent.putExtra(MessageUtils.MAIL_BOX_ID, mailboxId);
            failedIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            return failedIntent;
        }

        // The rest cases: the "failedCount" is 1.
        Uri msgUri;
        String type = getUndeliveredMessageType(cursor);
        Long msgId = getUndeliveredMessageId(cursor);
        if (TextUtils.isEmpty(type)) {
            return null;
        }
        if (type.equals("sms")) {
            failedIntent = new Intent(context, MailBoxMessageContent.class);
            msgUri = Uri.withAppendedPath(Sms.CONTENT_URI, String.valueOf(msgId));
            failedIntent.setData(msgUri);
        } else {
            // MMS type.
            if (isDownload) {
                //  Download fail will jump to MailBoxMessageList INBOX.
                failedIntent = new Intent(context, MailBoxMessageList.class);
                mailboxId = MailBoxMessageList.TYPE_INBOX;
                failedIntent.putExtra(MessageUtils.MAIL_BOX_ID, mailboxId);
            } else {
                failedIntent = new Intent(context, MobilePaperShowActivity.class);
                msgUri = Uri.withAppendedPath(Mms.CONTENT_URI, String.valueOf(msgId));
                failedIntent.setData(msgUri);
            }
        }
        failedIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        return failedIntent;
    } finally {
        cursor.close();
    }
}