1 /*
2  * Copyright (C) 2023 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 "sms_broadcast_subscriber_receiver.h"
17 #include "sms_persist_helper.h"
18 #include "telephony_log_wrapper.h"
19 #include "want.h"
20 #include "event_handler.h"
21 #include "event_runner.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 using namespace EventFwk;
26 
27 std::queue<std::shared_ptr<SmsBroadcastSubscriberReceiver>> sptrQueue;
28 std::shared_ptr<AppExecFwk::EventRunner> eventRunner_ = nullptr;
29 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_ = nullptr;
30 
SmsBroadcastSubscriberReceiver(const CommonEventSubscribeInfo & subscriberInfo,std::shared_ptr<SmsReceiveReliabilityHandler> handler,uint16_t refId,uint16_t dataBaseId,const std::string & address)31 SmsBroadcastSubscriberReceiver::SmsBroadcastSubscriberReceiver(const CommonEventSubscribeInfo &subscriberInfo,
32     std::shared_ptr<SmsReceiveReliabilityHandler> handler, uint16_t refId, uint16_t dataBaseId,
33     const std::string &address)
34     : CommonEventSubscriber(subscriberInfo)
35 {
36     handler_ = handler;
37     refId_ = refId;
38     dataBaseId_ = dataBaseId;
39     address_ = address;
40 }
41 
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)42 void SmsBroadcastSubscriberReceiver::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
43 {
44     std::string action = data.GetWant().GetAction();
45     bool isSms = action == CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED;
46     bool isWapPush = action == CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED;
47     if (isSms || isWapPush) {
48         TELEPHONY_LOGI("OnReceiveEvent refId_ =%{public}d, dataBaseId_ =%{public}d", refId_, dataBaseId_);
49         if (handler_ == nullptr) {
50             TELEPHONY_LOGE("handler_ is nullptr");
51             return;
52         }
53         if (!address_.empty() && isSms) {
54             DelayedSingleton<SmsPersistHelper>::GetInstance()->UpdateContact(address_);
55         }
56         handler_->DeleteMessageFormDb(refId_, dataBaseId_);
57         // only save one object of eventrunner and eventhandler because each object will create a new thread.
58         if (eventRunner_ == nullptr) {
59             eventRunner_ = AppExecFwk::EventRunner::Create("unsubscribe thread");
60         }
61         if (eventHandler_ == nullptr) {
62             eventHandler_ = std::make_shared<AppExecFwk::EventHandler>(eventRunner_);
63         }
64         if (eventHandler_ == nullptr) {
65             TELEPHONY_LOGE("create eventhandler fail");
66             return;
67         }
68         // post a task that be excuted after 10s, and callback is static function in this class.
69         bool postTaskResult = eventHandler_->PostTask(UnsubscribeInCes, "unsubscribe event task", 10000);
70         if (!postTaskResult) {
71             TELEPHONY_LOGE("post unsubscribe task fail");
72         }
73     }
74 }
75 
UnsubscribeInCes()76 void SmsBroadcastSubscriberReceiver::UnsubscribeInCes()
77 {
78     TELEPHONY_LOGE("UnsubscribeInCes entry");
79     if (sptrQueue.empty()) {
80         TELEPHONY_LOGE("SmsBroadcastSubscriberReceiver queue is empty");
81     } else {
82         auto smsBroadcastSubscriberReceiver = sptrQueue.front();
83         sptrQueue.pop();
84         // call api to unsubscribe
85         bool unsubscribeResult = CommonEventManager::UnSubscribeCommonEvent(smsBroadcastSubscriberReceiver);
86         if (!unsubscribeResult) {
87             TELEPHONY_LOGE("unsubscribe fail");
88         }
89     }
90 }
91 } // namespace Telephony
92 } // namespace OHOS