mirror of
https://github.com/openharmony/powermgr_battery_manager.git
synced 2026-07-01 06:42:05 -04:00
!792 merge master into master
电源需求补充11种语言 Created-by: Fan-jixiang Commit-by: Fan-jixiang Merged-by: openharmony_ci Description: > 请检查PR是否满足[《代码合入流程指导和规范》](https://gitee.com/openharmony/powermgr_power_manager/wikis/issue-commit-pr-standard) , 否则将不会审查通过。 **【关联Issue】** https://gitcode.com/openharmony/powermgr_battery_manager/issues/635 **【修改描述】** 电源需求补充11种语言 **【验证结果】** 不涉及 See merge request: openharmony/powermgr_battery_manager!792
This commit is contained in:
@@ -30,6 +30,7 @@ static const std::string BATTERY_NOTIFICATION_SYS_ABILITY_NAME = "";
|
||||
|
||||
void NotificationCenter::CreateBaseStyle(const BatteryConfig::NotificationConf& nCfg)
|
||||
{
|
||||
SetAlertOnce();
|
||||
SetNotificationId(nCfg.name);
|
||||
SetContent(nCfg.title, nCfg.text);
|
||||
SetCreatorUid();
|
||||
@@ -68,6 +69,11 @@ void NotificationCenter::SetContent(const std::string& title, const std::string&
|
||||
request_.SetContent(notificationContent);
|
||||
}
|
||||
|
||||
void NotificationCenter::SetAlertOnce()
|
||||
{
|
||||
request_.SetAlertOneTime(true);
|
||||
}
|
||||
|
||||
void NotificationCenter::SetCreatorUid()
|
||||
{
|
||||
request_.SetCreatorUid(BATTERY_NOTIFICATION_SYS_ABILITY_ID);
|
||||
|
||||
@@ -34,6 +34,8 @@ public:
|
||||
|
||||
void SetActionButton(const std::string& buttonName, const std::string& buttonAction) override;
|
||||
protected:
|
||||
void SetAlertOnce();
|
||||
|
||||
void SetNotificationId(const std::string& popupName);
|
||||
|
||||
void SetContent(const std::string& title, const std::string& text);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <cJSON.h>
|
||||
#include <securec.h>
|
||||
|
||||
#include "notification_locale.h"
|
||||
#include "config_policy_utils.h"
|
||||
@@ -24,6 +25,7 @@
|
||||
#include "battery_log.h"
|
||||
#include "battery_mgr_cjson_utils.h"
|
||||
#include "power_common.h"
|
||||
#include "battery_srv_client.h"
|
||||
|
||||
namespace {
|
||||
constexpr const char* LOCALE_CONFIG_PATH = "/system/etc/battery/resources/locale_path.json";
|
||||
@@ -31,6 +33,10 @@ constexpr const char* SYSTEM_BATTERY_RESOURCE_PATH = "/system/etc/battery/resour
|
||||
constexpr const char* SYSTEM_BATTERY_RESOURCEEXT_PATH = "/system/etc/battery/resourcesExt/";
|
||||
constexpr const char* ELEMENT_STRING_FILE = "/element/string.json";
|
||||
constexpr const char* DEFAULT_LANGUAGE_EN = "base";
|
||||
constexpr const char* REVERSE_CHARGE_WITH_POWER_DISPLAY_TEXT_KEY =
|
||||
"reverse_super_charge_with_power_display_start_text";
|
||||
constexpr int32_t MAX_BUFFER_SIZE = 1024;
|
||||
constexpr char WATT = 'W';
|
||||
}
|
||||
|
||||
namespace OHOS {
|
||||
@@ -136,9 +142,40 @@ std::string NotificationLocale::GetStringByKey(const std::string& key)
|
||||
{
|
||||
auto iter = stringMap_.find(key);
|
||||
if (iter != stringMap_.end()) {
|
||||
if (key == REVERSE_CHARGE_WITH_POWER_DISPLAY_TEXT_KEY) {
|
||||
return FillTextWithPower(iter->second);
|
||||
}
|
||||
return iter->second;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string NotificationLocale::FillTextWithPower(const std::string& text)
|
||||
{
|
||||
std::string powerStr;
|
||||
BatterySrvClient::GetInstance().GetBatteryConfig("rchg_charging_power", powerStr);
|
||||
BATTERY_HILOGI(COMP_SVC, "rchg_charging_power: %{public}s", powerStr.c_str());
|
||||
if (powerStr.empty()) {
|
||||
return text;
|
||||
}
|
||||
powerStr.pop_back();
|
||||
return GetPowerDisplayString(text, powerStr);
|
||||
}
|
||||
|
||||
std::string NotificationLocale::GetPowerDisplayString(const std::string& text, const std::string& power)
|
||||
{
|
||||
if (power.empty()) {
|
||||
return text;
|
||||
}
|
||||
std::string powerStr = power;
|
||||
powerStr.push_back(WATT);
|
||||
char buffer[MAX_BUFFER_SIZE] = {0};
|
||||
int ret = sprintf_s(buffer, sizeof(buffer), text.c_str(), powerStr.c_str());
|
||||
if (ret < 0) {
|
||||
BATTERY_HILOGE(COMP_SVC, "sprintf_s: %{public}d", ret);
|
||||
return text;
|
||||
}
|
||||
return std::string(buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -39,6 +39,8 @@ private:
|
||||
bool ParseJsonfile(const std::string& targetPath, std::unordered_map<std::string, std::string>& container);
|
||||
bool SaveJsonToMap(const std::string& fileStr, const std::string& targetPath,
|
||||
std::unordered_map<std::string, std::string>& container);
|
||||
std::string FillTextWithPower(const std::string& text);
|
||||
std::string GetPowerDisplayString(const std::string& text, const std::string& power);
|
||||
std::unordered_map<std::string, std::string> languageMap_;
|
||||
std::unordered_map<std::string, std::string> stringMap_;
|
||||
std::string localeBaseName_;
|
||||
@@ -49,4 +51,4 @@ private:
|
||||
} // namespace PowerMgr
|
||||
} // namespace OHOS
|
||||
|
||||
#endif // BATTERY_NOTIFICATION_LOCALE_H
|
||||
#endif // BATTERY_NOTIFICATION_LOCALE_H
|
||||
|
||||
@@ -21,6 +21,9 @@ namespace OHOS {
|
||||
namespace PowerMgr {
|
||||
static const uint32_t PUBLISH_POPUP_ACTION = 0;
|
||||
static const uint32_t CANCLE_POPUP_ACTION = 1;
|
||||
static constexpr const char* REVERSE_CHARGE_POPUP_NAME = "reverse_super_charge_start";
|
||||
static constexpr const char* REVERSE_CHARGE_WITH_POWER_DISPLAY_POPUP_NAME =
|
||||
"reverse_super_charge_with_power_display_start";
|
||||
|
||||
void NotificationManager::HandleNotification(const std::string& popupName, uint32_t popupAction,
|
||||
const std::unordered_map<std::string, BatteryConfig::NotificationConf>& nConfMap)
|
||||
@@ -100,6 +103,9 @@ BatteryConfig::NotificationConf NotificationManager::FillNotificationCfg(const B
|
||||
{
|
||||
auto& localeConfig = NotificationLocale::GetInstance();
|
||||
BatteryConfig::NotificationConf temp = cfg;
|
||||
if (temp.name == REVERSE_CHARGE_WITH_POWER_DISPLAY_POPUP_NAME) {
|
||||
temp.name = REVERSE_CHARGE_POPUP_NAME;
|
||||
}
|
||||
temp.title = localeConfig.GetStringByKey(cfg.title);
|
||||
temp.text = localeConfig.GetStringByKey(cfg.text);
|
||||
temp.firstButton.first = localeConfig.GetStringByKey(cfg.firstButton.first);
|
||||
|
||||
@@ -55,6 +55,50 @@
|
||||
{
|
||||
"name":"vi-CN",
|
||||
"value":"vi"
|
||||
},
|
||||
{
|
||||
"name":"de-CN",
|
||||
"value":"de"
|
||||
},
|
||||
{
|
||||
"name":"en-CN",
|
||||
"value":"en_GB"
|
||||
},
|
||||
{
|
||||
"name":"id-CN",
|
||||
"value":"id"
|
||||
},
|
||||
{
|
||||
"name":"it-CN",
|
||||
"value":"it"
|
||||
},
|
||||
{
|
||||
"name":"lo-CN",
|
||||
"value":"lo"
|
||||
},
|
||||
{
|
||||
"name":"ms-CN",
|
||||
"value":"ms"
|
||||
},
|
||||
{
|
||||
"name":"my-CN",
|
||||
"value":"my"
|
||||
},
|
||||
{
|
||||
"name":"pl-CN",
|
||||
"value":"pl"
|
||||
},
|
||||
{
|
||||
"name":"pt-CN",
|
||||
"value":"pt_PT"
|
||||
},
|
||||
{
|
||||
"name":"th-CN",
|
||||
"value":"th"
|
||||
},
|
||||
{
|
||||
"name":"tr-CN",
|
||||
"value":"tr"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -212,5 +212,31 @@ HWTEST_F(BatteryNotificationTest, BatteryNotification006, TestSize.Level1)
|
||||
EXPECT_TRUE(notificationMgr->notificationMap_.size() == 0);
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotification006 function end!");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @tc.name: BatteryNotification007
|
||||
* @tc.desc: Test FillNotificationCfg
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(BatteryNotificationTest, BatteryNotification007, TestSize.Level1)
|
||||
{
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotification007 function start!");
|
||||
constexpr const char* REVERSE_CHARGE_POPUP_NAME = "reverse_super_charge_start";
|
||||
constexpr const char* REVERSE_CHARGE_WITH_POWER_DISPLAY_POPUP_NAME =
|
||||
"reverse_super_charge_with_power_display_start";
|
||||
std::shared_ptr<NotificationManager> notificationMgr = std::make_shared<NotificationManager>();
|
||||
EXPECT_TRUE(notificationMgr != nullptr);
|
||||
BatteryConfig::NotificationConf nCfg;
|
||||
|
||||
nCfg.name = "testName";
|
||||
auto ret = notificationMgr->FillNotificationCfg(nCfg);
|
||||
EXPECT_EQ(ret.name, "testName");
|
||||
|
||||
nCfg.name = REVERSE_CHARGE_WITH_POWER_DISPLAY_POPUP_NAME;
|
||||
ret = notificationMgr->FillNotificationCfg(nCfg);
|
||||
EXPECT_EQ(ret.name, REVERSE_CHARGE_POPUP_NAME);
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotification007 function end!");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -572,5 +572,71 @@ HWTEST_F(BatteryNotifyTest, BatteryNotify026, TestSize.Level1)
|
||||
ffrt::wait();
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @tc.name: BatteryNotify027
|
||||
* @tc.desc: Test GetPowerDisplayString
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(BatteryNotifyTest, BatteryNotify027, TestSize.Level1)
|
||||
{
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify027 function start!");
|
||||
auto& notificationLocale = NotificationLocale::GetInstance();
|
||||
std::string fmtStr = "power: %s.";
|
||||
std::map<std::string, std::string> params = {
|
||||
{"77", "power: 77W."},
|
||||
{"1234", "power: 1234W."},
|
||||
{"", "power: %s."}
|
||||
};
|
||||
for (auto&[param, expected]: params) {
|
||||
auto ret = notificationLocale.GetPowerDisplayString(fmtStr, param);
|
||||
EXPECT_EQ(ret, expected);
|
||||
}
|
||||
std::string bigFmtStr(1025, 's');
|
||||
auto ret = notificationLocale.GetPowerDisplayString(bigFmtStr, "123");
|
||||
EXPECT_EQ(ret, bigFmtStr);
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify027 function end!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: BatteryNotify028
|
||||
* @tc.desc: Test FillTextWithPower
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(BatteryNotifyTest, BatteryNotify028, TestSize.Level1)
|
||||
{
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify028 function start!");
|
||||
auto& notificationLocale = NotificationLocale::GetInstance();
|
||||
std::string text = "%s";
|
||||
auto ret = notificationLocale.FillTextWithPower(text);
|
||||
EXPECT_EQ(ret, text);
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify028 function end!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: BatteryNotify029
|
||||
* @tc.desc: Test GetStringByKey
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(BatteryNotifyTest, BatteryNotify029, TestSize.Level1)
|
||||
{
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify029 function start!");
|
||||
constexpr const char* REVERSE_CHARGE_WITH_POWER_DISPLAY_TEXT_KEY =
|
||||
"reverse_super_charge_with_power_display_start_text";
|
||||
auto& notificationLocale = NotificationLocale::GetInstance();
|
||||
auto tmpMap = notificationLocale.stringMap_;
|
||||
notificationLocale.stringMap_.insert(std::make_pair(REVERSE_CHARGE_WITH_POWER_DISPLAY_TEXT_KEY, "power: %s"));
|
||||
notificationLocale.stringMap_.insert(std::make_pair("key", "text"));
|
||||
|
||||
auto ret = notificationLocale.GetStringByKey("key");
|
||||
EXPECT_EQ(ret, "text");
|
||||
ret = notificationLocale.GetStringByKey(REVERSE_CHARGE_WITH_POWER_DISPLAY_TEXT_KEY);
|
||||
EXPECT_EQ(ret, "power: %s");
|
||||
ret = notificationLocale.GetStringByKey("invalid key");
|
||||
EXPECT_EQ(ret, "");
|
||||
|
||||
notificationLocale.stringMap_ = tmpMap;
|
||||
BATTERY_HILOGI(LABEL_TEST, "BatteryNotify029 function end!");
|
||||
}
|
||||
} // namespace PowerMgr
|
||||
} // namespace OHOS
|
||||
|
||||
Reference in New Issue
Block a user