diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 1f8fb171066cc695f99f9068cf7e750a817030b2..15f7f0c44c6924b681a1b692e8253f36c226017a 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -154,9 +154,9 @@ ErrCode NotificationHelper::IsAllowedNotifySelf(bool &allowed) return DelayedSingleton::GetInstance()->IsAllowedNotifySelf(allowed); } -ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, bool &popFlag) +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, const sptr &callbackInfo) { - return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, popFlag); + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, callbackInfo); } ErrCode NotificationHelper::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index 0ef7e83d56c3aa5ca75d0c1e577d805eb7825611..8d1718553d79481cd318bc5ccfa21c34d7386fbe 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -417,9 +417,9 @@ HWTEST_F(NotificationHelperTest, RequestEnableNotification_00001, Function | Sma { std::string deviceId = "DeviceId"; NotificationHelper notificationHelper; - bool needPop = true; - ErrCode ret = notificationHelper.RequestEnableNotification(deviceId, needPop); - EXPECT_EQ(ret, (int)ERR_ANS_INVALID_BUNDLE); + sptr callbackInfo; + ErrCode ret = notificationHelper.RequestEnableNotification(deviceId, callbackInfo); + EXPECT_EQ(ret, (int)ERR_ANS_PARCELABLE_FAILED); } /** diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index a2a91826869321f8f5329ce35dd80659dbda8774..599042a3908dabbfc673cf4e7314249909431eaf 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -343,10 +343,10 @@ public: * @brief Allow notifications to be sent based on the deviceId. * * @param deviceId Indicates the device Id. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackinfo. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) = 0; + virtual ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callbackInfo) = 0; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index 4d736b1a23cfb13c2937ba8c6367df6beb024e4f..a72c962297b4c266d33d61a09df5fa86286893bc 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -331,10 +331,10 @@ public: * @brief Allow notifications to be sent based on the deviceId. * * @param deviceId Indicates the device Id. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackinfo. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callbackInfo) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index a770d555391faa3ef72ac1641030f45354169bdc..6d28330a43bc2e2fab3dd3294c8e011c535e1dcf 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -347,10 +347,11 @@ public: * @brief Allow notifications to be sent based on the deviceId. * * @param deviceId Indicates the device Id. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackInfo. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + ErrCode RequestEnableNotification( + const std::string &deviceId, const sptr &callbackInfo) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 59faa10115491f1a5f047e3e1be55e30a359eb0d..5dcd6a55fe6fd36fa6cec5c1391b2eddedd670f3 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -281,10 +281,10 @@ public: * * @param deviceId Indicates the ID of the device running the application. At present, this parameter can * only be null or an empty string, indicating the current device. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackInfo. * @return Returns set notifications enabled for default bundle result. */ - ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); + ErrCode RequestEnableNotification(std::string &deviceId, const sptr &callbackInfo); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 195a3f443b7360bc8dcff1b7e278d5ee25750105..01851d24bef6fae147f024b7cb47a83693000bc1 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1103,7 +1103,7 @@ ErrCode AnsManagerProxy::UpdateSlots( return result; } -ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, bool &popFlag) +ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, const sptr &callbackInfo) { ANS_LOGI("enter"); MessageParcel data; @@ -1117,6 +1117,11 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, return ERR_ANS_PARCELABLE_FAILED; } + if (!data.WriteRemoteObject(callbackInfo)) { + ANS_LOGE("[RequestEnableNotification] fail: write callbackInfo failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(REQUEST_ENABLE_NOTIFICATION, option, data, reply); @@ -1129,11 +1134,6 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, ANS_LOGE("[RequestEnableNotification] fail: read result failed."); return ERR_ANS_PARCELABLE_FAILED; } - - if (!reply.ReadBool(popFlag)) { - ANS_LOGE("[RequestEnableNotification] fail: read popFlag failed."); - return ERR_ANS_PARCELABLE_FAILED; - } return result; } diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index 3b39d6b93e793bf0e7cd5d5eb46ea57d65d46d30..8ffc32c44d7fc47db6ecf890cc04d5eeaa8b1f62 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -938,21 +938,20 @@ ErrCode AnsManagerStub::HandleRequestEnableNotification(MessageParcel &data, Mes { ANS_LOGI("enter"); std::string deviceId; - bool popFlag = false; if (!data.ReadString(deviceId)) { ANS_LOGE("[HandleRequestEnableNotification] fail: read deviceId failed."); return ERR_ANS_PARCELABLE_FAILED; } - ErrCode result = RequestEnableNotification(deviceId, popFlag); - if (!reply.WriteInt32(result)) { - ANS_LOGE("[HandleRequestEnableNotification] fail: write result failed, ErrCode=%{public}d", result); + sptr callbackInfo = data.ReadRemoteObject(); + if (callbackInfo == nullptr) { + ANS_LOGE("[HandleRequestEnableNotification] fail: read callbackInfo failed"); return ERR_ANS_PARCELABLE_FAILED; } - if (!reply.WriteBool(popFlag)) { - ANS_LOGE("[HandleRequestEnableNotification] fail: write popFlag failed, ErrCode=%{public}d", popFlag); + ErrCode result = RequestEnableNotification(deviceId, callbackInfo); + if (!reply.WriteInt32(result)) { + ANS_LOGE("[HandleRequestEnableNotification] fail: write result failed, ErrCode=%{public}d", result); return ERR_ANS_PARCELABLE_FAILED; } - ANS_LOGD("Write popFlag into reply. popFlag = %{public}d", popFlag); return ERR_OK; } @@ -1998,7 +1997,7 @@ ErrCode AnsManagerStub::UpdateSlots( return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, bool &popFlag) +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, const sptr &callbackInfo) { ANS_LOGE("AnsManagerStub::RequestEnableNotification called!"); return ERR_INVALID_OPERATION; diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 67371739decf88f299ace83565aefb7c94df601f..b2b064fd667a60c499f34ca84158da0ac4b74c75 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -373,14 +373,14 @@ ErrCode AnsNotification::IsAllowedNotifySelf(bool &allowed) return ansManagerProxy_->IsAllowedNotifySelf(allowed); } -ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, bool &popFlag) +ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, const sptr &callbackInfo) { ANS_LOGD("enter"); if (!GetAnsManagerProxy()) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - return ansManagerProxy_->RequestEnableNotification(deviceId, popFlag); + return ansManagerProxy_->RequestEnableNotification(deviceId, callbackInfo); } ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp index dc68a67b3cc8b1c7d68d2bc629877fbc8c81fd6c..98b95bffd64adbe71663959c966f46df7684a980 100644 --- a/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_proxy_test/ans_manager_proxy_unit_test.cpp @@ -1035,8 +1035,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0100, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + int32_t result = proxy->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); } @@ -1056,8 +1056,8 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0200, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = ""; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + int32_t result = proxy->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); } @@ -1079,10 +1079,9 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0300, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + int32_t result = proxy->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(ERR_OK, result); - EXPECT_EQ(true, popFlag); } /* @@ -1102,57 +1101,11 @@ HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0400, Function | std::shared_ptr proxy = std::make_shared(iremoteObject); ASSERT_NE(nullptr, proxy); std::string deviceId = "Device"; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + int32_t result = proxy->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(ERR_ANS_TRANSACT_FAILED, result); } -/* - * @tc.name: RequestEnableNotificationTest_0500 - * @tc.desc: test RequestEnableNotification function - * @tc.type: FUNC - * @tc.require: #I5XO2O - */ -HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0500, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) - << "AnsManagerProxyUnitTest, RequestEnableNotificationTest_0500, TestSize.Level1"; - sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObject); - EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1) - .WillRepeatedly(DoAll(Invoke(std::bind(SendRequestReplace, _1, _2, _3, _4, - ERR_OK, false, true, true)), Return(NO_ERROR))); - std::shared_ptr proxy = std::make_shared(iremoteObject); - ASSERT_NE(nullptr, proxy); - std::string deviceId = "Device"; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); - EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); -} - -/* - * @tc.name: RequestEnableNotificationTest_0600 - * @tc.desc: test RequestEnableNotification function - * @tc.type: FUNC - * @tc.require: #I5XO2O - */ -HWTEST_F(AnsManagerProxyUnitTest, RequestEnableNotificationTest_0600, Function | MediumTest | Level1) -{ - GTEST_LOG_(INFO) - << "AnsManagerProxyUnitTest, RequestEnableNotificationTest_0600, TestSize.Level1"; - sptr iremoteObject = new (std::nothrow) MockIRemoteObject(); - ASSERT_NE(nullptr, iremoteObject); - EXPECT_CALL(*iremoteObject, SendRequest(_, _, _, _)).Times(1) - .WillRepeatedly(DoAll(Invoke(std::bind(SendRequestReplace, _1, _2, _3, _4, - ERR_OK, true, true, false)), Return(NO_ERROR))); - std::shared_ptr proxy = std::make_shared(iremoteObject); - ASSERT_NE(nullptr, proxy); - std::string deviceId = "Device"; - bool popFlag = false; - int32_t result = proxy->RequestEnableNotification(deviceId, popFlag); - EXPECT_EQ(ERR_ANS_PARCELABLE_FAILED, result); -} - /* * @tc.name: RemoveSlotByTypeTest_0100 * @tc.desc: test RemoveSlotByType function diff --git a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp index d3f6cc428cdae7d1d8859d2f815902998f24282f..73cd2606585c8c9fe5276a6ffa64ae94b16f3bb6 100644 --- a/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp +++ b/frameworks/core/test/unittest/ans_manager_stub_test/ans_manager_stub_test.cpp @@ -1468,12 +1468,14 @@ HWTEST_F(AnsManagerStubTest, HandleRequestEnableNotification01, Function | Small MessageOption option = {MessageOption::TF_SYNC}; std::string deviceId = "this is a deviceId"; + sptr callbackInfo; data.WriteInterfaceToken(AnsManagerStub::GetDescriptor()); data.WriteString(deviceId); + data.WriteRemoteObject(callbackInfo); ErrCode ret = ansManagerStub_->OnRemoteRequest(code, data, reply, option); - EXPECT_EQ(ret, (int)ERR_OK); + EXPECT_EQ(ret, (int)ERR_ANS_PARCELABLE_FAILED); } /** @@ -3786,9 +3788,8 @@ HWTEST_F(AnsManagerStubTest, UpdateSlots01, Function | SmallTest | Level1) HWTEST_F(AnsManagerStubTest, RequestEnableNotification01, Function | SmallTest | Level1) { std::string deviceId = "this is deviceId"; - bool popFlag = true; - - ErrCode result = ansManagerStub_->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + ErrCode result = ansManagerStub_->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(result, (int)ERR_INVALID_OPERATION); } diff --git a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp index 8d7d2fcc757e6f3f700776bafdd3832503237d83..64c5910607ec13e9c4f553e927df3ceef21f4587 100644 --- a/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_test/ans_notification_unit_test.cpp @@ -334,8 +334,8 @@ HWTEST_F(AnsNotificationUnitTest, RequestEnableNotification_0100, Function | Med ASSERT_NE(nullptr, proxy); ans_->GetAnsManagerProxy(); std::string deviceId = "this is deviceId"; - bool popFlag = true; - ErrCode ret1 = ans_->RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + ErrCode ret1 = ans_->RequestEnableNotification(deviceId, callbackInfo); EXPECT_EQ(ret1, ERR_ANS_SERVICE_NOT_CONNECTED); bool suspended = true; ErrCode ret2 = ans_->AreNotificationsSuspended(suspended); diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index 70c2fb23d4a6985be1dd732a2474a555a9ae159d..6a8a65d353dc410c04ff76155a0d6bedad9640fd 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -78,7 +78,6 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, bool CreateCallbackStubImpl(AsyncCallbackInfoIsEnable *callbackInfo); void ResetCallbackStubImpl(); -void StartNotificationDialog(AsyncCallbackInfoIsEnable *callbackInfo); } // namespace NotificationNapi } // namespace OHOS #endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H \ No newline at end of file diff --git a/frameworks/js/napi/src/enable_notification.cpp b/frameworks/js/napi/src/enable_notification.cpp index f1ab35f1c0aa8c5a162fd12f67c2d16f328b3a80..9c144dc030c0c5a02dc6b266d624832ff03a317c 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -342,23 +342,19 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); if (asynccallbackinfo) { std::string deviceId {""}; - bool popFlag = false; - asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId, popFlag); - asynccallbackinfo->params.allowToPop = popFlag; - ANS_LOGI("errorCode = %{public}d, allowToPop = %{public}d", - asynccallbackinfo->info.errorCode, asynccallbackinfo->params.allowToPop); - if (asynccallbackinfo->info.errorCode == ERR_OK && asynccallbackinfo->params.allowToPop) { - ANS_LOGI("Begin to start notification dialog"); - auto *callbackInfo = static_cast(data); - StartNotificationDialog(callbackInfo); + auto *callbackInfo = static_cast(data); + if (CreateCallbackStubImpl(callbackInfo)) { + asynccallbackinfo->info.errorCode = + NotificationHelper::RequestEnableNotification(deviceId, callbackStubImpl_); } } }, [](napi_env env, napi_status status, void *data) { AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); if (asynccallbackinfo) { - if (!(asynccallbackinfo->info.errorCode == ERR_OK && asynccallbackinfo->params.allowToPop)) { + if (asynccallbackinfo->info.errorCode != ERR_OK) { AsyncCompleteCallbackIsNotificationEnabled(env, status, data); + ResetCallbackStubImpl(); } } }, @@ -374,27 +370,6 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) } } -void StartNotificationDialog(AsyncCallbackInfoIsEnable *callbackInfo) -{ - ANS_LOGD("%{public}s, Begin Calling StartNotificationDialog.", __func__); - if (CreateCallbackStubImpl(callbackInfo)) { - sptr token; - auto result = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token); - if (result == ERR_OK) { - AAFwk::Want want; - want.SetElementName("com.ohos.notificationdialog", "EnableNotificationDialog"); - want.SetParam("callbackStubImpl_", callbackStubImpl_); - want.SetParam("tokenId", token); - want.SetParam("from", AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName()); - ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token, -1); - ANS_LOGD("%{public}s, End Calling StartNotificationDialog. ret=%{public}d", __func__, err); - } else { - ANS_LOGE("%{public}s, show notification dialog failed", __func__); - ResetCallbackStubImpl(); - } - } -} - bool CreateCallbackStubImpl(AsyncCallbackInfoIsEnable *callbackInfo) { std::lock_guard lock(callbackMutex_); diff --git a/frameworks/js/napi/src/manager/napi_enable_notification.cpp b/frameworks/js/napi/src/manager/napi_enable_notification.cpp index 41493987cadba154c1a63ef797bb49686e3ec745..9046be8bd0a75238347bf95b02130e7d7c3bc46a 100644 --- a/frameworks/js/napi/src/manager/napi_enable_notification.cpp +++ b/frameworks/js/napi/src/manager/napi_enable_notification.cpp @@ -235,15 +235,10 @@ napi_value NapiRequestEnableNotification(napi_env env, napi_callback_info info) AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); if (asynccallbackinfo) { std::string deviceId {""}; - bool popFlag = false; - asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId, popFlag); - asynccallbackinfo->params.allowToPop = popFlag; - ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, allowToPop = %{public}d", - asynccallbackinfo->info.errorCode, asynccallbackinfo->params.allowToPop); - if (asynccallbackinfo->info.errorCode == ERR_OK && asynccallbackinfo->params.allowToPop) { - ANS_LOGI("Begin to start notification dialog"); - auto *callbackInfo = static_cast(data); - StartNotificationDialog(callbackInfo); + auto *callbackInfo = static_cast(data); + if (CreateCallbackStubImpl(callbackInfo)) { + asynccallbackinfo->info.errorCode = + NotificationHelper::RequestEnableNotification(deviceId, callbackStubImpl_); } } }, diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index e153cc4e02f4aa3117ee32a55c6ca91c67fc236f..7c15d7c7ce99b69c2252d24fc5255cf4932ebdb1 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -283,10 +283,10 @@ public: * * @param deviceId Indicates the ID of the device running the application. At present, this parameter can * only be null or an empty string, indicating the current device. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackInfo. * @return Returns set notifications enabled for default bundle result. */ - static ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); + static ErrCode RequestEnableNotification(std::string &deviceId, const sptr &callbackInfo); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 08fdd61d66c6c102704e52fe55bc10eeb0093881..63f549b99bca459e43a290c2aac945002b5922b9 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -31,6 +31,7 @@ ohos_shared_library("libans") { "src/advanced_notification_service_ability.cpp", "src/bundle_manager_helper.cpp", "src/event_report.cpp", + "src/notification_dialog.cpp", "src/notification_preferences.cpp", "src/notification_preferences_database.cpp", "src/notification_preferences_info.cpp", @@ -52,6 +53,7 @@ ohos_shared_library("libans") { cflags = [] deps = [ + "${ability_runtime_services_path}/abilitymgr:abilityms", "${core_path}:ans_core", "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", ] @@ -67,6 +69,7 @@ ohos_shared_library("libans") { } external_deps = [ + "ability_runtime:ability_manager", "ability_runtime:wantagent_innerkits", "access_token:libaccesstoken_sdk", "c_utils:utils", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 958a4331aace703deefab906745494f334a0f760..d1526a75fa1184e91fffaf9c8e9dff392324703c 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -343,10 +343,11 @@ public: * @brief Allow notifications to be sent based on the deviceId. * * @param deviceId Indicates the device Id. - * @param popFlag Indicates the flag that allows dialog to be popped. + * @param callbackInfo Indicates the callbackInfo. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; + ErrCode RequestEnableNotification(const std::string &deviceId, const sptr &callbackInfo +) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/services/ans/include/notification_dialog.h b/services/ans/include/notification_dialog.h new file mode 100644 index 0000000000000000000000000000000000000000..0e924ec5b2c21c1b3fa2143bbb166ef3a1267709 --- /dev/null +++ b/services/ans/include/notification_dialog.h @@ -0,0 +1,43 @@ +/* + * 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 "ability_record.h" +#include "iremote_object.h" + +namespace OHOS { +namespace Notification { +class NotificationDialog { +public: + NotificationDialog() = default; + ~NotificationDialog() = default; + + /** + * @brief To judge whether the caller is current application. + * + * @param abilityRecord The abilityRecord of comparison. + * @return true if it is selfcalled, else not. + */ + bool JudgeSelfCalled(const std::shared_ptr &abilityRecord); + + /** + * @brief To start the enableNotificationDialog ability. + * + * @param callbackInfo The callbackInfo. + * @return ERR_OK if success, else not. + */ + ErrCode StartEnableNotificationDialogAbility(const sptr &callbackInfo); +}; +} // namespace Notification +} // namespace OHOS diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index a22918bd6da41eeb0431a87b070454fde5835916..a2e51342fbac301aed91ac9649636f5e6b7cc94d 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -39,6 +39,7 @@ #include "hitrace_meter.h" #include "ipc_skeleton.h" #include "notification_constant.h" +#include "notification_dialog.h" #include "notification_filter.h" #include "notification_preferences.h" #include "notification_slot.h" @@ -1438,7 +1439,8 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( return result; } -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, bool &popFlag) +ErrCode AdvancedNotificationService::RequestEnableNotification( + const std::string &deviceId, const sptr &callbackInfo) { ANS_LOGD("%{public}s", __FUNCTION__); @@ -1455,7 +1457,6 @@ ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string ANS_LOGI("result = %{public}d, allowedNotify = %{public}d", result, allowedNotify); if (result != ERR_OK || allowedNotify) { ANS_LOGD("Already granted permission"); - popFlag = false; return result; } @@ -1464,11 +1465,19 @@ ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string result = GetHasPoppedDialog(bundleOption, hasPopped); if (result != ERR_OK || hasPopped) { ANS_LOGD("Already shown dialog"); - popFlag = false; return result; } + + ANS_LOGI("hasPopped = %{public}d, allowedNotify = %{public}d", hasPopped, allowedNotify); + if (!hasPopped && !allowedNotify) { + auto notificationDialog = std::make_shared(); + result = notificationDialog->StartEnableNotificationDialogAbility(callbackInfo); + if (result != ERR_OK) { + ANS_LOGD("StartEnableNotificationDialogAbility failed, result = %{public}d", result); + return result; + } + } SetHasPoppedDialog(bundleOption, true); - popFlag = true; return result; } diff --git a/services/ans/src/notification_dialog.cpp b/services/ans/src/notification_dialog.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b8a7e50065b36c0fc7e49b560af9489d85b2f6f2 --- /dev/null +++ b/services/ans/src/notification_dialog.cpp @@ -0,0 +1,61 @@ +/* + * 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 "notification_dialog.h" + +#include "ability_manager_client.h" +#include "advanced_notification_service.h" +#include "ans_callback_stub.h" +#include "in_process_call_wrapper.h" +#include "ipc_skeleton.h" + +namespace OHOS { +namespace Notification { +bool NotificationDialog::JudgeSelfCalled(const std::shared_ptr &abilityRecord) +{ + auto callingTokenId = IPCSkeleton::GetCallingTokenID(); + auto tokenID = abilityRecord->GetApplicationInfo().accessTokenId; + if (callingTokenId != tokenID) { + ANS_LOGE("Is not self, not enabled"); + return false; + } + return true; +} + +ErrCode NotificationDialog::StartEnableNotificationDialogAbility(const sptr &callbackInfo) +{ + ANS_LOGD("%{public}s, Enter.", __func__); + sptr token; + int result = IN_PROCESS_CALL(AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token)); + std::shared_ptr ability = AAFwk::Token::GetAbilityRecordByToken(token); + if (result != ERR_OK) { + ANS_LOGD("%{public}s, GetTopAbility failed. result=%{public}d", __func__, result); + return result; + } + if (!JudgeSelfCalled(ability)) { + ANS_LOGD("%{public}s, if it is not selfcalled.", __func__); + return result; + } + AAFwk::Want want; + want.SetElementName("com.ohos.notificationdialog", "EnableNotificationDialog"); + want.SetParam("callbackStubImpl", callbackInfo); + want.SetParam("tokenId", token); + want.SetParam("from", AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility().GetBundleName()); + result = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token, -1); + ANS_LOGD("%{public}s, End Calling StartNotificationDialog. result=%{public}d", __func__, result); + return result; +} +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 3ad175ccb16c81a80c7b00fe53bb956cfd504187..fb137bd4bd9471deb4cfe573e7394bb22b0465ad 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -59,6 +59,7 @@ ohos_unittest("reminder_unit_test") { deps = [ "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", "//third_party/googletest:gtest_main", ] @@ -162,6 +163,7 @@ ohos_unittest("ans_unit_test") { deps = [ "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", "//third_party/googletest:gtest_main", ] diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index c3e5043749d2ff5cb5a744b7adfc52e799de62d7..bb72d85c2aea8c8869d5b6ea51b584b42248c9fe 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -28,6 +28,7 @@ #include "ans_ut_constant.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "iremote_object.h" #include "mock_ipc_skeleton.h" #include "notification_preferences.h" #include "notification_subscriber.h" @@ -1762,8 +1763,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_13100, sptr req = new NotificationRequest(); EXPECT_NE(req, nullptr); std::string deviceId = "DeviceId"; - bool needPop = false; - EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, needPop), (int)ERR_ANS_INVALID_PARAM); + EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, nullptr), (int)ERR_ANS_INVALID_PARAM); } /** @@ -2343,7 +2343,7 @@ HWTEST_F(AdvancedNotificationServiceTest, AdvancedNotificationServiceTest_16900, std::string deviceId = "DeviceId"; bool needPop = false; - EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, needPop), ERR_ANS_INVALID_BUNDLE); + EXPECT_EQ(advancedNotificationService_->RequestEnableNotification(deviceId, nullptr), ERR_ANS_INVALID_BUNDLE); EXPECT_EQ(advancedNotificationService_->IsAllowedNotifySelf(needPop), ERR_ANS_INVALID_BUNDLE); sptr bundleOption; EXPECT_EQ(advancedNotificationService_->IsAllowedNotifySelf(bundleOption, needPop), ERR_ANS_INVALID_BUNDLE); diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts index a7d0b9d1912ad3be1c094459211ff7aeab5c8ffe..8bea5bedd5d14827a4b651965c3029f3d8761718 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/ServiceExtAbility/NotificationServiceExtAbility.ts @@ -22,7 +22,7 @@ export default class NotificationDialogServiceExtensionAbility extends extension onCreate(want) { console.debug(TAG, "onCreate, want: " + JSON.stringify(want)); globalThis.notificationExtensionContext = this.context; - globalThis.callbackImp = want.parameters.callbackStubImpl_; + globalThis.callbackImp = want.parameters.callbackStubImpl; globalThis.closeDialog = () => { console.info(TAG, 'click waiting for a response'); globalThis.notificationExtensionContext.terminateSelf(); diff --git a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/notificationDialog.ets b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/notificationDialog.ets index a64eb2948f4c56d11960d4d6680fa7df469d0f76..6d116e96ace80d6414a75cf8e1a6a120f53e0ba1 100644 --- a/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/notificationDialog.ets +++ b/services/dialog_ui/enable_notification_dialog/entry/src/main/ets/pages/notificationDialog.ets @@ -144,7 +144,7 @@ struct NotificationDialog { console.log(this.TAG, "dialog page appears") this.want = globalThis.abilityWant this.style = globalThis.style - this.callBackImp_ = globalThis.abilityWant.parameters['callbackStubImpl_'].value + this.callBackImp_ = globalThis.abilityWant.parameters['callbackStubImpl'].value this.token_ = globalThis.abilityWant.parameters['tokenId'].value globalThis.bundleName = globalThis.abilityWant.parameters['from'] } diff --git a/services/test/moduletest/BUILD.gn b/services/test/moduletest/BUILD.gn index 553b20c74e3434d4db758afc224ec67edd56e199..68af1f643076324424d8a3cbe491a273c02567f4 100644 --- a/services/test/moduletest/BUILD.gn +++ b/services/test/moduletest/BUILD.gn @@ -60,6 +60,7 @@ ohos_moduletest("ans_module_test") { deps = [ "${core_path}:ans_core", "${frameworks_module_ans_path}:ans_innerkits", + "${services_path}/ans:libans", "//foundation/arkui/ace_engine/interfaces/inner_api/ui_service_manager:ui_service_mgr", ] diff --git a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp index 5e2ce969859b09d3595f8606dba594e7530b0008..8ead6cb8f6b471f6c27932b959743e190b118217 100644 --- a/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp +++ b/test/fuzztest/ansmanagerstub_fuzzer/ansmanagerstub_fuzzer.cpp @@ -160,8 +160,8 @@ namespace OHOS { ansManagerStub.DeleteAll(); ansManagerStub.GetSlotsByBundle(bundleOption, slots); ansManagerStub.UpdateSlots(bundleOption, slots); - bool popFlag = true; - ansManagerStub.RequestEnableNotification(deviceId, popFlag); + sptr callbackInfo; + ansManagerStub.RequestEnableNotification(deviceId, callbackInfo); const std::string bundle = "this is a notification bundle"; bool enabled = true; ansManagerStub.SetNotificationsEnabledForBundle(bundle, enabled); diff --git a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp index 74d4ad5bc9d0de35f15a77f2ad8ece37c1cafe41..25769a27c87fa70e4f8331cad887eed0d2f1f701 100644 --- a/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp +++ b/test/fuzztest/setnotificationbadgenum_fuzzer/setnotificationbadgenum_fuzzer.cpp @@ -21,8 +21,8 @@ namespace OHOS { { // test RequestEnableNotification function std::string deviceId(data); - bool needPop = true; - Notification::NotificationHelper::RequestEnableNotification(deviceId, needPop); + sptr callbackInfo; + Notification::NotificationHelper::RequestEnableNotification(deviceId, callbackInfo); // test AreNotificationsSuspended function bool suspended = true; Notification::NotificationHelper::AreNotificationsSuspended(suspended);