Java Code Examples for android.text.TextUtils#expandTemplate()

The following examples show how to use android.text.TextUtils#expandTemplate() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: delion   File: UpdatePasswordInfoBar.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns the infobar message for the case when PasswordManager has a guess which credentials
 * pair should be updated. There are two cases possible: there is only one credential pair that
 * should be updated and there several guesses available. In the first case user is presented
 * with the username of the credentials that should be updated in the second case user is able
 * to choose which of the available credentials to update.
 * It also remembered the choice made.
 */
private CharSequence createUsernameMessageText(int selectedUsername) {
    CharSequence message;
    mSelectedUsername = selectedUsername;
    final String template = getContext().getString(R.string.update_password_for_account);
    final String usernameToDisplay = mUsernames[mSelectedUsername];
    if (mShowMultipleAccounts) {
        final String downPointingArrow = " \u25BE";
        SpannableString usernameSelector =
                new SpannableString(usernameToDisplay + downPointingArrow);
        usernameSelector.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                onUsernameLinkClicked(view);
            }
        }, 0, usernameSelector.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        message = TextUtils.expandTemplate(template, mBrandingSpan, usernameSelector);
    } else {
        message = TextUtils.expandTemplate(template, mBrandingSpan, usernameToDisplay);
    }
    return message;
}
 
Example 2
Source Project: delion   File: UpdatePasswordInfoBar.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void createContent(InfoBarLayout layout) {
    super.createContent(layout);
    CharSequence message;
    if (mIsSmartLockEnabled) {
        mBrandingSpan.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                onLinkClicked();
            }
        }, 0, mBrandingSpan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
    if (mShowMultipleAccounts || mUsernames.length != 0) {
        message = createUsernameMessageText(0);
    } else {
        String template = getContext().getString(R.string.update_password);
        message = TextUtils.expandTemplate(template, mBrandingSpan);
    }

    mMessageView = layout.getMessageTextView();
    mMessageView.setText(message, TextView.BufferType.SPANNABLE);
}
 
Example 3
Source Project: delion   File: DownloadOverwriteInfoBar.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create infobar message in the form of CharSequence.
 *
 * @param context The context.
 * @param template The template CharSequence.
 * @param fileName The file name.
 * @param dirName The directory name.
 * @param dirNameIntent The intent to be launched when user touches the directory name link.
 * @return CharSequence formatted message for InfoBar.
 */
private static CharSequence formatInfoBarMessage(final Context context, String template,
        String fileName, String dirName, final Intent dirNameIntent) {
    SpannableString formattedFileName = new SpannableString(fileName);
    formattedFileName.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    SpannableString formattedDirName = new SpannableString(dirName);
    if (canResolveIntent(context, dirNameIntent)) {
        formattedDirName.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View view) {
                context.startActivity(dirNameIntent);
            }
        }, 0, dirName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    return TextUtils.expandTemplate(template, formattedFileName, formattedDirName);
}
 
Example 4
/**
 * Helper method to get the text to be displayed on the infobar.
 * @param template Message template.
 * @param fileName Name of the file.
 * @param clickableSpan Action to perform when clicking on the file name.
 * @return message to be displayed on the infobar.
 */
private CharSequence getMessageText(final String template, final String fileName,
        final ClickableSpan clickableSpan) {
    final SpannableString formattedFilePath = new SpannableString(fileName);
    formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return TextUtils.expandTemplate(template, formattedFilePath);
}
 
Example 5
Source Project: talkback   File: ParseTreeResourceNode.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates CharSequence from {@code templateString} and its {@code parameters}. And spans in
 * parameters are keep in result.
 *
 * @param templateString template string that may contains parameters with spans.
 * @param parameters object arrays that are supposed but not necessary to be Spanned. If it is
 *     Spanned, the spans are keep in result Spannable.
 * @return CharSequence that composed by formatted template string and parameters.
 */
private static CharSequence getSpannedFormattedString(
    String templateString, Object[] parameters) {
  List<CharSequence> stringTypeList = new ArrayList<>();
  for (Object param : parameters) {
    if (param instanceof CharSequence) {
      stringTypeList.add((CharSequence) param);
    }
  }

  String formattedString = String.format(templateString, parameters);
  if (stringTypeList.isEmpty()) {
    return formattedString;
  }

  CharSequence expandableTemplate = toExpandableTemplate(templateString, parameters);
  try {
    // It will throw IllegalArgumentException if the template requests a value that was not
    // provided, or if more than 9 values are provided.
    return TextUtils.expandTemplate(
        expandableTemplate, stringTypeList.toArray(new CharSequence[stringTypeList.size()]));
  } catch (IllegalArgumentException exception) {
    LogUtils.e(
        TAG,
        "TextUtils.expandTemplate fail then try copySpansFromTemplateParameters."
            + " Exception=%s ",
        exception);
    // This is a fall-back method that may copy spans inaccurately
    return copySpansFromTemplateParameters(
        formattedString, stringTypeList.toArray(new CharSequence[stringTypeList.size()]));
  }
}
 
Example 6
/**
 * Helper method to get the text to be displayed on the infobar.
 * @param template Message template.
 * @param fileName Name of the file.
 * @param clickableSpan Action to perform when clicking on the file name.
 * @return message to be displayed on the infobar.
 */
private CharSequence getMessageText(final String template, final String fileName,
        final ClickableSpan clickableSpan) {
    final SpannableString formattedFilePath = new SpannableString(fileName);
    formattedFilePath.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    formattedFilePath.setSpan(clickableSpan, 0, fileName.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return TextUtils.expandTemplate(template, formattedFilePath);
}
 
Example 7
private CharSequence getStringForLanguage(int position) {
    // The spinners prepend a string to show if they're for the source or target language.
    String language = getItem(position).toString();
    SpannableString lang = new SpannableString(language);
    lang.setSpan(new ForegroundColorSpan(Color.BLACK), 0, lang.length(), 0);
    return TextUtils.expandTemplate(mTextTemplate, lang);
}
 
Example 8
private CharSequence getStringForLanguage(int position) {
    // The spinners prepend a string to show if they're for the source or target language.
    String language = getItem(position).toString();
    SpannableString lang = new SpannableString(language);
    lang.setSpan(new ForegroundColorSpan(Color.BLACK), 0, lang.length(), 0);
    return TextUtils.expandTemplate(mTextTemplate, lang);
}