From c5907b919457cf557c8bea77b2d61ba7730e350f Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 09:35:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=A8=E6=9C=9F=E6=80=A7?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- .../include/bundle_active_config_reader.h | 38 ++++++++ services/common/include/bundle_active_core.h | 5 +- .../src/bundle_active_config_reader.cpp | 86 +++++++++++++++++++ services/common/src/bundle_active_core.cpp | 7 +- 4 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 services/common/include/bundle_active_config_reader.h create mode 100644 services/common/src/bundle_active_config_reader.cpp diff --git a/services/common/include/bundle_active_config_reader.h b/services/common/include/bundle_active_config_reader.h new file mode 100644 index 0000000..27eade7 --- /dev/null +++ b/services/common/include/bundle_active_config_reader.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BUNDLE_ACTIVE_CONFIG_READER_H +#define BUNDLE_ACTIVE_CONFIG_READER_H + +namespace OHOS { +namespace DeviceUsageStats { + +class BundleActiveConfigReader { +public: + void LoadConfig(); + AppUsePeriodicallyConfig GetApplicationUsePeriodicallyConfig(); +private: + void LoadApplicationUsePeriodically(const char *path); + AppUsePeriodicallyConfig appUsePeriodicallyConfig_; +}; +struct AppUsePeriodicallyConfig { + int32_t minUseTimes; + int32_t maxUseTimes; + int32_t minUseDays; +} +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_CONFIG_READER_H + diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index 6c1911b..ca02dab 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -42,6 +42,7 @@ #include "bundle_active_group_handler.h" #include "bundle_active_common_event_subscriber.h" #include "bundle_active_constant.h" +#include "bundle_active_config_reader.h" namespace OHOS { namespace DeviceUsageStats { @@ -282,9 +283,7 @@ private: bool debugCore_; ffrt::mutex bundleUninstalledMutex_; std::set bundleUninstalledSet_; - uint32_t minUseDays_ = 3; - uint32_t minUseTimes_ = 1; - uint32_t maxUseTimes_ = 10; + std::shared_ptr bundleActiveConfigReader_; }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_config_reader.cpp b/services/common/src/bundle_active_config_reader.cpp new file mode 100644 index 0000000..1986c58 --- /dev/null +++ b/services/common/src/bundle_active_config_reader.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "bundle_active_config_reader.h" +#include "config_policy_utils.h" + +namespace OHOS { +namespace DeviceUsageStats { +const char* CONFIG_PATH = "etc/backgroundtask/config.json"; +const std::string APPLICATION_USE_PERIODICALLY_KEY = "application_use_periodically"; +const std::string MIN_USE_TIMES = "MinUseTimes"; +const std::string MAX_USE_TIMES = "MaxUseTimes"; +const std::string MIN_USE_DAYS = "MinUseDays"; +const int32_t DEFAULT_MIN_USE_TIMES = 1; +const int32_t DEFAULT_MAX_USE_TIMES = 10; +const int32_t DEFAULT_MIN_USE_DAYS = 3; +BundleActiveConfigReader::appUsePeriodicallyConfig_ = + { DEFAULT_MIN_USE_TIMES, DEFAULT_MAX_USE_TIMES, DEFAULT_MIN_USE_DAYS}; + +void BundleActiveConfigReader::LoadConfig() +{ + auto cfgFiles = GetCfgFiles(CONFIG_PATH); + if (!files) { + BUNDLE_ACTIVE_LOGE("GetCfgFiles failed"); + return; + } + for (const auto& filePath : cfgFiles->paths) { + LoadApplicationUsePeriodically(filePath); + } +}; + +void BundleActiveConfigReader::LoadApplicationUsePeriodically(const char *filePath) +{ + if (!filePath) { + return; + } + Json::Value root; + if (!GetJsonFromFile(filePath, root) || root.empty()) { + BUNDLE_ACTIVE_LOGE("file is empty %{private}s", filePath); + return; + } + if (!root.isMember(APPLICATION_USE_PERIODICALLY_KEY)) { + BUNDLE_ACTIVE_LOGE("not have application_use_periodically key"); + return; + } + Json::Value appUsePeriodicallyRoot = root[APPLICATION_USE_PERIODICALLY_KEY]; + if (appUsePeriodicallyRoot.empty() || !appUsePeriodicallyRoot.isObject()) { + BUNDLE_ACTIVE_LOGE("application_use_periodically content is empty"); + return; + } + AppUsePeriodicallyConfig AppUsePeriodicallyConfig; + if (!appUsePeriodicallyRoot.HasMember(MIN_USE_TIMES) || !appUsePeriodicallyRoot[MIN_USE_TIMES].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MinUseTimes key"); + return; + } + int32_t minUseTimes = appUsePeriodicallyRoot[MIN_USE_TIMES].GetInt(); + if (!appUsePeriodicallyRoot.HasMember(MAX_USE_TIMES) || !appUsePeriodicallyRoot[MAX_USE_TIMES].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MaxUseTimes key"); + return; + } + int32_t maxUseTimes = appUsePeriodicallyRoot[MAX_USE_TIMES].GetInt(); + if (!appUsePeriodicallyRoot.HasMember(MIN_USE_DAYS) || !appUsePeriodicallyRoot[MIN_USE_DAYS].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MinUseDays key"); + return; + } + int32_t minUseDays = appUsePeriodicallyRoot[MIN_USE_DAYS].GetInt(); + appUsePeriodicallyConfig_ = { minUseTimes, maxUseTimes, minUseDays}; + BUNDLE_ACTIVE_LOGI("load application_use_periodically success, minUseTimes:%{public}d, maxUseTimes:%{public}d," + + "minUseDays:%{public}d", minUseTimes, maxUseTimes, minUseDays); +}; + +} // namespace DeviceUsageStats +} // namespace OHOS + diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 8a17258..6ec93c2 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -256,6 +256,8 @@ void BundleActiveCore::Init() systemTimeShot_ = GetSystemTimeMs(); bundleGroupController_ = std::make_shared(debugCore_); BUNDLE_ACTIVE_LOGD("system time shot is %{public}lld", (long long)systemTimeShot_); + bundleActiveConfigReader_ = std::make_shared(); + bundleActiveConfigReader_->LoadConfig(); } void BundleActiveCore::InitBundleGroupController() @@ -790,11 +792,12 @@ bool BundleActiveCore::IsBundleUsePeriod(const std::string& bundleName, const in currentSystemTime, bundleName); int32_t useDayPeriod = 0; for (auto& item : packageStats) { - if (item.startCount_ >= minUseTimes_ && item.startCount_ <= maxUseTimes_) { + if (item.startCount_ >= bundleActiveConfigReader_->GetApplicationUsePeriodicallyConfig().minUseTimes + && item.startCount_ <= bundleActiveConfigReader_->GetApplicationUsePeriodicallyConfig().maxUseTimes) { useDayPeriod ++; } } - return useDayPeriod >= minUseDays_; + return useDayPeriod >= bundleActiveConfigReader_->GetApplicationUsePeriodicallyConfig().minUseDays; } void BundleActiveCore::GetAllActiveUser(std::vector& activatedOsAccountIds) -- Gitee