Search in sources :

Example 96 with VisibleForTesting

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;
}
Also used : Locale(java.util.Locale) ArrayList(java.util.ArrayList) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 97 with VisibleForTesting

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;
}
Also used : ArraySet(android.util.ArraySet) ArrayList(java.util.ArrayList) ArrayMap(android.util.ArrayMap) SimpleStringSplitter(android.text.TextUtils.SimpleStringSplitter) Pair(android.util.Pair) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 98 with VisibleForTesting

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));
    }
}
Also used : Locale(java.util.Locale) ULocale(android.icu.util.ULocale) HashMap(java.util.HashMap) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 99 with VisibleForTesting

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;
}
Also used : DisplayMetrics(android.util.DisplayMetrics) Display(android.view.Display) VisibleForTesting(com.android.internal.annotations.VisibleForTesting) NonNull(android.annotation.NonNull)

Example 100 with VisibleForTesting

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();
    }
}
Also used : XmlResourceParser(android.content.res.XmlResourceParser) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) AttributeSet(android.util.AttributeSet) ComponentName(android.content.ComponentName) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.android.internal.annotations.VisibleForTesting)141 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)18 ComponentName (android.content.ComponentName)15 File (java.io.File)14 RemoteException (android.os.RemoteException)13 ArraySet (android.util.ArraySet)10 AtomicFile (android.util.AtomicFile)10 FileInputStream (java.io.FileInputStream)10 Locale (java.util.Locale)10 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)9 FileNotFoundException (java.io.FileNotFoundException)9 Notification (android.app.Notification)6 ErrnoException (android.system.ErrnoException)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 ITransientNotification (android.app.ITransientNotification)5 UserInfo (android.content.pm.UserInfo)5