Example usage for android.text SpannedString SpannedString

List of usage examples for android.text SpannedString SpannedString

Introduction

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

Prototype

public SpannedString(CharSequence source) 

Source Link

Document

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

Usage

From source file:uk.co.pjmobile.mobile_apps.page_turner_reader.ReadingActivity.java

@Override
public void errorOnBookOpening(String errorMessage) {
    this.waitDialog.hide();
    String message = String.format(getString(R.string.error_open_bk), errorMessage);
    bookView.setText(new SpannedString(message));
}

From source file:com.gelakinetic.selfr.CameraActivity.java

/**
 * Jellybean had a weird bug, and this fixes it. Silly google!
 * https://code.google.com/p/android/issues/detail?id=35466#c2
 * <p/>/*  w w w  .jav  a 2  s .c om*/
 * Process HTML tags within a String before displaying (i.e. <br> becomes \n)
 *
 * @param source A string of HTML
 * @return a formatted Spanned which JellyBean is happy with
 */
private static Spanned formatHtmlString(String source) {
    /* Make sure we're not formatting a null string */
    if (source == null) {
        return new SpannedString("");
    }
    if (Build.VERSION.SDK_INT == 16) {
        source = source.replace("<", " <").replace(">", " >").replace("  ", " ");
    }
    return Html.fromHtml(source);
}

From source file:com.jecelyin.editor.v2.core.text.TextUtils.java

/**
 * Returns a CharSequence concatenating the specified CharSequences,
 * retaining their spans if any.//  ww  w .  ja  va  2s  . c  om
 */
public static CharSequence concat(CharSequence... text) {
    if (text.length == 0) {
        return "";
    }

    if (text.length == 1) {
        return text[0];
    }

    boolean spanned = false;
    for (int i = 0; i < text.length; i++) {
        if (text[i] instanceof Spanned) {
            spanned = true;
            break;
        }
    }

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length; i++) {
        sb.append(text[i]);
    }

    if (!spanned) {
        return sb.toString();
    }

    SpannableString ss = new SpannableString(sb);
    int off = 0;
    for (int i = 0; i < text.length; i++) {
        int len = text[i].length();

        if (text[i] instanceof Spanned) {
            copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off);
        }

        off += len;
    }

    return new SpannedString(ss);
}

From source file:com.ichi2.anki.AbstractFlashcardViewer.java

private void updateCard(String content) {
    Timber.d("updateCard()");

    // Add CSS for font color and font size
    if (mCurrentCard == null) {
        mCard.getSettings().setDefaultFontSize(calculateDynamicFontSize(content));
    }//from  w  w  w . j a v a2s.c om

    if (sDisplayAnswer) {
        // don't add answer sounds multiple times, such as when reshowing card after exiting editor
        // additionally, this condition reduces computation time
        if (!mAnswerSoundsAdded) {
            String answerSoundSource = removeFrontSideAudio(content);
            mSoundPlayer.addSounds(mBaseUrl, answerSoundSource, Sound.SOUNDS_ANSWER);
            mAnswerSoundsAdded = true;
        }
    } else {
        // reset sounds each time first side of card is displayed, which may happen repeatedly without ever
        // leaving the card (such as when edited)
        mSoundPlayer.resetSounds();
        mAnswerSoundsAdded = false;
        mSoundPlayer.addSounds(mBaseUrl, content, Sound.SOUNDS_QUESTION);
    }

    content = Sound.expandSounds(mBaseUrl, content);

    // In order to display the bold style correctly, we have to change
    // font-weight to 700
    content = content.replace("font-weight:600;", "font-weight:700;");

    // CSS class for card-specific styling
    String cardClass = "card card" + (mCurrentCard.getOrd() + 1);

    if (mPrefCenterVertically) {
        cardClass += " vertically_centered";
    }

    Timber.d("content card = \n %s", content);
    StringBuilder style = new StringBuilder();
    mExtensions.updateCssStyle(style);

    // Zoom cards
    if (mCardZoom != 100) {
        style.append(String.format("body { zoom: %s }\n", mCardZoom / 100.0));
    }

    // Zoom images
    if (mImageZoom != 100) {
        style.append(String.format("img { zoom: %s }\n", mImageZoom / 100.0));
    }

    Timber.d("::style::", style);

    if (mNightMode) {
        // Enable the night-mode class
        cardClass += " night_mode";
        // If card styling doesn't contain any mention of the night_mode class then do color inversion as fallback
        // TODO: find more robust solution that won't match unrelated classes like "night_mode_old"
        if (!mCurrentCard.css().contains(".night_mode")) {
            content = HtmlColors.invertColors(content);
        }
    }

    content = SmpToHtmlEntity(content);
    mCardContent = new SpannedString(mCardTemplate.replace("::content::", content)
            .replace("::style::", style.toString()).replace("::class::", cardClass));
    Timber.d("base url = %s", mBaseUrl);

    if (SAVE_CARD_CONTENT) {
        try {
            FileOutputStream f = new FileOutputStream(
                    new File(CollectionHelper.getCurrentAnkiDroidDirectory(this), "card.html"));
            try {
                f.write(mCardContent.toString().getBytes());
            } finally {
                f.close();
            }
        } catch (IOException e) {
            Timber.d(e, "failed to save card");
        }
    }
    fillFlashcard();

    if (!mConfigurationChanged) {
        playSounds(false); // Play sounds if appropriate
    }
}

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

private void updateCard(String content) {
    Log.i(AnkiDroidApp.TAG, "updateCard");

    Lookup.initialize(this, mCurrentCard.getDid());

    if (mCurrentSimpleInterface) {
        fillFlashcard(mShowAnimations);/* ww w.  j a va2s  .c  o  m*/
    } else {

        // Check whether there is a hard coded font-size in the content and apply the relative font size
        // Check needs to be done before CSS is applied to content;
        content = recalculateHardCodedFontSize(content, mDisplayFontSize);

        // Add CSS for font color and font size
        if (mCurrentCard == null) {
            mCard.getSettings().setDefaultFontSize(calculateDynamicFontSize(content));
        }

        // don't play question sound again when displaying answer
        String question = "";
        String answer = "";

        Sound.resetSounds();

        int qa = MetaDB.LANGUAGES_QA_QUESTION;
        if (sDisplayAnswer) {
            qa = MetaDB.LANGUAGES_QA_ANSWER;
        }
        answer = Sound.parseSounds(mBaseUrl, content, mSpeakText, qa);

        // If recording is enabled, the answer is being shown, a recording has been 
        // made and auto replaying is enabled
        try {
            if (mPrefRecord && sDisplayAnswer && Recorder.recorded() && mSched.getCol().getDecks()
                    .confForDid(mCurrentCard.getDid()).getBoolean("autoRecordReplay")) {
                // TODO play before or after other media
                Sound.addRecording(Recorder.getPath(), false, qa);
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }

        content = question + answer;

        // In order to display the bold style correctly, we have to change
        // font-weight to 700
        content = content.replace("font-weight:600;", "font-weight:700;");

        // CSS class for card-specific styling
        String cardClass = "card card" + (mCurrentCard.getOrd() + 1);

        Log.i(AnkiDroidApp.TAG, "content card = \n" + content);
        StringBuilder style = new StringBuilder();
        style.append(mCustomFontStyle);
        Log.i(AnkiDroidApp.TAG, "::style::" + style);

        if (mNightMode) {
            content = HtmlColors.invertColors(content);
        }

        content = SmpToHtmlEntity(content);
        mCardContent = new SpannedString(mCardTemplate.replace("::content::", content)
                .replace("::style::", style.toString()).replace("::class::", cardClass));
        Log.i(AnkiDroidApp.TAG, "base url = " + mBaseUrl);

        fillFlashcard(mShowAnimations);
    }

    if (!mConfigurationChanged) {
        playSounds();
    }
}

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

private void updateCard(String content) {
    // Log.i(AnkiDroidApp.TAG, "updateCard");

    Lookup.initialize(this, mCurrentCard.getDid());

    if (mCurrentSimpleInterface) {
        fillFlashcard(mShowAnimations);// w ww.j  ava 2 s. c om
    } else {

        // Check whether there is a hard coded font-size in the content and apply the relative font size
        // Check needs to be done before CSS is applied to content;
        content = recalculateHardCodedFontSize(content, mDisplayFontSize);

        // Add CSS for font color and font size
        if (mCurrentCard == null) {
            mCard.getSettings().setDefaultFontSize(calculateDynamicFontSize(content));
        }

        // don't play question sound again when displaying answer
        String question = "";
        String answer = "";

        Sound.resetSounds();

        int qa = MetaDB.LANGUAGES_QA_QUESTION;
        if (sDisplayAnswer) {
            qa = MetaDB.LANGUAGES_QA_ANSWER;
        }
        answer = Sound.parseSounds(mBaseUrl, content, mSpeakText, qa);

        content = question + answer;

        // In order to display the bold style correctly, we have to change
        // font-weight to 700
        content = content.replace("font-weight:600;", "font-weight:700;");

        // CSS class for card-specific styling
        String cardClass = "card card" + (mCurrentCard.getOrd() + 1);

        if (mPrefCenterVertically) {
            cardClass += " vertically_centered";
        }

        // Log.i(AnkiDroidApp.TAG, "content card = \n" + content);
        StringBuilder style = new StringBuilder();
        style.append(mCustomFontStyle);

        // Scale images.
        if (mRelativeImageSize != 100) {
            style.append(String.format("img { zoom: %s }\n", mRelativeImageSize / 100.0));
        }
        // Log.i(AnkiDroidApp.TAG, "::style::" + style);

        if (mNightMode) {
            content = HtmlColors.invertColors(content);
            cardClass += " night_mode";
        }

        content = SmpToHtmlEntity(content);
        mCardContent = new SpannedString(mCardTemplate.replace("::content::", content)
                .replace("::style::", style.toString()).replace("::class::", cardClass));
        // Log.i(AnkiDroidApp.TAG, "base url = " + mBaseUrl);

        if (SAVE_CARD_CONTENT) {
            try {
                FileOutputStream f = new FileOutputStream(
                        new File(AnkiDroidApp.getCurrentAnkiDroidDirectory(), "card.html"));
                try {
                    f.write(mCardContent.toString().getBytes());
                } finally {
                    f.close();
                }
            } catch (IOException e) {
                // Log.d(AnkiDroidApp.TAG, "failed to save card", e);
            }
        }
        fillFlashcard(mShowAnimations);
    }

    if (!mConfigurationChanged) {
        playSounds();
    }
}