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 0000000000000000000000000000000000000000..27eade73ebb42a55e47210b99601c9031a38195f --- /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 6c1911bc2eb241f77e7ecc7d86f934a66cacebd9..ca02dab5e53e268d03a5422a08e8222eebbe10ca 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 0000000000000000000000000000000000000000..1986c5855612655cc1d552000234088fa2d36b24 --- /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 8a17258cedb3bfc159341843f25c63d964faad61..6ec93c247545a5e49eda5e89e2202615a1a79d23 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)