Search in sources :

Example 11 with SpannedString

use of android.text.SpannedString in project SuperSaiyanScrollView by nolanlawson.

the class StringUtil method joinSpannables.

/**
     * Returns a CharSequence concatenating the specified CharSequences using the specified delimiter,
     * retaining their spans if any.
     * 
     * This is mostly borrowed from TextUtils.concat();
     */
public static CharSequence joinSpannables(String delimiter, 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++) {
        if (i > 0) {
            sb.append(delimiter);
        }
        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) {
            TextUtils.copySpansFrom((Spanned) text[i], 0, len, Object.class, ss, off);
        }
        off += len + delimiter.length();
    }
    return new SpannedString(ss);
}
Also used : SpannableString(android.text.SpannableString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned)

Example 12 with SpannedString

use of android.text.SpannedString in project android_frameworks_base by ParanoidAndroid.

the class DateFormat method format.

/**
     * Given a format string and a {@link java.util.Calendar} object, returns a CharSequence
     * containing the requested date.
     * @param inFormat the format string, as described in {@link android.text.format.DateFormat}
     * @param inDate the date to format
     * @return a {@link CharSequence} containing the requested text
     */
public static CharSequence format(CharSequence inFormat, Calendar inDate) {
    SpannableStringBuilder s = new SpannableStringBuilder(inFormat);
    int count;
    LocaleData localeData = LocaleData.get(Locale.getDefault());
    int len = inFormat.length();
    for (int i = 0; i < len; i += count) {
        count = 1;
        int c = s.charAt(i);
        if (c == QUOTE) {
            count = appendQuotedText(s, i, len);
            len = s.length();
            continue;
        }
        while ((i + count < len) && (s.charAt(i + count) == c)) {
            count++;
        }
        String replacement;
        switch(c) {
            case 'A':
            case 'a':
                replacement = localeData.amPm[inDate.get(Calendar.AM_PM) - Calendar.AM];
                break;
            case 'd':
                replacement = zeroPad(inDate.get(Calendar.DATE), count);
                break;
            case 'c':
            case 'E':
                replacement = getDayOfWeekString(localeData, inDate.get(Calendar.DAY_OF_WEEK), count, c);
                break;
            // hour in am/pm (0-11)
            case 'K':
            case // hour in am/pm (1-12)
            'h':
                {
                    int hour = inDate.get(Calendar.HOUR);
                    if (c == 'h' && hour == 0) {
                        hour = 12;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            // hour in day (0-23)
            case 'H':
            case // hour in day (1-24) [but see note below]
            'k':
                {
                    int hour = inDate.get(Calendar.HOUR_OF_DAY);
                    // times are abusing 'k'. http://b/8359981.
                    if (false && c == 'k' && hour == 0) {
                        hour = 24;
                    }
                    replacement = zeroPad(hour, count);
                }
                break;
            case 'L':
            case 'M':
                replacement = getMonthString(localeData, inDate.get(Calendar.MONTH), count, c);
                break;
            case 'm':
                replacement = zeroPad(inDate.get(Calendar.MINUTE), count);
                break;
            case 's':
                replacement = zeroPad(inDate.get(Calendar.SECOND), count);
                break;
            case 'y':
                replacement = getYearString(inDate.get(Calendar.YEAR), count);
                break;
            case 'z':
                replacement = getTimeZoneString(inDate, count);
                break;
            default:
                replacement = null;
                break;
        }
        if (replacement != null) {
            s.replace(i, i + count, replacement);
            // CARE: count is used in the for loop above
            count = replacement.length();
            len = s.length();
        }
    }
    if (inFormat instanceof Spanned) {
        return new SpannedString(s);
    } else {
        return s.toString();
    }
}
Also used : LocaleData(libcore.icu.LocaleData) SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) Spanned(android.text.Spanned) SpannableStringBuilder(android.text.SpannableStringBuilder)

Example 13 with SpannedString

use of android.text.SpannedString in project Anki-Android by Ramblurr.

the class Reviewer method updateCard.

private void updateCard(String content) {
    Log.i(AnkiDroidApp.TAG, "updateCard");
    Lookup.initialize(this, mCurrentCard.getDid());
    if (mCurrentSimpleInterface) {
        fillFlashcard(mShowAnimations);
    } 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));
        }
        String question = "";
        String answer = "";
        // prevent uninitialized variable errors
        int qa = -1;
        if (sDisplayAnswer) {
            qa = MetaDB.LANGUAGES_QA_ANSWER;
            answer = mCurrentCard.getPureAnswerForReading();
            // don't add answer sounds multiple times, such as when reshowing card after exiting editor
            if (!mAnswerSoundsAdded) {
                Sound.addSounds(mBaseUrl, answer, qa);
                mAnswerSoundsAdded = true;
            }
        } else {
            // reset sounds each time first side of card is displayed, even after leaving the editor
            Sound.resetSounds();
            mAnswerSoundsAdded = false;
            qa = MetaDB.LANGUAGES_QA_QUESTION;
            question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
            Sound.addSounds(mBaseUrl, question, qa);
        }
        content = Sound.expandSounds(mBaseUrl, content, mSpeakText, qa);
        // 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();
        mExtensions.updateCssStyle(style);
        // 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();
    }
}
Also used : SpannedString(android.text.SpannedString) FileOutputStream(java.io.FileOutputStream) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) IOException(java.io.IOException) File(java.io.File)

Example 14 with SpannedString

use of android.text.SpannedString in project Anki-Android by Ramblurr.

the class PreviewClass method updateCard.

private void updateCard(String content) {
    if (mCurrentSimpleInterface) {
        fillFlashcard(mShowAnimations);
    } 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));
        }
        String question = "";
        String answer = "";
        // prevent uninitialized variable errors
        int qa = -1;
        if (sDisplayAnswer) {
            qa = MetaDB.LANGUAGES_QA_ANSWER;
            answer = mCurrentCard.getPureAnswerForReading();
            if (!mAnswerSoundsAdded) {
                Sound.addSounds(mBaseUrl, answer, qa);
                mAnswerSoundsAdded = true;
            }
        } else {
            // reset sounds on first side of card
            Sound.resetSounds();
            mAnswerSoundsAdded = false;
            qa = MetaDB.LANGUAGES_QA_QUESTION;
            question = mCurrentCard.getQuestion(mCurrentSimpleInterface);
            Sound.addSounds(mBaseUrl, question, qa);
        }
        content = Sound.expandSounds(mBaseUrl, content, mSpeakText, qa);
        // 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;");
        // Log.i(AnkiDroidApp.TAG, "content card = \n" + content);
        StringBuilder style = new StringBuilder();
        style.append(mCustomFontStyle);
        if (mNightMode) {
            content = HtmlColors.invertColors(content);
        }
        content = SmpToHtmlEntity(content);
        mCardContent = new SpannedString(mCardTemplate.replace("::content::", content).replace("::style::", style.toString()));
        // Log.i(AnkiDroidApp.TAG, "base url = " + mBaseUrl);
        fillFlashcard(mShowAnimations);
    }
}
Also used : SpannedString(android.text.SpannedString) SpannedString(android.text.SpannedString) SpannableString(android.text.SpannableString) SuppressLint(android.annotation.SuppressLint)

Example 15 with SpannedString

use of android.text.SpannedString in project android_packages_inputmethods_LatinIME by CyanogenMod.

the class SpannableStringUtilsTests method testSplitCharSequencePreserveTrailingEmptySegmengs.

public void testSplitCharSequencePreserveTrailingEmptySegmengs() {
    assertEquals(1, SpannableStringUtils.split("", " ", false).length);
    assertEquals(1, SpannableStringUtils.split(new SpannedString(""), " ", false).length);
    assertEquals(1, SpannableStringUtils.split("", " ", true).length);
    assertEquals(1, SpannableStringUtils.split(new SpannedString(""), " ", true).length);
    assertEquals(0, SpannableStringUtils.split(" ", " ", false).length);
    assertEquals(0, SpannableStringUtils.split(new SpannedString(" "), " ", false).length);
    assertEquals(2, SpannableStringUtils.split(" ", " ", true).length);
    assertEquals(2, SpannableStringUtils.split(new SpannedString(" "), " ", true).length);
    assertEquals(3, SpannableStringUtils.split("a b c  ", " ", false).length);
    assertEquals(3, SpannableStringUtils.split(new SpannedString("a b c  "), " ", false).length);
    assertEquals(5, SpannableStringUtils.split("a b c  ", " ", true).length);
    assertEquals(5, SpannableStringUtils.split(new SpannedString("a b c  "), " ", true).length);
    assertEquals(6, SpannableStringUtils.split("a     b ", " ", false).length);
    assertEquals(6, SpannableStringUtils.split(new SpannedString("a     b "), " ", false).length);
    assertEquals(7, SpannableStringUtils.split("a     b ", " ", true).length);
    assertEquals(7, SpannableStringUtils.split(new SpannedString("a     b "), " ", true).length);
}
Also used : SpannedString(android.text.SpannedString)

Aggregations

SpannedString (android.text.SpannedString)19 Spanned (android.text.Spanned)11 SpannableStringBuilder (android.text.SpannableStringBuilder)8 SpannableString (android.text.SpannableString)7 LocaleData (libcore.icu.LocaleData)6 Test (org.junit.Test)2 SuppressLint (android.annotation.SuppressLint)1 TextPaint (android.text.TextPaint)1 AbsoluteSizeSpan (android.text.style.AbsoluteSizeSpan)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 TopDownGradientSpan (com.nolanlawson.keepscore.android.TopDownGradientSpan)1 TagHandlerReddit (com.winsonchiu.reader.data.reddit.TagHandlerReddit)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1