• 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 constexpr const char *UE_SWITCH_STATUS = "PASTEBOARD_SWITCH_STATUS";
PastedSwitch()29 PastedSwitch::PastedSwitch() : userId_(ERROR_USERID)
30 {
31     switchObserver_ = new (std::nothrow) PastedSwitchObserver([this]() -> void {
32         std::thread thread([userId = userId_, this]() {
33             SetSwitch(userId);
34         });
35         thread.detach();
36     });
37 }
38 
Init(int32_t userId)39 void PastedSwitch::Init(int32_t userId)
40 {
41     if (userId == ERROR_USERID) {
42         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "userId invalid.");
43         return;
44     }
45     this->userId_ = userId;
46     DataShareDelegate::GetInstance().SetUserId(userId_);
47     DataShareDelegate::GetInstance().RegisterObserver(DISTRIBUTED_PASTEBOARD_SWITCH, switchObserver_);
48     SetSwitch(userId);
49     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "Init SetSwitch %{public}d", userId);
50     ReportUeSwitchEvent();
51 }
52 
SetSwitch(int32_t userId)53 void PastedSwitch::SetSwitch(int32_t userId)
54 {
55     std::string value;
56     DataShareDelegate::GetInstance().SetUserId(userId);
57     DataShareDelegate::GetInstance().GetValue(DISTRIBUTED_PASTEBOARD_SWITCH, value);
58     if (value.empty()) {
59         PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "empty switch, set status enable, userId=%{public}d", userId);
60         DevProfile::GetInstance().PutDeviceStatus(true);
61         return;
62     }
63     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "switch status=%{public}s, userId=%{public}d", value.c_str(), userId);
64     DevProfile::GetInstance().PutDeviceStatus(value == SUPPORT_STATUS);
65 }
66 
DeInit()67 void PastedSwitch::DeInit()
68 {
69     DataShareDelegate::GetInstance().UnregisterObserver(DISTRIBUTED_PASTEBOARD_SWITCH, switchObserver_);
70 }
71 
ReportUeSwitchEvent()72 void PastedSwitch::ReportUeSwitchEvent()
73 {
74     std::string value;
75     DataShareDelegate::GetInstance().GetValue(DISTRIBUTED_PASTEBOARD_SWITCH, value);
76     UE_SWITCH(UE_SWITCH_STATUS, UeReporter::UE_STATUS_TYPE,
77         (value == SUPPORT_STATUS) ? UeReporter::SwitchStatus::SWITCH_OPEN : UeReporter::SwitchStatus::SWITCH_CLOSE);
78 }
79 
OnChange()80 void PastedSwitchObserver::OnChange()
81 {
82     if (func_ != nullptr) {
83         func_();
84     }
85 }
86 } // namespace OHOS::MiscServices