Fall back to en-US if localized bitmap is missing for a locale

We used to show the image for the last locale or an empty image, if
the localized image is missing for a specific locale. As the default
english one is more meaningful to users, we should just error out and
fall back to use the default locale when the image loading fails.

Bug: 128934634
Test: run graphic test, locale test
Change-Id: Iafd3e8466aec63b4952d1959b2a3d37e358677d4
This commit is contained in:
xunchang 2019-04-16 12:07:42 -07:00
parent b951138ebe
commit 9d05c8a357
4 changed files with 27 additions and 10 deletions

View file

@ -414,12 +414,18 @@ int res_create_localized_alpha_surface(const char* name,
__unused int len = row[4];
char* loc = reinterpret_cast<char*>(&row[5]);
if (y + 1 + h >= height || matches_locale(loc, locale)) {
// We need to include one additional line for the metadata of the localized image.
if (y + 1 + h > height) {
printf("Read exceeds the image boundary, y %u, h %d, height %u\n", y, h, height);
return -8;
}
if (matches_locale(loc, locale)) {
printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y);
auto surface = GRSurface::Create(w, h, w, 1);
if (!surface) {
return -8;
return -9;
}
for (int i = 0; i < h; ++i, ++y) {
@ -428,7 +434,7 @@ int res_create_localized_alpha_surface(const char* name,
}
*pSurface = surface.release();
break;
return 0;
}
for (int i = 0; i < h; ++i, ++y) {
@ -436,7 +442,7 @@ int res_create_localized_alpha_surface(const char* name,
}
}
return 0;
return -10;
}
void res_free_surface(GRSurface* surface) {

View file

@ -373,7 +373,6 @@ int main(int argc, char** argv) {
}
if (locale.empty()) {
static constexpr const char* DEFAULT_LOCALE = "en-US";
locale = DEFAULT_LOCALE;
}
}

View file

@ -27,6 +27,8 @@
#include <thread>
#include <vector>
static constexpr const char* DEFAULT_LOCALE = "en-US";
// Abstract class for controlling the user interface during recovery.
class RecoveryUI {
public:

View file

@ -817,12 +817,22 @@ std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadBitmap(const std::string& filen
std::unique_ptr<GRSurface> ScreenRecoveryUI::LoadLocalizedBitmap(const std::string& filename) {
GRSurface* surface;
if (auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface);
result < 0) {
LOG(ERROR) << "Failed to load bitmap " << filename << " (error " << result << ")";
return nullptr;
auto result = res_create_localized_alpha_surface(filename.c_str(), locale_.c_str(), &surface);
if (result == 0) {
return std::unique_ptr<GRSurface>(surface);
}
return std::unique_ptr<GRSurface>(surface);
// TODO(xunchang) create a error code enum to refine the retry condition.
LOG(WARNING) << "Failed to load bitmap " << filename << " for locale " << locale_ << " (error "
<< result << "). Falling back to use default locale.";
result = res_create_localized_alpha_surface(filename.c_str(), DEFAULT_LOCALE, &surface);
if (result == 0) {
return std::unique_ptr<GRSurface>(surface);
}
LOG(ERROR) << "Failed to load bitmap " << filename << " for locale " << DEFAULT_LOCALE
<< " (error " << result << ")";
return nullptr;
}
static char** Alloc2d(size_t rows, size_t cols) {