1 /* 2 * Copyright (C) 2021 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_receive_manager.h" 17 18 #include "cdma_sms_receive_handler.h" 19 #include "gsm_sms_receive_handler.h" 20 #include "runner_pool.h" 21 #include "telephony_log_wrapper.h" 22 23 namespace OHOS { 24 namespace Telephony { 25 using namespace std; SmsReceiveManager(int32_t slotId)26SmsReceiveManager::SmsReceiveManager(int32_t slotId) : slotId_(slotId) {} 27 ~SmsReceiveManager()28SmsReceiveManager::~SmsReceiveManager() 29 { 30 if (gsmSmsReceiveRunner_ != nullptr) { 31 gsmSmsReceiveRunner_->Stop(); 32 } 33 34 if (cdmaSmsReceiveRunner_ != nullptr) { 35 cdmaSmsReceiveRunner_->Stop(); 36 } 37 38 if (gsmSmsReceiveHandler_ != nullptr) { 39 gsmSmsReceiveHandler_->UnRegisterHandler(); 40 } 41 42 if (cdmaSmsReceiveHandler_ != nullptr) { 43 cdmaSmsReceiveHandler_->UnRegisterHandler(); 44 } 45 } 46 Init()47void SmsReceiveManager::Init() 48 { 49 gsmSmsReceiveRunner_ = RunnerPool::GetInstance().GetSmsSendReceiveRunnerBySlotId(slotId_); 50 if (gsmSmsReceiveRunner_ == nullptr) { 51 TELEPHONY_LOGE("failed to create GsmEventReceiverRunner"); 52 return; 53 } 54 cdmaSmsReceiveRunner_ = RunnerPool::GetInstance().GetSmsSendReceiveRunnerBySlotId(slotId_); 55 if (cdmaSmsReceiveRunner_ == nullptr) { 56 TELEPHONY_LOGE("failed to create CdmaEventReceiverRunner"); 57 return; 58 } 59 60 gsmSmsReceiveHandler_ = std::make_shared<GsmSmsReceiveHandler>(gsmSmsReceiveRunner_, slotId_); 61 if (gsmSmsReceiveHandler_ == nullptr) { 62 TELEPHONY_LOGE("failed to create GsmSmsReceiveHandler"); 63 return; 64 } 65 cdmaSmsReceiveHandler_ = std::make_shared<CdmaSmsReceiveHandler>(cdmaSmsReceiveRunner_, slotId_); 66 if (cdmaSmsReceiveHandler_ == nullptr) { 67 TELEPHONY_LOGE("failed to create CdmaSmsReceiveHandler"); 68 return; 69 } 70 71 gsmSmsReceiveHandler_->Init(); 72 cdmaSmsReceiveHandler_->Init(); 73 TELEPHONY_LOGI("SmsReceiveManager init ok."); 74 } 75 SetCdmaSender(const weak_ptr<SmsSender> & smsSender)76void SmsReceiveManager::SetCdmaSender(const weak_ptr<SmsSender> &smsSender) 77 { 78 if (cdmaSmsReceiveHandler_ != nullptr) { 79 CdmaSmsReceiveHandler *cdmaSmsReceiveHandler = (CdmaSmsReceiveHandler *)(cdmaSmsReceiveHandler_.get()); 80 cdmaSmsReceiveHandler->SetCdmaSender(smsSender); 81 } 82 } 83 } // namespace Telephony 84 } // namespace OHOS 85