1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <unistd.h>
17 #include "notification_locale.h"
18 #include "config_policy_utils.h"
19 #include "locale_config.h"
20 #include "locale_matcher.h"
21 #include "battery_info.h"
22 #include "battery_log.h"
23 #include "power_common.h"
24
25 namespace {
26 constexpr const char* LOCALE_CONFIG_PATH = "/system/etc/battery/resources/locale_path.json";
27 constexpr const char* SYSTEM_BATTERY_RESOURCE_PATH = "/system/etc/battery/resources/";
28 constexpr const char* SYSTEM_BATTERY_RESOURCEEXT_PATH = "/system/etc/battery/resourcesExt/";
29 constexpr const char* ELEMENT_STRING_FILE = "/element/string.json";
30 constexpr const char* DEFAULT_LANGUAGE_EN = "base";
31 }
32
33 namespace OHOS {
34 namespace PowerMgr {
35 std::shared_ptr<NotificationLocale> NotificationLocale::instance_ = nullptr;
36 std::mutex NotificationLocale::mutex_;
GetInstance()37 NotificationLocale& NotificationLocale::GetInstance()
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 if (instance_ == nullptr) {
41 instance_ = std::make_shared<NotificationLocale>();
42 }
43 return *(instance_.get());
44 }
45
ParseJsonfile(const std::string & targetPath,std::unordered_map<std::string,std::string> & container)46 bool NotificationLocale::ParseJsonfile(const std::string& targetPath,
47 std::unordered_map<std::string, std::string>& container)
48 {
49 if (access(targetPath.c_str(), F_OK) != 0) {
50 BATTERY_HILOGE(COMP_SVC, "targetPath %{public}s invalid", targetPath.c_str());
51 return false;
52 }
53 std::ifstream inputStream(targetPath.c_str(), std::ios::in | std::ios::binary);
54 std::string fileStr(std::istreambuf_iterator<char> {inputStream}, std::istreambuf_iterator<char> {});
55 Json::Reader reader;
56 Json::Value root;
57 if (!reader.parse(fileStr.data(), fileStr.data() + fileStr.size(), root)) {
58 BATTERY_HILOGE(COMP_SVC, "%{public}s json parse error", targetPath.c_str());
59 return false;
60 }
61 if (root.isNull() || !root.isObject()) {
62 BATTERY_HILOGE(COMP_SVC, "%{public}s json root error", targetPath.c_str());
63 return false;
64 }
65 Json::Value stringConf = root["string"];
66 if (stringConf.isNull() || !stringConf.isArray()) {
67 BATTERY_HILOGE(COMP_SVC, "%{public}s stringConf invalid", targetPath.c_str());
68 return false;
69 }
70 for (const auto& conf : stringConf) {
71 Json::Value nameObj = conf["name"];
72 Json::Value valueObj = conf["value"];
73 if (nameObj.isString() && valueObj.isString() &&
74 !nameObj.asString().empty() && !valueObj.asString().empty()) {
75 container.insert(std::make_pair(nameObj.asString(), valueObj.asString()));
76 }
77 }
78 BATTERY_HILOGI(COMP_SVC, "%{public}s stringConf end", targetPath.c_str());
79 return true;
80 }
81
ParseLocaleCfg()82 void NotificationLocale::ParseLocaleCfg()
83 {
84 if (islanguageMapInit_) {
85 return;
86 }
87 languageMap_.clear();
88 if (ParseJsonfile(LOCALE_CONFIG_PATH, languageMap_)) {
89 islanguageMapInit_ = true;
90 }
91 }
92
UpdateStringMap()93 void NotificationLocale::UpdateStringMap()
94 {
95 OHOS::Global::I18n::LocaleInfo locale(Global::I18n::LocaleConfig::GetSystemLocale());
96 std::string curBaseName = locale.GetBaseName();
97 if (localeBaseName_ == curBaseName) {
98 return;
99 }
100 BATTERY_HILOGI(COMP_SVC, "UpdateResourceMap: change from [%{public}s] to [%{public}s]",
101 localeBaseName_.c_str(), curBaseName.c_str());
102 localeBaseName_ = curBaseName;
103 std::string language = DEFAULT_LANGUAGE_EN;
104 if (languageMap_.find(localeBaseName_) != languageMap_.end()) {
105 language = languageMap_[localeBaseName_];
106 } else {
107 for (auto& iter : languageMap_) {
108 OHOS::Global::I18n::LocaleInfo eachLocale(iter.first);
109 if (OHOS::Global::I18n::LocaleMatcher::Match(&locale, &eachLocale)) {
110 language = iter.second;
111 break;
112 }
113 }
114 }
115 stringMap_.clear();
116 std::string resourcePath = SYSTEM_BATTERY_RESOURCE_PATH + language + ELEMENT_STRING_FILE;
117 ParseJsonfile(resourcePath, stringMap_);
118 resourcePath = SYSTEM_BATTERY_RESOURCEEXT_PATH + language + ELEMENT_STRING_FILE;
119 ParseJsonfile(resourcePath, stringMap_);
120 }
121
GetStringByKey(const std::string & key)122 std::string NotificationLocale::GetStringByKey(const std::string& key)
123 {
124 auto iter = stringMap_.find(key);
125 if (iter != stringMap_.end()) {
126 return iter->second;
127 }
128 return "";
129 }
130 }
131 }