1e5d0e473Sopenharmony_ci/* 2e5d0e473Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd. 3e5d0e473Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4e5d0e473Sopenharmony_ci * you may not use this file except in compliance with the License. 5e5d0e473Sopenharmony_ci * You may obtain a copy of the License at 6e5d0e473Sopenharmony_ci * 7e5d0e473Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8e5d0e473Sopenharmony_ci * 9e5d0e473Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10e5d0e473Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11e5d0e473Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12e5d0e473Sopenharmony_ci * See the License for the specific language governing permissions and 13e5d0e473Sopenharmony_ci * limitations under the License. 14e5d0e473Sopenharmony_ci */ 15e5d0e473Sopenharmony_ci 16e5d0e473Sopenharmony_ci#include "sms_state_observer.h" 17e5d0e473Sopenharmony_ci 18e5d0e473Sopenharmony_ci#include "common_event.h" 19e5d0e473Sopenharmony_ci#include "common_event_manager.h" 20e5d0e473Sopenharmony_ci#include "common_event_support.h" 21e5d0e473Sopenharmony_ci#include "iservice_registry.h" 22e5d0e473Sopenharmony_ci#include "singleton.h" 23e5d0e473Sopenharmony_ci#include "sms_hisysevent.h" 24e5d0e473Sopenharmony_ci#include "system_ability_definition.h" 25e5d0e473Sopenharmony_ci#include "telephony_log_wrapper.h" 26e5d0e473Sopenharmony_ci#include "want.h" 27e5d0e473Sopenharmony_ci 28e5d0e473Sopenharmony_cinamespace OHOS { 29e5d0e473Sopenharmony_cinamespace Telephony { 30e5d0e473Sopenharmony_ciusing namespace OHOS::EventFwk; 31e5d0e473Sopenharmony_cistatic const int32_t DEFAULT_VALUE = -1; 32e5d0e473Sopenharmony_ci 33e5d0e473Sopenharmony_civoid SmsStateObserver::StartEventSubscriber() 34e5d0e473Sopenharmony_ci{ 35e5d0e473Sopenharmony_ci MatchingSkills matchingSkills; 36e5d0e473Sopenharmony_ci matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED); 37e5d0e473Sopenharmony_ci matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED); 38e5d0e473Sopenharmony_ci matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED); 39e5d0e473Sopenharmony_ci matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED); 40e5d0e473Sopenharmony_ci CommonEventSubscribeInfo subscriberInfo(matchingSkills); 41e5d0e473Sopenharmony_ci subscriberInfo.SetThreadMode(EventFwk::CommonEventSubscribeInfo::COMMON); 42e5d0e473Sopenharmony_ci smsSubscriber_ = std::make_shared<SmsStateEventSubscriber>(subscriberInfo); 43e5d0e473Sopenharmony_ci smsSubscriber_->InitEventMap(); 44e5d0e473Sopenharmony_ci auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 45e5d0e473Sopenharmony_ci if (samgrProxy == nullptr) { 46e5d0e473Sopenharmony_ci TELEPHONY_LOGE("StartEventSubscriber samgrProxy is nullptr"); 47e5d0e473Sopenharmony_ci return; 48e5d0e473Sopenharmony_ci } 49e5d0e473Sopenharmony_ci smsStatusListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(smsSubscriber_); 50e5d0e473Sopenharmony_ci if (smsStatusListener_ == nullptr) { 51e5d0e473Sopenharmony_ci TELEPHONY_LOGE("StartEventSubscriber smsStatusListener_ is nullptr"); 52e5d0e473Sopenharmony_ci return; 53e5d0e473Sopenharmony_ci } 54e5d0e473Sopenharmony_ci int32_t ret = samgrProxy->SubscribeSystemAbility(COMMON_EVENT_SERVICE_ID, smsStatusListener_); 55e5d0e473Sopenharmony_ci TELEPHONY_LOGI("StartEventSubscriber SubscribeSystemAbility result:%{public}d", ret); 56e5d0e473Sopenharmony_ci} 57e5d0e473Sopenharmony_ci 58e5d0e473Sopenharmony_civoid SmsStateObserver::StopEventSubscriber() 59e5d0e473Sopenharmony_ci{ 60e5d0e473Sopenharmony_ci if (smsSubscriber_ == nullptr) { 61e5d0e473Sopenharmony_ci TELEPHONY_LOGE("SmsStateObserver::StopEventSubscriber subscriber_ is nullptr"); 62e5d0e473Sopenharmony_ci return; 63e5d0e473Sopenharmony_ci } 64e5d0e473Sopenharmony_ci bool unSubscribeResult = CommonEventManager::UnSubscribeCommonEvent(smsSubscriber_); 65e5d0e473Sopenharmony_ci smsSubscriber_ = nullptr; 66e5d0e473Sopenharmony_ci TELEPHONY_LOGI("SmsStateObserver::StopEventSubscriber unSubscribeResult = %{public}d", unSubscribeResult); 67e5d0e473Sopenharmony_ci} 68e5d0e473Sopenharmony_ci 69e5d0e473Sopenharmony_civoid SmsStateEventSubscriber::OnReceiveEvent(const OHOS::EventFwk::CommonEventData &data) 70e5d0e473Sopenharmony_ci{ 71e5d0e473Sopenharmony_ci OHOS::EventFwk::Want want = data.GetWant(); 72e5d0e473Sopenharmony_ci std::string action = data.GetWant().GetAction(); 73e5d0e473Sopenharmony_ci TELEPHONY_LOGI("SmsStateEventSubscriber::OnReceiveEvent: action = %{public}s", action.c_str()); 74e5d0e473Sopenharmony_ci int32_t slotId = want.GetIntParam("slotId", DEFAULT_VALUE); 75e5d0e473Sopenharmony_ci switch (GetSmsStateEventIntValue(action)) { 76e5d0e473Sopenharmony_ci case COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED: 77e5d0e473Sopenharmony_ci case COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED: 78e5d0e473Sopenharmony_ci DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingCbBroadcastTimeOut( 79e5d0e473Sopenharmony_ci slotId, SmsMmsMessageType::CELL_BROAD_CAST); 80e5d0e473Sopenharmony_ci SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::CELL_BROAD_CAST); 81e5d0e473Sopenharmony_ci break; 82e5d0e473Sopenharmony_ci case COMMON_EVENT_SMS_RECEIVE_COMPLETED: 83e5d0e473Sopenharmony_ci DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingSmsBroadcastTimeOut( 84e5d0e473Sopenharmony_ci slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE); 85e5d0e473Sopenharmony_ci SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::SMS_SHORT_MESSAGE); 86e5d0e473Sopenharmony_ci break; 87e5d0e473Sopenharmony_ci case COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED: 88e5d0e473Sopenharmony_ci DelayedSingleton<SmsHiSysEvent>::GetInstance()->JudgingWapPushBroadcastTimeOut( 89e5d0e473Sopenharmony_ci slotId, SmsMmsMessageType::WAP_PUSH); 90e5d0e473Sopenharmony_ci SmsHiSysEvent::WriteSmsReceiveBehaviorEvent(slotId, SmsMmsMessageType::WAP_PUSH); 91e5d0e473Sopenharmony_ci break; 92e5d0e473Sopenharmony_ci default: 93e5d0e473Sopenharmony_ci break; 94e5d0e473Sopenharmony_ci } 95e5d0e473Sopenharmony_ci} 96e5d0e473Sopenharmony_ci 97e5d0e473Sopenharmony_ciSmsStateEventIntValue SmsStateEventSubscriber::GetSmsStateEventIntValue(std::string &event) const 98e5d0e473Sopenharmony_ci{ 99e5d0e473Sopenharmony_ci auto iter = smsStateEvenMapIntValues_.find(event); 100e5d0e473Sopenharmony_ci if (iter == smsStateEvenMapIntValues_.end()) { 101e5d0e473Sopenharmony_ci return COMMON_EVENT_UNKNOWN; 102e5d0e473Sopenharmony_ci } 103e5d0e473Sopenharmony_ci return iter->second; 104e5d0e473Sopenharmony_ci} 105e5d0e473Sopenharmony_ci 106e5d0e473Sopenharmony_civoid SmsStateEventSubscriber::InitEventMap() 107e5d0e473Sopenharmony_ci{ 108e5d0e473Sopenharmony_ci smsStateEvenMapIntValues_ = { 109e5d0e473Sopenharmony_ci { CommonEventSupport::COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED, 110e5d0e473Sopenharmony_ci COMMON_EVENT_SMS_EMERGENCY_CB_RECEIVE_COMPLETED }, 111e5d0e473Sopenharmony_ci { CommonEventSupport::COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED, COMMON_EVENT_SMS_CB_RECEIVE_COMPLETED }, 112e5d0e473Sopenharmony_ci { CommonEventSupport::COMMON_EVENT_SMS_RECEIVE_COMPLETED, COMMON_EVENT_SMS_RECEIVE_COMPLETED }, 113e5d0e473Sopenharmony_ci { CommonEventSupport::COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED, COMMON_EVENT_SMS_WAPPUSH_RECEIVE_COMPLETED }, 114e5d0e473Sopenharmony_ci }; 115e5d0e473Sopenharmony_ci} 116e5d0e473Sopenharmony_ci 117e5d0e473Sopenharmony_ciSmsStateObserver::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener( 118e5d0e473Sopenharmony_ci std::shared_ptr<SmsStateEventSubscriber> &sub) 119e5d0e473Sopenharmony_ci : sub_(sub) 120e5d0e473Sopenharmony_ci{} 121e5d0e473Sopenharmony_ci 122e5d0e473Sopenharmony_civoid SmsStateObserver::SystemAbilityStatusChangeListener::OnAddSystemAbility( 123e5d0e473Sopenharmony_ci int32_t systemAbilityId, const std::string &deviceId) 124e5d0e473Sopenharmony_ci{ 125e5d0e473Sopenharmony_ci if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { 126e5d0e473Sopenharmony_ci TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID"); 127e5d0e473Sopenharmony_ci return; 128e5d0e473Sopenharmony_ci } 129e5d0e473Sopenharmony_ci if (sub_ == nullptr) { 130e5d0e473Sopenharmony_ci TELEPHONY_LOGE("OnAddSystemAbility COMMON_EVENT_SERVICE_ID sub_ is nullptr"); 131e5d0e473Sopenharmony_ci return; 132e5d0e473Sopenharmony_ci } 133e5d0e473Sopenharmony_ci bool subscribeResult = EventFwk::CommonEventManager::SubscribeCommonEvent(sub_); 134e5d0e473Sopenharmony_ci TELEPHONY_LOGI("SmsStateObserver::OnAddSystemAbility subscribeResult = %{public}d", subscribeResult); 135e5d0e473Sopenharmony_ci} 136e5d0e473Sopenharmony_ci 137e5d0e473Sopenharmony_civoid SmsStateObserver::SystemAbilityStatusChangeListener::OnRemoveSystemAbility( 138e5d0e473Sopenharmony_ci int32_t systemAbilityId, const std::string &deviceId) 139e5d0e473Sopenharmony_ci{ 140e5d0e473Sopenharmony_ci if (systemAbilityId != COMMON_EVENT_SERVICE_ID) { 141e5d0e473Sopenharmony_ci TELEPHONY_LOGE("systemAbilityId is not COMMON_EVENT_SERVICE_ID"); 142e5d0e473Sopenharmony_ci return; 143e5d0e473Sopenharmony_ci } 144e5d0e473Sopenharmony_ci if (sub_ == nullptr) { 145e5d0e473Sopenharmony_ci TELEPHONY_LOGE("OnRemoveSystemAbility COMMON_EVENT_SERVICE_ID opName_ is nullptr"); 146e5d0e473Sopenharmony_ci return; 147e5d0e473Sopenharmony_ci } 148e5d0e473Sopenharmony_ci bool subscribeResult = CommonEventManager::UnSubscribeCommonEvent(sub_); 149e5d0e473Sopenharmony_ci TELEPHONY_LOGI("DeviceStateObserver::OnRemoveSystemAbility subscribeResult = %{public}d", subscribeResult); 150e5d0e473Sopenharmony_ci} 151e5d0e473Sopenharmony_ci} // namespace Telephony 152e5d0e473Sopenharmony_ci} // namespace OHOS 153