1 /*
2 * Copyright (c) 2022 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_tools.h"
17
18 #include <sstream>
19 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
20 #include "notification_helper.h"
21 #endif
22 #include "continuous_task_log.h"
23 #include "string_wrapper.h"
24
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 namespace {
28 constexpr char NOTIFICATION_PREFIX[] = "bgmode";
29 constexpr char SEPARATOR[] = "_";
30 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
31 constexpr int32_t DEFAULT_USERID = -2;
32 #endif
33 }
NotificationTools()34 NotificationTools::NotificationTools() {}
35
~NotificationTools()36 NotificationTools::~NotificationTools() {}
37
CreateNotificationLabel(int32_t uid,const std::string & bundleName,const std::string & abilityName)38 std::string CreateNotificationLabel(int32_t uid, const std::string &bundleName,
39 const std::string &abilityName)
40 {
41 std::stringstream stream;
42 stream.clear();
43 stream.str("");
44 stream << NOTIFICATION_PREFIX << SEPARATOR << uid << SEPARATOR << std::hash<std::string>()(abilityName);
45 std::string label = stream.str();
46 BGTASK_LOGD("notification label: %{public}s", label.c_str());
47 return label;
48 }
49
PublishNotification(const std::shared_ptr<ContinuousTaskRecord> & continuousTaskRecord,const std::string & appName,const std::string & prompt,int32_t serviceUid)50 ErrCode NotificationTools::PublishNotification(const std::shared_ptr<ContinuousTaskRecord> &continuousTaskRecord,
51 const std::string &appName, const std::string &prompt, int32_t serviceUid)
52 {
53 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
54 std::shared_ptr<Notification::NotificationNormalContent> normalContent
55 = std::make_shared<Notification::NotificationNormalContent>();
56 normalContent->SetTitle(appName);
57 normalContent->SetText(prompt);
58
59 Notification::NotificationRequest notificationRequest = Notification::NotificationRequest();
60
61 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>();
62 extraInfo->SetParam("abilityName", AAFwk::String::Box(continuousTaskRecord->abilityName_));
63
64 std::string notificationLabel = CreateNotificationLabel(continuousTaskRecord->uid_,
65 continuousTaskRecord->bundleName_, continuousTaskRecord->abilityName_);
66
67 // set extraInfo to save abilityname Info.
68 notificationRequest.SetAdditionalData(extraInfo);
69
70 // set basic notification content
71 notificationRequest.SetContent(std::make_shared<Notification::NotificationContent>(normalContent));
72
73 // set wantagent param for click jump to target ability
74 notificationRequest.SetWantAgent(continuousTaskRecord->wantAgent_);
75
76 // set notification label distinguish different notification
77 notificationRequest.SetLabel(notificationLabel);
78
79 // set creator uid as background task manager service uid
80 notificationRequest.SetCreatorUid(serviceUid);
81
82 // set creator user id to -2 means to get all user's notification event callback.
83 notificationRequest.SetCreatorUserId(DEFAULT_USERID);
84
85 // set tapDismissed param to false make notification retained when clicked.
86 notificationRequest.SetTapDismissed(false);
87
88 if (Notification::NotificationHelper::PublishContinuousTaskNotification(notificationRequest) != ERR_OK) {
89 BGTASK_LOGE("publish notification error");
90 return ERR_BGTASK_NOTIFICATION_ERR;
91 }
92 continuousTaskRecord->notificationLabel_ = notificationLabel;
93 #endif
94 return ERR_OK;
95 }
96
CancelNotification(const std::string & label,int32_t id)97 ErrCode NotificationTools::CancelNotification(const std::string &label, int32_t id)
98 {
99 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
100 if (Notification::NotificationHelper::CancelContinuousTaskNotification(label, id) != ERR_OK) {
101 return ERR_BGTASK_NOTIFICATION_ERR;
102 }
103 #endif
104 return ERR_OK;
105 }
GetAllActiveNotificationsLabels(std::set<std::string> & notificationLabels)106 void NotificationTools::GetAllActiveNotificationsLabels(std::set<std::string> ¬ificationLabels)
107 {
108 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
109 std::vector<sptr<Notification::Notification>> notifications;
110 Notification::NotificationHelper::GetAllActiveNotifications(notifications);
111 for (auto &var : notifications) {
112 notificationLabels.emplace(var->GetLabel());
113 }
114 #endif
115 }
116
RefreshContinuousNotifications(const std::map<std::string,std::pair<std::string,std::string>> & newPromptInfos,int32_t serviceUid)117 void NotificationTools::RefreshContinuousNotifications(
118 const std::map<std::string, std::pair<std::string, std::string>> &newPromptInfos, int32_t serviceUid)
119 {
120 #ifdef DISTRIBUTED_NOTIFICATION_ENABLE
121 std::vector<sptr<Notification::Notification>> notifications;
122 Notification::NotificationHelper::GetAllActiveNotifications(notifications);
123 for (auto &var : notifications) {
124 Notification::NotificationRequest request = var->GetNotificationRequest();
125 std::string label = var->GetLabel();
126 if (newPromptInfos.count(label) == 0 || request.GetCreatorUid() != serviceUid) {
127 continue;
128 }
129 auto &content = request.GetContent();
130 auto const &normalContent = content->GetNotificationContent();
131 normalContent->SetTitle(newPromptInfos.at(label).first);
132 normalContent->SetText(newPromptInfos.at(label).second);
133 if (Notification::NotificationHelper::PublishContinuousTaskNotification(request) != ERR_OK) {
134 BGTASK_LOGE("refresh notification error");
135 }
136 }
137 #endif
138 }
139 }
140 }
141