diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 13e4ab9b8de88c8902f85256dfdad9fd185f9420..ed340bc0038b93f67671c90ec5372d39543304e9 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -317,5 +317,9 @@ interface OHOS.Notification.IAnsManager { void SetHashCodeRule([in] unsigned int type); + void GetMutilDeviceStatus([in] String deviceType, [in] unsigned int status, [out] String deviceId, [out] int userId); + + void GetTargetDeviceBundleList([in] String deviceType, [in] String deviceId, [out] String[] bundleList); + [macrodef NOTIFICATION_SMART_REMINDER_SUPPORTED] void RegisterSwingCallback([in] IRemoteObject swingCallback); } diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 5aaf74c97ec0a931ccca81e96b16fee6c96d1777..412cba8cdf3bb6b1ee9a56fd32d51c477ec9b62d 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -642,7 +642,7 @@ ErrCode NotificationHelper::SetSilentReminderEnabled(const NotificationBundleOpt { return DelayedSingleton::GetInstance()->SetSilentReminderEnabled(bundleOption, enabled); } - + ErrCode NotificationHelper::IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus) { return DelayedSingleton::GetInstance()->IsSilentReminderEnabled(bundleOption, enableStatus); @@ -752,5 +752,18 @@ ErrCode NotificationHelper::GetDistributedDevicelist(std::vector &d { return DelayedSingleton::GetInstance()->GetDistributedDevicelist(deviceTypes); } + +ErrCode NotificationHelper::GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) +{ + return DelayedSingleton::GetInstance()->GetMutilDeviceStatus(deviceType, status, deviceId, userId); +} + +ErrCode NotificationHelper::GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList) +{ + return DelayedSingleton::GetInstance()->GetTargetDeviceBundleList(deviceType, deviceId, + bundleList); +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/ans/src/notification_request.cpp b/frameworks/ans/src/notification_request.cpp index f7415525a761f84bc19148e64f394eeca9fd2269..a92c1a331bceef54b548821c97029441f65cd811 100644 --- a/frameworks/ans/src/notification_request.cpp +++ b/frameworks/ans/src/notification_request.cpp @@ -842,7 +842,7 @@ std::string NotificationRequest::Dump() (unifiedGroupInfo_ ? unifiedGroupInfo_->Dump() : "null")+ " }"; } -std::string NotificationRequest::CollaborationToJson() const +bool NotificationRequest::CollaborationToJson(std::string& data) const { nlohmann::json jsonObject; jsonObject["id"] = notificationId_; @@ -869,12 +869,12 @@ std::string NotificationRequest::CollaborationToJson() const nlohmann::json bundleOptionObj; if (!NotificationJsonConverter::ConvertToJson(agentBundle_.get(), bundleOptionObj)) { ANS_LOGE("Cannot convert agentBundle to JSON."); - return std::string(); + return false; } jsonObject["agentBundle"] = bundleOptionObj; } - - return jsonObject.dump(); + data = jsonObject.dump(); + return true; } NotificationRequest *NotificationRequest::CollaborationFromJson(const std::string& basicInfo) diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index c07787ad6a8907f227ec7665c4352f5e3e40f8a5..6a721cf7e9546f8e963266284183b17542901696 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1138,7 +1138,7 @@ public: * @return Returns set notifications enabled for specified bundle result. */ ErrCode SetSilentReminderEnabled(const NotificationBundleOption &bundleOption, const bool enabled); - + /** * @brief get whether to allow a specified application to publish notifications cross * device collaboration. The caller must have system permissions to call this method. @@ -1283,6 +1283,12 @@ public: ErrCode SetTargetDeviceSwitch(const std::string& deviceType, const std::string& deviceId, bool notificaitonEnable, bool liveViewEnable); + ErrCode GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList); + + ErrCode GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId); + #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED /** * @brief Register Swing swingCbFunc. diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 872646c32b2d5a8d7b46b45751959287baf35e52..1cfe6428595a9bd4906c2972bba731bce3979ca7 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2098,7 +2098,7 @@ ErrCode AnsNotification::SetDistributedAuthStatus( ANS_LOGE("Invalid deviceType or deviceId."); return ERR_ANS_INVALID_PARAM; } - + sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("UnregisterPushCallback fail."); @@ -2139,13 +2139,13 @@ ErrCode AnsNotification::SetSilentReminderEnabled(const NotificationBundleOption ANS_LOGE("Invalid bundle name."); return ERR_ANS_INVALID_PARAM; } - + sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("SetSilentReminderEnabledCallback fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); if (bo == nullptr) { ANS_LOGE("Fail: bundleOption is empty."); @@ -2153,7 +2153,7 @@ ErrCode AnsNotification::SetSilentReminderEnabled(const NotificationBundleOption } return proxy->SetSilentReminderEnabled(bo, enabled); } - + ErrCode AnsNotification::IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus) { @@ -2162,13 +2162,13 @@ ErrCode AnsNotification::IsSilentReminderEnabled(const NotificationBundleOption ANS_LOGE("Invalid bundle name."); return ERR_ANS_INVALID_PARAM; } - + sptr proxy = GetAnsManagerProxy(); if (!proxy) { ANS_LOGE("IsSilentReminderEnabledCallback fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); if (bo == nullptr) { ANS_LOGE("Fail: bundleOption is empty."); @@ -2282,6 +2282,30 @@ ErrCode AnsNotification::SetTargetDeviceBundleList(const std::string& deviceType return proxy->SetTargetDeviceBundleList(deviceType, deviceId, operatorType, bundleList); } +ErrCode AnsNotification::GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) +{ + ANS_LOGD("called"); + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("GetMutilDeviceStatus fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return proxy->GetMutilDeviceStatus(deviceType, status, deviceId, userId); +} + +ErrCode AnsNotification::GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList) +{ + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + return proxy->GetTargetDeviceBundleList(deviceType, deviceId, bundleList); +} + ErrCode AnsNotification::SetTargetDeviceSwitch(const std::string& deviceType, const std::string& deviceId, bool notificaitonEnable, bool liveViewEnable) { diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 204fe3f4cfb9f42126e1073a5adf4f8d4252dae8..2a7bf015710dbdbb7faafd44d07f69581e03b108 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -744,6 +744,18 @@ public: { return ERR_ANS_INVALID_PARAM; } + + ErrCode GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) override + { + return ERR_ANS_INVALID_PARAM; + } }; class AnsNotificationBranchTest : public testing::Test { diff --git a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h index 4dc17a9114958fc0654f4cc1ac000e1ef9b19008..85200503316d1d11803d5b2c391fc14fa14860af 100644 --- a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h +++ b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h @@ -12,15 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - + #ifndef OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H #define OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H - + #include "gmock/gmock.h" - + #include #include "ians_manager.h" - + namespace OHOS { namespace Notification { class MockAnsManagerProxy : public IAnsManager { @@ -190,10 +190,12 @@ public: MOCK_METHOD2(IsSilentReminderEnabled, ErrCode(const sptr &bundleOption, int32_t &enableStatusInt)); MOCK_METHOD1(GetDistributedDevicelist, ErrCode(std::vector& deviceList)); + MOCK_METHOD4(GetMutilDeviceStatus, ErrCode(const std::string&, const uint32_t, std::string&, int32_t&)); + MOCK_METHOD3(GetTargetDeviceBundleList, ErrCode(const std::string&, const std::string&, std::vector&)); #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED MOCK_METHOD1(RegisterSwingCallback, ErrCode(const sptr&)); #endif }; } // namespace Notification } // namespace OHOS -#endif // OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H \ No newline at end of file +#endif // OHOS_NOTIFICATION_MOCK_ANS_MANAGER_PROXY_H diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 8de9c6adaa1f9acef561fd5134f3eb072e8fc374..f8dafe1e52877300e79d295a250999557213e47b 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1187,7 +1187,7 @@ public: * @return Returns set notifications enabled for specified bundle result. */ static ErrCode IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus); - + /** * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders * @@ -1273,6 +1273,27 @@ public: static ErrCode SetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, int operatorType, const std::vector& bundleList); + /** + * @brief Get the status of the target device. + * + * @param deviceType Type of the device whose status you want to set. + * @param status The status. + * @return Returns set result. + */ + static ErrCode GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId); + + /** + * @brief get target device of bundle list. + * + * @param deviceType device type. + * @param deviceId device udid. + * @param bundleList device of bundle list. + * @return Returns ERR_OK on success, others on failure. + */ + static ErrCode GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList); + /** * @brief set target device of bundle list. * diff --git a/interfaces/inner_api/notification_request.h b/interfaces/inner_api/notification_request.h index 897ca0e80463fdcc8c9a08cfc2e0586b3e2cfa04..53773b8887c664aa89a5e03457b5afaf051e3b82 100644 --- a/interfaces/inner_api/notification_request.h +++ b/interfaces/inner_api/notification_request.h @@ -1492,7 +1492,7 @@ public: uint32_t GetCollaboratedReminderFlag() const; - std::string CollaborationToJson() const; + bool CollaborationToJson(std::string& data) const; static NotificationRequest *CollaborationFromJson(const std::string& basicInfo); diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 08f56f6f892de7750ef318acd7376a2af5572404..7e523740f23bfee4d017d0031ab905320e0f62a9 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1074,7 +1074,7 @@ public: * @return Returns set notifications enabled for specified bundle result. */ ErrCode SetSilentReminderEnabled(const sptr &bundleOption, const bool enabled) override; - + /* * @brief Get whether to allow a specified application to publish notifications cross * device collaboration. The caller must have system permissions to call this method. @@ -1217,6 +1217,12 @@ public: ErrCode SetTargetDeviceSwitch(const std::string& deviceType, const std::string& deviceId, bool notificaitonEnable, bool liveViewEnable) override; + ErrCode GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList) override; + + ErrCode GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) override; + /** * @brief clear notification when aggregate local switch close. */ diff --git a/services/ans/include/notification_extension/distributed_device_data_service.h b/services/ans/include/notification_extension/distributed_device_data_service.h index 1fec4385bb43b6d23f542f4b78d9d3154c823542..6fbbd8911e3b45ddce9e860e0113825a3d9d2bdc 100644 --- a/services/ans/include/notification_extension/distributed_device_data_service.h +++ b/services/ans/include/notification_extension/distributed_device_data_service.h @@ -41,6 +41,8 @@ public: bool notificationEnable, bool liveViewEnable); int32_t SetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, int operatorType, const std::vector& bundleList); + int32_t GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList); bool CheckDeviceBundleExist(const std::string& deviceType, const std::string& deviceId, const std::string bundleName); bool GetDeviceNotificationEnable(const std::string& deviceType, const std::string& deviceId); diff --git a/services/ans/src/advanced_notification_publish_service.cpp b/services/ans/src/advanced_notification_publish_service.cpp index 6c6ab20c7834e6e839f97fecc2c7933b34c0edb3..c8619308bb69377321ff34f295301382ff5a645c 100644 --- a/services/ans/src/advanced_notification_publish_service.cpp +++ b/services/ans/src/advanced_notification_publish_service.cpp @@ -277,7 +277,6 @@ ErrCode AdvancedNotificationService::CollaboratePublish(const sptrrequest, .bundleOption = record->bundleOption}; UpdateCollaborateTimerInfo(record); - SetNotificationRequestToDb(requestDb); }); notificationSvrQueue_->wait(handler); return ERR_OK; diff --git a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp index 79d0dcf6a49a63f20c1061303c628e7a1cef2336..12e859e5837e315ef90efd478a9d5c1505d3761b 100644 --- a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp +++ b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp @@ -489,6 +489,41 @@ ErrCode AdvancedNotificationService::SetTargetDeviceBundleList(const std::string operatorType, bundleList); } +ErrCode AdvancedNotificationService::GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) +{ + if (deviceType.empty()) { + return ERR_ANS_INVALID_PARAM; + } + + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("isSubsystem is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + DeviceStatus deviceStatus = DelayedSingleton::GetInstance()->GetMultiDeviceStatus( + deviceType, status); + userId = deviceStatus.userId; + deviceId = deviceStatus.deviceId; + return ERR_OK; +} + +ErrCode AdvancedNotificationService::GetTargetDeviceBundleList(const std::string& deviceType, + const std::string& deviceId, std::vector& bundleList) +{ + if (deviceType.empty() || deviceId.empty()) { + return ERR_ANS_INVALID_PARAM; + } + + if (!AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID())) { + return ERR_ANS_NON_SYSTEM_APP; + } + + return DistributedDeviceDataService::GetInstance().GetTargetDeviceBundleList(deviceType, deviceId, + bundleList); +} + ErrCode AdvancedNotificationService::SetTargetDeviceSwitch(const std::string& deviceType, const std::string& deviceId, bool notificaitonEnable, bool liveViewEnable) { @@ -610,10 +645,10 @@ ErrCode AdvancedNotificationService::SetDistributedEnabledByBundle(const sptrSetDistributedEnabledByBundle(bundle, deviceType, enabled); - + ANS_LOGI("%{public}s_%{public}d, deviceType: %{public}s, enabled: %{public}s, " "SetDistributedEnabledByBundle result: %{public}d", bundleOption->GetBundleName().c_str(), bundleOption->GetUid(), deviceType.c_str(), std::to_string(enabled).c_str(), result); diff --git a/services/ans/src/notification_extension/distributed_device_data_service.cpp b/services/ans/src/notification_extension/distributed_device_data_service.cpp index f032744a91a15bca7f1c5dd9054bea30ad18d2ce..4c26977174f1a32d9bbdbe9e40120fa143b83b39 100644 --- a/services/ans/src/notification_extension/distributed_device_data_service.cpp +++ b/services/ans/src/notification_extension/distributed_device_data_service.cpp @@ -119,6 +119,20 @@ int32_t DistributedDeviceDataService::SetTargetDeviceBundleList(const std::strin return ERR_OK; } +int32_t DistributedDeviceDataService::GetTargetDeviceBundleList(const std::string& deviceType, + const std::string& deviceId, std::vector& bundleList) +{ + std::lock_guard lock(lock_); + for (auto& item : devicesData_) { + if (item.deviceType == deviceType && item.deviceId == deviceId) { + bundleList.assign(item.installedBundles.begin(), item.installedBundles.end()); + return ERR_OK; + } + } + ANS_LOGW("Get bundle %{public}s %{public}s", deviceType.c_str(), StringAnonymous(deviceId).c_str()); + return ERR_ANS_INVALID_PARAM; +} + bool DistributedDeviceDataService::CheckDeviceBundleExist(const std::string& deviceType, const std::string& deviceId, const std::string bundleName) { diff --git a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp index 8958ed3fe8d475db6d9d262858fa0cba01c7016f..cfa197ff3e6e524fda97ab4e88299bbc2f46a784 100644 --- a/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test/advanced_notification_distributed_manager_service_test.cpp @@ -530,6 +530,40 @@ HWTEST_F(AdvancedNotificationDistMgrServiceTest, SetTargetDeviceBundleList_300, ASSERT_EQ(ret, (int)ERR_ANS_INVALID_PARAM); } +/** + * @tc.name: GetMutilDeviceStatus_100 + * @tc.desc: Test GetMutilDeviceStatus when caller is not subsystem. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AdvancedNotificationDistMgrServiceTest, GetMutilDeviceStatus_100, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID); + const std::string deviceType = "pad"; + uint32_t flag = 3; + int32_t userId; + std::string deviceId = ""; + + auto ret = advancedNotificationService_->GetMutilDeviceStatus(deviceType, flag, deviceId, userId); + ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP); +} + +/** + * @tc.name: GetTargetDeviceBundleList_100 + * @tc.desc: Test GetTargetDeviceBundleList when caller is not subsystem. + * @tc.type: FUNC + * @tc.require: issue + */ +HWTEST_F(AdvancedNotificationDistMgrServiceTest, GetTargetDeviceBundleList_100, Function | SmallTest | Level1) +{ + MockGetTokenTypeFlag(ATokenTypeEnum::TOKEN_INVALID); + const std::string deviceType = "pad"; + const std::string deviceId = "id"; + std::vector bundles; + auto ret = advancedNotificationService_->GetTargetDeviceBundleList(deviceType, deviceId, bundles); + ASSERT_EQ(ret, (int)ERR_ANS_NON_SYSTEM_APP); +} + /** * @tc.name: SetTargetDeviceSwitch_100 * @tc.desc: Test SetTargetDeviceSwitch when caller is not subsystem. diff --git a/services/distributed/include/base/distributed_device_data.h b/services/distributed/include/base/distributed_device_data.h index f157f6cadc8373e763da31c4d14559423b7db897..ef40e0505a47fc65db02b8ed2bbfdfd44085634f 100644 --- a/services/distributed/include/base/distributed_device_data.h +++ b/services/distributed/include/base/distributed_device_data.h @@ -46,6 +46,15 @@ struct DistributedDeviceInfo { : deviceType_(deviceType), deviceId_(deviceId) {} DistributedDeviceInfo(uint16_t deviceType, std::string deviceId, std::string networkId) : deviceType_(deviceType), deviceId_(deviceId), networkId_(networkId) {} + bool IsPadOrPc() const + { + if (deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD && + deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_2IN1 && + deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PC) { + return false; + } + return true; + } bool deviceUsage = false; bool liveViewSync = false; bool iconSync = false; diff --git a/services/distributed/include/helper/bundle_resource_helper.h b/services/distributed/include/helper/bundle_resource_helper.h index 0b049563b853ad98f5844bb65037319b381e1247..99597a6c3ff82e7e85517430a749dab789d859d7 100644 --- a/services/distributed/include/helper/bundle_resource_helper.h +++ b/services/distributed/include/helper/bundle_resource_helper.h @@ -89,6 +89,9 @@ public: bool CheckSystemApp(const std::string& bundleName, int32_t userId); + ErrCode GetBundleInfoV9(const std::string& bundleName, int32_t userId, + AppExecFwk::BundleInfo& bundleInfo); + private: void Connect(); void Disconnect(); diff --git a/services/distributed/include/soft_bus/distributed_publish_service.h b/services/distributed/include/soft_bus/distributed_publish_service.h index 760eb081bcc743b1a0a0c764fd19d382c5678f23..962f746f56f8fa21e633bdd48921740caf74204d 100644 --- a/services/distributed/include/soft_bus/distributed_publish_service.h +++ b/services/distributed/include/soft_bus/distributed_publish_service.h @@ -45,10 +45,12 @@ public: #ifdef DISTRIBUTED_FEATURE_MASTER void RemoveAllDistributedNotifications(DistributedDeviceInfo& deviceInfo); void SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce); + void SyncLiveViewList(const DistributedDeviceInfo device, const std::vector>& notifications); + void SyncLiveViewContent(const DistributedDeviceInfo device, const std::vector>& notifications); void SendNotifictionRequest(const std::shared_ptr request, const DistributedDeviceInfo& peerDevice, bool isSyncNotification = false); - void SetNotificationExtendInfo(const sptr notificationRequest, - int32_t deviceType, std::shared_ptr& requestBox); + bool SetNotificationExtendInfo(const sptr notificationRequest, + int32_t deviceType, bool isSyncNotification, std::shared_ptr& requestBox); private: void SyncNotifictionList(const DistributedDeviceInfo& peerDevice, const std::vector& notificationList); @@ -60,6 +62,10 @@ private: std::shared_ptr MakeRemvoeBox(std::string &hashCode, int32_t &slotTypes); std::shared_ptr MakeBatchRemvoeBox(std::vector& hashCodes, std::string &slotTypes); + bool FillSyncRequestExtendInfo(const sptr notificationRequest, int32_t deviceTypeId, + std::shared_ptr& requestBox, AAFwk::WantParams& wantParam); + bool FillNotSyncRequestExtendInfo(const sptr notificationRequest, int32_t deviceType, + std::shared_ptr& requestBox, AAFwk::WantParams& wantParam); #else void PublishNotification(const std::shared_ptr& boxMessage); void PublishSynchronousLiveView(const std::shared_ptr& boxMessage); diff --git a/services/distributed/src/helper/bundle_resource_helper.cpp b/services/distributed/src/helper/bundle_resource_helper.cpp index dec9d5cbe17c46ab1a91b76df2f20a2b7510d693..9262a30a660c5be3bd762bbb94facee31bada330 100644 --- a/services/distributed/src/helper/bundle_resource_helper.cpp +++ b/services/distributed/src/helper/bundle_resource_helper.cpp @@ -157,8 +157,18 @@ ErrCode BundleResourceHelper::GetApplicationInfo(const std::string &appName, int bool BundleResourceHelper::CheckSystemApp(const std::string& bundleName, int32_t userId) { AppExecFwk::BundleInfo bundleInfo; - int32_t flags = static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); + if (GetBundleInfoV9(bundleName, userId, bundleInfo) != ERR_OK) { + return false; + } + ANS_LOGI("Get installed bundle %{public}s %{public}d.", bundleName.c_str(), + bundleInfo.applicationInfo.isSystemApp); + return bundleInfo.applicationInfo.isSystemApp; +} +ErrCode BundleResourceHelper::GetBundleInfoV9(const std::string& bundleName, int32_t userId, + AppExecFwk::BundleInfo& bundleInfo) +{ + int32_t flags = static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); std::lock_guard lock(connectionMutex_); Connect(); if (bundleMgr_ == nullptr) { @@ -169,11 +179,9 @@ bool BundleResourceHelper::CheckSystemApp(const std::string& bundleName, int32_t IPCSkeleton::SetCallingIdentity(identity); if (result != ERR_OK) { ANS_LOGW("Get installed bundle %{public}s failed.", bundleName.c_str()); - return false; + return result; } - ANS_LOGI("Get installed bundle %{public}s %{public}d.", bundleName.c_str(), - bundleInfo.applicationInfo.isSystemApp); - return bundleInfo.applicationInfo.isSystemApp; + return ERR_OK; } } } diff --git a/services/distributed/src/soft_bus/distributed_device_service.cpp b/services/distributed/src/soft_bus/distributed_device_service.cpp index 369929e99239a02679e3d358a4008b817ea91e24..01b52c1604e807fda3d4afe23b2549a173216d94 100644 --- a/services/distributed/src/soft_bus/distributed_device_service.cpp +++ b/services/distributed/src/soft_bus/distributed_device_service.cpp @@ -72,12 +72,7 @@ DistributedDeviceInfo DistributedDeviceService::GetLocalDevice() bool DistributedDeviceService::IsLocalPadOrPC() { - if (localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_2IN1 && - localDevice_.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_PC) { - return false; - } - return true; + return localDevice_.IsPadOrPc(); } bool DistributedDeviceService::IsReportDataByHa() @@ -99,9 +94,7 @@ bool DistributedDeviceService::CheckNeedSubscribeAllConnect() { std::lock_guard lock(mapLock_); for (auto& device : peerDevice_) { - if (device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD || - device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_2IN1 || - device.second.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PC) { + if (device.second.IsPadOrPc()) { return true; } } 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 15c1c661f1ae9f926868640dccc0394c13ca74b6..92c632e31e4d4e147680e4e43ee9bd2ca6a323f2 100644 --- a/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp +++ b/services/distributed/src/soft_bus/distributed_publish_service_v2.cpp @@ -57,6 +57,7 @@ static const std::string EXTENDINFO_DEVICE_ID = "deviceId"; static const std::string EXTENDINFO_ENABLE_CHECK = "check"; static const std::string EXTENDINFO_DEVICETYPE = "deviceType"; static const std::string EXTENDINFO_LOCALTYPE = "localType"; +static const uint32_t UNLOCKED_USED_FLAG = 3; DistributedPublishService& DistributedPublishService::GetInstance() { @@ -317,6 +318,73 @@ std::shared_ptr DistributedPublishService::MakeBatch return batchRemoveBox; } +void DistributedPublishService::SyncLiveViewList(const DistributedDeviceInfo device, + const std::vector>& notifications) +{ + if (device.IsPadOrPc()) { + ANS_LOGI("Dans no need sync list."); + return; + } + + std::vector notificationList; + for (auto& notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || + !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + ANS_LOGI("Dans no need sync remove notification."); + continue; + } + notificationList.push_back(notification->GetKey()); + } + SyncNotifictionList(device, notificationList); +} + +void DistributedPublishService::SyncLiveViewContent(const DistributedDeviceInfo device, + const std::vector>& notifications) +{ + std::vector bundlesList; + bool checkBundleExist = false; + if (device.IsPadOrPc()) { + std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(device.deviceType_); + if (deviceType.empty()) { + ANS_LOGW("Dans sync invalid %{public}s %{public}u.", StringAnonymous(device.deviceId_).c_str(), + device.deviceType_); + return; + } + if (NotificationHelper::GetTargetDeviceBundleList(deviceType, device.udid_, bundlesList) != ERR_OK) { + ANS_LOGW("Get bundles %{public}s %{public}u.", StringAnonymous(device.deviceId_).c_str(), + device.deviceType_); + return; + } + ANS_LOGI("Get bundles size %{public}zu.", bundlesList.size()); + checkBundleExist = true; + } + + std::unordered_set bundleSet(bundlesList.begin(), bundlesList.end()); + for (auto& notification : notifications) { + if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || + !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { + ANS_LOGI("Dans no need sync notification."); + continue; + } + + auto requestPoint = notification->GetNotificationRequestPoint(); + std::string bundleName = requestPoint->GetOwnerBundleName(); + if (checkBundleExist && bundleSet.count(bundleName)) { + ANS_LOGI("Dans no need sync %{public}d %{public}s.", checkBundleExist, bundleName.c_str()); + continue; + } + + int32_t userId = requestPoint->GetOwnerUserId(); + if (DelayedSingleton::GetInstance()->CheckSystemApp(bundleName, userId)) { + ANS_LOGI("Bundle system no sycn %{public}d %{public}s.", userId, bundleName.c_str()); + return; + } + + std::shared_ptr sharedNotification = std::make_shared(*notification); + SendNotifictionRequest(sharedNotification, device, true); + } +} + void DistributedPublishService::SyncLiveViewNotification(const DistributedDeviceInfo peerDevice, bool isForce) { if (!DistributedDeviceService::GetInstance().IsSyncLiveView(peerDevice.deviceId_, isForce)) { @@ -349,26 +417,8 @@ void DistributedPublishService::SyncLiveViewNotification(const DistributedDevice return; } - std::vector notificationList; - for (auto& notification : notifications) { - if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || - !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { - ANS_LOGI("Dans no need sync remove notification."); - continue; - } - notificationList.push_back(notification->GetKey()); - } - SyncNotifictionList(device, notificationList); - - for (auto& notification : notifications) { - if (notification == nullptr || notification->GetNotificationRequestPoint() == nullptr || - !notification->GetNotificationRequestPoint()->IsCommonLiveView()) { - ANS_LOGI("Dans no need sync notification."); - continue; - } - std::shared_ptr sharedNotification = std::make_shared(*notification); - SendNotifictionRequest(sharedNotification, device, true); - } + SyncLiveViewList(device, notifications); + SyncLiveViewContent(device, notifications); DistributedDeviceService::GetInstance().SetDeviceSyncData(device.deviceId_, DistributedDeviceService::SYNC_LIVE_VIEW, true); } @@ -376,7 +426,7 @@ void DistributedPublishService::SyncLiveViewNotification(const DistributedDevice void DistributedPublishService::SyncNotifictionList(const DistributedDeviceInfo& peerDevice, const std::vector& notificationList) { - ANS_LOGI("Dans sync notification %{public}d.", (int32_t)(notificationList.size())); + ANS_LOGI("Dans sync notification %{public}zu.", notificationList.size()); std::shared_ptr notificationSyncBox = std::make_shared(); notificationSyncBox->SetLocalDeviceId(peerDevice.deviceId_); notificationSyncBox->SetNotificationEmpty(notificationList.empty()); @@ -406,9 +456,6 @@ void DistributedPublishService::SendNotifictionRequest(const std::shared_ptrGetKey().c_str(), requestPoint->GetFlags() == nullptr ? "null" : requestPoint->GetFlags()->Dump().c_str()); auto local = DistributedDeviceService::GetInstance().GetLocalDevice(); - if (peerDevice.deviceType_ != DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) { - requestBox->SetNotificationBasicInfo(requestPoint->CollaborationToJson()); - } requestBox->SetDeviceId(local.deviceId_); requestBox->SetAutoDeleteTime(requestPoint->GetAutoDeletedTime()); requestBox->SetFinishTime(requestPoint->GetFinishDeadLine()); @@ -433,7 +480,9 @@ void DistributedPublishService::SendNotifictionRequest(const std::shared_ptrSetCommonLiveView(buffer); } - SetNotificationExtendInfo(requestPoint, peerDevice.deviceType_, requestBox); + if (!SetNotificationExtendInfo(requestPoint, peerDevice.deviceType_, isSyncNotification, requestBox)) { + return; + } SetNotificationButtons(requestPoint, peerDevice.deviceType_, requestPoint->GetSlotType(), requestBox); SetNotificationContent(request->GetNotificationRequestPoint()->GetContent(), requestPoint->GetNotificationType(), requestBox); @@ -546,8 +595,89 @@ void DistributedPublishService::SetNotificationButtons(const sptr notificationRequest, - int32_t deviceType, std::shared_ptr& requestBox) +bool DistributedPublishService::FillSyncRequestExtendInfo(const sptr notificationRequest, + int32_t deviceTypeId, std::shared_ptr& requestBox, AAFwk::WantParams& wantParam) +{ + std::string appName; + auto params = notificationRequest->GetExtendInfo(); + if (params != nullptr) { + wantParam = *params; + appName = params->GetStringParam("notification_collaboration_app_name"); + } + std::string deviceType = DistributedDeviceService::DeviceTypeToTypeString(deviceTypeId); + std::string bundleName = appName.empty() ? notificationRequest->GetOwnerBundleName() : appName; + AppExecFwk::BundleResourceInfo resourceInfo; + if (DelayedSingleton::GetInstance()->GetBundleInfo(bundleName, resourceInfo) != ERR_OK) { + ANS_LOGW("Dans get bundle icon failed %{public}s.", bundleName.c_str()); + return false; + } + + int32_t userId; + std::string deviceId; + if (NotificationHelper::GetMutilDeviceStatus(deviceType, UNLOCKED_USED_FLAG, deviceId, userId) != ERR_OK) { + ANS_LOGW("Dans get status failed %{public}s.", deviceType.c_str()); + return false; + } + + if (appName.empty()) { + int32_t ownerUserId = notificationRequest->GetOwnerUserId(); + AppExecFwk::BundleInfo bundleInfo; + if (DelayedSingleton::GetInstance()->GetBundleInfoV9(bundleName, ownerUserId, + bundleInfo) != ERR_OK) { + ANS_LOGE("Dans get application, %{public}d, %{public}s", deviceTypeId, bundleName.c_str()); + return false; + } + + 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_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, + deviceId.c_str(), userId); + return true; + } + wantParam.SetParam(EXTENDINFO_INFO_PRE + EXTENDINFO_DEVICE_ID + "_" + deviceType, AAFwk::String::Box(deviceId)); + requestBox->SetSmallIcon(AnsImageUtil::CreatePixelMapByString(resourceInfo.icon)); + requestBox->SetReceiverUserId(userId); + return true; +} + +bool DistributedPublishService::FillNotSyncRequestExtendInfo(const sptr notificationRequest, + int32_t deviceType, std::shared_ptr& requestBox, AAFwk::WantParams& wantParam) +{ + auto params = notificationRequest->GetExtendInfo(); + if (params == nullptr) { + ANS_LOGW("Fill box invalid data."); + return false; + } + std::string content = params->GetStringParam("notification_collaboration_app_name"); + if (content.empty()) { + ANS_LOGI("Fill box invalid app name."); + return false; + } + AppExecFwk::BundleResourceInfo resourceInfo; + if (DelayedSingleton::GetInstance()->GetBundleInfo(content, resourceInfo) != 0) { + ANS_LOGW("Dans get bundle icon failed %{public}s.", content.c_str()); + return false; + } + std::shared_ptr icon = AnsImageUtil::CreatePixelMapByString(resourceInfo.icon); + requestBox->SetSmallIcon(icon); + std::string key = EXTENDINFO_INFO_PRE + EXTENDINFO_USERID + + DistributedDeviceService::DeviceTypeToTypeString(deviceType); + int32_t userId = params->GetIntParam(key, -1); + if (userId != -1) { + requestBox->SetReceiverUserId(userId); + } + wantParam = *params; + return true; +} + +bool DistributedPublishService::SetNotificationExtendInfo(const sptr notificationRequest, + int32_t deviceType, bool isSyncNotification, std::shared_ptr& requestBox) { if (notificationRequest->GetBigIcon() != nullptr) { requestBox->SetBigIcon(notificationRequest->GetBigIcon(), deviceType); @@ -558,32 +688,35 @@ void DistributedPublishService::SetNotificationExtendInfo(const sptrGetLittleIcon() != nullptr) { requestBox->SetSmallIcon(notificationRequest->GetLittleIcon()); } - auto params = notificationRequest->GetExtendInfo(); - if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH || params == nullptr) { + if (deviceType == DistributedHardware::DmDeviceType::DEVICE_TYPE_WATCH) { ANS_LOGI("Send request no extend info %{public}d.", deviceType); - return; + return true; } - std::string content = params->GetStringParam(EXTENDINFO_INFO_PRE + EXTENDINFO_APP_NAME); - if (!content.empty()) { - AppExecFwk::BundleResourceInfo resourceInfo; - if (DelayedSingleton::GetInstance()->GetBundleInfo(content, resourceInfo) != 0) { - ANS_LOGW("Dans get bundle icon failed %{public}s.", content.c_str()); - return; - } - std::shared_ptr icon = AnsImageUtil::CreatePixelMapByString(resourceInfo.icon); - requestBox->SetSmallIcon(icon); + + std::string basicInfo; + if (!notificationRequest->CollaborationToJson(basicInfo)) { + ANS_LOGW("Dans OnConsumed collaboration json failed."); + return false; } + requestBox->SetNotificationBasicInfo(basicInfo); - std::string key = EXTENDINFO_INFO_PRE + EXTENDINFO_USERID + - DistributedDeviceService::DeviceTypeToTypeString(deviceType); - int32_t userId = params->GetIntParam(key, -1); - if (userId != -1) { - requestBox->SetReceiverUserId(userId); + AAFwk::WantParams wantParam; + if (isSyncNotification) { + if (!FillSyncRequestExtendInfo(notificationRequest, deviceType, requestBox, wantParam)) { + ANS_LOGW("Dans fill sync failed."); + return false; + } + } else { + if (!FillNotSyncRequestExtendInfo(notificationRequest, deviceType, requestBox, wantParam)) { + ANS_LOGW("Dans fill not sync failed."); + return false; + } } - params->DumpInfo(0); - AAFwk::WantParamWrapper wantWrapper(*params); + wantParam.DumpInfo(0); + AAFwk::WantParamWrapper wantWrapper(wantParam); requestBox->SetBoxExtendInfo(wantWrapper.ToString()); requestBox->SetDeviceUserId(DistributedSubscribeService::GetCurrentActiveUserId()); + return true; } #else @@ -662,8 +795,7 @@ void DistributedPublishService::PublishSynchronousLiveView(const std::shared_ptr return; } - ANS_LOGI("Dans handle sync notification %{public}d %{public}d.", (int32_t)(notificationList.size()), - (int32_t)(notifications.size())); + ANS_LOGI("Dans sync notification %{public}zu %{public}zu.", notificationList.size(), notifications.size()); for (auto item : notificationList) { ANS_LOGI("Dans sync %{public}s.", item.c_str()); } diff --git a/services/distributed/src/soft_bus/distributed_service.cpp b/services/distributed/src/soft_bus/distributed_service.cpp index 9caf35560e181d9a0fe64cb12911f3d50617fccc..549abe7defd5de491d7b18fb797bd21bf48df066 100644 --- a/services/distributed/src/soft_bus/distributed_service.cpp +++ b/services/distributed/src/soft_bus/distributed_service.cpp @@ -137,10 +137,7 @@ void DistributedService::AddDevice(DistributedDeviceInfo device) DistributedDeviceInfo deviceItem = device; deviceItem.peerState_ = DeviceState::STATE_SYNC; DistributedDeviceService::GetInstance().AddDeviceInfo(deviceItem); - if (device.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PAD || - device.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_2IN1 || - device.deviceType_ == DistributedHardware::DmDeviceType::DEVICE_TYPE_PC || - DistributedDeviceService::GetInstance().IsLocalPadOrPC()) { + if (device.IsPadOrPc() || DistributedDeviceService::GetInstance().IsLocalPadOrPC()) { ANS_LOGI("Dans wait peer %{public}s.", StringAnonymous(device.deviceId_).c_str()); return; } diff --git a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp index 45dfd1ea9859e68efa61bb87d3daede9f04e8496..5bbf9c0401bb48056884e15a6394e5197f50c8de 100644 --- a/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp +++ b/test/fuzztest/notificationhelper_fuzzer/notificationhelper_fuzzer.cpp @@ -291,7 +291,12 @@ public: notificationHelper.SetTargetDeviceSwitch(deviceType, deviceId, fdp->ConsumeBool(), fdp->ConsumeBool()); - + notificationHelper.GetTargetDeviceBundleList(deviceType, deviceId, bundleList); + + int32_t targetUserId; + std::string targetDeviceId; + notificationHelper.GetMutilDeviceStatus(deviceType, controlFlag, targetDeviceId, targetUserId); + sptr disturbProfile = ObjectBuilder::Build(fdp); notificationHelper.GetDoNotDisturbProfile(uid, disturbProfile); diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index eaeceab1c66e7c03f4d0107c8bf86a05ac4daf9a..77390d824ec45803cc02acc712d4bf48d3fa1dc7 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -726,6 +726,18 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode GetTargetDeviceBundleList(const std::string& deviceType, const std::string& deviceId, + std::vector& bundleList) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode GetMutilDeviceStatus(const std::string &deviceType, const uint32_t status, + std::string& deviceId, int32_t& userId) override + { + return ERR_ANS_INVALID_PARAM; + } + private: std::string cmd_; std::string bundle_;