1 /*
2 * Copyright (C) 2022-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 "distributed_module_config.h"
16
17 #include "dev_manager.h"
18 #include "dev_profile.h"
19 #include "pasteboard_hilog.h"
20
21 namespace OHOS {
22 namespace MiscServices {
23 bool DistributedModuleConfig::status_ = false;
24 size_t DistributedModuleConfig::deviceNums_ = 0;
25 DistributedModuleConfig::Observer DistributedModuleConfig::observer_ = nullptr;
IsOn()26 bool DistributedModuleConfig::IsOn()
27 {
28 if (deviceNums_ != 0) {
29 Notify();
30 }
31 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "device online nums: %{public}zu, status_:%{public}d",
32 deviceNums_, status_);
33 return status_;
34 }
35
Watch(Observer observer)36 void DistributedModuleConfig::Watch(Observer observer)
37 {
38 observer_ = std::move(observer);
39 }
40
ForceNotify()41 void DistributedModuleConfig::ForceNotify()
42 {
43 if (observer_ != nullptr) {
44 observer_(true);
45 }
46 }
47
Notify()48 void DistributedModuleConfig::Notify()
49 {
50 bool newStatus = GetEnabledStatus();
51 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "Notify, status:%{public}d, newStatus:%{public}d",
52 status_, newStatus);
53 if (newStatus != status_) {
54 status_ = newStatus;
55 if (observer_ != nullptr) {
56 observer_(newStatus);
57 }
58 }
59 }
60
GetEnabledStatus()61 bool DistributedModuleConfig::GetEnabledStatus()
62 {
63 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "GetEnabledStatus start.");
64 if (!DevProfile::GetInstance().GetLocalEnable()) {
65 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "GetLocalEnable false.");
66 return false;
67 }
68
69 auto networkIds = DevManager::GetInstance().GetNetworkIds();
70 deviceNums_ = networkIds.size();
71 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "device online nums: %{public}zu", deviceNums_);
72 std::string remoteEnabledStatus = "false";
73 for (auto &id : networkIds) {
74 DevProfile::GetInstance().GetEnabledStatus(id, remoteEnabledStatus);
75 if (remoteEnabledStatus == "true") {
76 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "remoteEnabledStatus true.");
77 return true;
78 }
79 }
80 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "remoteEnabledStatus = %{public}s.", remoteEnabledStatus.c_str());
81 return false;
82 }
83 } // namespace MiscServices
84 } // namespace OHOS
85