diff --git a/interfaces/innerkits/include/bundle_active_client.h b/interfaces/innerkits/include/bundle_active_client.h index 660936c158bf48bd36d2d89a1ca64d96387dfbb8..a21e779499bbaf5f39bef4d0c49b1e7b509e31a4 100644 --- a/interfaces/innerkits/include/bundle_active_client.h +++ b/interfaces/innerkits/include/bundle_active_client.h @@ -44,7 +44,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName, int32_t& errCode); + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1); /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/interfaces/innerkits/include/bundle_active_proxy.h b/interfaces/innerkits/include/bundle_active_proxy.h index 5adce7c13271faf580a2aa3de8f8936a77df690c..9e0a205ce81a8235735cf91e4a6b1fbf90184c24 100644 --- a/interfaces/innerkits/include/bundle_active_proxy.h +++ b/interfaces/innerkits/include/bundle_active_proxy.h @@ -38,7 +38,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1) override; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/interfaces/innerkits/src/bundle_active_client.cpp b/interfaces/innerkits/src/bundle_active_client.cpp index e965add52550feb1abed89f3c59e1ab0fea8f179..0cd2f54efec4c8c82ac2e2dbd2f91d0ec46332b0 100644 --- a/interfaces/innerkits/src/bundle_active_client.cpp +++ b/interfaces/innerkits/src/bundle_active_client.cpp @@ -75,12 +75,12 @@ int32_t BundleActiveClient::ReportEvent(BundleActiveEvent event, const int32_t u return bundleActiveProxy_->ReportEvent(event, userId); } -bool BundleActiveClient::IsBundleIdle(const std::string& bundleName, int32_t& errCode) +bool BundleActiveClient::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { if (!GetBundleActiveProxy()) { return -1; } - return bundleActiveProxy_->IsBundleIdle(bundleName, errCode); + return bundleActiveProxy_->IsBundleIdle(bundleName, errCode, userId); } std::vector BundleActiveClient::QueryPackageStats(const int32_t intervalType, diff --git a/interfaces/innerkits/src/bundle_active_proxy.cpp b/interfaces/innerkits/src/bundle_active_proxy.cpp index 2343d0ead8a27989bca24b5bbb04ba831a7ccc82..f9844895001348c194ef4ec0632d141556093fdf 100644 --- a/interfaces/innerkits/src/bundle_active_proxy.cpp +++ b/interfaces/innerkits/src/bundle_active_proxy.cpp @@ -33,15 +33,17 @@ int32_t BundleActiveProxy::ReportEvent(BundleActiveEvent& event, const int32_t u return result; } -bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName, int32_t& errCode) +bool BundleActiveProxy::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { MessageParcel data; MessageParcel reply; MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { + if (!data.WriteInterfaceToken(GetDescriptor()) || + !data.WriteString(bundleName) || + !data.WriteInt32(errCode) || + !data.WriteInt32(userId)) { return false; } - data.WriteString(bundleName); Remote() -> SendRequest(IS_BUNDLE_IDLE, data, reply, option); bool result = reply.ReadInt32(); errCode = reply.ReadInt32(); diff --git a/services/common/include/bundle_active_service.h b/services/common/include/bundle_active_service.h index 438a37bc689b05f7f39e1e77934213d43ed169f8..ebe6615a511bac3b0b0c26af20b27304d643e221 100644 --- a/services/common/include/bundle_active_service.h +++ b/services/common/include/bundle_active_service.h @@ -58,7 +58,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) override; + bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId = -1) override; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/services/common/include/ibundle_active_service.h b/services/common/include/ibundle_active_service.h index ee375102594f0bc10ce2bfe98f0163c92d84c134..49351605d79f9dcc9913b15debde2966eb6f1c14 100644 --- a/services/common/include/ibundle_active_service.h +++ b/services/common/include/ibundle_active_service.h @@ -61,7 +61,7 @@ public: * parameters: bundleName * return: if bundle is idle, return true. if bundle is not idle, return false. */ - virtual bool IsBundleIdle(const std::string& bundleName, int32_t& errCode) = 0; + virtual bool IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) = 0; /* * function: QueryPackageStats, query all bundle usage statistics in specific time span for calling user. * parameters: intervalType, beginTime, endTime, errCode diff --git a/services/common/src/bundle_active_service.cpp b/services/common/src/bundle_active_service.cpp index 6ad5db9b200c6a819799c2a0175484f2cac852e1..6d082fce7fe989e1bc781e6f675613b6b8f0e58a 100644 --- a/services/common/src/bundle_active_service.cpp +++ b/services/common/src/bundle_active_service.cpp @@ -249,7 +249,7 @@ int32_t BundleActiveService::ReportEvent(BundleActiveEvent& event, const int32_t } } -bool BundleActiveService::IsBundleIdle(const std::string& bundleName, int32_t& errCode) +bool BundleActiveService::IsBundleIdle(const std::string& bundleName, int32_t& errCode, int32_t userId) { // get uid int32_t callingUid = OHOS::IPCSkeleton::GetCallingUid(); @@ -262,17 +262,20 @@ bool BundleActiveService::IsBundleIdle(const std::string& bundleName, int32_t& e sptrBundleMgr_->GetBundleNameForUid(callingUid, callingBundleName); BUNDLE_ACTIVE_LOGI("UID is %{public}d, bundle name is %{public}s", callingUid, callingBundleName.c_str()); // get user id - int32_t userId = -1; int32_t result = -1; - BundleActiveAccountHelper::GetUserId(callingUid, userId); - if (userId != -1 && !callingBundleName.empty()) { + if (userId == -1) { + BundleActiveAccountHelper::GetUserId(callingUid, userId); + } + if (userId != -1) { if (callingBundleName == bundleName) { BUNDLE_ACTIVE_LOGI("%{public}s check its own idle state", bundleName.c_str()); result = bundleActiveCore_->IsBundleIdle(bundleName, userId); } else { bool isSystemAppAndHasPermission = CheckBundleIsSystemAppAndHasPermission(callingUid, tokenId, errCode); BUNDLE_ACTIVE_LOGI("check other bundle idle state"); - if (isSystemAppAndHasPermission) { + if (isSystemAppAndHasPermission || + AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId) == + AccessToken::TypeATokenTypeEnum::TOKEN_NATIVE) { errCode = 0; result = bundleActiveCore_->IsBundleIdle(bundleName, userId); } else { diff --git a/services/common/src/bundle_active_stub.cpp b/services/common/src/bundle_active_stub.cpp index 3dae350adcddedb3211ad004de539188369b8d42..4745fe8592afec43b3a9a22ea86b5bdcbdfd4f35 100644 --- a/services/common/src/bundle_active_stub.cpp +++ b/services/common/src/bundle_active_stub.cpp @@ -41,7 +41,8 @@ int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, Me case IS_BUNDLE_IDLE: { std::string bundleName = data.ReadString(); int32_t errCode = data.ReadInt32(); - int32_t result = IsBundleIdle(bundleName, errCode); + int32_t userId = data.ReadInt32(); + int32_t result = IsBundleIdle(bundleName, errCode, userId); reply.WriteInt32(result); return reply.WriteInt32(errCode); } diff --git a/test/unittest/device_usage_statistics_test.cpp b/test/unittest/device_usage_statistics_test.cpp index 2748693d7810d193c3ef61cd27482f0e40af2c4f..7c8142b84229a1edc99ad90a17d6ee87ad94dd68 100644 --- a/test/unittest/device_usage_statistics_test.cpp +++ b/test/unittest/device_usage_statistics_test.cpp @@ -151,9 +151,9 @@ HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_QueryPackagesStats HWTEST_F(DeviceUsageStatisticsTest, DeviceUsageStatisticsTest_IsBundleIdle_001, Function | MediumTest | Level0) { int32_t errCode = 0; - bool result = BundleActiveClient::GetInstance().IsBundleIdle(DEFAULT_BUNDLENAME, errCode); + bool result = BundleActiveClient::GetInstance().IsBundleIdle(DEFAULT_BUNDLENAME, errCode, DEFAULT_USERID); EXPECT_EQ(result, false); - EXPECT_EQ(errCode, -1); + EXPECT_EQ(errCode, 0); } /*