From c53db8cd36f9b406f94038cbb09d8f1c2cd9e204 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Thu, 15 Jul 2021 21:13:03 +0900 Subject: [PATCH 1/3] Remove LocalizationChannel and move the logic to FlutterTizenEngine --- shell/platform/tizen/BUILD.gn | 2 - .../tizen/channels/localization_channel.cc | 204 ------------------ .../tizen/channels/localization_channel.h | 31 --- .../channels/localization_channel_stub.cc | 15 -- shell/platform/tizen/flutter_tizen.cc | 10 +- shell/platform/tizen/flutter_tizen_engine.cc | 115 +++++++++- shell/platform/tizen/flutter_tizen_engine.h | 7 +- 7 files changed, 115 insertions(+), 269 deletions(-) delete mode 100644 shell/platform/tizen/channels/localization_channel.cc delete mode 100644 shell/platform/tizen/channels/localization_channel.h delete mode 100644 shell/platform/tizen/channels/localization_channel_stub.cc diff --git a/shell/platform/tizen/BUILD.gn b/shell/platform/tizen/BUILD.gn index 3a2bc8ead4abd..cbd2e3bcb631f 100644 --- a/shell/platform/tizen/BUILD.gn +++ b/shell/platform/tizen/BUILD.gn @@ -127,7 +127,6 @@ template("embedder_for_profile") { sources = _flutter_tizen_source sources += [ - "channels/localization_channel.cc", "channels/platform_channel.cc", "channels/settings_channel.cc", "channels/settings_channel_tizen.cc", @@ -244,7 +243,6 @@ template("embedder_executable") { sources = _flutter_tizen_source sources += [ - "channels/localization_channel_stub.cc", "channels/platform_channel_stub.cc", "channels/settings_channel.cc", "channels/settings_channel_linux.cc", diff --git a/shell/platform/tizen/channels/localization_channel.cc b/shell/platform/tizen/channels/localization_channel.cc deleted file mode 100644 index 4a911cf94e042..0000000000000 --- a/shell/platform/tizen/channels/localization_channel.cc +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2020 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "localization_channel.h" - -#include - -#include - -#include "flutter/shell/platform/tizen/flutter_tizen_engine.h" -#include "flutter/shell/platform/tizen/tizen_log.h" -#include "rapidjson/document.h" -#include "rapidjson/writer.h" - -static constexpr char kChannelName[] = "flutter/localization"; - -namespace flutter { - -LocalizationChannel::LocalizationChannel(FlutterTizenEngine* engine) - : engine_(engine) {} - -LocalizationChannel::~LocalizationChannel() {} - -void LocalizationChannel::SendLocales() { - const char* defualt_locale = nullptr; - FlutterLocale* flutter_locale = nullptr; - std::vector flutter_locales; - - int ret = i18n_ulocale_set_default(getenv("LANG")); - ret = i18n_ulocale_get_default(&defualt_locale); - if (ret != I18N_ERROR_NONE) { - FT_LOGE("i18n_ulocale_get_default() failed."); - return; - } - - std::string without_encoding_type(defualt_locale); - auto n = without_encoding_type.find('.'); - without_encoding_type.erase(n, without_encoding_type.length() - n); - - flutter_locale = GetFlutterLocale(without_encoding_type.data()); - if (flutter_locale) { - FT_LOGI("Choose Default locale[%s]", without_encoding_type.data()); - flutter_locales.push_back(flutter_locale); - } - - int32_t count = i18n_ulocale_count_available(); - for (int i = 0; i < count; i++) { - const char* locale = i18n_ulocale_get_available(i); - if (without_encoding_type.compare(locale) != 0) { - flutter_locale = GetFlutterLocale(locale); - if (flutter_locale) { - flutter_locales.push_back(flutter_locale); - } - } - } - - FT_LOGI("Send %zu available locales", flutter_locales.size()); - // Send locales to engine - engine_->UpdateLocales( - const_cast(flutter_locales.data()), - flutter_locales.size()); - - for (auto it : flutter_locales) { - DestroyFlutterLocale(it); - } -} - -void LocalizationChannel::SendPlatformResolvedLocale() { - const char* locale; - int ret = i18n_ulocale_get_default(&locale); - if (ret != I18N_ERROR_NONE) { - FT_LOGE("i18n_ulocale_get_default() failed."); - return; - } - - FlutterLocale* flutter_locale = GetFlutterLocale(locale); - if (!flutter_locale) { - FT_LOGE("Language code is required but not present."); - return; - } - - rapidjson::Document document; - auto& allocator = document.GetAllocator(); - - document.SetObject(); - document.AddMember("method", "setPlatformResolvedLocale", allocator); - - rapidjson::Value language_code, country_code, script_code, variant_code; - language_code.SetString(flutter_locale->language_code, allocator); - country_code.SetString( - flutter_locale->country_code ? flutter_locale->country_code : "", - allocator); - script_code.SetString( - flutter_locale->script_code ? flutter_locale->script_code : "", - allocator); - variant_code.SetString( - flutter_locale->variant_code ? flutter_locale->variant_code : "", - allocator); - - rapidjson::Value args(rapidjson::kArrayType); - args.Reserve(4, allocator); - args.PushBack(language_code, allocator); - args.PushBack(country_code, allocator); - args.PushBack(script_code, allocator); - args.PushBack(variant_code, allocator); - - rapidjson::StringBuffer buffer; - rapidjson::Writer writer(buffer); - if (!document.Accept(writer)) { - FT_LOGE("document.Accept failed!"); - return; - } - - engine_->SendPlatformMessage( - kChannelName, reinterpret_cast(buffer.GetString()), - buffer.GetSize(), nullptr, nullptr); - - DestroyFlutterLocale(flutter_locale); -} - -FlutterLocale* LocalizationChannel::GetFlutterLocale(const char* locale) { - int capacity = 128; - char buffer[128] = {0}; - int bufSize; - int error; - char* language = nullptr; - char* country = nullptr; - char* script = nullptr; - char* variant = nullptr; - - // set language code, language code is a required field - error = i18n_ulocale_get_language(locale, buffer, capacity, &bufSize); - if (error == I18N_ERROR_NONE && bufSize > 0) { - language = new char[bufSize + 1]; - memcpy(language, buffer, bufSize); - language[bufSize] = '\0'; - } else { - FT_LOGE("i18n_ulocale_get_language failed!"); - return nullptr; - } - - // set country code, country code is an optional field - bufSize = i18n_ulocale_get_country(locale, buffer, capacity, &error); - if (error == I18N_ERROR_NONE && bufSize > 0) { - country = new char[bufSize + 1]; - memcpy(country, buffer, bufSize); - country[bufSize] = '\0'; - } - - // set script code, script code is an optional field - bufSize = i18n_ulocale_get_script(locale, buffer, capacity); - if (bufSize > 0) { - script = new char[bufSize + 1]; - memcpy(script, buffer, bufSize); - script[bufSize] = '\0'; - } - - // set variant code, variant code is an optional field - bufSize = i18n_ulocale_get_variant(locale, buffer, capacity); - if (bufSize > 0) { - variant = new char[bufSize + 1]; - memcpy(variant, buffer, bufSize); - variant[bufSize] = '\0'; - } - - FlutterLocale* flutter_locale = new FlutterLocale; - flutter_locale->struct_size = sizeof(FlutterLocale); - flutter_locale->language_code = language; - flutter_locale->country_code = country; - flutter_locale->script_code = script; - flutter_locale->variant_code = variant; - - return flutter_locale; -} - -void LocalizationChannel::DestroyFlutterLocale(FlutterLocale* flutter_locale) { - if (flutter_locale) { - if (flutter_locale->language_code) { - delete[] flutter_locale->language_code; - flutter_locale->language_code = nullptr; - } - - if (flutter_locale->country_code) { - delete[] flutter_locale->country_code; - flutter_locale->country_code = nullptr; - } - - if (flutter_locale->script_code) { - delete[] flutter_locale->script_code; - flutter_locale->script_code = nullptr; - } - - if (flutter_locale->variant_code) { - delete[] flutter_locale->variant_code; - flutter_locale->variant_code = nullptr; - } - - delete flutter_locale; - flutter_locale = nullptr; - } -} - -} // namespace flutter diff --git a/shell/platform/tizen/channels/localization_channel.h b/shell/platform/tizen/channels/localization_channel.h deleted file mode 100644 index a5743a4cd3da4..0000000000000 --- a/shell/platform/tizen/channels/localization_channel.h +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright 2020 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef EMBEDDER_LOCALIZATION_CHANNEL_H_ -#define EMBEDDER_LOCALIZATION_CHANNEL_H_ - -#include "flutter/shell/platform/embedder/embedder.h" - -namespace flutter { - -class FlutterTizenEngine; - -class LocalizationChannel { - public: - explicit LocalizationChannel(FlutterTizenEngine* engine); - virtual ~LocalizationChannel(); - - void SendLocales(); - - private: - void SendPlatformResolvedLocale(); - FlutterLocale* GetFlutterLocale(const char* locale); - void DestroyFlutterLocale(FlutterLocale* flutter_locale); - - FlutterTizenEngine* engine_; -}; - -} // namespace flutter - -#endif // EMBEDDER_LOCALIZATION_CHANNEL_H_ diff --git a/shell/platform/tizen/channels/localization_channel_stub.cc b/shell/platform/tizen/channels/localization_channel_stub.cc deleted file mode 100644 index 810b10b60e14b..0000000000000 --- a/shell/platform/tizen/channels/localization_channel_stub.cc +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "localization_channel.h" - -namespace flutter { - -LocalizationChannel::LocalizationChannel(FlutterTizenEngine* engine) {} - -LocalizationChannel::~LocalizationChannel() {} - -void LocalizationChannel::SendLocales() {} - -} // namespace flutter diff --git a/shell/platform/tizen/flutter_tizen.cc b/shell/platform/tizen/flutter_tizen.cc index 90e28f3a57308..fa8a1c72f9fbc 100644 --- a/shell/platform/tizen/flutter_tizen.cc +++ b/shell/platform/tizen/flutter_tizen.cc @@ -117,7 +117,11 @@ void FlutterDesktopMessengerSetCallback(FlutterDesktopMessengerRef messenger, } void FlutterDesktopNotifyLocaleChange(FlutterDesktopEngineRef engine) { - EngineFromHandle(engine)->localization_channel->SendLocales(); + EngineFromHandle(engine)->SetupLocales(); +} + +void FlutterDesktopNotifyLowMemoryWarning(FlutterDesktopEngineRef engine) { + EngineFromHandle(engine)->NotifyLowMemoryWarning(); } void FlutterDesktopNotifyAppIsInactive(FlutterDesktopEngineRef engine) { @@ -136,10 +140,6 @@ void FlutterDesktopNotifyAppIsDetached(FlutterDesktopEngineRef engine) { EngineFromHandle(engine)->lifecycle_channel->AppIsDetached(); } -void FlutterDesktopNotifyLowMemoryWarning(FlutterDesktopEngineRef engine) { - EngineFromHandle(engine)->NotifyLowMemoryWarning(); -} - void FlutterRegisterViewFactory( FlutterDesktopPluginRegistrarRef registrar, const char* view_type, diff --git a/shell/platform/tizen/flutter_tizen_engine.cc b/shell/platform/tizen/flutter_tizen_engine.cc index 378f2769bec7d..d96886184a743 100644 --- a/shell/platform/tizen/flutter_tizen_engine.cc +++ b/shell/platform/tizen/flutter_tizen_engine.cc @@ -5,6 +5,10 @@ #include "flutter_tizen_engine.h" +#ifndef __X64_SHELL__ +#include +#endif + #include #include #include @@ -31,6 +35,33 @@ constexpr double kProfileFactor = 2.0; constexpr double kProfileFactor = 1.0; #endif +#ifndef __X64_SHELL__ +struct LocaleInfo { + std::string language; + std::string country; + std::string script; + std::string variant; +}; + +// Converts a LocaleInfo struct to a FlutterLocale struct. |info| must outlive +// the returned value, since the returned FlutterLocale has pointers into it. +FlutterLocale CovertToFlutterLocale(const LocaleInfo& info) { + FlutterLocale locale = {}; + locale.struct_size = sizeof(FlutterLocale); + locale.language_code = info.language.c_str(); + if (!info.country.empty()) { + locale.country_code = info.country.c_str(); + } + if (!info.script.empty()) { + locale.script_code = info.script.c_str(); + } + if (!info.variant.empty()) { + locale.variant_code = info.variant.c_str(); + } + return locale; +} +#endif + } // namespace FlutterTizenEngine::FlutterTizenEngine(const FlutterProjectBundle& project) @@ -92,10 +123,6 @@ void FlutterTizenEngine::InitializeRenderer(int32_t x, #endif } -void FlutterTizenEngine::NotifyLowMemoryWarning() { - embedder_api_.NotifyLowMemoryWarning(engine_); -} - bool FlutterTizenEngine::RunEngine() { if (engine_ != nullptr) { FT_LOGE("The engine has already started."); @@ -218,8 +245,6 @@ bool FlutterTizenEngine::RunEngine() { internal_plugin_registrar_->messenger(), renderer.get()); settings_channel = std::make_unique( internal_plugin_registrar_->messenger()); - localization_channel = std::make_unique(this); - localization_channel->SendLocales(); lifecycle_channel = std::make_unique( internal_plugin_registrar_->messenger()); @@ -239,6 +264,8 @@ bool FlutterTizenEngine::RunEngine() { SetWindowOrientation(0); } + SetupLocales(); + return true; } @@ -388,9 +415,79 @@ void FlutterTizenEngine::OnVsync(intptr_t baton, frame_target_time_nanos); } -void FlutterTizenEngine::UpdateLocales(const FlutterLocale** locales, - size_t locales_count) { - embedder_api_.UpdateLocales(engine_, locales, locales_count); +void FlutterTizenEngine::SetupLocales() { +#ifndef __X64_SHELL__ + i18n_ulocale_set_default(getenv("LANG")); + + const char* locale_id; + int ret = i18n_ulocale_get_default(&locale_id); + if (ret != I18N_ERROR_NONE) { + FT_LOGE("i18n_ulocale_get_default() failed."); + return; + } + std::string preferred_locale(locale_id); + preferred_locale = preferred_locale.substr(0, preferred_locale.find(".")); + + std::vector locales; + int32_t count = i18n_ulocale_count_available(); + locales.reserve(count); + for (int i = 0; i < count; i++) { + LocaleInfo locale; + int ret; + char buffer[128] = {0}; + int32_t size; + + // The "language" field is required. + locale_id = i18n_ulocale_get_available(i); + ret = i18n_ulocale_get_language(locale_id, buffer, sizeof(buffer), &size); + if (ret != I18N_ERROR_NONE || size == 0) { + continue; + } + locale.language = std::string(buffer, size); + + // "country", "script", and "variant" are optional. + size = i18n_ulocale_get_country(locale_id, buffer, sizeof(buffer), &ret); + if (ret == I18N_ERROR_NONE && size > 0) { + locale.country = std::string(buffer, size); + } + size = i18n_ulocale_get_script(locale_id, buffer, sizeof(buffer)); + if (size > 0) { + locale.script = std::string(buffer, size); + } + size = i18n_ulocale_get_variant(locale_id, buffer, sizeof(buffer)); + if (size > 0) { + locale.variant = std::string(buffer, size); + } + + if (preferred_locale.compare(locale_id) == 0) { + locales.insert(locales.begin(), locale); + } else { + locales.push_back(locale); + } + } + FT_LOGI("Found %zu locales.", locales.size()); + + // The locale list should be converted into the FlutterLocale list, and again + // to the FlutterLocale* list. + std::vector flutter_locales; + flutter_locales.reserve(locales.size()); + for (const auto& info : locales) { + flutter_locales.push_back(CovertToFlutterLocale(info)); + } + std::vector flutter_locale_list; + flutter_locale_list.reserve(flutter_locales.size()); + std::transform( + flutter_locales.begin(), flutter_locales.end(), + std::back_inserter(flutter_locale_list), + [](const auto& arg) -> const auto* { return &arg; }); + + embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(), + flutter_locale_list.size()); +#endif +} + +void FlutterTizenEngine::NotifyLowMemoryWarning() { + embedder_api_.NotifyLowMemoryWarning(engine_); } bool FlutterTizenEngine::RegisterExternalTexture(int64_t texture_id) { diff --git a/shell/platform/tizen/flutter_tizen_engine.h b/shell/platform/tizen/flutter_tizen_engine.h index 163f2a99d20f5..9ab3dcc8934b0 100644 --- a/shell/platform/tizen/flutter_tizen_engine.h +++ b/shell/platform/tizen/flutter_tizen_engine.h @@ -13,7 +13,6 @@ #include "flutter/shell/platform/embedder/embedder.h" #include "flutter/shell/platform/tizen/channels/key_event_channel.h" #include "flutter/shell/platform/tizen/channels/lifecycle_channel.h" -#include "flutter/shell/platform/tizen/channels/localization_channel.h" #include "flutter/shell/platform/tizen/channels/navigation_channel.h" #include "flutter/shell/platform/tizen/channels/platform_channel.h" #include "flutter/shell/platform/tizen/channels/platform_view_channel.h" @@ -109,8 +108,11 @@ class FlutterTizenEngine : public TizenRenderer::Delegate { uint64_t frame_start_time_nanos, uint64_t frame_target_time_nanos); + // Passes locale information to the Flutter engine. + void SetupLocales(); + + // Posts a low memory notification to the Flutter engine. void NotifyLowMemoryWarning(); - void UpdateLocales(const FlutterLocale** locales, size_t locales_count); // Attempts to register the texture with the given |texture_id|. bool RegisterExternalTexture(int64_t texture_id); @@ -134,7 +136,6 @@ class FlutterTizenEngine : public TizenRenderer::Delegate { // The system channels for communicating between Flutter and the platform. std::unique_ptr key_event_channel; std::unique_ptr lifecycle_channel; - std::unique_ptr localization_channel; std::unique_ptr navigation_channel; std::unique_ptr platform_channel; std::unique_ptr settings_channel; From 2a72658c011e69108f0ba3e83c9f8691ce3ef229 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Fri, 16 Jul 2021 15:35:19 +0900 Subject: [PATCH 2/3] Create system_utils and add the desktop implementation --- shell/platform/tizen/BUILD.gn | 2 + shell/platform/tizen/flutter_tizen_engine.cc | 78 ++------------------ shell/platform/tizen/system_utils.h | 27 +++++++ shell/platform/tizen/system_utils_linux.cc | 56 ++++++++++++++ shell/platform/tizen/system_utils_tizen.cc | 68 +++++++++++++++++ 5 files changed, 160 insertions(+), 71 deletions(-) create mode 100644 shell/platform/tizen/system_utils.h create mode 100644 shell/platform/tizen/system_utils_linux.cc create mode 100644 shell/platform/tizen/system_utils_tizen.cc diff --git a/shell/platform/tizen/BUILD.gn b/shell/platform/tizen/BUILD.gn index cbd2e3bcb631f..4358b39e2e0b0 100644 --- a/shell/platform/tizen/BUILD.gn +++ b/shell/platform/tizen/BUILD.gn @@ -132,6 +132,7 @@ template("embedder_for_profile") { "channels/settings_channel_tizen.cc", "external_texture_pixel_gl.cc", "external_texture_surface_gl.cc", + "system_utils_tizen.cc", "tizen_log.cc", ] @@ -248,6 +249,7 @@ template("embedder_executable") { "channels/settings_channel_linux.cc", "external_texture_pixel_gl_stub.cc", "external_texture_surface_gl_stub.cc", + "system_utils_linux.cc", "tizen_log_stub.cc", "tizen_renderer_evas_gl.cc", ] diff --git a/shell/platform/tizen/flutter_tizen_engine.cc b/shell/platform/tizen/flutter_tizen_engine.cc index d96886184a743..73fba07c66f11 100644 --- a/shell/platform/tizen/flutter_tizen_engine.cc +++ b/shell/platform/tizen/flutter_tizen_engine.cc @@ -5,14 +5,11 @@ #include "flutter_tizen_engine.h" -#ifndef __X64_SHELL__ -#include -#endif - #include #include #include +#include "flutter/shell/platform/tizen/system_utils.h" #include "flutter/shell/platform/tizen/tizen_log.h" namespace flutter { @@ -35,17 +32,9 @@ constexpr double kProfileFactor = 2.0; constexpr double kProfileFactor = 1.0; #endif -#ifndef __X64_SHELL__ -struct LocaleInfo { - std::string language; - std::string country; - std::string script; - std::string variant; -}; - -// Converts a LocaleInfo struct to a FlutterLocale struct. |info| must outlive +// Converts a LanguageInfo struct to a FlutterLocale struct. |info| must outlive // the returned value, since the returned FlutterLocale has pointers into it. -FlutterLocale CovertToFlutterLocale(const LocaleInfo& info) { +FlutterLocale CovertToFlutterLocale(const LanguageInfo& info) { FlutterLocale locale = {}; locale.struct_size = sizeof(FlutterLocale); locale.language_code = info.language.c_str(); @@ -60,7 +49,6 @@ FlutterLocale CovertToFlutterLocale(const LocaleInfo& info) { } return locale; } -#endif } // namespace @@ -416,64 +404,13 @@ void FlutterTizenEngine::OnVsync(intptr_t baton, } void FlutterTizenEngine::SetupLocales() { -#ifndef __X64_SHELL__ - i18n_ulocale_set_default(getenv("LANG")); - - const char* locale_id; - int ret = i18n_ulocale_get_default(&locale_id); - if (ret != I18N_ERROR_NONE) { - FT_LOGE("i18n_ulocale_get_default() failed."); - return; - } - std::string preferred_locale(locale_id); - preferred_locale = preferred_locale.substr(0, preferred_locale.find(".")); - - std::vector locales; - int32_t count = i18n_ulocale_count_available(); - locales.reserve(count); - for (int i = 0; i < count; i++) { - LocaleInfo locale; - int ret; - char buffer[128] = {0}; - int32_t size; - - // The "language" field is required. - locale_id = i18n_ulocale_get_available(i); - ret = i18n_ulocale_get_language(locale_id, buffer, sizeof(buffer), &size); - if (ret != I18N_ERROR_NONE || size == 0) { - continue; - } - locale.language = std::string(buffer, size); - - // "country", "script", and "variant" are optional. - size = i18n_ulocale_get_country(locale_id, buffer, sizeof(buffer), &ret); - if (ret == I18N_ERROR_NONE && size > 0) { - locale.country = std::string(buffer, size); - } - size = i18n_ulocale_get_script(locale_id, buffer, sizeof(buffer)); - if (size > 0) { - locale.script = std::string(buffer, size); - } - size = i18n_ulocale_get_variant(locale_id, buffer, sizeof(buffer)); - if (size > 0) { - locale.variant = std::string(buffer, size); - } - - if (preferred_locale.compare(locale_id) == 0) { - locales.insert(locales.begin(), locale); - } else { - locales.push_back(locale); - } - } - FT_LOGI("Found %zu locales.", locales.size()); - - // The locale list should be converted into the FlutterLocale list, and again - // to the FlutterLocale* list. + std::vector languages = GetPreferredLanguageInfo(); std::vector flutter_locales; - flutter_locales.reserve(locales.size()); - for (const auto& info : locales) { + flutter_locales.reserve(languages.size()); + for (const auto& info : languages) { flutter_locales.push_back(CovertToFlutterLocale(info)); } + // Convert the locale list to the locale pointer list that must be provided. std::vector flutter_locale_list; flutter_locale_list.reserve(flutter_locales.size()); std::transform( @@ -483,7 +420,6 @@ void FlutterTizenEngine::SetupLocales() { embedder_api_.UpdateLocales(engine_, flutter_locale_list.data(), flutter_locale_list.size()); -#endif } void FlutterTizenEngine::NotifyLowMemoryWarning() { diff --git a/shell/platform/tizen/system_utils.h b/shell/platform/tizen/system_utils.h new file mode 100644 index 0000000000000..d67131a186040 --- /dev/null +++ b/shell/platform/tizen/system_utils.h @@ -0,0 +1,27 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EMBEDDER_SYSTEM_UTILS_H_ +#define EMBEDDER_SYSTEM_UTILS_H_ + +#include +#include + +namespace flutter { + +// Components of a system language/locale. +struct LanguageInfo { + std::string language; + std::string country; + std::string script; + std::string variant; +}; + +// Returns the list of user-preferred locales, in preference order, +// parsed into LanguageInfo structures. +std::vector GetPreferredLanguageInfo(); + +} // namespace flutter + +#endif // EMBEDDER_SYSTEM_UTILS_H_ diff --git a/shell/platform/tizen/system_utils_linux.cc b/shell/platform/tizen/system_utils_linux.cc new file mode 100644 index 0000000000000..85b0d211ca407 --- /dev/null +++ b/shell/platform/tizen/system_utils_linux.cc @@ -0,0 +1,56 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "system_utils.h" + +#include + +namespace flutter { + +namespace { + +const char* GetLocaleStringFromEnvironment() { + const char* retval; + retval = getenv("LANGUAGE"); + if (retval && (retval[0] != '\0')) { + return retval; + } + retval = getenv("LC_ALL"); + if (retval && (retval[0] != '\0')) { + return retval; + } + retval = getenv("LC_MESSAGES"); + if (retval && (retval[0] != '\0')) { + return retval; + } + retval = getenv("LANG"); + if (retval && (retval[0] != '\0')) { + return retval; + } + return ""; +} + +} // namespace + +std::vector GetPreferredLanguageInfo() { + std::vector languages; + + std::string locale(GetLocaleStringFromEnvironment()); + if (locale.empty()) { + // This is the default locale if none is specified according to ISO C. + locale = "C"; + } + LanguageInfo info; + size_t country_pos = locale.find("_"); + size_t codeset_pos = locale.find("."); + info.language = locale.substr(0, country_pos); + if (country_pos != std::string::npos) { + info.country = locale.substr(country_pos + 1, codeset_pos); + } + languages.push_back(info); + + return languages; +} + +} // namespace flutter diff --git a/shell/platform/tizen/system_utils_tizen.cc b/shell/platform/tizen/system_utils_tizen.cc new file mode 100644 index 0000000000000..75e6c226499b8 --- /dev/null +++ b/shell/platform/tizen/system_utils_tizen.cc @@ -0,0 +1,68 @@ +// Copyright 2021 Samsung Electronics Co., Ltd. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "system_utils.h" + +#include + +#include "flutter/shell/platform/tizen/tizen_log.h" + +namespace flutter { + +std::vector GetPreferredLanguageInfo() { + std::vector languages; + const char* locale; + + i18n_ulocale_set_default(getenv("LANG")); + int ret = i18n_ulocale_get_default(&locale); + if (ret != I18N_ERROR_NONE) { + FT_LOGE("i18n_ulocale_get_default() failed."); + return languages; + } + std::string preferred_locale(locale); + size_t codeset_pos = preferred_locale.find("."); + preferred_locale = preferred_locale.substr(0, codeset_pos); + + int32_t count = i18n_ulocale_count_available(); + languages.reserve(count); + for (int i = 0; i < count; i++) { + LanguageInfo info; + int ret; + char buffer[128] = {0}; + int32_t size; + + // The "language" field is required. + locale = i18n_ulocale_get_available(i); + ret = i18n_ulocale_get_language(locale, buffer, sizeof(buffer), &size); + if (ret != I18N_ERROR_NONE || size == 0) { + continue; + } + info.language = std::string(buffer, size); + + // "country", "script", and "variant" are optional. + size = i18n_ulocale_get_country(locale, buffer, sizeof(buffer), &ret); + if (ret == I18N_ERROR_NONE && size > 0) { + info.country = std::string(buffer, size); + } + size = i18n_ulocale_get_script(locale, buffer, sizeof(buffer)); + if (size > 0) { + info.script = std::string(buffer, size); + } + size = i18n_ulocale_get_variant(locale, buffer, sizeof(buffer)); + if (size > 0) { + info.variant = std::string(buffer, size); + } + + if (preferred_locale.compare(locale) == 0) { + languages.insert(languages.begin(), info); + } else { + languages.push_back(info); + } + } + FT_LOGI("Found %zu locales.", languages.size()); + + return languages; +} + +} // namespace flutter From 3e487875fd552bacd4a8039e3f7cab21e8cf36a3 Mon Sep 17 00:00:00 2001 From: Swift Kim Date: Mon, 19 Jul 2021 15:17:16 +0900 Subject: [PATCH 3/3] Organize includes --- shell/platform/tizen/channels/settings_channel_linux.cc | 3 +-- shell/platform/tizen/channels/settings_channel_tizen.cc | 3 ++- shell/platform/tizen/flutter_tizen_engine_unittest.cc | 4 ++-- .../tizen/flutter_tizen_texture_registrar_unittests.cc | 5 +++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/shell/platform/tizen/channels/settings_channel_linux.cc b/shell/platform/tizen/channels/settings_channel_linux.cc index f11ef08a51929..2fda077428526 100644 --- a/shell/platform/tizen/channels/settings_channel_linux.cc +++ b/shell/platform/tizen/channels/settings_channel_linux.cc @@ -4,8 +4,7 @@ #include "settings_channel.h" -#include -#include +#include namespace flutter { diff --git a/shell/platform/tizen/channels/settings_channel_tizen.cc b/shell/platform/tizen/channels/settings_channel_tizen.cc index 9253b22c67506..f5343d6c3bdf1 100644 --- a/shell/platform/tizen/channels/settings_channel_tizen.cc +++ b/shell/platform/tizen/channels/settings_channel_tizen.cc @@ -2,9 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include #include "settings_channel.h" +#include + namespace flutter { static void OnSettingsChangedCallback(system_settings_key_e key, diff --git a/shell/platform/tizen/flutter_tizen_engine_unittest.cc b/shell/platform/tizen/flutter_tizen_engine_unittest.cc index 2ffd863c03a10..33b278ad25b31 100644 --- a/shell/platform/tizen/flutter_tizen_engine_unittest.cc +++ b/shell/platform/tizen/flutter_tizen_engine_unittest.cc @@ -2,11 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include +#include "flutter/shell/platform/tizen/flutter_tizen_engine.h" #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" -#include "flutter/shell/platform/tizen/flutter_tizen_engine.h" #include "flutter/shell/platform/tizen/testing/engine_modifier.h" +#include "gtest/gtest.h" namespace flutter { namespace testing { diff --git a/shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc b/shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc index 803c8b0ccef7e..38d0a88793eda 100644 --- a/shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc +++ b/shell/platform/tizen/flutter_tizen_texture_registrar_unittests.cc @@ -2,13 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include +#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h" + #include #include "flutter/shell/platform/embedder/test_utils/proc_table_replacement.h" #include "flutter/shell/platform/tizen/flutter_tizen_engine.h" -#include "flutter/shell/platform/tizen/flutter_tizen_texture_registrar.h" #include "flutter/shell/platform/tizen/testing/engine_modifier.h" +#include "gtest/gtest.h" namespace flutter { namespace testing {