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 "task_notification_subscriber.h"
17
18 #include <sstream>
19
20 #include "bg_continuous_task_mgr.h"
21 #include "continuous_task_log.h"
22 #include "string_wrapper.h"
23
24
25 namespace OHOS {
26 namespace BackgroundTaskMgr {
27 namespace {
28 static constexpr char LABEL_SPLITER = '_';
29 static constexpr char NOTIFICATION_PREFIX[] = "bgmode";
30 static constexpr uint32_t LABEL_BGMODE_PREFIX_POS = 0;
31 static constexpr uint32_t LABEL_APP_UID_POS = 1;
32 static constexpr uint32_t LABEL_SIZE = 3;
33 }
34
35 std::shared_ptr<BgContinuousTaskMgr> TaskNotificationSubscriber::continuousTaskMgr_
36 = BgContinuousTaskMgr::GetInstance();
37
TaskNotificationSubscriber()38 TaskNotificationSubscriber::TaskNotificationSubscriber() {}
39
~TaskNotificationSubscriber()40 TaskNotificationSubscriber::~TaskNotificationSubscriber() {}
41
OnConnected()42 void TaskNotificationSubscriber::OnConnected() {}
43
OnDisconnected()44 void TaskNotificationSubscriber::OnDisconnected() {}
45
OnCanceled(const std::shared_ptr<Notification::Notification> & notification,const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap,int deleteReason)46 void TaskNotificationSubscriber::OnCanceled(const std::shared_ptr<Notification::Notification> ¬ification,
47 const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap, int deleteReason)
48 {
49 if (notification == nullptr) {
50 BGTASK_LOGW("notification param is null");
51 return;
52 }
53 Notification::NotificationRequest request = notification->GetNotificationRequest();
54 if (request.GetCreatorUid() != continuousTaskMgr_->GetBgTaskUid()) {
55 return;
56 }
57
58 // continuous task notification label is consisted of bgmode prefix, app uid, abilityName hash code.
59 std::string notificationLabel = request.GetLabel();
60 std::vector<std::string> labelSplits = StringSplit(notificationLabel, LABEL_SPLITER);
61
62 if (labelSplits.empty() || labelSplits[LABEL_BGMODE_PREFIX_POS] != NOTIFICATION_PREFIX
63 || labelSplits.size() != LABEL_SIZE) {
64 BGTASK_LOGW("callback notification label is invalid");
65 return;
66 }
67
68 if (deleteReason == Notification::NotificationConstant::APP_CANCEL_REASON_DELETE) {
69 BGTASK_LOGD("notification remove action is already triggered by cancel method.");
70 return;
71 }
72
73 std::shared_ptr<AAFwk::WantParams> extraInfo = request.GetAdditionalData();
74 if (extraInfo == nullptr) {
75 BGTASK_LOGE("notification extraInfo is null");
76 return;
77 }
78 BGTASK_LOGI("stop continuous task by user, the label is : %{public}s", notificationLabel.c_str());
79
80 std::string abilityName = AAFwk::String::Unbox(AAFwk::IString::Query(extraInfo->GetParam("abilityName")));
81 std::string taskInfoMapKey = labelSplits[LABEL_APP_UID_POS] + LABEL_SPLITER + abilityName;
82
83 if (continuousTaskMgr_->StopContinuousTaskByUser(taskInfoMapKey)) {
84 BGTASK_LOGI("remove continuous task record Key: %{public}s", taskInfoMapKey.c_str());
85 }
86 }
87
OnConsumed(const std::shared_ptr<Notification::Notification> & notification,const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap)88 void TaskNotificationSubscriber::OnConsumed(const std::shared_ptr<Notification::Notification> ¬ification,
89 const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap) {}
90
OnUpdate(const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap)91 void TaskNotificationSubscriber::OnUpdate(
92 const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap) {}
93
OnDied()94 void TaskNotificationSubscriber::OnDied() {}
95
OnDoNotDisturbDateChange(const std::shared_ptr<Notification::NotificationDoNotDisturbDate> & date)96 void TaskNotificationSubscriber::OnDoNotDisturbDateChange(
97 const std::shared_ptr<Notification::NotificationDoNotDisturbDate> &date) {}
98
OnEnabledNotificationChanged(const std::shared_ptr<Notification::EnabledNotificationCallbackData> & callbackData)99 void TaskNotificationSubscriber::OnEnabledNotificationChanged(
100 const std::shared_ptr<Notification::EnabledNotificationCallbackData> &callbackData) {}
101
OnBadgeChanged(const std::shared_ptr<Notification::BadgeNumberCallbackData> & badgeData)102 void TaskNotificationSubscriber::OnBadgeChanged(const std::shared_ptr<Notification::BadgeNumberCallbackData> &badgeData) {}
103
OnBatchCanceled(const std::vector<std::shared_ptr<Notification::Notification>> & requestList,const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap,int32_t deleteReason)104 void TaskNotificationSubscriber::OnBatchCanceled(const std::vector<std::shared_ptr<Notification::Notification>>
105 &requestList, const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap, int32_t deleteReason) {}
106
StringSplit(const std::string & str,const char & delim)107 std::vector<std::string> TaskNotificationSubscriber::StringSplit(const std::string &str, const char &delim)
108 {
109 std::vector<std::string> ret;
110 std::stringstream ss(str);
111 std::string tmp;
112 while (getline(ss, tmp, delim)) {
113 ret.push_back(tmp);
114 }
115 return ret;
116 }
117 } // namespace BackgroundTaskMgr
118 } // namespace OHOS
119