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 <vector>
17 #include <unistd.h>
18 #include "notification_helper.h"
19 #include "want_agent_helper.h"
20 #include "want_agent_info.h"
21 #include "battery_log.h"
22 #include "image_source.h"
23 #include "pixel_map.h"
24 #include "notification_center.h"
25
26 namespace OHOS {
27 namespace PowerMgr {
28 static const int BATTERY_NOTIFICATION_SYS_ABILITY_ID = 5528;
29 static const std::string BATTERY_NOTIFICATION_SYS_ABILITY_NAME = "";
30
CreateBaseStyle(const BatteryConfig::NotificationConf & nCfg)31 void NotificationCenter::CreateBaseStyle(const BatteryConfig::NotificationConf& nCfg)
32 {
33 SetNotificationId(nCfg.name);
34 SetContent(nCfg.title, nCfg.text);
35 SetCreatorUid();
36 SetCreatorBundleName();
37 SetSlotType();
38 SetInProgress();
39 SetUnremovable();
40 SetBadgeIconStyle();
41 SetLittleIcon(nCfg.icon);
42 }
43
SetNotificationId(const std::string & popupName)44 void NotificationCenter::SetNotificationId(const std::string& popupName)
45 {
46 int32_t notificationId = static_cast<int32_t>(std::hash<std::string>()(popupName));
47 BATTERY_HILOGI(COMP_SVC, "SetNotificationId notifationId[%{public}d]", notificationId);
48 request_.SetNotificationId(notificationId);
49 }
50
SetContent(const std::string & title,const std::string & text)51 void NotificationCenter::SetContent(const std::string& title, const std::string& text)
52 {
53 std::shared_ptr<Notification::NotificationNormalContent> content
54 = std::make_shared<Notification::NotificationNormalContent>();
55 if (content == nullptr) {
56 BATTERY_HILOGE(COMP_SVC, "Failed to create NotificationNormalContent");
57 return;
58 }
59 content->SetTitle(title);
60 content->SetText(text);
61 std::shared_ptr<Notification::NotificationContent> notificationContent
62 = std::make_shared<Notification::NotificationContent>(content);
63 if (notificationContent == nullptr) {
64 BATTERY_HILOGE(COMP_SVC, "Failed to create NotificationContent");
65 return;
66 }
67 request_.SetContent(notificationContent);
68 }
69
SetCreatorUid()70 void NotificationCenter::SetCreatorUid()
71 {
72 request_.SetCreatorUid(BATTERY_NOTIFICATION_SYS_ABILITY_ID);
73 }
74
SetCreatorBundleName()75 void NotificationCenter::SetCreatorBundleName()
76 {
77 request_.SetCreatorBundleName(BATTERY_NOTIFICATION_SYS_ABILITY_NAME);
78 }
79
SetSlotType()80 void NotificationCenter::SetSlotType()
81 {
82 request_.SetSlotType(OHOS::Notification::NotificationConstant::SlotType::SOCIAL_COMMUNICATION);
83 }
84
SetInProgress()85 void NotificationCenter::SetInProgress()
86 {
87 request_.SetInProgress(true);
88 }
89
SetUnremovable()90 void NotificationCenter::SetUnremovable()
91 {
92 request_.SetUnremovable(true);
93 }
94
SetBadgeIconStyle()95 void NotificationCenter::SetBadgeIconStyle()
96 {
97 request_.SetBadgeIconStyle(Notification::NotificationRequest::BadgeStyle::LITTLE);
98 }
99
SetLittleIcon(const std::string & iconPath)100 void NotificationCenter::SetLittleIcon(const std::string& iconPath)
101 {
102 if (access(iconPath.c_str(), F_OK) != 0) {
103 BATTERY_HILOGE(COMP_SVC, "iconPath[%{public}s] is invalid", iconPath.c_str());
104 return;
105 }
106 uint32_t errorCode = 0;
107 Media::SourceOptions opts;
108 opts.formatHint = "image/png";
109 auto imageSource = Media::ImageSource::CreateImageSource(iconPath, opts, errorCode);
110 if (imageSource == nullptr) {
111 BATTERY_HILOGE(COMP_SVC, "Failed to create ImageSource");
112 return;
113 }
114 Media::DecodeOptions decodeOpts;
115 std::unique_ptr<Media::PixelMap> pixelMap = imageSource->CreatePixelMap(decodeOpts, errorCode);
116 request_.SetLittleIcon(std::move(pixelMap));
117 }
118
SetActionButton(const std::string & buttonName,const std::string & buttonAction)119 void NotificationCenter::SetActionButton(const std::string& buttonName, const std::string& buttonAction)
120 {
121 if (buttonName.empty()) {
122 BATTERY_HILOGE(COMP_SVC, "SetActionButton buttonCfg is NULL");
123 return;
124 }
125 auto want = std::make_shared<AAFwk::Want>();
126 want->SetAction(buttonAction);
127 std::vector<std::shared_ptr<AAFwk::Want>> wants;
128 wants.push_back(want);
129 std::vector<AbilityRuntime::WantAgent::WantAgentConstant::Flags> flags;
130 flags.push_back(AbilityRuntime::WantAgent::WantAgentConstant::Flags::CONSTANT_FLAG);
131 AbilityRuntime::WantAgent::WantAgentInfo wantAgentInfo(
132 0, AbilityRuntime::WantAgent::WantAgentConstant::OperationType::SEND_COMMON_EVENT,
133 flags, wants, nullptr
134 );
135 auto wantAgentDeal = AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(wantAgentInfo);
136 std::shared_ptr<Notification::NotificationActionButton> actionButtonDeal =
137 Notification::NotificationActionButton::Create(nullptr, buttonName, wantAgentDeal);
138 request_.AddActionButton(actionButtonDeal);
139 }
140
PublishNotification()141 bool NotificationCenter::PublishNotification()
142 {
143 ErrCode code = Notification::NotificationHelper::PublishNotification(request_);
144 BATTERY_HILOGI(COMP_SVC, "NotificationCenter::PublishNotification: %{public}d", static_cast<int32_t>(code));
145 return true;
146 }
147
CancelNotification()148 bool NotificationCenter::CancelNotification()
149 {
150 int32_t notificationId = request_.GetNotificationId();
151 ErrCode code = Notification::NotificationHelper::CancelNotification(notificationId);
152 BATTERY_HILOGI(COMP_SVC, "NotificationCenter::CancelNotification: %{public}d", static_cast<int32_t>(code));
153 return true;
154 }
155 }
156 }