From 0a865ca81f5c8495ad368ed53086c66be6aedeec Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 14:54:11 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=91=A8=E6=9C=9F=E6=80=A7=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fengyang --- BUILD.gn | 12 ++++ bundle.json | 2 + services/common/include/bundle_active_core.h | 5 +- services/common/src/bundle_active_core.cpp | 7 +- services/common/src/bundle_active_service.cpp | 1 + .../device_usage_statistics_service_test.cpp | 66 +++++++++++++++++++ 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index cec8cbb..6a46545 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -169,6 +169,7 @@ ohos_shared_library("usagestatservice") { "services/common/src/bundle_active_app_state_obsever.cpp", "services/common/src/bundle_active_binary_search.cpp", "services/common/src/bundle_active_bundle_mgr_helper.cpp", + "services/common/src/bundle_active_config_reader.cpp", "services/common/src/bundle_active_continuous_task_observer.cpp", "services/common/src/bundle_active_core.cpp", "services/common/src/bundle_active_debug_mode.cpp", @@ -206,6 +207,7 @@ ohos_shared_library("usagestatservice") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hicollie:libhicollie", @@ -218,6 +220,10 @@ ohos_shared_library("usagestatservice") { "time_service:time_client", ] + public_external_deps = [ + "jsoncpp:jsoncpp", + ] + if (os_account_part_enabled) { cflags_cc += [ "-DOS_ACCOUNT_PART_ENABLED" ] external_deps += [ "os_account:os_account_innerkits" ] @@ -251,6 +257,7 @@ ohos_static_library("usagestatservice_static") { "services/common/src/bundle_active_app_state_obsever.cpp", "services/common/src/bundle_active_binary_search.cpp", "services/common/src/bundle_active_bundle_mgr_helper.cpp", + "services/common/src/bundle_active_config_reader.cpp", "services/common/src/bundle_active_continuous_task_observer.cpp", "services/common/src/bundle_active_core.cpp", "services/common/src/bundle_active_debug_mode.cpp", @@ -288,6 +295,7 @@ ohos_static_library("usagestatservice_static") { "bundle_framework:appexecfwk_core", "c_utils:utils", "common_event_service:cesfwk_innerkits", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hicollie:libhicollie", @@ -300,6 +308,10 @@ ohos_static_library("usagestatservice_static") { "time_service:time_client", ] + public_external_deps = [ + "jsoncpp:jsoncpp", + ] + if (os_account_part_enabled) { cflags_cc += [ "-DOS_ACCOUNT_PART_ENABLED" ] external_deps += [ "os_account:os_account_innerkits" ] diff --git a/bundle.json b/bundle.json index a655197..5f7bf35 100644 --- a/bundle.json +++ b/bundle.json @@ -27,6 +27,7 @@ "relational_store", "safwk", "common_event_service", + "config_policy", "os_account", "ipc", "access_token", @@ -44,6 +45,7 @@ "selinux_adapter", "time_service", "init", + "jsoncpp", "ffrt" ], "third_party": [] 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_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) diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 5cc1af2..d187bea 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -329,6 +329,7 @@ ErrCode BundleActiveService::IsBundleUsePeriod(bool& IsUsePeriod, const std::str } } IsUsePeriod = bundleActiveCore_->IsBundleUsePeriod(bundleName, userId); + BUNDLE_ACTIVE_LOGI("IsBundleUsePeriod %{public}d", IsUsePeriod); return ERR_OK; } diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 3edd8b7..1ca72d2 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -28,6 +28,7 @@ #include "bundle_active_user_history.h" #include "bundle_active_group_controller.h" #include "bundle_active_log.h" +#include "bundle_active_config_reader.h" using namespace testing::ext; @@ -1437,5 +1438,70 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Dele userService->DeleteUninstalledBundleStats("test", 100, appIndex); } +/* + * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_001 + * @tc.desc: ConfigReader + * @tc.type: FUNC + * @tc.require: issuesIBCE1G + */ +HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_001, + Function | MediumTest | Level0) +{ + auto bundleActiveConfigReader = std::make_shared(); + EXPECT_NE(bundleActiveConfigReader, nullptr); + bundleActiveConfigReader->LoadConfig(); + EXPECT_NE(bundleActiveConfigReader->GetApplicationUsePeriodicallyConfig().minUseTimes, 0); +} + +/* + * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_002 + * @tc.desc: ConfigReader + * @tc.type: FUNC + * @tc.require: issuesIBCE1G + */ +HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_002, + Function | MediumTest | Level0) +{ + auto bundleActiveConfigReader = std::make_shared(); + EXPECT_NE(bundleActiveConfigReader, nullptr); + const char *path = "test"; + bundleActiveConfigReader->LoadApplicationUsePeriodically(path); + EXPECT_EQ(bundleActiveConfigReader->GetApplicationUsePeriodicallyConfig().minUseTimes, 0); +} + +/* + * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_003 + * @tc.desc: ConfigReader + * @tc.type: FUNC + * @tc.require: issuesIBCE1G + */ +HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_003, + Function | MediumTest | Level0) +{ + auto bundleActiveConfigReader = std::make_shared(); + EXPECT_NE(bundleActiveConfigReader, nullptr); + const char *path = "test"; + Json::Value root; + bool result = bundleActiveConfigReader->GetJsonFromFile(path, root); + EXPECT_EQ(result, false); +} + +/* + * @tc.name: DeviceUsageStatisticsServiceTest_ConfigReader_004 + * @tc.desc: ConfigReader + * @tc.type: FUNC + * @tc.require: issuesIBCE1G + */ +HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_ConfigReader_004, + Function | MediumTest | Level0) +{ + auto bundleActiveConfigReader = std::make_shared(); + EXPECT_NE(bundleActiveConfigReader, nullptr); + std::string partialPath = "test"; + std::string fullPath = ""; + bool result = bundleActiveConfigReader->ConvertFullPath(partialPath, fullPath); + EXPECT_EQ(result, true); +} + } // namespace DeviceUsageStats } // namespace OHOS \ No newline at end of file -- Gitee From 74ca3a9ae979ba55e5807ad806e5c49d238fb7dd Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 15:38:20 +0800 Subject: [PATCH 2/6] push Signed-off-by: fengyang --- .../include/bundle_active_config_reader.h | 41 +++++ .../src/bundle_active_config_reader.cpp | 140 ++++++++++++++++++ 2 files changed, 181 insertions(+) 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..5ed759c --- /dev/null +++ b/services/common/include/bundle_active_config_reader.h @@ -0,0 +1,41 @@ +/* + * 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 +#include +#include "json/json.h" +namespace OHOS { +namespace DeviceUsageStats { +struct AppUsePeriodicallyConfig { + int32_t minUseTimes; + int32_t maxUseTimes; + int32_t minUseDays; +}; +class BundleActiveConfigReader { +public: + void LoadConfig(); + AppUsePeriodicallyConfig GetApplicationUsePeriodicallyConfig(); +private: + void LoadApplicationUsePeriodically(const char* path); + bool GetJsonFromFile(const char* filePath, Json::Value& root); + bool ConvertFullPath(const std::string& partialPath, std::string& fullPath); + AppUsePeriodicallyConfig appUsePeriodicallyConfig_; +}; + +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_CONFIG_READER_H + 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..52d9526 --- /dev/null +++ b/services/common/src/bundle_active_config_reader.cpp @@ -0,0 +1,140 @@ +/* + * 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" +#include "bundle_active_log.h" +#include +#include + +using namespace std; +namespace OHOS { +namespace DeviceUsageStats { +const char* CONFIG_PATH = "etc/device_usage_statistics/device_usage_statistics_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; +const int32_t MAX_BUFFER = 2048; + + +void BundleActiveConfigReader::LoadConfig() +{ + appUsePeriodicallyConfig_ = { DEFAULT_MIN_USE_TIMES, DEFAULT_MAX_USE_TIMES, DEFAULT_MIN_USE_DAYS}; + auto cfgFiles = GetCfgFiles(CONFIG_PATH); + if (!cfgFiles) { + 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; + } + if (appUsePeriodicallyRoot[MIN_USE_TIMES].empty() || !appUsePeriodicallyRoot[MIN_USE_TIMES].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MinUseTimes key"); + return; + } + int32_t minUseTimes = appUsePeriodicallyRoot[MIN_USE_TIMES].asInt(); + if (appUsePeriodicallyRoot[MAX_USE_TIMES].empty() || !appUsePeriodicallyRoot[MAX_USE_TIMES].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MaxUseTimes key"); + return; + } + int32_t maxUseTimes = appUsePeriodicallyRoot[MAX_USE_TIMES].asInt(); + if (appUsePeriodicallyRoot[MIN_USE_DAYS].empty() || !appUsePeriodicallyRoot[MIN_USE_DAYS].isInt()) { + BUNDLE_ACTIVE_LOGE("not have MinUseDays key"); + return; + } + int32_t minUseDays = appUsePeriodicallyRoot[MIN_USE_DAYS].asInt(); + appUsePeriodicallyConfig_ = { minUseTimes, maxUseTimes, minUseDays}; + BUNDLE_ACTIVE_LOGI("minUseTimes:%{public}d, maxUseTimes:%{public}d, minUseDays:%{public}d", + minUseTimes, maxUseTimes, minUseDays); +}; + +bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, Json::Value &root) +{ + ifstream fin; + std::string realPath; + if (!BundleActiveConfigReader::ConvertFullPath(filePath, realPath)) { + BUNDLE_ACTIVE_LOGE("Get real path failed %{private}s", filePath); + return false; + } + BUNDLE_ACTIVE_LOGD("Read from %{private}s", realPath.c_str()); + fin.open(realPath, ios::in); + if (!fin.is_open()) { + BUNDLE_ACTIVE_LOGE("cannot open file %{private}s", realPath.c_str()); + return false; + } + char buffer[MAX_BUFFER]; + ostringstream os; + while (fin.getline(buffer, MAX_BUFFER)) { + os << buffer; + } + string data = os.str(); + JSONCPP_STRING errs; + Json::CharReaderBuilder readerBuilder; + const unique_ptr jsonReader(readerBuilder.newCharReader()); + bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &root, &errs); + fin.close(); + if (!res || !errs.empty()) { + BUNDLE_ACTIVE_LOGE("parse %{private}s json error", realPath.c_str()); + return false; + } + return true; +} + +bool BundleActiveConfigReader::ConvertFullPath(const std::string& partialPath, std::string& fullPath) +{ + if (partialPath.empty() || partialPath.length() >= PATH_MAX) { + return false; + } + char tmpPath[PATH_MAX] = {0}; + if (realpath(partialPath.c_str(), tmpPath) == nullptr) { + return false; + } + fullPath = tmpPath; + return true; +} + +AppUsePeriodicallyConfig BundleActiveConfigReader::GetApplicationUsePeriodicallyConfig() +{ + return appUsePeriodicallyConfig_; +}; + +} // namespace DeviceUsageStats +} // namespace OHOS + -- Gitee From 07156998459cb33e20366a882ee09de4928c7466 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 16:00:13 +0800 Subject: [PATCH 3/6] push Signed-off-by: fengyang --- test/unittest/device_usage_statistics_service_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unittest/device_usage_statistics_service_test.cpp b/test/unittest/device_usage_statistics_service_test.cpp index 1ca72d2..134203b 100644 --- a/test/unittest/device_usage_statistics_service_test.cpp +++ b/test/unittest/device_usage_statistics_service_test.cpp @@ -1500,7 +1500,7 @@ HWTEST_F(DeviceUsageStatisticsServiceTest, DeviceUsageStatisticsServiceTest_Conf std::string partialPath = "test"; std::string fullPath = ""; bool result = bundleActiveConfigReader->ConvertFullPath(partialPath, fullPath); - EXPECT_EQ(result, true); + EXPECT_EQ(result, false); } } // namespace DeviceUsageStats -- Gitee From 4ea17aab2b79b074b3b7156e9d9016f426c743b0 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 16:11:28 +0800 Subject: [PATCH 4/6] push Signed-off-by: fengyang --- services/common/include/bundle_active_config_reader.h | 1 + services/common/src/bundle_active_config_reader.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/services/common/include/bundle_active_config_reader.h b/services/common/include/bundle_active_config_reader.h index 5ed759c..1400e4e 100644 --- a/services/common/include/bundle_active_config_reader.h +++ b/services/common/include/bundle_active_config_reader.h @@ -17,6 +17,7 @@ #define BUNDLE_ACTIVE_CONFIG_READER_H #include #include "json/json.h" + namespace OHOS { namespace DeviceUsageStats { struct AppUsePeriodicallyConfig { diff --git a/services/common/src/bundle_active_config_reader.cpp b/services/common/src/bundle_active_config_reader.cpp index 52d9526..6957bb9 100644 --- a/services/common/src/bundle_active_config_reader.cpp +++ b/services/common/src/bundle_active_config_reader.cpp @@ -44,6 +44,9 @@ void BundleActiveConfigReader::LoadConfig() for (const auto& filePath : cfgFiles->paths) { LoadApplicationUsePeriodically(filePath); } + BUNDLE_ACTIVE_LOGI("appUsePeriodicallyConfig minUseTimes:%{public}d, maxUseTimes:%{public}d," + "minUseDays:%{public}d", appUsePeriodicallyConfig_.minUseTimes, + appUsePeriodicallyConfig_.maxUseTimes, appUsePeriodicallyConfig_.minUseDays); }; void BundleActiveConfigReader::LoadApplicationUsePeriodically(const char *filePath) @@ -81,8 +84,6 @@ void BundleActiveConfigReader::LoadApplicationUsePeriodically(const char *filePa } int32_t minUseDays = appUsePeriodicallyRoot[MIN_USE_DAYS].asInt(); appUsePeriodicallyConfig_ = { minUseTimes, maxUseTimes, minUseDays}; - BUNDLE_ACTIVE_LOGI("minUseTimes:%{public}d, maxUseTimes:%{public}d, minUseDays:%{public}d", - minUseTimes, maxUseTimes, minUseDays); }; bool BundleActiveConfigReader::GetJsonFromFile(const char *filePath, Json::Value &root) -- Gitee From 8861d636df7a920f6b2031bf60a822ee0fc362fc Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 16:17:55 +0800 Subject: [PATCH 5/6] push Signed-off-by: fengyang --- services/common/src/bundle_active_config_reader.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/common/src/bundle_active_config_reader.cpp b/services/common/src/bundle_active_config_reader.cpp index 6957bb9..a7df1d9 100644 --- a/services/common/src/bundle_active_config_reader.cpp +++ b/services/common/src/bundle_active_config_reader.cpp @@ -14,7 +14,7 @@ */ #include "bundle_active_config_reader.h" -#include "config_policy_utils.h" +#include "config_policy_utils.h" #include "bundle_active_log.h" #include #include -- Gitee From c7b222d81f752954345edea9ca2bca6d12002eb1 Mon Sep 17 00:00:00 2001 From: fengyang Date: Thu, 19 Dec 2024 16:41:27 +0800 Subject: [PATCH 6/6] push Signed-off-by: fengyang --- BUILD.gn | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 6a46545..b6c53b4 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -220,9 +220,7 @@ ohos_shared_library("usagestatservice") { "time_service:time_client", ] - public_external_deps = [ - "jsoncpp:jsoncpp", - ] + public_external_deps = [ "jsoncpp:jsoncpp" ] if (os_account_part_enabled) { cflags_cc += [ "-DOS_ACCOUNT_PART_ENABLED" ] @@ -308,9 +306,7 @@ ohos_static_library("usagestatservice_static") { "time_service:time_client", ] - public_external_deps = [ - "jsoncpp:jsoncpp", - ] + public_external_deps = [ "jsoncpp:jsoncpp" ] if (os_account_part_enabled) { cflags_cc += [ "-DOS_ACCOUNT_PART_ENABLED" ] -- Gitee