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.screenspeak.formatter.TouchExplorationSystemUiFormatter.java

@Override
public boolean format(AccessibilityEvent event, ScreenSpeakService context, Utterance utterance) {
    final SpannableStringBuilder recordText = new SpannableStringBuilder();
    final List<CharSequence> entries = AccessibilityEventCompat.getRecord(event, 0).getText();

    // Reverse the entries so that time is read aloud first.
    Collections.reverse(entries);

    for (final CharSequence entry : entries) {
        StringBuilderUtils.appendWithSeparator(recordText, entry);
    }//from  ww w.j a  va  2s .co  m

    // Don't populate with empty text. This should never happen!
    if (TextUtils.isEmpty(recordText))
        return false;

    // Don't speak the same utterance twice.
    if (TextUtils.equals(mLastUtteranceText, recordText))
        return false;

    utterance.addSpoken(recordText);
    utterance.addHaptic(R.array.view_hovered_pattern);
    utterance.addAuditory(R.raw.focus);

    mLastUtteranceText.clear();
    mLastUtteranceText.append(recordText);

    return true;
}

From source file:com.andrada.sitracker.ui.fragment.AboutDialog.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    PackageManager pm = getActivity().getPackageManager();
    String packageName = getActivity().getPackageName();
    String versionName;//from  w w  w.  ja va 2 s.  c o  m
    try {
        PackageInfo info = pm.getPackageInfo(packageName, 0);
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        versionName = VERSION_UNAVAILABLE;
    }

    SpannableStringBuilder aboutBody = new SpannableStringBuilder();
    SpannableString licensesLink = new SpannableString(getString(R.string.about_licenses));
    licensesLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showOpenSourceLicenses(getActivity());
        }
    }, 0, licensesLink.length(), 0);
    SpannableString whatsNewLink = new SpannableString(getString(R.string.whats_new));
    whatsNewLink.setSpan(new ClickableSpan() {
        @Override
        public void onClick(View view) {
            showWhatsNew(getActivity());
        }
    }, 0, whatsNewLink.length(), 0);
    aboutBody.append(licensesLink);
    aboutBody.append("\n\n");
    aboutBody.append(whatsNewLink);

    AboutDialogView aboutBodyView = AboutDialogView_.build(getActivity());
    aboutBodyView.bindData(getString(R.string.app_version_format, versionName), aboutBody);

    return new AlertDialog.Builder(getActivity()).setTitle(R.string.action_about).setView(aboutBodyView)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.dismiss();
                }
            }).create();
}

From source file:com.android.utils.AccessibilityEventUtils.java

/**
 * Gets the text of an <code>event</code> by concatenating the text members
 * (regardless of their priority) using space as a delimiter.
 *
 * @param event The event./* www . j  ava  2  s  . c o m*/
 * @return The event text.
 */
public static CharSequence getEventAggregateText(AccessibilityEvent event) {
    if (event == null) {
        return null;
    }

    final SpannableStringBuilder aggregator = new SpannableStringBuilder();
    for (CharSequence text : event.getText()) {
        StringBuilderUtils.appendWithSeparator(aggregator, text);
    }

    return aggregator;
}

From source file:com.google.android.marvin.mytalkback.speechrules.RuleSwitch.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = (!TextUtils.isEmpty(node.getText())) ? node.getText()
            : AccessibilityEventUtils.getEventAggregateText(event);
    final CharSequence contentDescription = node.getContentDescription();

    // Prepend any contentDescription, if present
    StringBuilderUtils.appendWithSeparator(output, contentDescription);

    // Append node or event text
    final CharSequence switchDescription = context.getString(R.string.template_switch,
            (!TextUtils.isEmpty(text)) ? text : "");
    StringBuilderUtils.appendWithSeparator(output, switchDescription);

    // The text should contain the current state.  Explicitly speak state for ToggleButtons.
    if (TextUtils.isEmpty(text) || AccessibilityNodeInfoUtils.nodeMatchesClassByType(context, node,
            android.widget.ToggleButton.class)) {
        final CharSequence state = context
                .getString(node.isChecked() ? R.string.value_checked : R.string.value_not_checked);
        StringBuilderUtils.appendWithSeparator(output, state);
    }/*w w  w  . jav a2 s .c om*/

    return output;
}

From source file:com.android.screenspeak.speechrules.RuleSwitch.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = (!TextUtils.isEmpty(node.getText())) ? node.getText()
            : AccessibilityEventUtils.getEventAggregateText(event);
    final CharSequence contentDescription = node.getContentDescription();

    // Prepend any contentDescription, if present
    StringBuilderUtils.appendWithSeparator(output, contentDescription);

    // Append node or event text
    final String switchDescription = context.getString(R.string.template_switch,
            (!TextUtils.isEmpty(text)) ? text : "");
    final Spannable spannableSwitchDescription = StringBuilderUtils
            .createSpannableFromTextWithTemplate(switchDescription, text);
    StringBuilderUtils.appendWithSeparator(output, spannableSwitchDescription);

    // The text should contain the current state.  Explicitly speak state for ToggleButtons.
    if (TextUtils.isEmpty(text)
            || AccessibilityNodeInfoUtils.nodeMatchesClassByType(node, ToggleButton.class)) {
        final CharSequence state = context
                .getString(node.isChecked() ? R.string.value_checked : R.string.value_not_checked);
        StringBuilderUtils.appendWithSeparator(output, state);
    }/*w  w w.j  a v a  2s  .  c  o m*/

    return output;
}

From source file:com.android.talkback.speechrules.RuleSeekBar.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    if (node == null || !node.isAccessibilityFocused()) {
        return "";
    }//w w w . j  av a 2s . co m

    final SpannableStringBuilder output = new SpannableStringBuilder();

    final CharSequence text = AccessibilityNodeInfoUtils.getNodeText(node);
    final CharSequence roleText = Role.getRoleDescriptionOrDefault(context, node);

    StringBuilderUtils.append(output, text, roleText);

    // TODO: We need to be getting this information from the node.
    if ((event != null) && (event.getItemCount() > 0)) {
        final int percent = (100 * event.getCurrentItemIndex()) / event.getItemCount();
        final CharSequence formattedPercent = context.getString(R.string.template_percent, percent);

        StringBuilderUtils.appendWithSeparator(output, formattedPercent);
    }

    return output;
}

From source file:com.android.screenspeak.speechrules.RuleSeekBar.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    if (node == null || !node.isAccessibilityFocused()) {
        return "";
    }/*from  w w w  . ja va 2 s.  co  m*/

    final SpannableStringBuilder output = new SpannableStringBuilder();
    final CharSequence text = super.format(context, node, event);
    final String formattedText = context.getString(R.string.template_seek_bar, text);
    final Spannable spannableFormattedText = StringBuilderUtils
            .createSpannableFromTextWithTemplate(formattedText, text);

    StringBuilderUtils.appendWithSeparator(output, spannableFormattedText);

    // TODO: We need to be getting this information from the node.
    if ((event != null) && (event.getItemCount() > 0)) {
        final int percent = (100 * event.getCurrentItemIndex()) / event.getItemCount();
        final CharSequence formattedPercent = context.getString(R.string.template_percent, percent);

        StringBuilderUtils.appendWithSeparator(output, formattedPercent);
    }

    return output;
}

From source file:com.google.android.marvin.mytalkback.speechrules.RuleDefault.java

@Override
public CharSequence getHintText(Context context, AccessibilityNodeInfoCompat node) {
    // Disabled actionable items don't have any hint text.
    if (AccessibilityNodeInfoUtils.isActionableForAccessibility(node) && !node.isEnabled()) {
        return context.getString(R.string.value_disabled);
    }//from  ww  w .  j a v  a2  s  .co  m

    final SpannableStringBuilder builder = new SpannableStringBuilder();
    final int actions = node.getActions();

    // Don't read both the checkable AND clickable hints!
    if (node.isCheckable()) {
        StringBuilderUtils.appendWithSeparator(builder,
                NodeHintHelper.getHintString(context, R.string.template_hint_checkable));
    } else if (AccessibilityNodeInfoUtils.isClickable(node)) {
        StringBuilderUtils.appendWithSeparator(builder,
                NodeHintHelper.getHintString(context, R.string.template_hint_clickable));
    }

    // Long clickable is long.
    if (AccessibilityNodeInfoUtils.isLongClickable(node)) {
        StringBuilderUtils.appendWithSeparator(builder,
                NodeHintHelper.getHintString(context, R.string.template_hint_long_clickable));
    }

    return builder;
}

From source file:com.android.talkback.speechrules.RuleEditText.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    final CharSequence text = getText(context, node);
    boolean isCurrentlyEditing = node.isFocused();
    if (hasWindowSupport()) {
        isCurrentlyEditing = isCurrentlyEditing && isInputWindowOnScreen();
    }//w w  w  . j  av a2s .  c  o  m

    SpannableStringBuilder output = new SpannableStringBuilder();

    CharSequence roleText = Role.getRoleDescriptionOrDefault(context, node);
    StringBuilderUtils.append(output, roleText);

    if (isCurrentlyEditing) {
        CharSequence editing = context.getString(R.string.value_edit_box_editing);
        StringBuilderUtils.append(output, editing);
    }
    if (TalkBackService.getInstance() != null
            && TalkBackService.getInstance().getCursorController().isSelectionModeActive()) {
        StringBuilderUtils.append(output, context.getString(R.string.notification_type_selection_mode_on));
    }

    StringBuilderUtils.append(output, text);

    return output;
}

From source file:com.android.talkback.speechrules.RulePagerPage.java

@Override
public CharSequence format(Context context, AccessibilityNodeInfoCompat node, AccessibilityEvent event) {
    SpannableStringBuilder output = new SpannableStringBuilder();

    final CharSequence nodeText = AccessibilityNodeInfoUtils.getNodeText(node);
    final CharSequence roleText;
    if (node.getRoleDescription() != null) {
        roleText = node.getRoleDescription();
    } else {/*from   w  ww  .  java  2  s.  co m*/
        roleText = context.getString(R.string.value_pager_page);
    }

    StringBuilderUtils.append(output, nodeText, roleText);

    return output;
}