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 "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 using namespace DistributedHardware;
24 bool DistributedModuleConfig::status_ = false;
25 DistributedModuleConfig::Observer DistributedModuleConfig::observer_ = nullptr;
IsOn()26 bool DistributedModuleConfig::IsOn()
27 {
28 status_ = GetEnabledStatus();
29 return status_;
30 }
31
Watch(Observer observer)32 void DistributedModuleConfig::Watch(Observer observer)
33 {
34 observer_ = std::move(observer);
35 }
36
Notify()37 void DistributedModuleConfig::Notify()
38 {
39 bool newStatus = GetEnabledStatus();
40 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "Notify, status:%{public}d, newStatus:%{public}d", status_, newStatus);
41 if (newStatus != status_) {
42 status_ = newStatus;
43 if (observer_ != nullptr) {
44 observer_(newStatus);
45 }
46 }
47 }
48
GetEnabledStatus()49 bool DistributedModuleConfig::GetEnabledStatus()
50 {
51 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "GetEnabledStatus start.");
52
53 std::string localEnabledStatus = "false";
54 DevProfile::GetInstance().GetEnabledStatus("", localEnabledStatus);
55 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "localEnabledStatus = %{public}s.", localEnabledStatus.c_str());
56 if (localEnabledStatus == "false") {
57 return false;
58 }
59
60 auto deviceIds = DevManager::GetInstance().GetDeviceIds();
61 std::string remoteEnabledStatus = "false";
62 for (auto &id : deviceIds) {
63 DevProfile::GetInstance().GetEnabledStatus(id, remoteEnabledStatus);
64 if (remoteEnabledStatus == "true") {
65 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "remoteEnabledStatus true.");
66 return true;
67 }
68 }
69 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "remoteEnabledStatus = %{public}s.", remoteEnabledStatus.c_str());
70 return false;
71 }
72 } // namespace MiscServices
73 } // namespace OHOS