diff --git a/BUILD.gn b/BUILD.gn index 9505c9170614822f71b5eba1cb59679401a5579f..6fb466c0e046217418bbca7257ae0112ac82b90f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -91,6 +91,7 @@ ohos_shared_library("usagestatservice") { "services/common/src/bundle_active_binary_search.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", "services/common/src/bundle_active_log.cpp", "services/common/src/bundle_active_open_callback.cpp", "services/common/src/bundle_active_service.cpp", @@ -138,6 +139,7 @@ ohos_shared_library("usagestatservice") { "power_manager_native:powermgr_client", "safwk:system_ability_fwk", "samgr_standard:samgr_proxy", + "startup_l2:syspara", "time_native:time_service", "utils_base:utils", ] diff --git a/LICENSE b/LICENSE index 66a27ec5ff940d3a9652d2948746ebac4c9d0188..e454a52586f29b8ce8a6799163eac1f875e9ac01 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,4 @@ + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ diff --git a/services/common/include/bundle_active_constant.h b/services/common/include/bundle_active_constant.h index 721da942d9d5384e6bb4d4aab6a3564c23386298..3f6d447b923692002e31e1f0b1550c45500be7ec 100644 --- a/services/common/include/bundle_active_constant.h +++ b/services/common/include/bundle_active_constant.h @@ -59,8 +59,17 @@ const int BOOT_BASED_DURATION_COLUMN_INDEX = 0; const int SCREEN_ON_DURATION_COLUMN_INDEX = 1; const int DURATION_FLAG_COLUMN_INDEX = 2; const int TWO_SECONDS = 2 * 1000; -const int64_t SIX_DAY_IN_MILLIS_MAX = 6 * 24 * 60 * 60 * 1000; const int64_t THIRTY_MINUTE = 30 * 60 * 1000; +const int64_t SIX_DAY_IN_MILLIS_MAX_DEBUG = (int64_t)6 * 1 * 10 * 60 * 1000; +const int64_t SIX_DAY_IN_MILLIS_MAX = (int64_t)6 * 24 * 60 * 60 * 1000; +const int64_t ONE_DAY_TIME_DEBUG = (int64_t)1 * 10 * 60 * 1000; +const int64_t ONE_DAY_TIME = (int64_t)1 * 24 * 60 * 60 * 1000; +const int64_t ONE_WEEK_TIME_DEBUG = (int64_t)1 * 20 * 60 * 1000; +const int64_t ONE_WEEK_TIME = (int64_t)7 * 24 * 60 * 60 * 1000; +const int64_t ONE_MONTH_TIME_DEBUG = (int64_t)1 * 30 * 60 * 1000; +const int64_t ONE_MONTH_TIME = (int64_t)30 * 24 * 60 * 60 * 1000; +const int64_t ONE_YEAR_TIME_DEBUG = (int64_t)1 * 40 * 60 * 1000; +const int64_t ONE_YEAR_TIME = (int64_t)365 * 24 * 60 * 60 * 1000; const int64_t LAST_TIME_IN_MILLIS_MIN = 0; const int64_t EVENT_TIME_IN_MILLIS_MIN = 0; const int64_t EVENT_BEGIN_TIME_INITIAL_VALUE = -1; diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index bd1601e14853a6b1ac3706ecafe7acd89fc8f846..c5949d2f538adfa5cb194b50d5e30ad653ac3cc0 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -22,6 +22,7 @@ #include "os_account_manager.h" #include "ibundle_active_service.h" +#include "bundle_active_debug_mode.h" #include "bundle_active_stats_update_listener.h" #include "bundle_active_user_service.h" #include "bundle_active_group_controller.h" @@ -114,7 +115,7 @@ public: void ConvertToSystemTimeLocked(BundleActiveEvent& event); // get or create BundleActiveUserService object for specifice user. std::shared_ptr GetUserDataAndInitializeIfNeeded(const int userId, - const int64_t timeStamp); + const int64_t timeStamp, const bool debug); // when received a USER_REMOVED commen event, call it to remove data. void OnUserRemoved(const int userId); void OnUserSwitched(const int userId); @@ -128,7 +129,7 @@ public: int currentUsedUser_; private: - static const int64_t FLUSH_INTERVAL = THIRTY_MINUTE; + int64_t flushInterval_; static const int64_t TIME_CHANGE_THRESHOLD_MILLIS = TWO_SECONDS; const int DEFAULT_USER_ID = -1; std::map visibleActivities_; @@ -143,6 +144,7 @@ private: void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; void RestoreAllData(); + bool debugCore_; }; } } diff --git a/services/common/include/bundle_active_debug_mode.h b/services/common/include/bundle_active_debug_mode.h new file mode 100644 index 0000000000000000000000000000000000000000..94786dd396e9e69cf1f9645f3235e14aa3293ba6 --- /dev/null +++ b/services/common/include/bundle_active_debug_mode.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 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_DEBUG_MODE_H +#define BUNDLE_ACTIVE_DEBUG_MODE_H + +namespace OHOS { +namespace DeviceUsageStats { +extern const bool DEBUG_ON; +} +} +#endif \ No newline at end of file diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index f52a995c3d5046667ff34c34aa071c930d259774..3b436105785d759ff0caeef50a0c695281824ed9 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -56,6 +56,7 @@ public: std::shared_ptr>> GetBundleHistoryData(int userId); void OnPackageUninstalled(const int userId, const std::string& bundleName); + void ChangeToDebug(); private: void CheckDatabaseVersion(); @@ -96,6 +97,7 @@ private: uint32_t currentVersion_; std::mutex databaseMutex_; std::int64_t eventBeginTime_; + bool debugDatabase_; }; } } diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 655043c1f2ab92a3b8f2c04360e17dc5c4022e73..c5467ed22353346f1df5ed9c78e22a72f35bd674 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -53,6 +53,11 @@ BundleActiveCore::BundleActiveCore() systemTimeShot_ = -1; realTimeShot_ = -1; currentUsedUser_ = -1; + if (DEBUG_ON) { + flushInterval_ = TWO_MINUTE; + } else { + flushInterval_ = THIRTY_MINUTE; + } } BundleActiveCore::~BundleActiveCore() @@ -141,7 +146,7 @@ void BundleActiveCore::Init() } while (realTimeShot_ == -1 && systemTimeShot_ == -1); realTimeShot_ = timer->GetBootTimeMs(); systemTimeShot_ = GetSystemTimeMs(); - bundleGroupController_ = std::make_shared(); + bundleGroupController_ = std::make_shared(debugCore_); BUNDLE_ACTIVE_LOGI("system time shot is %{public}lld", systemTimeShot_); } @@ -154,7 +159,7 @@ void BundleActiveCore::InitBundleGroupController() BUNDLE_ACTIVE_LOGE("report handler is null"); return; } - bundleGroupHandler_ = std::make_shared(runner); + bundleGroupHandler_ = std::make_shared(runner, debugCore_); if (bundleGroupHandler_ == nullptr) { return; } @@ -182,13 +187,14 @@ void BundleActiveCore::SetHandler(const std::shared_ptr BundleActiveCore::GetUserDataAndInitializeIfNeeded(const int userId, - const int64_t timeStamp) + const int64_t timeStamp, const bool debug) { BUNDLE_ACTIVE_LOGI("GetUserDataAndInitializeIfNeeded called"); std::map>::iterator it = userStatServices_.find(userId); if (it == userStatServices_.end()) { BUNDLE_ACTIVE_LOGI("first initialize user service"); - std::shared_ptr service = std::make_shared(userId, *this); + std::shared_ptr service = std::make_shared(userId, *this, + debug); service->Init(timeStamp); userStatServices_[userId] = service; if (service == nullptr) { @@ -208,7 +214,7 @@ void BundleActiveCore::OnBundleUninstalled(const int userId, const std::string& if (timeNow == -1) { return; } - auto service = GetUserDataAndInitializeIfNeeded(userId, timeNow); + auto service = GetUserDataAndInitializeIfNeeded(userId, timeNow, debugCore_); if (service == nullptr) { return; } @@ -226,7 +232,7 @@ void BundleActiveCore::OnStatsChanged(const int userId) auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_FLUSH_TO_DISK, handlerobjToPtr); if (handler_.lock()->HasInnerEvent(static_cast(userId)) == false) { BUNDLE_ACTIVE_LOGI("OnStatsChanged send flush to disk event for user %{public}d", userId); - handler_.lock()->SendEvent(event, FLUSH_INTERVAL); + handler_.lock()->SendEvent(event, flushInterval_); } } } @@ -405,6 +411,7 @@ void BundleActiveCore::OnUserSwitched(const int userId) int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) { + BUNDLE_ACTIVE_LOGI("FLUSH interval is %{public}lld, debug is %{public}d", flushInterval_, debugCore_); std::lock_guard lock(mutex_); if (userId == 0 || userId == -1) { return -1; @@ -429,7 +436,7 @@ int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) return -1; } ConvertToSystemTimeLocked(event); - std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow); + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow, debugCore_); if (service == nullptr) { BUNDLE_ACTIVE_LOGE("get user data service failed!"); return -1; @@ -447,12 +454,14 @@ int BundleActiveCore::ReportEventToAllUserId(BundleActiveEvent& event) return -1; } if (userStatServices_.empty()) { - std::shared_ptr service = GetUserDataAndInitializeIfNeeded(DEFAULT_USER_ID, timeNow); + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(DEFAULT_USER_ID, timeNow, + debugCore_); } for (std::map>::iterator it = userStatServices_.begin(); it != userStatServices_.end(); it++) { ConvertToSystemTimeLocked(event); - std::shared_ptr service = GetUserDataAndInitializeIfNeeded(it->first, timeNow); + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(it->first, timeNow, + debugCore_); if (service == nullptr) { BUNDLE_ACTIVE_LOGE("get user data service failed!"); return -1; @@ -481,7 +490,7 @@ std::vector BundleActiveCore::QueryPackageStats(const BUNDLE_ACTIVE_LOGI("QueryPackageStats time span illegal"); return result; } - std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow); + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow, debugCore_); if (service == nullptr) { BUNDLE_ACTIVE_LOGI("QueryPackageStats service is null, failed"); return result; @@ -503,7 +512,7 @@ std::vector BundleActiveCore::QueryEvents(const int userId, c if (beginTime > timeNow || beginTime >= endTime) { return result; } - std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow); + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow, debugCore_); if (service == nullptr) { return result; } diff --git a/services/common/src/bundle_active_debug_mode.cpp b/services/common/src/bundle_active_debug_mode.cpp new file mode 100644 index 0000000000000000000000000000000000000000..19f1fe1ffeae745d9d19949e17206f09547710fb --- /dev/null +++ b/services/common/src/bundle_active_debug_mode.cpp @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 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 +#include + +#include "bundle_active_debug_mode.h" + +using OHOS::system::GetIntParameter; +using OHOS::system::GetParameter; + +namespace OHOS { +namespace DeviceUsageStats { +constexpr int DEBUG_ON_DEFAULT = false; +std::string DEBUG_MODE = "persist.sys.device_usage_debug_on"; +const bool DEBUG_ON = static_cast(GetIntParameter(DEBUG_MODE, DEBUG_ON_DEFAULT)); +} +} \ No newline at end of file diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 4433bcc69d9d1c9875ee86288519ee5e634d265f..c6589c172a82756b20b9f8c1bc211d100c411593 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -53,6 +53,7 @@ BundleActiveUsageDatabase::BundleActiveUsageDatabase() sortedTableArray_ = vector>(SORTED_TABLE_ARRAY_NUMBER); calendar_ = make_shared(); eventBeginTime_ = EVENT_BEGIN_TIME_INITIAL_VALUE; + debugDatabase_ = false; } BundleActiveUsageDatabase::~BundleActiveUsageDatabase() @@ -60,6 +61,11 @@ BundleActiveUsageDatabase::~BundleActiveUsageDatabase() RdbHelper::ClearCache(); } +void BundleActiveUsageDatabase::ChangeToDebug() +{ + calendar_->ChangeToDebug(); + debugDatabase_ = true; +} void BundleActiveUsageDatabase::InitUsageGroupInfo(int32_t databaseType) { lock_guard lock(databaseMutex_); @@ -276,7 +282,12 @@ void BundleActiveUsageDatabase::DeleteExcessiveTableData(unsigned int databaseTy return; } int64_t eventTableTime = ParseStartTime(eventTableName_); - int64_t deleteTimePoint = eventBeginTime_ - SIX_DAY_IN_MILLIS_MAX - eventTableTime; + int64_t deleteTimePoint = 0; + if (debugDatabase_) { + deleteTimePoint = eventBeginTime_ - SIX_DAY_IN_MILLIS_MAX_DEBUG - eventTableTime; + } else { + deleteTimePoint = eventBeginTime_ - SIX_DAY_IN_MILLIS_MAX - eventTableTime; + } if (deleteTimePoint <= 0) { return; } diff --git a/services/packagegroup/include/bundle_active_group_controller.h b/services/packagegroup/include/bundle_active_group_controller.h index 63d3b05d282e140e7a206e8329aa1bab6395b93a..9d0c2717d46b42c504dd159571f077a6ed964f7f 100644 --- a/services/packagegroup/include/bundle_active_group_controller.h +++ b/services/packagegroup/include/bundle_active_group_controller.h @@ -43,26 +43,15 @@ public: using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag; OHOS::AppExecFwk::ApplicationFlag flag = OHOS::AppExecFwk::ApplicationFlag::GET_BASIC_APPLICATION_INFO; bool bundleGroupEnable_ = true; - bool debug_ = false; const int LEVEL_GROUP[4] = { ACTIVE_GROUP_ALIVE, ACTIVE_GROUP_DAILY, ACTIVE_GROUP_FIXED, ACTIVE_GROUP_RARE }; - const int64_t SCREEN_TIME_LEVEL[4] = { - 0, - 0, - debug_ ? TWO_MINUTE : ONE_HOUR, - debug_ ? FOUR_MINUTE : TWO_HOUR - }; - const int64_t BOOT_TIME_LEVEL[4] = { - 0, - debug_ ? TWO_MINUTE : TWELVE_HOUR, - debug_ ? FOUR_MINUTE : TWENTY_FOUR_HOUR, - debug_ ? SIXTEEN_MINUTE : FOURTY_EIGHT_HOUR - }; - BundleActiveGroupController() {}; + std::vector screenTimeLevel_ = {0, 0, 0, 0}; + std::vector bootTimeLevel_ = {0, 0, 0, 0}; + BundleActiveGroupController(const bool debug); ~BundleActiveGroupController() {}; std::shared_ptr bundleUserHistory_; void SetHandlerAndCreateUserHistory(const std::shared_ptr& groupHandler, @@ -91,9 +80,9 @@ private: bool GetBundleMgrProxy(); std::weak_ptr activeGroupHandler_; uint32_t EventToGroupReason(const int eventId); - int64_t timeoutForDirectlyUse_ = debug_ ? THREE_MINUTE : ONE_HOUR; - int64_t timeoutForNotifySeen_ = debug_ ? ONE_MINUTE : TWELVE_HOUR; - int64_t timeoutForSystemInteraction_ = debug_ ? ONE_MINUTE : TEN_MINUTE; + int64_t timeoutForDirectlyUse_; + int64_t timeoutForNotifySeen_; + int64_t timeoutForSystemInteraction_; int64_t timeoutCalculated_ = 0; sptr sptrBundleMgr_; bool calculationTimeOut(const std::shared_ptr& oneBundleHistory, diff --git a/services/packagegroup/include/bundle_active_group_handler.h b/services/packagegroup/include/bundle_active_group_handler.h index 737a9db6ec3f13fc123d17e973eb90c1c321a344..06c801ba47e3ba2a30267670d52c3aead9697bb9 100644 --- a/services/packagegroup/include/bundle_active_group_handler.h +++ b/services/packagegroup/include/bundle_active_group_handler.h @@ -36,7 +36,7 @@ public: class BundleActiveGroupHandler : public AppExecFwk::EventHandler { public: - explicit BundleActiveGroupHandler(const std::shared_ptr &runner); + explicit BundleActiveGroupHandler(const std::shared_ptr &runner, const bool debug); ~BundleActiveGroupHandler() = default; /** * Process the event. Developers should override this method. @@ -48,7 +48,7 @@ public: static const int MSG_CHECK_BUNDLE_STATE = 0; static const int MSG_ONE_TIME_CHECK_BUNDLE_STATE = 1; static const int MSG_CHECK_IDLE_STATE = 2; - static const int CHECK_IDLE_INTERVAL = THREE_HOUR; + int64_t checkIdleInterval_; private: std::shared_ptr bundleActiveGroupController_; diff --git a/services/packagegroup/include/bundle_active_user_history.h b/services/packagegroup/include/bundle_active_user_history.h index 5449c0f6f77133e4b77f4bace27a66f995744788..c86a5456c9ad5feff5456bb67711f2da14a35b31 100644 --- a/services/packagegroup/include/bundle_active_user_history.h +++ b/services/packagegroup/include/bundle_active_user_history.h @@ -52,7 +52,7 @@ public: void SetBundleGroup(const std::string& bundleName, const int userId, const int64_t bootBasedTimeStamp, int newGroup, uint32_t groupReason, const bool& resetTimeout); int GetLevelIndex(const std::string& bundleName, const int userId, const int64_t bootBasedTimeStamp, - const int64_t screenTimeLevel[4], const int64_t bootFromTimeLevel[4]); + const std::vector screenTimeLeve, const std::vector bootFromTimeLevel); void WriteDeviceDuration(); void WriteBundleUsage(const int userId); void printdata(int userId); diff --git a/services/packagegroup/src/bundle_active_group_controller.cpp b/services/packagegroup/src/bundle_active_group_controller.cpp index 0f5ae55ceaa65a81f2c12a0fa22aa136b2693af4..a837554cab5f37564280dc190ba57d725a7fcdd2 100644 --- a/services/packagegroup/src/bundle_active_group_controller.cpp +++ b/services/packagegroup/src/bundle_active_group_controller.cpp @@ -29,6 +29,17 @@ BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject() userId_ = -1; } + +BundleActiveGroupController::BundleActiveGroupController(const bool debug) +{ + timeoutForDirectlyUse_ = debug ? THREE_MINUTE : ONE_HOUR; + timeoutForNotifySeen_ = debug ? ONE_MINUTE : TWELVE_HOUR; + timeoutForSystemInteraction_ = debug ? ONE_MINUTE : TEN_MINUTE; + screenTimeLevel_ = {0, 0, debug ? TWO_MINUTE : ONE_HOUR, debug ? FOUR_MINUTE : TWO_HOUR}; + bootTimeLevel_ = {0, debug ? TWO_MINUTE : TWELVE_HOUR, debug ? FOUR_MINUTE : TWENTY_FOUR_HOUR, + debug ? SIXTEEN_MINUTE : FOURTY_EIGHT_HOUR}; +} + void BundleActiveGroupController::RestoreDurationToDatabase() { std::lock_guard lock(mutex_); @@ -161,8 +172,8 @@ void BundleActiveGroupController::CheckIdleStatsOneTime() int BundleActiveGroupController::GetNewGroup(const std::string& bundleName, const int userId, const int64_t bootBasedTimeStamp) { - int groupIndex = bundleUserHistory_->GetLevelIndex(bundleName, userId, bootBasedTimeStamp, SCREEN_TIME_LEVEL, - BOOT_TIME_LEVEL); + int groupIndex = bundleUserHistory_->GetLevelIndex(bundleName, userId, bootBasedTimeStamp, screenTimeLevel_, + bootTimeLevel_); if (groupIndex < 0) { return -1; } diff --git a/services/packagegroup/src/bundle_active_group_handler.cpp b/services/packagegroup/src/bundle_active_group_handler.cpp index 2b4474873dafc6ecda277f8d80ba055a0bf5706b..53bef3cf31d91d6892d075e11e930f958841ad14 100644 --- a/services/packagegroup/src/bundle_active_group_handler.cpp +++ b/services/packagegroup/src/bundle_active_group_handler.cpp @@ -27,8 +27,13 @@ BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject(const BundleActiv } BundleActiveGroupHandler::BundleActiveGroupHandler - (const std::shared_ptr &runner) : AppExecFwk::EventHandler(runner) + (const std::shared_ptr &runner, const bool debug) : AppExecFwk::EventHandler(runner) { + if (debug) { + checkIdleInterval_ = TWO_MINUTE; + } else { + checkIdleInterval_ = THREE_HOUR; + } } void BundleActiveGroupHandler::Init(const std::shared_ptr& bundleActiveController) @@ -82,7 +87,7 @@ void BundleActiveGroupHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointe auto handlerEvent = AppExecFwk::InnerEvent::Get(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, handlerobjToPtr); bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_); - SendEvent(handlerEvent, CHECK_IDLE_INTERVAL); + SendEvent(handlerEvent, checkIdleInterval_); } break; } diff --git a/services/packagegroup/src/bundle_active_user_history.cpp b/services/packagegroup/src/bundle_active_user_history.cpp index 3d0f0f90f67710bf9a3abdd4c8cbd0513a0aee03..6479e6d76e53a3799b415d8ab0a3112c7bfa82d3 100644 --- a/services/packagegroup/src/bundle_active_user_history.cpp +++ b/services/packagegroup/src/bundle_active_user_history.cpp @@ -63,7 +63,8 @@ BundleActiveUserHistory::BundleActiveUserHistory(const int64_t bootBasedTimeStam } int BundleActiveUserHistory::GetLevelIndex(const string& bundleName, const int userId, - const int64_t bootBasedTimeStamp, const int64_t screenTimeLevel[4], const int64_t bootFromTimeLevel[4]) + const int64_t bootBasedTimeStamp, const std::vector screenTimeLevel, + const std::vector bootFromTimeLevel) { auto oneUserHistory = GetUserHistory(userId, false); if (oneUserHistory == nullptr) { diff --git a/services/packageusage/include/bundle_active_calendar.h b/services/packageusage/include/bundle_active_calendar.h index 745d56ecbb1dce48b28479f919ce15d09ad613a9..b8c166cd1b19258710df08028c910bb8567cfb51 100644 --- a/services/packageusage/include/bundle_active_calendar.h +++ b/services/packageusage/include/bundle_active_calendar.h @@ -18,20 +18,27 @@ #include +#include "bundle_active_constant.h" + namespace OHOS { namespace DeviceUsageStats { class BundleActiveCalendar { public: static const int64_t ONE_SECOND_MILLISECONDS = 1000; - static const int64_t DAY_MILLISECONDS = (int64_t)1 * 24 * 60 * 60 * 1000; - static const int64_t WEEK_MILLISECONDS = (int64_t)7 * 24 * 60 * 60 * 1000; - static const int64_t MONTH_MILLISECONDS = (int64_t)30 * 24 * 60 * 60 * 1000; - static const int64_t YEAR_MILLISECONDS = (int64_t)365 * 24 * 60 * 60 * 1000; + int64_t dayMilliseconds_; + int64_t weekMilliseconds_; + int64_t monthMilliseconds_; + int64_t yearMilliseconds_; BundleActiveCalendar(const int64_t timeStamp); BundleActiveCalendar() { time_ = 0; + dayMilliseconds_ = ONE_DAY_TIME; + weekMilliseconds_ = ONE_WEEK_TIME; + monthMilliseconds_ = ONE_MONTH_TIME; + yearMilliseconds_ = ONE_YEAR_TIME; } + void ChangeToDebug(); void TruncateToDay(); void TruncateToWeek(); void TruncateToMonth(); diff --git a/services/packageusage/include/bundle_active_user_service.h b/services/packageusage/include/bundle_active_user_service.h index 2bce5f34f9952224ef37e5c54a53702fbecffbe9..8bf0fed665ed340ccd6114ec5652e4932558c2d4 100644 --- a/services/packageusage/include/bundle_active_user_service.h +++ b/services/packageusage/include/bundle_active_user_service.h @@ -25,6 +25,7 @@ #include "bundle_active_calendar.h" #include "bundle_active_stats_combiner.h" #include "bundle_active_usage_database.h" +#include "bundle_active_constant.h" namespace OHOS { namespace DeviceUsageStats { @@ -33,7 +34,7 @@ class BundleActiveCore; class BundleActiveUserService { public: BundleActiveUserService() = delete; - BundleActiveUserService(const int userId, BundleActiveCore& listener):listener_(listener) + BundleActiveUserService(const int userId, BundleActiveCore& listener, const bool debug):listener_(listener) { for (int i = 0; i < BundleActivePeriodStats::PERIOD_COUNT; i++) { currentStats_.push_back(nullptr); @@ -41,6 +42,15 @@ public: userId_ = userId; dailyExpiryDate_.SetMilliseconds(0); statsChanged_ = false; + if (debug) { + dailyExpiryDate_.ChangeToDebug(); + database_.ChangeToDebug(); + debugUserService_ = true; + periodLength_ = {ONE_DAY_TIME_DEBUG, ONE_WEEK_TIME_DEBUG, ONE_MONTH_TIME_DEBUG, ONE_YEAR_TIME_DEBUG}; + } else { + debugUserService_ = false; + periodLength_ = {ONE_DAY_TIME, ONE_WEEK_TIME, ONE_MONTH_TIME, ONE_YEAR_TIME}; + } } void Init(const int64_t timeStamp); ~BundleActiveUserService() {}; @@ -64,11 +74,10 @@ private: BundleActiveUsageDatabase database_; std::vector> currentStats_; bool statsChanged_; + bool debugUserService_; std::string lastBackgroundBundle_; BundleActiveCore& listener_; - inline static const std::vector PERIOD_LENGTH = {BundleActiveCalendar::DAY_MILLISECONDS, - BundleActiveCalendar::WEEK_MILLISECONDS, BundleActiveCalendar::MONTH_MILLISECONDS, - BundleActiveCalendar::YEAR_MILLISECONDS}; + std::vector periodLength_ = {0, 0, 0, 0}; void NotifyStatsChanged(); void NotifyNewUpdate(); void PrintInMemPackageStats(const int idx); diff --git a/services/packageusage/src/bundle_active_calendar.cpp b/services/packageusage/src/bundle_active_calendar.cpp index 1806fac73256eb392d0e423b10cebf2d451e98e3..fa79effad46744ff82bf5daf93d37543f09f40c7 100644 --- a/services/packageusage/src/bundle_active_calendar.cpp +++ b/services/packageusage/src/bundle_active_calendar.cpp @@ -25,42 +25,42 @@ BundleActiveCalendar::BundleActiveCalendar(const int64_t timeStamp) void BundleActiveCalendar::TruncateToDay() { - time_ -= time_ % DAY_MILLISECONDS; + time_ -= time_ % dayMilliseconds_; } void BundleActiveCalendar::TruncateToWeek() { - time_ -= time_ % WEEK_MILLISECONDS; + time_ -= time_ % weekMilliseconds_; } void BundleActiveCalendar::TruncateToMonth() { - time_ -= time_ % MONTH_MILLISECONDS; + time_ -= time_ % monthMilliseconds_; } void BundleActiveCalendar::TruncateToYear() { - time_ -= time_ % YEAR_MILLISECONDS; + time_ -= time_ % yearMilliseconds_; } void BundleActiveCalendar::IncreaseDays(const int val) { - time_ += val * DAY_MILLISECONDS; + time_ += val * dayMilliseconds_; } void BundleActiveCalendar::IncreaseWeeks(const int val) { - time_ += val* WEEK_MILLISECONDS; + time_ += val* weekMilliseconds_; } void BundleActiveCalendar::IncreaseMonths(const int val) { - time_ += val * MONTH_MILLISECONDS; + time_ += val * monthMilliseconds_; } void BundleActiveCalendar::IncreaseYears(const int val) { - time_ += val * YEAR_MILLISECONDS; + time_ += val * yearMilliseconds_; } void BundleActiveCalendar::SetMilliseconds(const int64_t timeStamp) @@ -73,6 +73,14 @@ int64_t BundleActiveCalendar::GetMilliseconds() return time_; } +void BundleActiveCalendar::ChangeToDebug() +{ + dayMilliseconds_ = ONE_DAY_TIME_DEBUG; + weekMilliseconds_ = ONE_WEEK_TIME_DEBUG; + monthMilliseconds_ = ONE_MONTH_TIME_DEBUG; + yearMilliseconds_ = ONE_YEAR_TIME_DEBUG; +} + void BundleActiveCalendar::TruncateTo(int intervalType) { switch (intervalType) { diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index 931d54f32992f3fb2f2507a321b15b1a25c12251..fa0cad7d88455c5bc9896b6f3271aec913fa8446 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -182,9 +182,12 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo { BUNDLE_ACTIVE_LOGI("LoadActiveStats called"); BundleActiveCalendar tmpCalendar(0); + if (debugUserService_ == true) { + tmpCalendar.ChangeToDebug(); + } tmpCalendar.SetMilliseconds(timeStamp); tmpCalendar.TruncateTo(BundleActivePeriodStats::PERIOD_DAILY); - for (uint32_t intervalType = 0; intervalType < PERIOD_LENGTH.size(); intervalType++) { + for (uint32_t intervalType = 0; intervalType < periodLength_.size(); intervalType++) { if (!force && currentStats_[intervalType] != nullptr && currentStats_[intervalType]->beginTime_ == tmpCalendar.GetMilliseconds()) { continue; @@ -197,8 +200,8 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo // 如果当前时间在stats的统计时间范围内,则可以从数据库加载数据 BUNDLE_ACTIVE_LOGI("interval type is %{public}d, database stat BEGIN time is %{public}lld, " "timestamp is %{public}lld, expect end is %{public}lld", - intervalType, stats->beginTime_, timeStamp, stats->beginTime_ + PERIOD_LENGTH[intervalType]); - if (timeStamp > stats->beginTime_ && timeStamp < stats->beginTime_ + PERIOD_LENGTH[intervalType]) { + intervalType, stats->beginTime_, timeStamp, stats->beginTime_ + periodLength_[intervalType]); + if (timeStamp > stats->beginTime_ && timeStamp < stats->beginTime_ + periodLength_[intervalType]) { currentStats_[intervalType] = stats; } } @@ -301,7 +304,7 @@ std::vector BundleActiveUserService::QueryPackageStats return result; } if (currentStats->endTime_ == 0) { - if (beginTime > currentStats->beginTime_ + PERIOD_LENGTH[intervalType]) { + if (beginTime > currentStats->beginTime_ + periodLength_[intervalType]) { return result; } else { result = database_.QueryDatabaseUsageStats(intervalType, beginTime, endTime, userId);