diff --git a/BUILD.gn b/BUILD.gn index 87a5834a1f1f2ba5edec93411453178dabd2bb48..d2b3f4c05e4957121f08435fea38bdec026da3ef 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -110,6 +110,8 @@ ohos_shared_library("usagestatservice") { "services/packageusage/src/bundle_active_event_list.cpp", "services/packageusage/src/bundle_active_event_stats.cpp", "services/packageusage/src/bundle_active_event_tracker.cpp", + "services/packageusage/src/bundle_active_form_record.cpp", + "services/packageusage/src/bundle_active_module_record.cpp", "services/packageusage/src/bundle_active_package_stats.cpp", "services/packageusage/src/bundle_active_period_stats.cpp", "services/packageusage/src/bundle_active_report_handler.cpp", diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index 5bf1e97b65e236324beb3ab15a5f2b01c169d18a..46a7b6143134e1d6dd97dbf440639880b7a5bf4b 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -19,13 +19,16 @@ #include "ibundle_active_service.h" #include "bundle_active_package_stats.h" #include "bundle_active_event.h" +#include "bundle_active_package_stats.h" +#include "bundle_active_module_record.h" namespace OHOS { namespace DeviceUsageStats { class BundleActiveClient { public: - int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId); + int ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId); bool IsBundleIdle(const std::string& bundleName); std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode); @@ -35,6 +38,7 @@ public: const int64_t endTime); std::vector QueryCurrentEvents(const int64_t beginTime, const int64_t endTime); int QueryPackageGroup(); + int QueryFormStatistics(int32_t maxNum, std::vector& results); static BundleActiveClient& GetInstance(); BundleActiveClient() {} ~BundleActiveClient() {} diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index e5fe92ee9c2be47825f8fee03fec6da6f531ec19..f1b53978414dbeffafe879bfee4571bc3c44f431 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -18,13 +18,17 @@ #include "ibundle_active_service.h" #include "bundle_active_event.h" +#include "bundle_active_package_stats.h" +#include "bundle_active_package_stats.h" +#include "bundle_active_module_record.h" namespace OHOS { namespace DeviceUsageStats { class BundleActiveProxy : public IRemoteProxy { public: - int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) override; + int ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) override; bool IsBundleIdle(const std::string& bundleName) override; std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) override; @@ -35,6 +39,7 @@ public: const int64_t endTime) override; std::vector QueryCurrentEvents(const int64_t beginTime, const int64_t endTime) override; int QueryPackageGroup() override; + int QueryFormStatistics(int32_t maxNum, std::vector& results) override; explicit BundleActiveProxy(const sptr& impl) : IRemoteProxy(impl) {} virtual ~BundleActiveProxy() {} diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index 4ac0b1bc8062f1715d0fd8b2a0c05b45bcd5cb0a..7a8562b1d78eff440b915fc1c03ee590f51deb34 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -14,7 +14,6 @@ */ #include "bundle_active_client.h" -#include "bundle_active_package_stats.h" namespace OHOS { namespace DeviceUsageStats { @@ -46,14 +45,16 @@ bool BundleActiveClient::GetBundleActiveProxy() return true; } -int BundleActiveClient::ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) +int BundleActiveClient::ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) { BUNDLE_ACTIVE_LOGI("BundleActiveClient::ReportEvent called"); if (!GetBundleActiveProxy()) { return -1; } - return bundleActiveProxy_->ReportEvent(bundleName, abilityName, abilityId, continuousTask, userId, eventId); + return bundleActiveProxy_->ReportFormClickedOrRemoved(bundleName, moduleName, modulePackage, formName, formId, + formDimension, userId, eventId); } bool BundleActiveClient::IsBundleIdle(const std::string& bundleName) @@ -115,6 +116,14 @@ int BundleActiveClient::QueryPackageGroup() } return bundleActiveProxy_->QueryPackageGroup(); } + +int BundleActiveClient::QueryFormStatistics(int32_t maxNum, std::vector& results) +{ + if (!GetBundleActiveProxy()) { + return -1; + } + return bundleActiveProxy_->QueryFormStatistics(maxNum, results); +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index ffcab9cef07228d332af184c3cc4a9089f3be0df..ef99bd7c132f81cf51d532cc4ed4bdcc36f0596e 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -14,13 +14,12 @@ */ #include "bundle_active_proxy.h" -#include "bundle_active_package_stats.h" -#include "bundle_active_package_stats.h" namespace OHOS { namespace DeviceUsageStats { -int BundleActiveProxy::ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) +int BundleActiveProxy::ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) { MessageParcel data; MessageParcel reply; @@ -29,12 +28,14 @@ int BundleActiveProxy::ReportEvent(std::string& bundleName, std::string& ability return -1; } data.WriteString(bundleName); - data.WriteString(abilityName); - data.WriteString(abilityId); - data.WriteString(continuousTask); + data.WriteString(moduleName); + data.WriteString(modulePackage); + data.WriteString(formName); + data.WriteInt64(formId); + data.WriteInt32(formDimension); data.WriteInt32(userId); data.WriteInt32(eventId); - Remote() -> SendRequest(REPORT_EVENT, data, reply, option); + Remote() -> SendRequest(REPORT_FORM_EVENT, data, reply, option); int32_t result = reply.ReadInt32(); return result; @@ -74,7 +75,7 @@ std::vector BundleActiveProxy::QueryPackageStats(const int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int i = 0; i < size; i++) { - tmp = tmp->Unmarshalling(reply); + tmp = tmp->UnMarshalling(reply); if (tmp == nullptr) { continue; } @@ -108,7 +109,7 @@ std::vector BundleActiveProxy::QueryEvents(const int64_t begi int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int i = 0; i < size; i++) { - tmp = tmp->Unmarshalling(reply); + tmp = tmp->UnMarshalling(reply); if (tmp == nullptr) { continue; } @@ -152,7 +153,7 @@ std::vector BundleActiveProxy::QueryCurrentPackageStat int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int i = 0; i < size; i++) { - tmp = tmp->Unmarshalling(reply); + tmp = tmp->UnMarshalling(reply); if (tmp == nullptr) { continue; } @@ -183,7 +184,7 @@ std::vector BundleActiveProxy::QueryCurrentEvents(const int64 int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int i = 0; i < size; i++) { - tmp = tmp->Unmarshalling(reply); + tmp = tmp->UnMarshalling(reply); if (tmp == nullptr) { continue; } @@ -210,6 +211,28 @@ int BundleActiveProxy::QueryPackageGroup() BUNDLE_ACTIVE_LOGI("QueryPackageGroup result is %{public}d", packageGroup); return packageGroup; } + +int BundleActiveProxy::QueryFormStatistics(int32_t maxNum, std::vector& results) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!data.WriteInterfaceToken(GetDescriptor())) { + return -1; + } + data.WriteInt32(maxNum); + Remote() -> SendRequest(QUERY_FORM_STATS, data, reply, option); + int32_t size = reply.ReadInt32(); + std::shared_ptr tmp; + for (int i = 0; i < size; i++) { + tmp = tmp->UnMarshalling(reply); + if (tmp == nullptr) { + continue; + } + results.emplace_back(*tmp); + } + return 0; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index e2037b1cc3611b3958ac4684c133339b4d613bfa..2e1a870049999630c409864221a7724889c4dadc 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -109,6 +109,7 @@ public: int IsBundleIdle(const std::string& bundleName, const int userId); // query the app group for calling app. int QueryPackageGroup(const int userId, const std::string bundleName); + int QueryFormStatistics(int32_t maxNum, std::vector& results, int userId); // get the wall time and check if the wall time is changed. int64_t CheckTimeChangeAndGetWallTime(int userId = 0); // convert event timestamp from boot based time to wall time. diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index ff28fc2b0caadf26ced29958b6b2695ea014f740..bd9f3090fd5fbd794095947cad8f94f0f635eb9b 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -33,11 +33,16 @@ class BundleActiveService : public SystemAbility, public BundleActiveStub { DECLARE_SYSTEM_ABILITY(BundleActiveService); public: using IBundleMgr = OHOS::AppExecFwk::IBundleMgr; - using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo; using BundleInfo = OHOS::AppExecFwk::BundleInfo; using BundleFlag = OHOS::AppExecFwk::BundleFlag; - int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) override; + using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo; + using AbilityInfo = OHOS::AppExecFwk::AbilityInfo; + using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo; + using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag; + using AbilityType = OHOS::AppExecFwk::AbilityType; + int ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) override; bool IsBundleIdle(const std::string& bundleName) override; std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) override; @@ -48,6 +53,7 @@ public: const int64_t endTime) override; std::vector QueryCurrentEvents(const int64_t beginTime, const int64_t endTime) override; int QueryPackageGroup() override; + int QueryFormStatistics(int32_t maxNum, std::vector& results) override; BundleActiveService(int32_t systemAbilityId, int runOnCreate) : SystemAbility(systemAbilityId, runOnCreate) {} ~BundleActiveService() {} @@ -75,6 +81,10 @@ private: bool SubscribeAppState(); bool SubscribeContinuousTask(); OHOS::sptr GetAppManagerInstance(); + void GetAndSetModuleRecordInfos(BundleInfo& bundleInfo, HapModuleInfo& hapModuleInfo, + ApplicationInfo& appInfo, AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord); + void SerModuleProperties(const BundleInfo& bundleInfo, const HapModuleInfo& hapModuleInfo, + const ApplicationInfo& appInfo, const AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/bundle_active_usage_database.h b/services/common/include/bundle_active_usage_database.h index d858749611d3d87efe46786615e50d39ed258cba..64545d65e83a08392e6e7e830dccb7651e3e89c7 100644 --- a/services/common/include/bundle_active_usage_database.h +++ b/services/common/include/bundle_active_usage_database.h @@ -31,6 +31,7 @@ #include "bundle_active_period_stats.h" #include "bundle_active_calendar.h" #include "bundle_active_package_history.h" +#include "bundle_active_module_record.h" namespace OHOS { namespace DeviceUsageStats { @@ -57,6 +58,12 @@ public: GetBundleHistoryData(int userId); void OnPackageUninstalled(const int userId, const std::string& bundleName); void ChangeToDebug(); + void UpdateModuleData(const int userId, + std::map> moduleRecords_, const int64_t timeStamp); + void RemoveFormData(const int userId, const std::string formName, + const int32_t formDimension, const int64_t formId); + void GetFormDataWhenInit(const int32_t userId, std::map>& moduleRecords); private: void CheckDatabaseVersion(); diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index 401584ea340ab07bfc9b57ef6ba98a4e9c2bb2f5..dea9f7d773badedb896c8269edd9101f9660d563 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -40,10 +40,13 @@ namespace OHOS { namespace DeviceUsageStats { class BundleActivePackageStats; class BundleActiveEvent; +class BundleActiveModuleRecord; + class IBundleActiveService : public IRemoteBroker { public: - virtual int ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) = 0; + virtual int ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) = 0; virtual bool IsBundleIdle(const std::string& bundleName) = 0; virtual std::vector QueryPackageStats(const int intervalType, const int64_t beginTime, const int64_t endTime, int32_t& errCode) = 0; @@ -54,17 +57,19 @@ public: virtual std::vector QueryCurrentEvents(const int64_t beginTime, const int64_t endTime) = 0; virtual int QueryPackageGroup() = 0; virtual void SetBundleGroup(const std::string& bundleName, int newGroup, int userId) = 0; + virtual int QueryFormStatistics(int32_t maxNum, std::vector& results) = 0; public: enum { - REPORT_EVENT = 1, + REPORT_FORM_EVENT = 1, IS_BUNDLE_IDLE = 2, QUERY_USAGE_STATS = 3, QUERY_EVENTS = 4, QUERY_CURRENT_USAGE_STATS = 5, QUERY_CURRENT_EVENTS = 6, QUERY_BUNDLE_GROUP = 7, - SET_BUNDLE_GROUP = 8 + SET_BUNDLE_GROUP = 8, + QUERY_FORM_STATS = 9 }; public: DECLARE_INTERFACE_DESCRIPTOR(u"Resourceschedule.IBundleActiveService"); diff --git a/services/common/src/bundle_active_continuous_task_observer.cpp b/services/common/src/bundle_active_continuous_task_observer.cpp index 5492acee143f5b71b9199e836b05a1e4c2b34c88..1298b4640ce81ce6ed23634ea1ed3b56b4dfbc08 100644 --- a/services/common/src/bundle_active_continuous_task_observer.cpp +++ b/services/common/src/bundle_active_continuous_task_observer.cpp @@ -83,6 +83,7 @@ void BundleActiveContinuousTaskObserver::ReportContinuousTaskEvent( if (GetBundleMgr()) { bundleMgr_->GetBundleNameForUid(uid, bundleName); } else { + BUNDLE_ACTIVE_LOGE("Get bundle mgr failed!"); return; } OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(uid, userId); diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 01f0293a7b73840af847325695285af9ad877be2..6f67c08657fb9660c07c6de0c8c23ff508effa33 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -442,6 +442,11 @@ int BundleActiveCore::ReportEvent(BundleActiveEvent& event, const int userId) BUNDLE_ACTIVE_LOGE("get user data service failed!"); return -1; } + if (event.eventId_ == BundleActiveEvent::FORM_IS_CLICKED || + event.eventId_ == BundleActiveEvent::FORM_IS_REMOVED) { + service->ReportFormClickedOrRemoved(event); + return 0; + } service->ReportEvent(event); bundleGroupController_->ReportEvent(event, bootBasedTimeStamp, userId); return 0; @@ -520,6 +525,21 @@ std::vector BundleActiveCore::QueryEvents(const int userId, c return result; } +int BundleActiveCore::QueryFormStatistics(int32_t maxNum, std::vector& results, int userId) +{ + std::lock_guard lock(mutex_); + int64_t timeNow = CheckTimeChangeAndGetWallTime(userId); + if (timeNow == -1) { + return -1; + } + std::shared_ptr service = GetUserDataAndInitializeIfNeeded(userId, timeNow, debugCore_); + if (service == nullptr) { + return -1; + } + int errCode = service->QueryFormStatistics(maxNum, results); + return errCode; +} + void BundleActiveCore::SetBundleGroup(const std::string& bundleName, const int newGroup, const int userId) { int newReason = GROUP_CONTROL_REASON_FORCED; diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 8b73dc0363f83d3cb0e2e8eeca130d714c466a4c..ab97ae1685c95185228613e887a3cb21e4957a27 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -30,6 +30,8 @@ static const int PERIOD_BEST_JS = 0; static const int PERIOD_YEARLY_JS = 4; static const int PERIOD_BEST_SERVICE = 4; static const int DELAY_TIME = 2000; +static const int ROOT_UID = 0; +static const int SYSTEM_UID = 1000; REGISTER_SYSTEM_ABILITY_BY_ID(BundleActiveService, DEVICE_USAGE_STATISTICS_SYS_ABILITY_ID, true); const std::string NEEDED_PERMISSION = "ohos.permission.BUNDLE_ACTIVE_INFO"; @@ -201,22 +203,35 @@ void BundleActiveService::OnStop() } -int BundleActiveService::ReportEvent(std::string& bundleName, std::string& abilityName, std::string abilityId, - const std::string& continuousTask, const int userId, const int eventId) +int BundleActiveService::ReportFormClickedOrRemoved(const std::string& bundleName, const std::string& moduleName, + const std::string modulePackage, const std::string& formName, const int64_t formId, + const int32_t formDimension, const int userId, const int eventId) { - BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); - tmpHandlerObject.event_.bundleName_ = bundleName; - tmpHandlerObject.event_.abilityName_ = abilityName; - tmpHandlerObject.event_.abilityId_ = abilityId; - tmpHandlerObject.event_.eventId_ = eventId; - tmpHandlerObject.event_.continuousTaskAbilityName_ = continuousTask; - sptr timer = MiscServices::TimeServiceClient::GetInstance(); - tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); - std::shared_ptr handlerobjToPtr = - std::make_shared(tmpHandlerObject); - auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); - reportHandler_->SendEvent(event); - return 0; + int callingUid = OHOS::IPCSkeleton::GetCallingUid(); + if (!GetBundleMgrProxy()) { + BUNDLE_ACTIVE_LOGE("Get bundle manager proxy failed!"); + return -1; + } + bool bundleIsSystemApp = sptrBundleMgr_->CheckIsSystemAppByUid(callingUid); + if ((callingUid == ROOT_UID || callingUid == SYSTEM_UID) || bundleIsSystemApp) { + BundleActiveReportHandlerObject tmpHandlerObject(userId, ""); + tmpHandlerObject.event_.bundleName_ = bundleName; + tmpHandlerObject.event_.moduleName_ = moduleName; + tmpHandlerObject.event_.modulePackage_ = modulePackage; + tmpHandlerObject.event_.formName_ = formName; + tmpHandlerObject.event_.formId_ = formId; + tmpHandlerObject.event_.formDimension_ = formDimension; + tmpHandlerObject.event_.eventId_ = eventId; + sptr timer = MiscServices::TimeServiceClient::GetInstance(); + tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs(); + std::shared_ptr handlerobjToPtr = + std::make_shared(tmpHandlerObject); + auto event = AppExecFwk::InnerEvent::Get(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr); + reportHandler_->SendEvent(event); + return 0; + } else { + return -1; + } } bool BundleActiveService::IsBundleIdle(const std::string& bundleName) @@ -430,6 +445,74 @@ bool BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int uid, return true; } } + +int BundleActiveService::QueryFormStatistics(int32_t maxNum, std::vector& results) +{ + int callingUid = OHOS::IPCSkeleton::GetCallingUid(); + BUNDLE_ACTIVE_LOGI("QueryFormStatistics UID is %{public}d", callingUid); + // get userid + int userId = -1; + OHOS::ErrCode ret = OHOS::AccountSA::OsAccountManager::GetOsAccountLocalIdFromUid(callingUid, userId); + int32_t errCode = 0; + if (ret == ERR_OK && userId != -1) { + BUNDLE_ACTIVE_LOGI("QueryEvents userid is %{public}d", userId); + bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, userId, errCode); + if (isSystemAppAndHasPermission == true) { + errCode = bundleActiveCore_->QueryFormStatistics(maxNum, results, userId); + } + } + return errCode; +} + +void BundleActiveService::GetAndSetModuleRecordInfos(BundleInfo& bundleInfo, HapModuleInfo& hapModuleInfo, + ApplicationInfo& appInfo, AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord) +{ + if (!GetBundleMgrProxy()) { + return; + } + if (!sptrBundleMgr_->GetApplicationInfo(moduleRecord.bundleName_, ApplicationFlag::GET_BASIC_APPLICATION_INFO, + moduleRecord.userId_, appInfo)) { + BUNDLE_ACTIVE_LOGE("GetApplicationInfo failed!"); + return; + } + if (!sptrBundleMgr_->GetBundleInfo(moduleRecord.bundleName_, BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, + moduleRecord.userId_)) { + BUNDLE_ACTIVE_LOGE("GetBundleInfo failed!"); + return; + } + AbilityInfo mockAbilityInfo; + mockAbilityInfo.bundleName = moduleRecord.bundleName_; + mockAbilityInfo.package = moduleRecord.modulePackage_; + if (!sptrBundleMgr_->GetHapModuleInfo(mockAbilityInfo, hapModuleInfo)) { + BUNDLE_ACTIVE_LOGE("GetHapModuleInfo failed!"); + return; + } + std::string mainAbility = hapModuleInfo.mainAbility; + if (!hapModuleInfo.abilityInfos.empty()) { + for (auto oneAbilityInfo : hapModuleInfo.abilityInfos) { + if (oneAbilityInfo.type != AbilityType::PAGE) { + continue; + } + if (mainAbility.empty() || mainAbility.compare(abilityInfo.name) == 0) { + SerModuleProperties(bundleInfo, hapModuleInfo, appInfo, oneAbilityInfo, moduleRecord); + break; + } + } + } +} + +void BundleActiveService::SerModuleProperties(const BundleInfo& bundleInfo, const HapModuleInfo& hapModuleInfo, + const ApplicationInfo& appInfo, const AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord) +{ + moduleRecord.deviceId_ = appInfo.deviceId; + moduleRecord.abilityName_ = abilityInfo.name; + moduleRecord.appLabelId_ = appInfo.labelId; + // hapModuleInfo.labelId 待徐浩添加 + moduleRecord.abilityName_ = abilityInfo.labelId; + moduleRecord.descriptionId_ = abilityInfo.descriptionId; + moduleRecord.abilityIconId_ = abilityInfo.iconId; + moduleRecord.installFreeSupported_ = hapModuleInfo.installationFree; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index d22118718cfd9550617726b413797fe4dd6146ef..f69fd2ece6ba4bce9232153fd76dffbb97a720f0 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -16,6 +16,7 @@ #include "bundle_active_stub.h" #include "bundle_active_package_stats.h" #include "bundle_active_event.h" +#include "bundle_active_module_record.h" namespace OHOS { namespace DeviceUsageStats { @@ -26,14 +27,17 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me return -1; } switch (code) { - case REPORT_EVENT: { + case REPORT_FORM_EVENT: { std::string bundleName = data.ReadString(); - std::string ablityName = data.ReadString(); - std::string abilityId = data.ReadString(); - std::string continuousTaskName = data.ReadString(); + std::string moduleName = data.ReadString(); + std::string modulePackage = data.ReadString(); + std::string formName = data.ReadString(); + int64_t formId = data.ReadInt64(); + int32_t formDimension = data.ReadInt32(); int userId = data.ReadInt32(); int eventId = data.ReadInt32(); - int result = ReportEvent(bundleName, ablityName, abilityId, continuousTaskName, userId, eventId); + int result = ReportFormClickedOrRemoved(bundleName, moduleName, modulePackage, formName, formId, + formDimension, userId, eventId); return reply.WriteInt32(result); } case IS_BUNDLE_IDLE: { @@ -130,6 +134,21 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me result = QueryPackageGroup(); return reply.WriteInt32(result); } + case QUERY_FORM_STATS: { + std::vector results; + int size = 0; + int32_t maxNum = data.ReadInt32(); + QueryFormStatistics(maxNum, results); + size = static_cast(results.size()); + reply.WriteInt32(size); + for (int i = 0; i < size; i++) { + bool tmp = results[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; + } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } diff --git a/services/common/src/bundle_active_usage_database.cpp b/services/common/src/bundle_active_usage_database.cpp index 729b7ffe7daa696fb4068964a3fdd4cfe495672f..5455de54980a7f7538b5f78c7dc2283d3bb856f7 100644 --- a/services/common/src/bundle_active_usage_database.cpp +++ b/services/common/src/bundle_active_usage_database.cpp @@ -66,6 +66,7 @@ void BundleActiveUsageDatabase::ChangeToDebug() calendar_->ChangeToDebug(); debugDatabase_ = true; } + void BundleActiveUsageDatabase::InitUsageGroupInfo(int32_t databaseType) { lock_guard lock(databaseMutex_); @@ -1201,6 +1202,7 @@ vector BundleActiveUsageDatabase::QueryDatabaseEvents(int64_t void BundleActiveUsageDatabase::OnPackageUninstalled(const int userId, const string& bundleName) { + // 加入应用删除后删除卡片 lock_guard lock(databaseMutex_); for (uint32_t i = 0; i < sortedTableArray_.size(); i++) { if (sortedTableArray_.at(i).empty()) { @@ -1255,6 +1257,25 @@ int64_t BundleActiveUsageDatabase::GetSystemTimeMs() } return static_cast(tarDate); } + +void BundleActiveUsageDatabase::UpdateModuleData(const int userId, + std::map> moduleRecords_, const int64_t timeStamp) +{ + // mock +} + +void BundleActiveUsageDatabase::RemoveFormData(const int userId, const std::string formName, + const int32_t formDimension, const int64_t formId) +{ + // mock +} + +void BundleActiveUsageDatabase::GetFormDataWhenInit(const int32_t userId, std::map>& moduleRecords) +{ + // key: std::string combineInfo = bundlename + " " + modulePackage + " " + moduleName + // mock +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/include/bundle_active_event.h b/services/packageusage/include/bundle_active_event.h index dc26307bc7d5f3ab602bfeadbc5dc80485cebe58..00cd140a6182c4b60f11206203dccee2ed8eb169 100644 --- a/services/packageusage/include/bundle_active_event.h +++ b/services/packageusage/include/bundle_active_event.h @@ -40,11 +40,18 @@ public: static const int KEYGUARD_SHOWN = 15; static const int KEYGUARD_HIDDEN = 16; static const int NOTIFICATION_SEEN = 17; + static const int FORM_IS_CLICKED = 18; + static const int FORM_IS_REMOVED = 19; inline static const std::string DEVICE_EVENT_PACKAGE_NAME = "openharmony"; std::string bundleName_; std::string continuousTaskAbilityName_; std::string abilityName_; std::string abilityId_; + std::string moduleName_; + std::string modulePackage_; + std::string formName_; + int32_t formDimension_; + int64_t formId_; int64_t timeStamp_; int eventId_; @@ -58,7 +65,7 @@ public: int64_t GetTimeStamp(); int GetEventId(); virtual bool Marshalling(Parcel &parcel) const override; - std::shared_ptr Unmarshalling(Parcel &parcel); + std::shared_ptr UnMarshalling(Parcel &parcel); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/packageusage/include/bundle_active_form_record.h b/services/packageusage/include/bundle_active_form_record.h new file mode 100644 index 0000000000000000000000000000000000000000..5c482c9b4b2999ac5cefe9682fb9abf73f117ed9 --- /dev/null +++ b/services/packageusage/include/bundle_active_form_record.h @@ -0,0 +1,54 @@ +/* + * 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_FORM_RECORD_H +#define BUNDLE_ACTIVE_FORM_RECORD_H + +#include + +#include "ibundle_active_service.h" + +namespace OHOS { +namespace DeviceUsageStats { +class BundleActiveFormRecord : public Parcelable { +public: + std::string formName_; + int32_t formDimension_; + int64_t formId_; + int64_t formLastUsedTime_; + int32_t count_; + int32_t userId_; + void UpdateFormRecord(const int64_t timeStamp); + BundleActiveFormRecord(const BundleActiveFormRecord& orig); + virtual bool Marshalling(Parcel &parcel) const override; + std::shared_ptr UnMarshalling(Parcel &parcel); + BundleActiveFormRecord(); + BundleActiveFormRecord(const std::string formName, const int32_t formDimension, const int64_t formId, + const int64_t timeStamp, const int32_t userId); + ~BundleActiveFormRecord() {} + bool operator==(const BundleActiveFormRecord& formRecord) const + { + return (this->formId_ == formRecord.formId_); + } + bool operator==(const int64_t formId) const + { + return (this->formId_ == formId); + } + static bool cmp(const BundleActiveFormRecord& formRecordA, const BundleActiveFormRecord& formRecordB); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_FORM_RECORD_H + diff --git a/services/packageusage/include/bundle_active_module_record.h b/services/packageusage/include/bundle_active_module_record.h new file mode 100644 index 0000000000000000000000000000000000000000..c1545872e3cca48461c6970f284515a93e8b53d5 --- /dev/null +++ b/services/packageusage/include/bundle_active_module_record.h @@ -0,0 +1,62 @@ +/* + * 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_MODULE_RECORD_H +#define BUNDLE_ACTIVE_MODULE_RECORD_H + +#include +#include +#include + +#include "bundle_active_form_record.h" +#include "ibundle_active_service.h" + +namespace OHOS { +namespace DeviceUsageStats { +class BundleActiveModuleRecord : public Parcelable { +public: + void AddOrUpdateOneFormRecord(const std::string formName, const int32_t formDimension, + const int64_t formId, const int64_t timeStamp); + void RemoveOneFormRecord(const std::string formName, const int32_t formDimension, + const int64_t formId); + void UpdateModuleRecord(int64_t timeStamp); + BundleActiveModuleRecord(); + virtual bool Marshalling(Parcel &parcel) const override; + std::shared_ptr UnMarshalling(Parcel &parcel); + ~BundleActiveModuleRecord() {} + static bool cmp(const BundleActiveModuleRecord& moduleRecordA, const BundleActiveModuleRecord& moduleRecordB); +public: + std::string deviceId_; + std::string bundleName_; + std::string moduleName_; + std::string modulePackage_; + std::string abilityName_; + uint32_t appLabelId_; + uint32_t labelId_; + uint32_t descriptionId_; + uint32_t abilityLableId_; + uint32_t abilityDescriptionId_; + uint32_t abilityIconId_; + uint32_t launchedCount_; + int64_t lastModuleUsedTime_; + int32_t userId_; + bool removed_; + bool installFreeSupported_; + bool isNewAdded_; + std::vector formRecords_; // key:formid, value:formhistory +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_MODULE_RECORD_H \ No newline at end of file diff --git a/services/packageusage/include/bundle_active_package_stats.h b/services/packageusage/include/bundle_active_package_stats.h index 6e648a8d915b2a923049928730751bac0d7e3825..26af6d238b0bbf270bb64593be6167c5736e3107 100644 --- a/services/packageusage/include/bundle_active_package_stats.h +++ b/services/packageusage/include/bundle_active_package_stats.h @@ -55,7 +55,7 @@ public: void IncrementServiceTimeUsed(const int64_t timeStamp); void IncrementBundleLaunchedCount(); virtual bool Marshalling(Parcel &parcel) const override; - std::shared_ptr Unmarshalling(Parcel &parcel); + std::shared_ptr UnMarshalling(Parcel &parcel); void printdata(); private: diff --git a/services/packageusage/include/bundle_active_user_service.h b/services/packageusage/include/bundle_active_user_service.h index dffb022dc029faa403c1d9824f1961268b9f4826..8c44d353c285ce508b02b5abf69c669ad7a35711 100644 --- a/services/packageusage/include/bundle_active_user_service.h +++ b/services/packageusage/include/bundle_active_user_service.h @@ -26,6 +26,7 @@ #include "bundle_active_stats_combiner.h" #include "bundle_active_usage_database.h" #include "bundle_active_constant.h" +#include "bundle_active_module_record.h" namespace OHOS { namespace DeviceUsageStats { @@ -56,6 +57,7 @@ public: ~BundleActiveUserService() {} void ReportForShutdown(const BundleActiveEvent& event); void ReportEvent(const BundleActiveEvent& event); + void ReportFormClickedOrRemoved(const BundleActiveEvent& event); void RestoreStats(bool forced); void RenewStatsInMemory(const int64_t timeStamp); void RenewTableTime(int64_t oldTime, int64_t newTime); @@ -67,12 +69,14 @@ public: const int64_t endTime, const int userId, const std::string& bundleName); std::vector QueryEvents(const int64_t beginTime, const int64_t endTime, const int userId, const std::string& bundleName); + int QueryFormStatistics(int32_t maxNum, std::vector& results); void LoadActiveStats(const int64_t timeStamp, const bool& force, const bool& timeChanged); private: static const int64_t ONE_SECOND_MILLISECONDS = 1000; BundleActiveUsageDatabase database_; std::vector> currentStats_; + std::map> moduleRecords_; bool statsChanged_; bool debugUserService_; std::string lastBackgroundBundle_; @@ -80,6 +84,7 @@ private: std::vector periodLength_ = {0, 0, 0, 0}; void NotifyStatsChanged(); void NotifyNewUpdate(); + std::shared_ptr GetOrCreateModuleRecord(const BundleActiveEvent& event); void PrintInMemPackageStats(const int idx); void PrintInMemEventStats(); }; diff --git a/services/packageusage/src/bundle_active_event.cpp b/services/packageusage/src/bundle_active_event.cpp index e07abd48aea4f2a8435d6b64b938677ceb543cc5..c7b764f55e8d49e80dab80913afbf9c125d908ba 100644 --- a/services/packageusage/src/bundle_active_event.cpp +++ b/services/packageusage/src/bundle_active_event.cpp @@ -23,6 +23,11 @@ BundleActiveEvent::BundleActiveEvent() continuousTaskAbilityName_ = ""; abilityName_ = ""; abilityId_ = ""; + moduleName_ = ""; + modulePackage_ = ""; + formName_ = ""; + formId_ = 0; + formDimension_ = 0; timeStamp_ = 0; eventId_ = 0; } @@ -30,8 +35,14 @@ BundleActiveEvent::BundleActiveEvent() BundleActiveEvent::BundleActiveEvent (const BundleActiveEvent& orig) { bundleName_ = orig.bundleName_; + continuousTaskAbilityName_ = orig.continuousTaskAbilityName_; abilityName_ = orig.abilityName_; abilityId_ = orig.abilityId_; + moduleName_ = orig.moduleName_; + modulePackage_ = orig.modulePackage_; + formName_ = orig.formName_; + formId_ = orig.formId_; + formDimension_ = orig.formDimension_; timeStamp_ = orig.timeStamp_; eventId_ = orig.eventId_; } @@ -80,7 +91,7 @@ bool BundleActiveEvent::Marshalling(Parcel &parcel) const return false; } -std::shared_ptr BundleActiveEvent::Unmarshalling(Parcel &parcel) +std::shared_ptr BundleActiveEvent::UnMarshalling(Parcel &parcel) { std::shared_ptr result = std::make_shared(); result->bundleName_ = parcel.ReadString(); diff --git a/services/packageusage/src/bundle_active_form_record.cpp b/services/packageusage/src/bundle_active_form_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0f7de8ba0e7574aa76d2cb70b7fd5fd801ebd809 --- /dev/null +++ b/services/packageusage/src/bundle_active_form_record.cpp @@ -0,0 +1,89 @@ +/* + * 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 "bundle_active_form_record.h" + +namespace OHOS { +namespace DeviceUsageStats { +BundleActiveFormRecord::BundleActiveFormRecord() +{ + formName_ = ""; + formDimension_ = 0; + formId_ = 0; + formLastUsedTime_ = -1; + count_ = 0; + userId_ = -1; +} + +BundleActiveFormRecord::BundleActiveFormRecord(const std::string formName, const int32_t formDimension, + const int64_t formId, const int64_t timeStamp, const int32_t userId) +{ + formName_ = formName; + formDimension_ = formDimension; + formId_ = formId; + formLastUsedTime_ = timeStamp; + count_ = 1; + userId_ = userId; +} + +BundleActiveFormRecord::BundleActiveFormRecord(const BundleActiveFormRecord& orig) +{ + formName_ = orig.formName_; + formDimension_ = orig.formDimension_; + formId_ = orig.formId_; + formLastUsedTime_ = orig.formLastUsedTime_; + count_ = orig.count_; + userId_ = orig.userId_; +} + +void BundleActiveFormRecord::UpdateFormRecord(const int64_t timeStamp) +{ + if (formLastUsedTime_ < timeStamp) { + formLastUsedTime_ = timeStamp; + count_++; + } +} + +bool BundleActiveFormRecord::Marshalling(Parcel &parcel) const +{ + if (parcel.WriteString(formName_) && + parcel.WriteInt32(formDimension_) && + parcel.WriteInt64(formId_) && + parcel.WriteInt64(formLastUsedTime_) && + parcel.WriteInt32(count_)) { + return true; + } + return false; +} + +std::shared_ptr BundleActiveFormRecord::UnMarshalling(Parcel &parcel) +{ + std::shared_ptr result = std::make_shared(); + result->formName_ = parcel.ReadString(); + result->formDimension_ = parcel.ReadInt32(); + result->formId_ = parcel.ReadInt64(); + result->formLastUsedTime_ = parcel.ReadInt64(); + result->count_ = parcel.ReadInt32(); + return result; +} + +bool BundleActiveFormRecord::cmp(const BundleActiveFormRecord& formRecordA, + const BundleActiveFormRecord& formRecordB) +{ + return formRecordA.count_ > formRecordB.count_; +} +} // namespace DeviceUsageStats +} // namespace OHOS + diff --git a/services/packageusage/src/bundle_active_module_record.cpp b/services/packageusage/src/bundle_active_module_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e99ce0ec7542c85505dc6274a908202582f934d0 --- /dev/null +++ b/services/packageusage/src/bundle_active_module_record.cpp @@ -0,0 +1,131 @@ +/* + * 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 "bundle_active_module_record.h" + +namespace OHOS { +namespace DeviceUsageStats { +void BundleActiveModuleRecord::AddOrUpdateOneFormRecord(const std::string formName, const int32_t formDimension, + const int64_t formId, const int64_t timeStamp) +{ + BundleActiveFormRecord newFormRecord(formName, formDimension, formId, timeStamp, userId_); + for (auto& formRecord : formRecords_) { + if (formRecord == newFormRecord) { + formRecord.UpdateFormRecord(timeStamp); + return; + } + } + formRecords_.emplace_back(newFormRecord); +} + +void BundleActiveModuleRecord::RemoveOneFormRecord(const std::string formName, const int32_t formDimension, + const int64_t formId) +{ + for (auto it = formRecords_.begin(); it != formRecords_.end(); it++) { + if (*it == formId) { + formRecords_.erase(it); + return; + } + } +} + +void BundleActiveModuleRecord::UpdateModuleRecord(int64_t timeStamp) +{ + if (lastModuleUsedTime_ < timeStamp) { + lastModuleUsedTime_ = timeStamp; + launchedCount_++; + } +} + +BundleActiveModuleRecord::BundleActiveModuleRecord() +{ + deviceId_ = ""; + bundleName_ = ""; // in database + moduleName_ = ""; // in database + modulePackage_ = ""; // in database + abilityName_ = ""; + appLabelId_ = 0; + labelId_ = 0; + descriptionId_ = 0; + abilityLableId_ = 0; + abilityDescriptionId_ = 0; + abilityIconId_ = 0; + launchedCount_ = 0; + lastModuleUsedTime_ = 0; + removed_ = false; + installFreeSupported_ = false; + isNewAdded_ = false; + userId_ = -1; +} + +bool BundleActiveModuleRecord::Marshalling(Parcel &parcel) const +{ + if (parcel.WriteString(deviceId_) && + parcel.WriteString(bundleName_) && + parcel.WriteString(moduleName_) && + parcel.WriteString(modulePackage_) && + parcel.WriteString(abilityName_) && + parcel.WriteUint32(appLabelId_) && + parcel.WriteUint32(descriptionId_) && + parcel.WriteUint32(abilityLableId_) && + parcel.WriteUint32(abilityDescriptionId_) && + parcel.WriteUint32(abilityIconId_) && + parcel.WriteUint32(launchedCount_) && + parcel.WriteInt64(lastModuleUsedTime_) && + parcel.WriteUint32(formRecords_.size()) + ) { + for (auto formRecord : formRecords_) { + formRecord.Marshalling(parcel); + } + return true; + } + return false; +} + +std::shared_ptr BundleActiveModuleRecord::UnMarshalling(Parcel &parcel) +{ + std::shared_ptr result = std::make_shared(); + result->deviceId_ = parcel.ReadString(); + result->bundleName_ = parcel.ReadString(); + result->moduleName_ = parcel.ReadString(); + result->modulePackage_ = parcel.ReadString(); + result->abilityName_ = parcel.ReadString(); + result->appLabelId_ = parcel.ReadUint32(); + result->descriptionId_ = parcel.ReadUint32(); + result->abilityLableId_ = parcel.ReadUint32(); + result->abilityDescriptionId_ = parcel.ReadUint32(); + result->abilityIconId_ = parcel.ReadUint32(); + result->launchedCount_ = parcel.ReadUint32(); + result->lastModuleUsedTime_ = parcel.ReadInt64(); + uint32_t size = parcel.ReadUint32(); + std::shared_ptr tmp = std::make_shared(); + for (uint32_t i = 0; i < size; i++) { + tmp = tmp->UnMarshalling(parcel); + if (!tmp) { + continue; + } + result->formRecords_.emplace_back(*tmp); + } + return result; +} + +bool BundleActiveModuleRecord::cmp(const BundleActiveModuleRecord& moduleRecordA, + const BundleActiveModuleRecord& moduleRecordB) +{ + return moduleRecordA.lastModuleUsedTime_ > moduleRecordB.lastModuleUsedTime_; +} +} // namespace DeviceUsageStats +} // namespace OHOS + diff --git a/services/packageusage/src/bundle_active_package_stats.cpp b/services/packageusage/src/bundle_active_package_stats.cpp index aa09095d7d93fe2e7379ea4f5fb01264f3246fa1..f43e0812d22a68ccc965f0ac606e96101d5aa3ad 100644 --- a/services/packageusage/src/bundle_active_package_stats.cpp +++ b/services/packageusage/src/bundle_active_package_stats.cpp @@ -253,7 +253,7 @@ bool BundleActivePackageStats::Marshalling(Parcel &parcel) const return false; } -std::shared_ptr BundleActivePackageStats::Unmarshalling(Parcel &parcel) +std::shared_ptr BundleActivePackageStats::UnMarshalling(Parcel &parcel) { std::shared_ptr result = std::make_shared(); result->bundleName_ = parcel.ReadString(); diff --git a/services/packageusage/src/bundle_active_user_service.cpp b/services/packageusage/src/bundle_active_user_service.cpp index cf60faeeaaf7eefb3a026a304957327379756f82..c6d10a69bf3576b65aedc40889fe1a377e5a6bee 100644 --- a/services/packageusage/src/bundle_active_user_service.cpp +++ b/services/packageusage/src/bundle_active_user_service.cpp @@ -23,6 +23,7 @@ void BundleActiveUserService::Init(const int64_t timeStamp) database_.InitDatabaseTableInfo(timeStamp); BUNDLE_ACTIVE_LOGI("Init called"); LoadActiveStats(timeStamp, false, false); + database_.GetFormDataWhenInit(userId_, moduleRecords_); std::shared_ptr currentDailyStats = currentStats_[BundleActivePeriodStats::PERIOD_DAILY]; if (currentDailyStats != nullptr) { BundleActiveEvent startupEvent(BundleActiveEvent::STARTUP, timeStamp - ONE_SECOND_MILLISECONDS); @@ -172,6 +173,8 @@ void BundleActiveUserService::RestoreStats(bool forced) database_.UpdateUsageData(i, *(currentStats_[i])); } } + database_.UpdateModuleData(userId_, moduleRecords_, + currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->beginTime_); currentStats_[BundleActivePeriodStats::PERIOD_DAILY]->events_.Clear(); statsChanged_ = false; BUNDLE_ACTIVE_LOGI("change statsChanged_ to %{public}d user is %{public}d", statsChanged_, userId_); @@ -185,9 +188,9 @@ void BundleActiveUserService::LoadActiveStats(const int64_t timeStamp, const boo if (debugUserService_ == true) { tmpCalendar.ChangeToDebug(); } - tmpCalendar.SetMilliseconds(timeStamp); - tmpCalendar.TruncateTo(BundleActivePeriodStats::PERIOD_DAILY); for (uint32_t intervalType = 0; intervalType < periodLength_.size(); intervalType++) { + tmpCalendar.SetMilliseconds(timeStamp); + tmpCalendar.TruncateTo(static_cast(intervalType)); if (!force && currentStats_[intervalType] != nullptr && currentStats_[intervalType]->beginTime_ == tmpCalendar.GetMilliseconds()) { continue; @@ -375,6 +378,24 @@ std::vector BundleActiveUserService::QueryEvents(const int64_ return result; } +int BundleActiveUserService::QueryFormStatistics(int32_t maxNum, std::vector& results) +{ + for (auto oneModuleRecord = moduleRecords_.begin(); oneModuleRecord != moduleRecords_.end(); oneModuleRecord++) { + if (!oneModuleRecord->second) { + continue; + } + results.emplace_back(*(oneModuleRecord->second)); + } + std::sort(results.begin(), results.end(), BundleActiveModuleRecord::cmp); + if (static_cast(results.size()) < maxNum) { + results.resize(maxNum); + } + for (auto result : results) { + std::sort(result.formRecords_.begin(), result.formRecords_.end(), BundleActiveFormRecord::cmp); + } + return 0; +} + void BundleActiveUserService::PrintInMemPackageStats(const int idx) { BUNDLE_ACTIVE_LOGI("PrintInMemPackageStats called"); @@ -406,6 +427,39 @@ void BundleActiveUserService::PrintInMemEventStats() abilityId.c_str(), abilityname.c_str(), bundlename.c_str(), eventid, timestamp); } } + +void BundleActiveUserService::ReportFormClickedOrRemoved(const BundleActiveEvent& event) +{ + BUNDLE_ACTIVE_LOGI("ReportFormClickedOrRemoved called"); + auto moduleRecord = GetOrCreateModuleRecord(event); + if (event.eventId_ == BundleActiveEvent::FORM_IS_CLICKED && moduleRecord) { + if (!moduleRecord) { + return; + } + moduleRecord->UpdateModuleRecord(event.timeStamp_); + moduleRecord->AddOrUpdateOneFormRecord(event.formName_, event.formDimension_, event.formId_, event.timeStamp_); + NotifyStatsChanged(); + } else if (event.eventId_ == BundleActiveEvent::FORM_IS_REMOVED && moduleRecord) { + moduleRecord->RemoveOneFormRecord(event.formName_, event.formDimension_, event.formId_); + database_.RemoveFormData(userId_, event.formName_, event.formDimension_, event.formId_); + NotifyStatsChanged(); + } +} + +std::shared_ptr BundleActiveUserService::GetOrCreateModuleRecord( + const BundleActiveEvent& event) +{ + std::string combinedInfo = event.bundleName_ + " " + event.moduleName_ + " " + event.modulePackage_; + auto it = moduleRecords_.find(combinedInfo); + if (it == moduleRecords_.end()) { + auto moduleRecordInserted = std::make_shared(); + moduleRecordInserted->bundleName_ = event.bundleName_; + moduleRecordInserted->moduleName_ = event.moduleName_; + moduleRecordInserted->modulePackage_ = event.modulePackage_; + moduleRecords_[combinedInfo] = moduleRecordInserted; + } + return moduleRecords_[combinedInfo]; +} } // namespace DeviceUsageStats } // namespace OHOS diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index bade74218609c68293f97c8f157d7a2e9654e920..943170a7a9eaec3531d9f3ccf061d7f46cc064f8 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -31,6 +31,11 @@ using namespace testing::ext; namespace OHOS { namespace DeviceUsageStats { static std::string DEFAULT_BUNDLENAME = "com.ohos.camera"; +static std::string DEFAULT_MODULENAME = "defaultmodulename"; +static std::string DEFAULT_MODULE = "defaultmodule"; +static std::string DEFAULT_FORM_NAME = "defaultformname"; +static int32_t DEFAULT_DIMENSION = 4; +static int64_t DEFAULT_FORMID = 1; static std::string DEFAULT_ABILITYID = "1234"; static std::string DEFAULT_ABILITYNAME = "testability"; static int DEFAULT_USERID = 0; @@ -85,12 +90,12 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_GetServiceObject_0 */ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_ReportEvent_001, Function | MediumTest | Level0) { - BundleActiveClient::GetInstance().ReportEvent(DEFAULT_BUNDLENAME, DEFAULT_ABILITYNAME, DEFAULT_ABILITYID, "", - DEFAULT_USERID, 2); - BundleActiveClient::GetInstance().ReportEvent(DEFAULT_BUNDLENAME, DEFAULT_ABILITYNAME, DEFAULT_ABILITYID, "", - DEFAULT_USERID, 3); - BundleActiveClient::GetInstance().ReportEvent(DEFAULT_BUNDLENAME, DEFAULT_ABILITYNAME, DEFAULT_ABILITYID, "", - DEFAULT_USERID, 4); + BundleActiveClient::GetInstance().ReportFormClickedOrRemoved(DEFAULT_BUNDLENAME, DEFAULT_MODULENAME, + DEFAULT_MODULE, DEFAULT_FORM_NAME, DEFAULT_DIMENSION, DEFAULT_FORMID, DEFAULT_USERID, + BundleActiveEvent::FORM_IS_CLICKED); + BundleActiveClient::GetInstance().ReportFormClickedOrRemoved(DEFAULT_BUNDLENAME, DEFAULT_MODULENAME, + DEFAULT_MODULE, DEFAULT_FORM_NAME, DEFAULT_DIMENSION, DEFAULT_FORMID, DEFAULT_USERID, + BundleActiveEvent::FORM_IS_REMOVED); } /*