diff --git a/frameworks/ans/IAnsManager.idl b/frameworks/ans/IAnsManager.idl index 44ccca6324f6c7e7b9d3435236cc3b57c5b4ae75..88f585c97383acc07a4250795cee21dfcf1680f8 100644 --- a/frameworks/ans/IAnsManager.idl +++ b/frameworks/ans/IAnsManager.idl @@ -279,6 +279,10 @@ interface OHOS.Notification.IAnsManager { void SetSmartReminderEnabled([in] String deviceType, [in] boolean enabled); + void IsSilentReminderEnabled([in] sptr bundleOption, [out] int enabled); + + void SetSilentReminderEnabled([in] sptr bundleOption, [in] boolean enabled); + void IsDistributedEnabledByBundle([in] sptr bundleOption, [in] String deviceType, [out] boolean enabled); void CancelAsBundleWithAgent([in] sptr bundleOption, [in] int id); diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 2bd4e6516fa83550449b64e0839563a120c96d8b..c88bf75d4272edacefe382b69d415e13d0707a35 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -638,6 +638,16 @@ ErrCode NotificationHelper::IsSmartReminderEnabled(const std::string &deviceType return DelayedSingleton::GetInstance()->IsSmartReminderEnabled(deviceType, enabled); } +ErrCode NotificationHelper::SetSilentReminderEnabled(const NotificationBundleOption &bundleOption, const bool enabled) +{ + return DelayedSingleton::GetInstance()->SetSilentReminderEnabled(bundleOption, enabled); +} + +ErrCode NotificationHelper::IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus) +{ + return DelayedSingleton::GetInstance()->IsSilentReminderEnabled(bundleOption, enableStatus); +} + ErrCode NotificationHelper::SetDistributedEnabledBySlot( const NotificationConstant::SlotType &slotType, const std::string &deviceType, const bool enabled) { diff --git a/frameworks/ans/test/unittest/notification_helper_test.cpp b/frameworks/ans/test/unittest/notification_helper_test.cpp index 7b301240e14c5e82ce5adca656a206f80dd0773f..d3c25258c3fbdd1b6ac318e897444f9d9f065bfb 100644 --- a/frameworks/ans/test/unittest/notification_helper_test.cpp +++ b/frameworks/ans/test/unittest/notification_helper_test.cpp @@ -1319,5 +1319,49 @@ HWTEST_F(NotificationHelperTest, DisableNotificationFeature_00001, Function | Sm ErrCode ret = notificationHelper.DisableNotificationFeature(notificationDisable); EXPECT_EQ(ret, ERR_ANS_PERMISSION_DENIED); } + +/** + * @tc.name: SilentReminderEnabled_00001 + * @tc.desc: Test SetSilentReminderEnabled. + * @tc.type: FUNC + */ +HWTEST_F(NotificationHelperTest, SetSilentReminderEnabled_00001, Function | SmallTest | Level1) +{ + NotificationHelper notificationHelper; + NotificationBundleOption bo; + bo.SetBundleName("bundleName"); + bo.SetUid(1); + ErrCode ret = notificationHelper.SetSilentReminderEnabled(bo, true); + EXPECT_EQ(ret, ERR_ANS_PERMISSION_DENIED); +} + +/** + * @tc.name: SilentReminderEnabled_00002 + * @tc.desc: Test SetSilentReminderEnabled. + * @tc.type: FUNC + */ +HWTEST_F(NotificationHelperTest, SetSilentReminderEnabled_00002, Function | SmallTest | Level1) +{ + NotificationHelper notificationHelper; + NotificationBundleOption bo; + bo.SetUid(1); + ErrCode ret = notificationHelper.SetSilentReminderEnabled(bo, true); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name: IsSilentReminderEnabled_00001 + * @tc.desc: Test SetSilentReminderEnabled. + * @tc.type: FUNC + */ +HWTEST_F(NotificationHelperTest, IsSilentReminderEnabled_00001, Function | SmallTest | Level1) +{ + NotificationHelper notificationHelper; + NotificationBundleOption bo; + bo.SetUid(1); + int32_t enableStatus = 0; + ErrCode ret = notificationHelper.IsSilentReminderEnabled(bo, enableStatus); + EXPECT_EQ(ret, ERR_ANS_INVALID_PARAM); +} } } diff --git a/frameworks/core/common/include/ans_convert_enum.h b/frameworks/core/common/include/ans_convert_enum.h index a771515c44525412f767b75988e09e4009c876a7..5ecdbd87054f7f853c28427cc40d64fb1082b266 100644 --- a/frameworks/core/common/include/ans_convert_enum.h +++ b/frameworks/core/common/include/ans_convert_enum.h @@ -135,6 +135,13 @@ enum class LiveViewTypes { LIVE_VIEW_INSTANT_BANNER }; +enum class EnableStatus { + DEFAULT_FALSE, + DEFAULT_TRUE, + ENABLE_TRUE, + ENABLE_FALSE +}; + class AnsEnumUtil { public: /** diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index bdac614a0b8c8943d1aeb5b64359c0421fd57c00..b5d177b43b6384999310d8c9f8dc2dc889443c9e 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -1127,6 +1127,30 @@ public: ErrCode IsDistributedEnabledByBundle( const NotificationBundleOption &bundleOption, const std::string &deviceType, bool &enabled); + /** + * @brief Sets whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that notifications + * are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode SetSilentReminderEnabled(const NotificationBundleOption &bundleOption, const bool enabled); + + /** + * @brief get whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that notifications + * are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus); + /** * @brief Configuring Whether to Synchronize Common Notifications to Target Devices. * diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index aab66f3aac7a39b126f57805090eff3be14c1d64..96c86a0630910a9ad51c64d9d52d0eb5c7dbfcfd 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -2130,6 +2130,52 @@ ErrCode AnsNotification::IsDistributedEnabledByBundle(const NotificationBundleOp return proxy->IsDistributedEnabledByBundle(bo, deviceType, enabled); } +ErrCode AnsNotification::SetSilentReminderEnabled(const NotificationBundleOption &bundleOption, + const bool enabled) +{ + ANS_LOGD("enter"); + if (bundleOption.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("SetSilentReminderEnabledCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); + if (bo == nullptr) { + ANS_LOGE("Fail: bundleOption is empty."); + return ERR_ANS_INVALID_PARAM; + } + return proxy->SetSilentReminderEnabled(bo, enabled); +} + +ErrCode AnsNotification::IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, + int32_t &enableStatus) +{ + ANS_LOGD("enter"); + if (bundleOption.GetBundleName().empty()) { + ANS_LOGE("Invalid bundle name."); + return ERR_ANS_INVALID_PARAM; + } + + sptr proxy = GetAnsManagerProxy(); + if (!proxy) { + ANS_LOGE("IsSilentReminderEnabledCallback fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + sptr bo(new (std::nothrow) NotificationBundleOption(bundleOption)); + if (bo == nullptr) { + ANS_LOGE("Fail: bundleOption is empty."); + return ERR_ANS_INVALID_PARAM; + } + return proxy->IsSilentReminderEnabled(bo, enableStatus); +} + ErrCode AnsNotification::SetSmartReminderEnabled(const std::string &deviceType, const bool enabled) { ANS_LOGD("called"); diff --git a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp index 6b707dd2bb3d94d9239cb0bcc1ccb2691462f3f2..10fd8864d8af6e34919c7801444d05f411938cf1 100644 --- a/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp +++ b/frameworks/core/test/unittest/ans_notification_branch_test/ans_notification_branch_test.cpp @@ -727,6 +727,18 @@ public: { return ERR_ANS_INVALID_PARAM; } + + ErrCode SetSilentReminderEnabled(const sptr &bundleOption, + const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSilentReminderEnabled(const sptr &bundleOption, + int32_t &enableStatusInt) override + { + return ERR_ANS_INVALID_PARAM; + } }; class AnsNotificationBranchTest : public testing::Test { diff --git a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h index 19e59787089eba193e3cfaf4504a2fcb7e770137..c435ba9a4b6959953e45365c22ac8e54af338de4 100644 --- a/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h +++ b/frameworks/core/test/unittest/mock/mock_ans_manager_proxy.h @@ -184,6 +184,10 @@ public: MOCK_METHOD1(SetHashCodeRule, ErrCode(uint32_t)); MOCK_METHOD4(RemoveDistributedNotifications, ErrCode(const std::vector& hashcodes, const int32_t, const int32_t, const int32_t)); + MOCK_METHOD2(SetSilentReminderEnabled, ErrCode(const sptr &bundleOption, + const bool enabled)); + MOCK_METHOD2(IsSilentReminderEnabled, ErrCode(const sptr &bundleOption, + int32_t &enableStatusInt)); #ifdef NOTIFICATION_SMART_REMINDER_SUPPORTED MOCK_METHOD1(RegisterSwingCallback, ErrCode(const sptr&)); #endif diff --git a/frameworks/js/napi/include/constant.h b/frameworks/js/napi/include/constant.h index 4c03ae929a806ad16d27120019ce6ecfb1ec94fa..f5fdd10c255182e2bbc828137a7338f42a9a152a 100644 --- a/frameworks/js/napi/include/constant.h +++ b/frameworks/js/napi/include/constant.h @@ -31,6 +31,7 @@ napi_value DoNotDisturbTypeInit(napi_env env, napi_value exports); napi_value DeviceRemindTypeInit(napi_env env, napi_value exports); napi_value ConstantInit(napi_env env, napi_value exports); napi_value LiveViewStatusInit(napi_env env, napi_value exports); +napi_value EnableStatusInit(napi_env env, napi_value exports); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/include/manager/napi_silent_reminder_enable.h b/frameworks/js/napi/include/manager/napi_silent_reminder_enable.h new file mode 100644 index 0000000000000000000000000000000000000000..f5dfb6dfe170adfd6e80d64744a8caa7c99c5907 --- /dev/null +++ b/frameworks/js/napi/include/manager/napi_silent_reminder_enable.h @@ -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. + */ + +#ifndef BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SILEMT_REMINDER_ENABLE_H +#define BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SILEMT_REMINDER_ENABLE_H + +#include "common.h" +#include + +namespace OHOS { +namespace NotificationNapi { +using namespace OHOS::Notification; + +struct SilentReminderEnableParams { + NotificationBundleOption option; + int32_t enableStatus = 0; + bool enabled; +}; + +struct AsyncCallbackSilentReminderEnable { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + SilentReminderEnableParams params; + CallbackPromiseInfo info; +}; + +napi_value NapiSetSilentReminderEnabled(napi_env env, napi_callback_info info); +napi_value NapiIsSilentReminderEnabled(napi_env env, napi_callback_info info); +} +} + +#endif // BASE_NOTIFICATION_DISTRIBUTED_NOTIFICATION_SERVICE_FRAMEWORKS_JS_NAPI_SILEMT_REMINDER_ENABLE_H \ No newline at end of file diff --git a/frameworks/js/napi/src/constant.cpp b/frameworks/js/napi/src/constant.cpp index ea63e8069a65d877afa985e30a034c20d4092988..b59204c691c3b0661c31b1bd215c3df221d548cd 100644 --- a/frameworks/js/napi/src/constant.cpp +++ b/frameworks/js/napi/src/constant.cpp @@ -346,6 +346,26 @@ napi_value LiveViewStatusInit(napi_env env, napi_value exports) return exports; } +napi_value EnableStatusInit(napi_env env, napi_value exports) +{ + ANS_LOGD("%{public}s, called", __func__); + + napi_value obj = nullptr; + napi_create_object(env, &obj); + + SetNamedPropertyByInteger(env, obj, (int32_t)EnableStatus::DEFAULT_FALSE, "DEFAULT_FALSE"); + SetNamedPropertyByInteger(env, obj, (int32_t)EnableStatus::DEFAULT_TRUE, "DEFAULT_TRUE"); + SetNamedPropertyByInteger(env, obj, (int32_t)EnableStatus::ENABLE_TRUE, "ENABLE_TRUE"); + SetNamedPropertyByInteger(env, obj, (int32_t)EnableStatus::ENABLE_FALSE, "ENABLE_FALSE"); + + napi_property_descriptor exportFuncs[] = { + DECLARE_NAPI_PROPERTY("EnableStatus", obj), + }; + + napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs); + return exports; +} + napi_value ConstantInit(napi_env env, napi_value exports) { RemoveReasonInit(env, exports); @@ -362,6 +382,7 @@ napi_value ConstantInit(napi_env env, napi_value exports) DeviceRemindTypeInit(env, exports); NotificationFlagTypeInit(env, exports); LiveViewStatusInit(env, exports); + EnableStatusInit(env, exports); return exports; } } // namespace NotificationNapi diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index fad2ff86d3f0ecdf977e1943333e24633bee7773..e5f458619ff2c4b0fa84738cf2c18dd0e1349865 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -88,6 +88,7 @@ ohos_shared_library("notificationmanager") { "napi_push.cpp", "napi_push_callback.cpp", "napi_remove_group.cpp", + "napi_silent_reminder_enable.cpp", "napi_slot.cpp", "napi_sync_config.cpp", "napi_template.cpp", diff --git a/frameworks/js/napi/src/manager/init_module.cpp b/frameworks/js/napi/src/manager/init_module.cpp index c047d0524bec92137535b0a6881ca3c62308555f..0912bc380fa9fb591235edbe6aba5b81832a38ea 100644 --- a/frameworks/js/napi/src/manager/init_module.cpp +++ b/frameworks/js/napi/src/manager/init_module.cpp @@ -36,6 +36,7 @@ #include "napi_distributed_enable.h" #include "napi_sync_config.h" #include "napi_open_settings.h" +#include "napi_silent_reminder_enable.h" namespace OHOS { namespace NotificationNapi { @@ -100,6 +101,8 @@ napi_value NotificationManagerInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("setTargetDeviceStatus", NapiSetTargetDeviceStatus), DECLARE_NAPI_FUNCTION("requestEnableNotification", NapiRequestEnableNotification), DECLARE_NAPI_FUNCTION("getNotificationSetting", NapiGetNotificationSettings), + DECLARE_NAPI_FUNCTION("setSilentReminderEnabled", NapiSetSilentReminderEnabled), + DECLARE_NAPI_FUNCTION("isSilentReminderEnabled", NapiIsSilentReminderEnabled), #ifdef ANS_FEATURE_BADGE_MANAGER DECLARE_NAPI_FUNCTION("displayBadge", NapiDisplayBadge), diff --git a/frameworks/js/napi/src/manager/napi_silent_reminder_enable.cpp b/frameworks/js/napi/src/manager/napi_silent_reminder_enable.cpp new file mode 100644 index 0000000000000000000000000000000000000000..6f38d94b2164686cf75d103b8958dc7cfa7cb8d4 --- /dev/null +++ b/frameworks/js/napi/src/manager/napi_silent_reminder_enable.cpp @@ -0,0 +1,206 @@ +/* + * 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 "napi_silent_reminder_enable.h" + +#include "ans_inner_errors.h" +#include "js_native_api.h" +#include "js_native_api_types.h" + +namespace OHOS { +namespace NotificationNapi { + +const int SET_SILENT_REMINDER_ENABLE_MAX_PARA = 2; +const int SET_SILENT_REMINDER_ENABLE_MIN_PARA = 1; + +void AsyncCompleteCallbackNapiSetSilentReminderEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + AsyncCallbackSilentReminderEnable *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + Common::CreateReturnValue(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete NapiSetSmartReminderEnabled callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, SilentReminderEnableParams ¶ms) +{ + ANS_LOGD("enter"); + + size_t argc = SET_SILENT_REMINDER_ENABLE_MAX_PARA; + napi_value argv[SET_SILENT_REMINDER_ENABLE_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < SET_SILENT_REMINDER_ENABLE_MIN_PARA) { + ANS_LOGE("Wrong number of arguments."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, MANDATORY_PARAMETER_ARE_LEFT_UNSPECIFIED); + return nullptr; + } + + // argv[0]: bundleOption + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_object) { + ANS_LOGE("Parameter type error. Object expected."); + std::string msg = "Incorrect parameter types.The type of param must be object."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option); + if (retValue == nullptr) { + ANS_LOGE("GetBundleOption failed."); + Common::NapiThrow(env, ERROR_PARAM_INVALID, PARAMETER_VERIFICATION_FAILED); + return nullptr; + } + + if (argc > SET_SILENT_REMINDER_ENABLE_MIN_PARA) { + // argv[2]: enable + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + std::string msg = "Incorrect parameter types.The type of param must be boolean."; + Common::NapiThrow(env, ERROR_PARAM_INVALID, msg); + return nullptr; + } + napi_get_value_bool(env, argv[PARAM1], ¶ms.enabled); + } + + return Common::NapiGetNull(env); +} + +napi_value NapiSetSilentReminderEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGD("enter"); + SilentReminderEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackSilentReminderEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackSilentReminderEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "setSilentReminderEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("NapiSetSilentReminderEnabled work excute."); + AsyncCallbackSilentReminderEnable *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetSilentReminderEnabled( + asynccallbackinfo->params.option, asynccallbackinfo->params.enabled); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d", asynccallbackinfo->info.errorCode); + } + }, + AsyncCompleteCallbackNapiSetSilentReminderEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + + return promise; +} + +void AsyncCompleteCallbackNapiIsSilentReminderEnabled(napi_env env, napi_status status, void *data) +{ + ANS_LOGD("enter"); + if (!data) { + ANS_LOGE("Invalid async callback data"); + return; + } + ANS_LOGI("IsSmartReminderEnabled napi_create_async_work end"); + AsyncCallbackSilentReminderEnable *asynccallbackinfo = static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_create_int32(env, asynccallbackinfo->params.enableStatus, &result); + } + Common::CreateReturnValue(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + ANS_LOGD("Delete NapiIsSmartReminderEnabled callback reference."); + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } +} + +napi_value NapiIsSilentReminderEnabled(napi_env env, napi_callback_info info) +{ + ANS_LOGD("enter"); + SilentReminderEnableParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + Common::NapiThrow(env, ERROR_PARAM_INVALID); + return Common::NapiGetUndefined(env); + } + + AsyncCallbackSilentReminderEnable *asynccallbackinfo = + new (std::nothrow) AsyncCallbackSilentReminderEnable {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + Common::NapiThrow(env, ERROR_INTERNAL_ERROR); + return Common::JSParaError(env, nullptr); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, nullptr, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "isSmartReminderEnabled", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work(env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGD("NapiIsSmartReminderEnabled work excute."); + AsyncCallbackSilentReminderEnable *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::IsSilentReminderEnabled( + asynccallbackinfo->params.option, asynccallbackinfo->params.enableStatus); + ANS_LOGI("asynccallbackinfo->info.errorCode = %{public}d, enableStatus = %{public}d", + asynccallbackinfo->info.errorCode, asynccallbackinfo->params.enableStatus); + } + }, + AsyncCompleteCallbackNapiIsSilentReminderEnabled, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + napi_queue_async_work_with_qos(env, asynccallbackinfo->asyncWork, napi_qos_user_initiated); + return promise; +} +} +} \ No newline at end of file diff --git a/interfaces/inner_api/notification_constant.h b/interfaces/inner_api/notification_constant.h index 361bbc7ed98cbe4a0c6e410f2ee8be48ed09bc39..bf3f6e3eb969a136a16e6fb10205a21bb095153c 100644 --- a/interfaces/inner_api/notification_constant.h +++ b/interfaces/inner_api/notification_constant.h @@ -154,17 +154,22 @@ public: enum class ENABLE_STATUS { /** - * the device type notification switch never been set. + * the switch never been set, default status is false. */ - ENABLE_NONE, + DEFAULT_FALSE, /** - * the device type notification switch is enabled + * the switch never been set, default status is true. + */ + DEFAULT_TRUE, + + /** + * the switch is enabled */ ENABLE_TRUE, /** - * the device type notification switch is disable + * the switch is disable */ ENABLE_FALSE }; diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index 1f35fd1b0d3f0426e7c7d96646257a86870fe04a..2b7cba8ff3268058da9b50f48017d57d1c60f8b1 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -1177,6 +1177,28 @@ public: */ static ErrCode SetSmartReminderEnabled(const std::string &deviceType, const bool enabled); + /** + * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. + * The value true indicates that notifications are allowed, and the value + * false indicates that notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + static ErrCode IsSilentReminderEnabled(const NotificationBundleOption &bundleOption, int32_t &enableStatus); + + /** + * @brief Set Enable smartphone to collaborate with other devices for intelligent reminders + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. + * The value true indicates that notifications are allowed, and the value + * false indicates that notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + static ErrCode SetSilentReminderEnabled(const NotificationBundleOption &bundleOption, const bool enabled); + /** * @brief Set the channel switch for collaborative reminders. The caller must have system permissions to call this method. diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index a013b651f85fddb006e83042e79e16dc091260c1..0120dd0eb217237008144b5d9d80705c448ba313 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -101,6 +101,7 @@ ohos_source_set("ans_service_sources") { "src/distributed_manager/advanced_notification_distributed_manager_service.cpp", "src/disturb_manager/advanced_notification_disturb_manager_service.cpp", "src/enable_manager/enable_manager.cpp", + "src/enable_manager/silent_reminder_manager.cpp", "src/event_report.cpp", "src/liveview_all_scenarios_extension_wrapper.cpp", "src/notification_dialog.cpp", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index ffba5e81a72a14603c514e5c5759833715175253..4e143b977a70e7f7a307938e083cb0ca2db63f8d 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1060,6 +1060,30 @@ public: ErrCode IsDistributedEnabledByBundle(const sptr &bundleOption, const std::string &deviceType, bool &enabled) override; + /** + * @brief Sets whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that + * notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode SetSilentReminderEnabled(const sptr &bundleOption, const bool enabled) override; + + /* + * @brief Get whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that + * notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode IsSilentReminderEnabled(const sptr &bundleOption, int32_t &enableStatus) override; + /** * @brief configuring Whether to Synchronize Common Notifications to Target Devices. * @@ -1217,6 +1241,9 @@ public: void SetRequestBySlotType(const sptr &request, const sptr &bundleOption); + void HandleFlagsWithRequest(const sptr &request, + const sptr &bundleOption); + // Might fail if ces subscribe failed, if failed, dialogManager_ will be set nullptr bool CreateDialogManager(); @@ -1333,6 +1360,15 @@ public: void UpdateCloneBundleInfo(const NotificationCloneBundleInfo cloneBundleInfo); + void UpdateCloneBundleInfoForEnable( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle); + + void UpdateCloneBundleInfoFoSlot( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle); + + void UpdateCloneBundleInfoFoSilentReminder( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle); + void TryStartReminderAgentService(); static sptr GenerateBundleOption(); diff --git a/services/ans/include/notification_analytics_util.h b/services/ans/include/notification_analytics_util.h index 5da7a402152f1b66bfecb79bbe19f52b018b4e2e..fd05b8f2d1b02eebcbec17e7eb7340e57bcb7be9 100644 --- a/services/ans/include/notification_analytics_util.h +++ b/services/ans/include/notification_analytics_util.h @@ -57,6 +57,7 @@ enum EventSceneId { SCENE_22 = 22, SCENE_23 = 23, SCENE_24 = 24, + SCENE_25 = 25, }; enum EventBranchId { diff --git a/services/ans/include/notification_clone_bundle_info.h b/services/ans/include/notification_clone_bundle_info.h index 99a2af2c4ee798b66d8a14cce6b30f61439187bc..0bb5ac328bedae3a01af53230b1f8311e7b52719 100644 --- a/services/ans/include/notification_clone_bundle_info.h +++ b/services/ans/include/notification_clone_bundle_info.h @@ -22,6 +22,7 @@ #include "notification_constant.h" #include "notification_bundle_option.h" #include "notification_do_not_disturb_profile.h" +#include "notification_constant.h" namespace OHOS { namespace Notification { @@ -55,6 +56,9 @@ public: void SetEnableNotification(const bool &enable); bool GetEnableNotification() const; + void SetSilentReminderEnabled(const NotificationConstant::ENABLE_STATUS &silentReminderEnabled); + NotificationConstant::ENABLE_STATUS GetSilentReminderEnabled() const; + void AddSlotInfo(const SlotInfo &slotInfo); std::vector GetSlotInfo() const; @@ -71,6 +75,7 @@ private: bool isShowBadge_ = false; bool isEnabledNotification_ = false; std::vector slotsInfo_; + NotificationConstant::ENABLE_STATUS silentReminderEnabled_; }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/notification_preferences.h b/services/ans/include/notification_preferences.h index 2e5f02138ae857cb0b6fa0218a865d9c95d5c63f..783f66a13ed1271b9506a4ad7c8d8e3da62a7b4e 100644 --- a/services/ans/include/notification_preferences.h +++ b/services/ans/include/notification_preferences.h @@ -320,6 +320,31 @@ public: ErrCode SetDistributedEnabledByBundle(const sptr &bundleOption, const std::string &deviceType, const bool enabled); + /** + * @brief Sets whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that + * notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode SetSilentReminderEnabled(const sptr &bundleOption, const bool enabled); + + /** + * @brief Get whether to allow a specified application to publish notifications cross + * device collaboration. The caller must have system permissions to call this method. + * + * @param bundleOption Indicates the bundle name and uid of the application. + * @param enabled Specifies whether to allow the given application to publish notifications. The value + * true indicates that notifications are allowed, and the value false indicates that + * notifications are not allowed. + * @return Returns set notifications enabled for specified bundle result. + */ + ErrCode IsSilentReminderEnabled( + const sptr &bundleOption, NotificationConstant::ENABLE_STATUS &enableStatus); + /** * @brief Get Enable smartphone to collaborate with other devices for intelligent reminders * @@ -443,6 +468,7 @@ public: void RemoveSettings(int32_t userId); void RemoveAnsBundleDbInfo(const sptr &bundleOption); void RemoveEnabledDbByBundle(const sptr &bundleOption); + void RemoveSilentEnabledDbByBundle(const sptr &bundleOption); int32_t SetKvToDb(const std::string &key, const std::string &value, const int32_t &userId); int32_t SetByteToDb(const std::string &key, const std::vector &value, const int32_t &userId); int32_t GetKvFromDb(const std::string &key, std::string &value, const int32_t &userId); diff --git a/services/ans/include/notification_preferences_database.h b/services/ans/include/notification_preferences_database.h index 92ed70479c648eed1fc76eee88b95c5287de43e4..42cf20fd132cd8214cadc369d1d7c48e22d2473f 100644 --- a/services/ans/include/notification_preferences_database.h +++ b/services/ans/include/notification_preferences_database.h @@ -110,6 +110,26 @@ public: bool GetDistributedEnabledForBundle(const std::string deviceType, const NotificationPreferencesInfo::BundleInfo &bundleInfo, bool &enabled); + /** + * @brief Put distributed enable notification in the of bundle into disturbe DB. + * + * @param bundleInfo Indicates bundle info. + * @param enabled Indicates to whether to enabled + * @return Return true on success, false on failure. + */ + bool SetSilentReminderEnabled( + const NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo); + + /** + * @brief Get distributed enable notification in the of bundle into disturbe DB. + * + * @param bundleInfo Indicates bundle info. + * @param enabled Indicates to whether to enabled + * @return Return true on success, false on failure. + */ + bool IsSilentReminderEnabled( + NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo); + /** * @brief Put distributed enable notification in the of bundle into disturbe DB. * @@ -294,6 +314,7 @@ public: bool RemoveNotificationEnable(const int32_t userId); bool RemoveDoNotDisturbDate(const int32_t userId); bool RemoveAnsBundleDbInfo(std::string bundleName, int32_t uid); + bool RemoveSilentEnabledDbByBundle(std::string bundleName, int32_t uid); bool AddDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); bool RemoveDoNotDisturbProfiles( int32_t userId, const std::vector> &profiles); @@ -406,12 +427,15 @@ private: int64_t StringToInt64(const std::string &str) const; void StringSplit(const std::string content, char delim, std::vector& result) const; bool IsSlotKey(const std::string &bundleKey, const std::string &key) const; + bool IsSilentReminderKey(const std::string &bundleKey, const std::string &key) const; std::string GenerateSlotKey( const std::string &bundleKey, const std::string &type = "", const std::string &subType = "") const; std::string GenerateBundleKey(const std::string &bundleKey, const std::string &type = "") const; void ParseSlotFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair &entry, const int32_t &userId); + void ParseSilentReminderFromDisturbeDB(NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo, + const std::pair &entry); void ParseBundlePropertyFromDisturbeDB(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair &entry); void ParseBundleName(NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &value) const; @@ -453,6 +477,8 @@ private: const std::string &deviceType, const int32_t userId) const; std::string GenerateBundleLablel( const std::string &deviceType, const std::string &deviceId, const int32_t userId) const; + std::string GenerateSilentReminderKey( + const NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo) const; void GetDoNotDisturbType(NotificationPreferencesInfo &info, int32_t userId); void GetDoNotDisturbBeginDate(NotificationPreferencesInfo &info, int32_t userId); void GetDoNotDisturbEndDate(NotificationPreferencesInfo &info, int32_t userId); diff --git a/services/ans/include/notification_preferences_info.h b/services/ans/include/notification_preferences_info.h index ce9c93cc446924a10d9f2b9a3824b898f69d78d2..3307c3cc450e7472e593f650b884375514a24eb2 100644 --- a/services/ans/include/notification_preferences_info.h +++ b/services/ans/include/notification_preferences_info.h @@ -27,11 +27,18 @@ #include "advanced_notification_service.h" #include "notification_clone_bundle_info.h" #include "notification_disable.h" +#include "notification_constant.h" namespace OHOS { namespace Notification { class NotificationPreferencesInfo final { public: + + struct SilentReminderInfo { + std::string bundleName; + int32_t uid; + NotificationConstant::ENABLE_STATUS enableStatus {NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE}; + }; class BundleInfo final { public: BundleInfo(); @@ -259,6 +266,20 @@ public: */ bool GetBundleInfo(const sptr &bundleOption, BundleInfo &info) const; + /** + * set silent reminder info into preferences info. + * @param info Indicates the bundle. + */ + void SetSilentReminderInfo(SilentReminderInfo &info); + + /** + * get silent reminder info from preferences info. + * @param bundleOption Indicates the bundle info label. + * @param info Indicates the silent reminder info. + * @return Whether to get silent reminder info success. + */ + bool GetSilentReminderInfo(const sptr &bundleOption, SilentReminderInfo &info) const; + /** * remove bundle info from preferences info. * @param bundleOption Indicates the bundle info label. @@ -313,6 +334,7 @@ public: void RemoveNotificationEnable(const int32_t userId); void RemoveDoNotDisturbDate(const int32_t userId); void SetBundleInfoFromDb(BundleInfo &info, std::string bundleKey); + void SetSilentReminderInfoFromDb(SilentReminderInfo &silentReminderInfo, std::string bundleKey); std::string MakeDoNotDisturbProfileKey(int32_t userId, int64_t profileId); void AddDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); void RemoveDoNotDisturbProfiles(int32_t userId, const std::vector> &profiles); @@ -333,6 +355,7 @@ private: std::map> doNotDisturbProfiles_; std::map infos_; std::vector kioskAppTrustList_; + std::unordered_map silentReminderInfos_; struct DisableNotificationInfo { int32_t disabled = -1; diff --git a/services/ans/src/advanced_notification_slot_service.cpp b/services/ans/src/advanced_notification_slot_service.cpp index 7e4ad9b4954f645a5c803c97c85a8c95952f2f5d..b67ff61d53811ebc2347ebaac34a90e32389dd2f 100644 --- a/services/ans/src/advanced_notification_slot_service.cpp +++ b/services/ans/src/advanced_notification_slot_service.cpp @@ -131,6 +131,7 @@ ErrCode AdvancedNotificationService::GetSlotsByBundle( { ANS_LOGD("called"); + std::vector> slots_temp; bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { ANS_LOGD("IsSystemApp is false."); @@ -159,6 +160,17 @@ ErrCode AdvancedNotificationService::GetSlotsByBundle( result = ERR_OK; slots.clear(); } + NotificationConstant::ENABLE_STATUS enableStatus = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; + result = NotificationPreferences::GetInstance()->IsSilentReminderEnabled(bundleOption, enableStatus); + if (enableStatus == NotificationConstant::ENABLE_STATUS::ENABLE_TRUE) { + for (auto slot : slots) { + sptr value(new NotificationSlot(*slot)); + value->SetReminderMode(1 << 5); + slots_temp.emplace_back(value); + ANS_LOGD("GetSlotsByBundle ReminderMode:%{public}d", slot->GetReminderMode()); + } + slots = slots_temp; + } })); notificationSvrQueue_->wait(handler); @@ -670,12 +682,31 @@ void AdvancedNotificationService::SetRequestBySlotType(const sptrSetFlags(flags); + HandleFlagsWithRequest(request, bundleOption); +} + +void AdvancedNotificationService::HandleFlagsWithRequest(const sptr &request, + const sptr &bundleOption) +{ + auto flags = request->GetFlags(); + NotificationConstant::ENABLE_STATUS enableStatus = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; if (request->IsCommonLiveView()) { LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewReminderFlags(request); LIVEVIEW_ALL_SCENARIOS_EXTENTION_WRAPPER->UpdateLiveviewVoiceContent(request); + } else if (!request->IsSystemLiveView()) { + NotificationPreferences::GetInstance()->IsSilentReminderEnabled(bundleOption, enableStatus); + if (enableStatus == NotificationConstant::ENABLE_STATUS::ENABLE_TRUE) { + flags->SetSoundEnabled(NotificationConstant::FlagStatus::CLOSE); + flags->SetLockScreenVisblenessEnabled(false); + flags->SetBannerEnabled(false); + flags->SetLightScreenEnabled(false); + flags->SetVibrationEnabled(NotificationConstant::FlagStatus::CLOSE); + request->SetFlags(flags); + } } - ANS_LOGI("notificationKey = %{public}s flags = %{public}d", - request->GetKey().c_str(), flags->GetReminderFlags()); + ANS_LOGI("Key = %{public}s flags = %{public}d class = %{public}s silent = %{public}d", + request->GetKey().c_str(), flags->GetReminderFlags(), + request->GetClassification().c_str(), enableStatus); if (request->GetClassification() == NotificationConstant::ANS_VOIP && request->GetSlotType() == NotificationConstant::LIVE_VIEW) { return; @@ -683,7 +714,6 @@ void AdvancedNotificationService::SetRequestBySlotType(const sptr::GetInstance()->ReminderDecisionProcess(request); #endif - ANS_LOGI("classification:%{public}s", request->GetClassification().c_str()); } ErrCode AdvancedNotificationService::GetSlotByType(int32_t slotTypeInt, sptr &slot) diff --git a/services/ans/src/advanced_notification_utils.cpp b/services/ans/src/advanced_notification_utils.cpp index 20d653d6ad3704f70b1045864a1159ce594ab8ed..66566548c1635971a6d3810d78143bfec0a6ade0 100644 --- a/services/ans/src/advanced_notification_utils.cpp +++ b/services/ans/src/advanced_notification_utils.cpp @@ -518,6 +518,7 @@ void AdvancedNotificationService::OnBundleRemoved(const sptrRemoveEnabledDbByBundle(bundleOption); + NotificationPreferences::GetInstance()->RemoveSilentEnabledDbByBundle(bundleOption); #ifdef ENABLE_ANS_AGGREGATION EXTENTION_WRAPPER->UpdateByBundle(bundleOption->GetBundleName(), NotificationConstant::PACKAGE_REMOVE_REASON_DELETE); @@ -1938,32 +1939,8 @@ void AdvancedNotificationService::UpdateCloneBundleInfo(const NotificationCloneB return; } bundle->SetAppIndex(cloneBundleInfo.GetAppIndex()); - if (NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, - cloneBundleInfo.GetEnableNotification()) == ERR_OK) { - SetSlotFlagsTrustlistsAsBundle(bundle); - sptr bundleData = new (std::nothrow) EnabledNotificationCallbackData( - bundle->GetBundleName(), bundle->GetUid(), cloneBundleInfo.GetEnableNotification()); - if (bundleData == nullptr) { - return; - } - NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); - } else { - ANS_LOGW("Set notification enable failed."); - return; - } - - if (cloneBundleInfo.GetSlotInfo().empty()) { - PublishSlotChangeCommonEvent(bundle); - } - if (NotificationPreferences::GetInstance()->SetNotificationSlotFlagsForBundle(bundle, - cloneBundleInfo.GetSlotFlags()) != ERR_OK) { - ANS_LOGW("Set notification slot failed."); - return; - } - if (UpdateSlotReminderModeBySlotFlags(bundle, cloneBundleInfo.GetSlotFlags()) != ERR_OK) { - ANS_LOGW("Set notification reminder slot failed."); - return; - } + UpdateCloneBundleInfoForEnable(cloneBundleInfo, bundle); + UpdateCloneBundleInfoFoSlot(cloneBundleInfo, bundle); if (NotificationPreferences::GetInstance()->SetShowBadge(bundle, cloneBundleInfo.GetIsShowBadge()) == ERR_OK) { HandleBadgeEnabledChanged(bundle, cloneBundleInfo.GetIsShowBadge()); @@ -1971,14 +1948,63 @@ void AdvancedNotificationService::UpdateCloneBundleInfo(const NotificationCloneB ANS_LOGW("Set notification badge failed."); } - for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) { - if (SetEnabledForBundleSlotInner(bundle, bundle, cloneSlot.slotType_, cloneSlot.enable_, - cloneSlot.isForceControl_) != ERR_OK) { - ANS_LOGW("Set notification slots failed %{public}s.", cloneSlot.Dump().c_str()); - } - } + UpdateCloneBundleInfoFoSilentReminder(cloneBundleInfo, bundle); })); } +void AdvancedNotificationService::UpdateCloneBundleInfoForEnable( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle) +{ + if (NotificationPreferences::GetInstance()->SetNotificationsEnabledForBundle(bundle, + cloneBundleInfo.GetEnableNotification()) == ERR_OK) { + SetSlotFlagsTrustlistsAsBundle(bundle); + sptr bundleData = new (std::nothrow) EnabledNotificationCallbackData( + bundle->GetBundleName(), bundle->GetUid(), cloneBundleInfo.GetEnableNotification()); + if (bundleData == nullptr) { + return; + } + NotificationSubscriberManager::GetInstance()->NotifyEnabledNotificationChanged(bundleData); + } else { + ANS_LOGW("Set notification enable failed."); + return; + } +} + +void AdvancedNotificationService::UpdateCloneBundleInfoFoSlot( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle) +{ + if (cloneBundleInfo.GetSlotInfo().empty()) { + PublishSlotChangeCommonEvent(bundle); + } + if (NotificationPreferences::GetInstance()->SetNotificationSlotFlagsForBundle(bundle, + cloneBundleInfo.GetSlotFlags()) != ERR_OK) { + ANS_LOGW("Set notification slot failed."); + return; + } + if (UpdateSlotReminderModeBySlotFlags(bundle, cloneBundleInfo.GetSlotFlags()) != ERR_OK) { + ANS_LOGW("Set notification reminder slot failed."); + return; + } + + for (auto& cloneSlot : cloneBundleInfo.GetSlotInfo()) { + if (SetEnabledForBundleSlotInner(bundle, bundle, cloneSlot.slotType_, cloneSlot.enable_, + cloneSlot.isForceControl_) != ERR_OK) { + ANS_LOGW("Set notification slots failed %{public}s.", cloneSlot.Dump().c_str()); + } + } +} + +void AdvancedNotificationService::UpdateCloneBundleInfoFoSilentReminder( + const NotificationCloneBundleInfo cloneBundleInfo, const sptr bundle) +{ + auto enableStatus = cloneBundleInfo.GetSilentReminderEnabled(); + if (enableStatus != NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE && + enableStatus != NotificationConstant::ENABLE_STATUS::DEFAULT_TRUE) { + if (NotificationPreferences::GetInstance()->SetSilentReminderEnabled(bundle, + enableStatus == NotificationConstant::ENABLE_STATUS::ENABLE_TRUE ? true : false) != ERR_OK) { + ANS_LOGW("SetSilentReminderEnabled failed."); + } + } +} } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/clone/notification_clone_bundle_info.cpp b/services/ans/src/clone/notification_clone_bundle_info.cpp index 3f36a2a0ae14bbdc01ed06bfaaed6c30a15680d3..5e69207be383894264230e1c49c0958bddcc83e1 100644 --- a/services/ans/src/clone/notification_clone_bundle_info.cpp +++ b/services/ans/src/clone/notification_clone_bundle_info.cpp @@ -30,6 +30,7 @@ constexpr const char *BUNDLE_INFO_SLOT_LIST = "slotList"; constexpr const char *BUNDLE_INFO_SLOT_TYPE = "slotType"; constexpr const char *BUNDLE_INFO_SLOT_ENABLE = "slotEnable"; constexpr const char *BUNDLE_INFO_SLOT_CONTROL = "slotControl"; +constexpr const char *BUNDLE_INFO_SILENT_REMINDER = "enabledSilentReminder"; constexpr int32_t CONST_ENABLE_INT = 1; } void NotificationCloneBundleInfo::SetBundleName(const std::string &name) @@ -92,6 +93,17 @@ bool NotificationCloneBundleInfo::GetEnableNotification() const return isEnabledNotification_; } +void NotificationCloneBundleInfo::SetSilentReminderEnabled( + const NotificationConstant::ENABLE_STATUS &silentReminderEnabled) +{ + silentReminderEnabled_ = silentReminderEnabled; +} + +NotificationConstant::ENABLE_STATUS NotificationCloneBundleInfo::GetSilentReminderEnabled() const +{ + return silentReminderEnabled_; +} + void NotificationCloneBundleInfo::AddSlotInfo(const SlotInfo &slotInfo) { for (auto& item : slotsInfo_) { @@ -128,6 +140,7 @@ void NotificationCloneBundleInfo::ToJson(nlohmann::json &jsonObject) const jsonObject[BUNDLE_INFO_SLOT_FLAGS] = slotFlags_; jsonObject[BUNDLE_INFO_SHOW_BADGE] = isShowBadge_ ? 1 : 0; jsonObject[BUNDLE_INFO_ENABLE_NOTIFICATION] = isEnabledNotification_ ? 1 : 0; + jsonObject[BUNDLE_INFO_SILENT_REMINDER] = static_cast(silentReminderEnabled_); } void NotificationCloneBundleInfo::SlotsFromJson(const nlohmann::json &jsonObject) @@ -183,6 +196,10 @@ void NotificationCloneBundleInfo::FromJson(const nlohmann::json &jsonObject) int32_t enabledNotification = jsonObject.at(BUNDLE_INFO_ENABLE_NOTIFICATION).get(); isEnabledNotification_ = (enabledNotification == CONST_ENABLE_INT); } + if (jsonObject.contains(BUNDLE_INFO_SILENT_REMINDER) && jsonObject[BUNDLE_INFO_SILENT_REMINDER].is_number()) { + int32_t silentReminderEnabled = jsonObject.at(BUNDLE_INFO_SILENT_REMINDER).get(); + silentReminderEnabled_ = static_cast(silentReminderEnabled); + } SlotsFromJson(jsonObject); } std::string NotificationCloneBundleInfo::SlotInfo::Dump() const @@ -206,6 +223,7 @@ std::string NotificationCloneBundleInfo::Dump() const ", ShowBadge = " + std::to_string(isShowBadge_) + ", isEnabled = " + std::to_string(isEnabledNotification_) + ", slotsInfo = " + slotDump + + ", silentReminderEnabled = " + std::to_string(static_cast(silentReminderEnabled_)) + " }"; } } diff --git a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp index d56db12361a6c93da21161c2704fff5c89743ab0..8ba56696004302dfaf992bde8d170cc5ea8708d8 100644 --- a/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp +++ b/services/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp @@ -95,7 +95,7 @@ ErrCode AdvancedNotificationService::SetDistributedEnabledBySlot( ErrCode result = NotificationPreferences::GetInstance()->SetDistributedEnabledBySlot(slotType, deviceType, enabled); if (result == ERR_OK && slotType == NotificationConstant::SlotType::LIVE_VIEW) { - NotificationConstant::ENABLE_STATUS notification = NotificationConstant::ENABLE_STATUS::ENABLE_NONE; + NotificationConstant::ENABLE_STATUS notification = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; if (NotificationPreferences::GetInstance()->IsDistributedEnabled(deviceType, notification) != ERR_OK) { ANS_LOGW("Get notification distributed failed %{public}s!", deviceType.c_str()); diff --git a/services/ans/src/enable_manager/silent_reminder_manager.cpp b/services/ans/src/enable_manager/silent_reminder_manager.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7e969abc48eddeac21487758a1ee3a60e1336028 --- /dev/null +++ b/services/ans/src/enable_manager/silent_reminder_manager.cpp @@ -0,0 +1,126 @@ +/* + * 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 "advanced_notification_service.h" + +#include "accesstoken_kit.h" +#include "access_token_helper.h" +#include "ans_const_define.h" +#include "ans_inner_errors.h" +#include "ans_log_wrapper.h" +#include "ans_permission_def.h" + +#include "bundle_manager_helper.h" +#include "ipc_skeleton.h" + +#include "notification_preferences.h" +#include "notification_bundle_option.h" +#include "notification_analytics_util.h" +#include "os_account_manager_helper.h" +#include "notification_extension_wrapper.h" + +namespace OHOS { +namespace Notification { + +ErrCode AdvancedNotificationService::SetSilentReminderEnabled(const sptr &bundleOption, + const bool enabled) +{ + HaMetaMessage message = HaMetaMessage(EventSceneId::SCENE_25, EventBranchId::BRANCH_0); + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr) { + ANS_LOGE("BundleOption is null."); + NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE)); + return ERR_ANS_INVALID_BUNDLE; + } + + message.Message(bundleOption->GetBundleName() + "_" + std::to_string(bundleOption->GetUid()) + + " silentReminderEnabled:" + std::to_string(enabled)); + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGE("IsSystemApp is false."); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("Permission Denied."); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + ANS_LOGE("bundle is nullptr"); + NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(ERR_ANS_INVALID_BUNDLE)); + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + result = NotificationPreferences::GetInstance()->SetSilentReminderEnabled(bundle, enabled); + })); + notificationSvrQueue_->wait(handler); + ANS_LOGI("%{public}s_%{public}d, enabled: %{public}s, " + "SetSilentReminderEnabled result: %{public}d", bundleOption->GetBundleName().c_str(), + bundleOption->GetUid(), std::to_string(enabled).c_str(), result); + NotificationAnalyticsUtil::ReportModifyEvent(message.ErrorCode(result).BranchId(BRANCH_3)); + + return result; +} + +ErrCode AdvancedNotificationService::IsSilentReminderEnabled(const sptr &bundleOption, + int32_t &enableStatusInt) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr) { + ANS_LOGE("BundleOption is null."); + return ERR_ANS_INVALID_BUNDLE; + } + + NotificationConstant::ENABLE_STATUS enableStatus; + bool isSubsystem = AccessTokenHelper::VerifyNativeToken(IPCSkeleton::GetCallingTokenID()); + if (!isSubsystem && !AccessTokenHelper::IsSystemApp()) { + ANS_LOGD("IsSystemApp is bogus."); + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!AccessTokenHelper::CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + ANS_LOGE("no permission"); + return ERR_ANS_PERMISSION_DENIED; + } + + sptr bundle = GenerateValidBundleOption(bundleOption); + if (bundle == nullptr) { + return ERR_ANS_INVALID_BUNDLE; + } + + if (notificationSvrQueue_ == nullptr) { + ANS_LOGE("Serial queue is invalidity."); + return ERR_ANS_INVALID_PARAM; + } + + ErrCode result = ERR_OK; + ffrt::task_handle handler = notificationSvrQueue_->submit_h(std::bind([&]() { + result = NotificationPreferences::GetInstance()->IsSilentReminderEnabled(bundle, enableStatus); + enableStatusInt = static_cast(enableStatus); + })); + notificationSvrQueue_->wait(handler); + return result; +} + +} // Notification +} // OHOS \ No newline at end of file diff --git a/services/ans/src/notification_preferences.cpp b/services/ans/src/notification_preferences.cpp index 9f1a35a7bb580d83e18f1c043170a5e6267a8048..d23d6d39738f654b9ef34c60e0b0c0ad1178ebb8 100644 --- a/services/ans/src/notification_preferences.cpp +++ b/services/ans/src/notification_preferences.cpp @@ -1100,6 +1100,62 @@ ErrCode NotificationPreferences::IsDistributedEnabledByBundle(const sptr &bundleOption, + const bool enabled) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo::SilentReminderInfo silentReminderInfo; + silentReminderInfo.bundleName = bundleOption->GetBundleName(); + silentReminderInfo.uid = bundleOption->GetUid(); + silentReminderInfo.enableStatus = + enabled ? NotificationConstant::ENABLE_STATUS::ENABLE_TRUE : NotificationConstant::ENABLE_STATUS::ENABLE_FALSE; + bool storeDBResult = true; + storeDBResult = preferncesDB_->SetSilentReminderEnabled(silentReminderInfo); + if (storeDBResult) { + preferencesInfo_.SetSilentReminderInfo(silentReminderInfo); + } + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +ErrCode NotificationPreferences::IsSilentReminderEnabled(const sptr &bundleOption, + NotificationConstant::ENABLE_STATUS &enableStatus) +{ + ANS_LOGD("%{public}s", __FUNCTION__); + if (bundleOption == nullptr || bundleOption->GetBundleName().empty()) { + return ERR_ANS_INVALID_PARAM; + } + + std::lock_guard lock(preferenceMutex_); + NotificationPreferencesInfo::SilentReminderInfo silentReminderInfo; + if (preferencesInfo_.GetSilentReminderInfo(bundleOption, silentReminderInfo)) { + enableStatus = silentReminderInfo.enableStatus; + return ERR_OK; + } + silentReminderInfo.bundleName = bundleOption->GetBundleName(); + silentReminderInfo.uid = bundleOption->GetUid(); + bool storeDBResult = true; + storeDBResult = preferncesDB_->IsSilentReminderEnabled(silentReminderInfo); + if (storeDBResult) { + enableStatus = silentReminderInfo.enableStatus; + preferencesInfo_.SetSilentReminderInfo(silentReminderInfo); + } + return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; +} + +void NotificationPreferences::RemoveSilentEnabledDbByBundle(const sptr &bundleOption) +{ + ANS_LOGE("%{public}s", __FUNCTION__); + if (preferncesDB_ != nullptr && bundleOption != nullptr) { + std::lock_guard lock(preferenceMutex_); + preferncesDB_->RemoveSilentEnabledDbByBundle(bundleOption->GetBundleName(), bundleOption->GetUid()); + } +} + ErrCode NotificationPreferences::SetDistributedEnabled( const std::string &deviceType, const NotificationConstant::ENABLE_STATUS &enableStatus) { @@ -1116,7 +1172,7 @@ ErrCode NotificationPreferences::IsDistributedEnabled( ANS_LOGD("%{public}s", __FUNCTION__); std::lock_guard lock(preferenceMutex_); bool storeDBResult = true; - enableStatus = NotificationConstant::ENABLE_STATUS::ENABLE_NONE; + enableStatus = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; storeDBResult = preferncesDB_->GetDistributedEnabled(deviceType, enableStatus); return storeDBResult ? ERR_OK : ERR_ANS_PREFERENCES_NOTIFICATION_DB_OPERATION_FAILED; } diff --git a/services/ans/src/notification_preferences_database.cpp b/services/ans/src/notification_preferences_database.cpp index 1ee9e1ce560c0ddd00159a41bc70ac7758304005..07fb36696c67177b010e3593b0e79adecc76efde 100644 --- a/services/ans/src/notification_preferences_database.cpp +++ b/services/ans/src/notification_preferences_database.cpp @@ -112,6 +112,11 @@ const static std::string KEY_ENABLE_BUNDLE_DISTRIBUTED_NOTIFICATION = "enabledDi */ const static std::string KEY_SMART_REMINDER_ENABLE_NOTIFICATION = "enabledSmartReminder"; +/** + * Indicates that disturbe key which bundle enable notification. + */ +const static std::string KEY_SILENT_REMINDER_ENABLE_NOTIFICATION = "enabledSilentReminder"; + /** * Indicates that disturbe key which bundle enable notification. */ @@ -1151,11 +1156,14 @@ void NotificationPreferencesDatabase::ParseBundleFromDistureDB(NotificationPrefe rdbDataManager_->QueryDataBeginWithKey((GenerateBundleKey(bundleKey)), bundleEntries, userId); ANS_LOGD("Bundle key is %{public}s.", GenerateBundleKey(bundleKey).c_str()); NotificationPreferencesInfo::BundleInfo bunldeInfo; + NotificationPreferencesInfo::SilentReminderInfo silentReminderInfo; std::string keyStr = GenerateBundleKey(bundleKey, KEY_BUNDLE_SHOW_BADGE); bool badgeEnableExist = false; for (auto bundleEntry : bundleEntries) { if (IsSlotKey(GenerateBundleKey(bundleKey), bundleEntry.first)) { ParseSlotFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry, userId); + } else if (IsSilentReminderKey(GenerateBundleKey(bundleKey), bundleEntry.first)) { + ParseSilentReminderFromDisturbeDB(silentReminderInfo, bundleEntry); } else { ParseBundlePropertyFromDisturbeDB(bunldeInfo, bundleKey, bundleEntry); } @@ -1170,6 +1178,7 @@ void NotificationPreferencesDatabase::ParseBundleFromDistureDB(NotificationPrefe } info.SetBundleInfoFromDb(bunldeInfo, bundleKey); + info.SetSilentReminderInfoFromDb(silentReminderInfo, bundleKey); } } @@ -1192,6 +1201,15 @@ void NotificationPreferencesDatabase::ParseSlotFromDisturbeDB(NotificationPrefer bundleInfo.SetSlot(slot); } +void NotificationPreferencesDatabase::ParseSilentReminderFromDisturbeDB( + NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo, + const std::pair &entry) +{ + bool enable = static_cast(StringToInt(entry.second)); + silentReminderInfo.enableStatus = + enable ? NotificationConstant::ENABLE_STATUS::ENABLE_TRUE : NotificationConstant::ENABLE_STATUS::ENABLE_FALSE; +} + void NotificationPreferencesDatabase::ParseBundlePropertyFromDisturbeDB( NotificationPreferencesInfo::BundleInfo &bundleInfo, const std::string &bundleKey, const std::pair &entry) @@ -1361,6 +1379,16 @@ bool NotificationPreferencesDatabase::IsSlotKey(const std::string &bundleKey, co return false; } +bool NotificationPreferencesDatabase::IsSilentReminderKey(const std::string &bundleKey, const std::string &key) const +{ + std::string tempStr = FindLastString(bundleKey, key); + size_t pos = tempStr.find_first_of(KEY_UNDER_LINE); + if (!tempStr.compare(KEY_SILENT_REMINDER_ENABLE_NOTIFICATION)) { + return true; + } + return false; +} + std::string NotificationPreferencesDatabase::GenerateSlotKey( const std::string &bundleKey, const std::string &type, const std::string &subType) const { @@ -1383,6 +1411,14 @@ std::string NotificationPreferencesDatabase::GenerateSlotKey( return key; } +std::string NotificationPreferencesDatabase::GenerateSilentReminderKey( + const NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo) const +{ + std::string bundleKey = std::string().append(silentReminderInfo.bundleName) + .append(std::to_string(silentReminderInfo.uid)); + return GenerateBundleKey(bundleKey, KEY_SILENT_REMINDER_ENABLE_NOTIFICATION); +} + std::string NotificationPreferencesDatabase::GenerateBundleKey( const std::string &bundleKey, const std::string &type) const { @@ -1802,6 +1838,28 @@ bool NotificationPreferencesDatabase::RemoveAnsBundleDbInfo(std::string bundleNa return true; } +bool NotificationPreferencesDatabase::RemoveSilentEnabledDbByBundle(std::string bundleName, int32_t uid) +{ + if (!CheckRdbStore()) { + ANS_LOGE("RdbStore is nullptr."); + return false; + } + + // return std::string(KEY_SILENT_REMINDER_ENABLE_NOTIFICATION).append( + // bundleInfo.GetBundleName()).append(std::to_string(bundleInfo.GetBundleUid())); + std::string key = GenerateSilentReminderKey({bundleName, uid, NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE}); + int32_t userId = -1; + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(uid, userId); + int32_t result = rdbDataManager_->DeleteData(key, userId); + if (result != NativeRdb::E_OK) { + ANS_LOGE("Delete Silent db info failed, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid); + return false; + } + + ANS_LOGE("Remove Silent db info, bundle[%{public}s:%{public}d]", bundleName.c_str(), uid); + return true; +} + bool NotificationPreferencesDatabase::RemoveEnabledDbByBundleName(std::string bundleName, const int32_t &bundleUid) { if (!CheckRdbStore()) { @@ -2133,7 +2191,7 @@ bool NotificationPreferencesDatabase::GetDistributedEnabled( std::string key = std::string(KEY_DISTRIBUTED_NOTIFICATION_SWITCH).append(KEY_MIDDLE_LINE).append( deviceType).append(KEY_MIDDLE_LINE).append(std::to_string(userId)); bool result = false; - enabled = NotificationConstant::ENABLE_STATUS::ENABLE_NONE; + enabled = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) { switch (status) { case NativeRdb::E_EMPTY_VALUES_BUCKET: { @@ -2180,6 +2238,62 @@ ErrCode NotificationPreferencesDatabase::GetDistributedAuthStatus( return result; } +bool NotificationPreferencesDatabase::SetSilentReminderEnabled( + const NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo) +{ + if (silentReminderInfo.bundleName.empty()) { + ANS_LOGE("Bundle name is null."); + return false; + } + int32_t userId = -1; + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(silentReminderInfo.uid, userId); + + std::string key = GenerateSilentReminderKey(silentReminderInfo); + bool enableStatus = false; + if (silentReminderInfo.enableStatus == NotificationConstant::ENABLE_STATUS::ENABLE_TRUE) { + enableStatus = true; + } + int32_t result = PutDataToDB(key, static_cast(enableStatus), userId); + ANS_LOGI("SilentReminder result:%{public}d, key:%{public}s, enableStatus:%{public}d", + result, key.c_str(), silentReminderInfo.enableStatus); + return (result == NativeRdb::E_OK); +} + +bool NotificationPreferencesDatabase::IsSilentReminderEnabled( + NotificationPreferencesInfo::SilentReminderInfo &silentReminderInfo) +{ + if (silentReminderInfo.bundleName.empty()) { + ANS_LOGE("Bundle name is null."); + return false; + } + + std::string key = GenerateSilentReminderKey(silentReminderInfo); + bool result = false; + int32_t userId = -1; + OsAccountManagerHelper::GetInstance().GetOsAccountLocalIdFromUid(silentReminderInfo.uid, userId); + GetValueFromDisturbeDB(key, userId, [&](const int32_t &status, std::string &value) { + switch (status) { + case NativeRdb::E_EMPTY_VALUES_BUCKET: { + result = true; + silentReminderInfo.enableStatus = NotificationConstant::ENABLE_STATUS::DEFAULT_FALSE; + break; + } + case NativeRdb::E_OK: { + result = true; + NotificationConstant::ENABLE_STATUS enableStatus = + StringToInt(value) ? NotificationConstant::ENABLE_STATUS::ENABLE_TRUE : + NotificationConstant::ENABLE_STATUS::ENABLE_FALSE; + silentReminderInfo.enableStatus = enableStatus; + break; + } + default: + result = false; + break; + } + }); + return result; +} + ErrCode NotificationPreferencesDatabase::SetDistributedAuthStatus( const std::string &deviceType, const std::string &deviceId, int32_t userId, bool isAuth) { diff --git a/services/ans/src/notification_preferences_info.cpp b/services/ans/src/notification_preferences_info.cpp index e3efa260396794aec00124cb59fd66477f3feea9..7797677038f4c532f4e5658bd5affa9422986fbc 100644 --- a/services/ans/src/notification_preferences_info.cpp +++ b/services/ans/src/notification_preferences_info.cpp @@ -241,6 +241,24 @@ bool NotificationPreferencesInfo::GetBundleInfo( return false; } +void NotificationPreferencesInfo::SetSilentReminderInfo(SilentReminderInfo &info) +{ + std::string bundleKey = info.bundleName.append(std::to_string(info.uid)); + silentReminderInfos_.insert_or_assign(bundleKey, info); +} + +bool NotificationPreferencesInfo::GetSilentReminderInfo( + const sptr &bundleOption, SilentReminderInfo &info) const +{ + std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid()); + auto iter = silentReminderInfos_.find(bundleKey); + if (iter != silentReminderInfos_.end()) { + info = iter->second; + return true; + } + return false; +} + bool NotificationPreferencesInfo::RemoveBundleInfo(const sptr &bundleOption) { std::string bundleKey = bundleOption->GetBundleName() + std::to_string(bundleOption->GetUid()); @@ -363,6 +381,11 @@ void NotificationPreferencesInfo::GetAllCLoneBundlesInfo(const int32_t &userId, slotInfo.isForceControl_ = slot->GetForceControl(); cloneBundleInfo.AddSlotInfo(slotInfo); } + auto silentReminderIter = silentReminderInfos_.find(bundleItem.second); + if (silentReminderIter != silentReminderInfos_.end()) { + auto enableStatus = silentReminderIter->second.enableStatus; + cloneBundleInfo.SetSilentReminderEnabled(enableStatus); + } cloneBundles.emplace_back(cloneBundleInfo); } ANS_LOGI("GetAllCLoneBundlesInfo size: %{public}zu.", cloneBundles.size()); @@ -409,6 +432,12 @@ void NotificationPreferencesInfo::SetBundleInfoFromDb(BundleInfo &info, std::str infos_.insert_or_assign(bundleKey, info); } +void NotificationPreferencesInfo::SetSilentReminderInfoFromDb( + SilentReminderInfo &silentReminderInfo, std::string bundleKey) +{ + silentReminderInfos_.insert_or_assign(bundleKey, silentReminderInfo); +} + void NotificationPreferencesInfo::SetDisableNotificationInfo(const sptr ¬ificationDisable) { if (notificationDisable == nullptr) { diff --git a/services/ans/test/unittest/BUILD.gn b/services/ans/test/unittest/BUILD.gn index 0c1b19a507caa03949dad4c6369c11a0e9b54c9e..5c368267fafd209fc15672b04af3d1a88fa86a31 100644 --- a/services/ans/test/unittest/BUILD.gn +++ b/services/ans/test/unittest/BUILD.gn @@ -1517,6 +1517,7 @@ ohos_unittest("notification_extension_test") { "${services_path}/ans/src/distributed_manager/advanced_notification_distributed_manager_service.cpp", "${services_path}/ans/src/disturb_manager/advanced_notification_disturb_manager_service.cpp", "${services_path}/ans/src/enable_manager/enable_manager.cpp", + "${services_path}/ans/src/enable_manager/silent_reminder_manager.cpp", "${services_path}/ans/src/event_report.cpp", "${services_path}/ans/src/liveview_all_scenarios_extension_wrapper.cpp", "${services_path}/ans/src/notification_dialog.cpp", diff --git a/services/ans/test/unittest/advanced_notification_service_test.cpp b/services/ans/test/unittest/advanced_notification_service_test.cpp index 316ad312ca061f86d50137ac31db17c3ed08183d..8c9c23018e3d699a07e006babafd0f0e29e390b6 100644 --- a/services/ans/test/unittest/advanced_notification_service_test.cpp +++ b/services/ans/test/unittest/advanced_notification_service_test.cpp @@ -4530,5 +4530,107 @@ HWTEST_F(AdvancedNotificationServiceTest, SetNotificationRemindType_00005, Funct auto reminderType = advancedNotificationService_->GetRemindType(); ASSERT_EQ(reminderType, NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND); } + +/** + * @tc.name : SetSilentReminderEnabled_00001 + * @tc.number : + * @tc.desc : test SetSilentReminderEnabled + */ +HWTEST_F(AdvancedNotificationServiceTest, SetSilentReminderEnabled_00001, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + ErrCode ret = advancedNotificationService.SetSilentReminderEnabled(nullptr, true); + ASSERT_EQ(ret, ERR_ANS_INVALID_BUNDLE); + std::string bundleName = "test11"; + int32_t uid = -1; + sptr bo(new (std::nothrow) NotificationBundleOption(bundleName, uid)); + + MockIsSystemApp(false); + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + ret = advancedNotificationService.SetSilentReminderEnabled(bo, true); + ASSERT_EQ(ret, ERR_ANS_NON_SYSTEM_APP); + + MockIsSystemApp(true); + MockIsVerfyPermisson(false); + ret = advancedNotificationService.SetSilentReminderEnabled(bo, true); + ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED); + + MockIsVerfyPermisson(true); + MockQueryForgroundOsAccountId(false, 0); + ret = advancedNotificationService.SetSilentReminderEnabled(bo, true); + ASSERT_EQ(ret, ERR_ANS_INVALID_BUNDLE); + + MockQueryForgroundOsAccountId(true, 100); + advancedNotificationService.notificationSvrQueue_ = nullptr; + ret = advancedNotificationService.SetSilentReminderEnabled(bo, true); + ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM); +} + +/** + * @tc.name : SetSilentReminderEnabled_00001 + * @tc.number : + * @tc.desc : test SetSilentReminderEnabled + */ +HWTEST_F(AdvancedNotificationServiceTest, SetSilentReminderEnabled_00002, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + std::string bundleName = "test11"; + int32_t uid = 1; + sptr bo(new (std::nothrow) NotificationBundleOption(bundleName, uid)); + int32_t enableStatusInt; + + MockIsSystemApp(true); + MockIsVerfyPermisson(true); + MockQueryForgroundOsAccountId(true, 100); + + ErrCode ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_OK); + ASSERT_EQ(enableStatusInt, 0); + + ret = advancedNotificationService.SetSilentReminderEnabled(bo, true); + ASSERT_EQ(ret, ERR_OK); + + ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_OK); + ASSERT_EQ(enableStatusInt, 2); +} + +/** + * @tc.name : SetSilentReminderEnabled_00001 + * @tc.number : + * @tc.desc : test SetSilentReminderEnabled + */ +HWTEST_F(AdvancedNotificationServiceTest, IsSilentReminderEnabled_00001, Function | SmallTest | Level1) +{ + AdvancedNotificationService advancedNotificationService; + int32_t enableStatusInt; + + ErrCode ret = advancedNotificationService.IsSilentReminderEnabled(nullptr, enableStatusInt); + ASSERT_EQ(ret, ERR_ANS_INVALID_BUNDLE); + + std::string bundleName = "test11"; + int32_t uid = -1; + sptr bo(new (std::nothrow) NotificationBundleOption(bundleName, uid)); + + MockIsSystemApp(false); + MockGetTokenTypeFlag(Security::AccessToken::ATokenTypeEnum::TOKEN_HAP); + ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_ANS_NON_SYSTEM_APP); + + MockIsSystemApp(true); + MockIsVerfyPermisson(false); + ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_ANS_PERMISSION_DENIED); + + MockIsVerfyPermisson(true); + MockQueryForgroundOsAccountId(false, 0); + ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_ANS_INVALID_BUNDLE); + + MockQueryForgroundOsAccountId(true, 100); + advancedNotificationService.notificationSvrQueue_ = nullptr; + ret = advancedNotificationService.IsSilentReminderEnabled(bo, enableStatusInt); + ASSERT_EQ(ret, ERR_ANS_INVALID_PARAM); +} } // namespace Notification } // namespace OHOS diff --git a/tools/test/mock/mock_ans_manager_stub.h b/tools/test/mock/mock_ans_manager_stub.h index 4a51b657ed8965bbc1a356b5c9b4ef08b12f0033..1545c603c98b5048fbcd55380eaab5d077956ebb 100644 --- a/tools/test/mock/mock_ans_manager_stub.h +++ b/tools/test/mock/mock_ans_manager_stub.h @@ -708,6 +708,18 @@ public: return ERR_ANS_INVALID_PARAM; } + ErrCode SetSilentReminderEnabled(const sptr &bundleOption, + const bool enabled) override + { + return ERR_ANS_INVALID_PARAM; + } + + ErrCode IsSilentReminderEnabled(const sptr &bundleOption, + int32_t &enableStatusInt) override + { + return ERR_ANS_INVALID_PARAM; + } + private: std::string cmd_; std::string bundle_;