1 /*
2  * Copyright (c) 2024 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 <memory>
17 #include <string>
18 
19 #include "datashare_delegate.h"
20 #include "dev_profile.h"
21 #include "pasteboard_event_ue.h"
22 #include "pasteboard_hilog.h"
23 #include "pasteboard_switch.h"
24 
25 namespace OHOS::MiscServices {
26 using namespace UeReporter;
27 const constexpr char *DISTRIBUTED_PASTEDBOARD_SWITCH = "distributed_pasteboard_switch";
28 constexpr const char *SUPPORT_STATUS = "1";
PastedSwitch()29 PastedSwitch::PastedSwitch()
30 {
31     switchObserver_ = new (std::nothrow) PastedSwitchObserver([this]() -> void {
32         SetSwitch();
33     });
34 }
35 
Init()36 void PastedSwitch::Init()
37 {
38     DataShareDelegate::GetInstance().RegisterObserver(DISTRIBUTED_PASTEDBOARD_SWITCH, switchObserver_);
39     SetSwitch();
40     ReportUeSwitchEvent();
41 }
42 
SetSwitch()43 void PastedSwitch::SetSwitch()
44 {
45     std::string value;
46     DataShareDelegate::GetInstance().GetValue(DISTRIBUTED_PASTEDBOARD_SWITCH, value);
47     if (value.empty()) {
48         DevProfile::GetInstance().PutEnabledStatus(SUPPORT_STATUS);
49         return;
50     }
51     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "set switch status to %{public}s.", value.c_str());
52     DevProfile::GetInstance().PutEnabledStatus(value);
53 }
54 
DeInit()55 void PastedSwitch::DeInit()
56 {
57     DataShareDelegate::GetInstance().UnregisterObserver(DISTRIBUTED_PASTEDBOARD_SWITCH, switchObserver_);
58 }
59 
ReportUeSwitchEvent()60 void PastedSwitch::ReportUeSwitchEvent()
61 {
62     std::string value;
63     DataShareDelegate::GetInstance().GetValue(DISTRIBUTED_PASTEDBOARD_SWITCH, value);
64     UE_SWITCH(UeReporter::UE_SWITCH_STATUS, UeReporter::UE_STATUS_TYPE,
65         (value == SUPPORT_STATUS) ? UeReporter::SwitchStatus::SWITCH_OPEN : UeReporter::SwitchStatus::SWITCH_CLOSE);
66 }
67 
OnChange()68 void PastedSwitchObserver::OnChange()
69 {
70     if (func_ != nullptr) {
71         func_();
72     }
73 }
74 } // namespace OHOS::MiscServices