From 9dcb46cc81cae44d2532e7da48f36a9733b017d3 Mon Sep 17 00:00:00 2001 From: cheng-shichang Date: Sat, 30 Aug 2025 09:38:39 +0800 Subject: [PATCH 1/4] Signed-off-by: csc --- services/native/include/work_status.h | 25 +++++ services/native/src/work_status.cpp | 130 +++++++++++++++++++++++++- 2 files changed, 150 insertions(+), 5 deletions(-) diff --git a/services/native/include/work_status.h b/services/native/include/work_status.h index 04c9c36..3be3986 100644 --- a/services/native/include/work_status.h +++ b/services/native/include/work_status.h @@ -180,6 +180,31 @@ public: * @brief get min interval. */ int64_t GetMinInterval(); + /** + * @brief Judge whether the app is a mail application. + * + * @return True if the app is a mail app, false otherwise. + */ + bool IsMailApp(); + /** + * @brief Judge whether the device is currently charging. + * + * @return True if the device is charging, false otherwise. + */ + bool IsChargingState(); + /** + * @brief Set min interval when charging. + * + * @param group The new group. + */ + void SetMinIntervalWhenCharging(int32_t group); + /** + * @brief Set min interval when not charging. + * + * @param group The new group. + */ + void SetMinIntervalWhenNotCharging(int32_t group); + bool IsUriKeySwitchOn(); void ToString(WorkCondition::Type conditionType); void HasTimeout(); diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index fef833f..8d184e0 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -24,7 +24,7 @@ #include "work_sched_errors.h" #ifdef DEVICE_USAGE_STATISTICS_ENABLE #include "bundle_active_client.h" -#include "bundle_active_group_map.h" +#include "bundle_active_group_common.h" #endif #include "parameters.h" #include "work_sched_data_manager.h" @@ -32,6 +32,11 @@ #include "work_sched_constants.h" #include #include +#ifdef POWERMGR_BATTERY_MANAGER_ENABLE +#include "battery_srv_client.h" +#endif +#include "res_sched_client.h" +#include "res_type.h" using namespace std; @@ -43,11 +48,44 @@ static const int64_t MIN_INTERVAL_DEFAULT = 2 * 60 * 60 * 1000; std::map WorkStatus::s_uid_last_time_map; const int32_t DEFAULT_PRIORITY = 10000; const int32_t HIGH_PRIORITY = 0; -const int32_t ACTIVE_GROUP = 10; const string SWITCH_ON = "1"; const string DELIMITER = ","; ffrt::mutex WorkStatus::s_uid_last_time_mutex; +#ifdef DEVICE_USAGE_STATISTICS_ENABLE +namespace GroupConst{ +constexpr int32_t APP_TYPE_EMAIL = 673; +constexpr int64_t ONE_MINUTE = 60 * 1000LL; +constexpr int64_t TWENTY_MINUTE = 20 * ONE_MINUTE; +constexpr int64_t THIRTY_MINUTE = 30 * ONE_MINUTE; + +const std std::unordered_map GROUP_INTERVAL_MAP = { + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET, 0}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE, + DeviceUsageStats::DeviceUsageStatsGroupConst::TWO_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY, + 2 * DeviceUsageStats::DeviceUsageStatsGroupConst::TWO_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED, + DeviceUsageStats::DeviceUsageStatsGroupConst::TWENTY_FOUR_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE, + DeviceUsageStats::DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR}, +}; + +const std std::unordered_map MAIL_APP_GROUP_INTERVAL_MAP = { + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET, 0}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE,THIRTY_MINUTE}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY, + 2 * DeviceUsageStats::DeviceUsageStatsGroupConst::TWO_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED, + DeviceUsageStats::DeviceUsageStatsGroupConst::TWELVE_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE, + DeviceUsageStats::DeviceUsageStatsGroupConst::TWENTY_FOUR_HOUR}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_LIMIT, + DeviceUsageStats::DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR}, +}; +} // namespace GroupConst +#endif + std::unordered_map COND_TYPE_STRING_MAP = { {WorkCondition::Type::NETWORK, "NETWORK"}, {WorkCondition::Type::CHARGER, "CHARGER"}, @@ -423,13 +461,44 @@ bool WorkStatus::IsNapReady(WorkCondition::Type type) return true; } +bool WorkStatus::IsChargingState() +{ +#ifdef POWERMGR_BATTERY_MANAGER_ENABLE + auto type = PowerMgr::BatterySrcClient::GetInstance().GetPluggedType(); + return type != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE && + type != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_BUTT; +#else + return false; +#endif +} + +bool WorkStatus::IsMailApp() +{ + nlohmann::json payload; + nlohmann::json reply; + payload["bundleName"] = bundleName_; + int32_t ret = ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent( + ResourceSchedule::ResType::SYNC_RES_TYPE_GET_APP_TYPE, 0, payload, reply); + if (ret != ERR_OK) { + WS_HILOGE("getapp type err:%{public}d", ret); + return false; + } + int32_t pkgType = -1; + if (reply.contains("thirdKindId") && reply.at("thirdKindId").is_number_integer()) { + pkgType = reply["thirdKindId"].get(); + } else { + WS_HILOGD("no thirdKindId"); + } + return pkgType == GroupConst::APP_TYPE_EMAIL; +} + bool WorkStatus::SetMinInterval() { #ifdef DEVICE_USAGE_STATISTICS_ENABLE int32_t group = 0; if (workInfo_->IsCallBySystemApp()) { WS_HILOGD("system app %{public}s, default group is active.", bundleName_.c_str()); - return SetMinIntervalByGroup(ACTIVE_GROUP); + return SetMinIntervalByGroup(DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE); } bool res = DelayedSingleton::GetInstance()->FindGroup(bundleName_, userId_, group); if (!res) { @@ -438,12 +507,12 @@ bool WorkStatus::SetMinInterval() if (errCode != ERR_OK) { WS_HILOGE("query package group failed. userId = %{public}d, bundleName = %{public}s", userId_, bundleName_.c_str()); - group = ACTIVE_GROUP; + group = DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE; } DelayedSingleton::GetInstance()->AddGroup(bundleName_, userId_, group); } #else - int32_t group = ACTIVE_GROUP; + int32_t group = DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE; #endif return SetMinIntervalByGroup(group); } @@ -458,6 +527,11 @@ bool WorkStatus::SetMinIntervalByGroup(int32_t group) group > DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED) { newGroup = DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED; } + if (IsChargingState()) { + SetMinIntervalWhenCharging(newGroup); + } else { + SetMinIntervalWhenNotCharging(newGroup); + } auto itMap = DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_.find(newGroup); if (itMap != DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_.end()) { minInterval_ = DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_[newGroup]; @@ -473,6 +547,52 @@ bool WorkStatus::SetMinIntervalByGroup(int32_t group) return true; } +#ifdef DEVICE_USAGE_STATISTICS_ENABLE +void WorkStatus::SetMinIntervalWhenCharging(int32_t group) +{ + int32_t newGroup = group; + if (group < DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE) { + newGroup = DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE; + } else if (group == DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_LIMIT) { + newGroup = DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_RARE; + } + auto itMap = GroupConst::GROUP_INTERVAL_MAP.find(newGroup); + if (itMap == GroupConst::GROUP_INTERVAL_MAP.end()) { + WS_HILOGE("query package group interval failed. group:%{public}d, bundleName:%{public}s", + newGroup, bundleName_.c_str()); + minInterval_ = -1; + } + if (IsMailApp() && group == DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE) { + minInterval_ = GroupConst::TWENTY_MINUTE; + } else { + minInterval_ = GroupConst::GROUP_INTERVAL_MAP.at(newGroup); + } +} + +void WorkStatus::SetMinIntervalWhenNotCharging(int32_t group) +{ + if (IsMailAPP()) { + auto itMap = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.find(group); + if (itMap != GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.end()) { + minInterval_ = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.at(group);; + } else { + WS_HILOGE("query mail app group interval failed. group:%{public}d, bundleName:%{public}s", + group, bundleName_.c_str()); + minInterval_ = -1; + } + } else { + auto itMap = GroupConst::GROUP_INTERVAL_MAP.find(group); + if (itMap != GroupConst::GROUP_INTERVAL_MAP.end()) { + minInterval_ = GroupConst::GROUP_INTERVAL_MAP.at(group);; + } else { + WS_HILOGE("query package group interval failed. group:%{public}d, bundleName:%{public}s", + group, bundleName_.c_str()); + minInterval_ = -1; + } + } +} +#endif + void WorkStatus::SetMinIntervalByDump(int64_t interval) { WS_HILOGD("set min interval by dump to %{public}" PRId64 "", interval); -- Gitee From 3baa145b9b3acc0c85aae2af3408119e7d785e09 Mon Sep 17 00:00:00 2001 From: cheng-shichang Date: Sat, 30 Aug 2025 11:36:34 +0800 Subject: [PATCH 2/4] Signed-off-by: csc --- services/native/src/work_status.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index 8d184e0..d2fc704 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -53,7 +53,7 @@ const string DELIMITER = ","; ffrt::mutex WorkStatus::s_uid_last_time_mutex; #ifdef DEVICE_USAGE_STATISTICS_ENABLE -namespace GroupConst{ +namespace GroupConst { constexpr int32_t APP_TYPE_EMAIL = 673; constexpr int64_t ONE_MINUTE = 60 * 1000LL; constexpr int64_t TWENTY_MINUTE = 20 * ONE_MINUTE; @@ -73,7 +73,7 @@ const std std::unordered_map GROUP_INTERVAL_MAP = { const std std::unordered_map MAIL_APP_GROUP_INTERVAL_MAP = { {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET, 0}, - {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE,THIRTY_MINUTE}, + {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE, THIRTY_MINUTE}, {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY, 2 * DeviceUsageStats::DeviceUsageStatsGroupConst::TWO_HOUR}, {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FIXED, @@ -574,7 +574,7 @@ void WorkStatus::SetMinIntervalWhenNotCharging(int32_t group) if (IsMailAPP()) { auto itMap = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.find(group); if (itMap != GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.end()) { - minInterval_ = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.at(group);; + minInterval_ = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.at(group); } else { WS_HILOGE("query mail app group interval failed. group:%{public}d, bundleName:%{public}s", group, bundleName_.c_str()); @@ -583,7 +583,7 @@ void WorkStatus::SetMinIntervalWhenNotCharging(int32_t group) } else { auto itMap = GroupConst::GROUP_INTERVAL_MAP.find(group); if (itMap != GroupConst::GROUP_INTERVAL_MAP.end()) { - minInterval_ = GroupConst::GROUP_INTERVAL_MAP.at(group);; + minInterval_ = GroupConst::GROUP_INTERVAL_MAP.at(group); } else { WS_HILOGE("query package group interval failed. group:%{public}d, bundleName:%{public}s", group, bundleName_.c_str()); -- Gitee From 788c5f97253ab6eb20b18683febdfd08ac04ed23 Mon Sep 17 00:00:00 2001 From: cheng-shichang Date: Sat, 30 Aug 2025 15:24:40 +0800 Subject: [PATCH 3/4] Signed-off-by: csc --- services/native/src/work_status.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index d2fc704..84c97e3 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -59,7 +59,7 @@ constexpr int64_t ONE_MINUTE = 60 * 1000LL; constexpr int64_t TWENTY_MINUTE = 20 * ONE_MINUTE; constexpr int64_t THIRTY_MINUTE = 30 * ONE_MINUTE; -const std std::unordered_map GROUP_INTERVAL_MAP = { +const std::unordered_map GROUP_INTERVAL_MAP = { {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET, 0}, {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE, DeviceUsageStats::DeviceUsageStatsGroupConst::TWO_HOUR}, @@ -71,7 +71,7 @@ const std std::unordered_map GROUP_INTERVAL_MAP = { DeviceUsageStats::DeviceUsageStatsGroupConst::FOURTY_EIGHT_HOUR}, }; -const std std::unordered_map MAIL_APP_GROUP_INTERVAL_MAP = { +const std::unordered_map MAIL_APP_GROUP_INTERVAL_MAP = { {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_FORCED_SET, 0}, {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_ALIVE, THIRTY_MINUTE}, {DeviceUsageStats::DeviceUsageStatsGroupConst::ACTIVE_GROUP_DAILY, @@ -464,7 +464,7 @@ bool WorkStatus::IsNapReady(WorkCondition::Type type) bool WorkStatus::IsChargingState() { #ifdef POWERMGR_BATTERY_MANAGER_ENABLE - auto type = PowerMgr::BatterySrcClient::GetInstance().GetPluggedType(); + auto type = PowerMgr::BatterySrvClient::GetInstance().GetPluggedType(); return type != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_NONE && type != PowerMgr::BatteryPluggedType::PLUGGED_TYPE_BUTT; #else @@ -532,14 +532,6 @@ bool WorkStatus::SetMinIntervalByGroup(int32_t group) } else { SetMinIntervalWhenNotCharging(newGroup); } - auto itMap = DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_.find(newGroup); - if (itMap != DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_.end()) { - minInterval_ = DeviceUsageStats::DeviceUsageStatsGroupMap::groupIntervalMap_[newGroup]; - } else { - WS_HILOGE("query package group interval failed. group:%{public}d, bundleName:%{public}s", - newGroup, bundleName_.c_str()); - minInterval_ = -1; - } #else minInterval_ = MIN_INTERVAL_DEFAULT; #endif -- Gitee From 293d130f6fe95acbd5d150ba7e90d49e0038a07e Mon Sep 17 00:00:00 2001 From: cheng-shichang Date: Sat, 30 Aug 2025 17:53:33 +0800 Subject: [PATCH 4/4] Signed-off-by: csc --- services/native/src/work_status.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/native/src/work_status.cpp b/services/native/src/work_status.cpp index 84c97e3..23ac3c8 100644 --- a/services/native/src/work_status.cpp +++ b/services/native/src/work_status.cpp @@ -478,7 +478,7 @@ bool WorkStatus::IsMailApp() nlohmann::json reply; payload["bundleName"] = bundleName_; int32_t ret = ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent( - ResourceSchedule::ResType::SYNC_RES_TYPE_GET_APP_TYPE, 0, payload, reply); + ResourceSchedule::ResType::SYNC_RES_TYPE_GET_APP_STATUS, 0, payload, reply); if (ret != ERR_OK) { WS_HILOGE("getapp type err:%{public}d", ret); return false; @@ -563,7 +563,7 @@ void WorkStatus::SetMinIntervalWhenCharging(int32_t group) void WorkStatus::SetMinIntervalWhenNotCharging(int32_t group) { - if (IsMailAPP()) { + if (IsMailApp()) { auto itMap = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.find(group); if (itMap != GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.end()) { minInterval_ = GroupConst::MAIL_APP_GROUP_INTERVAL_MAP.at(group); -- Gitee