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