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 "notification_manager.h"
17 #include "notification_locale.h"
18 #include "battery_log.h"
19
20 namespace OHOS {
21 namespace PowerMgr {
22 static const uint32_t PUBLISH_POPUP_ACTION = 0;
23 static const uint32_t CANCLE_POPUP_ACTION = 1;
24
HandleNotification(const std::string & popupName,uint32_t popupAction,const std::unordered_map<std::string,BatteryConfig::NotificationConf> & nConfMap)25 void NotificationManager::HandleNotification(const std::string& popupName, uint32_t popupAction,
26 const std::unordered_map<std::string, BatteryConfig::NotificationConf>& nConfMap)
27 {
28 NotificationLocale::GetInstance().ParseLocaleCfg();
29 NotificationLocale::GetInstance().UpdateStringMap();
30 if (popupAction == PUBLISH_POPUP_ACTION) {
31 auto iter = nConfMap.find(popupName);
32 if (iter != nConfMap.end()) {
33 BatteryConfig::NotificationConf nCfg = FillNotificationCfg(iter->second);
34 PublishNotification(nCfg);
35 }
36 } else if (popupAction == CANCLE_POPUP_ACTION) {
37 CancleNotification(popupName);
38 }
39 }
40
PublishNotification(BatteryConfig::NotificationConf & nCfg)41 void NotificationManager::PublishNotification(BatteryConfig::NotificationConf& nCfg)
42 {
43 BATTERY_HILOGI(COMP_SVC, "Satrt PublishNotification %{public}s", nCfg.GetInfo().c_str());
44 std::shared_ptr<IBatteryNotification> batteryNotification = std::make_shared<NotificationCenter>();
45 if (batteryNotification == nullptr) {
46 BATTERY_HILOGE(COMP_SVC, "PublishNotification batteryNotification is null");
47 return;
48 }
49 batteryNotification->CreateBaseStyle(nCfg);
50
51 if (!nCfg.firstButton.first.empty()) {
52 batteryNotification = CreateButtonStyle(batteryNotification, nCfg.firstButton);
53 }
54 if (!nCfg.secondButton.first.empty()) {
55 batteryNotification = CreateButtonStyle(batteryNotification, nCfg.secondButton);
56 }
57
58 if (batteryNotification == nullptr) {
59 BATTERY_HILOGE(COMP_SVC, "CreateButtonStyle failed");
60 return;
61 }
62 batteryNotification->PublishNotification();
63 std::lock_guard<std::mutex> lock(mapMutex_);
64 notificationMap_.emplace(nCfg.name, batteryNotification);
65 }
66
CreateButtonStyle(const std::shared_ptr<IBatteryNotification> & batteryNotification,const std::pair<std::string,std::string> & nButton)67 std::shared_ptr<IBatteryNotification> NotificationManager::CreateButtonStyle(
68 const std::shared_ptr<IBatteryNotification>& batteryNotification,
69 const std::pair<std::string, std::string>& nButton)
70 {
71 std::shared_ptr<IBatteryNotification> buttonWarp = nullptr;
72 if (batteryNotification == nullptr) {
73 BATTERY_HILOGE(COMP_SVC, "CreateButtonStyle:%{public}s failed", nButton.first.c_str());
74 return buttonWarp;
75 }
76 buttonWarp = std::make_shared<ButtonDecorator>(batteryNotification);
77 if (buttonWarp == nullptr) {
78 BATTERY_HILOGE(COMP_SVC, "buttonWarp is null");
79 return buttonWarp;
80 }
81 buttonWarp->SetActionButton(nButton.first, nButton.second);
82 return buttonWarp;
83 }
84
85
CancleNotification(const std::string & popupName)86 void NotificationManager::CancleNotification(const std::string& popupName)
87 {
88 std::lock_guard<std::mutex> lock(mapMutex_);
89 auto iter = notificationMap_.find(popupName);
90 if (iter != notificationMap_.end()) {
91 std::shared_ptr<IBatteryNotification> batteryNotification = iter->second;
92 if (batteryNotification != nullptr) {
93 batteryNotification->CancelNotification();
94 }
95 notificationMap_.erase(popupName);
96 }
97 }
98
FillNotificationCfg(const BatteryConfig::NotificationConf & cfg)99 BatteryConfig::NotificationConf NotificationManager::FillNotificationCfg(const BatteryConfig::NotificationConf& cfg)
100 {
101 auto& localeConfig = NotificationLocale::GetInstance();
102 BatteryConfig::NotificationConf temp = cfg;
103 temp.title = localeConfig.GetStringByKey(cfg.title);
104 temp.text = localeConfig.GetStringByKey(cfg.text);
105 temp.firstButton.first = localeConfig.GetStringByKey(cfg.firstButton.first);
106 temp.secondButton.first = localeConfig.GetStringByKey(cfg.secondButton.first);
107 return temp;
108 }
109
HandleNotification(const std::string & name,int32_t action,const std::unordered_map<std::string,BatteryConfig::NotificationConf> & nConfMap)110 extern "C" API void HandleNotification(const std::string& name, int32_t action,
111 const std::unordered_map<std::string, BatteryConfig::NotificationConf>& nConfMap)
112 {
113 NotificationManager::GetInstance().HandleNotification(name, action, nConfMap);
114 }
115
116 }
117 }