From 0311764a80227dcbd7cdeb11fa38e2ae64fb6930 Mon Sep 17 00:00:00 2001 From: chenyuyan Date: Tue, 27 Sep 2022 11:48:49 +0800 Subject: [PATCH 1/2] window before Signed-off-by: chenyuyan Change-Id: I8dc20a721d68b98fb719b618678999fb9d7ee4fe --- frameworks/core/BUILD.gn | 1 + .../core/include/ans_callback_interface.h | 40 ++++++++ frameworks/core/include/ans_callback_stub.h | 35 +++++++ frameworks/core/src/ans_callback_stub.cpp | 50 ++++++++++ frameworks/js/napi/BUILD.gn | 1 + .../js/napi/include/enable_notification.h | 49 +++++++++ .../js/napi/src/enable_notification.cpp | 99 +++++++++++++------ .../ans/src/advanced_notification_service.cpp | 26 +++++ 8 files changed, 269 insertions(+), 32 deletions(-) create mode 100644 frameworks/core/include/ans_callback_interface.h create mode 100644 frameworks/core/include/ans_callback_stub.h create mode 100644 frameworks/core/src/ans_callback_stub.cpp diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index 4f9ef97a8..35b830b1b 100644 --- a/frameworks/core/BUILD.gn +++ b/frameworks/core/BUILD.gn @@ -32,6 +32,7 @@ ohos_shared_library("ans_core") { sources = [ "${core_path}/common/src/ans_log_wrapper.cpp", "${core_path}/common/src/ans_watchdog.cpp", + "${core_path}/src/ans_callback_stub.cpp", "${core_path}/src/ans_image_util.cpp", "${core_path}/src/ans_manager_death_recipient.cpp", "${core_path}/src/ans_manager_proxy.cpp", diff --git a/frameworks/core/include/ans_callback_interface.h b/frameworks/core/include/ans_callback_interface.h new file mode 100644 index 000000000..6670acfc8 --- /dev/null +++ b/frameworks/core/include/ans_callback_interface.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H + +#include "iremote_broker.h" + +namespace OHOS { +namespace Notification { +class AnsCallbackInterface : public IRemoteBroker { +public: + AnsCallbackInterface() = default; + virtual ~AnsCallbackInterface() = default; + DISALLOW_COPY_AND_MOVE(AnsCallbackInterface); + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Notification.AnsCallbackInterface"); + + virtual bool OnEnableNotification(bool isAllow) = 0; + +protected: + enum InterfaceCode : uint32_t { + ON_ENABLE_NOTIFICATION_CALLBACK = 0, + }; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_INTERFACE_H diff --git a/frameworks/core/include/ans_callback_stub.h b/frameworks/core/include/ans_callback_stub.h new file mode 100644 index 000000000..00961e940 --- /dev/null +++ b/frameworks/core/include/ans_callback_stub.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H + +#include "ans_callback_interface.h" +#include "iremote_stub.h" + +namespace OHOS { +namespace Notification { +class AnsCallbackStub : public IRemoteStub { +public: + AnsCallbackStub() = default; + virtual ~AnsCallbackStub() override = default; + DISALLOW_COPY_AND_MOVE(AnsCallbackStub); + + int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +}; +} // namespace Notification +} // namespace OHOS + +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_ANS_CALLBACK_STUB_H diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp new file mode 100644 index 000000000..52366c032 --- /dev/null +++ b/frameworks/core/src/ans_callback_stub.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021 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 "ans_callback_stub.h" + +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "message_option.h" +#include "message_parcel.h" +#include "parcel.h" + +namespace OHOS { +namespace Notification { +int32_t AnsCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &flags) +{ + std::u16string descriptor = data.ReadInterfaceToken(); + if (descriptor != AnsCallbackStub::GetDescriptor()) { + ANS_LOGW("[OnRemoteRequest] fail: invalid interface token!"); + return OBJECT_NULL; + } + + if (InterfaceCode::ON_ENABLE_NOTIFICATION_CALLBACK == code) { + bool result = false; + if (!reply.ReadBool(result)) { + ANS_LOGE("read notification enabled result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + OnEnableNotification(result); + } else { + ANS_LOGW("[OnRemoteRequest] fail: unknown code!"); + return IRemoteStub::OnRemoteRequest(code, data, reply, flags); + } + return NO_ERROR; +} +} // namespace Notification +} // namespace OHOS diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 2c9a2b832..affb294c3 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -75,6 +75,7 @@ ohos_shared_library("notification") { external_deps = [ "ability_base:want", "ability_base:zuri", + "ability_runtime:ability_manager", "ability_runtime:abilitykit_native", "ability_runtime:wantagent_innerkits", "c_utils:utils", diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index 20f468012..bdf6a7087 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -15,16 +15,65 @@ #ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H #define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_INCLUDE_ENABLE_NOTIFICATION_H +#include + +#include "ans_callback_stub.h" #include "common.h" namespace OHOS { namespace NotificationNapi { using namespace OHOS::Notification; +using RequestEnableTask = std::function; + +struct EnableParams { + NotificationBundleOption option; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + EnableParams params; + CallbackPromiseInfo info; +}; + +struct IsEnableParams { + NotificationBundleOption option; + napi_ref callback = nullptr; + bool hasBundleOption = false; + int32_t userId = SUBSCRIBE_USER_INIT; + bool hasUserId = false; +}; + +struct AsyncCallbackInfoIsEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + IsEnableParams params; + CallbackPromiseInfo info; + bool allowed = false; +}; + +class CallbackStubImpl : public AnsCallbackStub { +public: + explicit CallbackStubImpl(AsyncCallbackInfoIsEnable *task) : task_(task) {}; + ~CallbackStubImpl() override = default; + bool OnEnableNotification(bool isAllow) override; + +private: + AsyncCallbackInfoIsEnable *task_; +}; + +static std::mutex mutex; +static sptr callbackStubImpl; napi_value EnableNotification(napi_env env, napi_callback_info info); napi_value IsNotificationEnabled(napi_env env, napi_callback_info info); napi_value IsNotificationEnabledSelf(napi_env env, napi_callback_info info); napi_value RequestEnableNotification(napi_env env, napi_callback_info info); +bool CreateCallbackStubImpl(void *data); +void ResetCallbackStubImpl(); +void StartNotificationDialog(void *data); } // 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 de7e99a4e..e5298c0f7 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -14,6 +14,7 @@ */ #include "enable_notification.h" +#include "ability_manager_client.h" namespace OHOS { namespace NotificationNapi { @@ -21,35 +22,6 @@ const int ENABLE_NOTIFICATION_MAX_PARA = 3; const int ENABLE_NOTIFICATION_MIN_PARA = 2; const int IS_NOTIFICATION_ENABLE_MAX_PARA = 2; -struct EnableParams { - NotificationBundleOption option; - bool enable = false; - napi_ref callback = nullptr; -}; - -struct AsyncCallbackInfoEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - EnableParams params; - CallbackPromiseInfo info; -}; - -struct IsEnableParams { - NotificationBundleOption option; - napi_ref callback = nullptr; - bool hasBundleOption = false; - int32_t userId = SUBSCRIBE_USER_INIT; - bool hasUserId = false; -}; - -struct AsyncCallbackInfoIsEnable { - napi_env env = nullptr; - napi_async_work asyncWork = nullptr; - IsEnableParams params; - CallbackPromiseInfo info; - bool allowed = false; -}; - napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnableParams ¶ms) { ANS_LOGI("enter"); @@ -355,12 +327,19 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) [](napi_env env, void *data) { ANS_LOGI("RequestEnableNotification napi_create_async_work start"); AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); - std::string deviceId {""}; + // TODO 后端需要修改下,判断是否允许弹窗 asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); - ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + if (asynccallbackinfo->info.errorCode == ERR_OK) { + StartNotificationDialog(data); + } + }, + [](napi_env env, napi_status status, void *data) { + AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo->info.errorCode != ERR_OK) { + AsyncCompleteCallbackIsNotificationEnabled(env, status, data); + } }, - AsyncCompleteCallbackIsNotificationEnabled, (void *)asynccallbackinfo, &asynccallbackinfo->asyncWork); @@ -372,5 +351,61 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) return promise; } } + +void StartNotificationDialog(void *data) +{ + ANS_LOGD("%{public}s, Begin Calling StartNotificationDialog.", __func__); + if (CreateCallbackStubImpl(data)) { + sptr token; + auto result = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token); + if (result == ERR_OK) { + AAFwk::Want want; + want.SetElementName("", ""); + want.SetParam("", callbackStubImpl); + 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(void *data) +{ + std::lock_guard lock(mutex); + if (callbackStubImpl != nullptr) { + return false; + } + auto *callbackInfo = static_cast(data); + callbackStubImpl = new CallbackStubImpl(callbackInfo); + return true; +} + +void ResetCallbackStubImpl() +{ + std::lock_guard lock(mutex); + callbackStubImpl = nullptr; +} + +bool CallbackStubImpl::OnEnableNotification(bool isAllow) +{ + if (task_) { + task_->allowed = isAllow; + napi_value resourceName = nullptr; + napi_create_string_latin1(task_->env, "RequestEnableNotification", NAPI_AUTO_LENGTH, &resourceName); + napi_create_async_work(task_->env, + nullptr, + resourceName, + [](napi_env env, void *data) {}, + AsyncCompleteCallbackIsNotificationEnabled, + (void *)task_, + &task_->asyncWork); + NAPI_CALL_BASE(task_->env, napi_queue_async_work(task_->env, task_->asyncWork), false); + return true; + } + return false; +} + } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 40eba448f..2dd700460 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1498,6 +1498,32 @@ ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string return result; } +//ErrCode AdvancedNotificationService::RequestEnableNotification(const bool &deviceId) +//{ +// ANS_LOGD("%{public}s", __FUNCTION__); +// +// ErrCode result = ERR_OK; +// sptr bundleOption = GenerateBundleOption(); +// if (bundleOption == nullptr) { +// return ERR_ANS_INVALID_BUNDLE; +// } +// +// bool allowedNotify = false; +// result = IsAllowedNotifySelf(bundleOption, allowedNotify); +// if (result != ERR_OK || allowedNotify) { +// ANS_LOGD("Already granted permission"); +// return result; +// } +// +// bool hasPopped = false; +// result = GetHasPoppedDialog(bundleOption, hasPopped); +// if (result != ERR_OK || hasPopped) { +// ANS_LOGD("Already shown dialog"); +// return result; +// } +// return result; +//} + ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) { return ERR_INVALID_OPERATION; -- Gitee From baf07717a6c6f3680a04800b9394466284a0b23e Mon Sep 17 00:00:00 2001 From: chenyuyan Date: Mon, 10 Oct 2022 14:55:19 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=BC=B9=E7=AA=97=E4=BD=BF=E8=83=BDTEST?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chenyuyan Change-Id: I64bd414eb2bcdda1c3e504abccaf71674fcd1c35 --- frameworks/ans/src/notification_helper.cpp | 4 +- frameworks/core/include/ans_callback_stub.h | 2 +- .../core/include/ans_manager_interface.h | 3 +- frameworks/core/include/ans_manager_proxy.h | 2 +- frameworks/core/include/ans_manager_stub.h | 3 +- frameworks/core/include/ans_notification.h | 2 +- frameworks/core/src/ans_callback_stub.cpp | 2 +- frameworks/core/src/ans_manager_proxy.cpp | 11 ++- frameworks/core/src/ans_manager_stub.cpp | 16 +++- frameworks/core/src/ans_notification.cpp | 4 +- .../js/napi/include/enable_notification.h | 4 +- .../js/napi/src/enable_notification.cpp | 31 ++++--- interfaces/inner_api/notification_helper.h | 2 +- .../include/advanced_notification_service.h | 3 +- .../ans/src/advanced_notification_service.cpp | 84 ++++--------------- 15 files changed, 80 insertions(+), 93 deletions(-) diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 823d8a89c..1f8fb1710 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) +ErrCode NotificationHelper::RequestEnableNotification(std::string &deviceId, bool &popFlag) { - return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId); + return DelayedSingleton::GetInstance()->RequestEnableNotification(deviceId, popFlag); } ErrCode NotificationHelper::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/core/include/ans_callback_stub.h b/frameworks/core/include/ans_callback_stub.h index 00961e940..c7e3c46cd 100644 --- a/frameworks/core/include/ans_callback_stub.h +++ b/frameworks/core/include/ans_callback_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * 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 diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index 472582a1c..a2a918268 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -343,9 +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. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RequestEnableNotification(const std::string &deviceId) = 0; + virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) = 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 0cc02ac0d..ae24faca5 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -333,7 +333,7 @@ public: * @param deviceId Indicates the device Id. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId) override; + ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) 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 a20c40d2e..a770d5553 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -347,9 +347,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. * @return Returns ERR_OK on success, others on failure. */ - virtual ErrCode RequestEnableNotification(const std::string &deviceId) override; + virtual ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) 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 6c5e718d0..b4656044d 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -283,7 +283,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - ErrCode RequestEnableNotification(std::string &deviceId); + ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/frameworks/core/src/ans_callback_stub.cpp b/frameworks/core/src/ans_callback_stub.cpp index 52366c032..85cd72cad 100644 --- a/frameworks/core/src/ans_callback_stub.cpp +++ b/frameworks/core/src/ans_callback_stub.cpp @@ -41,7 +41,7 @@ int32_t AnsCallbackStub::OnRemoteRequest( } OnEnableNotification(result); } else { - ANS_LOGW("[OnRemoteRequest] fail: unknown code!"); + ANS_LOGW("[OnRemoteRequest] fail: unknown code! %{public}d", code); return IRemoteStub::OnRemoteRequest(code, data, reply, flags); } return NO_ERROR; diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 4a0e2b96f..a51508740 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -1103,8 +1103,9 @@ ErrCode AnsManagerProxy::UpdateSlots( return result; } -ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) +ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId, bool &popFlag) { + ANS_LOGI("enter"); MessageParcel data; if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { ANS_LOGE("[RequestEnableNotification] fail: write interface token failed."); @@ -1116,6 +1117,10 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) return ERR_ANS_PARCELABLE_FAILED; } + if (!data.WriteBool(popFlag)) { + ANS_LOGE("[RequestEnableNotification] fail: write popFlag failed"); + return ERR_ANS_PARCELABLE_FAILED; + } MessageParcel reply; MessageOption option = {MessageOption::TF_SYNC}; ErrCode result = InnerTransact(REQUEST_ENABLE_NOTIFICATION, option, data, reply); @@ -1129,6 +1134,10 @@ ErrCode AnsManagerProxy::RequestEnableNotification(const std::string &deviceId) 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 63bcb0b6a..4c40bbec2 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -936,17 +936,27 @@ ErrCode AnsManagerStub::HandleUpdateSlots(MessageParcel &data, MessageParcel &re ErrCode AnsManagerStub::HandleRequestEnableNotification(MessageParcel &data, MessageParcel &reply) { + 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); + if (!data.ReadBool(popFlag)) { + ANS_LOGE("[HandleRequestEnableNotification] fail: read popFlag 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); return ERR_ANS_PARCELABLE_FAILED; } + if (!reply.WriteBool(popFlag)) { + ANS_LOGE("[HandleRequestEnableNotification] fail: write popFlag failed, ErrCode=%{public}d", popFlag); + return ERR_ANS_PARCELABLE_FAILED; + } + ANS_LOGD("Write popFlag into reply. popFlag = %{public}d", popFlag); return ERR_OK; } @@ -1974,7 +1984,7 @@ ErrCode AnsManagerStub::UpdateSlots( return ERR_INVALID_OPERATION; } -ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId) +ErrCode AnsManagerStub::RequestEnableNotification(const std::string &deviceId, bool &popFlag) { 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 a78070892..67371739d 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) +ErrCode AnsNotification::RequestEnableNotification(std::string &deviceId, bool &popFlag) { ANS_LOGD("enter"); if (!GetAnsManagerProxy()) { ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } - return ansManagerProxy_->RequestEnableNotification(deviceId); + return ansManagerProxy_->RequestEnableNotification(deviceId, popFlag); } ErrCode AnsNotification::AreNotificationsSuspended(bool &suspended) diff --git a/frameworks/js/napi/include/enable_notification.h b/frameworks/js/napi/include/enable_notification.h index bdf6a7087..8f3248850 100644 --- a/frameworks/js/napi/include/enable_notification.h +++ b/frameworks/js/napi/include/enable_notification.h @@ -44,6 +44,8 @@ struct IsEnableParams { bool hasBundleOption = false; int32_t userId = SUBSCRIBE_USER_INIT; bool hasUserId = false; + bool allowToPop = false; + //allowpop 传到第二个lambda里 }; struct AsyncCallbackInfoIsEnable { @@ -65,7 +67,7 @@ private: }; static std::mutex mutex; -static sptr callbackStubImpl; +static sptr callbackStubImpl_; napi_value EnableNotification(napi_env env, napi_callback_info info); napi_value IsNotificationEnabled(napi_env env, napi_callback_info info); diff --git a/frameworks/js/napi/src/enable_notification.cpp b/frameworks/js/napi/src/enable_notification.cpp index e5298c0f7..2f1b5ea67 100644 --- a/frameworks/js/napi/src/enable_notification.cpp +++ b/frameworks/js/napi/src/enable_notification.cpp @@ -325,18 +325,25 @@ napi_value RequestEnableNotification(napi_env env, napi_callback_info info) nullptr, resourceName, [](napi_env env, void *data) { - ANS_LOGI("RequestEnableNotification napi_create_async_work start"); AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); std::string deviceId {""}; - // TODO 后端需要修改下,判断是否允许弹窗 - asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId); - if (asynccallbackinfo->info.errorCode == ERR_OK) { + // 在结构体里加个flag + bool popFlag = false; + asynccallbackinfo->info.errorCode = NotificationHelper::RequestEnableNotification(deviceId, popFlag); + //从RequestEnableNotification(这里面有个ipc过程)带一个数据结构出来,把窗口的样式参数都带出来,带进StartNotificationDialog + ANS_LOGD("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + asynccallbackinfo->params.allowToPop = popFlag; + ANS_LOGD("asynccallbackinfo->params.allowToPop = %{public}d", asynccallbackinfo->params.allowToPop); + if (asynccallbackinfo->info.errorCode == ERR_OK && popFlag) { + ANS_LOGI("Begin to start notification dialog"); + //这里要把窗口样式带进去,传到js侧 StartNotificationDialog(data); + ANS_LOGI("Start notification dialog success"); } }, [](napi_env env, napi_status status, void *data) { AsyncCallbackInfoIsEnable *asynccallbackinfo = static_cast(data); - if (asynccallbackinfo->info.errorCode != ERR_OK) { + if (asynccallbackinfo->info.errorCode != ERR_OK && !asynccallbackinfo->params.allowToPop) { AsyncCompleteCallbackIsNotificationEnabled(env, status, data); } }, @@ -360,8 +367,12 @@ void StartNotificationDialog(void *data) auto result = AAFwk::AbilityManagerClient::GetInstance()->GetTopAbility(token); if (result == ERR_OK) { AAFwk::Want want; - want.SetElementName("", ""); - want.SetParam("", callbackStubImpl); + want.SetElementName("com.example.dialogsPopped", "DialogPopped"); + want.SetParam("callbackStubImpl_", callbackStubImpl_); + want.SetParam("tokenId", token); + // 要把窗口样式带进want里 + // todo + //want.SetParam("uid", uid);暂时不塞 ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, token, -1); ANS_LOGD("%{public}s, End Calling StartNotificationDialog. ret=%{public}d", __func__, err); } else { @@ -374,18 +385,18 @@ void StartNotificationDialog(void *data) bool CreateCallbackStubImpl(void *data) { std::lock_guard lock(mutex); - if (callbackStubImpl != nullptr) { + if (callbackStubImpl_ != nullptr) { return false; } auto *callbackInfo = static_cast(data); - callbackStubImpl = new CallbackStubImpl(callbackInfo); + callbackStubImpl_ = new CallbackStubImpl(callbackInfo); return true; } void ResetCallbackStubImpl() { std::lock_guard lock(mutex); - callbackStubImpl = nullptr; + callbackStubImpl_ = nullptr; } bool CallbackStubImpl::OnEnableNotification(bool isAllow) diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 2c7855390..3755ac23c 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -285,7 +285,7 @@ public: * only be null or an empty string, indicating the current device. * @return Returns set notifications enabled for default bundle result. */ - static ErrCode RequestEnableNotification(std::string &deviceId); + static ErrCode RequestEnableNotification(std::string &deviceId, bool &popFlag); /** * @brief Checks whether this application is in the suspended state.Applications in this state cannot publish diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index 0b82dffee..958a4331a 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -343,9 +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. * @return Returns ERR_OK on success, others on failure. */ - ErrCode RequestEnableNotification(const std::string &deviceId) override; + ErrCode RequestEnableNotification(const std::string &deviceId, bool &popFlag) override; /** * @brief Set whether to allow the specified deviceId to send notifications for current bundle. diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index 2dd700460..302045560 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -1434,96 +1434,42 @@ ErrCode AdvancedNotificationService::GetSpecialActiveNotifications( return result; } -ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId) +ErrCode AdvancedNotificationService::RequestEnableNotification(const std::string &deviceId, bool &popFlag)//传个flag的出参出去判断能不能弹窗,这个玩意的声明也得改 { ANS_LOGD("%{public}s", __FUNCTION__); ErrCode result = ERR_OK; sptr bundleOption = GenerateBundleOption(); if (bundleOption == nullptr) { + ANS_LOGD("bundleOption == nullptr"); return ERR_ANS_INVALID_BUNDLE; } + // To get the permission bool allowedNotify = false; result = IsAllowedNotifySelf(bundleOption, allowedNotify); + ANS_LOGI("result = %{public}d", result); + ANS_LOGI("allowedNotify = %{public}d", allowedNotify); if (result != ERR_OK || allowedNotify) { ANS_LOGD("Already granted permission"); + popFlag = false; return result; } - + + // Check to see if it has been popover before bool hasPopped = false; result = GetHasPoppedDialog(bundleOption, hasPopped); if (result != ERR_OK || hasPopped) { ANS_LOGD("Already shown dialog"); + popFlag = false; return result; } - - int32_t positionX; - int32_t positionY; - int32_t width; - int32_t height; - bool wideScreen; - GetDisplayPosition(positionX, positionY, width, height, wideScreen); - - const std::string params = std::string("{\"requestNotification\":\"Allowed to send notification?\", ") + - std::string("\"allowButton\":\"Allow\", \"cancelButton\":\"Cancel\", \"uid\":\"") + - std::to_string(bundleOption->GetUid()) + std::string("\"}"); - Ace::UIServiceMgrClient::GetInstance()->ShowDialog( - "notification_dialog", - params, - Rosen::WindowType::WINDOW_TYPE_SYSTEM_ALARM_WINDOW, - positionX, - positionY, - width, - height, - [this](int32_t id, const std::string& event, const std::string& params) { - ANS_LOGD("Dialog callback: %{public}s, %{public}s", event.c_str(), params.c_str()); - int32_t uid = std::stoi(params, nullptr); - std::string bundle; - std::shared_ptr bundleManager = BundleManagerHelper::GetInstance(); - if (bundleManager != nullptr) { - bundle = bundleManager->GetBundleNameByUid(uid); - } - sptr bundleOption = new NotificationBundleOption(bundle, uid); - if (event == "EVENT_ALLOW") { - this->SetNotificationsEnabledForSpecialBundle("", bundleOption, true); - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); - } else { - this->SetNotificationsEnabledForSpecialBundle("", bundleOption, false); - Ace::UIServiceMgrClient::GetInstance()->CancelDialog(id); - } - this->SetHasPoppedDialog(bundleOption, true); - }); - + ANS_LOGI("popFlag1 in = %{public}d", popFlag); + popFlag = true; + ANS_LOGI("popFlag2 in = %{public}d", popFlag); return result; } -//ErrCode AdvancedNotificationService::RequestEnableNotification(const bool &deviceId) -//{ -// ANS_LOGD("%{public}s", __FUNCTION__); -// -// ErrCode result = ERR_OK; -// sptr bundleOption = GenerateBundleOption(); -// if (bundleOption == nullptr) { -// return ERR_ANS_INVALID_BUNDLE; -// } -// -// bool allowedNotify = false; -// result = IsAllowedNotifySelf(bundleOption, allowedNotify); -// if (result != ERR_OK || allowedNotify) { -// ANS_LOGD("Already granted permission"); -// return result; -// } -// -// bool hasPopped = false; -// result = GetHasPoppedDialog(bundleOption, hasPopped); -// if (result != ERR_OK || hasPopped) { -// ANS_LOGD("Already shown dialog"); -// return result; -// } -// return result; -//} - ErrCode AdvancedNotificationService::SetNotificationsEnabledForBundle(const std::string &deviceId, bool enabled) { return ERR_INVALID_OPERATION; @@ -1581,6 +1527,7 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( new EnabledNotificationCallbackData(bundle->GetBundleName(), bundle->GetUid(), enabled); ErrCode result = ERR_OK; + std::string dialogBundlename; handler_->PostSyncTask(std::bind([&]() { if (deviceId.empty()) { // Local device @@ -1595,6 +1542,11 @@ ErrCode AdvancedNotificationService::SetNotificationsEnabledForSpecialBundle( } else { // Remote revice } + // TODO + dialogBundlename = "com.example.dialogsPopped"; + if (bundle->GetBundleName() == dialogBundlename) { + SetHasPoppedDialog(bundleOption, true); + } })); SendEnableNotificationHiSysEvent(bundleOption, enabled, result); -- Gitee