use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.
the class InputMethodUtils method getSuitableLocalesForSpellChecker.
/**
* Returns a list of {@link Locale} in the order of appropriateness for the default spell
* checker service.
*
* <p>If the system language is English, and the region is also explicitly specified in the
* system locale, the following fallback order will be applied.</p>
* <ul>
* <li>(system-locale-language, system-locale-region, system-locale-variant) (if exists)</li>
* <li>(system-locale-language, system-locale-region)</li>
* <li>("en", "US")</li>
* <li>("en", "GB")</li>
* <li>("en")</li>
* </ul>
*
* <p>If the system language is English, but no region is specified in the system locale,
* the following fallback order will be applied.</p>
* <ul>
* <li>("en")</li>
* <li>("en", "US")</li>
* <li>("en", "GB")</li>
* </ul>
*
* <p>If the system language is not English, the following fallback order will be applied.</p>
* <ul>
* <li>(system-locale-language, system-locale-region, system-locale-variant) (if exists)</li>
* <li>(system-locale-language, system-locale-region) (if exists)</li>
* <li>(system-locale-language) (if exists)</li>
* <li>("en", "US")</li>
* <li>("en", "GB")</li>
* <li>("en")</li>
* </ul>
*
* @param systemLocale the current system locale to be taken into consideration.
* @return a list of {@link Locale}. The first one is considered to be most appropriate.
*/
@VisibleForTesting
public static ArrayList<Locale> getSuitableLocalesForSpellChecker(@Nullable final Locale systemLocale) {
final Locale systemLocaleLanguageCountryVariant;
final Locale systemLocaleLanguageCountry;
final Locale systemLocaleLanguage;
if (systemLocale != null) {
final String language = systemLocale.getLanguage();
final boolean hasLanguage = !TextUtils.isEmpty(language);
final String country = systemLocale.getCountry();
final boolean hasCountry = !TextUtils.isEmpty(country);
final String variant = systemLocale.getVariant();
final boolean hasVariant = !TextUtils.isEmpty(variant);
if (hasLanguage && hasCountry && hasVariant) {
systemLocaleLanguageCountryVariant = new Locale(language, country, variant);
} else {
systemLocaleLanguageCountryVariant = null;
}
if (hasLanguage && hasCountry) {
systemLocaleLanguageCountry = new Locale(language, country);
} else {
systemLocaleLanguageCountry = null;
}
if (hasLanguage) {
systemLocaleLanguage = new Locale(language);
} else {
systemLocaleLanguage = null;
}
} else {
systemLocaleLanguageCountryVariant = null;
systemLocaleLanguageCountry = null;
systemLocaleLanguage = null;
}
final ArrayList<Locale> locales = new ArrayList<>();
if (systemLocaleLanguageCountryVariant != null) {
locales.add(systemLocaleLanguageCountryVariant);
}
if (Locale.ENGLISH.equals(systemLocaleLanguage)) {
if (systemLocaleLanguageCountry != null) {
// - en
if (systemLocaleLanguageCountry != null) {
locales.add(systemLocaleLanguageCountry);
}
if (!LOCALE_EN_US.equals(systemLocaleLanguageCountry)) {
locales.add(LOCALE_EN_US);
}
if (!LOCALE_EN_GB.equals(systemLocaleLanguageCountry)) {
locales.add(LOCALE_EN_GB);
}
locales.add(Locale.ENGLISH);
} else {
// If the system language is English, but no region is specified, following
// fallback order will be applied.
// - en
// - en_US
// - en_GB
locales.add(Locale.ENGLISH);
locales.add(LOCALE_EN_US);
locales.add(LOCALE_EN_GB);
}
} else {
// - en
if (systemLocaleLanguageCountry != null) {
locales.add(systemLocaleLanguageCountry);
}
if (systemLocaleLanguage != null) {
locales.add(systemLocaleLanguage);
}
locales.add(LOCALE_EN_US);
locales.add(LOCALE_EN_GB);
locales.add(Locale.ENGLISH);
}
return locales;
}
use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.
the class InputMethodUtils method parseInputMethodsAndSubtypesString.
/**
* Parses the setting stored input methods and subtypes string value.
*
* @param inputMethodsAndSubtypesString The input method subtypes value stored in settings.
* @return Map from input method ID to set of input method subtypes IDs.
*/
@VisibleForTesting
public static ArrayMap<String, ArraySet<String>> parseInputMethodsAndSubtypesString(@Nullable final String inputMethodsAndSubtypesString) {
final ArrayMap<String, ArraySet<String>> imeMap = new ArrayMap<>();
if (TextUtils.isEmpty(inputMethodsAndSubtypesString)) {
return imeMap;
}
final SimpleStringSplitter typeSplitter = new SimpleStringSplitter(INPUT_METHOD_SEPARATOR);
final SimpleStringSplitter subtypeSplitter = new SimpleStringSplitter(INPUT_METHOD_SUBTYPE_SEPARATOR);
List<Pair<String, ArrayList<String>>> allImeSettings = InputMethodSettings.buildInputMethodsAndSubtypeList(inputMethodsAndSubtypesString, typeSplitter, subtypeSplitter);
for (Pair<String, ArrayList<String>> ime : allImeSettings) {
ArraySet<String> subtypes = new ArraySet<>();
if (ime.second != null) {
subtypes.addAll(ime.second);
}
imeMap.put(ime.first, subtypes);
}
return imeMap;
}
use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.
the class LocaleUtils method filterByLanguage.
/**
* Filters the given items based on language preferences.
*
* <p>For each language found in {@code preferredLanguages}, this method tries to copy at most
* one best-match item from {@code source} to {@code dest}. For example, if
* {@code "en-GB", "ja", "en-AU", "fr-CA", "en-IN"} is specified to {@code preferredLanguages},
* this method tries to copy at most one English locale, at most one Japanese, and at most one
* French locale from {@code source} to {@code dest}. Here the best matching English locale
* will be searched from {@code source} based on matching score. For the score design, see
* {@link LocaleUtils#calculateMatchingScore(ULocale, LocaleList, byte[])}</p>
*
* @param sources Source items to be filtered.
* @param extractor Type converter from the source items to {@link Locale} object.
* @param preferredLanguages Ordered list of locales with which the input items will be
* filtered.
* @param dest Destination into which the filtered items will be added.
* @param <T> Type of the data items.
*/
@VisibleForTesting
public static <T> void filterByLanguage(@NonNull List<T> sources, @NonNull LocaleExtractor<T> extractor, @NonNull LocaleList preferredLanguages, @NonNull ArrayList<T> dest) {
final HashMap<String, ScoreEntry> scoreboard = new HashMap<>();
final byte[] score = new byte[preferredLanguages.size()];
final int sourceSize = sources.size();
for (int i = 0; i < sourceSize; ++i) {
final Locale locale = extractor.get(sources.get(i));
if (locale == null || !calculateMatchingScore(ULocale.addLikelySubtags(ULocale.forLocale(locale)), preferredLanguages, score)) {
continue;
}
final String lang = locale.getLanguage();
final ScoreEntry bestScore = scoreboard.get(lang);
if (bestScore == null) {
scoreboard.put(lang, new ScoreEntry(score, i));
} else {
bestScore.updateIfBetter(score, i);
}
}
final ScoreEntry[] result = scoreboard.values().toArray(new ScoreEntry[scoreboard.size()]);
Arrays.sort(result);
for (final ScoreEntry entry : result) {
dest.add(sources.get(entry.mIndex));
}
}
use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.
the class ResourcesManager method getDisplayMetrics.
/**
* Protected so that tests can override and returns something a fixed value.
*/
@VisibleForTesting
@NonNull
protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments da) {
DisplayMetrics dm = new DisplayMetrics();
final Display display = getAdjustedDisplay(displayId, da);
if (display != null) {
display.getMetrics(dm);
} else {
dm.setToDefaults();
}
return dm;
}
use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by DirtyUnicorns.
the class RegisteredServicesCache method parseServiceInfo.
@VisibleForTesting
protected ServiceInfo<V> parseServiceInfo(ResolveInfo service) throws XmlPullParserException, IOException {
android.content.pm.ServiceInfo si = service.serviceInfo;
ComponentName componentName = new ComponentName(si.packageName, si.name);
PackageManager pm = mContext.getPackageManager();
XmlResourceParser parser = null;
try {
parser = si.loadXmlMetaData(pm, mMetaDataName);
if (parser == null) {
throw new XmlPullParserException("No " + mMetaDataName + " meta-data");
}
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
}
String nodeName = parser.getName();
if (!mAttributesName.equals(nodeName)) {
throw new XmlPullParserException("Meta-data does not start with " + mAttributesName + " tag");
}
V v = parseServiceAttributes(pm.getResourcesForApplication(si.applicationInfo), si.packageName, attrs);
if (v == null) {
return null;
}
final android.content.pm.ServiceInfo serviceInfo = service.serviceInfo;
return new ServiceInfo<V>(v, serviceInfo, componentName);
} catch (NameNotFoundException e) {
throw new XmlPullParserException("Unable to load resources for pacakge " + si.packageName);
} finally {
if (parser != null)
parser.close();
}
}
Aggregations