From cd22f142c76e6a9cc80583ea83e98107b3602b6c Mon Sep 17 00:00:00 2001 From: liujx9645 Date: Wed, 23 Apr 2025 20:24:50 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feature:notification=20ANi=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by:liujx9645 --- bundle.json | 4 +- frameworks/ani/BUILD.gn | 9 + frameworks/ani/notification_manager/BUILD.gn | 80 +++ .../ets/@ohos.notificationManager.ets | 70 +++ .../notification/NotificationCommonDef.ets | 24 + .../include/sts_common_util.h | 61 ++ .../include/sts_notification_manager.h | 24 + .../src/sts_common_util.cpp | 587 ++++++++++++++++++ .../src/sts_notification_manager.cpp | 108 ++++ 9 files changed, 966 insertions(+), 1 deletion(-) create mode 100644 frameworks/ani/BUILD.gn create mode 100644 frameworks/ani/notification_manager/BUILD.gn create mode 100644 frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets create mode 100644 frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets create mode 100644 frameworks/ani/notification_manager/include/sts_common_util.h create mode 100644 frameworks/ani/notification_manager/include/sts_notification_manager.h create mode 100644 frameworks/ani/notification_manager/src/sts_common_util.cpp create mode 100644 frameworks/ani/notification_manager/src/sts_notification_manager.cpp diff --git a/bundle.json b/bundle.json index de58f375c..c43af6061 100644 --- a/bundle.json +++ b/bundle.json @@ -84,6 +84,7 @@ "hisysevent", "hicollie", "access_token", + "runtime_core", "ipc", "player_framework", "image_framework", @@ -120,7 +121,8 @@ "//base/notification/distributed_notification_service/frameworks/js/napi:napi_notification", "//base/notification/distributed_notification_service/frameworks/js/napi:napi_reminder", "//base/notification/distributed_notification_service/interfaces/ndk:ohnotification", - "//base/notification/distributed_notification_service/frameworks/reminder:reminder_client" + "//base/notification/distributed_notification_service/frameworks/reminder:reminder_client", + "//base/notification/distributed_notification_service/frameworks/ani:ani_packages" ], "service_group": [ "//base/notification/distributed_notification_service/sa_profile:ans_sa_profile", diff --git a/frameworks/ani/BUILD.gn b/frameworks/ani/BUILD.gn new file mode 100644 index 000000000..6be65d076 --- /dev/null +++ b/frameworks/ani/BUILD.gn @@ -0,0 +1,9 @@ +import("//base/notification/distributed_notification_service/notification.gni") +import("//build/ohos.gni") + +group("ani_packages") { + deps = [ + "notification_manager:notification_manager_ani_kit", + "notification_manager:notification_manager_abc_etc" + ] +} \ No newline at end of file diff --git a/frameworks/ani/notification_manager/BUILD.gn b/frameworks/ani/notification_manager/BUILD.gn new file mode 100644 index 000000000..f6515f8cb --- /dev/null +++ b/frameworks/ani/notification_manager/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/config/components/ets_frontend/ets2abc_config.gni") +import("//build/ohos.gni") +import("//base/notification/distributed_notification_service/notification.gni") + +ohos_shared_library("notification_manager_ani_kit") { + branch_protector_ret = "pac_ret" + sanitize = { + cfi = true + cfi_cross_dso = true + cfi_vcall_icall_only = true + debug = false + } + + include_dirs = [ + "./include", + "${core_path}/common/include", + "${inner_api_path}", + ] + + sources = [ + "./src/sts_notification_manager.cpp", + "./src/sts_common_util.cpp" + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ + "//base/notification/distributed_notification_service/frameworks/ans:ans_innerkits", + ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "runtime_core:ani", + "ipc:ipc_core", + "ability_base:zuri", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +generate_static_abc("notification_manager_abc") { + base_url = "./ets" + files = [ + "./ets/notification/NotificationCommonDef.ets", + "./ets/@ohos.notificationManager.ets", + ] + + dst_file = "$target_out_dir/notification_manager.abc" + out_puts = [ "$target_out_dir/notification_manager.abc" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/notification_manager.abc" +} + +ohos_prebuilt_etc("notification_manager_abc_etc") { + source = "$target_out_dir/notification_manager.abc" + module_install_dir = "framework" + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" + deps = [ ":notification_manager_abc" ] +} diff --git a/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets new file mode 100644 index 000000000..c4f4e7f9f --- /dev/null +++ b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { BundleOption } from 'notification.NotificationCommonDef'; +import { BusinessError, AsyncCallback } from '@ohos.base'; + +type ResolveCallback = (data: T) => void; +type RejectCallback = (err: Object) => void; +export namespace notificationManager { + loadLibrary("notification_manager_ani_kit.z") + export native function nativeDisplayBadge(bundle: BundleOption, enable: boolean): int; + export native function nativeIsBadgeDisplayed(bundle: BundleOption): boolean; + + export function displayBadge(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void { + let p = taskpool.execute((): int => { return nativeDisplayBadge(bundle, enable); }); + p.then((e: NullishType): void => { + let ret : Int = e as Int; + let err: BusinessError = {code: ret.intValue(), data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + }) + } + + export function displayBadge(bundle: BundleOption, enable: boolean): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): int => { return nativeDisplayBadge(bundle, enable); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function isBadgeDisplayed(bundle: BundleOption, callback: AsyncCallback): void { + let p = taskpool.execute((): boolean => { return nativeIsBadgeDisplayed(bundle); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + let err: BusinessError = {code: 1, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + }) + } + + export function isBadgeDisplayed(bundle: BundleOption): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): boolean => { return nativeIsBadgeDisplayed(bundle); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } +} diff --git a/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets new file mode 100644 index 000000000..9a1b52928 --- /dev/null +++ b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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. + */ + +export interface BundleOption { + bundle: string; + uid?: number; +} + +class BundleOptionInner implements BundleOption { + bundle: string = ""; + uid?: number = -1; +} \ No newline at end of file diff --git a/frameworks/ani/notification_manager/include/sts_common_util.h b/frameworks/ani/notification_manager/include/sts_common_util.h new file mode 100644 index 000000000..ca0ef9ba0 --- /dev/null +++ b/frameworks/ani/notification_manager/include/sts_common_util.h @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_NOTIFICATION_SERVER_STS_COMMON_UTIL_H +#define OHOS_DISTRIBUTED_NOTIFICATION_SERVER_STS_COMMON_UTIL_H +#include "ani.h" +#include "notification_bundle_option.h" +#include +#include + +namespace OHOS { +namespace AniCommom { +#define SETTER_METHOD_NAME(property) "" #property + +ani_int GetIntOrUndefined(ani_env *env, ani_object param, const char *name); +double GetDoubleOrUndefined(ani_env *env, ani_object param, const char *name); +bool GetBoolOrUndefined(ani_env *env, ani_object param, const char *name); +bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res); +bool GetIntByName(ani_env *env, ani_object param, const char *name, int &value); +bool GetStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res); + +bool GetStdString(ani_env *env, ani_string str, std::string &res); + +ani_string GetAniString(ani_env *env, const std::string &str); +ani_array_ref GetAniArrayString(ani_env *env, const std::vector &values); +bool GetRefFieldByName(ani_env *env, ani_object param, const char *name, ani_ref &ref); + +bool ConverAniBundleOptionToNative(ani_env *env, ani_object param, Notification::NotificationBundleOption& option); + +bool SetFieldString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::string &value); +bool SetFieldDouble(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, double value); +bool SetFieldBoolean(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, bool value); +bool SetFieldInt(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, int value); +bool SetFieldArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::vector &values); +bool SetFieldRef(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, ani_ref value); + +void ClassSetter(ani_env* env, ani_class cls, ani_object object, const char* setterName, ...); + +inline bool AniBooleanToBool(ani_boolean value) +{ + return value == ANI_TRUE; +} + +inline ani_boolean BoolToAniBoolean(bool value) { + return value ? ANI_TRUE : ANI_FALSE; +} +} // namespace AniCommom +} // namespace OHOS +#endif diff --git a/frameworks/ani/notification_manager/include/sts_notification_manager.h b/frameworks/ani/notification_manager/include/sts_notification_manager.h new file mode 100644 index 000000000..4a09d7907 --- /dev/null +++ b/frameworks/ani/notification_manager/include/sts_notification_manager.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_NOTIFICATION_SERVER_NOTIFICATION_MANAGER_H +#define OHOS_DISTRIBUTED_NOTIFICATION_SERVER_NOTIFICATION_MANAGER_H +#include "ani.h" +namespace OHOS { +namespace NotificationManagerSts { +void StsNotificationManagerRegistryInit(ani_env *env); +} +} +#endif \ No newline at end of file diff --git a/frameworks/ani/notification_manager/src/sts_common_util.cpp b/frameworks/ani/notification_manager/src/sts_common_util.cpp new file mode 100644 index 000000000..907abdbe2 --- /dev/null +++ b/frameworks/ani/notification_manager/src/sts_common_util.cpp @@ -0,0 +1,587 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include "sts_common_util.h" +#include "ans_log_wrapper.h" +#include "securec.h" + +namespace OHOS { +namespace AniCommom { + +int GetIntOrUndefined(ani_env *env, ani_object param, const char *name) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + ani_int res = 0; + ani_status status = ANI_ERROR; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if (isUndefined){ + ANS_LOGD("%{public}s : undefined", name); + return res; + } + if ((status = env->Object_CallMethodByName_Int(reinterpret_cast(obj), "intValue", nullptr, &res)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + return res; +} + +bool GetIntByName(ani_env *env, ani_object param, const char *name, int &value) +{ + ani_int res; + ani_status status; + + status = env->Object_GetFieldByName_Int(param, name, &res); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + value = static_cast(res); + return true; +} + +double GetDoubleOrUndefined(ani_env *env, ani_object param, const char *name) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + ani_status status = ANI_ERROR; + ani_double res = 0.0; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if (isUndefined){ + ANS_LOGD("%{public}s : undefined", name); + return res; + } + if ((status = env->Object_CallMethodByName_Double(reinterpret_cast(obj), "doubleValue", nullptr, &res)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + return res; +} + +bool GetBoolOrUndefined(ani_env *env, ani_object param, const char *name) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + ani_status status = ANI_ERROR; + ani_boolean res = 0.0; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + if (isUndefined) { + ANS_LOGD("%{public}s : undefined", name); + return res; + } + if ((status = env->Object_CallMethodByName_Boolean(reinterpret_cast(obj), "booleanValue", nullptr, &res)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return res; + } + return res; +} + +bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + ani_status status = ANI_ERROR; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if (isUndefined) { + ANS_LOGD("%{public}s : undefined", name); + return false; + } + if (!GetStdString(env, reinterpret_cast(obj), res)) { + ANS_LOGD("GetStdString failed"); + return false; + } + return true; +} + +bool GetFixedStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res) +{ + ani_ref obj = nullptr; + ani_boolean isUndefined = true; + ani_status status; + ani_size size = 0; + ani_size i; + ani_ref ref; + std::string str; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if (isUndefined) { + ANS_LOGD("%{public}s : undefined", name); + return false; + } + + if ((status = env->Array_GetLength(reinterpret_cast(obj), &size)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + for (i = 0; i < size; i++) { + if ((status = env->Array_Get_Ref(reinterpret_cast(obj), i, &ref)) != ANI_OK) { + ANS_LOGD("status : %{public}d, index: %{public}zu", status, i); + return false; + } + + str = ""; + if (!GetStdString(env, reinterpret_cast(ref), str)) { + ANS_LOGD("GetStdString failed, index: %{public}zu", i); + return false; + } + + res.push_back(str); + } + + return true; +} + +bool SetFieldFixedArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, + const std::vector &values) +{ + ani_field field = nullptr; + ani_array_ref array = nullptr; + ani_class stringCls = nullptr; + ani_string string = nullptr; + ani_ref undefinedRef = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->FindClass("Lstd/core/String;", &stringCls); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->GetUndefined(&undefinedRef); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->Array_New_Ref(stringCls, values.size(), undefinedRef, &array); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + for (size_t i = 0; i < values.size(); ++i) { + string = nullptr; + status = env->String_NewUTF8(values[i].c_str(), values[i].size(), &string); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + status = env->Array_Set_Ref(array, i, string); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + } + status = env->Object_SetField_Ref(object, field, array); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + return true; +} + +bool GetStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res) +{ + ani_ref arrayObj = nullptr; + ani_boolean isUndefined = true; + ani_status status; + ani_double length; + std::string str; + + if ((status = env->Object_GetFieldByName_Ref(param, name, &arrayObj)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if ((status = env->Reference_IsUndefined(arrayObj, &isUndefined)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if (isUndefined) { + ANS_LOGD("%{public}s : undefined", name); + return false; + } + + status = env->Object_GetPropertyByName_Double(reinterpret_cast(arrayObj), "length", &length); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + for (int i = 0; i < static_cast(length); i++) { + ani_ref stringEntryRef; + status = env->Object_CallMethodByName_Ref(reinterpret_cast(arrayObj), + "$_get", "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d, index: %{public}d", status, i); + return false; + } + + str = ""; + if (!GetStdString(env, reinterpret_cast(stringEntryRef), str)) { + ANS_LOGD("GetStdString failed, index: %{public}d", i); + return false; + } + + res.push_back(str); + ANS_LOGD("GetStdString index: %{public}d %{public}s", i, str.c_str()); + } + + return true; +} + + +bool SetFieldArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, + const std::vector &values) +{ + ani_field field = nullptr; + ani_class arrayCls = nullptr; + ani_method arrayCtor; + ani_object arrayObj; + ani_string string = nullptr; + + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->FindClass("Lescompat/Array;", &arrayCls); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->Class_FindMethod(arrayCls, "", "I:V", &arrayCtor); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->Object_New(arrayCls, arrayCtor, &arrayObj, values.size()); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + for (size_t i = 0; i < values.size(); i++) { + string = nullptr; + status = env->String_NewUTF8(values[i].c_str(), values[i].size(), &string); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + status = env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", i, string); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + } + status = env->Object_SetField_Ref(object, field, arrayObj); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + return true; +} + +bool GetStdString(ani_env *env, ani_string str, std::string &res) +{ + ani_size sz {}; + ani_status status = ANI_ERROR; + if ((status = env->String_GetUTF8Size(str, &sz)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + res.resize(sz + 1); + if ((status = env->String_GetUTF8SubString(str, 0, sz, res.data(), res.size(), &sz)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + res.resize(sz); + return true; +} + +ani_string GetAniString(ani_env *env, const std::string &str) +{ + ani_string aniStr = nullptr; + ani_status status = env->String_NewUTF8(str.c_str(), str.size(), &aniStr); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return nullptr; + } + return aniStr; +} + +ani_array_ref GetAniArrayString(ani_env *env, const std::vector &values) +{ + // ani_size length = values.size(); + ani_array_ref aArrayRef = nullptr; + // ani_class aStringcls = nullptr; + // ani_status status = ANI_ERROR; + // if ((status = env->FindClass("Lstd/core/String;", &aStringcls)) != ANI_OK) { + // ANS_LOGD("status : %{public}d", status); + // return nullptr; + // } + // if ((status = env->Array_New_Ref(aStringcls, length, nullptr, &aArrayRef)) != ANI_OK) { + // ANS_LOGD("status : %{public}d", status); + // return nullptr; + // } + // ani_string aString = nullptr; + // for (ani_size i = 0; i < length; ++i) { + // env->String_NewUTF8(values[i].c_str(), values[i].size(), &aString); + // env->Array_Set_Ref(aArrayRef, i, aString); + // } + return aArrayRef; +} + +bool GetRefFieldByName(ani_env *env, ani_object param, const char *name, ani_ref &ref) +{ + ani_status status = ANI_ERROR; + if ((status = env->Object_GetFieldByName_Ref(param, name, &ref)) != ANI_OK) { + ANS_LOGD("Object_GetFieldByName_Ref failed, status : %{public}d", status); + return false; + } + + ani_boolean isUndefined = true; + if ((status = env->Reference_IsUndefined(ref, &isUndefined)) != ANI_OK) { + ANS_LOGD("Reference_IsUndefined failed, status : %{public}d", status); + return false; + } + if (isUndefined) { + ANS_LOGD("wantParams is undefined"); + return false; + } + return true; +} + +bool SetFieldString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::string &value) +{ + ani_field field = nullptr; + ani_string string = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + + ANS_LOGD("fieldName : %{public}s", fieldName.c_str()); + + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + if (value.empty()) { + ani_ref nullRef = nullptr; + if ((status = env->GetNull(&nullRef)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + if ((status = env->Object_SetField_Ref(object, field, nullRef)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + return true; + } + + if ((status = env->String_NewUTF8(value.c_str(), value.size(), &string)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + + if ((status = env->Object_SetField_Ref(object, field, string)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + return true; +} + +bool SetFieldDouble(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, double value) +{ + ani_field field = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + status = env->Object_SetField_Double(object, field, value); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + return true; +} + +bool SetFieldBoolean(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, bool value) +{ + ani_field field = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + status = env->Object_SetField_Boolean(object, field, value); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + return false; + } + return true; +} + +bool SetFieldInt(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, int value) +{ + ani_field field = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + ANS_LOGD("status : %{public}s", fieldName.c_str()); + return false; + } + status = env->Object_SetField_Int(object, field, value); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + ANS_LOGD("status : %{public}s", fieldName.c_str()); + return false; + } + return true; +} + +bool SetFieldRef(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, ani_ref value) +{ + ani_field field = nullptr; + ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); + if (status != ANI_OK) { + ANS_LOGD("FindField %{public}s failed, status: %{public}d", fieldName.c_str(), status); + return false; + } + status = env->Object_SetField_Ref(object, field, value); + if (status != ANI_OK) { + ANS_LOGD("SetField_Ref %{public}s failed, status: %{public}d", fieldName.c_str(), status); + return false; + } + return true; +} + +void ClassSetter( + ani_env* env, ani_class cls, ani_object object, const char* setterName, ...) +{ + ani_status status = ANI_ERROR; + ani_method setter; + if ((status = env->Class_FindMethod(cls, setterName, nullptr, &setter)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + } + va_list args; + va_start(args, setterName); + if ((status = env->Object_CallMethod_Void_V(object, setter, args)) != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + } + va_end(args); +} + +bool ConverAniBundleOptionToNative(ani_env *env, ani_object obj, Notification::NotificationBundleOption& option) +{ + ANS_LOGD("ConverAniBundleOptionToNative call"); + ani_ref bundleNameRef; + ani_status status = env->Object_GetPropertyByName_Ref(obj, "bundle", &bundleNameRef); + if (ANI_OK != status) { + ANS_LOGD("ConverAniBundleOptionToNative Object_GetField_Ref bundle fail, status: %{public}d", status); + return false; + } + + std::string bundleName; + if (!GetStdString(env, reinterpret_cast(bundleNameRef), bundleName)) { + ANS_LOGD("ConverAniBundleOptionToNative GetStdString failed"); + return false; + } + option.SetBundleName(bundleName); + ANS_LOGD("ConverAniBundleOptionToNative bundleName: %{public}s", bundleName.c_str()); + + ani_ref uidAni; + status = env->Object_GetPropertyByName_Ref(obj, "uid", &uidAni); + if (ANI_OK != status) { + ANS_LOGD("ConverAniBundleOptionToNative Object_GetPropertyByName_Ref uid fail, status: %{public}d", status); + return true; + } + ani_boolean isUndefined = true; + if ((status = env->Reference_IsUndefined(uidAni, &isUndefined)) != ANI_OK) { + ANS_LOGD("ConverAniBundleOptionToNative Object_GetPropertyByName_Ref status : %{public}d", status); + return true; + } + if (isUndefined) { + ANS_LOGD("ConverAniBundleOptionToNative uid is undefined"); + return true; + } + ani_double result = 0.0; + status = env->Object_CallMethodByName_Double(static_cast(uidAni), "doubleValue", nullptr, &result); + if (ANI_OK != status) { + ANS_LOGD("ConverAniBundleOptionToNative Object_CallMethodByName_Double uid fail, status: %{public}d", status); + return true; + } + + int32_t uid = static_cast(result); + + ANS_LOGD("ConverAniBundleOptionToNative bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), uid); + + option.SetUid(uid); + + return true; +} + + +} // namespace AppExecFwk +} // namespace OHOS diff --git a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp new file mode 100644 index 000000000..0432105e9 --- /dev/null +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "sts_notification_manager.h" +#include "ans_log_wrapper.h" +#include "sts_common_util.h" +#include "notification_bundle_option.h" +#include "notification_helper.h" + +using namespace OHOS::AniCommom; +namespace OHOS { +namespace NotificationManagerSts { + + +static int DisplayBadge(ani_env *env, ani_object obj, ani_boolean enable) +{ + ANS_LOGD("sts DisplayBadge call"); + Notification::NotificationBundleOption option; + ConverAniBundleOptionToNative(env, obj, option); + + int ret = Notification::NotificationHelper::SetShowBadgeEnabledForBundle(option, AniBooleanToBool(enable)); + ANS_LOGD("sts DisplayBadge end, ret: %{public}d", ret); + return ret; +} + +static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) +{ + ANS_LOGD("sts IsBadgeDisplayed call"); + Notification::NotificationBundleOption option; + bool ret = ConverAniBundleOptionToNative(env, obj, option); + bool isDisplayed = false; + int returncode = 0; + if (ret) + { + returncode = Notification::NotificationHelper::GetShowBadgeEnabledForBundle(option, isDisplayed); + } else { + returncode = Notification::NotificationHelper::GetShowBadgeEnabled(isDisplayed); + } + ANS_LOGD("sts IsBadgeDisplayed end, isDisplayed: %{public}d, returncode: %{public}d", isDisplayed, returncode); + return BoolToAniBoolean(isDisplayed); +} + +void StsNotificationManagerRegistryInit(ani_env *env) +{ + ANS_LOGD("StsNotificationManagerRegistryInit call"); + ani_status status = ANI_ERROR; + if (env->ResetError() != ANI_OK) { + ANS_LOGD("ResetError failed"); + } + + ani_namespace ns; + status = env->FindNamespace("L@ohos/notificationManager/notificationManager;", &ns); + if (status != ANI_OK) { + ANS_LOGD("FindNamespace notificationManager failed status : %{public}d", status); + return; + } + + std::array kitFunctions = { + ani_native_function {"nativeDisplayBadge", nullptr, reinterpret_cast(DisplayBadge)}, + ani_native_function {"nativeIsBadgeDisplayed", nullptr, reinterpret_cast(IsBadgeDisplayed)}, + }; + + status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); + if (status != ANI_OK) { + ANS_LOGD("Namespace_BindNativeFunctions failed status : %{public}d", status); + } + + if (env->ResetError() != ANI_OK) { + ANS_LOGD("ResetError failed"); + } + + ANS_LOGD("StsNotificationManagerRegistryInit end"); +} + +} +} + + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ANS_LOGD("ANI_Constructor"); + ani_env *env = nullptr; + ani_status status = ANI_ERROR; + status = vm->GetEnv(ANI_VERSION_1, &env); + if (status != ANI_OK) { + ANS_LOGD("GetEnv failed status : %{public}d", status); + return ANI_NOT_FOUND; + } + + OHOS::NotificationManagerSts::StsNotificationManagerRegistryInit(env); + *result = ANI_VERSION_1; + ANS_LOGD("ANI_Constructor finish"); + return ANI_OK; +} +} \ No newline at end of file -- Gitee From b4da4c458b064aee2915ac2d58be704a234ad5df Mon Sep 17 00:00:00 2001 From: liujx9645 Date: Thu, 24 Apr 2025 14:59:46 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:notification=20ani=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by:liujx9645 --- frameworks/ani/notification_manager/BUILD.gn | 6 +- .../notification_manager/ets/@ohos.base.ets | 49 ++ .../notification/NotificationCommonDef.ets | 8 +- .../include/sts_bundle_option.h | 28 + .../include/sts_common_util.h | 22 - .../src/sts_bundle_option.cpp | 73 +++ .../src/sts_common_util.cpp | 546 ------------------ .../src/sts_notification_manager.cpp | 10 +- 8 files changed, 163 insertions(+), 579 deletions(-) create mode 100644 frameworks/ani/notification_manager/ets/@ohos.base.ets create mode 100644 frameworks/ani/notification_manager/include/sts_bundle_option.h create mode 100644 frameworks/ani/notification_manager/src/sts_bundle_option.cpp diff --git a/frameworks/ani/notification_manager/BUILD.gn b/frameworks/ani/notification_manager/BUILD.gn index f6515f8cb..413812c22 100644 --- a/frameworks/ani/notification_manager/BUILD.gn +++ b/frameworks/ani/notification_manager/BUILD.gn @@ -32,7 +32,8 @@ ohos_shared_library("notification_manager_ani_kit") { sources = [ "./src/sts_notification_manager.cpp", - "./src/sts_common_util.cpp" + "./src/sts_common_util.cpp", + "./src/sts_bundle_option.cpp", ] cflags = [] @@ -41,7 +42,7 @@ ohos_shared_library("notification_manager_ani_kit") { } deps = [ - "//base/notification/distributed_notification_service/frameworks/ans:ans_innerkits", + "${frameworks_module_ans_path}:ans_innerkits", ] external_deps = [ @@ -62,6 +63,7 @@ generate_static_abc("notification_manager_abc") { files = [ "./ets/notification/NotificationCommonDef.ets", "./ets/@ohos.notificationManager.ets", + "./ets/@ohos.base.ets", ] dst_file = "$target_out_dir/notification_manager.abc" diff --git a/frameworks/ani/notification_manager/ets/@ohos.base.ets b/frameworks/ani/notification_manager/ets/@ohos.base.ets new file mode 100644 index 000000000..993f733f7 --- /dev/null +++ b/frameworks/ani/notification_manager/ets/@ohos.base.ets @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export type Callback = (data: T) => void; + +export type ErrorCallback = (err: T)=> void; + + +export type AsyncCallback = (err: BusinessError, data: T)=> void; + +export class BusinessError extends Error { + + // 构造函数重载1:无参数 + constructor() { + super(); // 调用父类 Error 的构造函数 + this.code = 0; // 默认错误代码 + this.data = undefined; // 默认数据为 undefined + } + + // 构造函数重载2:带有错误代码和 Error 对象 + constructor(code: number, error: Error) { + // constructor(name: String, message: String | undefined, options: ErrorOptions | undefined) + super(error.name, error.message, undefined); // 调用父类 Error 的构造函数,并传递错误信息 + this.code = code; + this.data = undefined; // 默认数据为 undefined + } + + constructor(code: number, data: T, error: Error) { + super(error.name, error.message, undefined); // 调用父类 Error 的构造函数,并传递错误信息 + this.code = code; + this.data = data; // 默认数据为 undefined + } + + code: number; + + data?: T; +} \ No newline at end of file diff --git a/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets index 9a1b52928..9f36eef0c 100644 --- a/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets +++ b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets @@ -1,10 +1,10 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"), + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http?://www.apache.org/licenses/LICENSE-2.0 + * 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, @@ -20,5 +20,5 @@ export interface BundleOption { class BundleOptionInner implements BundleOption { bundle: string = ""; - uid?: number = -1; + uid?: number = 0; } \ No newline at end of file diff --git a/frameworks/ani/notification_manager/include/sts_bundle_option.h b/frameworks/ani/notification_manager/include/sts_bundle_option.h new file mode 100644 index 000000000..72cc23e34 --- /dev/null +++ b/frameworks/ani/notification_manager/include/sts_bundle_option.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_NOTIFICATION_SERVER_STS_BUNDLE_OPTION_H +#define OHOS_DISTRIBUTED_NOTIFICATION_SERVER_STS_BUNDLE_OPTION_H + +#include "ani.h" +#include "notification_bundle_option.h" + +namespace OHOS { +namespace NotificationSts { + bool UnwrapBundleOption(ani_env *env, ani_object param, Notification::NotificationBundleOption& option); +} +} + +#endif \ No newline at end of file diff --git a/frameworks/ani/notification_manager/include/sts_common_util.h b/frameworks/ani/notification_manager/include/sts_common_util.h index ca0ef9ba0..be428cdfc 100644 --- a/frameworks/ani/notification_manager/include/sts_common_util.h +++ b/frameworks/ani/notification_manager/include/sts_common_util.h @@ -24,30 +24,8 @@ namespace OHOS { namespace AniCommom { #define SETTER_METHOD_NAME(property) "" #property -ani_int GetIntOrUndefined(ani_env *env, ani_object param, const char *name); -double GetDoubleOrUndefined(ani_env *env, ani_object param, const char *name); -bool GetBoolOrUndefined(ani_env *env, ani_object param, const char *name); -bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res); -bool GetIntByName(ani_env *env, ani_object param, const char *name, int &value); -bool GetStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res); - bool GetStdString(ani_env *env, ani_string str, std::string &res); -ani_string GetAniString(ani_env *env, const std::string &str); -ani_array_ref GetAniArrayString(ani_env *env, const std::vector &values); -bool GetRefFieldByName(ani_env *env, ani_object param, const char *name, ani_ref &ref); - -bool ConverAniBundleOptionToNative(ani_env *env, ani_object param, Notification::NotificationBundleOption& option); - -bool SetFieldString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::string &value); -bool SetFieldDouble(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, double value); -bool SetFieldBoolean(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, bool value); -bool SetFieldInt(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, int value); -bool SetFieldArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::vector &values); -bool SetFieldRef(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, ani_ref value); - -void ClassSetter(ani_env* env, ani_class cls, ani_object object, const char* setterName, ...); - inline bool AniBooleanToBool(ani_boolean value) { return value == ANI_TRUE; diff --git a/frameworks/ani/notification_manager/src/sts_bundle_option.cpp b/frameworks/ani/notification_manager/src/sts_bundle_option.cpp new file mode 100644 index 000000000..503bba6f6 --- /dev/null +++ b/frameworks/ani/notification_manager/src/sts_bundle_option.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "sts_common_util.h" +#include "ans_log_wrapper.h" + +using namespace OHOS::AniCommom; +namespace OHOS { +namespace NotificationSts { + +bool UnwrapBundleOption(ani_env *env, ani_object obj, Notification::NotificationBundleOption& option) +{ + ANS_LOGD("WrapBundleOption call"); + ani_ref bundleNameRef; + ani_status status = env->Object_GetPropertyByName_Ref(obj, "bundle", &bundleNameRef); + if (ANI_OK != status) { + ANS_LOGD("WrapBundleOption Object_GetField_Ref bundle fail, status: %{public}d", status); + return false; + } + + std::string bundleName; + if (!GetStdString(env, reinterpret_cast(bundleNameRef), bundleName)) { + ANS_LOGD("WrapBundleOption GetStdString failed"); + return false; + } + option.SetBundleName(bundleName); + ANS_LOGD("WrapBundleOption bundleName: %{public}s", bundleName.c_str()); + + ani_ref uidRef; + status = env->Object_GetPropertyByName_Ref(obj, "uid", &uidRef); + if (ANI_OK != status) { + ANS_LOGD("WrapBundleOption Object_GetPropertyByName_Ref uid fail, status: %{public}d", status); + return true; + } + ani_boolean isUndefined = true; + if ((status = env->Reference_IsUndefined(uidRef, &isUndefined)) != ANI_OK) { + ANS_LOGD("WrapBundleOption Object_GetPropertyByName_Ref status : %{public}d", status); + return true; + } + if (isUndefined) { + ANS_LOGD("WrapBundleOption uid is undefined"); + return true; + } + ani_double result = 0.0; + status = env->Object_CallMethodByName_Double(static_cast(uidRef), "doubleValue", nullptr, &result); + if (ANI_OK != status) { + ANS_LOGD("WrapBundleOption Object_CallMethodByName_Double uid fail, status: %{public}d", status); + return true; + } + + int32_t uid = static_cast(result); + + ANS_LOGD("WrapBundleOption bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), uid); + + option.SetUid(uid); + + return true; +} +} +} \ No newline at end of file diff --git a/frameworks/ani/notification_manager/src/sts_common_util.cpp b/frameworks/ani/notification_manager/src/sts_common_util.cpp index 907abdbe2..117772a70 100644 --- a/frameworks/ani/notification_manager/src/sts_common_util.cpp +++ b/frameworks/ani/notification_manager/src/sts_common_util.cpp @@ -20,329 +20,6 @@ namespace OHOS { namespace AniCommom { -int GetIntOrUndefined(ani_env *env, ani_object param, const char *name) -{ - ani_ref obj = nullptr; - ani_boolean isUndefined = true; - ani_int res = 0; - ani_status status = ANI_ERROR; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if (isUndefined){ - ANS_LOGD("%{public}s : undefined", name); - return res; - } - if ((status = env->Object_CallMethodByName_Int(reinterpret_cast(obj), "intValue", nullptr, &res)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - return res; -} - -bool GetIntByName(ani_env *env, ani_object param, const char *name, int &value) -{ - ani_int res; - ani_status status; - - status = env->Object_GetFieldByName_Int(param, name, &res); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - value = static_cast(res); - return true; -} - -double GetDoubleOrUndefined(ani_env *env, ani_object param, const char *name) -{ - ani_ref obj = nullptr; - ani_boolean isUndefined = true; - ani_status status = ANI_ERROR; - ani_double res = 0.0; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if (isUndefined){ - ANS_LOGD("%{public}s : undefined", name); - return res; - } - if ((status = env->Object_CallMethodByName_Double(reinterpret_cast(obj), "doubleValue", nullptr, &res)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - return res; -} - -bool GetBoolOrUndefined(ani_env *env, ani_object param, const char *name) -{ - ani_ref obj = nullptr; - ani_boolean isUndefined = true; - ani_status status = ANI_ERROR; - ani_boolean res = 0.0; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - if (isUndefined) { - ANS_LOGD("%{public}s : undefined", name); - return res; - } - if ((status = env->Object_CallMethodByName_Boolean(reinterpret_cast(obj), "booleanValue", nullptr, &res)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return res; - } - return res; -} - -bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res) -{ - ani_ref obj = nullptr; - ani_boolean isUndefined = true; - ani_status status = ANI_ERROR; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if (isUndefined) { - ANS_LOGD("%{public}s : undefined", name); - return false; - } - if (!GetStdString(env, reinterpret_cast(obj), res)) { - ANS_LOGD("GetStdString failed"); - return false; - } - return true; -} - -bool GetFixedStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res) -{ - ani_ref obj = nullptr; - ani_boolean isUndefined = true; - ani_status status; - ani_size size = 0; - ani_size i; - ani_ref ref; - std::string str; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &obj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if ((status = env->Reference_IsUndefined(obj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if (isUndefined) { - ANS_LOGD("%{public}s : undefined", name); - return false; - } - - if ((status = env->Array_GetLength(reinterpret_cast(obj), &size)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - for (i = 0; i < size; i++) { - if ((status = env->Array_Get_Ref(reinterpret_cast(obj), i, &ref)) != ANI_OK) { - ANS_LOGD("status : %{public}d, index: %{public}zu", status, i); - return false; - } - - str = ""; - if (!GetStdString(env, reinterpret_cast(ref), str)) { - ANS_LOGD("GetStdString failed, index: %{public}zu", i); - return false; - } - - res.push_back(str); - } - - return true; -} - -bool SetFieldFixedArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, - const std::vector &values) -{ - ani_field field = nullptr; - ani_array_ref array = nullptr; - ani_class stringCls = nullptr; - ani_string string = nullptr; - ani_ref undefinedRef = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->FindClass("Lstd/core/String;", &stringCls); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->GetUndefined(&undefinedRef); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->Array_New_Ref(stringCls, values.size(), undefinedRef, &array); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - for (size_t i = 0; i < values.size(); ++i) { - string = nullptr; - status = env->String_NewUTF8(values[i].c_str(), values[i].size(), &string); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - status = env->Array_Set_Ref(array, i, string); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - } - status = env->Object_SetField_Ref(object, field, array); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - return true; -} - -bool GetStringArrayOrUndefined(ani_env *env, ani_object param, const char *name, std::vector &res) -{ - ani_ref arrayObj = nullptr; - ani_boolean isUndefined = true; - ani_status status; - ani_double length; - std::string str; - - if ((status = env->Object_GetFieldByName_Ref(param, name, &arrayObj)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if ((status = env->Reference_IsUndefined(arrayObj, &isUndefined)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if (isUndefined) { - ANS_LOGD("%{public}s : undefined", name); - return false; - } - - status = env->Object_GetPropertyByName_Double(reinterpret_cast(arrayObj), "length", &length); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - for (int i = 0; i < static_cast(length); i++) { - ani_ref stringEntryRef; - status = env->Object_CallMethodByName_Ref(reinterpret_cast(arrayObj), - "$_get", "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d, index: %{public}d", status, i); - return false; - } - - str = ""; - if (!GetStdString(env, reinterpret_cast(stringEntryRef), str)) { - ANS_LOGD("GetStdString failed, index: %{public}d", i); - return false; - } - - res.push_back(str); - ANS_LOGD("GetStdString index: %{public}d %{public}s", i, str.c_str()); - } - - return true; -} - - -bool SetFieldArrayString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, - const std::vector &values) -{ - ani_field field = nullptr; - ani_class arrayCls = nullptr; - ani_method arrayCtor; - ani_object arrayObj; - ani_string string = nullptr; - - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->FindClass("Lescompat/Array;", &arrayCls); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->Class_FindMethod(arrayCls, "", "I:V", &arrayCtor); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->Object_New(arrayCls, arrayCtor, &arrayObj, values.size()); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - for (size_t i = 0; i < values.size(); i++) { - string = nullptr; - status = env->String_NewUTF8(values[i].c_str(), values[i].size(), &string); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - status = env->Object_CallMethodByName_Void(arrayObj, "$_set", "ILstd/core/Object;:V", i, string); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - } - status = env->Object_SetField_Ref(object, field, arrayObj); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - return true; -} - bool GetStdString(ani_env *env, ani_string str, std::string &res) { ani_size sz {}; @@ -360,228 +37,5 @@ bool GetStdString(ani_env *env, ani_string str, std::string &res) return true; } -ani_string GetAniString(ani_env *env, const std::string &str) -{ - ani_string aniStr = nullptr; - ani_status status = env->String_NewUTF8(str.c_str(), str.size(), &aniStr); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return nullptr; - } - return aniStr; -} - -ani_array_ref GetAniArrayString(ani_env *env, const std::vector &values) -{ - // ani_size length = values.size(); - ani_array_ref aArrayRef = nullptr; - // ani_class aStringcls = nullptr; - // ani_status status = ANI_ERROR; - // if ((status = env->FindClass("Lstd/core/String;", &aStringcls)) != ANI_OK) { - // ANS_LOGD("status : %{public}d", status); - // return nullptr; - // } - // if ((status = env->Array_New_Ref(aStringcls, length, nullptr, &aArrayRef)) != ANI_OK) { - // ANS_LOGD("status : %{public}d", status); - // return nullptr; - // } - // ani_string aString = nullptr; - // for (ani_size i = 0; i < length; ++i) { - // env->String_NewUTF8(values[i].c_str(), values[i].size(), &aString); - // env->Array_Set_Ref(aArrayRef, i, aString); - // } - return aArrayRef; -} - -bool GetRefFieldByName(ani_env *env, ani_object param, const char *name, ani_ref &ref) -{ - ani_status status = ANI_ERROR; - if ((status = env->Object_GetFieldByName_Ref(param, name, &ref)) != ANI_OK) { - ANS_LOGD("Object_GetFieldByName_Ref failed, status : %{public}d", status); - return false; - } - - ani_boolean isUndefined = true; - if ((status = env->Reference_IsUndefined(ref, &isUndefined)) != ANI_OK) { - ANS_LOGD("Reference_IsUndefined failed, status : %{public}d", status); - return false; - } - if (isUndefined) { - ANS_LOGD("wantParams is undefined"); - return false; - } - return true; -} - -bool SetFieldString(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, const std::string &value) -{ - ani_field field = nullptr; - ani_string string = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - - ANS_LOGD("fieldName : %{public}s", fieldName.c_str()); - - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - if (value.empty()) { - ani_ref nullRef = nullptr; - if ((status = env->GetNull(&nullRef)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - if ((status = env->Object_SetField_Ref(object, field, nullRef)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - return true; - } - - if ((status = env->String_NewUTF8(value.c_str(), value.size(), &string)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - - if ((status = env->Object_SetField_Ref(object, field, string)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - return true; -} - -bool SetFieldDouble(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, double value) -{ - ani_field field = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - status = env->Object_SetField_Double(object, field, value); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - return true; -} - -bool SetFieldBoolean(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, bool value) -{ - ani_field field = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - status = env->Object_SetField_Boolean(object, field, value); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - return false; - } - return true; -} - -bool SetFieldInt(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, int value) -{ - ani_field field = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - ANS_LOGD("status : %{public}s", fieldName.c_str()); - return false; - } - status = env->Object_SetField_Int(object, field, value); - if (status != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - ANS_LOGD("status : %{public}s", fieldName.c_str()); - return false; - } - return true; -} - -bool SetFieldRef(ani_env *env, ani_class cls, ani_object object, const std::string &fieldName, ani_ref value) -{ - ani_field field = nullptr; - ani_status status = env->Class_FindField(cls, fieldName.c_str(), &field); - if (status != ANI_OK) { - ANS_LOGD("FindField %{public}s failed, status: %{public}d", fieldName.c_str(), status); - return false; - } - status = env->Object_SetField_Ref(object, field, value); - if (status != ANI_OK) { - ANS_LOGD("SetField_Ref %{public}s failed, status: %{public}d", fieldName.c_str(), status); - return false; - } - return true; -} - -void ClassSetter( - ani_env* env, ani_class cls, ani_object object, const char* setterName, ...) -{ - ani_status status = ANI_ERROR; - ani_method setter; - if ((status = env->Class_FindMethod(cls, setterName, nullptr, &setter)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - } - va_list args; - va_start(args, setterName); - if ((status = env->Object_CallMethod_Void_V(object, setter, args)) != ANI_OK) { - ANS_LOGD("status : %{public}d", status); - } - va_end(args); -} - -bool ConverAniBundleOptionToNative(ani_env *env, ani_object obj, Notification::NotificationBundleOption& option) -{ - ANS_LOGD("ConverAniBundleOptionToNative call"); - ani_ref bundleNameRef; - ani_status status = env->Object_GetPropertyByName_Ref(obj, "bundle", &bundleNameRef); - if (ANI_OK != status) { - ANS_LOGD("ConverAniBundleOptionToNative Object_GetField_Ref bundle fail, status: %{public}d", status); - return false; - } - - std::string bundleName; - if (!GetStdString(env, reinterpret_cast(bundleNameRef), bundleName)) { - ANS_LOGD("ConverAniBundleOptionToNative GetStdString failed"); - return false; - } - option.SetBundleName(bundleName); - ANS_LOGD("ConverAniBundleOptionToNative bundleName: %{public}s", bundleName.c_str()); - - ani_ref uidAni; - status = env->Object_GetPropertyByName_Ref(obj, "uid", &uidAni); - if (ANI_OK != status) { - ANS_LOGD("ConverAniBundleOptionToNative Object_GetPropertyByName_Ref uid fail, status: %{public}d", status); - return true; - } - ani_boolean isUndefined = true; - if ((status = env->Reference_IsUndefined(uidAni, &isUndefined)) != ANI_OK) { - ANS_LOGD("ConverAniBundleOptionToNative Object_GetPropertyByName_Ref status : %{public}d", status); - return true; - } - if (isUndefined) { - ANS_LOGD("ConverAniBundleOptionToNative uid is undefined"); - return true; - } - ani_double result = 0.0; - status = env->Object_CallMethodByName_Double(static_cast(uidAni), "doubleValue", nullptr, &result); - if (ANI_OK != status) { - ANS_LOGD("ConverAniBundleOptionToNative Object_CallMethodByName_Double uid fail, status: %{public}d", status); - return true; - } - - int32_t uid = static_cast(result); - - ANS_LOGD("ConverAniBundleOptionToNative bundleName: %{public}s, uid: %{public}d", bundleName.c_str(), uid); - - option.SetUid(uid); - - return true; -} - - } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp index 0432105e9..922bc96c6 100644 --- a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -13,23 +13,23 @@ * limitations under the License. */ -#include "sts_notification_manager.h" #include "ans_log_wrapper.h" +#include "sts_notification_manager.h" #include "sts_common_util.h" +#include "sts_bundle_option.h" #include "notification_bundle_option.h" #include "notification_helper.h" using namespace OHOS::AniCommom; +using namespace OHOS::NotificationSts; namespace OHOS { namespace NotificationManagerSts { - static int DisplayBadge(ani_env *env, ani_object obj, ani_boolean enable) { ANS_LOGD("sts DisplayBadge call"); Notification::NotificationBundleOption option; - ConverAniBundleOptionToNative(env, obj, option); - + UnwrapBundleOption(env, obj, option); int ret = Notification::NotificationHelper::SetShowBadgeEnabledForBundle(option, AniBooleanToBool(enable)); ANS_LOGD("sts DisplayBadge end, ret: %{public}d", ret); return ret; @@ -39,7 +39,7 @@ static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) { ANS_LOGD("sts IsBadgeDisplayed call"); Notification::NotificationBundleOption option; - bool ret = ConverAniBundleOptionToNative(env, obj, option); + bool ret = UnwrapBundleOption(env, obj, option); bool isDisplayed = false; int returncode = 0; if (ret) -- Gitee From ae98a3c7e1367977bf28656bab025620390cd6f4 Mon Sep 17 00:00:00 2001 From: liujx9645 Date: Thu, 24 Apr 2025 20:25:46 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:notification=20ani=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by:liujx9645 --- .../ets/@ohos.notificationManager.ets | 99 ++++++++++++++++++- .../src/sts_notification_manager.cpp | 62 +++++++++++- 2 files changed, 154 insertions(+), 7 deletions(-) diff --git a/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets index c4f4e7f9f..6ba16c5f9 100644 --- a/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets +++ b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets @@ -20,14 +20,15 @@ type ResolveCallback = (data: T) => void; type RejectCallback = (err: Object) => void; export namespace notificationManager { loadLibrary("notification_manager_ani_kit.z") - export native function nativeDisplayBadge(bundle: BundleOption, enable: boolean): int; + export native function nativeDisplayBadge(bundle: BundleOption, enable: boolean): void; export native function nativeIsBadgeDisplayed(bundle: BundleOption): boolean; + export native function nativeGetActiveNotificationCount(): number; + export native function nativeIsNotificationEnabled(userId?: int, bundleOption?: BundleOption): boolean; export function displayBadge(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void { - let p = taskpool.execute((): int => { return nativeDisplayBadge(bundle, enable); }); + let p = taskpool.execute((): void => { return nativeDisplayBadge(bundle, enable); }); p.then((e: NullishType): void => { - let ret : Int = e as Int; - let err: BusinessError = {code: ret.intValue(), data: undefined}; + let err: BusinessError = {code: 1, data: undefined}; callback(err, undefined); }, (error: Object): void => { }) @@ -35,7 +36,7 @@ export namespace notificationManager { export function displayBadge(bundle: BundleOption, enable: boolean): Promise { let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { - let p = taskpool.execute((): int => { return nativeDisplayBadge(bundle, enable); }); + let p = taskpool.execute((): void => { return nativeDisplayBadge(bundle, enable); }); p.then((data: NullishType): void => { resolve(undefined); }, (error: Object): void => { @@ -67,4 +68,92 @@ export namespace notificationManager { }); return pPromise; } + + export function getActiveNotificationCount(): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback):void => { + let p = taskpool.execute((): number => { return nativeGetActiveNotificationCount(); }); + p.then((data :NullishType): void => { + let ret : number = data as number; + resolve(ret); + }, (err:Object): void => { + reject(err); + }); + }); + return pPromise; + } + + export function isNotificationEnabled(userId: int, callback: AsyncCallback): void + { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(userId, undefined); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + let err: BusinessError = {code: 1, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + }) + } + + export function isNotificationEnabled(userId: int): Promise + { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(userId, undefined); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function isNotificationEnabled(bundleOption: BundleOption, callback: AsyncCallback): void + { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(undefined, bundleOption); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + let err: BusinessError = {code: 1, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + }) + } + + export function isNotificationEnabled(bundleOption: BundleOption): Promise + { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(undefined, bundleOption); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function isNotificationEnabled(callback: AsyncCallback): void + { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(undefined, undefined); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + let err: BusinessError = {code: 1, data: undefined}; + callback(err, ret); + }, (error: Object): void => { + }) + } + + export function isNotificationEnabled(): Promise + { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): boolean => { return nativeIsNotificationEnabled(undefined, undefined); }); + p.then((data: NullishType): void => { + let ret : boolean = data as boolean; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } } diff --git a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp index 922bc96c6..b4753b7b8 100644 --- a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -25,14 +25,13 @@ using namespace OHOS::NotificationSts; namespace OHOS { namespace NotificationManagerSts { -static int DisplayBadge(ani_env *env, ani_object obj, ani_boolean enable) +static void DisplayBadge(ani_env *env, ani_object obj, ani_boolean enable) { ANS_LOGD("sts DisplayBadge call"); Notification::NotificationBundleOption option; UnwrapBundleOption(env, obj, option); int ret = Notification::NotificationHelper::SetShowBadgeEnabledForBundle(option, AniBooleanToBool(enable)); ANS_LOGD("sts DisplayBadge end, ret: %{public}d", ret); - return ret; } static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) @@ -52,6 +51,63 @@ static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) return BoolToAniBoolean(isDisplayed); } +static ani_double GetActiveNotificationCount(ani_env *env) +{ + ANS_LOGD("sts GetActiveNotificationCount call"); + uint64_t num = 0; + OHOS::Notification::NotificationHelper::GetActiveNotificationNums(num); + ANS_LOGD("sts GetActiveNotificationCount end, num: %{public}llu", num); + return static_cast(num);; +} + +static ani_boolean isNotificationEnabled([[maybe_unused]]ani_env *env, ani_object jsUserId, ani_object jsBundleOption) +{ + ani_int userId = 0; + bool allowed = false; + int32_t ret = 0; + ani_boolean isUserIdUndefined = ANI_FALSE; + ani_boolean isBundleUndefined = ANI_FALSE; + + ANS_LOGD("sts DisplayBadge call"); + + env->Reference_IsUndefined(jsUserId, &isUserIdUndefined); + env->Reference_IsUndefined(jsBundleOption, &isBundleUndefined); + + ANS_LOGD("isNotificationEnabled isUserIdUndefined = %{public}d isBundleUndefined = %{public}d", isUserIdUndefined, isBundleUndefined); + + // 3. 解析 userId + if (!isUserIdUndefined) { + ANS_LOGD("isNotificationEnabled ==>> isUserIdUndefined"); + if (ANI_OK !=env->Object_CallMethodByName_Int(jsUserId, "intValue", nullptr, &userId)) { + ANS_LOGD("isNotificationEnabled Object_CallMethodByName_Int Fail"); + userId = 0; + return ANI_FALSE; + } + + ret = Notification::NotificationHelper::IsAllowedNotify(userId, allowed); + + ANS_LOGD("isNotificationEnabled IsAllowedNotify ret = %{public}d allowed = %{public}d", ret, allowed); + } else if (!isBundleUndefined) { + ANS_LOGD("isNotificationEnabled ==>> isBundleUndefined"); + + Notification::NotificationBundleOption option; + UnwrapBundleOption(env, jsBundleOption, option); + + ret = Notification::NotificationHelper::IsAllowedNotify(option, allowed); + + }else { + ANS_LOGD("isNotificationEnabled ==>> no parameter"); + ret = Notification::NotificationHelper::IsAllowedNotify(allowed); + ANS_LOGD("isNotificationEnabled IsAllowedNotify ret = %{public}d allowed = %{public}d", ret, allowed); + } + + if (ret != ANI_OK) { + return ANI_FALSE; + } else { + return allowed ? ANI_TRUE : ANI_FALSE; + } +} + void StsNotificationManagerRegistryInit(ani_env *env) { ANS_LOGD("StsNotificationManagerRegistryInit call"); @@ -70,6 +126,8 @@ void StsNotificationManagerRegistryInit(ani_env *env) std::array kitFunctions = { ani_native_function {"nativeDisplayBadge", nullptr, reinterpret_cast(DisplayBadge)}, ani_native_function {"nativeIsBadgeDisplayed", nullptr, reinterpret_cast(IsBadgeDisplayed)}, + ani_native_function {"nativeGetActiveNotificationCount", ":D", reinterpret_cast(GetActiveNotificationCount)}, + ani_native_function {"nativeIsNotificationEnabled", nullptr, reinterpret_cast(isNotificationEnabled)}, }; status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); -- Gitee From e0d673a86a4a350afae049872121f1d5cb651532 Mon Sep 17 00:00:00 2001 From: liujx9645 Date: Thu, 24 Apr 2025 20:52:56 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:notification=20ani=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by:liujx9645 --- .../ets/@ohos.notificationManager.ets | 60 ++++++++++++++++++- .../src/sts_notification_manager.cpp | 32 ++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets index 6ba16c5f9..b0fe6d867 100644 --- a/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets +++ b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets @@ -19,11 +19,23 @@ import { BusinessError, AsyncCallback } from '@ohos.base'; type ResolveCallback = (data: T) => void; type RejectCallback = (err: Object) => void; export namespace notificationManager { - loadLibrary("notification_manager_ani_kit.z") + export enum SlotType { + UNKNOWN_TYPE = 0, + SOCIAL_COMMUNICATION = 1, + SERVICE_INFORMATION = 2, + CONTENT_INFORMATION = 3, + LIVE_VIEW = 4, + CUSTOMER_SERVICE = 5, + EMERGENCY_INFORMATION = 10, + OTHER_TYPES = 0xFFFF + } export native function nativeDisplayBadge(bundle: BundleOption, enable: boolean): void; export native function nativeIsBadgeDisplayed(bundle: BundleOption): boolean; export native function nativeGetActiveNotificationCount(): number; export native function nativeIsNotificationEnabled(userId?: int, bundleOption?: BundleOption): boolean; + export native function nativegetSlotFlagsByBundle(bundle: BundleOption): int; + export native function nativesetSlotFlagsByBundle(bundle: BundleOption, slotFlags: number): void; + export native function nativeSetNotificationEnable(bundle: BundleOption, enable: boolean): void; export function displayBadge(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void { let p = taskpool.execute((): void => { return nativeDisplayBadge(bundle, enable); }); @@ -156,4 +168,50 @@ export namespace notificationManager { }); return pPromise; } + + export function getSlotFlagsByBundle(bundle: BundleOption): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): number => { return nativegetSlotFlagsByBundle(bundle); }); + p.then((data: NullishType): void => { + let ret : Double = data as Double; + resolve(ret); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function setSlotFlagsByBundle(bundle: BundleOption, slotFlags: number): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativesetSlotFlagsByBundle(bundle, slotFlags); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function setNotificationEnable(bundle: BundleOption, enable: boolean): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void => { + let p = taskpool.execute((): void => { return nativeSetNotificationEnable(bundle, enable); }); + p.then((data: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + }); + }); + return pPromise; + } + + export function setNotificationEnable(bundle: BundleOption, enable: boolean, callback: AsyncCallback): void { + let p = taskpool.execute((): void => { return nativeSetNotificationEnable(bundle, enable); }); + p.then((data: NullishType): void => { + let err: BusinessError = {code: 1, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + }) + } } diff --git a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp index b4753b7b8..7b1c1c625 100644 --- a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -108,6 +108,35 @@ static ani_boolean isNotificationEnabled([[maybe_unused]]ani_env *env, ani_objec } } +static ani_int getSlotFlagsByBundle(ani_env *env, ani_object obj) +{ + ANS_LOGD("sts getSlotFlagsByBundle call"); + Notification::NotificationBundleOption option; + UnwrapBundleOption(env, obj, option); + uint32_t slotFlags = 0; + Notification::NotificationHelper::GetNotificationSlotFlagsAsBundle(option, slotFlags); + return slotFlags; +} + +static void setSlotFlagsByBundle(ani_env *env, ani_object obj) +{ + ANS_LOGD("sts setSlotFlagsByBundle call"); + Notification::NotificationBundleOption option; + UnwrapBundleOption(env, obj, option); + uint32_t slotFlags = 0; + Notification::NotificationHelper::SetNotificationSlotFlagsAsBundle(option, slotFlags); + return; +} + +static void SetNotificationEnable(ani_env *env, ani_object bundleOption, ani_boolean enable) +{ + ANS_LOGD("SetNotificationEnable enter "); + Notification::NotificationBundleOption option; + UnwrapBundleOption(env, bundleOption, option); + std::string deviceId {""}; + Notification::NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(option, deviceId, AniBooleanToBool(enable)); +} + void StsNotificationManagerRegistryInit(ani_env *env) { ANS_LOGD("StsNotificationManagerRegistryInit call"); @@ -128,6 +157,9 @@ void StsNotificationManagerRegistryInit(ani_env *env) ani_native_function {"nativeIsBadgeDisplayed", nullptr, reinterpret_cast(IsBadgeDisplayed)}, ani_native_function {"nativeGetActiveNotificationCount", ":D", reinterpret_cast(GetActiveNotificationCount)}, ani_native_function {"nativeIsNotificationEnabled", nullptr, reinterpret_cast(isNotificationEnabled)}, + ani_native_function {"nativegetSlotFlagsByBundle", nullptr, reinterpret_cast(getSlotFlagsByBundle)}, + ani_native_function {"nativesetSlotFlagsByBundle", nullptr, reinterpret_cast(setSlotFlagsByBundle)}, + ani_native_function {"nativeSetNotificationEnable", nullptr, reinterpret_cast(SetNotificationEnable)}, }; status = env->Namespace_BindNativeFunctions(ns, kitFunctions.data(), kitFunctions.size()); -- Gitee From 717f0da76a794df38ede4aedd423550bebd202b5 Mon Sep 17 00:00:00 2001 From: liujx9645 Date: Fri, 25 Apr 2025 09:53:26 +0800 Subject: [PATCH 5/5] feat:codecheck Signed-off-by:liujx9645 --- frameworks/ani/BUILD.gn | 13 +++++++++++ .../notification_manager/ets/@ohos.base.ets | 4 ++-- .../notification/NotificationCommonDef.ets | 4 ++-- .../include/sts_common_util.h | 6 ++--- .../include/sts_notification_manager.h | 2 +- .../src/sts_common_util.cpp | 2 +- .../src/sts_notification_manager.cpp | 23 +++++++++---------- 7 files changed, 33 insertions(+), 21 deletions(-) diff --git a/frameworks/ani/BUILD.gn b/frameworks/ani/BUILD.gn index 6be65d076..071a0da8d 100644 --- a/frameworks/ani/BUILD.gn +++ b/frameworks/ani/BUILD.gn @@ -1,3 +1,16 @@ +# Copyright (c) 2025 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + import("//base/notification/distributed_notification_service/notification.gni") import("//build/ohos.gni") diff --git a/frameworks/ani/notification_manager/ets/@ohos.base.ets b/frameworks/ani/notification_manager/ets/@ohos.base.ets index 993f733f7..f7b23f6b1 100644 --- a/frameworks/ani/notification_manager/ets/@ohos.base.ets +++ b/frameworks/ani/notification_manager/ets/@ohos.base.ets @@ -43,7 +43,7 @@ export class BusinessError extends Error { this.data = data; // 默认数据为 undefined } - code: number; + public code: number; - data?: T; + public data?: T; } \ No newline at end of file diff --git a/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets index 9f36eef0c..a434e6b75 100644 --- a/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets +++ b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets @@ -19,6 +19,6 @@ export interface BundleOption { } class BundleOptionInner implements BundleOption { - bundle: string = ""; - uid?: number = 0; + public bundle: string = ''; + public uid?: number = 0; } \ No newline at end of file diff --git a/frameworks/ani/notification_manager/include/sts_common_util.h b/frameworks/ani/notification_manager/include/sts_common_util.h index be428cdfc..bb0f19e4c 100644 --- a/frameworks/ani/notification_manager/include/sts_common_util.h +++ b/frameworks/ani/notification_manager/include/sts_common_util.h @@ -22,7 +22,6 @@ namespace OHOS { namespace AniCommom { -#define SETTER_METHOD_NAME(property) "" #property bool GetStdString(ani_env *env, ani_string str, std::string &res); @@ -31,9 +30,10 @@ inline bool AniBooleanToBool(ani_boolean value) return value == ANI_TRUE; } -inline ani_boolean BoolToAniBoolean(bool value) { +inline ani_boolean BoolToAniBoolean(bool value) +{ return value ? ANI_TRUE : ANI_FALSE; } } // namespace AniCommom } // namespace OHOS -#endif +#endif \ No newline at end of file diff --git a/frameworks/ani/notification_manager/include/sts_notification_manager.h b/frameworks/ani/notification_manager/include/sts_notification_manager.h index 4a09d7907..e24fdb058 100644 --- a/frameworks/ani/notification_manager/include/sts_notification_manager.h +++ b/frameworks/ani/notification_manager/include/sts_notification_manager.h @@ -21,4 +21,4 @@ namespace NotificationManagerSts { void StsNotificationManagerRegistryInit(ani_env *env); } } -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/frameworks/ani/notification_manager/src/sts_common_util.cpp b/frameworks/ani/notification_manager/src/sts_common_util.cpp index 117772a70..dc4ad565c 100644 --- a/frameworks/ani/notification_manager/src/sts_common_util.cpp +++ b/frameworks/ani/notification_manager/src/sts_common_util.cpp @@ -38,4 +38,4 @@ bool GetStdString(ani_env *env, ani_string str, std::string &res) } } // namespace AppExecFwk -} // namespace OHOS +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp index 7b1c1c625..ffd5fd93a 100644 --- a/frameworks/ani/notification_manager/src/sts_notification_manager.cpp +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -41,8 +41,7 @@ static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) bool ret = UnwrapBundleOption(env, obj, option); bool isDisplayed = false; int returncode = 0; - if (ret) - { + if (ret) { returncode = Notification::NotificationHelper::GetShowBadgeEnabledForBundle(option, isDisplayed); } else { returncode = Notification::NotificationHelper::GetShowBadgeEnabled(isDisplayed); @@ -54,10 +53,11 @@ static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) static ani_double GetActiveNotificationCount(ani_env *env) { ANS_LOGD("sts GetActiveNotificationCount call"); - uint64_t num = 0; + uint64_t num = 0; OHOS::Notification::NotificationHelper::GetActiveNotificationNums(num); ANS_LOGD("sts GetActiveNotificationCount end, num: %{public}llu", num); - return static_cast(num);; + ani_double retNum = static_cast(num); + return retNum; } static ani_boolean isNotificationEnabled([[maybe_unused]]ani_env *env, ani_object jsUserId, ani_object jsBundleOption) @@ -73,7 +73,8 @@ static ani_boolean isNotificationEnabled([[maybe_unused]]ani_env *env, ani_objec env->Reference_IsUndefined(jsUserId, &isUserIdUndefined); env->Reference_IsUndefined(jsBundleOption, &isBundleUndefined); - ANS_LOGD("isNotificationEnabled isUserIdUndefined = %{public}d isBundleUndefined = %{public}d", isUserIdUndefined, isBundleUndefined); + ANS_LOGD("isNotificationEnabled isUserIdUndefined = %{public}d isBundleUndefined = %{public}d", + isUserIdUndefined, isBundleUndefined); // 3. 解析 userId if (!isUserIdUndefined) { @@ -83,19 +84,15 @@ static ani_boolean isNotificationEnabled([[maybe_unused]]ani_env *env, ani_objec userId = 0; return ANI_FALSE; } - ret = Notification::NotificationHelper::IsAllowedNotify(userId, allowed); - ANS_LOGD("isNotificationEnabled IsAllowedNotify ret = %{public}d allowed = %{public}d", ret, allowed); } else if (!isBundleUndefined) { ANS_LOGD("isNotificationEnabled ==>> isBundleUndefined"); Notification::NotificationBundleOption option; UnwrapBundleOption(env, jsBundleOption, option); - ret = Notification::NotificationHelper::IsAllowedNotify(option, allowed); - - }else { + } else { ANS_LOGD("isNotificationEnabled ==>> no parameter"); ret = Notification::NotificationHelper::IsAllowedNotify(allowed); ANS_LOGD("isNotificationEnabled IsAllowedNotify ret = %{public}d allowed = %{public}d", ret, allowed); @@ -134,7 +131,8 @@ static void SetNotificationEnable(ani_env *env, ani_object bundleOption, ani_boo Notification::NotificationBundleOption option; UnwrapBundleOption(env, bundleOption, option); std::string deviceId {""}; - Notification::NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(option, deviceId, AniBooleanToBool(enable)); + Notification::NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(option, deviceId, + AniBooleanToBool(enable)); } void StsNotificationManagerRegistryInit(ani_env *env) @@ -155,7 +153,8 @@ void StsNotificationManagerRegistryInit(ani_env *env) std::array kitFunctions = { ani_native_function {"nativeDisplayBadge", nullptr, reinterpret_cast(DisplayBadge)}, ani_native_function {"nativeIsBadgeDisplayed", nullptr, reinterpret_cast(IsBadgeDisplayed)}, - ani_native_function {"nativeGetActiveNotificationCount", ":D", reinterpret_cast(GetActiveNotificationCount)}, + ani_native_function {"nativeGetActiveNotificationCount", ":D", + reinterpret_cast(GetActiveNotificationCount)}, ani_native_function {"nativeIsNotificationEnabled", nullptr, reinterpret_cast(isNotificationEnabled)}, ani_native_function {"nativegetSlotFlagsByBundle", nullptr, reinterpret_cast(getSlotFlagsByBundle)}, ani_native_function {"nativesetSlotFlagsByBundle", nullptr, reinterpret_cast(setSlotFlagsByBundle)}, -- Gitee