diff --git a/bundle.json b/bundle.json index de58f375c789c1037f09c35d63552c0f33216007..c43af60619c529303be2da46ad5c1c7971e3da1f 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 0000000000000000000000000000000000000000..071a0da8dfb3da17be6f183062cda92ae6d97cf2 --- /dev/null +++ b/frameworks/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_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 0000000000000000000000000000000000000000..413812c223c7a2264b75a4130a2580ecb7873bb3 --- /dev/null +++ b/frameworks/ani/notification_manager/BUILD.gn @@ -0,0 +1,82 @@ +# 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", + "./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", + ] + + 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", + "./ets/@ohos.base.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.base.ets b/frameworks/ani/notification_manager/ets/@ohos.base.ets new file mode 100644 index 0000000000000000000000000000000000000000..f7b23f6b13d5372713088cb07025bcfe5d60e1d2 --- /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 + } + + public code: number; + + public data?: T; +} \ No newline at end of file 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 0000000000000000000000000000000000000000..b0fe6d8675cc8518f0c134c12ab98ab4d8f2d8ed --- /dev/null +++ b/frameworks/ani/notification_manager/ets/@ohos.notificationManager.ets @@ -0,0 +1,217 @@ +/* + * 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 { + 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); }); + p.then((e: NullishType): void => { + let err: BusinessError = {code: 1, 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((): void => { 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; + } + + 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; + } + + 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/ets/notification/NotificationCommonDef.ets b/frameworks/ani/notification_manager/ets/notification/NotificationCommonDef.ets new file mode 100644 index 0000000000000000000000000000000000000000..a434e6b7580422a3c3ad00e4dff426a9f5115ad1 --- /dev/null +++ b/frameworks/ani/notification_manager/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 = 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 0000000000000000000000000000000000000000..72cc23e34b2ea9882d1a075be845e996959c5bd1 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..bb0f19e4c212c63ddde20c2a7583d6171b15f895 --- /dev/null +++ b/frameworks/ani/notification_manager/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_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 { + +bool GetStdString(ani_env *env, ani_string str, std::string &res); + +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 \ 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 new file mode 100644 index 0000000000000000000000000000000000000000..e24fdb058d38575915f570ede590db966c85e5a5 --- /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_bundle_option.cpp b/frameworks/ani/notification_manager/src/sts_bundle_option.cpp new file mode 100644 index 0000000000000000000000000000000000000000..503bba6f6f76029fd14291c6760b50dcb4cd14bf --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..dc4ad565cde1812df0989f82befcd878bb1ec0b7 --- /dev/null +++ b/frameworks/ani/notification_manager/src/sts_common_util.cpp @@ -0,0 +1,41 @@ +/* + * 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 { + +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; +} + +} // namespace AppExecFwk +} // 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 new file mode 100644 index 0000000000000000000000000000000000000000..ffd5fd93a3fd356cabdfbafe490e9a0a376adfd9 --- /dev/null +++ b/frameworks/ani/notification_manager/src/sts_notification_manager.cpp @@ -0,0 +1,197 @@ +/* + * 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_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 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); +} + +static ani_boolean IsBadgeDisplayed(ani_env *env, ani_object obj) +{ + ANS_LOGD("sts IsBadgeDisplayed call"); + Notification::NotificationBundleOption option; + bool ret = UnwrapBundleOption(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); +} + +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); + ani_double retNum = static_cast(num); + return retNum; +} + +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; + } +} + +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"); + 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)}, + 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()); + 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