1 /*
2  * Copyright (c) 2022-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 #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_INFO_H
16 #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_INFO_H
17 
18 #include <cstdint>
19 #include <list>
20 #include <map>
21 #include <memory>
22 #include <string>
23 
24 #include <parcel.h>
25 #include "json/json.h"
26 #include "want_params.h"
27 #include "refbase.h"
28 
29 #include "work_condition.h"
30 
31 namespace OHOS {
32 namespace WorkScheduler {
33 class WorkInfo : public Parcelable {
34 public:
35     explicit WorkInfo();
36     ~WorkInfo() override;
37     /**
38      * @brief Set the id of workId.
39      *
40      * @param workId The id of work.
41      */
42     void SetWorkId(int32_t workId);
43     /**
44      * @brief Set element.
45      *
46      * @param bundleName The name of bundle.
47      * @param abilityName The name of ability
48      */
49     void SetElement(std::string bundleName, std::string abilityName);
50     /**
51      * @brief Request persisted.
52      *
53      * @param persisted The persisted.
54      */
55     void RequestPersisted(bool persisted);
56     /**
57      * @brief Request network type.
58      *
59      * @param condition The condition.
60      */
61     void RequestNetworkType(WorkCondition::Network condition);
62     /**
63      * @brief Request charger type.
64      *
65      * @param isCharging Charging or not.
66      * @param condition The condition.
67      */
68     void RequestChargerType(bool isCharging, WorkCondition::Charger condition);
69     /**
70      * @brief Request battery level.
71      *
72      * @param battLevel The battery level.
73      */
74     void RequestBatteryLevel(int32_t battLevel);
75     /**
76      * @brief Request battery status.
77      *
78      * @param condition The condition.
79      */
80     void RequestBatteryStatus(WorkCondition::BatteryStatus condition);
81     /**
82      * @brief Request storage level.
83      *
84      * @param condition The condition.
85      */
86     void RequestStorageLevel(WorkCondition::Storage condition);
87     /**
88      * @brief Request repeat cycle.
89      *
90      * @param timeInterval The time interval.
91      * @param cycle The cycle.
92      */
93     void RequestRepeatCycle(uint32_t timeInterval, int32_t cycle);
94     /**
95      * @brief Request repeat cycle.
96      *
97      * @param timeInterval The time interval.
98      */
99     void RequestRepeatCycle(uint32_t timeInterval);
100     /**
101      * @brief Request base time and repeat cycle.
102      *
103      * @param baseTime The base time.
104      * @param cycle The cycle.
105      */
106     void RequestBaseTimeAndCycle(time_t baseTime, int32_t cycle);
107     /**
108      * @brief Request base time.
109      *
110      * @param baseTime The base time.
111      */
112     void RequestBaseTime(time_t baseTime);
113     /**
114      * @brief Request extra parameters.
115      *
116      * @param extras extra parameters.
117      */
118     void RequestExtras(AAFwk::WantParams extras);
119     /**
120      * @brief Request DeepIdle.
121      *
122      * @param deepIdle The DeepIdle status.
123      */
124     void RequestDeepIdle(bool deepIdle);
125     /**
126      * @brief Refresh uid.
127      *
128      * @param uid The uid.
129      */
130     void RefreshUid(int32_t uid);
131     /**
132      * @brief Refresh appIndex.
133      *
134      * @param appIndex The appIndex.
135      */
136     void RefreshAppIndex(int32_t appIndex);
137     /**
138      * @brief Refresh extension.
139      *
140      * @param extension The extension.
141      */
142     void RefreshExtension(bool extension);
143     /**
144      * @brief Set callBySystemApp flag.
145      */
146     void SetCallBySystemApp(bool callBySystemApp);
147     /**
148      * @brief Get uid.
149      *
150      * @return The uid.
151      */
152     int32_t GetUid();
153 
154     /**
155      * @brief Get the id of work.
156      *
157      * @return The id of work.
158      */
159     int32_t GetWorkId();
160     /**
161      * @brief Get the name of bundle.
162      *
163      * @return The name of bundle.
164      */
165     std::string GetBundleName();
166     /**
167      * @brief Get the name of ability.
168      *
169      * @return The name of ability.
170      */
171     std::string GetAbilityName();
172     /**
173      * @brief Check whether the work is persist.
174      *
175      * @return Persist or not.
176      */
177     bool IsPersisted();
178     /**
179      * @brief Get the type of network.
180      *
181      * @return The type of network.
182      */
183     WorkCondition::Network GetNetworkType();
184     /**
185      * @brief Get the type of charger.
186      *
187      * @return The type of charger.
188      */
189     WorkCondition::Charger GetChargerType();
190     /**
191      * @brief Get the level of battery.
192      *
193      * @return The level of battery.
194      */
195     int32_t GetBatteryLevel();
196     /**
197      * @brief Get the status of battery.
198      *
199      * @return The status of battery.
200      */
201     WorkCondition::BatteryStatus GetBatteryStatus();
202     /**
203      * @brief Get the level of storage.
204      *
205      * @return The level of storage.
206      */
207     WorkCondition::Storage GetStorageLevel();
208     /**
209      * @brief Check whether the work is repeat.
210      *
211      * @return Repeat or not.
212      */
213     bool IsRepeat();
214     /**
215      * @brief Get the time interval.
216      *
217      * @return The time interval.
218      */
219     uint32_t GetTimeInterval();
220     /**
221      * @brief Get the count of cycle.
222      *
223      * @return The count of cycle.
224      */
225     int32_t GetCycleCount();
226     /**
227      * @brief Get the base time.
228      *
229      * @return The base time.
230      */
231     time_t GetBaseTime();
232     /**
233      * @brief Get the map of condition.
234      *
235      * @return The map of condition.
236      */
237     std::shared_ptr<std::map<WorkCondition::Type, std::shared_ptr<Condition>>> GetConditionMap();
238     /**
239      * @brief Get extra parameters.
240      *
241      * @return extra parameters.
242      */
243     std::shared_ptr<AAFwk::WantParams> GetExtras() const;
244     /**
245      * @brief Get callBySystemApp flag.
246      */
247     bool IsCallBySystemApp();
248     /**
249      * @brief Marshalling.
250      *
251      * @param parcel The parcel.
252      * @return True if success,else false.
253      */
254     bool Marshalling(Parcel &parcel) const override;
255     /**
256      * @brief Unmarshalling.
257      *
258      * @param parcel The parcel.
259      * @return Read.
260      */
261     static sptr<WorkInfo> Unmarshalling(Parcel &parcel);
262     /**
263      * @brief Dump.
264      *
265      * @param result The result.
266      */
267     void Dump(std::string &result);
268     /**
269      * @brief Parse to json str.
270      *
271      * @return Result.
272      */
273     std::string ParseToJsonStr();
274     /**
275      * @brief Parse from json.
276      *
277      * @param value The value.
278      * @return True if success,else false.
279      */
280     bool ParseFromJson(const Json::Value &value);
281     /**
282      * @brief Set preinstalled flag.
283     */
284     void SetPreinstalled(bool preinstalled);
285     /**
286      * @brief Get preinstalled flag.
287      */
288     bool IsPreinstalled();
289     /**
290      * @brief Get uri key.
291      */
292     std::string GetUriKey();
293     /**
294      * @brief Get deepIdle status.
295      */
296     WorkCondition::DeepIdle GetDeepIdle();
297     /**
298      * @brief Get appIndex.
299      *
300      * @return The appIndex.
301      */
302     int32_t GetAppIndex() const;
303     /**
304      * @brief Get extension.
305      *
306      * @return The extension.
307      */
308     bool GetExtension() const;
309     /**
310      * @brief Get saId.
311      *
312      * @return The saId.
313      */
314     int32_t GetSaId() const;
315     /**
316      * @brief Refresh saId.
317      *
318      * @param saId The saId.
319      */
320     void RefreshSaId(int32_t saId);
321     /**
322      * @brief Get the work is residentSa.
323      *
324      * @return ResidentSa or not.
325      */
326     bool IsResidentSa() const;
327 
328 private:
329     int32_t workId_;
330     std::string bundleName_;
331     std::string abilityName_;
332     bool persisted_;
333     int32_t uid_;
334     std::shared_ptr<AAFwk::WantParams> extras_;
335     std::map<WorkCondition::Type, std::shared_ptr<Condition>> conditionMap_;
336     bool callBySystemApp_ {false};
337     bool preinstalled_ {false};
338     std::string uriKey_;
339     int32_t appIndex_;
340     bool extension_;
341     int32_t saId_;
342     bool residentSa_;
343 private:
344     static bool UnmarshallCondition(Parcel &parcel, sptr<WorkInfo> &read, uint32_t mapsize);
345     void ParseConditionToJsonStr(Json::Value &root);
346     void ParseConditionFromJsonStr(const Json::Value &value);
347     void ParseParametersFromJsonStr(const Json::Value &value);
348     void ParseTimerFormJsonStr(const Json::Value &conditions);
349     bool IsHasBoolProp(const Json::Value &value, const std::string &key);
350 };
351 } // namespace WorkScheduler
352 } // namespace OHOS
353 #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_WORK_INFO_H