Revert "Reland "Pass all preferred locales to AssetManager""

This reverts commit 04cf00dc276789b890b7eaa717dddd85832e8151.

Reason for revert: causes b/292445325 and b/292321908

Fixes: 292445325
Fixes: 292321908
Change-Id: Idb44723ad7e8b14b737305d194f79dd70630621d
diff --git a/libs/androidfw/AssetManager2.cpp b/libs/androidfw/AssetManager2.cpp
index 6a955bc..e3f0c8f 100644
--- a/libs/androidfw/AssetManager2.cpp
+++ b/libs/androidfw/AssetManager2.cpp
@@ -91,17 +91,12 @@
   StringPoolRef entry_string_ref;
 };
 
-AssetManager2::AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration) {
-  configurations_.push_back(configuration);
-
+AssetManager2::AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration)
+    : configuration_(configuration) {
   // Don't invalidate caches here as there's nothing cached yet.
   SetApkAssets(apk_assets, false);
 }
 
-AssetManager2::AssetManager2() {
-  configurations_.resize(1);
-}
-
 bool AssetManager2::SetApkAssets(ApkAssetsList apk_assets, bool invalidate_caches) {
   BuildDynamicRefTable(apk_assets);
   RebuildFilterList();
@@ -426,16 +421,9 @@
   return false;
 }
 
-void AssetManager2::SetConfigurations(std::vector<ResTable_config> configurations) {
-  int diff = 0;
-  if (configurations_.size() != configurations.size()) {
-    diff = -1;
-  } else {
-    for (int i = 0; i < configurations_.size(); i++) {
-      diff |= configurations_[i].diff(configurations[i]);
-    }
-  }
-  configurations_ = std::move(configurations);
+void AssetManager2::SetConfiguration(const ResTable_config& configuration) {
+  const int diff = configuration_.diff(configuration);
+  configuration_ = configuration;
 
   if (diff) {
     RebuildFilterList();
@@ -632,6 +620,16 @@
 
   auto op = StartOperation();
 
+  // Might use this if density_override != 0.
+  ResTable_config density_override_config;
+
+  // Select our configuration or generate a density override configuration.
+  const ResTable_config* desired_config = &configuration_;
+  if (density_override != 0 && density_override != configuration_.density) {
+    density_override_config = configuration_;
+    density_override_config.density = density_override;
+    desired_config = &density_override_config;
+  }
 
   // Retrieve the package group from the package id of the resource id.
   if (UNLIKELY(!is_valid_resid(resid))) {
@@ -650,160 +648,119 @@
   }
 
   const PackageGroup& package_group = package_groups_[package_idx];
-  std::optional<FindEntryResult> final_result;
-  bool final_has_locale = false;
-  bool final_overlaid = false;
-  for (auto & config : configurations_) {
-    // Might use this if density_override != 0.
-    ResTable_config density_override_config;
+  auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
+                                  stop_at_first_match, ignore_configuration);
+  if (UNLIKELY(!result.has_value())) {
+    return base::unexpected(result.error());
+  }
 
-    // Select our configuration or generate a density override configuration.
-    const ResTable_config* desired_config = &config;
-    if (density_override != 0 && density_override != config.density) {
-      density_override_config = config;
-      density_override_config.density = density_override;
-      desired_config = &density_override_config;
+  bool overlaid = false;
+  if (!stop_at_first_match && !ignore_configuration) {
+    const auto& assets = GetApkAssets(result->cookie);
+    if (!assets) {
+      ALOGE("Found expired ApkAssets #%d for resource ID 0x%08x.", result->cookie, resid);
+      return base::unexpected(std::nullopt);
     }
-
-    auto result = FindEntryInternal(package_group, type_idx, entry_idx, *desired_config,
-                                    stop_at_first_match, ignore_configuration);
-    if (UNLIKELY(!result.has_value())) {
-      return base::unexpected(result.error());
-    }
-    bool overlaid = false;
-    if (!stop_at_first_match && !ignore_configuration) {
-      const auto& assets = GetApkAssets(result->cookie);
-      if (!assets) {
-        ALOGE("Found expired ApkAssets #%d for resource ID 0x%08x.", result->cookie, resid);
-        return base::unexpected(std::nullopt);
-      }
-      if (!assets->IsLoader()) {
-        for (const auto& id_map : package_group.overlays_) {
-          auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
-          if (!overlay_entry) {
-            // No id map entry exists for this target resource.
-            continue;
-          }
-          if (overlay_entry.IsInlineValue()) {
-            // The target resource is overlaid by an inline value not represented by a resource.
-            ConfigDescription best_frro_config;
-            Res_value best_frro_value;
-            bool frro_found = false;
-            for( const auto& [config, value] : overlay_entry.GetInlineValue()) {
-              if ((!frro_found || config.isBetterThan(best_frro_config, desired_config))
-                  && config.match(*desired_config)) {
-                frro_found = true;
-                best_frro_config = config;
-                best_frro_value = value;
-              }
+    if (!assets->IsLoader()) {
+      for (const auto& id_map : package_group.overlays_) {
+        auto overlay_entry = id_map.overlay_res_maps_.Lookup(resid);
+        if (!overlay_entry) {
+          // No id map entry exists for this target resource.
+          continue;
+        }
+        if (overlay_entry.IsInlineValue()) {
+          // The target resource is overlaid by an inline value not represented by a resource.
+          ConfigDescription best_frro_config;
+          Res_value best_frro_value;
+          bool frro_found = false;
+          for( const auto& [config, value] : overlay_entry.GetInlineValue()) {
+            if ((!frro_found || config.isBetterThan(best_frro_config, desired_config))
+                && config.match(*desired_config)) {
+              frro_found = true;
+              best_frro_config = config;
+              best_frro_value = value;
             }
-            if (!frro_found) {
-              continue;
-            }
-            result->entry = best_frro_value;
-            result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
-            result->cookie = id_map.cookie;
-
-            if (UNLIKELY(logging_enabled)) {
-              last_resolution_.steps.push_back(Resolution::Step{
-                  Resolution::Step::Type::OVERLAID_INLINE, result->cookie, String8()});
-              if (auto path = assets->GetPath()) {
-                const std::string overlay_path = path->data();
-                if (IsFabricatedOverlay(overlay_path)) {
-                  // FRRO don't have package name so we use the creating package here.
-                  String8 frro_name = String8("FRRO");
-                  // Get the first part of it since the expected one should be like
-                  // {overlayPackageName}-{overlayName}-{4 alphanumeric chars}.frro
-                  // under /data/resource-cache/.
-                  const std::string name = overlay_path.substr(overlay_path.rfind('/') + 1);
-                  const size_t end = name.find('-');
-                  if (frro_name.size() != overlay_path.size() && end != std::string::npos) {
-                    frro_name.append(base::StringPrintf(" created by %s",
-                                                        name.substr(0 /* pos */,
-                                                                    end).c_str()).c_str());
-                  }
-                  last_resolution_.best_package_name = frro_name;
-                } else {
-                  last_resolution_.best_package_name = result->package_name->c_str();
-                }
-              }
-              overlaid = true;
-            }
+          }
+          if (!frro_found) {
             continue;
           }
-
-          auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override,
-                                          false /* stop_at_first_match */,
-                                          false /* ignore_configuration */);
-          if (UNLIKELY(IsIOError(overlay_result))) {
-            return base::unexpected(overlay_result.error());
-          }
-          if (!overlay_result.has_value()) {
-            continue;
-          }
-
-          if (!overlay_result->config.isBetterThan(result->config, desired_config)
-              && overlay_result->config.compare(result->config) != 0) {
-            // The configuration of the entry for the overlay must be equal to or better than the
-            // target configuration to be chosen as the better value.
-            continue;
-          }
-
-          result->cookie = overlay_result->cookie;
-          result->entry = overlay_result->entry;
-          result->config = overlay_result->config;
+          result->entry = best_frro_value;
           result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
+          result->cookie = id_map.cookie;
 
           if (UNLIKELY(logging_enabled)) {
             last_resolution_.steps.push_back(
-                Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->cookie,
-                                 overlay_result->config.toString()});
-            last_resolution_.best_package_name =
-                overlay_result->package_name->c_str();
+                Resolution::Step{Resolution::Step::Type::OVERLAID_INLINE, result->cookie, String8()});
+            if (auto path = assets->GetPath()) {
+              const std::string overlay_path = path->data();
+              if (IsFabricatedOverlay(overlay_path)) {
+                // FRRO don't have package name so we use the creating package here.
+                String8 frro_name = String8("FRRO");
+                // Get the first part of it since the expected one should be like
+                // {overlayPackageName}-{overlayName}-{4 alphanumeric chars}.frro
+                // under /data/resource-cache/.
+                const std::string name = overlay_path.substr(overlay_path.rfind('/') + 1);
+                const size_t end = name.find('-');
+                if (frro_name.size() != overlay_path.size() && end != std::string::npos) {
+                  frro_name.append(base::StringPrintf(" created by %s",
+                                                      name.substr(0 /* pos */,
+                                                                  end).c_str()).c_str());
+                }
+                last_resolution_.best_package_name = frro_name;
+              } else {
+                last_resolution_.best_package_name = result->package_name->c_str();
+              }
+            }
             overlaid = true;
           }
+          continue;
+        }
+
+        auto overlay_result = FindEntry(overlay_entry.GetResourceId(), density_override,
+                                        false /* stop_at_first_match */,
+                                        false /* ignore_configuration */);
+        if (UNLIKELY(IsIOError(overlay_result))) {
+          return base::unexpected(overlay_result.error());
+        }
+        if (!overlay_result.has_value()) {
+          continue;
+        }
+
+        if (!overlay_result->config.isBetterThan(result->config, desired_config)
+            && overlay_result->config.compare(result->config) != 0) {
+          // The configuration of the entry for the overlay must be equal to or better than the target
+          // configuration to be chosen as the better value.
+          continue;
+        }
+
+        result->cookie = overlay_result->cookie;
+        result->entry = overlay_result->entry;
+        result->config = overlay_result->config;
+        result->dynamic_ref_table = id_map.overlay_res_maps_.GetOverlayDynamicRefTable();
+
+        if (UNLIKELY(logging_enabled)) {
+          last_resolution_.steps.push_back(
+              Resolution::Step{Resolution::Step::Type::OVERLAID, overlay_result->cookie,
+                               overlay_result->config.toString()});
+          last_resolution_.best_package_name =
+              overlay_result->package_name->c_str();
+          overlaid = true;
         }
       }
     }
-
-    bool has_locale = false;
-    if (result->config.locale == 0) {
-      if (default_locale_ != 0) {
-        ResTable_config conf;
-        conf.locale = default_locale_;
-        // Since we know conf has a locale and only a locale, match will tell us if that locale
-        // matches
-        has_locale = conf.match(config);
-      }
-    } else {
-      has_locale = true;
-    }
-
-    // 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) ||
-        // 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)
-            && has_locale && !final_has_locale)) {
-      final_result = result.value();
-      final_overlaid = overlaid;
-      final_has_locale = has_locale;
-    }
   }
 
   if (UNLIKELY(logging_enabled)) {
-    last_resolution_.cookie = final_result->cookie;
-    last_resolution_.type_string_ref = final_result->type_string_ref;
-    last_resolution_.entry_string_ref = final_result->entry_string_ref;
-    last_resolution_.best_config_name = final_result->config.toString();
-    if (!final_overlaid) {
-      last_resolution_.best_package_name = final_result->package_name->c_str();
+    last_resolution_.cookie = result->cookie;
+    last_resolution_.type_string_ref = result->type_string_ref;
+    last_resolution_.entry_string_ref = result->entry_string_ref;
+    last_resolution_.best_config_name = result->config.toString();
+    if (!overlaid) {
+      last_resolution_.best_package_name = result->package_name->c_str();
     }
   }
 
-  return *final_result;
+  return result;
 }
 
 base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntryInternal(
@@ -821,10 +778,8 @@
   // If `desired_config` is not the same as the set configuration or the caller will accept a value
   // from any configuration, then we cannot use our filtered list of types since it only it contains
   // types matched to the set configuration.
-  const bool use_filtered = !ignore_configuration && std::find_if(
-      configurations_.begin(), configurations_.end(),
-      [&desired_config](auto& value) { return &desired_config == &value; })
-      != configurations_.end();
+  const bool use_filtered = !ignore_configuration && &desired_config == &configuration_;
+
   const size_t package_count = package_group.packages_.size();
   for (size_t pi = 0; pi < package_count; pi++) {
     const ConfiguredPackage& loaded_package_impl = package_group.packages_[pi];
@@ -979,22 +934,10 @@
   }
 
   std::stringstream log_stream;
-  if (configurations_.size() == 1) {
-    log_stream << base::StringPrintf("Resolution for 0x%08x %s\n"
-                                     "\tFor config - %s", resid, resource_name_string.c_str(),
-                                     configurations_[0].toString().c_str());
-  } else {
-    ResTable_config conf = configurations_[0];
-    conf.clearLocale();
-    log_stream << base::StringPrintf("Resolution for 0x%08x %s\n\tFor config - %s and locales",
-                                     resid, resource_name_string.c_str(), conf.toString().c_str());
-    char str[40];
-    str[0] = '\0';
-    for(auto iter = configurations_.begin(); iter < configurations_.end(); iter++) {
-      iter->getBcp47Locale(str);
-      log_stream << base::StringPrintf(" %s%s", str, iter < configurations_.end() ? "," : "");
-    }
-  }
+  log_stream << base::StringPrintf("Resolution for 0x%08x %s\n"
+                                   "\tFor config - %s", resid, resource_name_string.c_str(),
+                                   configuration_.toString().c_str());
+
   for (const Resolution::Step& step : last_resolution_.steps) {
     constexpr static std::array kStepStrings = {
         "Found initial",
@@ -1480,14 +1423,11 @@
       package.loaded_package_->ForEachTypeSpec([&](const TypeSpec& type_spec, uint8_t type_id) {
         FilteredConfigGroup* group = nullptr;
         for (const auto& type_entry : type_spec.type_entries) {
-          for (auto & config : configurations_) {
-            if (type_entry.config.match(config)) {
-              if (!group) {
-                group = &package.filtered_configs_.editItemAt(type_id - 1);
-              }
-              group->type_entries.push_back(&type_entry);
-              break;
+          if (type_entry.config.match(configuration_)) {
+            if (!group) {
+              group = &package.filtered_configs_.editItemAt(type_id - 1);
             }
+            group->type_entries.push_back(&type_entry);
           }
         }
       });
diff --git a/libs/androidfw/ResourceTypes.cpp b/libs/androidfw/ResourceTypes.cpp
index 06d19e0..5a63612 100644
--- a/libs/androidfw/ResourceTypes.cpp
+++ b/libs/androidfw/ResourceTypes.cpp
@@ -2568,22 +2568,6 @@
     return false;
 }
 
-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);
-            }
-
-            if ((mnc != o.mnc) && requested->mnc) {
-                return (mnc);
-            }
-        }
-    }
-    return false;
-}
-
 bool ResTable_config::isBetterThan(const ResTable_config& o,
         const ResTable_config* requested) const {
     if (requested) {
diff --git a/libs/androidfw/include/androidfw/AssetManager2.h b/libs/androidfw/include/androidfw/AssetManager2.h
index d9ff35b..f611d0d 100644
--- a/libs/androidfw/include/androidfw/AssetManager2.h
+++ b/libs/androidfw/include/androidfw/AssetManager2.h
@@ -100,7 +100,7 @@
   using ApkAssetsWPtr = wp<const ApkAssets>;
   using ApkAssetsList = std::span<const ApkAssetsPtr>;
 
-  AssetManager2();
+  AssetManager2() = default;
   explicit AssetManager2(AssetManager2&& other) = default;
   AssetManager2(ApkAssetsList apk_assets, const ResTable_config& configuration);
 
@@ -156,14 +156,10 @@
 
   // Sets/resets the configuration for this AssetManager. This will cause all
   // caches that are related to the configuration change to be invalidated.
-  void SetConfigurations(std::vector<ResTable_config> configurations);
+  void SetConfiguration(const ResTable_config& configuration);
 
-  inline const std::vector<ResTable_config>& GetConfigurations() const {
-    return configurations_;
-  }
-
-  inline void SetDefaultLocale(uint32_t default_locale) {
-    default_locale_ = default_locale;
+  inline const ResTable_config& GetConfiguration() const {
+    return configuration_;
   }
 
   // Returns all configurations for which there are resources defined, or an I/O error if reading
@@ -469,11 +465,9 @@
   // without taking too much memory.
   std::array<uint8_t, std::numeric_limits<uint8_t>::max() + 1> package_ids_;
 
-  uint32_t default_locale_;
-
-  // The current configurations set for this AssetManager. When this changes, cached resources
+  // The current configuration set for this AssetManager. When this changes, cached resources
   // may need to be purged.
-  std::vector<ResTable_config> configurations_;
+  ResTable_config configuration_ = {};
 
   // Cached set of bags. These are cached because they can inherit keys from parent bags,
   // which involves some calculation.
diff --git a/libs/androidfw/include/androidfw/ResourceTypes.h b/libs/androidfw/include/androidfw/ResourceTypes.h
index 52666ab..4eb1d7a 100644
--- a/libs/androidfw/include/androidfw/ResourceTypes.h
+++ b/libs/androidfw/include/androidfw/ResourceTypes.h
@@ -1375,8 +1375,6 @@
     // 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;
-
     String8 toString() const;
 };
 
diff --git a/libs/androidfw/tests/AssetManager2_bench.cpp b/libs/androidfw/tests/AssetManager2_bench.cpp
index 2caa98c..6fae72a 100644
--- a/libs/androidfw/tests/AssetManager2_bench.cpp
+++ b/libs/androidfw/tests/AssetManager2_bench.cpp
@@ -228,12 +228,10 @@
 
   ResTable_config config;
   memset(&config, 0, sizeof(config));
-  std::vector<ResTable_config> configs;
-  configs.push_back(config);
 
   while (state.KeepRunning()) {
-    configs[0].sdkVersion = ~configs[0].sdkVersion;
-    assets.SetConfigurations(configs);
+    config.sdkVersion = ~config.sdkVersion;
+    assets.SetConfiguration(config);
   }
 }
 BENCHMARK(BM_AssetManagerSetConfigurationFramework);
diff --git a/libs/androidfw/tests/AssetManager2_test.cpp b/libs/androidfw/tests/AssetManager2_test.cpp
index c62f095..df3fa02 100644
--- a/libs/androidfw/tests/AssetManager2_test.cpp
+++ b/libs/androidfw/tests/AssetManager2_test.cpp
@@ -113,7 +113,7 @@
   desired_config.language[1] = 'e';
 
   AssetManager2 assetmanager;
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_});
 
   auto value = assetmanager.GetResource(basic::R::string::test1);
@@ -137,7 +137,7 @@
   desired_config.language[1] = 'e';
 
   AssetManager2 assetmanager;
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_, basic_de_fr_assets_});
 
   auto value = assetmanager.GetResource(basic::R::string::test1);
@@ -466,10 +466,10 @@
 TEST_F(AssetManager2Test, DensityOverride) {
   AssetManager2 assetmanager;
   assetmanager.SetApkAssets({basic_assets_, basic_xhdpi_assets_, basic_xxhdpi_assets_});
-  assetmanager.SetConfigurations({{
+  assetmanager.SetConfiguration({
     .density = ResTable_config::DENSITY_XHIGH,
     .sdkVersion = 21,
-  }});
+  });
 
   auto value = assetmanager.GetResource(basic::R::string::density, false /*may_be_bag*/);
   ASSERT_TRUE(value.has_value());
@@ -721,7 +721,7 @@
   ResTable_config desired_config;
 
   AssetManager2 assetmanager;
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_});
   assetmanager.SetResourceResolutionLoggingEnabled(false);
 
@@ -736,7 +736,7 @@
   ResTable_config desired_config;
 
   AssetManager2 assetmanager;
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_});
 
   auto result = assetmanager.GetLastResourceResolution();
@@ -751,7 +751,7 @@
 
   AssetManager2 assetmanager;
   assetmanager.SetResourceResolutionLoggingEnabled(true);
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_});
 
   auto value = assetmanager.GetResource(basic::R::string::test1);
@@ -774,7 +774,7 @@
 
   AssetManager2 assetmanager;
   assetmanager.SetResourceResolutionLoggingEnabled(true);
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_, basic_de_fr_assets_});
 
   auto value = assetmanager.GetResource(basic::R::string::test1);
@@ -796,7 +796,7 @@
 
   AssetManager2 assetmanager;
   assetmanager.SetResourceResolutionLoggingEnabled(true);
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({basic_assets_});
 
   auto value = assetmanager.GetResource(basic::R::string::test1);
@@ -817,7 +817,7 @@
 
   AssetManager2 assetmanager;
   assetmanager.SetResourceResolutionLoggingEnabled(true);
-  assetmanager.SetConfigurations({desired_config});
+  assetmanager.SetConfiguration(desired_config);
   assetmanager.SetApkAssets({overlayable_assets_});
 
   const auto map = assetmanager.GetOverlayableMapForPackage(0x7f);
diff --git a/libs/androidfw/tests/BenchmarkHelpers.cpp b/libs/androidfw/tests/BenchmarkHelpers.cpp
index 8b883f4..b97dd96 100644
--- a/libs/androidfw/tests/BenchmarkHelpers.cpp
+++ b/libs/androidfw/tests/BenchmarkHelpers.cpp
@@ -66,7 +66,7 @@
   AssetManager2 assetmanager;
   assetmanager.SetApkAssets(apk_assets);
   if (config != nullptr) {
-    assetmanager.SetConfigurations({*config});
+    assetmanager.SetConfiguration(*config);
   }
 
   while (state.KeepRunning()) {
diff --git a/libs/androidfw/tests/Theme_test.cpp b/libs/androidfw/tests/Theme_test.cpp
index 181d141..e08a6a7 100644
--- a/libs/androidfw/tests/Theme_test.cpp
+++ b/libs/androidfw/tests/Theme_test.cpp
@@ -260,7 +260,7 @@
   ResTable_config night{};
   night.uiMode = ResTable_config::UI_MODE_NIGHT_YES;
   night.version = 8u;
-  am_night.SetConfigurations({night});
+  am_night.SetConfiguration(night);
 
   auto theme = am.NewTheme();
   {