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