1 /* 2 * Copyright (C) 2023-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 "ce_service.h" 17 18 #include "accesstoken_kit.h" 19 #include "common_event_handler.h" 20 #include "loghelper.h" 21 22 namespace OHOS { 23 namespace NFC { 24 25 const int FIELD_COMMON_EVENT_INTERVAL = 1000; 26 const int DEACTIVATE_TIMEOUT = 6000; 27 const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED = "usual.event.nfc.action.RF_FIELD_ON_DETECTED"; 28 const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED = "usual.event.nfc.action.RF_FIELD_OFF_DETECTED"; 29 CeService(std::weak_ptr<NfcService> nfcService)30CeService::CeService(std::weak_ptr<NfcService> nfcService) : nfcService_(nfcService) 31 { 32 } 33 ~CeService()34CeService::~CeService() 35 { 36 } 37 PublishFieldOnOrOffCommonEvent(bool isFieldOn)38void CeService::PublishFieldOnOrOffCommonEvent(bool isFieldOn) 39 { 40 AAFwk::Want want; 41 if (isFieldOn) { 42 want.SetAction(COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED); 43 } else { 44 want.SetAction(COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED); 45 } 46 EventFwk::CommonEventData data; 47 data.SetWant(want); 48 EventFwk::CommonEventManager::PublishCommonEvent(data); 49 } 50 HandleFieldActivated()51void CeService::HandleFieldActivated() 52 { 53 if (nfcService_.expired() || nfcService_.lock()->eventHandler_ == nullptr) { 54 return; 55 } 56 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF)); 57 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT)); 58 nfcService_.lock()->eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT), 59 DEACTIVATE_TIMEOUT); 60 61 uint64_t currentTime = KITS::NfcSdkCommon::GetCurrentTime(); 62 if (currentTime - lastFieldOnTime_ > FIELD_COMMON_EVENT_INTERVAL) { 63 lastFieldOnTime_ = currentTime; 64 nfcService_.lock()->eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_ON)); 65 } 66 } 67 HandleFieldDeactivated()68void CeService::HandleFieldDeactivated() 69 { 70 if (nfcService_.expired() || nfcService_.lock()->eventHandler_ == nullptr) { 71 return; 72 } 73 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF_TIMEOUT)); 74 nfcService_.lock()->eventHandler_->RemoveEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF)); 75 76 uint64_t currentTime = KITS::NfcSdkCommon::GetCurrentTime(); 77 if (currentTime - lastFieldOffTime_ > FIELD_COMMON_EVENT_INTERVAL) { 78 lastFieldOffTime_ = currentTime; 79 nfcService_.lock()->eventHandler_->SendEvent(static_cast<uint32_t>(NfcCommonEvent::MSG_NOTIFY_FIELD_OFF), 80 FIELD_COMMON_EVENT_INTERVAL); 81 } 82 } 83 } // NFC 84 } // OHOS