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