Search in sources :

Example 6 with IllegalFormatException

use of java.util.IllegalFormatException in project android_frameworks_base by ResurrectionRemix.

the class InputMethodSubtype method getDisplayName.

/**
     * Returns a display name for this subtype.
     *
     * <p>If {@code subtypeNameResId} is specified (!= 0) text generated from that resource will
     * be returned. The localized string resource of the label should be capitalized for inclusion
     * in UI lists. The string resource may contain at most one {@code %s}. If present, the
     * {@code %s} will be replaced with the display name of the subtype locale in the user's locale.
     *
     * <p>If {@code subtypeNameResId} is not specified (== 0) the framework returns the display name
     * of the subtype locale, as capitalized for use in UI lists, in the user's locale.
     *
     * @param context {@link Context} will be used for getting {@link Locale} and
     * {@link android.content.pm.PackageManager}.
     * @param packageName The package name of the input method.
     * @param appInfo The {@link ApplicationInfo} of the input method.
     * @return a display name for this subtype.
     */
@NonNull
public CharSequence getDisplayName(Context context, String packageName, ApplicationInfo appInfo) {
    if (mSubtypeNameResId == 0) {
        return getLocaleDisplayName(getLocaleFromContext(context), getLocaleObject(), DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    }
    final CharSequence subtypeName = context.getPackageManager().getText(packageName, mSubtypeNameResId, appInfo);
    if (TextUtils.isEmpty(subtypeName)) {
        return "";
    }
    final String subtypeNameString = subtypeName.toString();
    String replacementString;
    if (containsExtraValueKey(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)) {
        replacementString = getExtraValueOf(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME);
    } else {
        final DisplayContext displayContext;
        if (TextUtils.equals(subtypeNameString, "%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU;
        } else if (subtypeNameString.startsWith("%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE;
        } else {
            displayContext = DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE;
        }
        replacementString = getLocaleDisplayName(getLocaleFromContext(context), getLocaleObject(), displayContext);
    }
    if (replacementString == null) {
        replacementString = "";
    }
    try {
        return String.format(subtypeNameString, replacementString);
    } catch (IllegalFormatException e) {
        Slog.w(TAG, "Found illegal format in subtype name(" + subtypeName + "): " + e);
        return "";
    }
}
Also used : DisplayContext(android.icu.text.DisplayContext) IllegalFormatException(java.util.IllegalFormatException) NonNull(android.annotation.NonNull)

Example 7 with IllegalFormatException

use of java.util.IllegalFormatException in project android_frameworks_base by crdroidandroid.

the class InputMethodSubtype method getDisplayName.

/**
     * Returns a display name for this subtype.
     *
     * <p>If {@code subtypeNameResId} is specified (!= 0) text generated from that resource will
     * be returned. The localized string resource of the label should be capitalized for inclusion
     * in UI lists. The string resource may contain at most one {@code %s}. If present, the
     * {@code %s} will be replaced with the display name of the subtype locale in the user's locale.
     *
     * <p>If {@code subtypeNameResId} is not specified (== 0) the framework returns the display name
     * of the subtype locale, as capitalized for use in UI lists, in the user's locale.
     *
     * @param context {@link Context} will be used for getting {@link Locale} and
     * {@link android.content.pm.PackageManager}.
     * @param packageName The package name of the input method.
     * @param appInfo The {@link ApplicationInfo} of the input method.
     * @return a display name for this subtype.
     */
@NonNull
public CharSequence getDisplayName(Context context, String packageName, ApplicationInfo appInfo) {
    if (mSubtypeNameResId == 0) {
        return getLocaleDisplayName(getLocaleFromContext(context), getLocaleObject(), DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU);
    }
    final CharSequence subtypeName = context.getPackageManager().getText(packageName, mSubtypeNameResId, appInfo);
    if (TextUtils.isEmpty(subtypeName)) {
        return "";
    }
    final String subtypeNameString = subtypeName.toString();
    String replacementString;
    if (containsExtraValueKey(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME)) {
        replacementString = getExtraValueOf(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME);
    } else {
        final DisplayContext displayContext;
        if (TextUtils.equals(subtypeNameString, "%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_UI_LIST_OR_MENU;
        } else if (subtypeNameString.startsWith("%s")) {
            displayContext = DisplayContext.CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE;
        } else {
            displayContext = DisplayContext.CAPITALIZATION_FOR_MIDDLE_OF_SENTENCE;
        }
        replacementString = getLocaleDisplayName(getLocaleFromContext(context), getLocaleObject(), displayContext);
    }
    if (replacementString == null) {
        replacementString = "";
    }
    try {
        return String.format(subtypeNameString, replacementString);
    } catch (IllegalFormatException e) {
        Slog.w(TAG, "Found illegal format in subtype name(" + subtypeName + "): " + e);
        return "";
    }
}
Also used : DisplayContext(android.icu.text.DisplayContext) IllegalFormatException(java.util.IllegalFormatException) NonNull(android.annotation.NonNull)

Example 8 with IllegalFormatException

use of java.util.IllegalFormatException in project android_frameworks_base by crdroidandroid.

the class Chronometer method updateText.

private synchronized void updateText(long now) {
    mNow = now;
    long seconds = mCountDown ? mBase - now : now - mBase;
    seconds /= 1000;
    boolean negative = false;
    if (seconds < 0) {
        seconds = -seconds;
        negative = true;
    }
    String text = DateUtils.formatElapsedTime(mRecycle, seconds);
    if (negative) {
        text = getResources().getString(R.string.negative_duration, text);
    }
    if (mFormat != null) {
        Locale loc = Locale.getDefault();
        if (mFormatter == null || !loc.equals(mFormatterLocale)) {
            mFormatterLocale = loc;
            mFormatter = new Formatter(mFormatBuilder, loc);
        }
        mFormatBuilder.setLength(0);
        mFormatterArgs[0] = text;
        try {
            mFormatter.format(mFormat, mFormatterArgs);
            text = mFormatBuilder.toString();
        } catch (IllegalFormatException ex) {
            if (!mLogged) {
                Log.w(TAG, "Illegal format string: " + mFormat);
                mLogged = true;
            }
        }
    }
    setText(text);
}
Also used : Locale(java.util.Locale) Formatter(java.util.Formatter) IllegalFormatException(java.util.IllegalFormatException)

Example 9 with IllegalFormatException

use of java.util.IllegalFormatException in project android_frameworks_base by ParanoidAndroid.

the class InputMethodSubtype method getDisplayName.

/**
     * @param context Context will be used for getting Locale and PackageManager.
     * @param packageName The package name of the IME
     * @param appInfo The application info of the IME
     * @return a display name for this subtype. The string resource of the label (mSubtypeNameResId)
     * may have exactly one %s in it. If there is, the %s part will be replaced with the locale's
     * display name by the formatter. If there is not, this method returns the string specified by
     * mSubtypeNameResId. If mSubtypeNameResId is not specified (== 0), it's up to the framework to
     * generate an appropriate display name.
     */
public CharSequence getDisplayName(Context context, String packageName, ApplicationInfo appInfo) {
    final Locale locale = constructLocaleFromString(mSubtypeLocale);
    final String localeStr = locale != null ? locale.getDisplayName() : mSubtypeLocale;
    if (mSubtypeNameResId == 0) {
        return localeStr;
    }
    final CharSequence subtypeName = context.getPackageManager().getText(packageName, mSubtypeNameResId, appInfo);
    if (!TextUtils.isEmpty(subtypeName)) {
        final String replacementString = containsExtraValueKey(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME) ? getExtraValueOf(EXTRA_KEY_UNTRANSLATABLE_STRING_IN_SUBTYPE_NAME) : localeStr;
        try {
            return String.format(subtypeName.toString(), replacementString != null ? replacementString : "");
        } catch (IllegalFormatException e) {
            Slog.w(TAG, "Found illegal format in subtype name(" + subtypeName + "): " + e);
            return "";
        }
    } else {
        return localeStr;
    }
}
Also used : Locale(java.util.Locale) IllegalFormatException(java.util.IllegalFormatException)

Example 10 with IllegalFormatException

use of java.util.IllegalFormatException in project robovm by robovm.

the class OldStringTest method test_format_Locale.

@SuppressWarnings("boxing")
public void test_format_Locale() {
    Locale l = new Locale("UK");
    assertEquals("13% of sum is 0x11", String.format(l, "%d%% of %s is 0x%x", 13, "sum", 17));
    assertEquals("empty format", "", String.format("", 123, this));
    try {
        String.format(l, null, "");
        fail("NPE is expected on null format");
    } catch (NullPointerException ok) {
    }
    try {
        String.format(l, "%d", "test");
        fail("IllegalFormatException wasn't thrown.");
    } catch (IllegalFormatException ife) {
    //expected
    }
}
Also used : Locale(java.util.Locale) IllegalFormatException(java.util.IllegalFormatException)

Aggregations

IllegalFormatException (java.util.IllegalFormatException)15 Locale (java.util.Locale)9 Formatter (java.util.Formatter)7 NonNull (android.annotation.NonNull)5 DisplayContext (android.icu.text.DisplayContext)5 StringWriter (java.io.StringWriter)1 Test (org.junit.Test)1