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