• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "work_info.h"
16 
17 #include "work_sched_hilog.h"
18 #include "work_sched_constants.h"
19 
20 namespace OHOS {
21 namespace WorkScheduler {
22 const int32_t INVALID_TIME_VALUE = 0;
23 const uint32_t MAX_SIZE = 1024;
24 const int32_t APPINDEX_INIT_VALUE = 0;
25 
WorkInfo()26 WorkInfo::WorkInfo()
27 {
28     workId_ = INVALID_VALUE;
29     uid_ = INVALID_VALUE;
30     persisted_ = false;
31     extras_ = nullptr;
32     appIndex_ = APPINDEX_INIT_VALUE;
33     extension_ = true;
34     saId_ = INVALID_VALUE;
35     residentSa_ = false;
36 }
37 
~WorkInfo()38 WorkInfo::~WorkInfo() {}
39 
SetWorkId(int32_t workId)40 void WorkInfo::SetWorkId(int32_t workId)
41 {
42     workId_ = workId;
43 }
44 
SetElement(std::string bundleName,std::string abilityName)45 void WorkInfo::SetElement(std::string bundleName, std::string abilityName)
46 {
47     bundleName_ = bundleName;
48     abilityName_ = abilityName;
49 }
50 
RequestPersisted(bool persisted)51 void WorkInfo::RequestPersisted(bool persisted)
52 {
53     persisted_ = persisted;
54 }
55 
RequestNetworkType(WorkCondition::Network condition)56 void WorkInfo::RequestNetworkType(WorkCondition::Network condition)
57 {
58     std::shared_ptr<Condition> networkCondition = std::make_shared<Condition>();
59     networkCondition->enumVal = static_cast<int32_t>(condition);
60     conditionMap_.emplace(WorkCondition::Type::NETWORK, networkCondition);
61 }
62 
RequestChargerType(bool isCharging,WorkCondition::Charger condition)63 void WorkInfo::RequestChargerType(bool isCharging, WorkCondition::Charger condition)
64 {
65     std::shared_ptr<Condition> chargerCondition = std::make_shared<Condition>();
66     chargerCondition->boolVal = isCharging;
67     chargerCondition->enumVal = static_cast<int32_t>(condition);
68     conditionMap_.emplace(WorkCondition::Type::CHARGER, chargerCondition);
69 }
70 
RequestBatteryLevel(int32_t battLevel)71 void WorkInfo::RequestBatteryLevel(int32_t battLevel)
72 {
73     std::shared_ptr<Condition> batteryLevelCondition = std::make_shared<Condition>();
74     batteryLevelCondition->intVal = battLevel;
75     conditionMap_.emplace(WorkCondition::Type::BATTERY_LEVEL, batteryLevelCondition);
76 }
77 
RequestBatteryStatus(WorkCondition::BatteryStatus condition)78 void WorkInfo::RequestBatteryStatus(WorkCondition::BatteryStatus condition)
79 {
80     std::shared_ptr<Condition> batteryCondition = std::make_shared<Condition>();
81     batteryCondition->enumVal = static_cast<int32_t>(condition);
82     conditionMap_.emplace(WorkCondition::Type::BATTERY_STATUS, batteryCondition);
83 }
84 
RequestStorageLevel(WorkCondition::Storage condition)85 void WorkInfo::RequestStorageLevel(WorkCondition::Storage condition)
86 {
87     std::shared_ptr<Condition> storageCondition = std::make_shared<Condition>();
88     storageCondition->enumVal = static_cast<int32_t>(condition);
89     conditionMap_.emplace(WorkCondition::Type::STORAGE, storageCondition);
90 }
91 
RequestRepeatCycle(uint32_t timeInterval,int32_t cycle)92 void WorkInfo::RequestRepeatCycle(uint32_t timeInterval, int32_t cycle)
93 {
94     std::shared_ptr<Condition> repeatCycle = std::make_shared<Condition>();
95     repeatCycle->uintVal = timeInterval;
96     repeatCycle->intVal = cycle;
97     repeatCycle->boolVal = false;
98     conditionMap_.emplace(WorkCondition::Type::TIMER, repeatCycle);
99 }
100 
RequestRepeatCycle(uint32_t timeInterval)101 void WorkInfo::RequestRepeatCycle(uint32_t timeInterval)
102 {
103     std::shared_ptr<Condition> repeatCycle = std::make_shared<Condition>();
104     repeatCycle->uintVal = timeInterval;
105     repeatCycle->boolVal = true;
106     conditionMap_.emplace(WorkCondition::Type::TIMER, repeatCycle);
107 }
108 
RequestBaseTimeAndCycle(time_t baseTime,int32_t cycle)109 void WorkInfo::RequestBaseTimeAndCycle(time_t baseTime, int32_t cycle)
110 {
111     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
112         conditionMap_.at(WorkCondition::Type::TIMER)->timeVal = baseTime;
113         conditionMap_.at(WorkCondition::Type::TIMER)->intVal = cycle;
114     }
115 }
116 
RequestBaseTime(time_t baseTime)117 void WorkInfo::RequestBaseTime(time_t baseTime)
118 {
119     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
120         conditionMap_.at(WorkCondition::Type::TIMER)->timeVal = baseTime;
121     }
122 }
123 
RequestExtras(AAFwk::WantParams extras)124 void WorkInfo::RequestExtras(AAFwk::WantParams extras)
125 {
126     extras_ = std::make_shared<AAFwk::WantParams>(extras);
127 }
128 
RefreshUid(int32_t uid)129 void WorkInfo::RefreshUid(int32_t uid)
130 {
131     uid_ = uid;
132 }
133 
RefreshAppIndex(int32_t appIndex)134 void WorkInfo::RefreshAppIndex(int32_t appIndex)
135 {
136     appIndex_ = appIndex;
137 }
138 
RefreshExtension(bool extension)139 void WorkInfo::RefreshExtension(bool extension)
140 {
141     extension_ = extension;
142 }
143 
RequestDeepIdle(bool deepIdle)144 void WorkInfo::RequestDeepIdle(bool deepIdle)
145 {
146     std::shared_ptr<Condition> deepIdleCondition = std::make_shared<Condition>();
147     deepIdleCondition->boolVal = deepIdle;
148     conditionMap_.emplace(WorkCondition::Type::DEEP_IDLE, deepIdleCondition);
149 }
150 
SetCallBySystemApp(bool callBySystemApp)151 void WorkInfo::SetCallBySystemApp(bool callBySystemApp)
152 {
153     callBySystemApp_ = callBySystemApp;
154 }
155 
SetPreinstalled(bool preinstalled)156 void WorkInfo::SetPreinstalled(bool preinstalled)
157 {
158     preinstalled_ = preinstalled;
159 }
160 
IsPreinstalled()161 bool WorkInfo::IsPreinstalled()
162 {
163     return preinstalled_;
164 }
165 
GetUriKey()166 std::string WorkInfo::GetUriKey()
167 {
168     return uriKey_;
169 }
170 
GetUid()171 int32_t WorkInfo::GetUid()
172 {
173     return uid_;
174 }
175 
GetWorkId()176 int32_t WorkInfo::GetWorkId()
177 {
178     return workId_;
179 }
180 
GetBundleName()181 std::string WorkInfo::GetBundleName()
182 {
183     return bundleName_;
184 }
185 
GetAbilityName()186 std::string WorkInfo::GetAbilityName()
187 {
188     return abilityName_;
189 }
190 
IsPersisted()191 bool WorkInfo::IsPersisted()
192 {
193     return persisted_;
194 }
195 
GetAppIndex() const196 int32_t WorkInfo::GetAppIndex() const
197 {
198     return appIndex_;
199 }
200 
GetExtension() const201 bool WorkInfo::GetExtension() const
202 {
203     return extension_;
204 }
205 
GetNetworkType()206 WorkCondition::Network WorkInfo::GetNetworkType()
207 {
208     if (conditionMap_.count(WorkCondition::Type::NETWORK) > 0) {
209         int32_t enumVal = conditionMap_.at(WorkCondition::Type::NETWORK)->enumVal;
210         WorkCondition::Network network = WorkCondition::Network(enumVal);
211         return WorkCondition::Network(network);
212     }
213     return WorkCondition::Network::NETWORK_UNKNOWN;
214 }
215 
GetChargerType()216 WorkCondition::Charger WorkInfo::GetChargerType()
217 {
218     if (conditionMap_.count(WorkCondition::Type::CHARGER) > 0) {
219         int32_t enumVal = conditionMap_.at(WorkCondition::Type::CHARGER)->enumVal;
220         WorkCondition::Charger charger = WorkCondition::Charger(enumVal);
221         return WorkCondition::Charger(charger);
222     }
223     return WorkCondition::Charger::CHARGING_UNKNOWN;
224 }
225 
GetBatteryLevel()226 int32_t WorkInfo::GetBatteryLevel()
227 {
228     if (conditionMap_.count(WorkCondition::Type::BATTERY_LEVEL) > 0) {
229         return conditionMap_.at(WorkCondition::Type::BATTERY_LEVEL)->intVal;
230     }
231     return INVALID_VALUE;
232 }
233 
GetBatteryStatus()234 WorkCondition::BatteryStatus WorkInfo::GetBatteryStatus()
235 {
236     if (conditionMap_.count(WorkCondition::Type::BATTERY_STATUS) > 0) {
237         int32_t enumVal = conditionMap_.at(WorkCondition::Type::BATTERY_STATUS)->enumVal;
238         WorkCondition::BatteryStatus battery = WorkCondition::BatteryStatus(enumVal);
239         return WorkCondition::BatteryStatus(battery);
240     }
241     return WorkCondition::BatteryStatus::BATTERY_UNKNOWN;
242 }
243 
GetStorageLevel()244 WorkCondition::Storage WorkInfo::GetStorageLevel()
245 {
246     if (conditionMap_.count(WorkCondition::Type::STORAGE) > 0) {
247         int32_t enumVal = conditionMap_.at(WorkCondition::Type::STORAGE)->enumVal;
248         WorkCondition::Storage storage = WorkCondition::Storage(enumVal);
249         return WorkCondition::Storage(storage);
250     }
251     return WorkCondition::Storage::STORAGE_UNKNOWN;
252 }
253 
IsRepeat()254 bool WorkInfo::IsRepeat()
255 {
256     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
257         return conditionMap_.at(WorkCondition::Type::TIMER)->boolVal;
258     }
259     return false;
260 }
261 
GetTimeInterval()262 uint32_t WorkInfo::GetTimeInterval()
263 {
264     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
265         return conditionMap_.at(WorkCondition::Type::TIMER)->uintVal;
266     }
267     return INVALID_TIME_VALUE;
268 }
269 
GetCycleCount()270 int32_t WorkInfo::GetCycleCount()
271 {
272     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
273         if (IsRepeat()) {
274             return INVALID_VALUE;
275         }
276         return conditionMap_.at(WorkCondition::Type::TIMER)->intVal;
277     }
278     return INVALID_VALUE;
279 }
280 
GetBaseTime()281 time_t WorkInfo::GetBaseTime()
282 {
283     if (conditionMap_.count(WorkCondition::Type::TIMER) > 0) {
284         return conditionMap_.at(WorkCondition::Type::TIMER)->timeVal;
285     }
286     time_t result;
287     time(&result);
288     return result;
289 }
290 
GetDeepIdle()291 WorkCondition::DeepIdle WorkInfo::GetDeepIdle()
292 {
293     if (conditionMap_.count(WorkCondition::Type::DEEP_IDLE) <= 0) {
294         return WorkCondition::DeepIdle::DEEP_IDLE_UNKNOWN;
295     }
296     return conditionMap_.at(WorkCondition::Type::DEEP_IDLE)->boolVal ?
297         WorkCondition::DeepIdle::DEEP_IDLE_IN : WorkCondition::DeepIdle::DEEP_IDLE_OUT;
298 }
299 
GetConditionMap()300 std::shared_ptr<std::map<WorkCondition::Type, std::shared_ptr<Condition>>> WorkInfo::GetConditionMap()
301 {
302     return std::make_shared<std::map<WorkCondition::Type, std::shared_ptr<Condition>>>(conditionMap_);
303 }
304 
GetExtras() const305 std::shared_ptr<AAFwk::WantParams> WorkInfo::GetExtras() const
306 {
307     return extras_;
308 }
309 
IsCallBySystemApp()310 bool WorkInfo::IsCallBySystemApp()
311 {
312     return callBySystemApp_;
313 }
314 
Marshalling(Parcel & parcel) const315 bool WorkInfo::Marshalling(Parcel &parcel) const
316 {
317     bool ret = false;
318     ret = parcel.WriteInt32(workId_);
319     ret = ret && parcel.WriteString(bundleName_);
320     ret = ret && parcel.WriteString(abilityName_);
321     ret = ret && parcel.WriteBool(persisted_);
322     ret = ret && parcel.WriteInt32(uid_);
323     ret = ret && parcel.WriteUint32(conditionMap_.size());
324     for (auto it : conditionMap_) {
325         switch (it.first) {
326             case WorkCondition::Type::NETWORK:
327             case WorkCondition::Type::BATTERY_STATUS:
328             case WorkCondition::Type::STORAGE: {
329                 ret = ret && parcel.WriteInt32(it.first);
330                 ret = ret && parcel.WriteInt32(it.second->enumVal);
331                 break;
332             }
333             case WorkCondition::Type::CHARGER:
334             case WorkCondition::Type::DEEP_IDLE: {
335                 ret = ret && parcel.WriteInt32(it.first);
336                 ret = ret && parcel.WriteBool(it.second->boolVal);
337                 ret = ret && parcel.WriteInt32(it.second->enumVal);
338                 break;
339             }
340             case WorkCondition::Type::BATTERY_LEVEL: {
341                 ret = ret && parcel.WriteInt32(it.first);
342                 ret = ret && parcel.WriteInt32(it.second->intVal);
343                 break;
344             }
345             case WorkCondition::Type::TIMER: {
346                 ret = ret && parcel.WriteInt32(it.first);
347                 ret = ret && parcel.WriteUint32(it.second->uintVal);
348                 ret = ret && parcel.WriteBool(it.second->boolVal);
349                 if (!it.second->boolVal) {
350                     ret = ret && parcel.WriteInt32(it.second->intVal);
351                 }
352                 break;
353             }
354             default: {
355                 ret = false;
356             }
357         }
358     }
359     ret = ret && parcel.WriteBool(extras_ ? true : false);
360     if (extras_) {
361         ret = ret && extras_->Marshalling(parcel);
362     }
363     return ret;
364 }
365 
Unmarshalling(Parcel & parcel)366 WorkInfo* WorkInfo::Unmarshalling(Parcel &parcel)
367 {
368     auto read = new (std::nothrow) WorkInfo();
369     if (read == nullptr) {
370         WS_HILOGE("read is nullptr.");
371         return nullptr;
372     }
373     if (!parcel.ReadInt32(read->workId_) || !parcel.ReadString(read->bundleName_) ||
374         !parcel.ReadString(read->abilityName_)) {
375         WS_HILOGE("Failed to read the workId or bundleName or abilityName.");
376         delete read;
377         return nullptr;
378     }
379     if (!parcel.ReadBool(read->persisted_)) {
380         WS_HILOGE("Failed to read the persisted.");
381         delete read;
382         return nullptr;
383     }
384     if (!parcel.ReadInt32(read->uid_)) {
385         WS_HILOGE("Failed to read the uid.");
386         delete read;
387         return nullptr;
388     }
389     uint32_t mapsize;
390     if (!parcel.ReadUint32(mapsize) || mapsize >= MAX_SIZE) {
391         WS_HILOGE("Failed to read the mapsize or mapsize is too big.");
392         delete read;
393         return nullptr;
394     }
395     if (!UnmarshallCondition(parcel, read, mapsize)) {
396         WS_HILOGE("Failed to read the work condition map.");
397         delete read;
398         return nullptr;
399     }
400     bool hasExtras;
401     if (!parcel.ReadBool(hasExtras)) {
402         WS_HILOGE("Failed to read the extras existence.");
403         delete read;
404         return nullptr;
405     }
406     if (!hasExtras) {
407         return read;
408     }
409     AAFwk::WantParams *wantParams = AAFwk::WantParams::Unmarshalling(parcel);
410     if (wantParams != nullptr) {
411         read->extras_ = std::make_shared<AAFwk::WantParams>(*wantParams);
412     }
413     return read;
414 }
415 
UnmarshallCondition(Parcel & parcel,WorkInfo * read,uint32_t mapsize)416 bool WorkInfo::UnmarshallCondition(Parcel &parcel, WorkInfo* read, uint32_t mapsize)
417 {
418     read->conditionMap_ = std::map<WorkCondition::Type, std::shared_ptr<Condition>>();
419     for (uint32_t i = 0; i < mapsize; i++) {
420         int32_t key;
421         if (!parcel.ReadInt32(key)) {
422             return false;
423         }
424         auto condition = std::make_shared<Condition>();
425         switch (key) {
426             case WorkCondition::Type::NETWORK:
427             case WorkCondition::Type::BATTERY_STATUS:
428             case WorkCondition::Type::STORAGE: {
429                 if (!parcel.ReadInt32(condition->enumVal)) {
430                     return false;
431                 }
432                 break;
433             }
434             case WorkCondition::Type::DEEP_IDLE:
435             case WorkCondition::Type::CHARGER: {
436                 if (!parcel.ReadBool(condition->boolVal) || !parcel.ReadInt32(condition->enumVal)) {
437                     return false;
438                 }
439                 break;
440             }
441             case WorkCondition::Type::BATTERY_LEVEL: {
442                 if (!parcel.ReadInt32(condition->intVal)) {
443                     return false;
444                 }
445                 break;
446             }
447             case WorkCondition::Type::TIMER: {
448                 if (!parcel.ReadUint32(condition->uintVal) || !parcel.ReadBool(condition->boolVal)) {
449                     return false;
450                 }
451                 if (!condition->boolVal && !parcel.ReadInt32(condition->intVal)) {
452                     return false;
453                 }
454                 break;
455             }
456             default: {
457             }
458         }
459         read->conditionMap_.emplace(WorkCondition::Type(key), condition);
460     }
461     return true;
462 }
463 
ParseToJsonStr()464 std::string WorkInfo::ParseToJsonStr()
465 {
466     nlohmann::json root;
467     if (uid_ != INVALID_VALUE) {
468         root["uid"] = uid_;
469     }
470     root["workId"] = workId_;
471     if (IsSA()) {
472         root["saId"] = saId_;
473         root["resident"] = IsResidentSa() ? "true" : "false";
474     } else {
475         root["bundleName"] = bundleName_;
476         root["abilityName"] = abilityName_;
477         root["callBySystemApp"] = callBySystemApp_;
478         root["appIndex"] = appIndex_;
479         root["extension"] = extension_;
480     }
481     root["persisted"] = persisted_;
482     root["preinstalled"] = preinstalled_;
483     root["uriKey"] = uriKey_;
484     ParseConditionToJsonStr(root);
485     if (extras_) {
486         nlohmann::json extras;
487         nlohmann::json extrasType;
488         std::map<std::string, sptr<AAFwk::IInterface>> extrasMap = extras_->GetParams();
489         int typeId = INVALID_VALUE;
490         for (auto it : extrasMap) {
491             typeId = AAFwk::WantParams::GetDataType(it.second);
492             extrasType[it.first] = typeId;
493             if (typeId != INVALID_VALUE) {
494                 std::string value = AAFwk::WantParams::GetStringByType(it.second, typeId);
495                 extras[it.first] = value;
496             } else {
497                 WS_HILOGE("parameters: type error.");
498             }
499         }
500         root["parameters"] = extras;
501         root["parametersType"] = extrasType;
502     }
503     std::string result = root.dump(JSON_INDENT_WIDTH);
504     return result;
505 }
506 
ParseConditionToJsonStr(nlohmann::json & root)507 void WorkInfo::ParseConditionToJsonStr(nlohmann::json &root)
508 {
509     nlohmann::json conditions;
510     for (auto it : conditionMap_) {
511         switch (it.first) {
512             case WorkCondition::Type::NETWORK: {
513                 conditions["network"] = it.second->enumVal;
514                 break;
515             }
516             case WorkCondition::Type::CHARGER: {
517                 conditions["isCharging"] = it.second->boolVal;
518                 conditions["chargerType"] = it.second->enumVal;
519                 break;
520             }
521             case WorkCondition::Type::BATTERY_LEVEL: {
522                 conditions["batteryLevel"] = it.second->intVal;
523                 break;
524             }
525             case WorkCondition::Type::BATTERY_STATUS: {
526                 conditions["batteryStatus"] = it.second->enumVal;
527                 break;
528             }
529             case WorkCondition::Type::STORAGE: {
530                 conditions["storage"] = it.second->enumVal;
531                 break;
532             }
533             case WorkCondition::Type::TIMER: {
534                 conditions["timer"] = it.second->uintVal;
535                 conditions["repeat"] = it.second->boolVal;
536                 conditions["baseTime"] = it.second->timeVal;
537                 if (!it.second->boolVal) {
538                     conditions["cycle"] = it.second->intVal;
539                 }
540                 break;
541             }
542             case WorkCondition::Type::DEEP_IDLE: {
543                 conditions["isDeepIdle"] = it.second->boolVal;
544                 break;
545             }
546             default: {}
547         }
548     }
549     root["conditions"] = conditions;
550 }
551 
ParseFromJson(const nlohmann::json & value)552 bool WorkInfo::ParseFromJson(const nlohmann::json &value)
553 {
554     if (value.is_null() || value.empty()) {
555         WS_HILOGE("workinfo json is empty");
556         return false;
557     }
558     if (!value.contains("workId") || !value["workId"].is_number_integer()) {
559         WS_HILOGE("workinfo json is invalid, workId is missing or not int");
560         return false;
561     }
562     this->workId_ = value["workId"].get<int32_t>();
563     if ((value.contains("saId") && value["saId"].is_number_integer()) && IsHasBoolProp(value, "residentSa")) {
564         this->saId_ = value["saId"].get<int32_t>();
565         this->residentSa_ = value["residentSa"].get<bool>();
566     }
567     if (!ParseElementFromJson(value)) {
568         return false;
569     }
570     if (IsHasBoolProp(value, "persisted")) {
571         this->persisted_ = value["persisted"].get<bool>();
572     }
573     if (IsHasBoolProp(value, "preinstalled")) {
574         this->preinstalled_ = value["preinstalled"].get<bool>();
575     }
576     if (value.contains("uriKey") && value["uriKey"].is_string()) {
577         this->uriKey_ = value["uriKey"].get<std::string>();
578     }
579     if (IsHasBoolProp(value, "callBySystemApp")) {
580         this->callBySystemApp_ = value["callBySystemApp"].get<bool>();
581     }
582     if (value.contains("appIndex") && value["appIndex"].is_number_integer()) {
583         this->appIndex_ = value["appIndex"].get<int32_t>();
584     }
585     if (IsHasBoolProp(value, "extension")) {
586         this->extension_ = value["extension"].get<bool>();
587     }
588     ParseConditionFromJsonStr(value);
589     if (!value.contains("parameters")) {
590         return true;
591     }
592     ParseParametersFromJsonStr(value);
593     return true;
594 }
595 
ParseElementFromJson(const nlohmann::json & value)596 bool WorkInfo::ParseElementFromJson(const nlohmann::json &value)
597 {
598     if (!IsSA()) {
599         if (!value.contains("bundleName") || !value["bundleName"].is_string() ||
600             !value.contains("abilityName") || !value["abilityName"].is_string()) {
601             WS_HILOGE("workinfo json is invalid, bundleName or abilityName is missing or not string");
602             return false;
603         }
604         this->bundleName_ = value["bundleName"].get<std::string>();
605         this->abilityName_ = value["abilityName"].get<std::string>();
606     }
607     return true;
608 }
609 
ParseParametersFromJsonStr(const nlohmann::json & value)610 void WorkInfo::ParseParametersFromJsonStr(const nlohmann::json &value)
611 {
612     nlohmann::json extrasJson = value["parameters"];
613     nlohmann::json extrasType = value["parametersType"];
614     AAFwk::WantParams extras;
615     int typeId = INVALID_VALUE;
616     for (const auto &[key, parameter] : extrasJson.items()) {
617         if (extrasType[key].is_number_integer()) {
618             typeId = extrasType[key].get<int32_t>();
619         }
620         if (typeId != INVALID_VALUE && extrasJson[key].is_string()) {
621             sptr<AAFwk::IInterface> exInterface = AAFwk::WantParams::GetInterfaceByType(typeId,
622                 extrasJson[key].get<std::string>());
623             extras.SetParam(key, exInterface);
624         }
625     }
626     this->RequestExtras(extras);
627 }
628 
ParseConditionFromJsonStr(const nlohmann::json & value)629 void WorkInfo::ParseConditionFromJsonStr(const nlohmann::json &value)
630 {
631     if (value.contains("uid") && value["uid"].is_number_integer()) {
632         this->uid_ = value["uid"].get<int32_t>();
633     }
634     nlohmann::json conditions = value["conditions"];
635     if (conditions.contains("network") && conditions["network"].is_number_integer()) {
636         this->RequestNetworkType(WorkCondition::Network(conditions["network"].get<int32_t>()));
637     }
638     if (conditions.contains("isCharging") && conditions["isCharging"].is_boolean() &&
639         conditions.contains("chargerType") && conditions["chargerType"].is_number_integer()) {
640         this->RequestChargerType(conditions["isCharging"].get<bool>(),
641             WorkCondition::Charger(conditions["chargerType"].get<int32_t>()));
642     }
643     if (conditions.contains("batteryLevel") && conditions["batteryLevel"].is_number_integer()) {
644         this->RequestBatteryLevel(conditions["batteryLevel"].get<int32_t>());
645     }
646     if (conditions.contains("batteryStatus") && conditions["batteryStatus"].is_number_integer()) {
647         this->RequestBatteryStatus(WorkCondition::BatteryStatus(conditions["batteryStatus"].get<int32_t>()));
648     }
649     if (conditions.contains("storage") && conditions["storage"].is_number_integer()) {
650         this->RequestStorageLevel(WorkCondition::Storage(conditions["storage"].get<int32_t>()));
651     }
652     if (conditions.contains("isDeepIdle") && conditions["isDeepIdle"].is_boolean()) {
653         this->RequestDeepIdle(conditions["isDeepIdle"].get<bool>());
654     }
655     ParseTimerFormJsonStr(conditions);
656 }
657 
ParseTimerFormJsonStr(const nlohmann::json & conditions)658 void WorkInfo::ParseTimerFormJsonStr(const nlohmann::json &conditions)
659 {
660     if (conditions.contains("timer") && conditions["timer"].is_number_integer() &&
661         conditions.contains("repeat") && conditions["repeat"].is_boolean()) {
662         if (conditions["repeat"].get<bool>()) {
663             this->RequestRepeatCycle(conditions["timer"].get<int32_t>());
664         } else {
665             if (conditions.contains("cycle") && conditions["cycle"].is_number_integer()) {
666                 this->RequestRepeatCycle(conditions["timer"].get<int32_t>(), conditions["cycle"].get<int32_t>());
667             }
668         }
669         if (conditions.contains("baseTime") && conditions["baseTime"].is_number_integer()) {
670             time_t baseTime = (time_t)(conditions["baseTime"].get<int64_t>());
671             this->RequestBaseTime(baseTime);
672         }
673     }
674 }
675 
Dump(std::string & result)676 void WorkInfo::Dump(std::string &result)
677 {
678     result.append(ParseToJsonStr());
679 }
680 
IsHasBoolProp(const nlohmann::json & value,const std::string & key)681 bool WorkInfo::IsHasBoolProp(const nlohmann::json &value, const std::string &key)
682 {
683     if (value.contains(key) && value[key].is_boolean()) {
684         return true;
685     }
686     return false;
687 }
688 
GetSaId() const689 int32_t WorkInfo::GetSaId() const
690 {
691     return saId_;
692 }
693 
RefreshSaId(int32_t saId)694 void WorkInfo::RefreshSaId(int32_t saId)
695 {
696     saId_ = saId;
697 }
698 
IsResidentSa() const699 bool WorkInfo::IsResidentSa() const
700 {
701     return residentSa_;
702 }
703 
IsSA()704 bool WorkInfo::IsSA()
705 {
706     return saId_ != INVALID_VALUE;
707 }
708 
GetBriefInfo()709 std::string WorkInfo::GetBriefInfo()
710 {
711     if (IsSA()) {
712         return std::to_string(GetSaId()) + "_" + std::to_string(GetWorkId());
713     } else {
714         return GetBundleName() + "_" + std::to_string(GetWorkId());
715     }
716 }
717 
SetIsInnerApply(bool isInnerApply)718 void WorkInfo::SetIsInnerApply(bool isInnerApply)
719 {
720     isInnerApply_ = isInnerApply;
721 }
722 
GetIsInnerApply() const723 bool WorkInfo::GetIsInnerApply() const
724 {
725     return isInnerApply_;
726 }
727 } // namespace WorkScheduler
728 } // namespace OHOS
729