diff --git a/frameworks/ans/src/notification_helper.cpp b/frameworks/ans/src/notification_helper.cpp index 294d93a01fd9b31c82b1b5f55031f057481d592b..330879179c1a9548f94a29339302c4e2b270babf 100644 --- a/frameworks/ans/src/notification_helper.cpp +++ b/frameworks/ans/src/notification_helper.cpp @@ -422,5 +422,17 @@ ErrCode NotificationHelper::GetEnabledForBundleSlot( { return DelayedSingleton::GetInstance()->GetEnabledForBundleSlot(bundleOption, slotType, enabled); } + +ErrCode NotificationHelper::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + return DelayedSingleton::GetInstance()->SetSyncNotificationEnabledWithoutApp( + userId, enabled); +} + +ErrCode NotificationHelper::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + return DelayedSingleton::GetInstance()->GetSyncNotificationEnabledWithoutApp( + userId, enabled); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/core/include/ans_manager_interface.h b/frameworks/core/include/ans_manager_interface.h index f032f64b9a7b244042b6908efd62b007b3795c41..376489c55cb5880366dd1dc7ba735f5f25ca0f86 100644 --- a/frameworks/core/include/ans_manager_interface.h +++ b/frameworks/core/include/ans_manager_interface.h @@ -720,6 +720,24 @@ public: virtual ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, std::vector &dumpInfo) = 0; + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + virtual ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) = 0; + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + virtual ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) = 0; + protected: enum TransactId : uint32_t { PUBLISH_NOTIFICATION = FIRST_CALL_TRANSACTION, @@ -798,7 +816,9 @@ protected: SET_DO_NOT_DISTURB_DATE_BY_USER, GET_DO_NOT_DISTURB_DATE_BY_USER, SET_ENABLED_FOR_BUNDLE_SLOT, - GET_ENABLED_FOR_BUNDLE_SLOT + GET_ENABLED_FOR_BUNDLE_SLOT, + SET_SYNC_NOTIFICATION_ENABLED_WITHOUT_APP, + GET_SYNC_NOTIFICATION_ENABLED_WITHOUT_APP }; }; } // namespace Notification diff --git a/frameworks/core/include/ans_manager_proxy.h b/frameworks/core/include/ans_manager_proxy.h index cde0055ca5351700593ec9b12a634b922046bc85..696c44c56f9b8c6adc0a6009d8906cfda12de207 100644 --- a/frameworks/core/include/ans_manager_proxy.h +++ b/frameworks/core/include/ans_manager_proxy.h @@ -707,6 +707,24 @@ public: ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, std::vector &dumpInfo) override; + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override; + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override; + private: ErrCode InnerTransact(uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply); diff --git a/frameworks/core/include/ans_manager_stub.h b/frameworks/core/include/ans_manager_stub.h index 11871d41ac22c8cb5231ec2e0674e63f034fdc8c..2d857d6f1a6318c18dde26fef8b280e2d071f0b9 100644 --- a/frameworks/core/include/ans_manager_stub.h +++ b/frameworks/core/include/ans_manager_stub.h @@ -728,6 +728,24 @@ public: virtual ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, std::vector &dumpInfo) override; + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + virtual ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override; + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + virtual ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override; + private: static const std::map> interfaces_; @@ -809,6 +827,8 @@ private: ErrCode HandleGetDoNotDisturbDateByUser(MessageParcel &data, MessageParcel &reply); ErrCode HandleSetEnabledForBundleSlot(MessageParcel &data, MessageParcel &reply); ErrCode HandleGetEnabledForBundleSlot(MessageParcel &data, MessageParcel &reply); + ErrCode HandleDistributedSetEnabledWithoutApp(MessageParcel &data, MessageParcel &reply); + ErrCode HandleDistributedGetEnabledWithoutApp(MessageParcel &data, MessageParcel &reply); template bool WriteParcelableVector(const std::vector> &parcelableVector, MessageParcel &reply, ErrCode &result); diff --git a/frameworks/core/include/ans_notification.h b/frameworks/core/include/ans_notification.h index 3301bd1731fd584b2e05f675d0e07ecd6d51102d..05bf40439a3cd8cee4a2b6b21b002ef723506790 100644 --- a/frameworks/core/include/ans_notification.h +++ b/frameworks/core/include/ans_notification.h @@ -851,6 +851,25 @@ public: */ ErrCode ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, std::vector &dumpInfo); + + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled); + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled); + private: /** * @brief Gets Ans Manager proxy. diff --git a/frameworks/core/src/ans_manager_proxy.cpp b/frameworks/core/src/ans_manager_proxy.cpp index 7c00a7b9d0c8d434fb5f3c15de12bffb5aa87cdf..fb5851ff8211599b1a4b1df6d4230d11d1d6e8a7 100644 --- a/frameworks/core/src/ans_manager_proxy.cpp +++ b/frameworks/core/src/ans_manager_proxy.cpp @@ -2712,7 +2712,75 @@ ErrCode AnsManagerProxy::GetEnabledForBundleSlot( } if (!reply.ReadBool(enabled)) { - ANS_LOGE("[GetEnabledForBundleSlot] fail: read canPublish failed."); + ANS_LOGE("[GetEnabledForBundleSlot] fail: read enable failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[SetSyncNotificationEnabledWithoutApp] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGE("[SetSyncNotificationEnabledWithoutApp] fail:: write userId failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteBool(enabled)) { + ANS_LOGE("[SetSyncNotificationEnabledWithoutApp] fail: write enabled failed"); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(SET_SYNC_NOTIFICATION_ENABLED_WITHOUT_APP, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[SetSyncNotificationEnabledWithoutApp] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[SetSyncNotificationEnabledWithoutApp] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + return result; +} + +ErrCode AnsManagerProxy::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(AnsManagerProxy::GetDescriptor())) { + ANS_LOGE("[GetSyncNotificationEnabledWithoutApp] fail: write interface token failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!data.WriteInt32(userId)) { + ANS_LOGE("[GetSyncNotificationEnabledWithoutApp] fail:: write userId failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + MessageParcel reply; + MessageOption option = {MessageOption::TF_SYNC}; + ErrCode result = InnerTransact(GET_SYNC_NOTIFICATION_ENABLED_WITHOUT_APP, option, data, reply); + if (result != ERR_OK) { + ANS_LOGE("[GetSyncNotificationEnabledWithoutApp] fail: transact ErrCode=%{public}d", result); + return ERR_ANS_TRANSACT_FAILED; + } + + if (!reply.ReadInt32(result)) { + ANS_LOGE("[GetSyncNotificationEnabledWithoutApp] fail: read result failed."); + return ERR_ANS_PARCELABLE_FAILED; + } + + if (!reply.ReadBool(enabled)) { + ANS_LOGE("[GetSyncNotificationEnabledWithoutApp] fail: read enable failed."); return ERR_ANS_PARCELABLE_FAILED; } diff --git a/frameworks/core/src/ans_manager_stub.cpp b/frameworks/core/src/ans_manager_stub.cpp index cab285e1e7b4a93d683973b4df87de6f8facf834..e72fdafc9683ee010bf7cd445499933d152b6d32 100644 --- a/frameworks/core/src/ans_manager_stub.cpp +++ b/frameworks/core/src/ans_manager_stub.cpp @@ -263,6 +263,12 @@ const std::map ¬ification) { ANS_LOGE("AnsManagerStub::Publish called!"); @@ -2282,11 +2336,24 @@ ErrCode AnsManagerStub::GetEnabledForBundleSlot( return ERR_INVALID_OPERATION; } + ErrCode AnsManagerStub::ShellDump(const std::string &cmd, const std::string &bundle, int32_t userId, std::vector &dumpInfo) { ANS_LOGE("AnsManagerStub::ShellDump called!"); return ERR_INVALID_OPERATION; } + +ErrCode AnsManagerStub::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + ANS_LOGE("AnsManagerStub::SetSyncNotificationEnabledWithoutApp called!"); + return ERR_INVALID_OPERATION; +} + +ErrCode AnsManagerStub::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + ANS_LOGE("AnsManagerStub::GetSyncNotificationEnabledWithoutApp called!"); + return ERR_INVALID_OPERATION; +} } // namespace Notification } // namespace OHOS diff --git a/frameworks/core/src/ans_notification.cpp b/frameworks/core/src/ans_notification.cpp index 15d9ab6c43ffe8ddfebebdb13da048ae60a5cffc..a88d5b6e6369c0c4a04d14a9d630d35bf0d15c4f 100644 --- a/frameworks/core/src/ans_notification.cpp +++ b/frameworks/core/src/ans_notification.cpp @@ -1387,7 +1387,38 @@ ErrCode AnsNotification::ShellDump(const std::string &cmd, const std::string &bu ANS_LOGE("GetAnsManagerProxy fail."); return ERR_ANS_SERVICE_NOT_CONNECTED; } + return ansManagerProxy_->ShellDump(cmd, bundle, userId, dumpInfo); } + +ErrCode AnsNotification::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->SetSyncNotificationEnabledWithoutApp(userId, enabled); +} + +ErrCode AnsNotification::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + if (userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Input userId is invalid."); + return ERR_ANS_INVALID_PARAM; + } + + if (!GetAnsManagerProxy()) { + ANS_LOGE("GetAnsManagerProxy fail."); + return ERR_ANS_SERVICE_NOT_CONNECTED; + } + + return ansManagerProxy_->GetSyncNotificationEnabledWithoutApp(userId, enabled); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/include/distributed.h b/frameworks/js/napi/include/distributed.h index 755db443e6ac76d552389c303f69f4a0517f5844..630de77cd4f7ead1464c6625ed07801106978c59 100644 --- a/frameworks/js/napi/include/distributed.h +++ b/frameworks/js/napi/include/distributed.h @@ -28,6 +28,8 @@ napi_value EnableDistributedByBundle(napi_env env, napi_callback_info info); napi_value EnableDistributedSelf(napi_env env, napi_callback_info info); napi_value IsDistributedEnableByBundle(napi_env env, napi_callback_info info); napi_value GetDeviceRemindType(napi_env env, napi_callback_info info); +napi_value SetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); +napi_value GetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info); } // namespace NotificationNapi } // namespace OHOS diff --git a/frameworks/js/napi/src/distributed.cpp b/frameworks/js/napi/src/distributed.cpp index 3eb314dc520389add0fdb8bcd37eb3d08e7d5dfc..94c1b99839c8de32082252509f856f68e28611cf 100644 --- a/frameworks/js/napi/src/distributed.cpp +++ b/frameworks/js/napi/src/distributed.cpp @@ -23,6 +23,8 @@ const int ENABLED_BUNDLE_MAX_PARA = 3; const int ENABLED_BUNDLE_MIN_PARA = 2; const int IS_ENABLED_BUNDLE_MAX_PARA = 2; const int IS_ENABLED_BUNDLE_MIN_PARA = 1; +const int ENABLED_SYNC_MAX_PARA = 3; +const int ENABLED_SYNC_MIN_PARA = 2; struct AsyncCallbackInfoIsEnabled { napi_env env = nullptr; @@ -76,6 +78,32 @@ struct AsyncCallbackInfoGetRemindType { NotificationConstant::RemindType remindType = NotificationConstant::RemindType::NONE; }; +struct EnabledWithoutAppParams { + int32_t userId = SUBSCRIBE_USER_INIT; + bool enable = false; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoEnabledWithoutApp { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + EnabledWithoutAppParams params; +}; + +struct GetEnabledWithoutAppParams { + int32_t userId = SUBSCRIBE_USER_INIT; + napi_ref callback = nullptr; +}; + +struct AsyncCallbackInfoGetEnabledWithoutApp { + napi_env env = nullptr; + napi_async_work asyncWork = nullptr; + CallbackPromiseInfo info; + GetEnabledWithoutAppParams params; + bool enable = false; +}; + napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledParams ¶ms) { ANS_LOGI("enter"); @@ -84,18 +112,27 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[ENABLED_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= ENABLED_MIN_PARA, "Wrong number of arguments"); + if (argc < ENABLED_MIN_PARA) { + ANS_LOGE("Wrong number of arguments."); + return nullptr; + } napi_valuetype valuetype = napi_undefined; // argv[0]: enable NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_boolean, "Wrong argument type. Bool expected."); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + return nullptr; + } napi_get_value_bool(env, argv[PARAM0], ¶ms.enable); // argv[1]:callback if (argc >= ENABLED_MAX_PARA) { NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); } @@ -110,12 +147,18 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[ENABLED_BUNDLE_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= ENABLED_BUNDLE_MIN_PARA, "Wrong number of arguments"); + if (argc < ENABLED_BUNDLE_MIN_PARA) { + ANS_LOGE("Wrong number of arguments."); + return nullptr; + } // argv[0]: bundle napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return nullptr; + } auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option); if (retValue == nullptr) { ANS_LOGE("GetBundleOption failed."); @@ -124,14 +167,20 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, // argv[1]: enable NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_boolean, "Wrong argument type. Bool expected."); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + return nullptr; + } napi_get_value_bool(env, argv[PARAM1], ¶ms.enable); // argv[2]:callback if (argc >= ENABLED_BUNDLE_MAX_PARA) { napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); } @@ -146,12 +195,18 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, napi_value argv[IS_ENABLED_BUNDLE_MAX_PARA] = {nullptr}; napi_value thisVar = nullptr; NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc >= IS_ENABLED_BUNDLE_MIN_PARA, "Wrong number of arguments"); + if (argc < IS_ENABLED_BUNDLE_MIN_PARA) { + ANS_LOGE("Wrong number of arguments."); + return nullptr; + } // argv[0]: bundle napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_object, "Wrong argument type. Object expected."); + if (valuetype != napi_object) { + ANS_LOGE("Wrong argument type. Object expected."); + return nullptr; + } auto retValue = Common::GetBundleOption(env, argv[PARAM0], params.option); if (retValue == nullptr) { ANS_LOGE("GetBundleOption failed."); @@ -162,7 +217,10 @@ napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, if (argc >= IS_ENABLED_BUNDLE_MAX_PARA) { napi_valuetype valuetype = napi_undefined; NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); - NAPI_ASSERT(env, valuetype == napi_function, "Wrong argument type. Function expected."); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); } @@ -550,5 +608,228 @@ napi_value GetDeviceRemindType(napi_env env, napi_callback_info info) return promise; } } + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, EnabledWithoutAppParams ¶ms) +{ + ANS_LOGI("enter"); + + size_t argc = ENABLED_SYNC_MAX_PARA; + napi_value argv[ENABLED_SYNC_MAX_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr)); + if (argc < ENABLED_SYNC_MIN_PARA) { + ANS_LOGE("Wrong number of arguments."); + return nullptr; + } + + // argv[0]: userId + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_number) { + ANS_LOGE("Wrong argument type. Number expected."); + return nullptr; + } + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId)); + if (params.userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Wrong userId[%{public}d].", params.userId); + return nullptr; + } + + // argv[1]: enable + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_boolean) { + ANS_LOGE("Wrong argument type. Bool expected."); + return nullptr; + } + napi_get_value_bool(env, argv[PARAM1], ¶ms.enable); + + // argv[2]:callback + if (argc >= ENABLED_SYNC_MAX_PARA) { + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM2], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, argv[PARAM2], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} + +napi_value SetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + EnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoEnabledWithoutApp {.env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "SetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::SetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->params.enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("SetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, Common::NapiGetNull(env)); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} + +napi_value ParseParameters(const napi_env &env, const napi_callback_info &info, GetEnabledWithoutAppParams ¶ms) +{ + ANS_LOGI("enter"); + + size_t argc = ENABLED_SYNC_MIN_PARA; + napi_value argv[ENABLED_SYNC_MIN_PARA] = {nullptr}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + if (argc < ENABLED_SYNC_MIN_PARA - 1) { + ANS_LOGE("Wrong number of arguments."); + return nullptr; + } + + // argv[0]: userId + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM0], &valuetype)); + if (valuetype != napi_number) { + ANS_LOGE("Wrong argument type. Number expected."); + return nullptr; + } + NAPI_CALL(env, napi_get_value_int32(env, argv[PARAM0], ¶ms.userId)); + if (params.userId <= SUBSCRIBE_USER_INIT) { + ANS_LOGE("Wrong userId[%{public}d].", params.userId); + return nullptr; + } + + // argv[1]:callback + if (argc >= ENABLED_SYNC_MIN_PARA) { + napi_valuetype valuetype = napi_undefined; + NAPI_CALL(env, napi_typeof(env, argv[PARAM1], &valuetype)); + if (valuetype != napi_function) { + ANS_LOGE("Wrong argument type. Function expected."); + return nullptr; + } + napi_create_reference(env, argv[PARAM1], 1, ¶ms.callback); + } + + return Common::NapiGetNull(env); +} + +napi_value GetSyncNotificationEnabledWithoutApp(napi_env env, napi_callback_info info) +{ + ANS_LOGI("enter"); + + GetEnabledWithoutAppParams params {}; + if (ParseParameters(env, info, params) == nullptr) { + return Common::JSParaError(env, params.callback); + } + + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + new (std::nothrow) AsyncCallbackInfoGetEnabledWithoutApp { + .env = env, .asyncWork = nullptr, .params = params}; + if (!asynccallbackinfo) { + return Common::JSParaError(env, params.callback); + } + napi_value promise = nullptr; + Common::PaddingCallbackPromiseInfo(env, params.callback, asynccallbackinfo->info, promise); + + napi_value resourceName = nullptr; + napi_create_string_latin1(env, "GetSyncNotificationEnabledWithoutApp", NAPI_AUTO_LENGTH, &resourceName); + // Asynchronous function call + napi_create_async_work( + env, + nullptr, + resourceName, + [](napi_env env, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work start"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + asynccallbackinfo->info.errorCode = NotificationHelper::GetSyncNotificationEnabledWithoutApp( + asynccallbackinfo->params.userId, asynccallbackinfo->enable); + } + }, + [](napi_env env, napi_status status, void *data) { + ANS_LOGI("GetSyncNotificationEnabledWithoutApp napi_create_async_work end"); + AsyncCallbackInfoGetEnabledWithoutApp *asynccallbackinfo = + static_cast(data); + if (asynccallbackinfo) { + napi_value result = nullptr; + if (asynccallbackinfo->info.errorCode != ERR_OK) { + result = Common::NapiGetNull(env); + } else { + napi_get_boolean(env, asynccallbackinfo->enable, &result); + } + Common::ReturnCallbackPromise(env, asynccallbackinfo->info, result); + if (asynccallbackinfo->info.callback != nullptr) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + } + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + } + }, + (void *)asynccallbackinfo, + &asynccallbackinfo->asyncWork); + + if (napi_queue_async_work(env, asynccallbackinfo->asyncWork) != napi_ok) { + napi_delete_reference(env, asynccallbackinfo->info.callback); + napi_delete_async_work(env, asynccallbackinfo->asyncWork); + delete asynccallbackinfo; + asynccallbackinfo = nullptr; + return Common::JSParaError(env, params.callback); + } + + if (asynccallbackinfo->info.isCallback) { + return Common::NapiGetNull(env); + } + return promise; +} } // namespace NotificationNapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/src/init.cpp b/frameworks/js/napi/src/init.cpp index e1136712bf3705e165cf945f66bb5b851f269a40..d5e90605013d26746fdac230210269e5fc89621a 100644 --- a/frameworks/js/napi/src/init.cpp +++ b/frameworks/js/napi/src/init.cpp @@ -81,6 +81,8 @@ napi_value NotificationInit(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("show", ShowNotification), DECLARE_NAPI_FUNCTION("enableNotificationSlot", EnableNotificationSlot), DECLARE_NAPI_FUNCTION("isNotificationSlotEnabled", IsEnableNotificationSlot), + DECLARE_NAPI_FUNCTION("setSyncNotificationEnabledForUninstallApp", SetSyncNotificationEnabledWithoutApp), + DECLARE_NAPI_FUNCTION("getSyncNotificationEnabledForUninstallApp", GetSyncNotificationEnabledWithoutApp), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); diff --git a/frameworks/test/moduletest/BUILD.gn b/frameworks/test/moduletest/BUILD.gn index c1e6b76ba2f46d691555889e81b010fc2ed9d907..96f48597ec7f59f10e74c522ae8386fd9945da4e 100644 --- a/frameworks/test/moduletest/BUILD.gn +++ b/frameworks/test/moduletest/BUILD.gn @@ -339,8 +339,7 @@ group("moduletest") { # ":ans_fw_module_test", ":ans_innerkits_module_publish_test", - - # ":ans_innerkits_module_setting_test", + ":ans_innerkits_module_setting_test", ":ans_innerkits_module_slot_test", ] } diff --git a/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp b/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp index 01fccfbe188f84f3980ac203eec6e64df11077db..a94994a06919c8a76ef9777d5e945d0b4f525dac 100644 --- a/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp +++ b/frameworks/test/moduletest/ans_innerkits_module_setting_test.cpp @@ -30,6 +30,7 @@ namespace Notification { static sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); const int32_t CALLING_UID = 9998; +const int32_t USERID = 100; class AnsInnerKitsModuleSettingTest : public testing::Test { public: @@ -141,11 +142,10 @@ HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_004 */ HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00500, Function | MediumTest | Level1) { - std::string templateName("process"); + std::string templateName("downloadTemplate"); bool support = false; - EXPECT_EQ((int)ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, - NotificationHelper::IsSupportTemplate(templateName, support)); - EXPECT_EQ(false, support); + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(true, support); } /** @@ -156,11 +156,40 @@ HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_005 */ HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00700, Function | MediumTest | Level1) { - std::string templateName("template123"); + std::string templateName("downloadTemplate_1"); bool support = false; - EXPECT_EQ((int)ERR_ANS_PREFERENCES_NOTIFICATION_READ_TEMPLATE_CONFIG_FAILED, - NotificationHelper::IsSupportTemplate(templateName, support)); + EXPECT_EQ(0, NotificationHelper::IsSupportTemplate(templateName, support)); EXPECT_EQ(false, support); } + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00800 + * @tc.name : NotificationSetting_00800 + * @tc.desc : Set whether to sync notifications to devices that do not have the app installed. + * @tc.expected : Set true, get true. + */ +HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00800, Function | MediumTest | Level1) +{ + NotificationBundleOption bundleOption("bundlename", CALLING_UID); + EXPECT_EQ(0, NotificationHelper::SetSyncNotificationEnabledWithoutApp(USERID, true)); + bool enabled = false; + EXPECT_EQ(0, NotificationHelper::GetSyncNotificationEnabledWithoutApp(USERID, enabled)); + EXPECT_EQ(true, enabled); +} + +/** + * @tc.number : ANS_Interface_MT_NotificationSetting_00900 + * @tc.name : NotificationSetting_00900 + * @tc.desc : Set a specified application do not show badge, get the specified application can not show badge. + * @tc.expected : Set false, get false. + */ +HWTEST_F(AnsInnerKitsModuleSettingTest, ANS_Interface_MT_NotificationSetting_00900, Function | MediumTest | Level1) +{ + NotificationBundleOption bundleOption("bundlename", CALLING_UID); + EXPECT_EQ(0, NotificationHelper::SetSyncNotificationEnabledWithoutApp(USERID, false)); + bool enabled = true; + EXPECT_EQ(0, NotificationHelper::GetSyncNotificationEnabledWithoutApp(USERID, enabled)); + EXPECT_EQ(false, enabled); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/notification_helper.h b/interfaces/inner_api/notification_helper.h index bcd14df6cf6f7d5f08ab2795dcca9a8c73685ba3..9cf917e7bcd47dc555ece7877b4055c0a6ff1141 100644 --- a/interfaces/inner_api/notification_helper.h +++ b/interfaces/inner_api/notification_helper.h @@ -804,6 +804,24 @@ public: */ static ErrCode GetEnabledForBundleSlot( const NotificationBundleOption &bundleOption, const NotificationConstant::SlotType &slotType, bool &enabled); + + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + static ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled); + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + static ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index da212fcbd26c1a7bd9c8f3d3aaa962a792366b18..5d831e96dcd02018847913340bec533bd98930d0 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -700,6 +700,24 @@ public: */ void OnBundleRemoved(const sptr &bundleOption); + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) override; + + /** + * @brief Obtains whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns get enabled result. + */ + ErrCode GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) override; + #ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED /** * @brief Obtains the event of turn on screen. @@ -804,6 +822,7 @@ private: void OnDistributedDelete( const std::string &deviceId, const std::string &bundleName, const std::string &label, int32_t id); ErrCode GetDistributedEnableInApplicationInfo(const sptr bundleOption, bool &enable); + bool CheckPublishWithoutApp(const int32_t userId, const sptr &request); #endif ErrCode SetDoNotDisturbDateByUser(const int32_t &userId, const sptr &date); diff --git a/services/ans/src/advanced_notification_service.cpp b/services/ans/src/advanced_notification_service.cpp index a532e307b155e9fc80611a9aecfb9a32dde20bed..8bdf5ac9df1ccd596dd7d402eabac2527adf86b2 100644 --- a/services/ans/src/advanced_notification_service.cpp +++ b/services/ans/src/advanced_notification_service.cpp @@ -3012,7 +3012,7 @@ void AdvancedNotificationService::OnDistributedPublish( } request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); - handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { + handler_->PostTask(std::bind([this, deviceId, bundleName, request, activeUserId]() { if (!CheckDistributedNotificationType(request)) { ANS_LOGD("device type not support display."); return; @@ -3020,7 +3020,8 @@ void AdvancedNotificationService::OnDistributedPublish( sptr bundleOption = GenerateValidBundleOption(new NotificationBundleOption(bundleName, 0)); - if (bundleOption == nullptr) { + if (bundleOption == nullptr && !CheckPublishWithoutApp(activeUserId, request)) { + ANS_LOGE("bundle does not exit and enable is closed!"); return; } std::shared_ptr record = std::make_shared(); @@ -3067,14 +3068,15 @@ void AdvancedNotificationService::OnDistributedUpdate( } request->SetCreatorUid(BundleManagerHelper::GetInstance()->GetDefaultUidByBundleName(bundleName, activeUserId)); - handler_->PostTask(std::bind([this, deviceId, bundleName, request]() { + handler_->PostTask(std::bind([this, deviceId, bundleName, request, activeUserId]() { if (!CheckDistributedNotificationType(request)) { ANS_LOGD("device type not support display."); return; } sptr bundleOption = GenerateValidBundleOption(new NotificationBundleOption(bundleName, 0)); - if (bundleOption == nullptr) { + if (bundleOption == nullptr && !CheckPublishWithoutApp(activeUserId, request)) { + ANS_LOGE("bundle does not exit, or enable is false!"); return; } std::shared_ptr record = std::make_shared(); @@ -3170,6 +3172,30 @@ ErrCode AdvancedNotificationService::GetDistributedEnableInApplicationInfo( return ERR_OK; } + +bool AdvancedNotificationService::CheckPublishWithoutApp(const int32_t userId, const sptr &request) +{ + bool enabled = false; + DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled); + if (!enabled) { + ANS_LOGE("enable is false, userId[%{public}d]", userId); + return false; + } + + std::shared_ptr wantAgent = request->GetWantAgent(); + if (!wantAgent) { + ANS_LOGE("Failed to get wantAgent!"); + return false; + } + + std::shared_ptr want = AbilityRuntime::WantAgent::WantAgentHelper::GetWant(wantAgent); + if (!want || want->GetDeviceId().empty()) { + ANS_LOGE("Failed to get want!"); + return false; + } + + return true; +} #endif ErrCode AdvancedNotificationService::PrepareContinuousTaskNotificationRequest( @@ -3856,5 +3882,53 @@ void AdvancedNotificationService::SendFlowControlOccurHiSysEvent(const std::shar eventInfo.uid = record->bundleOption->GetUid(); EventReport::SendHiSysEvent(FLOW_CONTROL_OCCUR, eventInfo); } + +ErrCode AdvancedNotificationService::SetSyncNotificationEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + ANS_LOGD("userId: %{public}d, enabled: %{public}d", userId, enabled); + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + ErrCode result = ERR_OK; + handler_->PostSyncTask( + std::bind([&]() { + result = DistributedPreferences::GetInstance()->SetSyncEnabledWithoutApp(userId, enabled); + })); + return result; +#else + return ERR_INVALID_OPERATION; +#endif +} + +ErrCode AdvancedNotificationService::GetSyncNotificationEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + ANS_LOGD("userId: %{public}d", userId); + +#ifdef DISTRIBUTED_NOTIFICATION_SUPPORTED + if (!IsSystemApp()) { + return ERR_ANS_NON_SYSTEM_APP; + } + + if (!CheckPermission(OHOS_PERMISSION_NOTIFICATION_CONTROLLER)) { + return ERR_ANS_PERMISSION_DENIED; + } + + ErrCode result = ERR_OK; + handler_->PostSyncTask( + std::bind([&]() { + result = DistributedPreferences::GetInstance()->GetSyncEnabledWithoutApp(userId, enabled); + })); + return result; +#else + return ERR_INVALID_OPERATION; +#endif +} } // namespace Notification } // namespace OHOS diff --git a/services/distributed/include/distributed_preferences.h b/services/distributed/include/distributed_preferences.h index 108414c539cf27a883804e4319cc4f0ed864deb9..35e34a73ae9beebf0a2699e6a2d3b5173fd481eb 100644 --- a/services/distributed/include/distributed_preferences.h +++ b/services/distributed/include/distributed_preferences.h @@ -34,7 +34,7 @@ public: /** * @brief Set whether the device supports distributed notifications. * - * @param enable Specifies whether to enable the device to support distributed notification. + * @param isEnable Specifies whether to enable the device to support distributed notification. * @return Returns enable distributed result. */ ErrCode SetDistributedEnable(bool isEnable); @@ -42,7 +42,7 @@ public: /** * @brief Check if the device supports distributed notification. * - * @param enabled True if the device supports distributed notification; false otherwise. + * @param isEnable True if the device supports distributed notification; false otherwise. * @return Returns is distributed enabled result. */ ErrCode GetDistributedEnable(bool &isEnable); @@ -51,7 +51,7 @@ public: * @brief Set whether an application supports distributed notifications. * * @param bundleOption Indicates the bundle name and uid of an application. - * @param enabled Specifies whether to enable an application to support distributed notification. + * @param isEnable Specifies whether to enable an application to support distributed notification. * @return Returns enable distributed by bundle result. */ ErrCode SetDistributedBundleEnable(const sptr &bundleOption, bool isEnable); @@ -60,7 +60,7 @@ public: * @brief Check whether an application supports distributed notifications. * * @param bundleOption Indicates the bundle name and uid of an application. - * @param enabled True if the application supports distributed notification; false otherwise. + * @param isEnable True if the application supports distributed notification; false otherwise. * @return Returns is distributed enabled by bundle result. */ ErrCode GetDistributedBundleEnable(const sptr &bundleOption, bool &isEnable); @@ -80,16 +80,26 @@ public: */ ErrCode ClearDataInRestoreFactorySettings(); + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + ErrCode SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); + ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + private: - struct ResolveKey { - bool isMainKey = false; - std::string bundleName; - int32_t uid = 0; - }; bool InitDistributedAllInfo(void); void GetDistributedMainKey(std::string &key); void GetDistributedBundleKey(const sptr &bundleOption, std::string &key); - bool ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey); + bool ResolveDistributedKey(const DistributedKv::Entry &entry); + void GetEnabledWithoutApp(const int32_t userId, std::string &key); + + bool ResolveDistributedEnable(const std::string &value); + bool ResolveDistributedBundleEnable(const std::string &key, const int32_t startPos, const std::string &value); + bool ResolveSyncWithoutAppEnable(const std::string &key, const int32_t startPos, const std::string &value); private: std::unique_ptr preferencesInfo_ = nullptr; diff --git a/services/distributed/include/distributed_preferences_info.h b/services/distributed/include/distributed_preferences_info.h index efefe5836ab70a78a1026162d6c2efc4f7a88832..cb00bca5e8e9112ba0e42fc832f31d5626e41ceb 100644 --- a/services/distributed/include/distributed_preferences_info.h +++ b/services/distributed/include/distributed_preferences_info.h @@ -19,6 +19,8 @@ #include #include +#include "ans_inner_errors.h" + namespace OHOS { namespace Notification { class DistributedPreferencesInfo { @@ -40,14 +42,6 @@ public: */ bool GetDistributedEnable(void); - /** - * @brief Set whether an application supports distributed notifications. - * - * @param bundleOption Indicates the bundle name and uid of an application. - * @param enabled Specifies whether to enable an application to support distributed notification. - * @return Returns enable distributed by bundle result. - */ - /** * @brief Set whether an application supports distributed notifications. * @@ -74,9 +68,20 @@ public: */ void DeleteDistributedBundleInfo(const std::string &bundleName, int32_t uid); + /** + * @brief Set whether to sync notifications to devices that do not have the app installed. + * + * @param userId Indicates the specific user. + * @param enabled Allow or disallow sync notifications. + * @return Returns set enabled result. + */ + void SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled); + ErrCode GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled); + private: bool distributedEnable_ = true; std::map, bool> bundleEnable_; + std::map enabledWithoutApp_; }; } // namespace Notification } // namespace OHOS diff --git a/services/distributed/src/distributed_preferences.cpp b/services/distributed/src/distributed_preferences.cpp index ba469f54d32fa6172d527643a188c6414242c719..980860796180f1696ca5bb9481bf3f41589db12c 100644 --- a/services/distributed/src/distributed_preferences.cpp +++ b/services/distributed/src/distributed_preferences.cpp @@ -26,6 +26,7 @@ const std::string DISTRIBUTED_LABEL = "distributed"; const std::string DELIMITER = "|"; const std::string MAIN_LABEL = "ans_main"; const std::string BUNDLE_LABEL = "bundle"; +const std::string WITHOUT_APP = "without_app"; } // namespace inline bool GetBoolFromString(const std::string &str) @@ -52,18 +53,8 @@ bool DistributedPreferences::InitDistributedAllInfo(void) } for (auto entry : entries) { - ResolveKey resolveKey; - if (!ResolveDistributedKey(entry.key.ToString(), resolveKey)) { + if (!ResolveDistributedKey(entry)) { ANS_LOGE("key <%{public}s> is invalid.", entry.key.ToString().c_str()); - continue; - } - - if (resolveKey.isMainKey) { - int32_t value = atoi(entry.value.ToString().data()); - preferencesInfo_->SetDistributedEnable(static_cast(value)); - } else { - preferencesInfo_->SetDistributedBundleEnable( - resolveKey.bundleName, resolveKey.uid, GetBoolFromString(entry.value.ToString())); } } @@ -84,8 +75,9 @@ void DistributedPreferences::GetDistributedBundleKey( } } -bool DistributedPreferences::ResolveDistributedKey(const std::string &key, ResolveKey &resolveKey) +bool DistributedPreferences::ResolveDistributedKey(const DistributedKv::Entry &entry) { + std::string key = entry.key.ToString(); std::size_t distributedLabelPosition = 0; std::size_t distributedLabelEndPosition = key.find(DELIMITER, distributedLabelPosition); if (distributedLabelEndPosition == std::string::npos) { @@ -97,12 +89,28 @@ bool DistributedPreferences::ResolveDistributedKey(const std::string &key, Resol return false; } - if (key.substr(typeLabelPosition, typeLabelEndPosition - typeLabelPosition).compare(MAIN_LABEL) == 0) { - resolveKey.isMainKey = true; - return true; + std::string sign = key.substr(typeLabelPosition, typeLabelEndPosition - typeLabelPosition); + if (sign == MAIN_LABEL) { + return ResolveDistributedEnable(entry.value.ToString()); } + if (sign == WITHOUT_APP) { + return ResolveSyncWithoutAppEnable(key, typeLabelEndPosition, entry.value.ToString()); + } + return ResolveDistributedBundleEnable(key, typeLabelEndPosition, entry.value.ToString()); +} + +bool DistributedPreferences::ResolveDistributedEnable(const std::string &value) +{ + int32_t enabled = atoi(value.data()); + preferencesInfo_->SetDistributedEnable(static_cast(enabled)); + + return true; +} - std::size_t bundleNamePosition = typeLabelEndPosition + DELIMITER.size(); +bool DistributedPreferences::ResolveDistributedBundleEnable(const std::string &key, + const int32_t startPos, const std::string &value) +{ + std::size_t bundleNamePosition = startPos + DELIMITER.size(); std::size_t bundleNameEndPosition = key.find(DELIMITER, bundleNamePosition); if (bundleNameEndPosition == std::string::npos) { return false; @@ -113,9 +121,19 @@ bool DistributedPreferences::ResolveDistributedKey(const std::string &key, Resol return false; } - resolveKey.isMainKey = false; - resolveKey.bundleName = key.substr(bundleNamePosition, bundleNameEndPosition - bundleNamePosition); - resolveKey.uid = atoi(&key[uidPosition]); + std::string bundleName = key.substr(bundleNamePosition, bundleNameEndPosition - bundleNamePosition); + int32_t uid = atoi(&key[uidPosition]); + preferencesInfo_->SetDistributedBundleEnable(bundleName, uid, GetBoolFromString(value)); + + return true; +} + +bool DistributedPreferences::ResolveSyncWithoutAppEnable(const std::string &key, + const int32_t startPos, const std::string &value) +{ + std::size_t pos = startPos + DELIMITER.size(); + int32_t userId = atoi(&key[pos]); + preferencesInfo_->SetSyncEnabledWithoutApp(userId, GetBoolFromString(value)); return true; } @@ -214,5 +232,30 @@ ErrCode DistributedPreferences::ClearDataInRestoreFactorySettings() return ERR_OK; } + +void DistributedPreferences::GetEnabledWithoutApp(const int32_t userId, std::string &key) +{ + key = DISTRIBUTED_LABEL + DELIMITER + WITHOUT_APP + DELIMITER + std::to_string(userId) + DELIMITER; +} + +ErrCode DistributedPreferences::SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + std::string key; + GetEnabledWithoutApp(userId, key); + + if (!database_->PutToDistributedDB(key, std::to_string(enabled))) { + ANS_LOGE("put to distributed DB failed. key:%{public}s", key.c_str()); + return ERR_ANS_DISTRIBUTED_OPERATION_FAILED; + } + + preferencesInfo_->SetSyncEnabledWithoutApp(userId, enabled); + + return ERR_OK; +} + +ErrCode DistributedPreferences::GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + return preferencesInfo_->GetSyncEnabledWithoutApp(userId, enabled); +} } // namespace Notification } // namespace OHOS \ No newline at end of file diff --git a/services/distributed/src/distributed_preferences_info.cpp b/services/distributed/src/distributed_preferences_info.cpp index 1e43052f77fe5df326a6da43c43817b565f2d19f..2bd254bad6145824438485acac53b7cb48f2c258 100644 --- a/services/distributed/src/distributed_preferences_info.cpp +++ b/services/distributed/src/distributed_preferences_info.cpp @@ -55,5 +55,22 @@ void DistributedPreferencesInfo::DeleteDistributedBundleInfo(const std::string & { bundleEnable_.erase(std::make_pair(bundleName, uid)); } + +void DistributedPreferencesInfo::SetSyncEnabledWithoutApp(const int32_t userId, const bool enabled) +{ + enabledWithoutApp_[userId] = enabled; +} + +ErrCode DistributedPreferencesInfo::GetSyncEnabledWithoutApp(const int32_t userId, bool &enabled) +{ + auto iter = enabledWithoutApp_.find(userId); + if (iter == enabledWithoutApp_.end()) { + ANS_LOGW("userId(%{public}d) not found.", userId); + return ERR_ANS_INVALID_PARAM; + } + + enabled = iter->second; + return ERR_OK; +} } // namespace Notification } // namespace OHOS \ No newline at end of file