Loading core/jni/android_util_AssetManager.cpp +6 −8 Original line number Diff line number Diff line Loading @@ -409,19 +409,17 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin configs.push_back(configuration); } uint32_t default_locale_int = 0; std::optional<ResTable_config> default_locale_opt; if (default_locale != nullptr) { ResTable_config config; static_assert(std::is_same_v<decltype(config.locale), decltype(default_locale_int)>); ScopedUtfChars locale_utf8(env, default_locale); CHECK(locale_utf8.c_str() != nullptr); config.setBcp47Locale(locale_utf8.c_str()); default_locale_int = config.locale; default_locale_opt.emplace(); default_locale_opt->setBcp47Locale(locale_utf8.c_str()); } auto assetmanager = LockAndStartAssetManager(ptr); assetmanager->SetConfigurations(std::move(configs), force_refresh != JNI_FALSE); assetmanager->SetDefaultLocale(default_locale_int); assetmanager->SetDefaultLocale(default_locale_opt); } static void NativeSetOverlayConstraints(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, Loading libs/androidfw/AssetManager2.cpp +60 −8 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <map> #include <set> #include <span> #include <sstream> #include <utility> #include "android-base/logging.h" Loading Loading @@ -441,6 +442,24 @@ bool AssetManager2::ContainsAllocatedTable() const { return false; } static std::string ConfigVecToString(std::span<const ResTable_config> configurations) { std::stringstream ss; ss << "["; bool first = true; for (const auto& config : configurations) { if (!first) { ss << ","; } char out[RESTABLE_MAX_LOCALE_LEN] = {}; config.getBcp47Locale(out); ss << out; first = false; } ss << "]"; return ss.str(); } void AssetManager2::SetConfigurations(std::span<const ResTable_config> configurations, bool force_refresh) { int diff = 0; Loading @@ -455,6 +474,17 @@ void AssetManager2::SetConfigurations(std::span<const ResTable_config> configura } } } // Log the locale list change to investigate b/392255526 if (diff & ConfigDescription::CONFIG_LOCALE) { auto oldstr = ConfigVecToString(configurations_); auto newstr = ConfigVecToString(configurations); if (oldstr != newstr) { LOG(INFO) << "AssetManager2(" << this << ") locale list changing from " << oldstr << " to " << newstr; } } configurations_.clear(); for (auto&& config : configurations) { configurations_.emplace_back(config); Loading @@ -465,6 +495,28 @@ void AssetManager2::SetConfigurations(std::span<const ResTable_config> configura } } void AssetManager2::SetDefaultLocale(std::optional<ResTable_config> default_locale) { int diff = 0; if (default_locale_ && default_locale) { diff = default_locale_->diff(default_locale.value()); } else if (default_locale_ || default_locale) { diff = -1; } if (diff & ConfigDescription::CONFIG_LOCALE) { char old_loc[RESTABLE_MAX_LOCALE_LEN] = {}; char new_loc[RESTABLE_MAX_LOCALE_LEN] = {}; if (default_locale_) { default_locale_->getBcp47Locale(old_loc); } if (default_locale) { default_locale->getBcp47Locale(new_loc); } LOG(INFO) << "AssetManager2(" << this << ") default locale changing from '" << old_loc << "' to '" << new_loc << "'"; } default_locale_ = default_locale; } void AssetManager2::SetOverlayConstraints(int32_t display_id, int32_t device_id) { bool changed = false; if (display_id_ != display_id) { Loading Loading @@ -814,11 +866,11 @@ base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry( bool has_locale = false; if (result->config.locale == 0) { if (default_locale_ != 0) { ResTable_config 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); // The default_locale_ is the locale used for any resources with no locale in the config if (default_locale_) { // Since we know default_locale_ has a locale and only a locale, match will tell us if that // locale matches has_locale = default_locale_->match(config); } } else { has_locale = true; Loading libs/androidfw/include/androidfw/AssetManager2.h +3 −4 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <array> #include <limits> #include <optional> #include <set> #include <span> #include <unordered_map> Loading Loading @@ -167,9 +168,7 @@ class AssetManager2 { return configurations_; } inline void SetDefaultLocale(uint32_t default_locale) { default_locale_ = default_locale; } void SetDefaultLocale(const std::optional<ResTable_config> default_locale); void SetOverlayConstraints(int32_t display_id, int32_t device_id); Loading Loading @@ -481,7 +480,7 @@ class AssetManager2 { // without taking too much memory. std::array<uint8_t, std::numeric_limits<uint8_t>::max() + 1> package_ids_ = {}; uint32_t default_locale_ = 0; std::optional<ResTable_config> default_locale_; // The current configurations set for this AssetManager. When this changes, cached resources // may need to be purged. Loading Loading
core/jni/android_util_AssetManager.cpp +6 −8 Original line number Diff line number Diff line Loading @@ -409,19 +409,17 @@ static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jin configs.push_back(configuration); } uint32_t default_locale_int = 0; std::optional<ResTable_config> default_locale_opt; if (default_locale != nullptr) { ResTable_config config; static_assert(std::is_same_v<decltype(config.locale), decltype(default_locale_int)>); ScopedUtfChars locale_utf8(env, default_locale); CHECK(locale_utf8.c_str() != nullptr); config.setBcp47Locale(locale_utf8.c_str()); default_locale_int = config.locale; default_locale_opt.emplace(); default_locale_opt->setBcp47Locale(locale_utf8.c_str()); } auto assetmanager = LockAndStartAssetManager(ptr); assetmanager->SetConfigurations(std::move(configs), force_refresh != JNI_FALSE); assetmanager->SetDefaultLocale(default_locale_int); assetmanager->SetDefaultLocale(default_locale_opt); } static void NativeSetOverlayConstraints(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, Loading
libs/androidfw/AssetManager2.cpp +60 −8 Original line number Diff line number Diff line Loading @@ -23,6 +23,7 @@ #include <map> #include <set> #include <span> #include <sstream> #include <utility> #include "android-base/logging.h" Loading Loading @@ -441,6 +442,24 @@ bool AssetManager2::ContainsAllocatedTable() const { return false; } static std::string ConfigVecToString(std::span<const ResTable_config> configurations) { std::stringstream ss; ss << "["; bool first = true; for (const auto& config : configurations) { if (!first) { ss << ","; } char out[RESTABLE_MAX_LOCALE_LEN] = {}; config.getBcp47Locale(out); ss << out; first = false; } ss << "]"; return ss.str(); } void AssetManager2::SetConfigurations(std::span<const ResTable_config> configurations, bool force_refresh) { int diff = 0; Loading @@ -455,6 +474,17 @@ void AssetManager2::SetConfigurations(std::span<const ResTable_config> configura } } } // Log the locale list change to investigate b/392255526 if (diff & ConfigDescription::CONFIG_LOCALE) { auto oldstr = ConfigVecToString(configurations_); auto newstr = ConfigVecToString(configurations); if (oldstr != newstr) { LOG(INFO) << "AssetManager2(" << this << ") locale list changing from " << oldstr << " to " << newstr; } } configurations_.clear(); for (auto&& config : configurations) { configurations_.emplace_back(config); Loading @@ -465,6 +495,28 @@ void AssetManager2::SetConfigurations(std::span<const ResTable_config> configura } } void AssetManager2::SetDefaultLocale(std::optional<ResTable_config> default_locale) { int diff = 0; if (default_locale_ && default_locale) { diff = default_locale_->diff(default_locale.value()); } else if (default_locale_ || default_locale) { diff = -1; } if (diff & ConfigDescription::CONFIG_LOCALE) { char old_loc[RESTABLE_MAX_LOCALE_LEN] = {}; char new_loc[RESTABLE_MAX_LOCALE_LEN] = {}; if (default_locale_) { default_locale_->getBcp47Locale(old_loc); } if (default_locale) { default_locale->getBcp47Locale(new_loc); } LOG(INFO) << "AssetManager2(" << this << ") default locale changing from '" << old_loc << "' to '" << new_loc << "'"; } default_locale_ = default_locale; } void AssetManager2::SetOverlayConstraints(int32_t display_id, int32_t device_id) { bool changed = false; if (display_id_ != display_id) { Loading Loading @@ -814,11 +866,11 @@ base::expected<FindEntryResult, NullOrIOError> AssetManager2::FindEntry( bool has_locale = false; if (result->config.locale == 0) { if (default_locale_ != 0) { ResTable_config 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); // The default_locale_ is the locale used for any resources with no locale in the config if (default_locale_) { // Since we know default_locale_ has a locale and only a locale, match will tell us if that // locale matches has_locale = default_locale_->match(config); } } else { has_locale = true; Loading
libs/androidfw/include/androidfw/AssetManager2.h +3 −4 Original line number Diff line number Diff line Loading @@ -21,6 +21,7 @@ #include <array> #include <limits> #include <optional> #include <set> #include <span> #include <unordered_map> Loading Loading @@ -167,9 +168,7 @@ class AssetManager2 { return configurations_; } inline void SetDefaultLocale(uint32_t default_locale) { default_locale_ = default_locale; } void SetDefaultLocale(const std::optional<ResTable_config> default_locale); void SetOverlayConstraints(int32_t display_id, int32_t device_id); Loading Loading @@ -481,7 +480,7 @@ class AssetManager2 { // without taking too much memory. std::array<uint8_t, std::numeric_limits<uint8_t>::max() + 1> package_ids_ = {}; uint32_t default_locale_ = 0; std::optional<ResTable_config> default_locale_; // The current configurations set for this AssetManager. When this changes, cached resources // may need to be purged. Loading