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 #ifndef WORK_SCHED_SERVICES_WORK_STATUS_H 17 #define WORK_SCHED_SERVICES_WORK_STATUS_H 18 19 #include <memory> 20 #include <string> 21 #include <map> 22 #include <mutex> 23 24 #include "timer.h" 25 #include "work_info.h" 26 27 namespace OHOS { 28 namespace WorkScheduler { 29 class WorkStatus { 30 public: 31 enum Status { 32 WAIT_CONDITION = 0, 33 CONDITION_READY, 34 RUNNING, 35 REMOVED 36 }; 37 WorkStatus(WorkInfo &workInfo, int32_t uid); 38 ~WorkStatus(); 39 40 /** 41 * @brief Make work id. 42 * 43 * @param workId The id of work. 44 * @param uid The uid. 45 * @return Workid and uid. 46 */ 47 static std::string MakeWorkId(int32_t workId, int32_t uid); 48 49 std::string workId_; 50 std::string bundleName_; 51 std::string abilityName_; 52 int32_t uid_; 53 int32_t userId_; 54 bool persisted_; 55 int32_t priority_; 56 bool needRetrigger_ {false}; 57 int32_t timeRetrigger_ {INT32_MAX}; 58 std::map<WorkCondition::Type, std::shared_ptr<Condition>> conditionMap_; 59 std::shared_ptr<WorkInfo> workInfo_; 60 61 /** 62 * @brief Judge state whether is ready. 63 * 64 * @return True if success,else false. 65 */ 66 bool IsReady(); 67 /** 68 * @brief Judge state whether is ready status. 69 * 70 * @return True if success,else false. 71 */ 72 bool IsReadyStatus(); 73 /** 74 * @brief Judge state whether is running. 75 * 76 * @return True if success,else false. 77 */ 78 bool IsRunning(); 79 /** 80 * @brief Judge state whether is removed. 81 * 82 * @return True if success,else false. 83 */ 84 bool IsRemoved(); 85 /** 86 * @brief Judge state whether is repeating. 87 * 88 * @return True if success,else false. 89 */ 90 bool IsRepeating(); 91 /** 92 * @brief Judge state whether is last work timeout. 93 * 94 * @return True if success,else false. 95 */ 96 bool IsLastWorkTimeout(); 97 /** 98 * @brief The OnConditionChanged callback. 99 * 100 * @param type The type. 101 * @param value The value. 102 */ 103 int32_t OnConditionChanged(WorkCondition::Type &type, std::shared_ptr<Condition> value); 104 /** 105 * @brief Mark round. 106 */ 107 void MarkRound(); 108 /** 109 * @brief Mark status. 110 */ 111 void MarkStatus(Status status); 112 /** 113 * @brief Get status. 114 * 115 * @return The current status. 116 */ 117 Status GetStatus(); 118 /** 119 * @brief Dump. 120 * 121 * @param result The result. 122 */ 123 void Dump(std::string& result); 124 /** 125 * @brief Update timer if need. 126 */ 127 void UpdateTimerIfNeed(); 128 /** 129 * @brief Need remove. 130 * 131 * @return True if success,else false. 132 */ 133 bool NeedRemove(); 134 135 bool lastTimeout_ {false}; 136 /** 137 * @brief Set min interval by group. 138 * 139 * @param group The new group. 140 * @return True if success,else false. 141 */ 142 bool SetMinIntervalByGroup(int32_t group); 143 /** 144 * @brief Update map<uid, lastTime>. 145 */ 146 void UpdateUidLastTimeMap(); 147 /** 148 * @brief clear uidLastTimeMap by uid. 149 */ 150 static void ClearUidLastTimeMap(int32_t uid); 151 /** 152 * @brief Set min interval by dump. 153 */ 154 void SetMinIntervalByDump(int64_t interval); 155 /** 156 * @brief get min interval. 157 */ 158 int64_t GetMinInterval(); 159 private: 160 Status currentStatus_; 161 time_t baseTime_; 162 int64_t minInterval_; 163 bool callbackFlag_; 164 bool isStandby_ {false}; 165 std::mutex conditionMapMutex_; 166 static std::mutex s_uid_last_time_mutex; 167 static std::map<int32_t, time_t> s_uid_last_time_map; 168 void MarkTimeout(); 169 bool IsSameUser(); 170 bool SetMinInterval(); 171 bool IsBatteryAndNetworkReady(WorkCondition::Type type); 172 bool IsStorageAndChargerAndTimerReady(WorkCondition::Type type); 173 int GetPriority(); 174 }; 175 } // namespace WorkScheduler 176 } // namespace OHOS 177 #endif // WORK_SCHED_SERVICES_WORK_STATUS_H