Example usage for android.text SpannableString SpannableString

List of usage examples for android.text SpannableString SpannableString

Introduction

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

Prototype

public SpannableString(CharSequence source) 

Source Link

Document

For the backward compatibility reasons, this constructor copies all spans including android.text.NoCopySpan .

Usage

From source file:im.neon.activity.VectorRoomActivity.java

/**
 * Refresh the notifications area.//www.  j  av  a  2 s .  c  om
 */
private void refreshNotificationsArea() {
    // sanity check
    // might happen when the application is logged out
    if ((null == mSession.getDataHandler()) || (null == mRoom) || (null != sRoomPreviewData)) {
        return;
    }

    int iconId = -1;
    int textColor = -1;
    boolean isAreaVisible = false;
    SpannableString text = new SpannableString("");
    boolean hasUnsentEvent = false;

    // remove any listeners
    mNotificationTextView.setOnClickListener(null);
    mNotificationIconImageView.setOnClickListener(null);

    //  no network
    if (!Matrix.getInstance(this).isConnected()) {
        isAreaVisible = true;
        iconId = R.drawable.error;
        textColor = R.color.vector_fuchsia_color;
        text = new SpannableString(getResources().getString(R.string.room_offline_notification));
    } else {
        List<Event> undeliveredEvents = mSession.getDataHandler().getStore()
                .getUndeliverableEvents(mRoom.getRoomId());
        List<Event> unknownDeviceEvents = mSession.getDataHandler().getStore()
                .getUnknownDeviceEvents(mRoom.getRoomId());

        boolean hasUndeliverableEvents = (null != undeliveredEvents) && (undeliveredEvents.size() > 0);
        boolean hasUnknownDeviceEvents = (null != unknownDeviceEvents) && (unknownDeviceEvents.size() > 0);

        if (hasUndeliverableEvents || hasUnknownDeviceEvents) {
            hasUnsentEvent = true;
            isAreaVisible = true;
            iconId = R.drawable.error;

            String cancelAll = getResources().getString(R.string.room_prompt_cancel);
            String resendAll = getResources().getString(R.string.room_prompt_resend);
            String message = getResources()
                    .getString(hasUnknownDeviceEvents ? R.string.room_unknown_devices_messages_notification
                            : R.string.room_unsent_messages_notification, resendAll, cancelAll);

            int cancelAllPos = message.indexOf(cancelAll);
            int resendAllPos = message.indexOf(resendAll);

            text = new SpannableString(message);

            // cancelAllPos should always be > 0 but a GA crash reported here
            if (cancelAllPos >= 0) {
                text.setSpan(new cancelAllClickableSpan(), cancelAllPos, cancelAllPos + cancelAll.length(), 0);
            }

            // resendAllPos should always be > 0 but a GA crash reported here
            if (resendAllPos >= 0) {
                text.setSpan(new resendAllClickableSpan(), resendAllPos, resendAllPos + resendAll.length(), 0);
            }

            mNotificationTextView.setMovementMethod(LinkMovementMethod.getInstance());
            textColor = R.color.vector_fuchsia_color;

        } else if ((null != mIsScrolledToTheBottom) && (!mIsScrolledToTheBottom)) {
            isAreaVisible = true;

            int unreadCount = 0;

            RoomSummary summary = mRoom.getDataHandler().getStore().getSummary(mRoom.getRoomId());

            if (null != summary) {
                unreadCount = mRoom.getDataHandler().getStore().eventsCountAfter(mRoom.getRoomId(),
                        summary.getLatestReadEventId());
            }

            if (unreadCount > 0) {
                iconId = R.drawable.newmessages;
                textColor = R.color.vector_fuchsia_color;

                if (unreadCount == 1) {
                    text = new SpannableString(
                            getResources().getString(R.string.room_new_message_notification));
                } else {
                    text = new SpannableString(
                            getResources().getString(R.string.room_new_messages_notification, unreadCount));
                }
            } else {
                iconId = R.drawable.scrolldown;
                textColor = R.color.vector_text_gray_color;

                if (!TextUtils.isEmpty(mLatestTypingMessage)) {
                    text = new SpannableString(mLatestTypingMessage);
                }
            }

            mNotificationTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mVectorMessageListFragment.scrollToBottom(0);
                }
            });

            mNotificationIconImageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mVectorMessageListFragment.scrollToBottom(0);
                }
            });

        } else if (!TextUtils.isEmpty(mLatestTypingMessage)) {
            isAreaVisible = true;

            iconId = R.drawable.vector_typing;
            text = new SpannableString(mLatestTypingMessage);
            textColor = R.color.vector_text_gray_color;
        }
    }

    if (TextUtils.isEmpty(mEventId)) {
        mNotificationsArea.setVisibility(isAreaVisible ? View.VISIBLE : View.INVISIBLE);
    }

    if ((-1 != iconId) && (-1 != textColor)) {
        mNotificationIconImageView.setImageResource(iconId);
        mNotificationTextView.setText(text);
        mNotificationTextView.setTextColor(getResources().getColor(textColor));
    }

    //
    if (null != mResendUnsentMenuItem) {
        mResendUnsentMenuItem.setVisible(hasUnsentEvent);
    }

    if (null != mResendDeleteMenuItem) {
        mResendDeleteMenuItem.setVisible(hasUnsentEvent);
    }

    if (null != mSearchInRoomMenuItem) {
        // the server search does not work on encrypted rooms.
        mSearchInRoomMenuItem.setVisible(!mRoom.isEncrypted());
    }
}

From source file:com.tct.mail.utils.NotificationUtils.java

private static CharSequence getSingleMessageBigText(Context context, final Message message, String from) {

    final TextAppearanceSpan notificationSubjectSpan = new TextAppearanceSpan(context,
            R.style.NotificationPrimaryText);

    final String snippet = getMessageBodyWithoutElidedText(message);

    // Change multiple newlines (with potential white space between), into a single new line
    final String collapsedSnippet = !TextUtils.isEmpty(snippet) ? snippet.replaceAll("\\n\\s+", "\n") : "";

    if (TextUtils.isEmpty(from)) {
        // If the subject is empty, just use the snippet.
        return snippet;
    } else if (TextUtils.isEmpty(collapsedSnippet)) {
        // If the snippet is empty, just use the subject.
        final SpannableString spannableString = new SpannableString(from);
        spannableString.setSpan(notificationSubjectSpan, 0, from.length(), 0);

        return spannableString;
    } else {//w w  w.  j a  v  a2 s.  c o  m
        final String notificationBigTextFormat = context.getResources()
                .getString(R.string.single_new_message_notification_big_text);

        // Localizers may change the order of the parameters, look at how the format
        // string is structured.
        final boolean isSubjectFirst = notificationBigTextFormat.indexOf("%2$s") > notificationBigTextFormat
                .indexOf("%1$s");
        final String bigText = String.format(notificationBigTextFormat, from, collapsedSnippet);
        final SpannableString spannableString = new SpannableString(bigText);

        final int subjectOffset = (isSubjectFirst ? bigText.indexOf(from) : bigText.lastIndexOf(from));
        spannableString.setSpan(new ForegroundColorSpan(Color.BLACK), subjectOffset,
                subjectOffset + from.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        return spannableString;
    }
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void showAccessNotification(int uid) {
    String name = TextUtils.join(", ", Util.getApplicationNames(uid, ServiceSinkhole.this));

    Intent main = new Intent(ServiceSinkhole.this, ActivityMain.class);
    main.putExtra(ActivityMain.EXTRA_SEARCH, Integer.toString(uid));
    PendingIntent pi = PendingIntent.getActivity(ServiceSinkhole.this, uid + 10000, main,
            PendingIntent.FLAG_UPDATE_CURRENT);

    TypedValue tv = new TypedValue();
    getTheme().resolveAttribute(R.attr.colorOn, tv, true);
    int colorOn = tv.data;
    getTheme().resolveAttribute(R.attr.colorOff, tv, true);
    int colorOff = tv.data;

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_cloud_upload_white_24dp).setGroup("AccessAttempt")
            .setContentTitle(getString(R.string.app_name)).setContentText(getString(R.string.msg_access, name))
            .setContentIntent(pi).setColor(colorOff).setOngoing(false).setAutoCancel(true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        builder.setCategory(Notification.CATEGORY_STATUS).setVisibility(Notification.VISIBILITY_SECRET);
    }/*www  .  j  ava  2 s.  c o  m*/

    DateFormat df = new SimpleDateFormat("dd HH:mm");

    NotificationCompat.InboxStyle notification = new NotificationCompat.InboxStyle(builder);
    String sname = getString(R.string.msg_access, name);
    int pos = sname.indexOf(name);
    Spannable sp = new SpannableString(sname);
    sp.setSpan(new StyleSpan(Typeface.BOLD), pos, pos + name.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    notification.addLine(sp);

    Cursor cursor = DatabaseHelper.getInstance(ServiceSinkhole.this).getAccessUnset(uid, 7);
    int colDAddr = cursor.getColumnIndex("daddr");
    int colTime = cursor.getColumnIndex("time");
    int colAllowed = cursor.getColumnIndex("allowed");
    while (cursor.moveToNext()) {
        StringBuilder sb = new StringBuilder();
        sb.append(df.format(cursor.getLong(colTime))).append(' ');

        String daddr = cursor.getString(colDAddr);
        if (Util.isNumericAddress(daddr))
            try {
                daddr = InetAddress.getByName(daddr).getHostName();
            } catch (UnknownHostException ignored) {
            }
        sb.append(daddr);

        int allowed = cursor.getInt(colAllowed);
        if (allowed >= 0) {
            pos = sb.indexOf(daddr);
            sp = new SpannableString(sb);
            ForegroundColorSpan fgsp = new ForegroundColorSpan(allowed > 0 ? colorOn : colorOff);
            sp.setSpan(fgsp, pos, pos + daddr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        notification.addLine(sp);
    }
    cursor.close();

    NotificationManagerCompat.from(this).notify(uid + 10000, notification.build());
}

From source file:com.ichi2.anki2.Reviewer.java

private void updateScreenCounts() {
    if (mCurrentCard == null) {
        return;//  w w  w  .  j  av  a 2  s. c o  m
    }

    try {
        String[] title = mSched.getCol().getDecks().get(mCurrentCard.getDid()).getString("name").split("::");
        AnkiDroidApp.getCompat().setTitle(this, title[title.length - 1], mInvertedColors);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

    int[] counts = mSched.counts(mCurrentCard);

    int eta = mSched.eta(counts, false);
    AnkiDroidApp.getCompat().setSubtitle(this,
            getResources().getQuantityString(R.plurals.reviewer_window_title, eta, eta), mInvertedColors);

    SpannableString newCount = new SpannableString(String.valueOf(counts[0]));
    SpannableString lrnCount = new SpannableString(String.valueOf(counts[1]));
    SpannableString revCount = new SpannableString(String.valueOf(counts[2]));
    if (mPrefHideDueCount) {
        revCount = new SpannableString("???");
    }

    switch (mCurrentCard.getQueue()) {
    case Card.TYPE_NEW:
        newCount.setSpan(new UnderlineSpan(), 0, newCount.length(), 0);
        break;
    case Card.TYPE_LRN:
        lrnCount.setSpan(new UnderlineSpan(), 0, lrnCount.length(), 0);
        break;
    case Card.TYPE_REV:
        revCount.setSpan(new UnderlineSpan(), 0, revCount.length(), 0);
        break;
    }

    mTextBarRed.setText(newCount);
    mTextBarBlack.setText(lrnCount);
    mTextBarBlue.setText(revCount);
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private void updateScreenCounts() {
    if (mCurrentCard == null) {
        return;/*from  w  ww.  j a  v  a  2  s  .  c o m*/
    }

    try {
        String[] title = mSched.getCol().getDecks().get(mCurrentCard.getDid()).getString("name").split("::");
        AnkiDroidApp.getCompat().setTitle(this, title[title.length - 1], mInvertedColors);
    } catch (JSONException e) {
        throw new RuntimeException(e);
    }

    int[] counts = mSched.counts(mCurrentCard);

    int eta = mSched.eta(counts, false);
    //        AnkiDroidApp.getCompat().setSubtitle(this, getResources().getQuantityString(R.plurals.reviewer_window_title, eta, eta), mInvertedColors);

    SpannableString newCount = new SpannableString(String.valueOf(counts[0]));
    SpannableString lrnCount = new SpannableString(String.valueOf(counts[1]));
    SpannableString revCount = new SpannableString(String.valueOf(counts[2]));
    if (mPrefHideDueCount) {
        revCount = new SpannableString("???");
    }

    switch (mCurrentCard.getQueue()) {
    case Card.TYPE_NEW:
        newCount.setSpan(new UnderlineSpan(), 0, newCount.length(), 0);
        break;
    case Card.TYPE_LRN:
        lrnCount.setSpan(new UnderlineSpan(), 0, lrnCount.length(), 0);
        break;
    case Card.TYPE_REV:
        revCount.setSpan(new UnderlineSpan(), 0, revCount.length(), 0);
        break;
    }

    //   mTextBarRed.setText(newCount);
    //   mTextBarBlack.setText(lrnCount);
    //   mTextBarBlue.setText(revCount);

    mTextBarRed.setText(getString(R.string.subtitle_new) + " " + newCount);
    mTextBarBlack.setText(getString(R.string.subtitle_learning) + " " + lrnCount);
    mTextBarBlue.setText(getString(R.string.subtitle_review) + " " + revCount);
}

From source file:com.ichi2.anki2.Reviewer.java

private void displayCardQuestion() {
    // show timer, if activated in the deck's preferences
    initTimer();/*from w  w w.j a  va 2s.c o m*/

    sDisplayAnswer = false;

    if (mButtonHeight == 0 && mRelativeButtonSize != 100) {
        mButtonHeight = mFlipCard.getHeight() * mRelativeButtonSize / 100;
        mFlipCard.setHeight(mButtonHeight);
        mEase1.setHeight(mButtonHeight);
        mEase2.setHeight(mButtonHeight);
        mEase3.setHeight(mButtonHeight);
        mEase4.setHeight(mButtonHeight);
    }

    setInterface();

    String question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
    question = typeAnsQuestionFilter(question);

    if (mPrefFixArabic) {
        question = ArabicUtilities.reshapeSentence(question, true);
    }

    Log.i(AnkiDroidApp.TAG, "question: '" + question + "'");

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(question);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_question));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        // If the user wants to write the answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.VISIBLE);

            // Show soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(mAnswerField, InputMethodManager.SHOW_FORCED);
        }

        displayString = enrichWithQADiv(question, false);

        if (mSpeakText) {
            // ReadText.setLanguageInformation(Model.getModel(DeckManager.getMainDeck(),
            // mCurrentCard.getCardModelId(), false).getId(), mCurrentCard.getCardModelId());
        }

        if (mPrefRecord) {
            Recorder.reset();
            // check for auto record
            try {
                if (mSched.getCol().getDecks().confForDid(mCurrentCard.getDid()).getBoolean("autoRecord")) {
                    Recorder.startRecording();
                }
            } catch (JSONException e) {
                throw new RuntimeException(e);
            }
        }

    }

    updateCard(displayString);
    hideEaseButtons();

    // If the user want to show answer automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowAnswerTask);
        mTimeoutHandler.postDelayed(mShowAnswerTask, mWaitAnswerSecond * 1000);
    }
}

From source file:com.ichi2.anki2.Reviewer.java

private void displayCardAnswer() {
    Log.i(AnkiDroidApp.TAG, "displayCardAnswer");

    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;/*from ww  w .  j a v  a  2s . com*/
    }

    sDisplayAnswer = true;
    setFlipCardAnimation();

    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();
        Recorder.stopRecording();

        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }

        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);

                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();

                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ")
                        .append(mTypeSize).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }

            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }

    mIsSelecting = false;
    updateCard(displayString);
    showEaseButtons();

    // If the user want to show next question automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowQuestionTask);
        mTimeoutHandler.postDelayed(mShowQuestionTask, mWaitQuestionSecond * 1000);
    }
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private void displayCardQuestion() {
    // show timer, if activated in the deck's preferences
    initTimer();// w w w.  j  ava 2 s  . c  om

    sDisplayAnswer = false;

    if (mButtonHeight == 0 && mRelativeButtonSize != 100) {
        mButtonHeight = mFlipCard.getHeight() * mRelativeButtonSize / 100;
        mFlipCard.setHeight(mButtonHeight);
        mEase1.setHeight(mButtonHeight);
        mEase2.setHeight(mButtonHeight);
        mEase3.setHeight(mButtonHeight);
        mEase4.setHeight(mButtonHeight);
    }

    setInterface();

    String question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
    question = typeAnsQuestionFilter(question);

    if (mPrefFixArabic) {
        question = ArabicUtilities.reshapeSentence(question, true);
    }

    // Log.i(AnkiDroidApp.TAG, "question: '" + question + "'");

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(question);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_question));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        // If the user wants to write the answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.VISIBLE);

            // Show soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.showSoftInput(mAnswerField, InputMethodManager.SHOW_FORCED);
        }

        displayString = enrichWithQADiv(question, false);

        if (mSpeakText) {
            // ReadText.setLanguageInformation(Model.getModel(DeckManager.getMainDeck(),
            // mCurrentCard.getCardModelId(), false).getId(), mCurrentCard.getCardModelId());
        }
    }

    updateCard(displayString);
    hideEaseButtons();

    // If the user want to show answer automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowAnswerTask);
        mTimeoutHandler.postDelayed(mShowAnswerTask, mWaitAnswerSecond * 1000);
    }
}

From source file:com.hichinaschool.flashcards.anki.Reviewer.java

private void displayCardAnswer() {
    // Log.i(AnkiDroidApp.TAG, "displayCardAnswer");

    // prevent answering (by e.g. gestures) before card is loaded
    if (mCurrentCard == null) {
        return;/*from  w  ww.j  a v a  2s  .  co m*/
    }

    sDisplayAnswer = true;
    setFlipCardAnimation();

    String answer = mCurrentCard.getAnswer(mCurrentSimpleInterface);
    answer = typeAnsAnswerFilter(answer);

    String displayString = "";

    if (mCurrentSimpleInterface) {
        mCardContent = convertToSimple(answer);
        if (mCardContent.length() == 0) {
            SpannableString hint = new SpannableString(
                    getResources().getString(R.string.simple_interface_hint, R.string.card_details_answer));
            hint.setSpan(new StyleSpan(Typeface.ITALIC), 0, mCardContent.length(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mCardContent = hint;
        }
    } else {
        Sound.stopSounds();

        if (mPrefFixArabic) {
            // reshape
            answer = ArabicUtilities.reshapeSentence(answer, true);
        }

        // If the user wrote an answer
        if (typeAnswer()) {
            mAnswerField.setVisibility(View.GONE);
            if (mCurrentCard != null) {
                if (mPrefFixArabic) {
                    // reshape
                    mTypeCorrect = ArabicUtilities.reshapeSentence(mTypeCorrect, true);
                }
                // Obtain the user answer and the correct answer
                String userAnswer = mAnswerField.getText().toString();
                Matcher matcher = sSpanPattern.matcher(Utils.stripHTMLMedia(mTypeCorrect));
                String correctAnswer = matcher.replaceAll("");
                matcher = sBrPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("\n");
                matcher = Sound.sSoundPattern.matcher(correctAnswer);
                correctAnswer = matcher.replaceAll("");
                // Log.i(AnkiDroidApp.TAG, "correct answer = " + correctAnswer);

                // Obtain the diff and send it to updateCard
                DiffEngine diff = new DiffEngine();

                StringBuffer span = new StringBuffer();
                span.append("<span style=\"font-family: '").append(mTypeFont).append("'; font-size: ")
                        .append(mTypeSize).append("px\">");
                span.append(diff.diff_prettyHtml(diff.diff_main(userAnswer, correctAnswer), mNightMode));
                span.append("</span>");
                span.append("<br/>").append(answer);
                displayString = enrichWithQADiv(span.toString(), true);
            }

            // Hide soft keyboard
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(
                    Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(mAnswerField.getWindowToken(), 0);
        } else {
            displayString = enrichWithQADiv(answer, true);
        }
    }

    mIsSelecting = false;
    updateCard(displayString);
    showEaseButtons();

    // If the user want to show next question automatically
    if (mPrefUseTimer) {
        mTimeoutHandler.removeCallbacks(mShowQuestionTask);
        mTimeoutHandler.postDelayed(mShowQuestionTask, mWaitQuestionSecond * 1000);
    }
}

From source file:com.android.contacts.quickcontact.QuickContactActivity.java

/**
 * Converts a {@link DataItem} into an {@link ExpandingEntryCardView.Entry} for display.
 * If the {@link ExpandingEntryCardView.Entry} has no visual elements, null is returned.
 *
 * This runs on a background thread. This is set as static to avoid accidentally adding
 * additional dependencies on unsafe things (like the Activity).
 *
 * @param dataItem The {@link DataItem} to convert.
 * @param secondDataItem A second {@link DataItem} to help build a full entry for some
 *  mimetypes//from ww  w.  j a  v  a  2 s . c  o m
 * @return The {@link ExpandingEntryCardView.Entry}, or null if no visual elements are present.
 */
private static Entry dataItemToEntry(DataItem dataItem, DataItem secondDataItem, Context context,
        Contact contactData, final MutableString aboutCardName) {
    Drawable icon = null;
    String header = null;
    String subHeader = null;
    Drawable subHeaderIcon = null;
    String text = null;
    Drawable textIcon = null;
    StringBuilder primaryContentDescription = new StringBuilder();
    Spannable phoneContentDescription = null;
    Spannable smsContentDescription = null;
    Intent intent = null;
    boolean shouldApplyColor = true;
    Drawable alternateIcon = null;
    Intent alternateIntent = null;
    StringBuilder alternateContentDescription = new StringBuilder();
    final boolean isEditable = false;
    EntryContextMenuInfo entryContextMenuInfo = null;
    Drawable thirdIcon = null;
    Intent thirdIntent = null;
    int thirdAction = Entry.ACTION_NONE;
    String thirdContentDescription = null;
    Bundle thirdExtras = null;
    int iconResourceId = 0;

    context = context.getApplicationContext();
    final Resources res = context.getResources();
    DataKind kind = dataItem.getDataKind();

    if (dataItem instanceof ImDataItem) {
        final ImDataItem im = (ImDataItem) dataItem;
        intent = ContactsUtils.buildImIntent(context, im).first;
        final boolean isEmail = im.isCreatedFromEmail();
        final int protocol;
        if (!im.isProtocolValid()) {
            protocol = Im.PROTOCOL_CUSTOM;
        } else {
            protocol = isEmail ? Im.PROTOCOL_GOOGLE_TALK : im.getProtocol();
        }
        if (protocol == Im.PROTOCOL_CUSTOM) {
            // If the protocol is custom, display the "IM" entry header as well to distinguish
            // this entry from other ones
            header = res.getString(R.string.header_im_entry);
            subHeader = Im.getProtocolLabel(res, protocol, im.getCustomProtocol()).toString();
            text = im.getData();
        } else {
            header = Im.getProtocolLabel(res, protocol, im.getCustomProtocol()).toString();
            subHeader = im.getData();
        }
        entryContextMenuInfo = new EntryContextMenuInfo(im.getData(), header, dataItem.getMimeType(),
                dataItem.getId(), dataItem.isSuperPrimary());
    } else if (dataItem instanceof OrganizationDataItem) {
        final OrganizationDataItem organization = (OrganizationDataItem) dataItem;
        header = res.getString(R.string.header_organization_entry);
        subHeader = organization.getCompany();
        entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header, dataItem.getMimeType(),
                dataItem.getId(), dataItem.isSuperPrimary());
        text = organization.getTitle();
    } else if (dataItem instanceof NicknameDataItem) {
        final NicknameDataItem nickname = (NicknameDataItem) dataItem;
        // Build nickname entries
        final boolean isNameRawContact = (contactData.getNameRawContactId() == dataItem.getRawContactId());

        final boolean duplicatesTitle = isNameRawContact
                && contactData.getDisplayNameSource() == DisplayNameSources.NICKNAME;

        if (!duplicatesTitle) {
            header = res.getString(R.string.header_nickname_entry);
            subHeader = nickname.getName();
            entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header, dataItem.getMimeType(),
                    dataItem.getId(), dataItem.isSuperPrimary());
        }
    } else if (dataItem instanceof NoteDataItem) {
        final NoteDataItem note = (NoteDataItem) dataItem;
        header = res.getString(R.string.header_note_entry);
        subHeader = note.getNote();
        entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header, dataItem.getMimeType(),
                dataItem.getId(), dataItem.isSuperPrimary());
    } else if (dataItem instanceof WebsiteDataItem) {
        final WebsiteDataItem website = (WebsiteDataItem) dataItem;
        header = res.getString(R.string.header_website_entry);
        subHeader = website.getUrl();
        entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header, dataItem.getMimeType(),
                dataItem.getId(), dataItem.isSuperPrimary());
        try {
            final WebAddress webAddress = new WebAddress(website.buildDataStringForDisplay(context, kind));
            intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webAddress.toString()));
        } catch (final ParseException e) {
            Log.e(TAG, "Couldn't parse website: " + website.buildDataStringForDisplay(context, kind));
        }
    } else if (dataItem instanceof EventDataItem) {
        final EventDataItem event = (EventDataItem) dataItem;
        final String dataString = event.buildDataStringForDisplay(context, kind);
        final Calendar cal = DateUtils.parseDate(dataString, false);
        if (cal != null) {
            final Date nextAnniversary = DateUtils.getNextAnnualDate(cal);
            final Uri.Builder builder = CalendarContract.CONTENT_URI.buildUpon();
            builder.appendPath("time");
            ContentUris.appendId(builder, nextAnniversary.getTime());
            intent = new Intent(Intent.ACTION_VIEW).setData(builder.build());
        }
        header = res.getString(R.string.header_event_entry);
        if (event.hasKindTypeColumn(kind)) {
            subHeader = EventCompat.getTypeLabel(res, event.getKindTypeColumn(kind), event.getLabel())
                    .toString();
        }
        text = DateUtils.formatDate(context, dataString);
        entryContextMenuInfo = new EntryContextMenuInfo(text, header, dataItem.getMimeType(), dataItem.getId(),
                dataItem.isSuperPrimary());
    } else if (dataItem instanceof RelationDataItem) {
        final RelationDataItem relation = (RelationDataItem) dataItem;
        final String dataString = relation.buildDataStringForDisplay(context, kind);
        if (!TextUtils.isEmpty(dataString)) {
            intent = new Intent(Intent.ACTION_SEARCH);
            intent.putExtra(SearchManager.QUERY, dataString);
            intent.setType(Contacts.CONTENT_TYPE);
        }
        header = res.getString(R.string.header_relation_entry);
        subHeader = relation.getName();
        entryContextMenuInfo = new EntryContextMenuInfo(subHeader, header, dataItem.getMimeType(),
                dataItem.getId(), dataItem.isSuperPrimary());
        if (relation.hasKindTypeColumn(kind)) {
            text = Relation.getTypeLabel(res, relation.getKindTypeColumn(kind), relation.getLabel()).toString();
        }
    } else if (dataItem instanceof PhoneDataItem) {
        final PhoneDataItem phone = (PhoneDataItem) dataItem;
        String phoneLabel = null;
        if (!TextUtils.isEmpty(phone.getNumber())) {
            primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
            header = sBidiFormatter.unicodeWrap(phone.buildDataStringForDisplay(context, kind),
                    TextDirectionHeuristics.LTR);
            entryContextMenuInfo = new EntryContextMenuInfo(header, res.getString(R.string.phoneLabelsGroup),
                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
            if (phone.hasKindTypeColumn(kind)) {
                final int kindTypeColumn = phone.getKindTypeColumn(kind);
                final String label = phone.getLabel();
                phoneLabel = label;
                if (kindTypeColumn == Phone.TYPE_CUSTOM && TextUtils.isEmpty(label)) {
                    text = "";
                } else {
                    text = Phone.getTypeLabel(res, kindTypeColumn, label).toString();
                    phoneLabel = text;
                    primaryContentDescription.append(text).append(" ");
                }
            }
            primaryContentDescription.append(header);
            phoneContentDescription = com.android.contacts.common.util.ContactDisplayUtils
                    .getTelephoneTtsSpannable(primaryContentDescription.toString(), header);
            icon = res.getDrawable(R.drawable.ic_phone_24dp);
            iconResourceId = R.drawable.ic_phone_24dp;
            if (PhoneCapabilityTester.isPhone(context)) {
                intent = CallUtil.getCallIntent(phone.getNumber());
            }
            alternateIntent = new Intent(Intent.ACTION_SENDTO,
                    Uri.fromParts(ContactsUtils.SCHEME_SMSTO, phone.getNumber(), null));

            alternateIcon = res.getDrawable(R.drawable.ic_message_24dp);
            alternateContentDescription.append(res.getString(R.string.sms_custom, header));
            smsContentDescription = com.android.contacts.common.util.ContactDisplayUtils
                    .getTelephoneTtsSpannable(alternateContentDescription.toString(), header);

            int videoCapability = CallUtil.getVideoCallingAvailability(context);
            boolean isPresenceEnabled = (videoCapability & CallUtil.VIDEO_CALLING_PRESENCE) != 0;
            boolean isVideoEnabled = (videoCapability & CallUtil.VIDEO_CALLING_ENABLED) != 0;

            if (CallUtil.isCallWithSubjectSupported(context)) {
                thirdIcon = res.getDrawable(R.drawable.ic_call_note_white_24dp);
                thirdAction = Entry.ACTION_CALL_WITH_SUBJECT;
                thirdContentDescription = res.getString(R.string.call_with_a_note);
                // Create a bundle containing the data the call subject dialog requires.
                thirdExtras = new Bundle();
                thirdExtras.putLong(CallSubjectDialog.ARG_PHOTO_ID, contactData.getPhotoId());
                thirdExtras.putParcelable(CallSubjectDialog.ARG_PHOTO_URI,
                        UriUtils.parseUriOrNull(contactData.getPhotoUri()));
                thirdExtras.putParcelable(CallSubjectDialog.ARG_CONTACT_URI, contactData.getLookupUri());
                thirdExtras.putString(CallSubjectDialog.ARG_NAME_OR_NUMBER, contactData.getDisplayName());
                thirdExtras.putBoolean(CallSubjectDialog.ARG_IS_BUSINESS, false);
                thirdExtras.putString(CallSubjectDialog.ARG_NUMBER, phone.getNumber());
                thirdExtras.putString(CallSubjectDialog.ARG_DISPLAY_NUMBER, phone.getFormattedPhoneNumber());
                thirdExtras.putString(CallSubjectDialog.ARG_NUMBER_LABEL, phoneLabel);
            } else if (isVideoEnabled) {
                // Check to ensure carrier presence indicates the number supports video calling.
                int carrierPresence = dataItem.getCarrierPresence();
                boolean isPresent = (carrierPresence & Phone.CARRIER_PRESENCE_VT_CAPABLE) != 0;

                if ((isPresenceEnabled && isPresent) || !isPresenceEnabled) {
                    thirdIcon = res.getDrawable(R.drawable.ic_videocam);
                    thirdAction = Entry.ACTION_INTENT;
                    thirdIntent = CallUtil.getVideoCallIntent(phone.getNumber(),
                            CALL_ORIGIN_QUICK_CONTACTS_ACTIVITY);
                    thirdContentDescription = res.getString(R.string.description_video_call);
                }
            }
        }
    } else if (dataItem instanceof EmailDataItem) {
        final EmailDataItem email = (EmailDataItem) dataItem;
        final String address = email.getData();
        if (!TextUtils.isEmpty(address)) {
            primaryContentDescription.append(res.getString(R.string.email_other)).append(" ");
            final Uri mailUri = Uri.fromParts(ContactsUtils.SCHEME_MAILTO, address, null);
            intent = new Intent(Intent.ACTION_SENDTO, mailUri);
            header = email.getAddress();
            entryContextMenuInfo = new EntryContextMenuInfo(header, res.getString(R.string.emailLabelsGroup),
                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
            if (email.hasKindTypeColumn(kind)) {
                text = Email.getTypeLabel(res, email.getKindTypeColumn(kind), email.getLabel()).toString();
                primaryContentDescription.append(text).append(" ");
            }
            primaryContentDescription.append(header);
            icon = res.getDrawable(R.drawable.ic_email_24dp);
            iconResourceId = R.drawable.ic_email_24dp;
        }
    } else if (dataItem instanceof StructuredPostalDataItem) {
        StructuredPostalDataItem postal = (StructuredPostalDataItem) dataItem;
        final String postalAddress = postal.getFormattedAddress();
        if (!TextUtils.isEmpty(postalAddress)) {
            primaryContentDescription.append(res.getString(R.string.map_other)).append(" ");
            intent = StructuredPostalUtils.getViewPostalAddressIntent(postalAddress);
            header = postal.getFormattedAddress();
            entryContextMenuInfo = new EntryContextMenuInfo(header, res.getString(R.string.postalLabelsGroup),
                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
            if (postal.hasKindTypeColumn(kind)) {
                text = StructuredPostal.getTypeLabel(res, postal.getKindTypeColumn(kind), postal.getLabel())
                        .toString();
                primaryContentDescription.append(text).append(" ");
            }
            primaryContentDescription.append(header);
            alternateIntent = StructuredPostalUtils.getViewPostalAddressDirectionsIntent(postalAddress);
            alternateIcon = res.getDrawable(R.drawable.ic_directions_24dp);
            alternateContentDescription.append(res.getString(R.string.content_description_directions))
                    .append(" ").append(header);
            icon = res.getDrawable(R.drawable.ic_place_24dp);
            iconResourceId = R.drawable.ic_place_24dp;
        }
    } else if (dataItem instanceof SipAddressDataItem) {
        final SipAddressDataItem sip = (SipAddressDataItem) dataItem;
        final String address = sip.getSipAddress();
        if (!TextUtils.isEmpty(address)) {
            primaryContentDescription.append(res.getString(R.string.call_other)).append(" ");
            if (PhoneCapabilityTester.isSipPhone(context)) {
                final Uri callUri = Uri.fromParts(PhoneAccount.SCHEME_SIP, address, null);
                intent = CallUtil.getCallIntent(callUri);
            }
            header = address;
            entryContextMenuInfo = new EntryContextMenuInfo(header, res.getString(R.string.phoneLabelsGroup),
                    dataItem.getMimeType(), dataItem.getId(), dataItem.isSuperPrimary());
            if (sip.hasKindTypeColumn(kind)) {
                text = SipAddress.getTypeLabel(res, sip.getKindTypeColumn(kind), sip.getLabel()).toString();
                primaryContentDescription.append(text).append(" ");
            }
            primaryContentDescription.append(header);
            icon = res.getDrawable(R.drawable.ic_dialer_sip_black_24dp);
            iconResourceId = R.drawable.ic_dialer_sip_black_24dp;
        }
    } else if (dataItem instanceof StructuredNameDataItem) {
        // If the name is already set and this is not the super primary value then leave the
        // current value. This way we show the super primary value when we are able to.
        if (dataItem.isSuperPrimary() || aboutCardName.value == null || aboutCardName.value.isEmpty()) {
            final String givenName = ((StructuredNameDataItem) dataItem).getGivenName();
            if (!TextUtils.isEmpty(givenName)) {
                aboutCardName.value = res.getString(R.string.about_card_title) + " " + givenName;
            } else {
                aboutCardName.value = res.getString(R.string.about_card_title);
            }
        }
    } else {
        // Custom DataItem
        header = dataItem.buildDataStringForDisplay(context, kind);
        text = kind.typeColumn;
        intent = new Intent(Intent.ACTION_VIEW);
        final Uri uri = ContentUris.withAppendedId(Data.CONTENT_URI, dataItem.getId());
        intent.setDataAndType(uri, dataItem.getMimeType());

        if (intent != null) {
            final String mimetype = intent.getType();

            // Build advanced entry for known 3p types. Otherwise default to ResolveCache icon.
            switch (mimetype) {
            case MIMETYPE_GPLUS_PROFILE:
                // If a secondDataItem is available, use it to build an entry with
                // alternate actions
                if (secondDataItem != null) {
                    icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
                    alternateIcon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
                    final GPlusOrHangoutsDataItemModel itemModel = new GPlusOrHangoutsDataItemModel(intent,
                            alternateIntent, dataItem, secondDataItem, alternateContentDescription, header,
                            text, context);

                    populateGPlusOrHangoutsDataItemModel(itemModel);
                    intent = itemModel.intent;
                    alternateIntent = itemModel.alternateIntent;
                    alternateContentDescription = itemModel.alternateContentDescription;
                    header = itemModel.header;
                    text = itemModel.text;
                } else {
                    if (GPLUS_PROFILE_DATA_5_ADD_TO_CIRCLE.equals(intent.getDataString())) {
                        icon = res.getDrawable(R.drawable.ic_add_to_circles_black_24);
                    } else {
                        icon = res.getDrawable(R.drawable.ic_google_plus_24dp);
                    }
                }
                break;
            case MIMETYPE_HANGOUTS:
                // If a secondDataItem is available, use it to build an entry with
                // alternate actions
                if (secondDataItem != null) {
                    icon = res.getDrawable(R.drawable.ic_hangout_24dp);
                    alternateIcon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
                    final GPlusOrHangoutsDataItemModel itemModel = new GPlusOrHangoutsDataItemModel(intent,
                            alternateIntent, dataItem, secondDataItem, alternateContentDescription, header,
                            text, context);

                    populateGPlusOrHangoutsDataItemModel(itemModel);
                    intent = itemModel.intent;
                    alternateIntent = itemModel.alternateIntent;
                    alternateContentDescription = itemModel.alternateContentDescription;
                    header = itemModel.header;
                    text = itemModel.text;
                } else {
                    if (HANGOUTS_DATA_5_VIDEO.equals(intent.getDataString())) {
                        icon = res.getDrawable(R.drawable.ic_hangout_video_24dp);
                    } else {
                        icon = res.getDrawable(R.drawable.ic_hangout_24dp);
                    }
                }
                break;
            default:
                entryContextMenuInfo = new EntryContextMenuInfo(header, mimetype, dataItem.getMimeType(),
                        dataItem.getId(), dataItem.isSuperPrimary());
                icon = ResolveCache.getInstance(context).getIcon(dataItem.getMimeType(), intent);
                // Call mutate to create a new Drawable.ConstantState for color filtering
                if (icon != null) {
                    icon.mutate();
                }
                shouldApplyColor = false;
            }
        }
    }

    if (intent != null) {
        // Do not set the intent is there are no resolves
        if (!PhoneCapabilityTester.isIntentRegistered(context, intent)) {
            intent = null;
        }
    }

    if (alternateIntent != null) {
        // Do not set the alternate intent is there are no resolves
        if (!PhoneCapabilityTester.isIntentRegistered(context, alternateIntent)) {
            alternateIntent = null;
        } else if (TextUtils.isEmpty(alternateContentDescription)) {
            // Attempt to use package manager to find a suitable content description if needed
            alternateContentDescription.append(getIntentResolveLabel(alternateIntent, context));
        }
    }

    // If the Entry has no visual elements, return null
    if (icon == null && TextUtils.isEmpty(header) && TextUtils.isEmpty(subHeader) && subHeaderIcon == null
            && TextUtils.isEmpty(text) && textIcon == null) {
        return null;
    }

    // Ignore dataIds from the Me profile.
    final int dataId = dataItem.getId() > Integer.MAX_VALUE ? -1 : (int) dataItem.getId();

    return new Entry(dataId, icon, header, subHeader, subHeaderIcon, text, textIcon,
            phoneContentDescription == null ? new SpannableString(primaryContentDescription.toString())
                    : phoneContentDescription,
            intent, alternateIcon, alternateIntent,
            smsContentDescription == null ? new SpannableString(alternateContentDescription.toString())
                    : smsContentDescription,
            shouldApplyColor, isEditable, entryContextMenuInfo, thirdIcon, thirdIntent, thirdContentDescription,
            thirdAction, thirdExtras, iconResourceId);
}