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 #include "nfc_sa_manager.h"
16
17 #include "loghelper.h"
18 #include "system_ability_definition.h"
19
20 namespace OHOS {
21 namespace NFC {
22 const bool REGISTER_RESULT =
23 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<NfcSaManager>::GetInstance().get());
24
NfcSaManager()25 NfcSaManager::NfcSaManager() : SystemAbility(NFC_MANAGER_SYS_ABILITY_ID, true) {}
26
~NfcSaManager()27 NfcSaManager::~NfcSaManager()
28 {
29 if (nfcService_) {
30 nfcService_ = nullptr;
31 }
32 }
33
OnStart()34 void NfcSaManager::OnStart()
35 {
36 if (state_ == ServiceRunningState::STATE_RUNNING) {
37 InfoLog("NfcSaManager has already started.");
38 return;
39 }
40
41 if (!Init()) {
42 InfoLog("failed to init NfcSaManager");
43 return;
44 }
45 state_ = ServiceRunningState::STATE_RUNNING;
46 InfoLog("NfcSaManager::OnStart start service success.");
47 }
48
Init()49 bool NfcSaManager::Init()
50 {
51 InfoLog("NfcSaManager::Init ready to init.");
52 if (!registerToService_) {
53 nfcService_ = std::make_shared<NfcService>();
54 nfcService_->Initialize();
55
56 sptr<ISystemAbilityManager> systemAbilityMgr =
57 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58 if (systemAbilityMgr && (systemAbilityMgr->AddSystemAbility(NFC_MANAGER_SYS_ABILITY_ID,
59 nfcService_->nfcControllerImpl_) == 0)) {
60 } else {
61 InfoLog("NfcSaManager::Init Add System Ability failed!");
62 return false;
63 }
64 AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
65 registerToService_ = true;
66 }
67 InfoLog("NfcSaManager::Init init success.");
68 return true;
69 }
70
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)71 void NfcSaManager::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
72 {
73 InfoLog("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
74 if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
75 InfoLog("OnAddSystemAbility systemAbilityId is not COMMON_EVENT_SERVICE_ID");
76 return;
77 }
78 if (nfcService_ == nullptr) {
79 ErrorLog("nfcService_ is nullptr");
80 return;
81 }
82 if (nfcService_->eventHandler_ == nullptr) {
83 ErrorLog("eventHandler_ is nullptr");
84 return;
85 }
86 nfcService_->eventHandler_->SubscribePackageChangedEvent();
87 nfcService_->eventHandler_->SubscribeScreenChangedEvent();
88 }
89
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)90 void NfcSaManager::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
91 {
92 InfoLog("NfcSaManager OnRemoveSystemAbility finish");
93 }
94
OnStop()95 void NfcSaManager::OnStop()
96 {
97 InfoLog("NfcSaManager::OnStop ready to stop service.");
98 state_ = ServiceRunningState::STATE_NOT_START;
99 registerToService_ = false;
100 InfoLog("NfcSaManager::OnStop stop service success.");
101 }
102 } // namespace NFC
103 } // namespace OHOS
104