Example usage for android.text SpannableString length

List of usage examples for android.text SpannableString length

Introduction

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

Prototype

int length();

Source Link

Document

Returns the length of this character sequence.

Usage

From source file:com.strathclyde.highlightingkeyboard.SoftKeyboardService.java

/**
 * Handles the manual selection of suggestions from the suggestion bar
 * @param index the position of the suggestion in the suggestion list
 *///  w w w. jav  a  2s . co  m
public void pickSuggestionManually(int index) {
    if (mCompletionOn && mCompletions != null && index >= 0 && index < mCompletions.length) {
        CompletionInfo ci = mCompletions[index];
        ic.commitCompletion(ci);
        if (mCandidateView != null) {
            mCandidateView.clear();
        }
        updateShiftKeyState(getCurrentInputEditorInfo());
    } else if (mComposing.length() >= 0) {

        String picked;
        if (!replacemode)
            picked = suggestions.get(index) + " ";
        else
            picked = suggestions.get(index);

        //Log.i("Suggestion picked 2", picked);

        //ic.commitText(picked, picked.length());

        getCurrentInputEditorInfo();

        //colour the text according to user preferences
        SpannableString text = new SpannableString(picked);
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (sharedPrefs.getBoolean("suggestion_highlight", false))
            text.setSpan(new BackgroundColorSpan(suggestion), 0, picked.length() - 1,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        currentSession.nSuggestionsPicked++;

        if (replacemode) {
            replacemode = false;
            //remove from list of autocorrected words.
            //Log.i("PickSuggestion", "Removed "+autocorrected_words.remove(text.toString()));

            //find the current word
            extr = ic.getExtractedText(new ExtractedTextRequest(), 0);
            WordDetails w = findWord(extr.selectionStart, extr.text);
            //Log.i("FindWord", w.word+", "+w.wordStart+" - "+w.wordEnd);
            ic.setComposingRegion(w.wordStart, w.wordEnd);
            text.setSpan(null, 0, text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            /*
            //clear any spans       
            BackgroundColorSpan[] spans=(new SpannableString(extr.text.toString())).getSpans(w.wordStart, w.wordEnd, BackgroundColorSpan.class);
            for(int i=0; i<spans.length; i++){
              text.removeSpan(spans[i]);
            }*/

            //commit the update
            ic.commitText(text, 1);

        } else
            ic.commitText(text, picked.length());

        coreEngine.resetCoreString();
        coreEngine.insertText(picked);
        mComposing.setLength(0);
        updateCandidates();
    }
}

From source file:com.strathclyde.highlightingkeyboard.SoftKeyboardService.java

/**
 * handle the receipt of suggestions from the spell checker
 * colour the text in the editor as required
 * pass information to the keyboard view so it can draw the colour bar
 * initiate audio and haptic feedback as required
 */// w w  w .j  a  v  a  2 s.co m
@Override
public void onGetSuggestions(SuggestionsInfo[] results) {
    // TODO Auto-generated method stub
    int colortype = -1;
    final StringBuilder sb = new StringBuilder();

    if (updateSuggestionList) {
        updateSuggestionList = false;
        ArrayList<String> s = new ArrayList<String>();
        for (int i = 0; i < results.length; ++i) {
            final int length = results[i].getSuggestionsCount();
            for (int j = 0; j < length; ++j) {
                s.add(results[i].getSuggestionAt(j));
            }
        }
        updateSuggestionListWithSpellChecker(s);
    } else {

        for (int i = 0; i < results.length; ++i) {
            // Returned suggestions are contained in SuggestionsInfo

            final int len = results[i].getSuggestionsCount();
            sb.append("Suggestion Attribs: " + results[i].getSuggestionsAttributes());
            if ((results[i].getSuggestionsAttributes()
                    & SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) == SuggestionsInfo.RESULT_ATTR_IN_THE_DICTIONARY) {
                sb.append("The word was found in the dictionary\n");
                mInputView.wordcompletedtype = 3;
            } else {

                if ((results[i].getSuggestionsAttributes()
                        & SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) == SuggestionsInfo.RESULT_ATTR_LOOKS_LIKE_TYPO) {
                    if ((results[i].getSuggestionsAttributes()
                            & SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS) == SuggestionsInfo.RESULT_ATTR_HAS_RECOMMENDED_SUGGESTIONS) {
                        colortype = 1; //yellow
                        mInputView.wordcompletedtype = 1;
                        sb.append("There are strong candidates for this word\n");
                        currentSession.nLowErrors++;
                    } else {
                        colortype = 2; //red
                        mInputView.wordcompletedtype = 2;
                        sb.append("The word looks like a typo\n");
                        currentSession.nHighErrors++;

                    }
                }

            }

            sb.append("\n--These are the suggestions--\n");
            for (int j = 0; j < len; ++j) {
                sb.append("," + results[i].getSuggestionAt(j));
            }
            sb.append(" (" + len + ")");
        }
        //Log.i("Spelling suggestions", sb.toString());

        //this comes after a word separator, hence just add 1 to the cursor
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

        SpannableString text = new SpannableString(mComposingTemp);

        if (sharedPrefs.getBoolean("highlightwords", true)) {
            switch (colortype) {
            case 1:
                text.setSpan(new BackgroundColorSpan(small_err), 0, mComposingTemp.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                break;
            case 2:
                text.setSpan(new BackgroundColorSpan(big_err), 0, mComposingTemp.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                break;
            default:
                break;
            }
        }

        if (sharedPrefs.getBoolean("autocorrect", true) && mInputView.wordcompletedtype == 1) //handle autocorrection
        {
            SpannableString autoc = autocorrect(results);
            autocorrected_words.put(autoc.toString(), text.toString()); //autocorrected word, original input
            //Log.i("Autocorrecting","Key= "+autoc.toString()+", Value= "+text.toString());
            text = autoc;
            if (sharedPrefs.getBoolean("highlightwords", true))
                text.setSpan(new BackgroundColorSpan(autocorrect), 0, text.length(),
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mInputView.wordcompletedtype = 4;
        } else //autocorrection is turned off
        {
            if (!sharedPrefs.getBoolean("autocorrect", true) && colortype >= 1) //a mistake word
            {
                //Log.i("OnGetSentenceSuggestions","Key= "+text.toString()+", Value= "+text.toString());
                //no autocorrects, just put the word in and itself as the replacement
                autocorrected_words.put(text.toString(), text.toString());
            }
        }

        if (sharedPrefs.getBoolean("vibrator", false)) {
            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            final int on_time = Integer.parseInt(sharedPrefs.getString("shortvibe", "35"));

            switch (mInputView.wordcompletedtype) {
            case 1: //small err
                // Vibrate for 300 milliseconds
                v.vibrate(on_time);
                break;
            case 2: //big err
                //v.vibrate(Integer.parseInt(sharedPrefs.getString("longvibe", "300")));
                v.vibrate(new long[] { 0, on_time, 200, on_time }, -1);
                break;
            case 4: //autocorr
                v.vibrate(on_time);
                break;
            default:
                break;

            }
        }

        if (sharedPrefs.getBoolean("audio", false)) {
            final ToneGenerator tg = new ToneGenerator(AudioManager.STREAM_NOTIFICATION, 100);
            switch (mInputView.wordcompletedtype) {
            case 1: //small err
                tg.startTone(ToneGenerator.TONE_PROP_BEEP);
                break;
            case 2: //big err
                tg.startTone(ToneGenerator.TONE_PROP_BEEP2);
                break;
            case 4: //autocorr
                tg.startTone(ToneGenerator.TONE_PROP_BEEP);
                break;
            default:
                break;

            }
        }

        mInputView.invalidateAllKeys();
        ic.commitText(text, 1);
        sendKey(wordSeparatorKeyCode);
        coreEngine.resetCoreString();
        updateCandidates();
    }

}

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

private void updateScreenCounts() {
    if (mCurrentCard == null) {
        return;//from  ww  w . ja v  a 2s  .  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  w w . j av  a2s .  c om
    }

    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.android.mms.ui.ComposeMessageActivity.java

private final void addCallAndContactMenuItems(ContextMenu menu, MsgListMenuClickListener l,
        MessageItem msgItem) {// www. j a  v a  2  s .  com
    if (TextUtils.isEmpty(msgItem.mBody)) {
        return;
    }
    SpannableString msg = new SpannableString(msgItem.mBody);
    Linkify.addLinks(msg, Linkify.ALL);
    ArrayList<String> uris = MessageUtils.extractUris(msg.getSpans(0, msg.length(), URLSpan.class));

    // Remove any dupes so they don't get added to the menu multiple times
    HashSet<String> collapsedUris = new HashSet<String>();
    for (String uri : uris) {
        collapsedUris.add(uri.toLowerCase());
    }
    for (String uriString : collapsedUris) {
        String prefix = null;
        int sep = uriString.indexOf(":");
        if (sep >= 0) {
            prefix = uriString.substring(0, sep);
            uriString = uriString.substring(sep + 1);
        }
        Uri contactUri = null;
        boolean knownPrefix = true;
        if ("mailto".equalsIgnoreCase(prefix)) {
            contactUri = getContactUriForEmail(uriString);
        } else if ("tel".equalsIgnoreCase(prefix)) {
            contactUri = getContactUriForPhoneNumber(uriString);
        } else {
            knownPrefix = false;
        }
        if (knownPrefix && contactUri == null) {
            Intent intent = ConversationList.createAddContactIntent(uriString);

            String addContactString = getString(R.string.menu_add_address_to_contacts, uriString);
            menu.add(0, MENU_ADD_ADDRESS_TO_CONTACTS, 0, addContactString).setOnMenuItemClickListener(l)
                    .setIntent(intent);
        }
    }
}