diff --git a/bundle.json b/bundle.json index 30d7336b2f3adc69229370dcdba3e466ea3d503f..e7b77fcbe28cc712d1f5294faa01cc07c823d033 100644 --- a/bundle.json +++ b/bundle.json @@ -52,6 +52,8 @@ "relational_store", "drivers_interface_input", "cJSON", + "i18n", + "config_policy", "selinux_adapter" ] }, diff --git a/extdevmgr.gni b/extdevmgr.gni index 2c2cf2a6ea77dbaa96821d8fae2d7d5d317b4bd6..6baa6532b672982634c1fe2bd34639893a982887 100644 --- a/extdevmgr.gni +++ b/extdevmgr.gni @@ -14,7 +14,7 @@ import("//build/ohos.gni") ext_mgr_path = "//drivers/external_device_manager" utils_path = "//drivers/external_device_manager/utils" - +ext_mgr_native_part_name = "external_device_manager" declare_args() { extdevmgr_usb_pass_through = true } diff --git a/services/BUILD.gn b/services/BUILD.gn index 3cbf50892043d60b96ec3ce596b10fe53f1290b6..f654a2160a4fa86572407f1ac0fd663fc74953b7 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -18,6 +18,7 @@ ohos_shared_library("driver_extension_manager") { sources = [ "${ext_mgr_path}/services/native/driver_extension_manager/src/driver_ext_mgr_types.cpp", "native/driver_extension_manager/src/driver_ext_mgr.cpp", + "native/driver_extension_manager/src/notification_locale.cpp", "native/driver_extension_manager/src/ext_permission_manager.cpp", ] @@ -53,6 +54,8 @@ ohos_shared_library("driver_extension_manager") { "ipc:ipc_core", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "i18n:intl_util", + "config_policy:configpolicy_util", ] cflags_cc = [ "-fno-asynchronous-unwind-tables", diff --git a/services/native/driver_extension_manager/include/notification_locale.h b/services/native/driver_extension_manager/include/notification_locale.h new file mode 100644 index 0000000000000000000000000000000000000000..95b2374c6daef532b9c58f987c78c3ed2c89752b --- /dev/null +++ b/services/native/driver_extension_manager/include/notification_locale.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DRIVER_EXTERNAL_DEVICE_NOTIFICATION_LOCALE_H +#define DRIVER_EXTERNAL_DEVICE_NOTIFICATION_LOCALE_H + +#include +#include +#include +#include +#include +#include +#include "nocopyable.h" + +namespace OHOS { +namespace ExternalDeviceManager { +class NotificationLocale : public NoCopyable { +public: + static NotificationLocale &GetInstance(); + + void ParseLocaleCfg(); + + void UpdateStringMap(); + + std::string GetStringByKey(const std::string &key); + + // base bo_CN ug zh_CN zh_HK zh_TW zz_ZX. 临时代码上库前需要删除 + void SetLanguage(const std::string &language) { + curLanguage_ = language; + } + + // 临时代码上库前需要删除 + std::string GetLanguage() const { + return curLanguage_; + } +private: + bool ParseJsonfile(const std::string &targetPath, std::unordered_map &container); + std::unordered_map languageMap_; + std::unordered_map stringMap_; + std::string localeBaseName_; + std::string curLanguage_; //临时代码上库前需要删除 + bool islanguageMapInit_ {false}; + static std::mutex mutex_; + static std::shared_ptr instance_; +}; +} // namespace ExternalDeviceManager +} // namespace OHOS + +#endif // DRIVER_EXTERNAL_DEVICE_NOTIFICATION_LOCALE_H diff --git a/services/native/driver_extension_manager/resources/BUILD.gn b/services/native/driver_extension_manager/resources/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..29f08226830d1138081715d217a0a4c03045986a --- /dev/null +++ b/services/native/driver_extension_manager/resources/BUILD.gn @@ -0,0 +1,77 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and + +import("../../../extdevmgr.gni") + +## Install locale_path.json to /system/etc/peripheral/resources/locale_path.json +ohos_prebuilt_etc("peripheral_locale_path") { + source = "locale_path.json" + relative_install_dir = "peripheral/resources" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install base/element/string.json to /system/etc/peripheral/resources/base/element/string.json +ohos_prebuilt_etc("peripheral_notification_base") { + source = "base/element/string.json" + relative_install_dir = "peripheral/resources/base/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install zh_CN/element/string.json to /system/etc/peripheral/resources/zh_CN/element/string.json +ohos_prebuilt_etc("peripheral_notification_zh_CN") { + source = "zh_CN/element/string.json" + relative_install_dir = "peripheral/resources/zh_CN/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install bo_CN/element/string.json to /system/etc/peripheral/resources/bo_CN/element/string.json +ohos_prebuilt_etc("peripheral_notification_bo_CN") { + source = "bo_CN/element/string.json" + relative_install_dir = "peripheral/resources/bo_CN/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install ug/element/string.json to /system/etc/peripheral/resources/ug/element/string.json +ohos_prebuilt_etc("peripheral_notification_ug") { + source = "ug/element/string.json" + relative_install_dir = "peripheral/resources/ug/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install zh_HK/element/string.json to /system/etc/peripheral/resources/zh_HK/element/string.json +ohos_prebuilt_etc("peripheral_notification_zh_HK") { + source = "zh_HK/element/string.json" + relative_install_dir = "peripheral/resources/zh_HK/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install zh_TW/element/string.json to /system/etc/peripheral/resources/zh_TW/element/string.json +ohos_prebuilt_etc("peripheral_notification_zh_TW") { + source = "zh_TW/element/string.json" + relative_install_dir = "peripheral/resources/zh_TW/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} + +## Install zz_ZX/element/string.json to /system/etc/peripheral/resources/zz_ZX/element/string.json +ohos_prebuilt_etc("peripheral_notification_zz_ZX") { + source = "zz_ZX/element/string.json" + relative_install_dir = "peripheral/resources/zz_ZX/element" + part_name = "${ext_mgr_native_part_name}" + subsystem_name = "hdf" +} diff --git a/services/native/driver_extension_manager/resources/bo_CN/element/string.json b/services/native/driver_extension_manager/resources/bo_CN/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cf2ee9d443a1f930f7c13d6a62fb5f73b57fe1d --- /dev/null +++ b/services/native/driver_extension_manager/resources/bo_CN/element/string.json @@ -0,0 +1,8 @@ +{ + "string":[ + { + "name":"", + "value":"" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/resources/locale_path.json b/services/native/driver_extension_manager/resources/locale_path.json new file mode 100644 index 0000000000000000000000000000000000000000..4dff61e061ba69da813a48e4a299edbf4fec20a4 --- /dev/null +++ b/services/native/driver_extension_manager/resources/locale_path.json @@ -0,0 +1,32 @@ +{ + "String":[ + { + "name":"zh-Hans-CN", + "value":"zh_CN" + }, + { + "name":"en-Latn-US", + "value": "base" + }, + { + "name":"zh-Hant-HK", + "value":"zh_HK" + }, + { + "name":"zh-Hant-TW", + "value":"zh_TW" + }, + { + "name":"ug-CN", + "value":"ug" + }, + { + "name":"bo-CN", + "value":"bo_CN" + }, + { + "name":"zz-ZX", + "value":"zz_ZX" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/resources/ug/element/string.json b/services/native/driver_extension_manager/resources/ug/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cf2ee9d443a1f930f7c13d6a62fb5f73b57fe1d --- /dev/null +++ b/services/native/driver_extension_manager/resources/ug/element/string.json @@ -0,0 +1,8 @@ +{ + "string":[ + { + "name":"", + "value":"" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/resources/zh_HK/element/string.json b/services/native/driver_extension_manager/resources/zh_HK/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cf2ee9d443a1f930f7c13d6a62fb5f73b57fe1d --- /dev/null +++ b/services/native/driver_extension_manager/resources/zh_HK/element/string.json @@ -0,0 +1,8 @@ +{ + "string":[ + { + "name":"", + "value":"" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/resources/zh_TW/element/string.json b/services/native/driver_extension_manager/resources/zh_TW/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cf2ee9d443a1f930f7c13d6a62fb5f73b57fe1d --- /dev/null +++ b/services/native/driver_extension_manager/resources/zh_TW/element/string.json @@ -0,0 +1,8 @@ +{ + "string":[ + { + "name":"", + "value":"" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/resources/zz_ZX/element/string.json b/services/native/driver_extension_manager/resources/zz_ZX/element/string.json new file mode 100644 index 0000000000000000000000000000000000000000..1cf2ee9d443a1f930f7c13d6a62fb5f73b57fe1d --- /dev/null +++ b/services/native/driver_extension_manager/resources/zz_ZX/element/string.json @@ -0,0 +1,8 @@ +{ + "string":[ + { + "name":"", + "value":"" + } + ] +} \ No newline at end of file diff --git a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp index 334346abec71d8498237bf7bf4af2394881d8965..9c93dd72bff2f09184c5c92b82db61dcade04f07 100644 --- a/services/native/driver_extension_manager/src/driver_ext_mgr.cpp +++ b/services/native/driver_extension_manager/src/driver_ext_mgr.cpp @@ -25,10 +25,12 @@ #include "hilog_wrapper.h" #include "idriver_change_callback.h" #include "iservice_registry.h" +#include "notification_locale.h" #include "system_ability_definition.h" #include "usb_device_info.h" #include "usb_driver_info.h" + namespace OHOS { namespace ExternalDeviceManager { const bool G_REGISTER_RESULT = @@ -39,8 +41,44 @@ static const std::string ACCESS_DDK_DRIVERS_PERMISSION = "ohos.permission.ACCESS DriverExtMgr::DriverExtMgr() : SystemAbility(HDF_EXTERNAL_DEVICE_MANAGER_SA_ID, true) {} DriverExtMgr::~DriverExtMgr() {} +static bool bInit = false; // 本地调试代码.上库前需要删除 + +// 临时代码上库前需要删除 +static bool TestLocal() +{ + auto& localeConfig = NotificationLocale::GetInstance(); + EDM_LOGE(MODULE_SERVICE, "[lhb] TestLocal begin."); + + std::string language; + std::string titleValue; + std::string msgValue; + std::string curTitile = "usb_transmission_error_title"; + std::string curMsg = "usb_troubleshoot_message"; + + language = "base"; + // language = "bo_CN"; + // language = "ug"; + // language = "zh_CN"; + // language = "zh_HK"; + // language = "zh_TW"; + // language = "zz_ZX"; + localeConfig.SetLanguage(language); + localeConfig.ParseLocaleCfg(); + localeConfig.UpdateStringMap(); + titleValue = localeConfig.GetStringByKey(curTitile); + msgValue = localeConfig.GetStringByKey(curMsg); + EDM_LOGI(MODULE_SERVICE, "[lhb] TestLocal language: %{public}s title: %{public}s msg: %{public}s", language.c_str(), titleValue.c_str(), msgValue.c_str()); + + bInit = true; + return true; +} void DriverExtMgr::OnStart() { + if(!bInit) { + if (!TestLocal()) { + EDM_LOGE(MODULE_SERVICE, "[lhb] DriverExtMgr OnStart failed, test local failed"); + } + } int32_t ret; EDM_LOGI(MODULE_SERVICE, "hdf_ext_devmgr OnStart"); BusExtensionCore::GetInstance().LoadBusExtensionLibs(); diff --git a/services/native/driver_extension_manager/src/notification_locale.cpp b/services/native/driver_extension_manager/src/notification_locale.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8d7cec9753978f7c65404754781cfbab02f34113 --- /dev/null +++ b/services/native/driver_extension_manager/src/notification_locale.cpp @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "notification_locale.h" +#include "config_policy_utils.h" +#include "hilog_wrapper.h" +#include "locale_config.h" +#include "locale_matcher.h" + +namespace { +constexpr const char *LOCALE_CONFIG_PATH = "/system/etc/peripheral/resources/locale_path.json"; +constexpr const char *SYSTEM_PERIPHERAL_RESOURCE_PATH = "/system/etc/peripheral/resources/"; +constexpr const char *SYSTEM_PERIPHERAL_RESOURCEEXT_PATH = "/system/etc/peripheral/resourcesExt/"; +constexpr const char *ELEMENT_STRING_FILE = "/element/string.json"; +// constexpr const char *DEFAULT_LANGUAGE_EN = "base"; //为方便本地调试临时注释。上库时需要恢复 +} // namespace + +namespace OHOS { +namespace ExternalDeviceManager { +std::shared_ptr NotificationLocale::instance_ = nullptr; +std::mutex NotificationLocale::mutex_; +NotificationLocale &NotificationLocale::GetInstance() +{ + std::lock_guard lock(mutex_); + if (instance_ == nullptr) { + instance_ = std::make_shared(); + } + return *(instance_.get()); +} + +bool NotificationLocale::ParseJsonfile( + const std::string &targetPath, std::unordered_map &container) +{ + if (access(targetPath.c_str(), F_OK) != 0) { + EDM_LOGE(MODULE_SERVICE, "[lhb] targetPath %{public}s invalid", targetPath.c_str()); + return false; + } + std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary); + std::string fileStr(std::istreambuf_iterator {inputStream}, std::istreambuf_iterator {}); + cJSON *root = cJSON_Parse(fileStr.c_str()); + if (!root) { + EDM_LOGE(MODULE_SERVICE, "%{public}s json parse error", targetPath.c_str()); + return false; + } + if (cJSON_IsNull(root) || !cJSON_IsObject(root)) { + EDM_LOGE(MODULE_SERVICE, "%{public}s json root error", targetPath.c_str()); + cJSON_Delete(root); + return false; + } + cJSON *stringConf = cJSON_GetObjectItem(root, "String"); + if (!stringConf || cJSON_IsNull(stringConf) || !cJSON_IsArray(stringConf)) { + EDM_LOGE(MODULE_SERVICE, "%{public}s stringConf invalid", targetPath.c_str()); + cJSON_Delete(root); + return false; + } + cJSON *conf = nullptr; + cJSON_ArrayForEach(conf, stringConf) + { + cJSON *nameObj = cJSON_GetObjectItemCaseSensitive(conf, "name"); + cJSON *valueObj = cJSON_GetObjectItemCaseSensitive(conf, "value"); + if (nameObj && valueObj && cJSON_IsString(nameObj) && cJSON_IsString(valueObj) && + (strlen(nameObj->valuestring) > 0) && (strlen(valueObj->valuestring) > 0)) { + container.insert(std::make_pair(nameObj->valuestring, valueObj->valuestring)); + } + } + cJSON_Delete(root); + + return true; +} + +void NotificationLocale::ParseLocaleCfg() +{ + if (islanguageMapInit_) { + return; + } + languageMap_.clear(); + if (ParseJsonfile(LOCALE_CONFIG_PATH, languageMap_)) { + islanguageMapInit_ = true; + } +} + +void NotificationLocale::UpdateStringMap() +{ + // OHOS::Global::I18n::LocaleInfo locale(Global::I18n::LocaleConfig::GetSystemLocale()); + // std::string curBaseName = locale.GetBaseName(); + // if (localeBaseName_ == curBaseName) { + // return; + // } + // BATTERY_HILOGI(COMP_SVC, "UpdateResourceMap: change from [%{public}s] to [%{public}s]", + // localeBaseName_.c_str(), curBaseName.c_str()); + // localeBaseName_ = curBaseName; + // std::string language = DEFAULT_LANGUAGE_EN; + // if (languageMap_.find(localeBaseName_) != languageMap_.end()) { + // language = languageMap_[localeBaseName_]; + // } else { + // for (auto& iter : languageMap_) { + // OHOS::Global::I18n::LocaleInfo eachLocale(iter.first); + // if (OHOS::Global::I18n::LocaleMatcher::Match(&locale, &eachLocale)) { + // language = iter.second; + // break; + // } + // } + // } + auto language = GetLanguage(); // 本地调试代码。本地调试时需要放开本行代码,注释掉L103~L130代码。上库前需要删除本行代码,恢复L103~L130代码。 + stringMap_.clear(); + std::string resourcePath = SYSTEM_PERIPHERAL_RESOURCE_PATH + language + ELEMENT_STRING_FILE; + ParseJsonfile(resourcePath, stringMap_); + resourcePath = SYSTEM_PERIPHERAL_RESOURCEEXT_PATH + language + ELEMENT_STRING_FILE; + ParseJsonfile(resourcePath, stringMap_); +} + +std::string NotificationLocale::GetStringByKey(const std::string &key) +{ + auto iter = stringMap_.find(key); + if (iter != stringMap_.end()) { + return iter->second; + } + return ""; +} +} // namespace ExternalDeviceManager +} // namespace OHOS