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