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