Example usage for android.text SpannableStringBuilder SpannableStringBuilder

List of usage examples for android.text SpannableStringBuilder SpannableStringBuilder

Introduction

In this page you can find the example usage for android.text SpannableStringBuilder SpannableStringBuilder.

Prototype

public SpannableStringBuilder() 

Source Link

Document

Create a new SpannableStringBuilder with empty contents

Usage

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

private CharSequence formatMessage(MessageItem msgItem, String body, String subject, Pattern highlight,
        String contentType) {//  w  ww.ja  v a2s  .  c o  m
    SpannableStringBuilder buf = new SpannableStringBuilder();

    boolean hasSubject = !TextUtils.isEmpty(subject);
    SmileyParser parser = SmileyParser.getInstance();
    if (hasSubject) {
        CharSequence smilizedSubject = parser.addSmileySpans(subject);
        // Can't use the normal getString() with extra arguments for string replacement
        // because it doesn't preserve the SpannableText returned by addSmileySpans.
        // We have to manually replace the %s with our text.
        buf.append(TextUtils.replace(mContext.getResources().getString(R.string.inline_subject),
                new String[] { "%s" }, new CharSequence[] { smilizedSubject }));
    }

    if (!TextUtils.isEmpty(body)) {
        // Converts html to spannable if ContentType is "text/html".
        if (contentType != null && ContentType.TEXT_HTML.equals(contentType)) {
            buf.append("\n");
            buf.append(Html.fromHtml(body));
        } else {
            if (hasSubject) {
                buf.append(" - ");
            }
            buf.append(parser.addSmileySpans(body));
        }
    }

    if (highlight != null) {
        Matcher m = highlight.matcher(buf.toString());
        while (m.find()) {
            buf.setSpan(new StyleSpan(Typeface.BOLD), m.start(), m.end(), 0);
        }
    }
    return buf;
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

public static Notification buildConversationPageForWearable(final String conversationId, int participantCount) {
    final Context context = Factory.get().getApplicationContext();

    // Limit the number of messages to show. We just want enough to provide context for the
    // notification. Fetch one more than we need, so we can tell if there are more messages
    // before the one we're showing.
    // TODO: in the query, a multipart message will contain a row for each part.
    // We might need a smarter GROUP_BY. On the other hand, we might want to show each of the
    // parts as separate messages on the wearable.
    final int limit = MAX_MESSAGES_IN_WEARABLE_PAGE + 1;

    final List<CharSequence> messages = Lists.newArrayList();
    boolean hasSeenMessagesBeforeNotification = false;
    Cursor convMessageCursor = null;
    try {/*from w  ww  .j a  va 2s  .co m*/
        final DatabaseWrapper db = DataModel.get().getDatabase();

        final String[] queryArgs = { conversationId };
        final String convPageSql = ConversationMessageData.getWearableQuerySql() + " LIMIT " + limit;
        convMessageCursor = db.rawQuery(convPageSql, queryArgs);

        if (convMessageCursor == null || !convMessageCursor.moveToFirst()) {
            return null;
        }
        final ConversationMessageData convMessageData = new ConversationMessageData();

        final HashMap<String, Integer> firstNames = scanFirstNames(conversationId);
        do {
            convMessageData.bind(convMessageCursor);

            final String authorFullName = convMessageData.getSenderFullName();
            final String authorFirstName = convMessageData.getSenderFirstName();
            String text = convMessageData.getText();

            final boolean isSmsPushNotification = convMessageData.getIsMmsNotification();

            // if auto-download was off to show a message to tap to download the message. We
            // might need to get that working again.
            if (isSmsPushNotification && text != null) {
                text = convertHtmlAndStripUrls(text).toString();
            }
            // Skip messages without any content
            if (TextUtils.isEmpty(text) && !convMessageData.hasAttachments()) {
                continue;
            }
            // Track whether there are messages prior to the one(s) shown in the notification.
            if (convMessageData.getIsSeen()) {
                hasSeenMessagesBeforeNotification = true;
            }

            final boolean usedMoreThanOnce = firstNameUsedMoreThanOnce(firstNames, authorFirstName);
            String displayName = usedMoreThanOnce ? authorFullName : authorFirstName;
            if (TextUtils.isEmpty(displayName)) {
                if (convMessageData.getIsIncoming()) {
                    displayName = convMessageData.getSenderDisplayDestination();
                    if (TextUtils.isEmpty(displayName)) {
                        displayName = context.getString(R.string.unknown_sender);
                    }
                } else {
                    displayName = context.getString(R.string.unknown_self_participant);
                }
            }

            Uri attachmentUri = null;
            String attachmentType = null;
            final List<MessagePartData> attachments = convMessageData.getAttachments();
            for (final MessagePartData messagePartData : attachments) {
                // Look for the first attachment that's not the text piece.
                if (!messagePartData.isText()) {
                    attachmentUri = messagePartData.getContentUri();
                    attachmentType = messagePartData.getContentType();
                    break;
                }
            }

            final CharSequence message = BugleNotifications.buildSpaceSeparatedMessage(displayName, text,
                    attachmentUri, attachmentType);
            messages.add(message);

        } while (convMessageCursor.moveToNext());
    } finally {
        if (convMessageCursor != null) {
            convMessageCursor.close();
        }
    }

    // If there is no conversation history prior to what is already visible in the main
    // notification, there's no need to include the conversation log, too.
    final int maxMessagesInNotification = getMaxMessagesInConversationNotification();
    if (!hasSeenMessagesBeforeNotification && messages.size() <= maxMessagesInNotification) {
        return null;
    }

    final SpannableStringBuilder bigText = new SpannableStringBuilder();
    // There is at least 1 message prior to the first one that we're going to show.
    // Indicate this by inserting an ellipsis at the beginning of the conversation log.
    if (convMessageCursor.getCount() == limit) {
        bigText.append(context.getString(R.string.ellipsis) + "\n\n");
        if (messages.size() > MAX_MESSAGES_IN_WEARABLE_PAGE) {
            messages.remove(messages.size() - 1);
        }
    }
    // Messages are sorted in descending timestamp order, so iterate backwards
    // to get them back in ascending order for display purposes.
    for (int i = messages.size() - 1; i >= 0; --i) {
        bigText.append(messages.get(i));
        if (i > 0) {
            bigText.append("\n\n");
        }
    }
    ++participantCount; // Add in myself

    if (participantCount > 2) {
        final SpannableString statusText = new SpannableString(context.getResources()
                .getQuantityString(R.plurals.wearable_participant_count, participantCount, participantCount));
        statusText.setSpan(
                new ForegroundColorSpan(
                        context.getResources().getColor(R.color.wearable_notification_participants_count)),
                0, statusText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        bigText.append("\n\n").append(statusText);
    }

    final NotificationCompat.Builder notifBuilder = new NotificationCompat.Builder(context);
    final NotificationCompat.Style notifStyle = new NotificationCompat.BigTextStyle(notifBuilder)
            .bigText(bigText);
    notifBuilder.setStyle(notifStyle);

    final WearableExtender wearableExtender = new WearableExtender();
    wearableExtender.setStartScrollBottom(true);
    notifBuilder.extend(wearableExtender);

    return notifBuilder.build();
}

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

private static CharSequence formatSenders(Context context, ArrayList<NotificationInfo> senders) {
    final TextAppearanceSpan notificationSenderSpan = new TextAppearanceSpan(context,
            R.style.NotificationPrimaryText);

    String separator = context.getString(R.string.enumeration_comma); // ", "
    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    int len = senders.size();
    for (int i = 0; i < len; i++) {
        if (i > 0) {
            spannableStringBuilder.append(separator);
        }/*from   w w  w.j  a  va2s  .c o m*/
        spannableStringBuilder.append(senders.get(i).mSender.getName());
    }
    spannableStringBuilder.setSpan(notificationSenderSpan, 0, spannableStringBuilder.length(), 0);
    return spannableStringBuilder;
}

From source file:com.juick.android.MessageMenu.java

protected void runActions() {
    if (!isDialogMode()) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        final CharSequence[] items = new CharSequence[menuActions.size()];
        for (int j = 0; j < items.length; j++) {
            items[j] = menuActions.get(j).title;
        }//from  w  w w.ja  v a2  s. c o m
        builder.setItems(items, this);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                ColorsTheme.ColorTheme colorTheme = JuickMessagesAdapter.getColorTheme(activity);
                ColorDrawable divider = new ColorDrawable(
                        colorTheme.getColor(ColorsTheme.ColorKey.COMMON_BACKGROUND, 0xFFFFFFFF));
                alertDialog.getListView().setDivider(divider);
                alertDialog.getListView().setDividerHeight(1);
            }
        });
        alertDialog.show();

        final ListAdapter adapter = alertDialog.getListView().getAdapter();
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
        float menuFontScale = 1;
        try {
            menuFontScale = Float.parseFloat(sp.getString("menuFontScale", "1.0"));
        } catch (Exception e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
        final boolean compressedMenu = sp.getBoolean("compressedMenu", false);
        final boolean singleLineMenu = sp.getBoolean("singleLineMenu", false);
        final float finalMenuFontScale = menuFontScale;
        final int screenHeight = activity.getWindow().getWindowManager().getDefaultDisplay().getHeight();
        alertDialog.getListView().setAdapter(new ListAdapter() {
            @Override
            public boolean areAllItemsEnabled() {
                return adapter.areAllItemsEnabled();
            }

            @Override
            public boolean isEnabled(int position) {
                return adapter.isEnabled(position);
            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                adapter.registerDataSetObserver(observer);
            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                adapter.unregisterDataSetObserver(observer);
            }

            @Override
            public int getCount() {
                return items.length;
            }

            @Override
            public Object getItem(int position) {
                return adapter.getItem(position);
            }

            @Override
            public long getItemId(int position) {
                return adapter.getItemId(position);
            }

            @Override
            public boolean hasStableIds() {
                return adapter.hasStableIds();
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                View retval = adapter.getView(position, null, parent);
                if (retval instanceof TextView) {
                    TextView tv = (TextView) retval;
                    if (compressedMenu) {
                        int minHeight = (int) ((screenHeight * 0.7) / getCount());
                        tv.setMinHeight(minHeight);
                        tv.setMinimumHeight(minHeight);
                    }
                    if (singleLineMenu) {
                        tv.setSingleLine(true);
                        tv.setEllipsize(TextUtils.TruncateAt.MIDDLE);
                    }
                    tv.setTextSize(22 * finalMenuFontScale);
                }
                return retval;
            }

            @Override
            public int getItemViewType(int position) {
                return adapter.getItemViewType(position);
            }

            @Override
            public int getViewTypeCount() {
                return adapter.getViewTypeCount();
            }

            @Override
            public boolean isEmpty() {
                return adapter.isEmpty();
            }
        });
    } else {
        AlertDialog.Builder builder = new AlertDialog.Builder(
                new ContextThemeWrapper(activity, R.style.Theme_Sherlock));
        View dialogView = activity.getLayoutInflater().inflate(R.layout.message_menu2, null);
        builder.setView(dialogView);
        builder.setCancelable(true);
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        View scrollView = dialogView.findViewById(R.id.scrollView);
        scrollView.getLayoutParams().width = (int) (width * 0.90);
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                //MainActivity.restyleChildrenOrWidget(alertDialog.getWindow().getDecorView());
            }
        });
        TextView messageNo = (TextView) dialogView.findViewById(R.id.message_no);
        messageNo.setText(listSelectedItem.getDisplayMessageNo());
        Spinner openUrl = (Spinner) dialogView.findViewById(R.id.open_url);
        Button singleURL = (Button) dialogView.findViewById(R.id.single_url);
        if (urls != null && urls.size() == 1) {
            singleURL.setVisibility(View.VISIBLE);
            openUrl.setVisibility(View.GONE);
            SpannableStringBuilder sb = new SpannableStringBuilder();
            sb.append(urls.get(0));
            sb.setSpan(new UnderlineSpan(), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            singleURL.setText(sb);
            singleURL.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alertDialog.dismiss();
                    launchURL(listSelectedItem.getMID(), urls.get(0));
                }
            });
        } else if (urls != null && urls.size() > 0) {
            singleURL.setVisibility(View.GONE);
            openUrl.setVisibility(View.VISIBLE);
            openUrl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position != 0) {
                        alertDialog.dismiss();
                        launchURL(listSelectedItem.getMID(), urls.get(position));
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });
            urls.add(0, activity.getString(R.string.ClickToSelectURL));
            openUrl.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return urls.size();
                }

                @Override
                public Object getItem(int position) {
                    return position;
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    View rowView = activity.getLayoutInflater().inflate(android.R.layout.simple_list_item_1,
                            null);
                    TextView textView = (TextView) rowView.findViewById(android.R.id.text1);
                    textView.setSingleLine(false);
                    textView.setMaxLines(5);
                    SpannableStringBuilder sb = new SpannableStringBuilder();
                    sb.append(urls.get(position));
                    if (position == 0) {
                        textView.setTextColor(0xFF808080);
                    } else {
                        sb.setSpan(new UnderlineSpan(), 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                    textView.setText(sb);
                    return rowView;
                }
            });
        } else {
            openUrl.setVisibility(View.GONE);
            singleURL.setVisibility(View.GONE);
        }
        final String UName = listSelectedItem.User.UName;
        View recommendMessage = dialogView.findViewById(R.id.recommend_message);
        View deleteMessage = dialogView.findViewById(R.id.delete_message);
        View saveMessage = dialogView.findViewById(R.id.save_message);
        View unsaveMessage = dialogView.findViewById(R.id.unsave_message);
        //View subscribeUser = dialogView.findViewById(R.id.subscribe_user);
        View subscribeMessage = dialogView.findViewById(R.id.subscribe_message);
        //View unsubscribeUser = dialogView.findViewById(R.id.unsubscribe_user);
        View unsubscribeMessage = dialogView.findViewById(R.id.unsubscribe_message);
        View translateMessage = dialogView.findViewById(R.id.translate_message);
        View shareMessage = dialogView.findViewById(R.id.share_message);
        //View blacklistUser = dialogView.findViewById(R.id.blacklist_user);
        //View filterUser = dialogView.findViewById(R.id.filter_user);
        //View userBlog = dialogView.findViewById(R.id.user_blog);
        //View userStats = dialogView.findViewById(R.id.user_stats);
        View openMessageInBrowser = dialogView.findViewById(R.id.open_message_in_browser);
        Button userCenter = (Button) dialogView.findViewById(R.id.user_center);
        if (null == dialogView.findViewById(R.id.column_3)) {
            // only for portrait
            userCenter.setText("@" + listSelectedItem.User.UName + " " + userCenter.getText());
        }

        unsubscribeMessage.setEnabled(listSelectedItem.getRID() == 0);
        subscribeMessage.setEnabled(listSelectedItem.getRID() == 0);
        unsaveMessage.setEnabled(listSelectedItem.getRID() == 0);
        recommendMessage.setEnabled(listSelectedItem.getRID() == 0);

        if (UName.equalsIgnoreCase(JuickAPIAuthorizer.getJuickAccountName(activity.getApplicationContext()))) {
            recommendMessage.setVisibility(View.GONE);
        } else {
            deleteMessage.setVisibility(View.GONE);
        }
        if (messagesSource instanceof SavedMessagesSource) {
            saveMessage.setVisibility(View.GONE);
        } else {
            unsaveMessage.setVisibility(View.GONE);
        }
        recommendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionRecommendMessage();
            }
        });
        deleteMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionDeleteMessage();
            }
        });
        saveMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionSaveMessage();
            }
        });
        unsaveMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUnsaveMessage();
            }
        });
        //            subscribeUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionSubscribeUser();
        //                }
        //            });
        subscribeMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionSubscribeMessage();
            }
        });
        //            unsubscribeUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUnsubscribeUser();
        //                }
        //            });
        unsubscribeMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUnsubscribeMessage();
            }
        });
        translateMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionTranslateMessage();
            }
        });
        shareMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionShareMessage();
            }
        });
        //            blacklistUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionBlacklistUser();
        //                }
        //            });
        //            filterUser.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionFilterUser(UName);
        //                }
        //            });
        //            userBlog.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUserBlog();
        //                }
        //            });
        //            userStats.setOnClickListener(new View.OnClickListener() {
        //                @Override
        //                public void onClick(View v) {
        //                    alertDialog.dismiss();
        //                    actionUserStats();
        //                }
        //            });
        openMessageInBrowser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionOpenMessageInBrowser();
            }
        });
        userCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
                actionUserCenter();
            }
        });
        completeInitDialogMode(alertDialog, dialogView);
        alertDialog.show();
    }
}

From source file:com.csipsimple.ui.incall.CallActivity.java

/**
 * Controls buttons (accept, reject etc).
 * @param whichAction what action has been done
 * @param call//ww  w . ja va2 s  .  c  om
 */
@Override
public void onTrigger(int whichAction, final SipCallSession call) {

    // Sanity check for actions requiring valid call id
    if (whichAction == TAKE_CALL || whichAction == REJECT_CALL || whichAction == DONT_TAKE_CALL
            || whichAction == TERMINATE_CALL || whichAction == DETAILED_DISPLAY || whichAction == TOGGLE_HOLD
            || whichAction == START_RECORDING || whichAction == STOP_RECORDING || whichAction == DTMF_DISPLAY
            || whichAction == XFER_CALL || whichAction == TRANSFER_CALL || whichAction == START_VIDEO
            || whichAction == STOP_VIDEO) {
        // We check that current call is valid for any actions
        if (call == null) {
            Log.e(TAG, "Try to do an action on a null call !!!");
            return;
        }
        if (call.getCallId() == SipCallSession.INVALID_CALL_ID) {
            Log.e(TAG, "Try to do an action on an invalid call !!!");
            return;
        }
    }

    // Reset proximity sensor timer
    proximityManager.restartTimer();

    try {
        switch (whichAction) {
        case TAKE_CALL: {
            if (service != null) {
                Log.i(TAG, "Answering call " + call.getCallId());

                boolean shouldHoldOthers = false;

                // Well actually we should be always before confirmed
                if (call.isBeforeConfirmed()) {
                    shouldHoldOthers = true;
                }

                service.answer(call.getCallId(), StatusCode.OK);

                // if it's a ringing call, we assume that user wants to
                // hold other calls
                if (shouldHoldOthers && callsInfo != null) {
                    for (SipCallSession callInfo : callsInfo) {
                        // For each active and running call
                        if (SipCallSession.InvState.CONFIRMED == callInfo.getCallState()
                                && !callInfo.isLocalHeld() && callInfo.getCallId() != call.getCallId()) {

                            Log.d(TAG, "Hold call " + callInfo.getCallId());
                            service.hold(callInfo.getCallId());

                        }
                    }
                }
            }
            break;
        }
        case DONT_TAKE_CALL: {
            if (service != null) {
                Log.i(TAG, "Rejecting the call with BUSY_HERE");
                service.hangup(call.getCallId(), StatusCode.BUSY_HERE);
            }
            break;
        }
        case REJECT_CALL:
            if (service != null) {
                Log.i(TAG, "Rejecting the call");
                service.hangup(call.getCallId(), 0);
            }
            break;
        case TERMINATE_CALL: {
            if (service != null) {
                Log.i(TAG, "Terminating the call");
                service.hangup(call.getCallId(), 0);
            }
            break;
        }
        case CANCEL_CALL: {
            if (service != null) {
                Log.i(TAG, "Cancelling the call");
                service.hangup(call.getCallId(), 0);
            }
            sendCancelCallBroadcast();
            break;
        }
        case MUTE_ON:
        case MUTE_OFF: {
            if (service != null) {
                Log.i(TAG, "Set mute " + (whichAction == MUTE_ON));
                service.setMicrophoneMute((whichAction == MUTE_ON) ? true : false);
            }
            break;
        }
        case SPEAKER_ON:
        case SPEAKER_OFF: {
            if (service != null) {
                Log.d(TAG, "Set speaker " + (whichAction == SPEAKER_ON));
                useAutoDetectSpeaker = false;
                service.setSpeakerphoneOn((whichAction == SPEAKER_ON) ? true : false);
            }
            break;
        }
        case BLUETOOTH_ON:
        case BLUETOOTH_OFF: {
            if (service != null) {
                Log.d(TAG, "Set bluetooth " + (whichAction == BLUETOOTH_ON));
                service.setBluetoothOn((whichAction == BLUETOOTH_ON) ? true : false);
            }
            break;
        }
        case DTMF_DISPLAY: {
            Log.d(TAG, "DTMF_DISPLAY");
            showDialpad(call.getCallId());
            break;
        }
        case DETAILED_DISPLAY: {
            if (service != null) {
                if (infoDialog != null) {
                    infoDialog.dismiss();
                }
                String infos = service.showCallInfosDialog(call.getCallId());
                String natType = service.getLocalNatType();
                SpannableStringBuilder buf = new SpannableStringBuilder();
                Builder builder = new Builder(this);

                buf.append(infos);
                if (!TextUtils.isEmpty(natType)) {
                    buf.append("\r\nLocal NAT type detected : ");
                    buf.append(natType);
                }
                TextAppearanceSpan textSmallSpan = new TextAppearanceSpan(this,
                        android.R.style.TextAppearance_Small);
                buf.setSpan(textSmallSpan, 0, buf.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

                infoDialog = builder.setIcon(android.R.drawable.ic_dialog_info).setMessage(buf)
                        .setNeutralButton(R.string.ok, null).create();
                infoDialog.show();
            }
            break;
        }
        case TOGGLE_HOLD: {
            if (service != null) {
                // Log.d(TAG,
                // "Current state is : "+callInfo.getCallState().name()+" / "+callInfo.getMediaStatus().name());
                if (call.getMediaStatus() == SipCallSession.MediaState.LOCAL_HOLD
                        || call.getMediaStatus() == SipCallSession.MediaState.NONE) {
                    service.reinvite(call.getCallId(), true);
                } else {
                    service.hold(call.getCallId());
                }
            }
            break;
        }
        case MEDIA_SETTINGS: {
            startActivity(new Intent(this, InCallMediaControl.class));
            break;
        }
        case XFER_CALL: {
            Intent pickupIntent = new Intent(this, PickupSipUri.class);
            pickupIntent.putExtra(CALL_ID, call.getCallId());
            startActivityForResult(pickupIntent, PICKUP_SIP_URI_XFER);
            break;
        }
        case TRANSFER_CALL: {
            final ArrayList<SipCallSession> remoteCalls = new ArrayList<SipCallSession>();
            if (callsInfo != null) {
                for (SipCallSession remoteCall : callsInfo) {
                    // Verify not current call
                    if (remoteCall.getCallId() != call.getCallId() && remoteCall.isOngoing()) {
                        remoteCalls.add(remoteCall);
                    }
                }
            }

            if (remoteCalls.size() > 0) {
                Builder builder = new Builder(this);
                CharSequence[] simpleAdapter = new String[remoteCalls.size()];
                for (int i = 0; i < remoteCalls.size(); i++) {
                    simpleAdapter[i] = remoteCalls.get(i).getRemoteContact();
                }
                builder.setSingleChoiceItems(simpleAdapter, -1, new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (service != null) {
                            try {
                                // 1 = PJSUA_XFER_NO_REQUIRE_REPLACES
                                service.xferReplace(call.getCallId(), remoteCalls.get(which).getCallId(), 1);
                            } catch (RemoteException e) {
                                Log.e(TAG, "Was not able to call service method", e);
                            }
                        }
                        dialog.dismiss();
                    }
                }).setCancelable(true).setNeutralButton(R.string.cancel, new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).show();
            }

            break;
        }
        case ADD_CALL: {
            Intent pickupIntent = new Intent(this, PickupSipUri.class);
            startActivityForResult(pickupIntent, PICKUP_SIP_URI_NEW_CALL);
            break;
        }
        case START_RECORDING: {
            if (service != null) {
                // TODO : add a tweaky setting for two channel recording in different files.
                // Would just result here in two calls to start recording with different bitmask
                service.startRecording(call.getCallId(), SipManager.BITMASK_ALL);
            }
            break;
        }
        case STOP_RECORDING: {
            if (service != null) {
                service.stopRecording(call.getCallId());
            }
            break;
        }
        case START_VIDEO:
        case STOP_VIDEO: {
            if (service != null) {
                Bundle opts = new Bundle();
                opts.putBoolean(SipCallSession.OPT_CALL_VIDEO, whichAction == START_VIDEO);
                service.updateCallOptions(call.getCallId(), opts);
            }
            break;
        }
        case ZRTP_TRUST: {
            if (service != null) {
                service.zrtpSASVerified(call.getCallId());
            }
            break;
        }
        case ZRTP_REVOKE: {
            if (service != null) {
                service.zrtpSASRevoke(call.getCallId());
            }
            break;
        }
        }
    } catch (RemoteException e) {
        Log.e(TAG, "Was not able to call service method", e);
    }
}

From source file:com.android.messaging.datamodel.MessageNotificationState.java

/**
 * Performs a query on the database.//from w  w  w  .j a va2 s.  c  o m
 */
private static ConversationInfoList createConversationInfoList() {
    // Map key is conversation id. We use LinkedHashMap to ensure that entries are iterated in
    // the same order they were originally added. We scan unseen messages from newest to oldest,
    // so the corresponding conversations are added in that order, too.
    final Map<String, ConversationLineInfo> convLineInfos = new LinkedHashMap<>();
    int messageCount = 0;

    Cursor convMessageCursor = null;
    try {
        final Context context = Factory.get().getApplicationContext();
        final DatabaseWrapper db = DataModel.get().getDatabase();

        convMessageCursor = db.rawQuery(ConversationMessageData.getNotificationQuerySql(), null);

        if (convMessageCursor != null && convMessageCursor.moveToFirst()) {
            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "MessageNotificationState: Found unseen message notifications.");
            }
            final ConversationMessageData convMessageData = new ConversationMessageData();

            HashMap<String, Integer> firstNames = null;
            String conversationIdForFirstNames = null;
            String groupConversationName = null;
            final int maxMessages = getMaxMessagesInConversationNotification();

            do {
                convMessageData.bind(convMessageCursor);

                // First figure out if this is a valid message.
                String authorFullName = convMessageData.getSenderFullName();
                String authorFirstName = convMessageData.getSenderFirstName();
                final String messageText = convMessageData.getText();

                final String convId = convMessageData.getConversationId();
                final String messageId = convMessageData.getMessageId();

                CharSequence text = messageText;
                final boolean isManualDownloadNeeded = convMessageData.getIsMmsNotification();
                if (isManualDownloadNeeded) {
                    // Don't try and convert the text from html if it's sms and not a sms push
                    // notification.
                    Assert.equals(MessageData.BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD,
                            convMessageData.getStatus());
                    text = context.getResources().getString(R.string.message_title_manual_download);
                }
                ConversationLineInfo currConvInfo = convLineInfos.get(convId);
                if (currConvInfo == null) {
                    final ConversationListItemData convData = ConversationListItemData
                            .getExistingConversation(db, convId);
                    if (!convData.getNotificationEnabled()) {
                        // Skip conversations that have notifications disabled.
                        continue;
                    }
                    final int subId = BugleDatabaseOperations.getSelfSubscriptionId(db, convData.getSelfId());
                    groupConversationName = convData.getName();
                    final Uri avatarUri = AvatarUriUtil.createAvatarUri(
                            convMessageData.getSenderProfilePhotoUri(), convMessageData.getSenderFullName(),
                            convMessageData.getSenderNormalizedDestination(),
                            convMessageData.getSenderContactLookupKey());
                    currConvInfo = new ConversationLineInfo(convId, convData.getIsGroup(),
                            groupConversationName, convData.getIncludeEmailAddress(),
                            convMessageData.getReceivedTimeStamp(), convData.getSelfId(),
                            convData.getNotificationSoundUri(), convData.getNotificationEnabled(),
                            convData.getNotifiationVibrate(), avatarUri,
                            convMessageData.getSenderContactLookupUri(), subId, convData.getParticipantCount());
                    convLineInfos.put(convId, currConvInfo);
                }
                // Prepare the message line
                if (currConvInfo.mTotalMessageCount < maxMessages) {
                    if (currConvInfo.mIsGroup) {
                        if (authorFirstName == null) {
                            // authorFullName might be null as well. In that case, we won't
                            // show an author. That is better than showing all the group
                            // names again on the 2nd line.
                            authorFirstName = authorFullName;
                        }
                    } else {
                        // don't recompute this if we don't need to
                        if (!TextUtils.equals(conversationIdForFirstNames, convId)) {
                            firstNames = scanFirstNames(convId);
                            conversationIdForFirstNames = convId;
                        }
                        if (firstNames != null) {
                            final Integer count = firstNames.get(authorFirstName);
                            if (count != null && count > 1) {
                                authorFirstName = authorFullName;
                            }
                        }

                        if (authorFullName == null) {
                            authorFullName = groupConversationName;
                        }
                        if (authorFirstName == null) {
                            authorFirstName = groupConversationName;
                        }
                    }
                    final String subjectText = MmsUtils.cleanseMmsSubject(context.getResources(),
                            convMessageData.getMmsSubject());
                    if (!TextUtils.isEmpty(subjectText)) {
                        final String subjectLabel = context.getString(R.string.subject_label);
                        final SpannableStringBuilder spanBuilder = new SpannableStringBuilder();

                        spanBuilder.append(
                                context.getString(R.string.notification_subject, subjectLabel, subjectText));
                        spanBuilder.setSpan(new TextAppearanceSpan(context, R.style.NotificationSubjectText), 0,
                                subjectLabel.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        if (!TextUtils.isEmpty(text)) {
                            // Now add the actual message text below the subject header.
                            spanBuilder.append(System.getProperty("line.separator") + text);
                        }
                        text = spanBuilder;
                    }
                    // If we've got attachments, find the best one. If one of the messages is
                    // a photo, save the url so we'll display a big picture notification.
                    // Otherwise, show the first one we find.
                    Uri attachmentUri = null;
                    String attachmentType = null;
                    final MessagePartData messagePartData = getMostInterestingAttachment(convMessageData);
                    if (messagePartData != null) {
                        attachmentUri = messagePartData.getContentUri();
                        attachmentType = messagePartData.getContentType();
                    }
                    currConvInfo.mLineInfos
                            .add(new MessageLineInfo(currConvInfo.mIsGroup, authorFullName, authorFirstName,
                                    text, attachmentUri, attachmentType, isManualDownloadNeeded, messageId));
                }
                messageCount++;
                currConvInfo.mTotalMessageCount++;
            } while (convMessageCursor.moveToNext());
        }
    } finally {
        if (convMessageCursor != null) {
            convMessageCursor.close();
        }
    }
    if (convLineInfos.isEmpty()) {
        return null;
    } else {
        return new ConversationInfoList(messageCount, Lists.newLinkedList(convLineInfos.values()));
    }
}

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

/**
 * updateNotification is *the* main function for building the actual notification handed to
 * the NotificationManager//from  w ww.j  av a  2 s .c o  m
 * @param context
 * @param isNew if we've got a new message, show the ticker
 * @param uniqueThreadCount
 */
private static void updateNotification(Context context, boolean isNew, int uniqueThreadCount) {
    // TDH
    Log.d("NotificationDebug", "updateNotification()");

    // If the user has turned off notifications in settings, don't do any notifying.
    if (!MessagingPreferenceActivity.getNotificationEnabled(context)) {
        if (DEBUG) {
            Log.d(TAG, "updateNotification: notifications turned off in prefs, bailing");
        }
        // TDH
        Log.d("NotificationDebug", "Notifications not enabled!");

        return;
    }

    // Figure out what we've got -- whether all sms's, mms's, or a mixture of both.
    int messageCount = sNotificationSet.size();

    // TDH:
    Log.d("NotificationDebug", "messageCount: " + messageCount);
    if (messageCount == 0) {
        Log.d("NotificationDebug", "WTF. Should have at least one message.");
        return;
    }

    NotificationInfo mostRecentNotification = sNotificationSet.first();

    // TDH: Use NotificationCompat2 (and in other places but it is obvious where).
    final NotificationCompat2.Builder noti = new NotificationCompat2.Builder(context)
            .setWhen(mostRecentNotification.mTimeMillis);

    if (isNew) {
        noti.setTicker(mostRecentNotification.mTicker);
    }
    // TDH
    Log.d("NotificationDebug", "Creating TaskStackBuilder");

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    // TDH
    Log.d("NotificationDebug", "Created TaskStackBuilder. UniqueThreadCount: " + uniqueThreadCount);

    // If we have more than one unique thread, change the title (which would
    // normally be the contact who sent the message) to a generic one that
    // makes sense for multiple senders, and change the Intent to take the
    // user to the conversation list instead of the specific thread.

    // Cases:
    //   1) single message from single thread - intent goes to ComposeMessageActivity
    //   2) multiple messages from single thread - intent goes to ComposeMessageActivity
    //   3) messages from multiple threads - intent goes to ConversationList

    final Resources res = context.getResources();
    String title = null;
    Bitmap avatar = null;
    if (uniqueThreadCount > 1) { // messages from multiple threads
        Intent mainActivityIntent = new Intent(Intent.ACTION_MAIN);

        mainActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        mainActivityIntent.setType("vnd.android-dir/mms-sms");
        taskStackBuilder.addNextIntent(mainActivityIntent);
        title = context.getString(R.string.message_count_notification, messageCount);
    } else { // same thread, single or multiple messages
        title = mostRecentNotification.mTitle;
        BitmapDrawable contactDrawable = (BitmapDrawable) mostRecentNotification.mSender.getAvatar(context,
                null);
        if (contactDrawable != null) {
            // Show the sender's avatar as the big icon. Contact bitmaps are 96x96 so we
            // have to scale 'em up to 128x128 to fill the whole notification large icon.
            avatar = contactDrawable.getBitmap();
            if (avatar != null) {
                final int idealIconHeight = res
                        .getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
                final int idealIconWidth = res
                        .getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
                if (avatar.getHeight() < idealIconHeight) {
                    // Scale this image to fit the intended size
                    avatar = Bitmap.createScaledBitmap(avatar, idealIconWidth, idealIconHeight, true);
                }
                if (avatar != null) {
                    noti.setLargeIcon(avatar);
                }
            }
        }

        taskStackBuilder.addParentStack(ComposeMessageActivity.class);
        taskStackBuilder.addNextIntent(mostRecentNotification.mClickIntent);
    }

    // TDH
    Log.d("NotificationDebug", "title: " + title);

    // Always have to set the small icon or the notification is ignored
    noti.setSmallIcon(R.drawable.stat_notify_sms);

    NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    // Update the notification.
    noti.setContentTitle(title)
            .setContentIntent(taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
    // TDH: Can't do these yet.
    //            .addKind(Notification.KIND_MESSAGE)
    //            .setPriority(Notification.PRIORITY_DEFAULT);     // TODO: set based on contact coming
    //                                                             // from a favorite.

    int defaults = 0;

    if (isNew) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String vibrateWhen;
        if (sp.contains(MessagingPreferenceActivity.NOTIFICATION_VIBRATE_WHEN)) {
            vibrateWhen = sp.getString(MessagingPreferenceActivity.NOTIFICATION_VIBRATE_WHEN, null);
        } else if (sp.contains(MessagingPreferenceActivity.NOTIFICATION_VIBRATE)) {
            vibrateWhen = sp.getBoolean(MessagingPreferenceActivity.NOTIFICATION_VIBRATE, false)
                    ? context.getString(R.string.prefDefault_vibrate_true)
                    : context.getString(R.string.prefDefault_vibrate_false);
        } else {
            vibrateWhen = context.getString(R.string.prefDefault_vibrateWhen);
        }

        boolean vibrateAlways = vibrateWhen.equals("always");
        boolean vibrateSilent = vibrateWhen.equals("silent");
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        boolean nowSilent = audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE;

        if (vibrateAlways || vibrateSilent && nowSilent) {
            defaults |= Notification.DEFAULT_VIBRATE;
        }

        String ringtoneStr = sp.getString(MessagingPreferenceActivity.NOTIFICATION_RINGTONE, null);
        noti.setSound(TextUtils.isEmpty(ringtoneStr) ? null : Uri.parse(ringtoneStr));
        if (DEBUG) {
            Log.d(TAG, "updateNotification: new message, adding sound to the notification");
        }
    }

    defaults |= Notification.DEFAULT_LIGHTS;

    noti.setDefaults(defaults);

    // set up delete intent
    noti.setDeleteIntent(PendingIntent.getBroadcast(context, 0, sNotificationOnDeleteIntent, 0));

    final Notification notification;

    if (messageCount == 1) {
        // We've got a single message

        // TDH
        Log.d("NotificationDebug",
                "Single message, with text: " + mostRecentNotification.formatBigMessage(context));

        // This sets the text for the collapsed form:
        noti.setContentText(mostRecentNotification.formatBigMessage(context));

        if (mostRecentNotification.mAttachmentBitmap != null) {
            // The message has a picture, show that

            notification = new NotificationCompat2.BigPictureStyle(noti)
                    .bigPicture(mostRecentNotification.mAttachmentBitmap)
                    // This sets the text for the expanded picture form:
                    .setSummaryText(mostRecentNotification.formatPictureMessage(context)).build();
        } else {
            // Show a single notification -- big style with the text of the whole message
            notification = new NotificationCompat2.BigTextStyle(noti)
                    .bigText(mostRecentNotification.formatBigMessage(context)).build();
        }
    } else {
        // We've got multiple messages
        if (uniqueThreadCount == 1) {
            // We've got multiple messages for the same thread.
            // Starting with the oldest new message, display the full text of each message.
            // Begin a line for each subsequent message.
            SpannableStringBuilder buf = new SpannableStringBuilder();
            NotificationInfo infos[] = sNotificationSet.toArray(new NotificationInfo[sNotificationSet.size()]);
            int len = infos.length;
            for (int i = len - 1; i >= 0; i--) {
                NotificationInfo info = infos[i];

                buf.append(info.formatBigMessage(context));

                if (i != 0) {
                    buf.append('\n');
                }
            }

            noti.setContentText(context.getString(R.string.message_count_notification, messageCount));

            // Show a single notification -- big style with the text of all the messages
            notification = new NotificationCompat2.BigTextStyle(noti).bigText(buf)
                    // Forcibly show the last line, with the app's smallIcon in it, if we 
                    // kicked the smallIcon out with an avatar bitmap
                    .setSummaryText((avatar == null) ? null : " ").build();
        } else {
            // Build a set of the most recent notification per threadId.
            HashSet<Long> uniqueThreads = new HashSet<Long>(sNotificationSet.size());
            ArrayList<NotificationInfo> mostRecentNotifPerThread = new ArrayList<NotificationInfo>();
            Iterator<NotificationInfo> notifications = sNotificationSet.iterator();
            while (notifications.hasNext()) {
                NotificationInfo notificationInfo = notifications.next();
                if (!uniqueThreads.contains(notificationInfo.mThreadId)) {
                    uniqueThreads.add(notificationInfo.mThreadId);
                    mostRecentNotifPerThread.add(notificationInfo);
                }
            }
            // When collapsed, show all the senders like this:
            //     Fred Flinstone, Barry Manilow, Pete...
            noti.setContentText(formatSenders(context, mostRecentNotifPerThread));
            NotificationCompat2.InboxStyle inboxStyle = new NotificationCompat2.InboxStyle(noti);

            // We have to set the summary text to non-empty so the content text doesn't show
            // up when expanded.
            inboxStyle.setSummaryText(" ");

            // At this point we've got multiple messages in multiple threads. We only
            // want to show the most recent message per thread, which are in
            // mostRecentNotifPerThread.
            int uniqueThreadMessageCount = mostRecentNotifPerThread.size();
            int maxMessages = Math.min(MAX_MESSAGES_TO_SHOW, uniqueThreadMessageCount);

            for (int i = 0; i < maxMessages; i++) {
                NotificationInfo info = mostRecentNotifPerThread.get(i);
                inboxStyle.addLine(info.formatInboxMessage(context));
            }
            notification = inboxStyle.build();
            if (DEBUG) {
                Log.d(TAG, "updateNotification: multi messages," + " showing inboxStyle notification");
            }
        }
    }

    // TDH
    Log.d("NotificationDebug", "Showing notification: " + notification);

    nm.notify(NOTIFICATION_ID, notification);
}

From source file:com.android.mail.browse.ConversationItemView.java

SpannableStringBuilder elideParticipants(List<SpannableString> parts) {
    final SpannableStringBuilder builder = new SpannableStringBuilder();
    float totalWidth = 0;
    boolean ellipsize = false;
    float width;/*w  w w .j ava 2 s  .c o m*/
    boolean skipToHeader = false;

    // start with "To: " if we're showing recipients
    if (mDisplayedFolder.shouldShowRecipients() && !parts.isEmpty()) {
        final SpannableString toHeader = SendersView.getFormattedToHeader();
        CharacterStyle[] spans = toHeader.getSpans(0, toHeader.length(), CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        totalWidth += sPaint.measureText(toHeader.toString());
        builder.append(toHeader);
        skipToHeader = true;
    }

    final SpannableStringBuilder messageInfoString = mHeader.messageInfoString;
    if (!TextUtils.isEmpty(messageInfoString)) {
        CharacterStyle[] spans = messageInfoString.getSpans(0, messageInfoString.length(),
                CharacterStyle.class);
        // There is only 1 character style span; make sure we apply all the
        // styles to the paint object before measuring.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // Paint the message info string to see if we lose space.
        float messageInfoWidth = sPaint.measureText(messageInfoString.toString());
        totalWidth += messageInfoWidth;
    }
    SpannableString prevSender = null;
    SpannableString ellipsizedText;
    for (SpannableString sender : parts) {
        // There may be null sender strings if there were dupes we had to remove.
        if (sender == null) {
            continue;
        }
        // No more width available, we'll only show fixed fragments.
        if (ellipsize) {
            break;
        }
        CharacterStyle[] spans = sender.getSpans(0, sender.length(), CharacterStyle.class);
        // There is only 1 character style span.
        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // If there are already senders present in this string, we need to
        // make sure we prepend the dividing token
        if (SendersView.sElidedString.equals(sender.toString())) {
            sender = copyStyles(spans, sElidedPaddingToken + sender + sElidedPaddingToken);
        } else if (!skipToHeader && builder.length() > 0
                && (prevSender == null || !SendersView.sElidedString.equals(prevSender.toString()))) {
            sender = copyStyles(spans, sSendersSplitToken + sender);
        } else {
            skipToHeader = false;
        }
        prevSender = sender;

        if (spans.length > 0) {
            spans[0].updateDrawState(sPaint);
        }
        // Measure the width of the current sender and make sure we have space
        width = (int) sPaint.measureText(sender.toString());
        if (width + totalWidth > mSendersWidth) {
            // The text is too long, new line won't help. We have to
            // ellipsize text.
            ellipsize = true;
            width = mSendersWidth - totalWidth; // ellipsis width?
            ellipsizedText = copyStyles(spans, TextUtils.ellipsize(sender, sPaint, width, TruncateAt.END));
            width = (int) sPaint.measureText(ellipsizedText.toString());
        } else {
            ellipsizedText = null;
        }
        totalWidth += width;

        final CharSequence fragmentDisplayText;
        if (ellipsizedText != null) {
            fragmentDisplayText = ellipsizedText;
        } else {
            fragmentDisplayText = sender;
        }
        builder.append(fragmentDisplayText);
    }
    mHeader.styledMessageInfoStringOffset = builder.length();
    if (!TextUtils.isEmpty(messageInfoString)) {
        builder.append(messageInfoString);
    }
    return builder;
}

From source file:com.fa.mastodon.activity.ComposeActivity.java

private void onUploadSuccess(final QueuedMedia item, Media media) {
    item.id = media.id;/*from  w  ww.  j  a  v a2  s. co  m*/

    /* Add the upload URL to the text field. Also, keep a reference to the span so if the user
     * chooses to remove the media, the URL is also automatically removed. */
    item.uploadUrl = new URLSpan(media.textUrl);
    int end = 1 + media.textUrl.length();
    SpannableStringBuilder builder = new SpannableStringBuilder();
    builder.append(' ');
    builder.append(media.textUrl);
    builder.setSpan(item.uploadUrl, 0, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textEditor.append(builder);

    waitForMediaLatch.countDown();
}

From source file:com.android.messaging.datamodel.BugleNotifications.java

static CharSequence formatInboxMessage(final String sender, final CharSequence message, final Uri attachmentUri,
        final String attachmentType) {
    final Context context = Factory.get().getApplicationContext();
    final TextAppearanceSpan notificationSenderSpan = new TextAppearanceSpan(context,
            R.style.NotificationSenderText);

    final TextAppearanceSpan notificationTertiaryText = new TextAppearanceSpan(context,
            R.style.NotificationTertiaryText);

    final SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
    if (!TextUtils.isEmpty(sender)) {
        spannableStringBuilder.append(sender);
        spannableStringBuilder.setSpan(notificationSenderSpan, 0, sender.length(), 0);
    }/*from www .  j a  v  a 2 s .  c  om*/
    final String separator = context.getString(R.string.notification_separator);

    if (!TextUtils.isEmpty(message)) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        final int start = spannableStringBuilder.length();
        spannableStringBuilder.append(message);
        spannableStringBuilder.setSpan(notificationTertiaryText, start, start + message.length(), 0);
    }
    if (attachmentUri != null) {
        if (spannableStringBuilder.length() > 0) {
            spannableStringBuilder.append(separator);
        }
        spannableStringBuilder.append(formatAttachmentTag(null, attachmentType));
    }
    return spannableStringBuilder;
}