diff options
author | 2025-03-17 17:00:04 -0700 | |
---|---|---|
committer | 2025-03-17 18:12:06 -0700 | |
commit | 2e609b19a6ed19403ce7a5330058d290a6312cba (patch) | |
tree | c8df592d772599e02246541df0ca694c8cad41de | |
parent | ab6139120fd01b792519e5e800e735f46cd83d45 (diff) |
[res] Minor code cleanups
- try with resources
- use a reference for non-optional argument instead of a pointer
Test: unit tests
Flag: EXEMPT minor refactoring
Change-Id: I365b8fc2058b01edd6dd6bd7d3a131af4834d98c
-rw-r--r-- | core/java/android/app/LocaleConfig.java | 27 | ||||
-rw-r--r-- | libs/androidfw/AssetManager2.cpp | 4 | ||||
-rw-r--r-- | libs/androidfw/ResourceTypes.cpp | 16 | ||||
-rw-r--r-- | libs/androidfw/include/androidfw/ResourceTypes.h | 5 |
4 files changed, 25 insertions, 27 deletions
diff --git a/core/java/android/app/LocaleConfig.java b/core/java/android/app/LocaleConfig.java index f56bf4d434e7..cbfd7fccb7c6 100644 --- a/core/java/android/app/LocaleConfig.java +++ b/core/java/android/app/LocaleConfig.java @@ -144,15 +144,12 @@ public class LocaleConfig implements Parcelable { } } Resources res = context.getResources(); - //Get the resource id int resId = context.getApplicationInfo().getLocaleConfigRes(); if (resId == 0) { mStatus = STATUS_NOT_SPECIFIED; return; } - try { - //Get the parser to read XML data - XmlResourceParser parser = res.getXml(resId); + try (XmlResourceParser parser = res.getXml(resId)) { parseLocaleConfig(parser, res); } catch (Resources.NotFoundException e) { Slog.w(TAG, "The resource file pointed to by the given resource ID isn't found."); @@ -208,22 +205,22 @@ public class LocaleConfig implements Parcelable { String defaultLocale = null; if (android.content.res.Flags.defaultLocale()) { // Read the defaultLocale attribute of the LocaleConfig element - TypedArray att = res.obtainAttributes( - attrs, com.android.internal.R.styleable.LocaleConfig); - defaultLocale = att.getString( - R.styleable.LocaleConfig_defaultLocale); - att.recycle(); + try (TypedArray att = res.obtainAttributes( + attrs, com.android.internal.R.styleable.LocaleConfig)) { + defaultLocale = att.getString( + R.styleable.LocaleConfig_defaultLocale); + } } Set<String> localeNames = new HashSet<>(); while (XmlUtils.nextElementWithin(parser, outerDepth)) { if (TAG_LOCALE.equals(parser.getName())) { - final TypedArray attributes = res.obtainAttributes( - attrs, com.android.internal.R.styleable.LocaleConfig_Locale); - String nameAttr = attributes.getString( - com.android.internal.R.styleable.LocaleConfig_Locale_name); - localeNames.add(nameAttr); - attributes.recycle(); + try (TypedArray attributes = res.obtainAttributes( + attrs, com.android.internal.R.styleable.LocaleConfig_Locale)) { + String nameAttr = attributes.getString( + com.android.internal.R.styleable.LocaleConfig_Locale_name); + localeNames.add(nameAttr); + } } else { XmlUtils.skipCurrentTag(parser); } diff --git a/libs/androidfw/AssetManager2.cpp b/libs/androidfw/AssetManager2.cpp index 714f6e41ff05..e09ab5fd1643 100644 --- a/libs/androidfw/AssetManager2.cpp +++ b/libs/androidfw/AssetManager2.cpp @@ -879,10 +879,10 @@ base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry( // if we don't have a result yet if (!final_result || // or this config is better before the locale than the existing result - result->config.isBetterThanBeforeLocale(final_result->config, desired_config) || + result->config.isBetterThanBeforeLocale(final_result->config, *desired_config) || // or the existing config isn't better before locale and this one specifies a locale // whereas the existing one doesn't - (!final_result->config.isBetterThanBeforeLocale(result->config, desired_config) + (!final_result->config.isBetterThanBeforeLocale(result->config, *desired_config) && has_locale && !final_has_locale)) { final_result = result.value(); final_overlaid = overlaid; diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp index 8ecd6ba9b253..6ec605c2ced5 100644 --- a/libs/androidfw/ResourceTypes.cpp +++ b/libs/androidfw/ResourceTypes.cpp @@ -2615,16 +2615,14 @@ bool ResTable_config::isLocaleBetterThan(const ResTable_config& o, } bool ResTable_config::isBetterThanBeforeLocale(const ResTable_config& o, - const ResTable_config* requested) const { - if (requested) { - if (imsi || o.imsi) { - if ((mcc != o.mcc) && requested->mcc) { - return (mcc); - } + const ResTable_config& requested) const { + if (imsi || o.imsi) { + if ((mcc != o.mcc) && requested.mcc) { + return mcc; + } - if ((mnc != o.mnc) && requested->mnc) { - return (mnc); - } + if ((mnc != o.mnc) && requested.mnc) { + return mnc; } } return false; diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h index 63b28da075cd..bd72d3741460 100644 --- a/libs/androidfw/include/androidfw/ResourceTypes.h +++ b/libs/androidfw/include/androidfw/ResourceTypes.h @@ -1416,7 +1416,10 @@ struct ResTable_config // match the requested configuration at all. bool isLocaleBetterThan(const ResTable_config& o, const ResTable_config* requested) const; - bool isBetterThanBeforeLocale(const ResTable_config& o, const ResTable_config* requested) const; + // The first part of isBetterThan() that only compares the fields that are higher priority than + // the locale. Use it when you need to do custom locale matching to filter out the configs prior + // to that. + bool isBetterThanBeforeLocale(const ResTable_config& o, const ResTable_config& requested) const; String8 toString() const; |