diff --git a/BUILD.gn b/BUILD.gn deleted file mode 100644 index e30fadd37c70113321998108ac98b23de1ad2eea..0000000000000000000000000000000000000000 --- a/BUILD.gn +++ /dev/null @@ -1,210 +0,0 @@ -# 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. - -import("//build/ohos/sa_profile/sa_profile.gni") -import("device_usage_statistics.gni") - -ohos_sa_profile("device_usage_statistics_sa_profile") { - sources = [ "sa_profile/1907.xml" ] - part_name = "${device_usage_statistics_part_name}" -} - -config("usagestatsinner_public_config") { - include_dirs = [ - "services/common/include", - "services/packageusage/include", - "services/packagegroup/include", - "interfaces/innerkits/include", - "interfaces/kits/bundlestats/napi/include", - ] -} - -ohos_shared_library("usagestatsinner") { - sources = [ - "interfaces/innerkits/src/app_group_callback_info.cpp", - "interfaces/innerkits/src/app_group_callback_proxy.cpp", - "interfaces/innerkits/src/app_group_callback_stub.cpp", - "interfaces/innerkits/src/bundle_active_client.cpp", - "interfaces/innerkits/src/bundle_active_proxy.cpp", - "services/common/src/bundle_active_log.cpp", - "services/packageusage/src/bundle_active_event.cpp", - "services/packageusage/src/bundle_active_event_stats.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", - ] - public_configs = [ ":usagestatsinner_public_config" ] - external_deps = [ - "c_utils:utils", - "eventhandler:libeventhandler", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "samgr:samgr_proxy", - ] - - part_name = "${device_usage_statistics_part_name}" - subsystem_name = "resourceschedule" -} - -ohos_prebuilt_etc("device_usage_statistics_service_init") { - source = "init/device_usage_statistics_service.cfg" - relative_install_dir = "init" - part_name = "${device_usage_statistics_part_name}" - subsystem_name = "resourceschedule" -} - -ohos_shared_library("bundlestate") { - sources = [ - "frameworks/src/bundle_state_common.cpp", - "frameworks/src/bundle_state_init.cpp", - "frameworks/src/bundle_state_query.cpp", - ] - include_dirs = [ - "interfaces/kits/bundlestats/napi/include", - "services/common/include", - "interfaces/innerkits/include", - "services/packageusage/include", - "services/packagegroup/include", - "//base/notification/eventhandler/interfaces/inner_api", - ] - - deps = [ - ":usagestatservice", - ":usagestatsinner", - ] - - external_deps = [ - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "napi:ace_napi", - ] - relative_install_dir = "module" - part_name = "${device_usage_statistics_part_name}" - subsystem_name = "resourceschedule" -} - -ohos_shared_library("usagestatistics") { - sources = [ - "frameworks/src/app_group_observer_napi.cpp", - "frameworks/src/bundle_active_app_group_napi.cpp", - "frameworks/src/bundle_state_common.cpp", - "frameworks/src/bundle_state_query_napi.cpp", - "frameworks/src/usage_statistics_init.cpp", - ] - include_dirs = [ - "interfaces/kits/bundlestats/napi/include", - "services/common/include", - "interfaces/innerkits/include", - "services/packageusage/include", - "services/packagegroup/include", - "//base/notification/eventhandler/interfaces/inner_api", - ] - - deps = [ - ":usagestatservice", - ":usagestatsinner", - ] - - external_deps = [ - "c_utils:utils", - "hiviewdfx_hilog_native:libhilog", - "ipc:ipc_core", - "napi:ace_napi", - ] - relative_install_dir = "module/resourceschedule" - part_name = "${device_usage_statistics_part_name}" - subsystem_name = "resourceschedule" -} - -ohos_shared_library("usagestatservice") { - cflags_cc = [] - sources = [ - "interfaces/innerkits/src/app_group_callback_info.cpp", - "interfaces/innerkits/src/app_group_callback_proxy.cpp", - "services/common/src/bundle_active_account_helper.cpp", - "services/common/src/bundle_active_app_state_obsever.cpp", - "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_power_state_callback_proxy.cpp", - "services/common/src/bundle_active_power_state_callback_service.cpp", - "services/common/src/bundle_active_power_state_callback_stub.cpp", - "services/common/src/bundle_active_service.cpp", - "services/common/src/bundle_active_shutdown_callback_proxy.cpp", - "services/common/src/bundle_active_shutdown_callback_service.cpp", - "services/common/src/bundle_active_shutdown_callback_stub.cpp", - "services/common/src/bundle_active_stub.cpp", - "services/common/src/bundle_active_usage_database.cpp", - "services/packagegroup/src/bundle_active_group_controller.cpp", - "services/packagegroup/src/bundle_active_group_handler.cpp", - "services/packagegroup/src/bundle_active_user_history.cpp", - "services/packageusage/src/bundle_active_calendar.cpp", - "services/packageusage/src/bundle_active_event.cpp", - "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", - "services/packageusage/src/bundle_active_stats_combiner.cpp", - "services/packageusage/src/bundle_active_user_service.cpp", - ] - include_dirs = [ - ":bundle_active_config", - "services/common/include", - "services/packageusage/include", - "services/packagegroup/include", - "interfaces/innerkits/include", - "interfaces/kits/bundlestats/napi/include", - ] - - configs = [ "//foundation/ability/ability_runtime/interfaces/inner_api/wantagent:wantagent_innerkits_public_config" ] - - external_deps = [ - "ability_base:want", - "ability_runtime:app_manager", - "access_token:libaccesstoken_sdk", - "bundle_framework:appexecfwk_base", - "bundle_framework:appexecfwk_core", - "c_utils:utils", - "common_event_service:cesfwk_innerkits", - "eventhandler:libeventhandler", - "hiviewdfx_hilog_native:libhilog", - "init:libbegetutil", - "ipc:ipc_core", - "power_manager:powermgr_client", - "relational_store:native_rdb", - "safwk:system_ability_fwk", - "samgr:samgr_proxy", - "time_service:time_client", - ] - - if (os_account_part_enabled) { - cflags_cc += [ "-DOS_ACCOUNT_PART_ENABLED" ] - external_deps += [ "os_account:os_account_innerkits" ] - } - - defines = [] - if (bgtaskmgr_enable) { - external_deps += [ "background_task_mgr:bgtaskmgr_innerkits" ] - defines += [ "BGTASKMGR_ENABLE" ] - } - part_name = "${device_usage_statistics_part_name}" - subsystem_name = "resourceschedule" -} diff --git a/interfaces/innerkits/include/bundle_active_form_record.h b/interfaces/innerkits/include/bundle_active_form_record.h new file mode 100644 index 0000000000000000000000000000000000000000..ca105b68f6b60b6986aeeb19e2e07f776fdce701 --- /dev/null +++ b/interfaces/innerkits/include/bundle_active_form_record.h @@ -0,0 +1,105 @@ +/* + * 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 +#include +#include "parcel.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_; + +public: + /* + * function: BundleActiveFormRecord, copy constructor. + * parameters: orig + */ + BundleActiveFormRecord(const BundleActiveFormRecord& orig); + /* + * function: BundleActiveFormRecord, default constructor. + */ + BundleActiveFormRecord(); + /* + * function: BundleActiveFormRecord, use formName, formDimension, formId, timeStamp, userId to construct object. + * parameters: formName, formDimension, formId, timeStamp, userId + */ + BundleActiveFormRecord(const std::string formName, const int32_t formDimension, const int64_t formId, + const int64_t timeStamp, const int32_t userId); + /* + * function: ~BundleActiveFormRecord, default destructor. + */ + ~BundleActiveFormRecord() {} + /* + * function: UpdateFormRecord, update form record by timestamp. + * parameters: timeStamp + */ + void UpdateFormRecord(const int64_t timeStamp); + /* + * function: operator==, override operator ==. + * parameters: formRecord + * return: true if the formId_ is same. + */ + bool operator==(const BundleActiveFormRecord& formRecord) const + { + return (this->formId_ == formRecord.formId_); + } + /* + * function: operator==, override operator ==. + * parameters: formId + * return: true if the formId_ is same. + */ + bool operator==(const int64_t formId) const + { + return (this->formId_ == formId); + } + /* + * function: cmp, compare two BundleActiveFormRecord. + * parameters: formRecordA, formRecordB + * return: true if formRecordA.count_ > formRecordB. + */ + static bool cmp(const BundleActiveFormRecord& formRecordA, const BundleActiveFormRecord& formRecordB); + /* + * function: Marshalling, mashalling form record object to parcel. + * parameters: parcel + * return: result of mashalling, true means successful, flase means failed. + */ + bool Marshalling(Parcel &parcel) const override; + /* + * function: UnMarshalling, Unmashalling record object from parcel. + * parameters: parcel + * return: point to a BundleActiveFormRecord. + */ + static std::shared_ptr UnMarshalling(Parcel &parcel); + /* + * function: ToString, change form record object to string. + * return: string of form name, form id, form dimension, last used time and touch count. + */ + std::string ToString(); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_FORM_RECORD_H + diff --git a/interfaces/innerkits/include/bundle_active_module_record.h b/interfaces/innerkits/include/bundle_active_module_record.h new file mode 100644 index 0000000000000000000000000000000000000000..98f1701299d35f92332184b845e85572dd91706e --- /dev/null +++ b/interfaces/innerkits/include/bundle_active_module_record.h @@ -0,0 +1,58 @@ +/* + * 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 "bundle_active_form_record.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(); + ~BundleActiveModuleRecord() {} + static bool cmp(const BundleActiveModuleRecord& moduleRecordA, const BundleActiveModuleRecord& moduleRecordB); + bool Marshalling(Parcel &parcel) const override; + static std::shared_ptr UnMarshalling(Parcel &parcel); + std::string ToString(); + +public: + std::string deviceId_; + std::string bundleName_; + std::string moduleName_; + std::string abilityName_; + uint32_t appLabelId_; + uint32_t labelId_; + uint32_t descriptionId_; + uint32_t abilityLableId_; + uint32_t abilityDescriptionId_; + uint32_t abilityIconId_; + int32_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/interfaces/innerkits/src/bundle_active_module_record.cpp b/interfaces/innerkits/src/bundle_active_module_record.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e23ae2bd7883ec5d4fd087c6cec7579b0471af0c --- /dev/null +++ b/interfaces/innerkits/src/bundle_active_module_record.cpp @@ -0,0 +1,141 @@ +/* + * 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 { +namespace { + static const int32_t MAX_FORM_NUM = 1000; +} +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 + abilityName_ = ""; + appLabelId_ = 0; + labelId_ = 0; + descriptionId_ = 0; + abilityLableId_ = 0; + abilityDescriptionId_ = 0; + abilityIconId_ = 0; + launchedCount_ = 0; + lastModuleUsedTime_ = -1; + 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(abilityName_) && + parcel.WriteUint32(appLabelId_) && + parcel.WriteUint32(descriptionId_) && + parcel.WriteUint32(abilityLableId_) && + parcel.WriteUint32(abilityDescriptionId_) && + parcel.WriteUint32(abilityIconId_) && + parcel.WriteInt32(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->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.ReadInt32(); + result->lastModuleUsedTime_ = parcel.ReadInt64(); + uint32_t size = parcel.ReadUint32(); + if (size > MAX_FORM_NUM) { + return nullptr; + } + 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_; +} + +std::string BundleActiveModuleRecord::ToString() +{ + return "bundle name is " + this->bundleName_ + + ", module name is " + this->moduleName_ + + ", last used time stamp is " + std::to_string(this->lastModuleUsedTime_) + + ", module is used for " + std::to_string(this->launchedCount_) + " times\n"; +} +} // namespace DeviceUsageStats +} // namespace OHOS diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 4b01289dcce2a27653f725347754a4a8a3275348..e9186a1dfb3db2277c5f66bb9612fb64e5504ed5 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -63,6 +63,9 @@ ErrCode BundleActiveProxy::QueryBundleStatsInfoByInterval(std::vector SendRequest(QUERY_BUNDLE_STATS_INFO_BY_INTERVAL, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { @@ -97,6 +100,9 @@ ErrCode BundleActiveProxy::QueryBundleEvents(std::vector& bun data.WriteInt32(userId); Remote() -> SendRequest(QUERY_BUNDLE_EVENTS, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { @@ -142,6 +148,9 @@ ErrCode BundleActiveProxy::QueryBundleStatsInfos(std::vector SendRequest(QUERY_BUNDLE_STATS_INFOS, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { @@ -177,6 +186,9 @@ ErrCode BundleActiveProxy::QueryCurrentBundleEvents(std::vector SendRequest(QUERY_CURRENT_BUNDLE_EVENTS, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { @@ -224,6 +236,9 @@ ErrCode BundleActiveProxy::QueryModuleUsageRecords(int32_t maxNum, std::vector SendRequest(QUERY_MODULE_USAGE_RECORDS, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { @@ -327,6 +342,9 @@ ErrCode BundleActiveProxy::IPCCommunication(int64_t beginTime, int64_t endTime, data.WriteInt32(userId); Remote() -> SendRequest(communicationFlag, data, reply, option); ErrCode errCode = reply.ReadInt32(); + if (errCode == ERR_QUERY_RESULT_TOO_LARGE) { + return errCode; + } int32_t size = reply.ReadInt32(); std::shared_ptr tmp; for (int32_t i = 0; i < size; i++) { diff --git a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h index acd6418089e7483b6bca3aab453aa26cff208f5b..e9eb3ee53dad583a4b9c1729a565c72c42e99b07 100644 --- a/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h +++ b/interfaces/kits/bundlestats/napi/include/bundle_state_inner_errors.h @@ -102,6 +102,7 @@ enum ServiceError { ERR_NO_RECORDS_INFO_BY_INTERVAL, ERR_QUERY_TIME_OUT_OF_RANGE, ERR_GET_BUNDLE_USED_HISTORY_FAILED, + ERR_QUERY_RESULT_TOO_LARGE, ERR_REPEAT_REGISTER_OR_DEREGISTER_GROUP_CALLBACK = 1010000101, ERR_REPEAT_SET_APP_GROUP, }; @@ -123,6 +124,9 @@ static std::map saErrCodeMsgMap = { "According to inetrvalType. no information is recorded in the related table."}, {ERR_QUERY_TIME_OUT_OF_RANGE, "Failed to get the application information. According to beginTime and endTime. no information is recorded"}, + {ERR_QUERY_RESULT_TOO_LARGE, + "Query events max size is 100000, Query package stats max result size is 1000." + "Query result is too large, please narrow query time span."}, {ERR_GET_BUNDLE_USED_HISTORY_FAILED, "Failed to get the application information. According to userId、bundleName, no information is recorded"}, {ERR_TIME_OPERATION_FAILED, "Get system or actual time operation failed."}, diff --git a/services/common/include/bundle_active_bundle_mgr_helper.h b/services/common/include/bundle_active_bundle_mgr_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..0a181197170947acd58dc61b8f49b98f96ae3dbb --- /dev/null +++ b/services/common/include/bundle_active_bundle_mgr_helper.h @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 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_BUNDLE_MGR_HELPER_H +#define BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H + +#include "errors.h" +#include "bundle_mgr_interface.h" +#include "bundle_mgr_proxy.h" +#include "ipc_skeleton.h" +#include "iremote_object.h" +#include "singleton.h" + +#include "bundle_active_log.h" + +namespace OHOS { +namespace DeviceUsageStats { +class BundleActiveBundleMgrHelper : public DelayedSingleton { +public: + /** + * @brief GetNameForUid by uid. + * + * @param uid . + * @param bundleName . + */ + void GetNameForUid(int32_t uid, std::string& bundleName); + + /** + * @brief GetBundleInfo by bundleName、flag、userId. + * + * @param bundleName . + * @param flag which type is AppExecFwk::ApplicationFlag. + * @param bundleInfo. + * @param userId which type is AppExecFwk::BundleInfo. + */ + bool GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, + AppExecFwk::BundleInfo &bundleInfo, int32_t userId); + + /** + * @brief GetApplicationInfo, get bundleName by uid. + * + * @param appName . + * @param flag which type is AppExecFwk::ApplicationFlag. + * @param userId. + * @param appInfo which type is AppExecFwk::ApplicationInfo. + */ + bool GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, + const int userId, AppExecFwk::ApplicationInfo &appInfo); + +private: + bool Connect(); + +private: + sptr bundleMgr_ = nullptr; + std::mutex connectionMutex_; + + DECLARE_DELAYED_SINGLETON(BundleActiveBundleMgrHelper); +}; +} // namespace DeviceUsageStats +} // namespace OHOS +#endif // BUNDLE_ACTIVE_BUNDLE_MGR_HELPER_H diff --git a/services/common/include/bundle_active_core.h b/services/common/include/bundle_active_core.h index dfba3f148b72a89e55fcfac081a4da7c9405b076..bad1bb3525cfa54fc9fd1792d2f76cdcf2773cf2 100644 --- a/services/common/include/bundle_active_core.h +++ b/services/common/include/bundle_active_core.h @@ -241,8 +241,7 @@ private: int64_t systemTimeShot_; int64_t realTimeShot_; std::mutex mutex_; - std::mutex callbackMutex_; - std::mutex deathRecipientMutex_; + std::recursive_mutex callbackMutex_; std::map> userStatServices_; void RegisterSubscriber(); std::shared_ptr commonEventSubscriber_; diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 5f186a5c991ce910e88e25f0745ae5e2bd81aeac..9997602de529c874f2b3fb92d8f12cddaf17c492 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -16,7 +16,6 @@ #ifndef BUNDLE_ACTIVE_SERVICE_H #define BUNDLE_ACTIVE_SERVICE_H -#include "bundle_mgr_interface.h" #include "singleton.h" #include "app_mgr_interface.h" @@ -224,7 +223,6 @@ private: int32_t ConvertIntervalType(const int32_t intervalType); void InitNecessaryState(); void InitService(); - ErrCode GetBundleMgrProxy(); ErrCode CheckBundleIsSystemAppAndHasPermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId); ErrCode CheckSystemAppOrNativePermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId); diff --git a/services/common/include/bundle_active_stub.h b/services/common/include/bundle_active_stub.h index a3c5983441ad32ab4b4162d355d9b5f8de2673d6..83ef05c23734acbcdde8ab106cf8a98ed9523971 100644 --- a/services/common/include/bundle_active_stub.h +++ b/services/common/include/bundle_active_stub.h @@ -34,6 +34,20 @@ public: */ int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) override; +private: + ErrCode HandleReportEvent(MessageParcel& data, MessageParcel& reply); + ErrCode HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleStatsInfoByInterval(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleEvents(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryBundleStatsInfos(MessageParcel& data, MessageParcel& reply); + ErrCode HandleSetAppGroup(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryCurrentBundleEvents(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryModuleUsageRecords(MessageParcel &data, MessageParcel &reply); + ErrCode HandleQueryAppGroup(MessageParcel& data, MessageParcel& reply); + ErrCode HandleRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply); + ErrCode HandleUnRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryDeviceEventStats(MessageParcel& data, MessageParcel& reply); + ErrCode HandleQueryNotificationEventStats(MessageParcel& data, MessageParcel& reply); }; } // namespace DeviceUsageStats } // namespace OHOS diff --git a/services/common/include/ibundle_active_service_ipc_interface_code.h b/services/common/include/ibundle_active_service_ipc_interface_code.h new file mode 100644 index 0000000000000000000000000000000000000000..73771e1b80def6cf96d1ef7468710fcdb3a83920 --- /dev/null +++ b/services/common/include/ibundle_active_service_ipc_interface_code.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 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_SERVICE_IPC_INTERFACE_CODE_H +#define BUNDLE_ACTIVE_SERVICE_IPC_INTERFACE_CODE_H + + /* SAID: 1907 */ +namespace OHOS { +namespace DeviceUsageStats { + enum class IBundleActiveServiceInterfaceCode { + REPORT_EVENT = 1, + IS_BUNDLE_IDLE = 2, + QUERY_BUNDLE_STATS_INFO_BY_INTERVAL = 3, + QUERY_BUNDLE_EVENTS = 4, + QUERY_BUNDLE_STATS_INFOS = 5, + QUERY_CURRENT_BUNDLE_EVENTS = 6, + QUERY_APP_GROUP = 7, + SET_APP_GROUP = 8, + QUERY_MODULE_USAGE_RECORDS = 9, + REGISTER_APP_GROUP_CALLBACK = 10, + UNREGISTER_APP_GROUP_CALLBACK = 11, + QUERY_DEVICE_EVENT_STATES = 12, + QUERY_NOTIFICATION_NUMBER = 13 + }; +} // namespace BackgroundTaskMgr +} // namespace OHOS +#endif // BUNDLE_ACTIVE_SERVICE_IPC_INTERFACE_CODE_H \ No newline at end of file diff --git a/services/common/src/bundle_active_bundle_mgr_helper.cpp b/services/common/src/bundle_active_bundle_mgr_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..dd2328680cd12001b8ffd3728bc16c6189a81486 --- /dev/null +++ b/services/common/src/bundle_active_bundle_mgr_helper.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2023 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_bundle_mgr_helper.h" + +#include "accesstoken_kit.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" +#include "tokenid_kit.h" + +#include "continuous_task_log.h" + +namespace OHOS { +namespace DeviceUsageStats { +using OHOS::AppExecFwk::Constants::PERMISSION_GRANTED; + +BundleActiveBundleMgrHelper::BundleActiveBundleMgrHelper() +{ +} + +BundleActiveBundleMgrHelper::~BundleActiveBundleMgrHelper() +{ +} + +void BundleActiveBundleMgrHelper::GetNameForUid(int32_t uid, std::string& bundleName) +{ + std::lock_guard lock(connectionMutex_); + if (!Connect()) { + return; + } + if (!bundleMgr_) { + return; + } + bundleMgr_->GetNameForUid(uid, bundleName); + BUNDLE_ACTIVE_LOGD("get Bundle Name: %{public}s", bundleName.c_str()); +} + +bool BundleActiveBundleMgrHelper::GetApplicationInfo(const std::string &appName, const AppExecFwk::ApplicationFlag flag, + const int userId, AppExecFwk::ApplicationInfo &appInfo) +{ + BUNDLE_ACTIVE_LOGD("start get application info"); + std::lock_guard lock(connectionMutex_); + + if (!Connect()) { + return false; + } + if (!bundleMgr_) { + return false; + } + BUNDLE_ACTIVE_LOGD("bundleMgr is null: %{public}d ", bundleMgr_ == nullptr); + if (bundleMgr_ != nullptr && bundleMgr_->GetApplicationInfo(appName, flag, userId, appInfo)) { + return true; + } + return false; +} + +bool BundleActiveBundleMgrHelper::GetBundleInfo(const std::string &bundleName, const AppExecFwk::BundleFlag flag, + AppExecFwk::BundleInfo &bundleInfo, int32_t userId) +{ + std::lock_guard lock(connectionMutex_); + + if (!Connect()) { + return false; + } + if (!bundleMgr_) { + return false; + } + if (bundleMgr_ != nullptr && bundleMgr_->GetBundleInfo(bundleName, flag, bundleInfo, userId)) { + return true; + } + return false; +} + +bool BundleActiveBundleMgrHelper::Connect() +{ + if (bundleMgr_ != nullptr) { + return true; + } + + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + BUNDLE_ACTIVE_LOGE("get SystemAbilityManager failed"); + return false; + } + + sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (remoteObject == nullptr) { + BUNDLE_ACTIVE_LOGE("get Bundle Manager failed"); + return false; + } + + bundleMgr_ = iface_cast(remoteObject); + return bundleMgr_ ? true : false; +} +} // namespace DeviceUsageStats +} // namespace OHOS \ No newline at end of file diff --git a/services/common/src/bundle_active_core.cpp b/services/common/src/bundle_active_core.cpp index 5f27292be0ee63f06ffca73aae3805ca1b35e872..efc1d2248d820169b6a802cf02145f7781e39f73 100644 --- a/services/common/src/bundle_active_core.cpp +++ b/services/common/src/bundle_active_core.cpp @@ -694,7 +694,7 @@ int64_t BundleActiveCore::GetSystemTimeMs() void BundleActiveCore::OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); AccessToken::HapTokenInfo tokenInfo = AccessToken::HapTokenInfo(); for (const auto &item : groupChangeObservers_) { auto observer = item.second; @@ -719,7 +719,7 @@ void BundleActiveCore::OnAppGroupChanged(const AppGroupCallbackInfo& callbackInf ErrCode BundleActiveCore::RegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { return ERR_MEMORY_OPERATION_FAILED; } @@ -736,7 +736,7 @@ ErrCode BundleActiveCore::RegisterAppGroupCallBack(const AccessToken::AccessToke ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, const sptr &observer) { - std::lock_guard lock(callbackMutex_); + std::lock_guard lock(callbackMutex_); auto item = groupChangeObservers_.find(tokenId); if (item == groupChangeObservers_.end()) { BUNDLE_ACTIVE_LOGI("UnRegisterAppGroupCallBack observer is not exist, return"); @@ -749,7 +749,7 @@ ErrCode BundleActiveCore::UnRegisterAppGroupCallBack(const AccessToken::AccessTo void BundleActiveCore::AddObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(deathRecipientMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { BUNDLE_ACTIVE_LOGI("observer nullptr."); return; @@ -775,7 +775,7 @@ void BundleActiveCore::AddObserverDeathRecipient(const sptr & } void BundleActiveCore::RemoveObserverDeathRecipient(const sptr &observer) { - std::lock_guard lock(deathRecipientMutex_); + std::lock_guard lock(callbackMutex_); if (!observer) { return; } @@ -802,12 +802,12 @@ void BundleActiveCore::OnObserverDied(const wptr &remote) void BundleActiveCore::OnObserverDiedInner(const wptr &remote) { - std::lock_guard lock(deathRecipientMutex_); sptr objectProxy = remote.promote(); if (remote == nullptr || !objectProxy) { BUNDLE_ACTIVE_LOGE("get remote object failed"); return; } + std::lock_guard lock(callbackMutex_); for (const auto& item : groupChangeObservers_) { if (!(item.second)) { continue; diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 5ad925528376b5c08bd4e3ff257431596ea2730f..a2d66a5f2509f5ff772f46662fdce52b8f636ee0 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -15,7 +15,6 @@ #include "time_service_client.h" #include "power_mgr_client.h" -#include "bundle_mgr_proxy.h" #include "unistd.h" #include "accesstoken_kit.h" @@ -23,6 +22,9 @@ #include "bundle_active_event.h" #include "bundle_active_package_stats.h" #include "bundle_active_account_helper.h" +#include "bundle_active_bundle_mgr_helper.h" +#include "tokenid_kit.h" + #include "bundle_active_service.h" namespace OHOS { @@ -266,13 +268,9 @@ ErrCode BundleActiveService::IsBundleIdle(bool& isBundleIdle, const std::string& // get uid int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); - ErrCode ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("IsBundleIdle Get bundle manager proxy failed!"); - return ret; - } + ErrCode ret = ERR_OK; std::string callingBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, callingBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, callingBundleName); BUNDLE_ACTIVE_LOGI("UID is %{public}d, bundle name is %{public}s", callingUid, callingBundleName.c_str()); // get user id int32_t result = -1; @@ -285,7 +283,7 @@ ErrCode BundleActiveService::IsBundleIdle(bool& isBundleIdle, const std::string& if (callingBundleName == bundleName) { if (!sptrBundleMgr_->CheckIsSystemAppByUid(callingUid)) { - BUNDLE_ACTIVE_LOGE("%{public}s is not sys app", bundleName.c_str()); + BUNDLE_ACTIVE_LOGE("%{public}s is not system app", bundleName.c_str()); return ERR_NOT_SYSTEM_APP; } BUNDLE_ACTIVE_LOGI("%{public}s check its own idle state", bundleName.c_str()); @@ -364,13 +362,8 @@ ErrCode BundleActiveService::SetAppGroup(const std::string& bundleName, int32_t } BUNDLE_ACTIVE_LOGI("SetAppGroup userId is %{public}d", userId); - ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("SetAppGroup Get bundle manager proxy failed!"); - return ret; - } std::string localBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, localBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, localBundleName); if (localBundleName == bundleName) { BUNDLE_ACTIVE_LOGI("SetAppGroup can not set its bundleName"); return ERR_PERMISSION_DENIED; @@ -395,13 +388,8 @@ ErrCode BundleActiveService::QueryBundleStatsInfos(std::vectorGetBundleNameForUid(callingUid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); ErrCode isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId); if (!bundleName.empty() && isSystemAppAndHasPermission == ERR_OK) { int32_t convertedIntervalType = ConvertIntervalType(intervalType); @@ -422,17 +410,12 @@ ErrCode BundleActiveService::QueryCurrentBundleEvents(std::vectorGetBundleNameForUid(callingUid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, bundleName); if (!sptrBundleMgr_->CheckIsSystemAppByUid(callingUid)) { - BUNDLE_ACTIVE_LOGE("%{public}s is not sys app", bundleName.c_str()); - return ERR_NOT_SYSTEM_APP; - } + BUNDLE_ACTIVE_LOGE("%{public}s is not system app", bundleName.c_str()); + return ERR_NOT_SYSTEM_APP; + } if (!bundleName.empty()) { BUNDLE_ACTIVE_LOGI("QueryCurrentBundleEvents buindle name is %{public}s", bundleName.c_str()); @@ -456,15 +439,10 @@ ErrCode BundleActiveService::QueryAppGroup(int32_t& appGroup, std::string& bundl } } if (bundleName.empty()) { - ret = GetBundleMgrProxy(); - if (ret != ERR_OK) { - BUNDLE_ACTIVE_LOGE("QueryAppGroup Get bundle manager proxy failed!"); - return ret; - } std::string localBundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(callingUid, localBundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(callingUid, localBundleName); if (!sptrBundleMgr_->CheckIsSystemAppByUid(callingUid)) { - BUNDLE_ACTIVE_LOGE("%{public}s is not sys app", localBundleName.c_str()); + BUNDLE_ACTIVE_LOGE("%{public}s is not system app", localBundleName.c_str()); return ERR_NOT_SYSTEM_APP; } bundleName = localBundleName; @@ -507,34 +485,6 @@ ErrCode BundleActiveService::UnRegisterAppGroupCallBack(const sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - BUNDLE_ACTIVE_LOGE("Failed to get system ability mgr."); - return ERR_GET_SYSTEM_ABILITY_MANAGER_FAILED; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - BUNDLE_ACTIVE_LOGE("Failed to get bundle manager service."); - return ERR_GET_SYSTEM_ABILITY_FAILED; - } - sptrBundleMgr_ = iface_cast(remoteObject); - if (!sptrBundleMgr_) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, sptrBundleMgr_"); - return ERR_REMOTE_OBJECT_IF_CAST_FAILED; - } - auto object = sptrBundleMgr_->AsObject(); - if (!object) { - BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, sptrBundleMgr_->AsObject()"); - return ERR_REMOTE_OBJECT_IF_CAST_FAILED; - } - } - return ERR_OK; -} - int32_t BundleActiveService::ConvertIntervalType(const int32_t intervalType) { if (intervalType == PERIOD_BEST_JS) { @@ -548,13 +498,8 @@ int32_t BundleActiveService::ConvertIntervalType(const int32_t intervalType) ErrCode BundleActiveService::CheckBundleIsSystemAppAndHasPermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId) { - ErrCode errCode = GetBundleMgrProxy(); - if (errCode != ERR_OK) { - BUNDLE_ACTIVE_LOGE("CheckBundleIsSystemAppAndHasPermission Get bundle manager proxy failed!"); - return errCode; - } std::string bundleName = ""; - sptrBundleMgr_->GetBundleNameForUid(uid, bundleName); + BundleActiveBundleMgrHelper::GetInstance()->GetNameForUid(uid, bundleName); int32_t bundleHasPermission = AccessToken::AccessTokenKit::VerifyAccessToken(tokenId, NEEDED_PERMISSION); if (bundleHasPermission != 0) { @@ -658,19 +603,15 @@ ErrCode BundleActiveService::QueryNotificationEventStats(int64_t beginTime, int6 void BundleActiveService::QueryModuleRecordInfos(BundleActiveModuleRecord& moduleRecord) { - ErrCode errCode = GetBundleMgrProxy(); - if (errCode != ERR_OK) { - return; - } ApplicationInfo appInfo; - bool getInfoIsSuccess = sptrBundleMgr_->GetApplicationInfo(moduleRecord.bundleName_, + bool getInfoIsSuccess = BundleActiveBundleMgrHelper::GetInstance()->GetApplicationInfo(moduleRecord.bundleName_, ApplicationFlag::GET_BASIC_APPLICATION_INFO, moduleRecord.userId_, appInfo); if (!getInfoIsSuccess) { BUNDLE_ACTIVE_LOGE("GetApplicationInfo failed!"); return; } BundleInfo bundleInfo; - getInfoIsSuccess = sptrBundleMgr_->GetBundleInfo(moduleRecord.bundleName_, + getInfoIsSuccess = BundleActiveBundleMgrHelper::GetInstance()->GetBundleInfo(moduleRecord.bundleName_, BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, moduleRecord.userId_); if (!getInfoIsSuccess) { BUNDLE_ACTIVE_LOGE("GetBundleInfo failed!"); @@ -800,4 +741,3 @@ void BundleActiveService::DumpUsage(std::string &result) } } // namespace DeviceUsageStats } // namespace OHOS - diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index 0408f068aa6cbb8bec5273c7ebf76c155cb142c5..a463e6c980607e6f3113a5c6520b6b1689ed236d 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -25,10 +25,15 @@ #include "bundle_active_module_record.h" #include "bundle_active_package_stats.h" #include "iapp_group_callback.h" - +#include "ibundle_active_service_ipc_interface_code.h" namespace OHOS { namespace DeviceUsageStats { +namespace { + constexpr int32_t EVENT_MAX_SIZE = 100000; + constexpr int32_t PACKAGE_MAX_SIZE = 1000; +} + int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply, MessageOption &option) { @@ -36,183 +41,282 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me return -1; } switch (code) { - case REPORT_EVENT: { - int32_t userId = data.ReadInt32(); - std::shared_ptr tmpEvent = BundleActiveEvent::UnMarshalling(data); - if (!tmpEvent) { - return -1; - } - int32_t result = ReportEvent(*tmpEvent, userId); - return reply.WriteInt32(result); - } - case IS_BUNDLE_IDLE: { - bool isBundleIdle = false; - std::string bundleName = data.ReadString(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId); - reply.WriteInt32(isBundleIdle); - return reply.WriteInt32(errCode); - } - case QUERY_BUNDLE_STATS_INFO_BY_INTERVAL: { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId); - reply.WriteInt32(errCode); - int32_t size = static_cast(result.size()); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case QUERY_BUNDLE_EVENTS: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId); - int32_t size = static_cast(result.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case SET_APP_GROUP: { - std::string bundleName = data.ReadString(); - int32_t newGroup = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = SetAppGroup(bundleName, newGroup, userId); - return reply.WriteInt32(errCode); - } - case QUERY_BUNDLE_STATS_INFOS: { - std::vector result; - int32_t intervalType = data.ReadInt32(); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType); - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime); - int32_t size = static_cast(result.size()); - BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case QUERY_CURRENT_BUNDLE_EVENTS: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime); - int32_t size = static_cast(result.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case QUERY_APP_GROUP: { - int32_t appGroup = -1; - std::string bundleName = data.ReadString(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId); - reply.WriteInt32(appGroup); - return reply.WriteInt32(errCode); - } - case QUERY_MODULE_USAGE_RECORDS: { - std::vector results; - int32_t maxNum = data.ReadInt32(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId); - int32_t size = static_cast(results.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = results[i].Marshalling(reply); - if (tmp == false) { - return 1; - } - } - return size == 0; - } - case REGISTER_APP_GROUP_CALLBACK: { - auto observer = iface_cast(data.ReadRemoteObject()); - if (!observer) { - BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return"); - return false; - } - BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok"); - ErrCode errCode = RegisterAppGroupCallBack(observer); - return reply.WriteInt32(errCode); - } - case UNREGISTER_APP_GROUP_CALLBACK: { - auto observer = iface_cast(data.ReadRemoteObject()); - if (!observer) { - BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return"); - return false; - } - ErrCode errCode = UnRegisterAppGroupCallBack(observer); - return reply.WriteInt32(errCode); - } - case QUERY_DEVICE_EVENT_STATES: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId); - int32_t size = static_cast(result.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (!tmp) { - return 1; - } - } - return size == 0; - } - case QUERY_NOTIFICATION_NUMBER: { - std::vector result; - int64_t beginTime = data.ReadInt64(); - int64_t endTime = data.ReadInt64(); - int32_t userId = data.ReadInt32(); - ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId); - int32_t size = static_cast(result.size()); - reply.WriteInt32(errCode); - reply.WriteInt32(size); - for (int32_t i = 0; i < size; i++) { - bool tmp = result[i].Marshalling(reply); - if (!tmp) { - return 1; - } - } - return size == 0; + case static_cast(IBundleActiveServiceInterfaceCode::REPORT_EVENT): { + return HandleReportEvent(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::IS_BUNDLE_IDLE): { + return HandleIsBundleIdle(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFO_BY_INTERVAL): { + return HandleQueryBundleStatsInfoByInterval(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_EVENTS): { + return HandleQueryBundleEvents(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::SET_APP_GROUP): { + return HandleSetAppGroup(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFOS): { + return HandleQueryBundleStatsInfos(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_CURRENT_BUNDLE_EVENTS): { + return HandleQueryCurrentBundleEvents(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_APP_GROUP): { + return HandleQueryAppGroup(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_MODULE_USAGE_RECORDS): { + return HandleQueryModuleUsageRecords(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::REGISTER_APP_GROUP_CALLBACK): { + return HandleRegisterAppGroupCallBack(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::UNREGISTER_APP_GROUP_CALLBACK): { + return HandleUnRegisterAppGroupCallBack(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_DEVICE_EVENT_STATES): { + return HandleQueryDeviceEventStats(data, reply); + } + case static_cast(IBundleActiveServiceInterfaceCode::QUERY_NOTIFICATION_NUMBER): { + return HandleQueryNotificationEventStats(data, reply); } default: return IPCObjectStub::OnRemoteRequest(code, data, reply, option); } + return ERR_OK; +} + +ErrCode BundleActiveStub::HandleReportEvent(MessageParcel& data, MessageParcel& reply) +{ + int32_t userId = data.ReadInt32(); + std::shared_ptr tmpEvent = BundleActiveEvent::UnMarshalling(data); + if (!tmpEvent) { + return -1; + } + int32_t result = ReportEvent(*tmpEvent, userId); + return reply.WriteInt32(result); +} + +ErrCode BundleActiveStub::HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply) +{ + bool isBundleIdle = false; + std::string bundleName = data.ReadString(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId); + reply.WriteInt32(isBundleIdle); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryBundleStatsInfoByInterval(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryBundleEvents(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryBundleStatsInfos(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int32_t intervalType = data.ReadInt32(); + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType); + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size); + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} +ErrCode BundleActiveStub::HandleSetAppGroup(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName = data.ReadString(); + int32_t newGroup = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = SetAppGroup(bundleName, newGroup, userId); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryCurrentBundleEvents(MessageParcel &data, MessageParcel &reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; } -} // namespace DeviceUsageStats -} // namespace OHOS +ErrCode BundleActiveStub::HandleQueryModuleUsageRecords(MessageParcel &data, MessageParcel &reply) +{ + std::vector results; + int32_t maxNum = data.ReadInt32(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId); + int32_t size = static_cast(results.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = results[i].Marshalling(reply); + if (tmp == false) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryAppGroup(MessageParcel& data, MessageParcel& reply) +{ + int32_t appGroup = -1; + std::string bundleName = data.ReadString(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId); + reply.WriteInt32(appGroup); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply) +{ + auto observer = iface_cast(data.ReadRemoteObject()); + if (!observer) { + BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return"); + return false; + } + BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok"); + ErrCode errCode = RegisterAppGroupCallBack(observer); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleUnRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply) +{ + auto observer = iface_cast(data.ReadRemoteObject()); + if (!observer) { + BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return"); + return false; + } + ErrCode errCode = UnRegisterAppGroupCallBack(observer); + return reply.WriteInt32(errCode); +} + +ErrCode BundleActiveStub::HandleQueryDeviceEventStats(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId); + int32_t size = static_cast(result.size()); + if (size > EVENT_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (!tmp) { + return 1; + } + } + return size == 0; +} + +ErrCode BundleActiveStub::HandleQueryNotificationEventStats(MessageParcel& data, MessageParcel& reply) +{ + std::vector result; + int64_t beginTime = data.ReadInt64(); + int64_t endTime = data.ReadInt64(); + int32_t userId = data.ReadInt32(); + ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId); + int32_t size = static_cast(result.size()); + if (size > PACKAGE_MAX_SIZE) { + errCode = ERR_QUERY_RESULT_TOO_LARGE; + reply.WriteInt32(errCode); + return -1; + } + reply.WriteInt32(errCode); + reply.WriteInt32(size); + for (int32_t i = 0; i < size; i++) { + bool tmp = result[i].Marshalling(reply); + if (!tmp) { + return 1; + } + } + return size == 0; +} +} // namespace DeviceUsageStats +} // namespace OHOS \ No newline at end of file diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 88161e4ec10a265636dcd86a85cc1fce6540f97b..ef28c086625a282a38fb3b0ae02eea9cb7bdb7b7 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -26,6 +26,11 @@ #include "app_group_callback_stub.h" #include "bundle_active_group_map.h" #include "app_group_callback_info.h" +#include "bundle_active_form_record.h" +#include "bundle_active_event_stats.h" +#include "bundle_active_module_record.h" +#include "bundle_active_package_stats.h" +#include "app_group_callback_proxy.h" #include "iapp_group_callback.h" using namespace testing::ext; @@ -366,6 +371,174 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_DeathRecipient_001 EXPECT_TRUE(deathTest != nullptr); deathTest->OnRemoteDied(nullptr); } + +/* + * @tc.name: DeviceUsageStatisticsTest_AppGroupCallbackInfo_001 + * @tc.desc: AppGroupCallbackInfo_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_AppGroupCallbackInfo_001, Function | MediumTest | Level0) +{ + int32_t oldGroup = 60; + int32_t newGroup = 10; + uint32_t changeReason = 1; + auto appGroupCallbackInfo = + std::make_shared(COMMON_USERID, oldGroup, newGroup, changeReason, g_defaultBundleName); + + MessageParcel data; + EXPECT_TRUE(appGroupCallbackInfo->Marshalling(data)); + auto appGroupCallback = appGroupCallbackInfo->Unmarshalling(data); + EXPECT_TRUE(appGroupCallback != nullptr); + + EXPECT_EQ(appGroupCallback->GetUserId(), COMMON_USERID); + EXPECT_EQ(appGroupCallback->GetOldGroup(), oldGroup); + EXPECT_EQ(appGroupCallback->GetNewGroup(), newGroup); + EXPECT_EQ(appGroupCallback->GetChangeReason(), changeReason); + EXPECT_EQ(appGroupCallback->GetBundleName(), g_defaultBundleName); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEventStat_001 + * @tc.desc: BundleActiveEventStats_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEventStat_001, Function | MediumTest | Level0) +{ + auto bundleActiveEventStats = std::make_shared(); + bundleActiveEventStats->eventId_ = COMMON_USERID; + bundleActiveEventStats->beginTimeStamp_ = 0; + bundleActiveEventStats->endTimeStamp_ = LARGE_NUM; + bundleActiveEventStats->lastEventTime_ = LARGE_NUM; + bundleActiveEventStats->totalTime_ = LARGE_NUM; + bundleActiveEventStats->count_ = 1; + + MessageParcel data; + EXPECT_TRUE(bundleActiveEventStats->Marshalling(data)); + auto tempEventStats = bundleActiveEventStats->UnMarshalling(data); + EXPECT_TRUE(tempEventStats != nullptr); + + EXPECT_EQ(tempEventStats->GetEventId(), COMMON_USERID); + EXPECT_EQ(tempEventStats->GetFirstTimeStamp(), 0); + EXPECT_EQ(tempEventStats->GetLastTimeStamp(), LARGE_NUM); + EXPECT_EQ(tempEventStats->GetLastEventTime(), LARGE_NUM); + EXPECT_EQ(tempEventStats->GetTotalTime(), LARGE_NUM); + EXPECT_EQ(tempEventStats->GetCount(), 1); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_FormRecord_001 + * @tc.desc: BundleActiveFormRecord_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_FormRecord_001, Function | MediumTest | Level0) +{ + auto bundleActiveFormRecord = std::make_shared(); + EXPECT_NE(bundleActiveFormRecord->ToString(), " "); + + MessageParcel data; + EXPECT_TRUE(bundleActiveFormRecord->Marshalling(data)); + EXPECT_TRUE(bundleActiveFormRecord->UnMarshalling(data) != nullptr); + + BundleActiveFormRecord bundleActiveFormRecordA; + bundleActiveFormRecordA.count_ = 2; + BundleActiveFormRecord bundleActiveFormRecordB; + bundleActiveFormRecordB.count_ = 1; + EXPECT_TRUE(bundleActiveFormRecord->cmp(bundleActiveFormRecordA, bundleActiveFormRecordB)); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_BundleActiveEvent_001 + * @tc.desc: BundleActiveEvent_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_BundleActiveEvent_001, Function | MediumTest | Level0) +{ + BundleActiveEvent bundleActiveEvent; + BundleActiveEvent bundleActiveEventA; + EXPECT_NE(bundleActiveEvent.ToString(), " "); + bundleActiveEvent = bundleActiveEventA; +} + +/* + * @tc.name: DeviceUsageStatisticsTest_PackageStats_001 + * @tc.desc: BundleActivePackageStats_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_PackageStats_001, Function | MediumTest | Level0) +{ + auto bundleActivePackageStats = std::make_shared(); + bundleActivePackageStats->IncrementBundleLaunchedCount(); + EXPECT_NE(bundleActivePackageStats->ToString(), " "); + + MessageParcel data; + EXPECT_TRUE(bundleActivePackageStats->Marshalling(data)); + EXPECT_TRUE(bundleActivePackageStats->UnMarshalling(data) != nullptr); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_ModuleRecord_001 + * @tc.desc: BundleActiveModuleRecord_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_ModuleRecord_001, Function | MediumTest | Level0) +{ + auto bundleActiveModuleRecord = std::make_shared(); + EXPECT_NE(bundleActiveModuleRecord->ToString(), " "); + + MessageParcel data; + EXPECT_TRUE(bundleActiveModuleRecord->Marshalling(data)); + EXPECT_TRUE(bundleActiveModuleRecord->UnMarshalling(data) != nullptr); + + BundleActiveModuleRecord bundleActiveModuleRecordA; + bundleActiveModuleRecordA.lastModuleUsedTime_ = 2; + BundleActiveModuleRecord bundleActiveModuleRecordB; + bundleActiveModuleRecordB.lastModuleUsedTime_ = 1; + EXPECT_TRUE(bundleActiveModuleRecord->cmp(bundleActiveModuleRecordA, bundleActiveModuleRecordB)); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_AppGroupCallbackProxy_001 + * @tc.desc: AppGroupCallbackProxy_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_AppGroupCallbackProxy_001, Function | MediumTest | Level0) +{ + int32_t oldGroup = 60; + int32_t newGroup = 10; + uint32_t changeReason = 1; + AppGroupCallbackInfo appGroupCallbackInfo(COMMON_USERID, oldGroup, newGroup, changeReason, g_defaultBundleName); + + auto appGroupCallbackProxy = std::make_shared(nullptr); + appGroupCallbackProxy->OnAppGroupChanged(appGroupCallbackInfo); +} + +/* + * @tc.name: DeviceUsageStatisticsTest_AppGroupCallbackStub_001 + * @tc.desc: AppGroupCallbackStub_001 + * @tc.type: FUNC + * @tc.require: issuesI5SOZY + */ +HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_AppGroupCallbackStub_001, Function | MediumTest | Level0) +{ + int32_t oldGroup = 60; + int32_t newGroup = 10; + uint32_t changeReason = 1; + AppGroupCallbackInfo appGroupCallbackInfo(COMMON_USERID, oldGroup, newGroup, changeReason, g_defaultBundleName); + + auto appGroupCallbackStub = std::make_shared(); + appGroupCallbackStub->OnAppGroupChanged(appGroupCallbackInfo); + MessageParcel data1; + MessageParcel reply; + MessageOption option; + EXPECT_EQ(appGroupCallbackStub->OnRemoteRequest(1, data1, reply, option), -1); +} } // namespace DeviceUsageStats } // namespace OHOS