diff --git a/services/ans/include/smart_reminder_center.h b/services/ans/include/smart_reminder_center.h index 584c48eb2190efa99b7f98a374dc2791676ad56b..5d53c8a99601908592e6cb6c3a907c1da244f1b3 100644 --- a/services/ans/include/smart_reminder_center.h +++ b/services/ans/include/smart_reminder_center.h @@ -120,6 +120,7 @@ private: const std::string EXTEND_INFO_APP_NAME = "app_name"; const std::string EXTEND_INFO_APP_LABEL = "app_label"; const std::string EXTEND_INFO_APP_INDEX = "app_index"; + const std::string EXTEND_INFO_APP_UID = "app_uid"; const std::string EXTEND_INFO_DEVICE_ID = "deviceId"; const std::string EXTEND_INFO_USER_ID = "userId"; }; diff --git a/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp b/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp index d8ff25d6dcfa4e1300fb54c48a0b1342efb1e147..903fd15a9caefc8d24e2687c3e5debd7c8316394 100644 --- a/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp +++ b/services/ans/src/notification_smart_reminder/smart_reminder_center.cpp @@ -489,6 +489,7 @@ void SmartReminderCenter::FillRequestExtendInfo(const string &deviceType, Device if (userId == SUBSCRIBE_USER_INIT) { OsAccountManagerHelper::GetInstance().GetCurrentActiveUserId(userId); } + int32_t index = BundleManagerHelper::GetInstance()->GetAppIndexByUid(request->GetOwnerUid()); std::shared_ptr extendInfo = request->GetExtendInfo(); if (extendInfo == nullptr) { extendInfo = std::make_shared(); @@ -497,16 +498,18 @@ void SmartReminderCenter::FillRequestExtendInfo(const string &deviceType, Device extendInfo->SetParam(EXTEND_INFO_PRE + "_" + EXTEND_INFO_APP_LABEL, AAFwk::String::Box(bundleResourceInfo.label)); extendInfo->SetParam(EXTEND_INFO_PRE + "_" + EXTEND_INFO_APP_INDEX, - AAFwk::Integer::Box(appInfo.appIndex)); + AAFwk::Integer::Box(index)); + extendInfo->SetParam(EXTEND_INFO_PRE + "_" + EXTEND_INFO_APP_UID, + AAFwk::Integer::Box(request->GetOwnerUid())); extendInfo->SetParam(EXTEND_INFO_PRE + "_" + EXTEND_INFO_DEVICE_ID + "_" + deviceType, AAFwk::String::Box(deviceStatus.deviceId)); extendInfo->SetParam(EXTEND_INFO_PRE + "_" + EXTEND_INFO_USER_ID + "_" + deviceType, AAFwk::Integer::Box(deviceStatus.userId)); request->SetExtendInfo(extendInfo); - ANS_LOGI("FillRequestExtendInfo result: %{public}s %{public}s %{public}d %{public}s %{public}d", - appInfo.name.c_str(), bundleResourceInfo.label.c_str(), appInfo.appIndex, - StringAnonymous(deviceStatus.deviceId).c_str(), deviceStatus.userId); + ANS_LOGI("FillRequestExtendInfo result: %{public}s %{public}s %{public}d %{public}s %{public}d %{public}d", + appInfo.name.c_str(), bundleResourceInfo.label.c_str(), index, + StringAnonymous(deviceStatus.deviceId).c_str(), deviceStatus.userId, request->GetOwnerUid()); } void SmartReminderCenter::HandleReminderMethods( diff --git a/services/distributed/include/helper/bundle_resource_helper.h b/services/distributed/include/helper/bundle_resource_helper.h index 17fcd81d27b724829c4a2a53ee1df5dfbc026fd3..23f16800d78f98d993362630e8aaebc744b28cdf 100644 --- a/services/distributed/include/helper/bundle_resource_helper.h +++ b/services/distributed/include/helper/bundle_resource_helper.h @@ -93,6 +93,7 @@ public: ErrCode GetBundleInfoV9(const std::string& bundleName, int32_t userId, AppExecFwk::BundleInfo& bundleInfo); + int32_t GetAppIndexByUid(const int32_t uid); private: void Connect(); void Disconnect(); diff --git a/services/distributed/src/helper/bundle_resource_helper.cpp b/services/distributed/src/helper/bundle_resource_helper.cpp index 29cbe34a87af6f6a1de25cd6d76c927a7320869b..a6cfa7cd3a6d1054c15083710ececc2fba88281a 100644 --- a/services/distributed/src/helper/bundle_resource_helper.cpp +++ b/services/distributed/src/helper/bundle_resource_helper.cpp @@ -193,5 +193,24 @@ ErrCode BundleResourceHelper::GetBundleInfoV9(const std::string& bundleName, int } return ERR_OK; } + +int32_t BundleResourceHelper::GetAppIndexByUid(const int32_t uid) +{ + int32_t appIndex = 0; + std::lock_guard lock(connectionMutex_); + Connect(); + if (bundleMgr_ == nullptr) { + return appIndex; + } + std::string bundleName; + std::string identity = IPCSkeleton::ResetCallingIdentity(); + int32_t result = bundleMgr_->GetNameAndIndexForUid(uid, bundleName, appIndex); + if (result != ERR_OK) { + ANS_LOGW("Get bundle index %{public}d.", uid); + return 0; + } + IPCSkeleton::SetCallingIdentity(identity); + return appIndex; +} } } diff --git a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp index 9eecdebe30fca1c473377a2715dae11e106b9b9d..d27d4e2bcfdaf6bdc59008e6a29843ac68d4fe30 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -53,6 +53,7 @@ static const std::string EXTENDINFO_APP_NAME = "app_name"; static const std::string EXTENDINFO_APP_LABEL = "app_label"; static const std::string EXTENDINFO_APP_ICON = "app_icon"; static const std::string EXTENDINFO_APP_INDEX = "app_index"; +static const std::string EXTENDINFO_APP_UID = "app_uid"; static const std::string EXTENDINFO_DEVICE_USERID = "userId"; static const std::string EXTENDINFO_DEVICE_ID = "deviceId"; static const std::string EXTENDINFO_ENABLE_CHECK = "check"; @@ -652,17 +653,20 @@ bool DistributedPublishService::FillSyncRequestExtendInfo(const sptr::GetInstance()->GetAppIndexByUid( + notificationRequest->GetOwnerUid()); AppExecFwk::ApplicationInfo appInfo = bundleInfo.applicationInfo; wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_NAME, AAFwk::String::Box(appInfo.name)); wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_LABEL, AAFwk::String::Box(resourceInfo.label)); - wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_INDEX, AAFwk::Integer::Box(appInfo.appIndex)); + wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_UID, + AAFwk::Integer::Box(notificationRequest->GetOwnerUid())); + wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_INDEX, AAFwk::Integer::Box(index)); wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID + "_" + deviceType, AAFwk::String::Box(deviceId)); requestBox->SetSmallIcon(AnsImageUtil::CreatePixelMapByString(resourceInfo.icon)); requestBox->SetReceiverUserId(userId); - ANS_LOGI("Dans fill %{public}s %{public}d %{public}s %{public}d", resourceInfo.label.c_str(), appInfo.appIndex, - StringAnonymous(deviceId).c_str(), userId); + ANS_LOGI("Dans fill %{public}s %{public}d %{public}s %{public}d %{public}d", resourceInfo.label.c_str(), index, + StringAnonymous(deviceId).c_str(), userId, notificationRequest->GetOwnerUid()); return true; } wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID + "_" + deviceType, AAFwk::String::Box(deviceId)); diff --git a/services/distributed/test/unittest/BUILD.gn b/services/distributed/test/unittest/BUILD.gn index db2c6ca8c8506075ceee1a827810d05ab52d4381..20e8a5dbd955a189b5ad35e3186e79e497662b41 100644 --- a/services/distributed/test/unittest/BUILD.gn +++ b/services/distributed/test/unittest/BUILD.gn @@ -839,14 +839,14 @@ ohos_unittest("distributed_subscriber_test") { ohos_unittest("distributed_library_test") { module_out_path = module_output_path include_dirs = [ "/${services_path}/distributed/include/base" ] - + sources = [ "/${services_path}/distributed/src/base/distributed_liveview_all_scenarios_extension_wrapper.cpp", "distributed_library_test/distributed_liveview_all_scenarios_extension_wrapper_unit_test.cpp", ] - + deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] - + external_deps = [ "ability_base:zuri", "ability_runtime:ability_manager", @@ -1030,7 +1030,8 @@ ohos_unittest("distributed_helper_test") { "/${services_path}/distributed/src/tlv_box/response_box.cpp", "/${services_path}/distributed/src/tlv_box/state_box.cpp", "/${services_path}/distributed/src/tlv_box/tlv_box.cpp", - "distributed_helper_test/distributed_notification_analytics_util_test.cpp" + "distributed_helper_test/distributed_notification_analytics_util_test.cpp", + "distributed_helper_test/bundle_resource_helper_test.cpp" ] configs = [ @@ -1096,16 +1097,16 @@ ohos_unittest("distributed_soft_bus_master_test") { "/${services_path}/distributed/include/helper", "/${services_path}/distributed/include/tlv_box", ] - + sources = [ "/${services_path}/distributed/src/helper/analytics_util.cpp", "/${services_path}/distributed/src/base/distributed_liveview_all_scenarios_extension_wrapper.cpp", "/${services_path}/distributed/src/soft_bus/distributed_unlock_listener_oper_service.cpp", "/${services_path}/distributed/src/soft_bus/distributed_device_service.cpp", "distributed_soft_bus_test/distributed_unlock_listener_oper_service_unit_test.cpp", ] - + deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] - + external_deps = [ "ability_base:zuri", "ability_runtime:ability_manager", @@ -1133,11 +1134,11 @@ ohos_unittest("distributed_soft_bus_master_test") { external_deps += [ "screenlock_mgr:screenlock_client" ] defines += [ "SCREENLOCK_MGR_ENABLE" ] } - + if (distributed_notification_service_feature_phone_collaboration) { defines += [ "DISTRIBUTED_FEATURE_MASTER" ] } - + subsystem_name = "${subsystem_name}" part_name = "${component_name}" } @@ -1152,7 +1153,7 @@ ohos_unittest("distributed_soft_bus_test") { "/${services_path}/distributed/include/tlv_box", "/${services_path}/distributed/include/base", ] - + sources = [ "/${services_path}/distributed/src/tlv_box/box_base.cpp", "/${services_path}/distributed/src/soft_bus/distributed_operation_service.cpp", @@ -1183,9 +1184,9 @@ ohos_unittest("distributed_soft_bus_test") { "/${services_path}/distributed/src/base/distributed_local_config.cpp", "distributed_soft_bus_test/distributed_operation_service_unit_test.cpp", ] - + deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] - + external_deps = [ "ability_base:zuri", "ability_runtime:ability_manager", @@ -1213,7 +1214,7 @@ ohos_unittest("distributed_soft_bus_test") { external_deps += [ "screenlock_mgr:screenlock_client" ] defines += [ "SCREENLOCK_MGR_ENABLE" ] } - + subsystem_name = "${subsystem_name}" part_name = "${component_name}" -} \ No newline at end of file +} diff --git a/services/distributed/test/unittest/distributed_helper_test/bundle_resource_helper_test.cpp b/services/distributed/test/unittest/distributed_helper_test/bundle_resource_helper_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..70418e449c43349f2aafc387657303a1dcd7dc66 --- /dev/null +++ b/services/distributed/test/unittest/distributed_helper_test/bundle_resource_helper_test.cpp @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "gtest/gtest.h" +#include "bundle_resource_helper.h" + +using namespace testing::ext; + +namespace OHOS { +namespace Notification { + +class DistributedBundleHelperTest : public testing::Test { +public: + void SetUp() override; + void TearDown() override; +}; + +void DistributedBundleHelperTest::SetUp() {} + +void DistributedBundleHelperTest::TearDown() {} + + +/** + * @tc.name : DistributedBundleHelperTest_00100 + * @tc.number : DistributedBundleHelperTest_00100 + * @tc.desc : test GetAppIndexByUid function + */ +HWTEST_F(DistributedBundleHelperTest, DistributedServiceTest_00100, Function | SmallTest | Level1) +{ + int32_t uid = 100010; + int32_t index = DelayedSingleton::GetInstance()->GetAppIndexByUid(uid); + ASSERT_EQ(0, index); +} +} // namespace Notification +} // namespace OHOS