• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 "clip_para.h"
16 
17 #include <chrono>
18 #include "pasteboard_hilog.h"
19 
20 namespace OHOS {
21 namespace MiscServices {
22 using namespace std::chrono;
23 
ClipPara()24 ClipPara::ClipPara()
25 {
26 }
27 
GetInstance()28 ClipPara &ClipPara::GetInstance()
29 {
30     static ClipPara instance;
31     return instance;
32 }
33 
InitMemberVariable()34 void ClipPara::InitMemberVariable()
35 {
36     PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "InitMemberVariable");
37     sendInformation_ = nullptr;
38     remoteExpiration_ = 0;
39     lastSyncNetworkId_ = "";
40     isPullEvent_ = false;
41     isPullEventResult_ = false;
42     isPasted_ = false;
43 }
44 
HasRemoteData()45 bool ClipPara::HasRemoteData()
46 {
47     uint64_t curTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
48     return sendInformation_ != nullptr && remoteExpiration_ > curTime;
49 }
50 
GetLastLocalSyncKey()51 std::string ClipPara::GetLastLocalSyncKey()
52 {
53     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
54     return lastLocalSyncKey_;
55 }
56 
GetLastRemoteSyncKey()57 std::string ClipPara::GetLastRemoteSyncKey()
58 {
59     return lastRemoteSyncKey_;
60 }
61 
GetLastSyncNetworkId()62 std::string ClipPara::GetLastSyncNetworkId()
63 {
64     return lastSyncNetworkId_;
65 }
66 
GetLocalExpiration()67 uint64_t ClipPara::GetLocalExpiration()
68 {
69     return localExpiration_.load();
70 }
71 
GetRemoteExpiration()72 uint64_t ClipPara::GetRemoteExpiration()
73 {
74     return remoteExpiration_;
75 }
76 
GetSendInformation()77 std::shared_ptr<ClipPara::SendInformation> ClipPara::GetSendInformation()
78 {
79     return sendInformation_;
80 }
81 
GetFirstStageValue()82 uint32_t ClipPara::GetFirstStageValue()
83 {
84     return firstStageValue_.load();
85 }
86 
GetSecondStageValue()87 uint32_t ClipPara::GetSecondStageValue()
88 {
89     return secondStageValue_.load();
90 }
91 
GetPullEvent()92 bool ClipPara::GetPullEvent()
93 {
94     return isPullEvent_.load();
95 }
96 
GetPullEventResult()97 bool ClipPara::GetPullEventResult()
98 {
99     return isPullEventResult_.load();
100 }
101 
GetPasted()102 bool ClipPara::GetPasted()
103 {
104     return isPasted_.load();
105 }
106 
SetLastLocalSyncKey(const std::string lastSyncKey)107 void ClipPara::SetLastLocalSyncKey(const std::string lastSyncKey)
108 {
109     std::lock_guard<decltype(mutex_)> lockGuard(mutex_);
110     lastLocalSyncKey_ = lastSyncKey;
111 }
112 
SetLastRemoteSyncKey(const std::string lastRemoteSyncKey)113 void ClipPara::SetLastRemoteSyncKey(const std::string lastRemoteSyncKey)
114 {
115     lastRemoteSyncKey_ = lastRemoteSyncKey;
116 }
117 
SetLastSyncNetworkId(const std::string lastSyncNetworkId)118 void ClipPara::SetLastSyncNetworkId(const std::string lastSyncNetworkId)
119 {
120     lastSyncNetworkId_ = lastSyncNetworkId;
121 }
122 
SetLocalExpiration(const uint64_t expiration)123 void ClipPara::SetLocalExpiration(const uint64_t expiration)
124 {
125     localExpiration_.store(expiration);
126 }
127 
SetRemoteExpiration(const uint64_t remoteExpiration)128 void ClipPara::SetRemoteExpiration(const uint64_t remoteExpiration)
129 {
130     remoteExpiration_.store(remoteExpiration);
131 }
132 
SetSendInformation(const ClipPara::SendInformation & senderInformation)133 void ClipPara::SetSendInformation(const ClipPara::SendInformation &senderInformation)
134 {
135     sendInformation_ = std::make_shared<ClipPara::SendInformation>(senderInformation);
136 }
137 
SetFirstStageValue(const uint32_t firstStageValue)138 void ClipPara::SetFirstStageValue(const uint32_t firstStageValue)
139 {
140     firstStageValue_.store(firstStageValue);
141 }
142 
SetSecondStageValue(const uint32_t secondStageValue)143 void ClipPara::SetSecondStageValue(const uint32_t secondStageValue)
144 {
145     secondStageValue_.store(secondStageValue);
146 }
147 
SetPullEvent(const bool isPullEvent)148 void ClipPara::SetPullEvent(const bool isPullEvent)
149 {
150     isPullEvent_.store(isPullEvent);
151 }
152 
SetPullEventResult(const bool isPullEventResult)153 void ClipPara::SetPullEventResult(const bool isPullEventResult)
154 {
155     isPullEventResult_.store(isPullEventResult);
156 }
157 
SetPasted(const bool isPasted)158 void ClipPara::SetPasted(const bool isPasted)
159 {
160     isPasted_.store(isPasted);
161 }
162 
UpdateStageValue(const uint64_t & expiration,bool isPasting)163 void ClipPara::UpdateStageValue(const uint64_t &expiration, bool isPasting)
164 {
165     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "start, firstStageValue = %{public}s, secondStageValue = %{public}s",
166         std::to_string(firstStageValue_).c_str(), std::to_string(secondStageValue_).c_str());
167     if (!isPullEvent_ && !isPasting) {
168         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "not do pre-establish");
169         return;
170     }
171 
172     if (isPasted_) {
173         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "stage value has been update");
174         return;
175     }
176     uint64_t curTime = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
177     if (isPasting && expiration > curTime) {
178         PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "UpdateStageValue");
179         isPasted_ = true;
180         if ((firstStageValue_ == MIN_STAGE_VALUE && secondStageValue_ == MAX_STAGE_VALUE) ||
181             firstStageValue_ != MIN_STAGE_VALUE) {
182             firstStageValue_ = MAX_STAGE_VALUE;
183             secondStageValue_ = MIN_STAGE_VALUE;
184         } else {
185             secondStageValue_.fetch_add(1);
186         }
187     } else {
188         if (firstStageValue_ == MIN_STAGE_VALUE) {
189             auto diffValue = (secondStageValue_ != MIN_STAGE_VALUE) ? 1 : 0;
190             secondStageValue_.fetch_sub(diffValue);
191         } else {
192             firstStageValue_.fetch_sub(1);
193         }
194     }
195     PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "end, firstStageValue = %{public}s, secondStageValue = %{public}s",
196         std::to_string(firstStageValue_).c_str(), std::to_string(secondStageValue_).c_str());
197 }
198 } // namespace MiscServices
199 } // namespace OHOS