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 "sa_mgr_client.h"
17 #include "hilog_tag_wrapper.h"
18 
19 #include "if_system_ability_manager.h"
20 #include "iservice_registry.h"
21 
22 namespace OHOS {
23 namespace AAFwk {
SaMgrClient()24 SaMgrClient::SaMgrClient()
25 {}
26 
~SaMgrClient()27 SaMgrClient::~SaMgrClient()
28 {}
29 
GetSystemAbility(const int32_t systemAbilityId)30 sptr<IRemoteObject> SaMgrClient::GetSystemAbility(const int32_t systemAbilityId)
31 {
32     TAG_LOGI(AAFwkTag::TEST, "Test GetSystemAbility id : %{public}d", systemAbilityId);
33     if (servicesMap_[systemAbilityId] == nullptr) {
34         OHOS::sptr<ISystemAbilityManager> systemAbilityManager =
35             SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36         if (systemAbilityManager == nullptr) {
37             TAG_LOGE(AAFwkTag::TEST, "%s:fail to get Registry", __func__);
38             return nullptr;
39         }
40         OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->GetSystemAbility(systemAbilityId);
41         servicesMap_[systemAbilityId] = object;
42     }
43     return servicesMap_[systemAbilityId];
44 }
45 
CheckSystemAbility(const int32_t systemAbilityId)46 sptr<IRemoteObject> SaMgrClient::CheckSystemAbility(const int32_t systemAbilityId)
47 {
48     TAG_LOGI(AAFwkTag::TEST, "Test CheckSystemAbility id : %{public}d", systemAbilityId);
49     if (servicesMap_[systemAbilityId] == nullptr) {
50         OHOS::sptr<ISystemAbilityManager> systemAbilityManager =
51                 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52         if (systemAbilityManager == nullptr) {
53             TAG_LOGE(AAFwkTag::TEST, "%s:fail to get Registry", __func__);
54             return nullptr;
55         }
56         OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->CheckSystemAbility(systemAbilityId);
57         servicesMap_[systemAbilityId] = object;
58     }
59     return servicesMap_[systemAbilityId];
60 }
61 
RegisterSystemAbility(const int32_t systemAbilityId,sptr<IRemoteObject> broker)62 void SaMgrClient::RegisterSystemAbility(const int32_t systemAbilityId, sptr<IRemoteObject> broker)
63 {
64     TAG_LOGI(AAFwkTag::TEST, "Test RegisterSystemAbility id : %{public}d", systemAbilityId);
65     servicesMap_[systemAbilityId] = broker;
66 }
67 }  // namespace AAFwk
68 }  // namespace OHOS
69