1 /*
2 * Copyright (C) 2022 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 "work_record.h"
17
18 #include "string_ex.h"
19
20 namespace OHOS {
21 namespace Location {
22 const int MAX_RECORD_COUNT = 100;
23
WorkRecord()24 WorkRecord::WorkRecord()
25 {
26 num_ = 0;
27 }
28
ReadFromParcel(Parcel & parcel)29 void WorkRecord::ReadFromParcel(Parcel& parcel)
30 {
31 int num = parcel.ReadInt32();
32 if (num > MAX_RECORD_COUNT) {
33 num = MAX_RECORD_COUNT;
34 }
35 for (int i = 0; i < num; i++) {
36 int uid = parcel.ReadInt32();
37 int pid = parcel.ReadInt32();
38 std::string name = parcel.ReadString();
39 int timeInterval = parcel.ReadInt32();
40 std::string uuid = parcel.ReadString();
41 Add(uid, pid, name, timeInterval, uuid);
42 }
43 deviceId_ = parcel.ReadString();
44 }
45
Unmarshalling(Parcel & parcel)46 std::unique_ptr<WorkRecord> WorkRecord::Unmarshalling(Parcel& parcel)
47 {
48 std::unique_ptr<WorkRecord> workRecord = std::make_unique<WorkRecord>();
49 workRecord->ReadFromParcel(parcel);
50 return workRecord;
51 }
52
Marshalling(Parcel & parcel) const53 bool WorkRecord::Marshalling(Parcel& parcel) const
54 {
55 std::unique_lock<std::mutex> lock(workRecordMutex_);
56 parcel.WriteInt32(num_);
57 for (int i = 0; i < num_; i++) {
58 parcel.WriteInt32(uids_[i]);
59 parcel.WriteInt32(pids_[i]);
60 parcel.WriteString(names_[i]);
61 parcel.WriteInt32(timeInterval_[i]);
62 parcel.WriteString(uuid_[i]);
63 }
64 parcel.WriteString(deviceId_);
65 return true;
66 }
67
MarshallingWorkRecord(Parcel & parcel) const68 bool WorkRecord::MarshallingWorkRecord(Parcel& parcel) const
69 {
70 std::unique_lock<std::mutex> lock(workRecordMutex_);
71 // write numbers
72 parcel.WriteInt32(num_);
73 // write uids
74 parcel.WriteInt32(num_);
75 for (int i = 0; i < num_; i++) {
76 parcel.WriteInt32(uids_[i]);
77 }
78 // write names
79 parcel.WriteInt32(num_);
80 for (int i = 0; i < num_; i++) {
81 parcel.WriteString16(Str8ToStr16(names_[i]));
82 }
83 return true;
84 }
85
ToString()86 std::string WorkRecord::ToString()
87 {
88 std::unique_lock<std::mutex> lock(workRecordMutex_);
89 std::string result = "[";
90 if (!IsEmpty()) {
91 for (int i = 0; i < num_; i++) {
92 result += std::to_string(uids_[i]);
93 result += ",";
94 result += std::to_string(pids_[i]);
95 result += ",";
96 result += names_[i];
97 result += ",";
98 result += std::to_string(timeInterval_[i]);
99 result += ",";
100 result += uuid_[i];
101 result += "; ";
102 }
103 }
104 result += "]";
105 return result;
106 }
107
GetName(int index)108 std::string WorkRecord::GetName(int index)
109 {
110 std::unique_lock<std::mutex> lock(workRecordMutex_);
111 if (index >= 0 && index < num_) {
112 return names_[index];
113 }
114 return "";
115 }
116
GetUid(int index)117 int WorkRecord::GetUid(int index)
118 {
119 std::unique_lock<std::mutex> lock(workRecordMutex_);
120 if (index >= 0 && index < num_) {
121 return uids_[index];
122 }
123 return -1;
124 }
125
GetPid(int index)126 int WorkRecord::GetPid(int index)
127 {
128 std::unique_lock<std::mutex> lock(workRecordMutex_);
129 if (index >= 0 && index < num_) {
130 return pids_[index];
131 }
132 return -1;
133 }
134
GetTimeInterval(int index)135 int WorkRecord::GetTimeInterval(int index)
136 {
137 std::unique_lock<std::mutex> lock(workRecordMutex_);
138 if (index >= 0 && index < num_) {
139 return timeInterval_[index];
140 }
141 return -1;
142 }
143
GetUuid(int index)144 std::string WorkRecord::GetUuid(int index)
145 {
146 std::unique_lock<std::mutex> lock(workRecordMutex_);
147 if (index >= 0 && index < num_) {
148 return uuid_[index];
149 }
150 return "";
151 }
152
SetDeviceId(std::string deviceId)153 void WorkRecord::SetDeviceId(std::string deviceId)
154 {
155 deviceId_ = deviceId;
156 }
157
GetDeviceId()158 std::string WorkRecord::GetDeviceId()
159 {
160 return deviceId_;
161 }
162
Size()163 int WorkRecord::Size()
164 {
165 return num_;
166 }
167
IsEmpty()168 bool WorkRecord::IsEmpty()
169 {
170 if (num_ == 0) {
171 return true;
172 }
173 return false;
174 }
175
Add(int uid,int pid,std::string name,int timeInterval,std::string uuid)176 bool WorkRecord::Add(int uid, int pid, std::string name, int timeInterval, std::string uuid)
177 {
178 std::unique_lock<std::mutex> lock(workRecordMutex_);
179 uids_.push_back(uid);
180 pids_.push_back(pid);
181 names_.push_back(name);
182 timeInterval_.push_back(timeInterval);
183 uuid_.push_back(uuid);
184 num_++;
185 return true;
186 }
187
Remove(int uid,int pid,std::string name,std::string uuid)188 bool WorkRecord::Remove(int uid, int pid, std::string name, std::string uuid)
189 {
190 std::unique_lock<std::mutex> lock(workRecordMutex_);
191 if (uids_.size() <= 0) {
192 return false;
193 }
194 unsigned int i = 0;
195 for (auto iterUid = uids_.begin(); iterUid != uids_.end(); iterUid++, i++) {
196 if (*iterUid == uid) {
197 if ((name.compare(names_[i]) == 0) && (uuid.compare(uuid_[i]) == 0)) {
198 break;
199 }
200 }
201 }
202 if (uids_.size() - i == 0) {
203 return false;
204 }
205 uids_.erase(uids_.begin() + i);
206 pids_.erase(pids_.begin() + i);
207 names_.erase(names_.begin() + i);
208 timeInterval_.erase(timeInterval_.begin() + i);
209 uuid_.erase(uuid_.begin() + i);
210 num_--;
211 return true;
212 }
213
Remove(std::string name)214 bool WorkRecord::Remove(std::string name)
215 {
216 std::unique_lock<std::mutex> lock(workRecordMutex_);
217 if (uids_.size() <= 0) {
218 return false;
219 }
220 unsigned int i = 0;
221 for (auto iter = names_.begin(); iter != names_.end(); iter++, i++) {
222 if (iter->compare(name) == 0) {
223 break;
224 }
225 }
226 if (names_.size() - i == 0) {
227 return false;
228 }
229 uids_.erase(uids_.begin() + i);
230 pids_.erase(pids_.begin() + i);
231 names_.erase(names_.begin() + i);
232 timeInterval_.erase(timeInterval_.begin() + i);
233 uuid_.erase(uuid_.begin() + i);
234 num_--;
235 return true;
236 }
237
Find(int uid,std::string name,std::string uuid)238 bool WorkRecord::Find(int uid, std::string name, std::string uuid)
239 {
240 std::unique_lock<std::mutex> lock(workRecordMutex_);
241 if (uids_.size() <= 0) {
242 return false;
243 }
244 int i = 0;
245 for (auto iterUid = uids_.begin(); iterUid != uids_.end(); iterUid++, i++) {
246 if (*iterUid == uid) {
247 if ((name.compare(names_[i]) == 0) && (uuid.compare(uuid_[i]) == 0)) {
248 return true;
249 }
250 }
251 }
252 return false;
253 }
254
Clear()255 void WorkRecord::Clear()
256 {
257 std::unique_lock<std::mutex> lock(workRecordMutex_);
258 uids_.clear();
259 pids_.clear();
260 names_.clear();
261 timeInterval_.clear();
262 uuid_.clear();
263 num_ = 0;
264 }
265
Set(WorkRecord & workRecord)266 void WorkRecord::Set(WorkRecord &workRecord)
267 {
268 Clear();
269 int num = workRecord.Size();
270 for (int i = 0; i < num; i++) {
271 int uid = workRecord.GetUid(i);
272 int pid = workRecord.GetPid(i);
273 std::string name = workRecord.GetName(i);
274 int timeInterval = workRecord.GetTimeInterval(i);
275 std::string uuid = workRecord.GetUuid(i);
276 Add(uid, pid, name, timeInterval, uuid);
277 }
278 }
279 } // namespace Location
280 } // namespace OHOS
281