安卓删除系统语言代码解析

本文所在的平台是安卓9

前言:

上一篇已经介绍了安卓主要修改哪些配置文件去修改系统语言。本文就主要讲解一下代码,大家来探究一下。

上一篇:安卓增加或删除系统语言

1.找出添加语言的这个button布局 add_language

安卓删除系统语言代码解析_第1张图片

1.1在settings源码里搜索关键字 Add a language :

安卓删除系统语言代码解析_第2张图片

路径:packages/apps/Settings/res/values/strings.xml

1.2根据字符串的id搜索button  搜索 add_a_language

安卓删除系统语言代码解析_第3张图片

路径:packages/apps/Settings/res/layout/locale_order_list.xml

1.3在根据布局id定位源码    搜索add_a_languge上面的 add_language

安卓删除系统语言代码解析_第4张图片

最后图的路径:/packages/apps/Settings/localepicker/LocaleListEditor.java

 

2.寻找LocalePickerWithRegion类

路径:frameworks/base/core/java/com/android/internal/app/LocalePickerWithRegion.java

安卓删除系统语言代码解析_第5张图片

2.2找方法createLanguagePicker

安卓删除系统语言代码解析_第6张图片

2.3找方法setListener

private boolean setListener(Context context, LocaleSelectedListener listener,
            LocaleStore.LocaleInfo parent, boolean translatedOnly) {
        this.mParentLocale = parent;
        this.mListener = listener;
        this.mTranslatedOnly = translatedOnly;
        setRetainInstance(true);

        final HashSet langTagsToIgnore = new HashSet<>();
        if (!translatedOnly) {
            final LocaleList userLocales = LocalePicker.getLocales();
            final String[] langTags = userLocales.toLanguageTags().split(",");
            Collections.addAll(langTagsToIgnore, langTags);
        }

        if (parent != null) {
            mLocaleList = LocaleStore.getLevelLocales(context,
                    langTagsToIgnore, parent, translatedOnly);
            if (mLocaleList.size() <= 1) {
                if (listener != null && (mLocaleList.size() == 1)) {
                    listener.onLocaleSelected(mLocaleList.iterator().next());
                }
                return false;
            }
        } else {
            mLocaleList = LocaleStore.getLevelLocales(context, langTagsToIgnore,
                    null /* no parent */, translatedOnly);
        }

        return true;
    }

2.4搜索LocaleStore.getLevelLocales方法

 mLocaleList = LocaleStore.getLevelLocales(context, langTagsToIgnore, null /* no parent */, translatedOnly);

类路径:frameworks/base/core/java/com/android/internal/app/LocaleStore.java

public static Set getLevelLocales(Context context, Set ignorables,
            LocaleInfo parent, boolean translatedOnly) {
        fillCache(context);
        String parentId = parent == null ? null : parent.getId();

        HashSet result = new HashSet<>();
        Collection values =
                new ArrayList(sLocaleCache.values());
        for (LocaleStore.LocaleInfo li : values) {
            int level = getLevel(ignorables, li, translatedOnly);
            if (level == 2) {
                if (parent != null) { // region selection
                    if (parentId.equals(li.getParent().toLanguageTag())) {
                        result.add(li);
                    }
                } else { // language selection
                    if (li.isSuggestionOfType(LocaleInfo.SUGGESTION_TYPE_SIM)) {
                        result.add(li);
                    } else {
                        result.add(getLocaleInfo(li.getParent()));
                    }
                }
            }
        }
        return result;
    }

继续搜索:fillCache(context)

public static void fillCache(Context context) {
        if (sFullyInitialized) {
            return;
        }

        Set simCountries = getSimCountries(context);

        final boolean isInDeveloperMode = Settings.Global.getInt(context.getContentResolver(),
                Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
        for (String localeId : LocalePicker.getSupportedLocales(context)) {
            //看到这个getSupportedLocales
            //那就我们继续搜索
            if (localeId.isEmpty()) {
                throw new IllformedLocaleException("Bad locale entry in locale_config.xml");
            }
            LocaleInfo li = new LocaleInfo(localeId);

            if (LocaleList.isPseudoLocale(li.getLocale())) {
                if (isInDeveloperMode) {
                    li.setTranslated(true);
                    li.mIsPseudo = true;
                    li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
                } else {
                    // Do not display pseudolocales unless in development mode.
                    continue;
                }
            }

            if (simCountries.contains(li.getLocale().getCountry())) {
                li.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_SIM;
            }
            sLocaleCache.put(li.getId(), li);
            final Locale parent = li.getParent();
            if (parent != null) {
                String parentId = parent.toLanguageTag();
                if (!sLocaleCache.containsKey(parentId)) {
                    sLocaleCache.put(parentId, new LocaleInfo(parent));
                }
            }
        }

        // TODO: See if we can reuse what LocaleList.matchScore does
        final HashSet localizedLocales = new HashSet<>();
        for (String localeId : LocalePicker.getSystemAssetLocales()) {
            LocaleInfo li = new LocaleInfo(localeId);
            final String country = li.getLocale().getCountry();
            // All this is to figure out if we should suggest a country
            if (!country.isEmpty()) {
                LocaleInfo cachedLocale = null;
                if (sLocaleCache.containsKey(li.getId())) { // the simple case, e.g. fr-CH
                    cachedLocale = sLocaleCache.get(li.getId());
                } else { // e.g. zh-TW localized, zh-Hant-TW in cache
                    final String langScriptCtry = li.getLangScriptKey() + "-" + country;
                    if (sLocaleCache.containsKey(langScriptCtry)) {
                        cachedLocale = sLocaleCache.get(langScriptCtry);
                    }
                }
                if (cachedLocale != null) {
                    cachedLocale.mSuggestionFlags |= LocaleInfo.SUGGESTION_TYPE_CFG;
                }
            }
            localizedLocales.add(li.getLangScriptKey());
        }

        for (LocaleInfo li : sLocaleCache.values()) {
            li.setTranslated(localizedLocales.contains(li.getLangScriptKey()));
        }

        addSuggestedLocalesForRegion(Locale.getDefault());

        sFullyInitialized = true;
    }

看到for (String localeId : LocalePicker.getSupportedLocales(context)) 

2.5搜索 LocalePicker.getSupportedLocales

类路径:frameworks/base/core/java/com/android/internal/app/LocalePicker.java

 

啊,终于找到了,R.array.supported_locales,结合上一篇的文章,我们要修改的就是这个数组。

最后,希望可以帮到大家,如果有用的就收藏一下,白嫖的就点个赞。

 

你可能感兴趣的:(android,framework,android,移动开发,安卓)