Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2e609b19 authored by Yurii Zubrytskyi's avatar Yurii Zubrytskyi
Browse files

[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
parent ab613912
Loading
Loading
Loading
Loading
+12 −15
Original line number Diff line number Diff line
@@ -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);
            try (TypedArray att = res.obtainAttributes(
                    attrs, com.android.internal.R.styleable.LocaleConfig)) {
                defaultLocale = att.getString(
                        R.styleable.LocaleConfig_defaultLocale);
            att.recycle();
            }
        }

        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);
                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);
                attributes.recycle();
                }
            } else {
                XmlUtils.skipCurrentTag(parser);
            }
+2 −2
Original line number Diff line number Diff line
@@ -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;
+7 −9
Original line number Diff line number Diff line
@@ -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) {
        const ResTable_config& requested) const {
    if (imsi || o.imsi) {
            if ((mcc != o.mcc) && requested->mcc) {
                return (mcc);
        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;
+4 −1
Original line number Diff line number Diff line
@@ -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;