diff --git a/bundle.json b/bundle.json index de58f375c789c1037f09c35d63552c0f33216007..28b467092ad59b86ee9edd7c78847d1aa16e3d25 100644 --- a/bundle.json +++ b/bundle.json @@ -76,6 +76,7 @@ "device_usage_statistics", "bundle_framework", "time_service", + "runtime_core", "safwk", "ability_runtime", "ability_base", @@ -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/interfaces/kits/ani:ani_ans_packages" ], "service_group": [ "//base/notification/distributed_notification_service/sa_profile:ans_sa_profile", diff --git a/interfaces/kits/ani/BUILD.gn b/interfaces/kits/ani/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..4882b30ff25119d66cd535a87adfb4feeb4d9b76 --- /dev/null +++ b/interfaces/kits/ani/BUILD.gn @@ -0,0 +1,22 @@ +# 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") + +group("ani_ans_packages") { + deps = [ + "notification_subscriber:notification_subscribe_ani_kit", + "notification_subscriber:notification_subscribe_abc_etc" + ] +} \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/BUILD.gn b/interfaces/kits/ani/notification_subscriber/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..713677db47e3b7d6e02289f372b302052d56b11f --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/BUILD.gn @@ -0,0 +1,87 @@ +# 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_subscribe_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", + ] + + configs = [ + "${frameworks_module_ans_path}:ans_innerkits_public_config", + ] + + sources = [ + "./src/sts_notification_subscribe.cpp", + "./src/sts_common_util.cpp", + "./src/sts_notification_key.cpp", + "./src/sts_reason_enum.cpp", + "./src/sts_bundle_option.cpp", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + deps = [ "${frameworks_module_ans_path}:ans_innerkits" ] + + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "runtime_core:ani", + "ipc:ipc_core", + "ability_base:zuri", + "ability_base:want", + "image_framework:image", + "ability_runtime:wantagent_innerkits", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" +} + +generate_static_abc("notification_subscribe_abc") { + base_url = "./ets" + files = [ + "./ets/@ohos.notificationSubscribe.ets", + "./ets/notification/NotificationCommonDef.ets", + ] + + dst_file = "$target_out_dir/notification_subscribe.abc" + out_puts = [ "$target_out_dir/notification_subscribe.abc" ] + + is_boot_abc = "True" + device_dst_file = "/system/framework/notification_subscribe.abc" +} + +ohos_prebuilt_etc("notification_subscribe_abc_etc") { + source = "$target_out_dir/notification_subscribe.abc" + module_install_dir = "framework" + subsystem_name = "${subsystem_name}" + part_name = "${component_name}" + deps = [ ":notification_subscribe_abc" ] +} \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/ets/@ohos.notificationSubscribe.ets b/interfaces/kits/ani/notification_subscriber/ets/@ohos.notificationSubscribe.ets new file mode 100644 index 0000000000000000000000000000000000000000..9be3f68cb47d1256abbf539592b55066f4e95622 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/ets/@ohos.notificationSubscribe.ets @@ -0,0 +1,149 @@ +/* + * 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 { AsyncCallback, BusinessError } from '@ohos.base'; +import { BundleOption } from 'notification.NotificationCommonDef'; +type ResolveCallback = (data: T) => void; +type RejectCallback = (err: Object) => void; +export default namespace notificationSubscribe { + export interface NotificationKey { + id: number; + label?: string; + } + + class NotificationKeyInner implements NotificationKey { + public id: number = -1; + public label?: string; + } + + export enum RemoveReason { + CLICK_REASON_REMOVE = 1, + CANCEL_REASON_REMOVE = 2 + } + + export native function nativeRemove( + bundle: BundleOption, + notificationKey: NotificationKey, + reason: RemoveReason): void; + export native function nativeRemove(hashCode: string, reason: RemoveReason): void; + export native function nativeRemove(hashCodes: Array, reason: RemoveReason): void; + export native function nativeDistributeOperation(hashcode: string, operationInfo?: OperationInfo): void; + + export function remove( + bundle: BundleOption, + notificationKey: NotificationKey, + reason: RemoveReason, + callback: AsyncCallback + ): void { + let p = taskpool.execute((): void => { + notificationSubscribe.nativeRemove(bundle, notificationKey, reason); + }); + p.then( + (e: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + } + ) + } + + export function remove( + bundle: BundleOption, + notificationKey: NotificationKey, + reason: RemoveReason + ): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void =>{ + let p = taskpool.execute((): void => { + notificationSubscribe.nativeRemove(bundle, notificationKey, reason); + }); + p.then( + (e: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + } + ) + }); + return pPromise; + } + + export function remove(hashCode: string, reason: RemoveReason, callback: AsyncCallback): void { + let p = taskpool.execute((): void => { + return notificationSubscribe.nativeRemove(hashCode, reason); + }); + p.then( + (e: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + } + ) + } + + export function remove(hashCode: string, reason: RemoveReason): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void =>{ + let p = taskpool.execute((): void => { + return notificationSubscribe.nativeRemove(hashCode, reason); + }); + p.then( + (e: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + } + ) + }); + return pPromise; + } + + export function remove(hashCodes: Array, reason: RemoveReason, callback: AsyncCallback): void { + let p = taskpool.execute((): void => { + notificationSubscribe.nativeRemove(hashCodes, reason); + }); + p.then( + (e: NullishType): void => { + let err: BusinessError = {code: 0, data: undefined}; + callback(err, undefined); + }, (error: Object): void => { + } + ) + } + + export function remove(hashCodes: Array, reason: RemoveReason): Promise { + let pPromise = new Promise((resolve: ResolveCallback, reject: RejectCallback): void =>{ + let p = taskpool.execute((): void => { + notificationSubscribe.nativeRemove(hashCodes, reason); + }); + p.then( + (e: NullishType): void => { + resolve(undefined); + }, (error: Object): void => { + reject(error); + } + ) + }); + return pPromise; + } + + export interface OperationInfo { + actionName?: string; + userInput?: string; + } + + class OperationInfoInner implements OperationInfo { + public actionName?: string; + public userInput?: string; + } +} \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/ets/notification/NotificationCommonDef.ets b/interfaces/kits/ani/notification_subscriber/ets/notification/NotificationCommonDef.ets new file mode 100644 index 0000000000000000000000000000000000000000..7fd120946bb82014a93ba33358f0b476a214acae --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/ets/notification/NotificationCommonDef.ets @@ -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. + */ + +export interface BundleOption { + bundle: string; + uid?: number; +} + +class BundleOptionInner implements BundleOption { + public bundle: string = ''; + public uid?: number; +} \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/include/sts_bundle_option.h b/interfaces/kits/ani/notification_subscriber/include/sts_bundle_option.h new file mode 100755 index 0000000000000000000000000000000000000000..d789b2a1e72dde6a7c251e35160cd637d1dd0e79 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/include/sts_bundle_option.h @@ -0,0 +1,27 @@ +/* + * 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 { +using NotificationBundleOption = OHOS::Notification::NotificationBundleOption; + bool UnWrapBundleOption(ani_env *env, ani_object param, NotificationBundleOption& option); +} // namespace NotificationSts +} // namespace OHOS +#endif // OHOS_DISTRIBUTED_NOTIFICATION_SERVER_STS_BUNDLE_OPTION_H \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/include/sts_common_util.h b/interfaces/kits/ani/notification_subscriber/include/sts_common_util.h new file mode 100644 index 0000000000000000000000000000000000000000..9b14a4f81ed6b8c97913774697f09769bb20a6ca --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/include/sts_common_util.h @@ -0,0 +1,39 @@ +/* + * 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_STS_COMMON_UTIL_H +#define OHOS_STS_COMMON_UTIL_H +#include "ani.h" +#include +#include +namespace OHOS { +namespace NotificationSts { +static const char *cRemoveForBundleSignature = + "Lnotification/NotificationCommonDef/BundleOption;" + "L@ohos/notificationSubscribe/notificationSubscribe/NotificationKey;" + "L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V"; +static const char *cRemoveForHashCodeSignature = + "Lstd/core/String;L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V"; +static const char *cRemoveForHashCodesSignature = + "Lescompat/Array;L@ohos/notificationSubscribe/notificationSubscribe/RemoveReason;:V"; + + bool GetPropertyRef(ani_env *env, ani_object obj, const char *name, ani_ref &ref, ani_boolean &isUndefined); + bool GetStdString(ani_env *env, const ani_string str, std::string &res); + bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res); + bool ConvertToStdVectorString(ani_env *env, const ani_object ani_obj, std::vector &std_Vstring); + bool GetDouble(ani_env *env, ani_object param, const char *className, const char *name, ani_double &value); +} // namespace NotificationSts +} // namespace OHOS +#endif // OHOS_STS_COMMON_UTIL_H diff --git a/interfaces/kits/ani/notification_subscriber/include/sts_notification_key.h b/interfaces/kits/ani/notification_subscriber/include/sts_notification_key.h new file mode 100644 index 0000000000000000000000000000000000000000..74208599403a485bae8e5480857af9fb3cf4a88d --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/include/sts_notification_key.h @@ -0,0 +1,26 @@ +/* + * 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_STS_NOTIFICATION_KEY_UTIL_H +#define OHOS_STS_NOTIFICATION_KEY_UTIL_H +#include "ani.h" +#include "notification_request.h" +namespace OHOS { +namespace NotificationSts { +using NotificationKey = OHOS::Notification::NotificationKey; + bool UnWarpNotificationKey(ani_env *env, const ani_object obj, NotificationKey &OutObj); +} // namespace NotificationSts +} // namespace OHOS +#endif // OHOS_STS_NOTIFICATION_KEY_UTIL_H diff --git a/interfaces/kits/ani/notification_subscriber/include/sts_notification_subscribe.h b/interfaces/kits/ani/notification_subscriber/include/sts_notification_subscribe.h new file mode 100644 index 0000000000000000000000000000000000000000..c856576d2bbbb7b7c3aa07ab323acf19d9ed6cbd --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/include/sts_notification_subscribe.h @@ -0,0 +1,25 @@ +/* + * 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_STS_NOTIFICATION_SUBSCRIBE_H +#define OHOS_STS_NOTIFICATION_SUBSCRIBE_H +#include "ani.h" + +namespace OHOS { +namespace NotificationSts { +void StsNotificationSubscribeRegistryInit(ani_env *env); +} // namespace NotificationSts +} // namespace OHOS +#endif // OHOS_STS_NOTIFICATION_SUBSCRIBE_H diff --git a/interfaces/kits/ani/notification_subscriber/include/sts_reason_enum.h b/interfaces/kits/ani/notification_subscriber/include/sts_reason_enum.h new file mode 100644 index 0000000000000000000000000000000000000000..cf44bc3669bfc39da49b269b31bdd8437fbfdb11 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/include/sts_reason_enum.h @@ -0,0 +1,25 @@ +/* + * 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_STS_REASON_ENUM_UTIL_H +#define OHOS_STS_REASON_ENUM_UTIL_H +#include "ani.h" +namespace OHOS { +namespace NotificationSts { + bool IsValidRemoveReason(int32_t reasonType); + bool UnwarpReasonEnum(ani_env *env, const ani_object enumItem, int32_t &outEnum); +} // namespace NotificationSts +} // namespace OHOS +#endif // OHOS_STS_REASON_ENUM_UTIL_H diff --git a/interfaces/kits/ani/notification_subscriber/src/sts_bundle_option.cpp b/interfaces/kits/ani/notification_subscriber/src/sts_bundle_option.cpp new file mode 100755 index 0000000000000000000000000000000000000000..d191070f69d5f188adfb822a335122ddbf1a2717 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/src/sts_bundle_option.cpp @@ -0,0 +1,65 @@ +/* + * 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 "sts_bundle_option.h" +#include "ans_log_wrapper.h" + +namespace OHOS { +namespace NotificationSts { +bool UnWrapBundleOption(ani_env *env, ani_object obj, 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 NotificationSts +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/src/sts_common_util.cpp b/interfaces/kits/ani/notification_subscriber/src/sts_common_util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6da4486691054b97a404e62f15ada0663bf17129 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/src/sts_common_util.cpp @@ -0,0 +1,112 @@ +/* + * 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_common_util.h" +#include "ans_log_wrapper.h" +#include "notification_helper.h" +namespace OHOS { +namespace NotificationSts { +bool GetPropertyRef(ani_env *env, ani_object obj, const char *name, ani_ref &ref, ani_boolean &isUndefined) +{ + ani_status status = env->Object_GetPropertyByName_Ref(obj, name, &ref); + if (status != ANI_OK) { + ANS_LOGD("Failed to get property '%{public}s', status: %{public}d", name, status); + return false; + } + status = env->Reference_IsUndefined(ref, &isUndefined); + if (status != ANI_OK) { + ANS_LOGD("Failed to check undefined for '%{public}s', status: %{public}d", name, status); + return false; + } + return true; +} + +bool GetStdString(ani_env *env, const 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; +} + +bool GetStringOrUndefined(ani_env *env, ani_object param, const char *name, std::string &res) +{ + ani_ref obj = nullptr; + ani_boolean hasValue = true; + if (GetPropertyRef(env, param, name, obj, hasValue) && hasValue) { + ANS_LOGD("%{public}s : undefined", name); + return false; + } + if (!GetStdString(env, reinterpret_cast(obj), res)) { + ANS_LOGD("GetStdString failed"); + return false; + } + return true; +} + +bool ConvertToStdVectorString(ani_env *env, const ani_object ani_obj, std::vector &std_Vstring) +{ + ani_double length; + ani_status status = env->Object_GetPropertyByName_Double(ani_obj, "length", &length); + if (status != ANI_OK) { + ANS_LOGD("Object_GetPropertyByName_Double faild. status %{public}d", status); + return false; + } + for (int i = 0; i < int(length); i++) { + ani_ref stringEntryRef; + status = env->Object_CallMethodByName_Ref(ani_obj, + "$_get", "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i); + if (status != ANI_OK) { + ANS_LOGD("status : %{public}d", status); + } + std::string std_string; + if (!GetStdString(env, static_cast(stringEntryRef), std_string)) { + ANS_LOGD("GetStdString faild"); + return false; + } + std_Vstring.emplace_back(std_string); + } + return true; +} + +bool GetDouble(ani_env *env, ani_object param, const char *className, const char *name, ani_double &value) +{ + ani_class cls; + if (ANI_OK != env->FindClass(className, &cls)) { + ANS_LOGD("FindClass faild. %{public}s", className); + return false; + } + ani_method idGetter; + if (ANI_OK != env->Class_FindMethod(cls, "id", nullptr, &idGetter)) { + ANS_LOGD("Class_FindMethod faild. %{public}s", className); + return false; + } + if (ANI_OK != env->Object_CallMethod_Double(param, idGetter, &value)) { + ANS_LOGD("Object_CallMethod_Double faild. %{public}s", className); + return false; + } + return true; +} +} // namespace NotificationSts +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/src/sts_notification_key.cpp b/interfaces/kits/ani/notification_subscriber/src/sts_notification_key.cpp new file mode 100644 index 0000000000000000000000000000000000000000..675fa8d6213eb7d88c3976ac241f03c7eda96ac7 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/src/sts_notification_key.cpp @@ -0,0 +1,40 @@ +/* + * 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 "ans_log_wrapper.h" +#include "sts_common_util.h" +#include "sts_notification_key.h" +namespace OHOS { +namespace NotificationSts { +bool UnWarpNotificationKey(ani_env *env, const ani_object obj, NotificationKey &OutObj) +{ + ani_double idDouble = 0.0; + if (!GetDouble(env, obj, + "L@ohos/notificationSubscribe/notificationSubscribe/NotificationKeyInner;", "id", idDouble)) { + ANS_LOGD("GetDouble id fail"); + return false; + } + OutObj.id = static_cast(idDouble); + std::string label; + if (!GetStringOrUndefined(env, obj, "label", label)) { + ANS_LOGD("ConvertNotificationKeyToNative GetStringOrUndefined label fail"); + return false; + } + OutObj.label = label; + ANS_LOGD("ConvertNotificationKeyToNative id: %{public}d, label: %{public}s", OutObj.id, OutObj.label.c_str()); + return true; +} +} // namespace NotificationSts +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/src/sts_notification_subscribe.cpp b/interfaces/kits/ani/notification_subscriber/src/sts_notification_subscribe.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f6ee9da3732223cb0f11e62593dc0436eabc547 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/src/sts_notification_subscribe.cpp @@ -0,0 +1,142 @@ +/* + * 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 "ans_log_wrapper.h" +#include "sts_notification_subscribe.h" +#include +#include "notification_helper.h" +#include "sts_reason_enum.h" +#include "sts_notification_key.h" +#include "sts_bundle_option.h" +#include "sts_common_util.h" + +namespace OHOS { +namespace NotificationSts { +static void StsRemoveForBundle(ani_env *env, ani_object bundle, ani_object notificationKey, ani_object reasonEnum) +{ + ANS_LOGD("StsRemoveForBundle enter"); + NotificationBundleOption option; + NotificationKey key; + int32_t reasonType = -1; + if (!UnWrapBundleOption(env, bundle, option)) { + ANS_LOGD("bundle is valid"); + return; + } + if (!UnWarpNotificationKey(env, notificationKey, key)) { + ANS_LOGD("notificationKey is valid"); + return; + } + if (!UnwarpReasonEnum(env, reasonEnum, reasonType)) { + ANS_LOGD("enum convert failed"); + return; + } + if (!IsValidRemoveReason(reasonType)) { + ANS_LOGD("reasonType is valid"); + return; + } + int ret = OHOS::Notification::NotificationHelper::RemoveNotification(option, key.id, key.label, reasonType); + ANS_LOGD("StsRemoveForBundle RemoveNotification ret %{public}d", ret); +} + +static void StsRemoveForHashCode(ani_env *env, ani_string hashCode, ani_object reasonEnum) +{ + ANS_LOGD("StsRemoveForHashCode enter"); + int32_t reasonType = -1; + std::string hashCodeStd; + if (!GetStdString(env, hashCode, hashCodeStd)) { + ANS_LOGD("hashCode is valid"); + return; + } + if (!UnwarpReasonEnum(env, reasonEnum, reasonType)) { + ANS_LOGD("enum convert failed"); + return; + } + if (!IsValidRemoveReason(reasonType)) { + ANS_LOGD("reasonType is valid"); + return; + } + ANS_LOGD("hashCode: %{public}s, reasonType: %{public}d", hashCodeStd.c_str(), reasonType); + int ret = OHOS::Notification::NotificationHelper::RemoveNotification(hashCodeStd, reasonType); + ANS_LOGD("StsRemoveForHashCode RemoveNotification ret %{public}d", ret); +} + +static void StsRemoveForHashCodes(ani_env *env, ani_object hashCodes, ani_object reasonEnum) +{ + ANS_LOGD("StsRemoveForHashCodes enter"); + std::vector hashCodesStd; + int32_t reasonType; + if (!ConvertToStdVectorString(env, hashCodes, hashCodesStd)) { + ANS_LOGD("hashCodes is valid"); + return; + } + if (!UnwarpReasonEnum(env, reasonEnum, reasonType)) { + ANS_LOGD("enum convert failed"); + return; + } + if (!IsValidRemoveReason(reasonType)) { + ANS_LOGD("reasonType is valid"); + return; + } + int ret = OHOS::Notification::NotificationHelper::RemoveNotifications(hashCodesStd, reasonType); + ANS_LOGD("StsRemoveForHashCodes RemoveNotification ret %{public}d", ret); +} + +void StsNotificationSubscribeRegistryInit(ani_env *env) +{ + static const char *npName = "L@ohos/notificationSubscribe/notificationSubscribe;"; + ani_namespace np; + if (ANI_OK != env->FindNamespace(npName, &np)) { + ANS_LOGD("Not found '%{public}s'", npName); + return; + } + + std::array methods = { + ani_native_function {"nativeRemove", cRemoveForBundleSignature, reinterpret_cast(StsRemoveForBundle)}, + ani_native_function {"nativeRemove", + cRemoveForHashCodeSignature, reinterpret_cast(StsRemoveForHashCode)}, + ani_native_function {"nativeRemove", + cRemoveForHashCodesSignature, reinterpret_cast(StsRemoveForHashCodes)}, + }; + + ANS_LOGD("Start bind native methods to '%{public}s'", npName); + if (ANI_OK != env->Namespace_BindNativeFunctions(np, methods.data(), methods.size())) { + ANS_LOGD("Cannot bind native methods to '%{public}s'", npName); + return; + }; + ANS_LOGD("Finish bind native methods to '%{public}s'", npName); + + if (env->ResetError() != ANI_OK) { + ANS_LOGD("ResetError failed"); + } +} +} // NotificationSts +} // OHOS + +extern "C" { +ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) +{ + ANS_LOGD("ANI_Constructor enter"); + ani_env *env; + if (ANI_OK != vm->GetEnv(ANI_VERSION_1, &env)) { + ANS_LOGD("Unsupported ANI_VERSION_1"); + return ANI_ERROR; + } + + OHOS::NotificationSts::StsNotificationSubscribeRegistryInit(env); + ANS_LOGD("ANI_Constructor OK"); + *result = ANI_VERSION_1; + return ANI_OK; +} +} \ No newline at end of file diff --git a/interfaces/kits/ani/notification_subscriber/src/sts_reason_enum.cpp b/interfaces/kits/ani/notification_subscriber/src/sts_reason_enum.cpp new file mode 100644 index 0000000000000000000000000000000000000000..4e1596dad2fecc06b624b7e79aa678dfa4d68504 --- /dev/null +++ b/interfaces/kits/ani/notification_subscriber/src/sts_reason_enum.cpp @@ -0,0 +1,44 @@ +/* + * 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 "ans_log_wrapper.h" +#include "notification_constant.h" +#include "sts_reason_enum.h" +namespace OHOS { +namespace NotificationSts { +bool IsValidRemoveReason(int32_t reasonType) +{ + if (reasonType == Notification::NotificationConstant::CLICK_REASON_DELETE || + reasonType == Notification::NotificationConstant::CANCEL_REASON_DELETE) { + return true; + } + ANS_LOGD("Reason %{public}d is an invalid value", reasonType); + return false; +} + +bool UnwarpReasonEnum(ani_env *env, const ani_object enumItem, int32_t &outEnum) +{ + ani_status status = ANI_ERROR; + ani_int intValue{}; + status = env->EnumItem_GetValue_Int(static_cast(enumItem), &intValue); + if (ANI_OK != status) { + ANS_LOGD("EnumConvert_StsToNative failed, status : %{public}d", status); + return false; + } + outEnum = static_cast(intValue); + return true; +} +} // namespace NotificationSts +} // namespace OHOS \ No newline at end of file