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 "sms_state_observer.h"
17
18 #include "common_event.h"
19 #include "common_event_manager.h"
20 #include "common_event_support.h"
21 #include "iservice_registry.h"
22 #include "singleton.h"
23 #include "sms_hisysevent.h"
24 #include "system_ability_definition.h"
25 #include "telephony_log_wrapper.h"
26 #include "want.h"
27
28 namespace OHOS {
29 namespace Telephony {
30 using namespace OHOS::EventFwk;
31 static const int32_t DEFAULT_VALUE = -1;
32
StartEventSubscriber()33 void SmsStateObserver::StartEventSubscriber()
34 {
35 MatchingSkills matchingSkills;
36 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED);
37 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED);
38 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED);
39 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED);
40 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
41 smsSubscriber_ = std::make_shared<SmsStateEventSubscriber>(subscriberInfo);
42 smsSubscriber_->InitEventMap();
43 auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (samgrProxy == nullptr) {
45 TELEPHONY_LOGE("StartEventSubscriber samgrProxy is nullptr");
46 return;
47 }
48 smsStatusListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(smsSubscriber_);
49 if (smsStatusListener_ == nullptr) {
50 TELEPHONY_LOGE("StartEventSubscriber smsStatusListener_ is nullptr");
51 return;
52 }
53 int32_t ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, smsStatusListener_);
54 TELEPHONY_LOGI("StartEventSubscriber SubscribeSystemAbility result:%{public}d", ret);
55 }
56
StopEventSubscriber()57 void SmsStateObserver::StopEventSubscriber()
58 {
59 if (smsSubscriber_ == nullptr) {
60 TELEPHONY_LOGE("SmsStateObserver::StopEventSubscriber subscriber_ is nullptr");
61 return;
62 }
63 bool unSubscribeResult = CommonEventManager::UnSubscribeCommonEvent(smsSubscriber_);
64 smsSubscriber_ = nullptr;
65 TELEPHONY_LOGI("SmsStateObserver::StopEventSubscriber unSubscribeResult = %{public}d", unSubscribeResult);
66 }
67
OnReceiveEvent(const OHOS::EventFwk::CommonEventData & data)68 void SmsStateEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data)
69 {
70 OHOS::EventFwk::Want want = data.GetWant();
71 std::string action = data.GetWant().GetAction();
72 TELEPHONY_LOGI("SmsStateEventSubscriber::OnReceiveEvent: action = %{public}s", action.c_str());
73 int32_t slotId = want.GetIntParam("slotId", DEFAULT_VALUE);
74 switch (GetSmsStateEventIntValue(action)) {
75 case COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED:
76 case COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED:
77 DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingCbBroadcastTimeOut(
78 slotId, SmsMmsMessageType::CELL_BROAD_CAST);
79 SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::CELL_BROAD_CAST);
80 break;
81 case COMMON_EVENT_SMS_RECEIVE_COMPLETED:
82 DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingSmsBroadcastTimeOut(
83 slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE);
84 SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE);
85 break;
86 case COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED:
87 DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingWapPushBroadcastTimeOut(
88 slotId, SmsMmsMessageType::WAP_PUSH);
89 SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::WAP_PUSH);
90 break;
91 default:
92 break;
93 }
94 }
95
GetSmsStateEventIntValue(std::string & event) const96 SmsStateEventIntValue SmsStateEventSubscriber::GetSmsStateEventIntValue(std::string &event) const
97 {
98 auto iter = smsStateEvenMapIntValues_.find(event);
99 if (iter == smsStateEvenMapIntValues_.end()) {
100 return COMMON_EVENT_UNKNOWN;
101 }
102 return iter->second;
103 }
104
InitEventMap()105 void SmsStateEventSubscriber::InitEventMap()
106 {
107 smsStateEvenMapIntValues_ = {
108 { CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED,
109 COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED },
110 { CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED, COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED },
111 { CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED, COMMON_EVENT_SMS_RECEIVE_COMPLETED },
112 { CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED, COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED },
113 };
114 }
115
SystemAbilityStatusChangeListener(std::shared_ptr<SmsStateEventSubscriber> & sub)116 SmsStateObserver::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
117 std::shared_ptr<SmsStateEventSubscriber> &sub)
118 : sub_(sub)
119 {}
120
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)121 void SmsStateObserver::SystemAbilityStatusChangeListener::OnAddSystemAbility(
122 int32_t systemAbilityId, const std::string &deviceId)
123 {
124 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
125 TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
126 return;
127 }
128 if (sub_ == nullptr) {
129 TELEPHONY_LOGE("OnAddSystemAbility COMMON_EVENT_SERVICE_ID sub_ is nullptr");
130 return;
131 }
132 bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(sub_);
133 TELEPHONY_LOGI("SmsStateObserver::OnAddSystemAbility subscribeResult = %{public}d", subscribeResult);
134 }
135
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)136 void SmsStateObserver::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
137 int32_t systemAbilityId, const std::string &deviceId)
138 {
139 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
140 TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID");
141 return;
142 }
143 if (sub_ == nullptr) {
144 TELEPHONY_LOGE("OnRemoveSystemAbility COMMON_EVENT_SERVICE_ID opName_ is nullptr");
145 return;
146 }
147 bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(sub_);
148 TELEPHONY_LOGI("DeviceStateObserver::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult);
149 }
150 } // namespace Telephony
151 } // namespace OHOS
152