• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "loghelper.h"
17 #include "system_ability_definition.h"
18 #include "external_deps_proxy.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, false) {}
26 
~NfcSaManager()27 NfcSaManager::~NfcSaManager()
28 {
29     if (nfcService_) {
30         nfcService_ = nullptr;
31     }
32 }
33 
OnStart(const SystemAbilityOnDemandReason & startReason)34 void NfcSaManager::OnStart(const SystemAbilityOnDemandReason &startReason)
35 {
36     if (state_ == ServiceRunningState::STATE_RUNNING) {
37         InfoLog("NfcSaManager has already started.");
38         return;
39     }
40 
41     if (!Init(startReason)) {
42         InfoLog("failed to init NfcSaManager");
43         // record init sa failed event.
44         NfcFailedParams err;
45         ExternalDepsProxy::GetInstance().BuildFailedParams(err, MainErrorCode::INIT_SA_FAILED,
46             SubErrorCode::DEFAULT_ERR_DEF);
47         ExternalDepsProxy::GetInstance().WriteNfcFailedHiSysEvent(&err);
48         return;
49     }
50     state_ = ServiceRunningState::STATE_RUNNING;
51     InfoLog("NfcSaManager::OnStart start service success.");
52 }
53 
Init(const SystemAbilityOnDemandReason & startReason)54 bool NfcSaManager::Init(const SystemAbilityOnDemandReason &startReason)
55 {
56     std::lock_guard<std::mutex> guard(initMutex_);
57     InfoLog("NfcSaManager::Init ready to init, Reason %{public}s, id %{public}d, value %{public}s",
58         startReason.GetName().c_str(), static_cast<int32_t>(startReason.GetId()), startReason.GetValue().c_str());
59     if (!registerToService_) {
60         nfcService_ = std::make_shared<NfcService>();
61         nfcService_->Initialize();
62         if (startReason.GetName() != "usual.event.BOOT_COMPLETED") {
63             nfcService_->ExecuteTask(KITS::TASK_INITIALIZE);
64         }
65         bool ret = Publish(nfcService_->nfcControllerImpl_);
66         if (ret) {
67             InfoLog("NfcSaManager::Init Add System Ability SUCCESS!");
68         } else {
69             ErrorLog("NfcSaManager::Init Add System Ability FAILED!");
70             return false;
71         }
72         AddSystemAbilityListener(COMMON_EVENT_SERVICE_ID);
73         registerToService_ = true;
74     }
75     InfoLog("NfcSaManager::Init init success.");
76     return true;
77 }
78 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)79 void NfcSaManager::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
80 {
81     InfoLog("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
82     if (systemAbilityId != COMMON_EVENT_SERVICE_ID) {
83         InfoLog("OnAddSystemAbility systemAbilityId is not COMMON_EVENT_SERVICE_ID");
84         return;
85     }
86     InfoLog("Start to resubscribe common event.");
87     if (nfcService_ == nullptr) {
88         ErrorLog("nfcService_ is nullptr");
89         return;
90     }
91     if (nfcService_->eventHandler_ == nullptr) {
92         ErrorLog("eventHandler_ is nullptr");
93         return;
94     }
95     nfcService_->eventHandler_->SubscribePackageChangedEvent();
96     nfcService_->eventHandler_->SubscribeScreenChangedEvent();
97     nfcService_->eventHandler_->SubscribeShutdownEvent();
98 }
99 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)100 void NfcSaManager::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
101 {
102     InfoLog("NfcSaManager OnRemoveSystemAbility finish");
103 }
104 
OnStop()105 void NfcSaManager::OnStop()
106 {
107     InfoLog("NfcSaManager::OnStop ready to stop service.");
108     state_ = ServiceRunningState::STATE_NOT_START;
109     registerToService_ = false;
110     InfoLog("NfcSaManager::OnStop stop service success.");
111 }
112 }  // namespace NFC
113 }  // namespace OHOS
114