Example usage for android.content Intent CATEGORY_APP_MESSAGING

List of usage examples for android.content Intent CATEGORY_APP_MESSAGING

Introduction

In this page you can find the example usage for android.content Intent CATEGORY_APP_MESSAGING.

Prototype

String CATEGORY_APP_MESSAGING

To view the source code for android.content Intent CATEGORY_APP_MESSAGING.

Click Source Link

Document

Used with #ACTION_MAIN to launch the messaging application.

Usage

From source file:com.appsimobile.appsihomeplugins.dashclock.phone.SmsExtension.java

@Override
public void onUpdateData(FieldValues.Builder builder) {
    long lastUnreadThreadId = 0;
    Set<Long> unreadThreadIds = new HashSet<Long>();
    Set<String> unreadThreadParticipantNames = new HashSet<String>();
    boolean showingAllConversationParticipants = false;

    Cursor cursor = tryOpenSimpleThreadsCursor();
    if (cursor != null) {
        while (cursor.moveToNext()) {
            if (cursor.getInt(SimpleThreadsQuery.READ) == 0) {
                long threadId = cursor.getLong(SimpleThreadsQuery._ID);
                unreadThreadIds.add(threadId);
                lastUnreadThreadId = threadId;

                // Some devices will fail on tryOpenMmsSmsCursor below, so
                // store a list of participants on unread threads as a fallback.
                String recipientIdsStr = cursor.getString(SimpleThreadsQuery.RECIPIENT_IDS);
                if (!TextUtils.isEmpty(recipientIdsStr)) {
                    String[] recipientIds = TextUtils.split(recipientIdsStr, " ");
                    for (String recipientId : recipientIds) {
                        Cursor canonAddrCursor = tryOpenCanonicalAddressCursorById(Long.parseLong(recipientId));
                        if (canonAddrCursor == null) {
                            continue;
                        }/*from ww  w .  j  av  a2  s  .co  m*/
                        if (canonAddrCursor.moveToFirst()) {
                            String address = canonAddrCursor.getString(CanonicalAddressQuery.ADDRESS);
                            String displayName = getDisplayNameForContact(0, address);
                            if (!TextUtils.isEmpty(displayName)) {
                                unreadThreadParticipantNames.add(displayName);
                            }
                        }
                        canonAddrCursor.close();
                    }
                }
            }
        }
        cursor.close();

        LOGD(TAG, "Unread thread IDs: [" + TextUtils.join(", ", unreadThreadIds) + "]");
    }

    int unreadConversations = 0;
    StringBuilder names = new StringBuilder();
    cursor = tryOpenMmsSmsCursor();
    if (cursor != null) {
        // Most devices will hit this code path.
        while (cursor.moveToNext()) {
            // Get display name. SMS's are easy; MMS's not so much.
            long id = cursor.getLong(MmsSmsQuery._ID);
            long contactId = cursor.getLong(MmsSmsQuery.PERSON);
            String address = cursor.getString(MmsSmsQuery.ADDRESS);
            long threadId = cursor.getLong(MmsSmsQuery.THREAD_ID);
            if (unreadThreadIds != null && !unreadThreadIds.contains(threadId)) {
                // We have the list of all thread IDs (same as what the messaging app uses), and
                // this supposedly unread message's thread isn't in the list. This message is
                // likely an orphaned message whose thread was deleted. Not skipping it is
                // likely the cause of http://code.google.com/p/dashclock/issues/detail?id=8
                LOGD(TAG, "Skipping probably orphaned message " + id + " with thread ID " + threadId);
                continue;
            }

            ++unreadConversations;
            lastUnreadThreadId = threadId;

            if (contactId == 0 && TextUtils.isEmpty(address) && id != 0) {
                // Try MMS addr query
                Cursor addrCursor = tryOpenMmsAddrCursor(id);
                if (addrCursor != null) {
                    if (addrCursor.moveToFirst()) {
                        contactId = addrCursor.getLong(MmsAddrQuery.CONTACT_ID);
                        address = addrCursor.getString(MmsAddrQuery.ADDRESS);
                    }
                    addrCursor.close();
                }
            }

            String displayName = getDisplayNameForContact(contactId, address);

            if (names.length() > 0) {
                names.append(", ");
            }
            names.append(displayName);
        }
        cursor.close();

    } else {
        // In case the cursor is null (some Samsung devices like the Galaxy S4), use the
        // fall back on the list of participants in unread threads.
        unreadConversations = unreadThreadIds.size();
        names.append(TextUtils.join(", ", unreadThreadParticipantNames));
        showingAllConversationParticipants = true;
    }

    Intent clickIntent;
    if (unreadConversations == 1 && lastUnreadThreadId > 0) {
        clickIntent = new Intent(Intent.ACTION_VIEW, TelephonyProviderConstants.MmsSms.CONTENT_CONVERSATIONS_URI
                .buildUpon().appendPath(Long.toString(lastUnreadThreadId)).build());
    } else {
        clickIntent = IntentCompat.makeMainSelectorActivity(Intent.ACTION_MAIN, Intent.CATEGORY_APP_MESSAGING);
    }

    PendingIntent pendingIntent = PendingIntent.getActivity(getContext(),
            DashClockHomeExtension.DASHCLOCK_EXTENSION_SMS, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    builder.leftImageResId(R.drawable.ic_extension_sms).intent(pendingIntent).header(getResources()
            .getQuantityString(R.plurals.sms_title_template, unreadConversations, unreadConversations));
    if (unreadConversations > 0) {
        builder.text(getString(showingAllConversationParticipants ? R.string.sms_body_all_participants_template
                : R.string.sms_body_template, names.toString()));
    }

    /*
            
    publishUpdate(new ExtensionData()
        .visible(unreadConversations > 0)
        .icon(R.drawable.ic_extension_sms)
        .status(Integer.toString(unreadConversations))
        .expandedTitle(
                getResources().getQuantityString(
                        R.plurals.sms_title_template, unreadConversations,
                        unreadConversations))
        .expandedBody(getString(showingAllConversationParticipants
                ? R.string.sms_body_all_participants_template
                : R.string.sms_body_template,
                names.toString()))
        .clickIntent(clickIntent));
    */
}