• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "dms_sa_client.h"
16 
17 namespace OHOS {
18 namespace AAFwk {
GetInstance()19 DmsSaClient &DmsSaClient::GetInstance()
20 {
21     HILOGI("%{public}s called.", __func__);
22     static DmsSaClient instance;
23     return instance;
24 }
25 
SubscribeDmsSA()26 bool DmsSaClient::SubscribeDmsSA()
27 {
28     HILOGI("%{public}s called.", __func__);
29     int32_t ret = saMgrProxy_->SubscribeSystemAbility(DISTRIBUTED_SCHED_SA_ID, this);
30     if (ret != ERR_OK) {
31         HILOGE("Failed to subscribe system ability DISTRIBUTED_SCHED_SA_ID ret:%{public}d", ret);
32         return false;
33     }
34     return true;
35 }
36 
AddListener(const std::string & type,const sptr<IRemoteOnListener> & listener)37 int32_t DmsSaClient::AddListener(const std::string& type, const sptr<IRemoteOnListener>& listener)
38 {
39     HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
40     saMgrProxy_ = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
41     if (saMgrProxy_ == nullptr) {
42         HILOGE("fail to get saMgrProxy.");
43         return INNER_ERR;
44     }
45     if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
46         AbilityManagerClient::GetInstance()->RegisterOnListener(type, listener);
47     }
48     std::lock_guard<std::mutex> lock(eventMutex_);
49     if (!hasSubscribeDmsSA_) {
50         if (SubscribeDmsSA()) {
51             hasSubscribeDmsSA_ = true;
52             listeners_[type] = listener;
53         } else {
54             return INNER_ERR;
55         }
56     }
57     return NO_ERROR;
58 }
59 
DelListener(const std::string & type,const sptr<IRemoteOnListener> & listener)60 int32_t DmsSaClient::DelListener(const std::string& type, const sptr<IRemoteOnListener>& listener)
61 {
62     HILOGI("%{public}s called, the type is %{public}s", __func__, type.c_str());
63     if (saMgrProxy_->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID)) {
64         AbilityManagerClient::GetInstance()->RegisterOffListener(type, listener);
65     }
66     std::lock_guard<std::mutex> lock(eventMutex_);
67     listeners_.erase(type);
68     return NO_ERROR;
69 }
70 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)71 void DmsSaClient::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
72 {
73     HILOGI("%{public}s called, the systemAbilityId is %{public}d", __func__, systemAbilityId);
74     std::lock_guard<std::mutex> lock(eventMutex_);
75     if (systemAbilityId == DISTRIBUTED_SCHED_SA_ID) {
76         HILOGI("%{public}s listeners size: %{public}zu .", __func__, listeners_.size());
77         for (auto& listener : listeners_) {
78             AbilityManagerClient::GetInstance()->RegisterOnListener(listener.first, listener.second);
79         }
80     } else {
81         HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %{public}d", systemAbilityId);
82     }
83 }
84 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)85 void DmsSaClient::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
86 {
87     HILOGI("%{public}s called, the systemAbilityId is %{public}d", __func__, systemAbilityId);
88     std::lock_guard<std::mutex> lock(eventMutex_);
89     if (systemAbilityId == DISTRIBUTED_SCHED_SA_ID) {
90         for (auto& listener : listeners_) {
91             AbilityManagerClient::GetInstance()->RegisterOffListener(listener.first, listener.second);
92         }
93     } else {
94         HILOGE("SystemAbilityId must be DISTRIBUTED_SCHED_SA_ID,but it is %{public}d", systemAbilityId);
95     }
96 }
97 }  // namespace AAFwk
98 }  // namespace OHOS
99