diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index 8f55eb686180d35d6c4e92c49160e2d77a194b2f..dabbd3044c63b3dfaaecaece1e66168629e5002d 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 0000000000000000000000000000000000000000..6670acfc8375f870aec68dc73bd5c34dc9c92f61 --- /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 0000000000000000000000000000000000000000..00961e940832b610a15b8c85e386c9b8e438ce53 --- /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 0000000000000000000000000000000000000000..52366c032beaba3a9fc1c5de93fbf2738e5b3604 --- /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 2c9a2b8320493448166b10d173c069830c532fc5..affb294c3a9ba130c7c24c29cbdd609df5e38c68 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 20f46801266bc19764fd3731f805dbbb05418eb9..bdf6a708757d15b986e8dbdb5eb6180a85f73a3c 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 de7e99a4e2b61ce7fd8d74f088f870d29e4a4e3f..e5298c0f72812f9dbfd4d92f3c699c09cf18f9cc 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 bd9a13570f0f0beaaee3f338dc2d46eb446b3c99..6baab83cb91091b6754bb495f3a18684ad7b4f50 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;