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_LOGI("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_LOGD("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)88 void TaskNotificationSubscriber::OnConsumed(const std::shared_ptr<Notification::Notification> ¬ification) {}
89
OnConsumed(const std::shared_ptr<Notification::Notification> & notification,const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap)90 void TaskNotificationSubscriber::OnConsumed(const std::shared_ptr<Notification::Notification> ¬ification,
91 const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap) {}
92
OnUpdate(const std::shared_ptr<Notification::NotificationSortingMap> & sortingMap)93 void TaskNotificationSubscriber::OnUpdate(
94 const std::shared_ptr<Notification::NotificationSortingMap> &sortingMap) {}
95
OnDied()96 void TaskNotificationSubscriber::OnDied() {}
97
OnDoNotDisturbDateChange(const std::shared_ptr<Notification::NotificationDoNotDisturbDate> & date)98 void TaskNotificationSubscriber::OnDoNotDisturbDateChange(
99 const std::shared_ptr<Notification::NotificationDoNotDisturbDate> &date) {}
100
OnEnabledNotificationChanged(const std::shared_ptr<Notification::EnabledNotificationCallbackData> & callbackData)101 void TaskNotificationSubscriber::OnEnabledNotificationChanged(
102 const std::shared_ptr<Notification::EnabledNotificationCallbackData> &callbackData) {}
103
StringSplit(const std::string & str,const char & delim)104 std::vector<std::string> TaskNotificationSubscriber::StringSplit(const std::string &str, const char &delim)
105 {
106 std::vector<std::string> ret;
107 std::stringstream ss(str);
108 std::string tmp;
109 while (getline(ss, tmp, delim)) {
110 ret.push_back(tmp);
111 }
112 return ret;
113 }
114 } // namespace BackgroundTaskMgr
115 } // namespace OHOS