1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "notification_manager_impl.h" 17 #include "notification_utils.h" 18 #include "inner_errors.h" 19 #include "notification_enable.h" 20 #include "notification_manager_log.h" 21 #include "pixel_map_impl.h" 22 23 namespace OHOS { 24 namespace CJSystemapi { 25 26 using namespace OHOS::Notification; 27 using namespace OHOS::CJSystemapi::Notification; 28 ParseParameters(CNotificationRequestV2 params,NotificationRequest & request)29 static bool ParseParameters(CNotificationRequestV2 params, NotificationRequest &request) 30 { 31 if (!GetNotificationRequestByNumberV2(params, request)) { 32 return false; 33 } 34 35 if (!GetNotificationRequestByStringV2(params, request)) { 36 return false; 37 } 38 39 if (!GetNotificationRequestByBoolV2(params, request)) { 40 return false; 41 } 42 43 if (!GetNotificationRequestByCustomV2(params, request)) { 44 return false; 45 } 46 return true; 47 } 48 ParseBundleOption(CNotificationBundleOptionV2 & option,NotificationBundleOption & bundleOption)49 static bool ParseBundleOption(CNotificationBundleOptionV2 &option, NotificationBundleOption &bundleOption) 50 { 51 char bundle[STR_MAX_SIZE] = {0}; 52 if (strcpy_s(bundle, STR_MAX_SIZE, option.bundle) != EOK) { 53 return false; 54 } 55 bundleOption.SetBundleName(std::string(bundle)); 56 int32_t uid = option.uid; 57 bundleOption.SetUid(uid); 58 return true; 59 } 60 Publish(CNotificationRequestV2 cjRequest)61 int NotificationManagerImplV2::Publish(CNotificationRequestV2 cjRequest) 62 { 63 NotificationRequest request; 64 if (!ParseParameters(cjRequest, request)) { 65 return ERROR_PARAM_INVALID; 66 } 67 int code = NotificationHelper::PublishNotification(request); 68 return ErrorToExternal(code); 69 } 70 Cancel(int32_t id,const char * label)71 int NotificationManagerImplV2::Cancel(int32_t id, const char* label) 72 { 73 int code = NotificationHelper::CancelNotification(label, id); 74 return ErrorToExternal(code); 75 } 76 CancelAll()77 int NotificationManagerImplV2::CancelAll() 78 { 79 int code = NotificationHelper::CancelAllNotifications(); 80 return ErrorToExternal(code); 81 } 82 AddSlot(int32_t type)83 int NotificationManagerImplV2::AddSlot(int32_t type) 84 { 85 NotificationConstant::SlotType slot = NotificationConstant::SlotType::OTHER; 86 if (!SlotTypeCJToCV2(SlotTypeV2(type), slot)) { 87 return ERROR_PARAM_INVALID; 88 } 89 int code = NotificationHelper::AddSlotByType(slot); 90 return ErrorToExternal(code); 91 } 92 GetSlot(int32_t type,int32_t & errCode)93 CNotificationSlotV2 NotificationManagerImplV2::GetSlot(int32_t type, int32_t &errCode) 94 { 95 CNotificationSlotV2 notificationSlot = { 96 .notificationType = 0, 97 .level = 0, 98 .desc = NULL, 99 .badgeFlag = false, 100 .bypassDnd = false, 101 .lockscreenVisibility = 0, 102 .vibrationEnabled = false, 103 .sound = NULL, 104 .lightEnabled = false, 105 .lightColor = 0, 106 .vibrationValues = { .head = NULL, .size = 0 }, 107 .enabled = false 108 }; 109 NotificationConstant::SlotType slotType = NotificationConstant::SlotType::OTHER; 110 if (!SlotTypeCJToCV2(SlotTypeV2(type), slotType)) { 111 errCode = ERROR_PARAM_INVALID; 112 return notificationSlot; 113 } 114 115 sptr<NotificationSlot> slot = nullptr; 116 errCode = ErrorToExternal(NotificationHelper::GetNotificationSlot(slotType, slot)); 117 if (slot != nullptr && !SetNotificationSlotV2(*slot, notificationSlot)) { 118 errCode = ERROR_PARAM_INVALID; 119 } 120 return notificationSlot; 121 } 122 GetSlots(int32_t & errCode)123 CArrayNotificationSlotsV2 NotificationManagerImplV2::GetSlots(int32_t &errCode) 124 { 125 CArrayNotificationSlotsV2 notificationSlots = { .head = nullptr, .size = 0 }; 126 std::vector<sptr<NotificationSlot>> slots; 127 errCode = ErrorToExternal(NotificationHelper::GetNotificationSlots(slots)); 128 CNotificationSlotV2* head = 129 reinterpret_cast<CNotificationSlotV2 *>(malloc(sizeof(CNotificationSlotV2) * slots.size())); 130 if (head == nullptr) { 131 LOGE("null head"); 132 return notificationSlots; 133 } 134 int32_t count = 0; 135 for (auto vec : slots) { 136 if (!vec) { 137 LOGE("Invalidated NotificationSlot object ptr."); 138 continue; 139 } 140 if (!SetNotificationSlotV2(*vec, head[count])) { 141 LOGE("null SetNotificationSlotV2"); 142 continue; 143 } 144 count++; 145 } 146 notificationSlots.size = static_cast<int64_t>(slots.size()); 147 notificationSlots.head = head; 148 return notificationSlots; 149 } 150 RemoveSlot(int32_t type)151 int NotificationManagerImplV2::RemoveSlot(int32_t type) 152 { 153 NotificationConstant::SlotType slot = NotificationConstant::SlotType::OTHER; 154 if (!SlotTypeCJToCV2(SlotTypeV2(type), slot)) { 155 return ERROR_PARAM_INVALID; 156 } 157 int code = NotificationHelper::RemoveNotificationSlot(slot); 158 return ErrorToExternal(code); 159 } 160 RemoveAllSlots()161 int NotificationManagerImplV2::RemoveAllSlots() 162 { 163 int code = NotificationHelper::RemoveAllSlots(); 164 return ErrorToExternal(code); 165 } 166 GetActiveNotificationCount()167 RetDataUI32 NotificationManagerImplV2::GetActiveNotificationCount() 168 { 169 RetDataUI32 ret = { .code = 0, .data = 0 }; 170 uint64_t num = 0; 171 int code = NotificationHelper::GetActiveNotificationNums(num); 172 ret.code = static_cast<uint32_t>(ErrorToExternal(code)); 173 ret.data = static_cast<uint32_t>(num); 174 return ret; 175 } 176 GetActiveNotifications(int32_t & errCode)177 CArrayNotificationRequestV2 NotificationManagerImplV2::GetActiveNotifications(int32_t &errCode) 178 { 179 CArrayNotificationRequestV2 notificationRequests = { .head = nullptr, .size = 0 }; 180 std::vector<sptr<NotificationRequest>> requests; 181 int code = NotificationHelper::GetActiveNotifications(requests); 182 errCode = ErrorToExternal(code); 183 if (code != ERR_OK) { 184 return notificationRequests; 185 } 186 CNotificationRequestV2** head = 187 reinterpret_cast<CNotificationRequestV2 **>(malloc(sizeof(CNotificationRequestV2*) * requests.size())); 188 if (head == nullptr) { 189 return notificationRequests; 190 } 191 notificationRequests.size = static_cast<int64_t>(requests.size()); 192 int32_t count = 0; 193 for (auto vec : requests) { 194 if (!vec) { 195 LOGI("Invalid NotificationRequest object ptr"); 196 continue; 197 } 198 head[count] = reinterpret_cast<CNotificationRequestV2 *>(malloc(sizeof(CNotificationRequestV2))); 199 if (head[count] == nullptr) { 200 LOGE("null head[count]"); 201 for (int32_t i = 0 ; i < count; i++) { 202 free(head[i]); 203 } 204 free(head); 205 head = nullptr; 206 break; 207 } 208 if (!SetNotificationRequestV2(vec.GetRefPtr(), *(head[count++]))) { 209 LOGI("Set NotificationRequest object failed"); 210 continue; 211 } 212 } 213 notificationRequests.head = head; 214 return notificationRequests; 215 } 216 CancelGroup(const char * cGroupName)217 int NotificationManagerImplV2::CancelGroup(const char* cGroupName) 218 { 219 std::string groupName(cGroupName); 220 int code = NotificationHelper::CancelGroup(groupName); 221 return ErrorToExternal(code); 222 } 223 IsSupportTemplate(const char * cTemplateName)224 RetDataBool NotificationManagerImplV2::IsSupportTemplate(const char* cTemplateName) 225 { 226 RetDataBool ret = { .code = 0, .data = false }; 227 std::string templateName(cTemplateName); 228 bool isSupport = false; 229 int code = NotificationHelper::IsSupportTemplate(templateName, isSupport); 230 ret.code = ErrorToExternal(code); 231 ret.data = isSupport; 232 return ret; 233 } 234 SetNotificationEnable(CNotificationBundleOptionV2 option,bool enable)235 int NotificationManagerImplV2::SetNotificationEnable(CNotificationBundleOptionV2 option, bool enable) 236 { 237 NotificationBundleOption bundleOption; 238 if (!ParseBundleOption(option, bundleOption)) { 239 return ERROR_PARAM_INVALID; 240 } 241 std::string deviceId {""}; 242 int code = NotificationHelper::SetNotificationsEnabledForSpecifiedBundle(bundleOption, deviceId, enable); 243 return ErrorToExternal(code); 244 } 245 DisplayBadge(CNotificationBundleOptionV2 option,bool enable)246 int NotificationManagerImplV2::DisplayBadge(CNotificationBundleOptionV2 option, bool enable) 247 { 248 NotificationBundleOption bundleOption; 249 if (!ParseBundleOption(option, bundleOption)) { 250 return ERROR_PARAM_INVALID; 251 } 252 int code = NotificationHelper::SetShowBadgeEnabledForBundle(bundleOption, enable); 253 return ErrorToExternal(code); 254 } 255 IsBadgeDisplayed(CNotificationBundleOptionV2 option)256 RetDataBool NotificationManagerImplV2::IsBadgeDisplayed(CNotificationBundleOptionV2 option) 257 { 258 NotificationBundleOption bundleOption; 259 RetDataBool ret = { .code = 0, .data = false }; 260 if (!ParseBundleOption(option, bundleOption)) { 261 ret.code = ERROR_PARAM_INVALID; 262 return ret; 263 } 264 bool enabled = false; 265 int code = NotificationHelper::GetShowBadgeEnabledForBundle(bundleOption, enabled); 266 ret.code = ErrorToExternal(code); 267 ret.data = enabled; 268 return ret; 269 } 270 SetSlotFlagsByBundle(CNotificationBundleOptionV2 option,int32_t slotFlags)271 int NotificationManagerImplV2::SetSlotFlagsByBundle(CNotificationBundleOptionV2 option, int32_t slotFlags) 272 { 273 NotificationBundleOption bundleOption; 274 if (!ParseBundleOption(option, bundleOption)) { 275 return ERROR_PARAM_INVALID; 276 } 277 int code = NotificationHelper::SetNotificationSlotFlagsAsBundle(bundleOption, slotFlags); 278 return ErrorToExternal(code); 279 } 280 GetSlotFlagsByBundle(CNotificationBundleOptionV2 option)281 RetDataUI32 NotificationManagerImplV2::GetSlotFlagsByBundle(CNotificationBundleOptionV2 option) 282 { 283 RetDataUI32 ret = { .code = 0, .data = 0 }; 284 uint32_t slotFlags = 0; 285 NotificationBundleOption bundleOption; 286 if (!ParseBundleOption(option, bundleOption)) { 287 ret.code = ERROR_PARAM_INVALID; 288 return ret; 289 } 290 int code = NotificationHelper::GetNotificationSlotFlagsAsBundle(bundleOption, slotFlags); 291 ret.code = static_cast<uint32_t>(ErrorToExternal(code)); 292 ret.data = slotFlags; 293 return ret; 294 } 295 GetSlotNumByBundle(CNotificationBundleOptionV2 option)296 RetDataUI32 NotificationManagerImplV2::GetSlotNumByBundle(CNotificationBundleOptionV2 option) 297 { 298 RetDataUI32 ret = { .code = 0, .data = 0 }; 299 uint64_t num = 0; 300 NotificationBundleOption bundleOption; 301 if (!ParseBundleOption(option, bundleOption)) { 302 ret.code = ERROR_PARAM_INVALID; 303 return ret; 304 } 305 int code = NotificationHelper::GetNotificationSlotNumAsBundle(bundleOption, num); 306 ret.code = static_cast<uint32_t>(ErrorToExternal(code)); 307 ret.data = static_cast<uint32_t>(num); 308 return ret; 309 } 310 RemoveGroupByBundle(CNotificationBundleOptionV2 option,const char * cGroupName)311 int NotificationManagerImplV2::RemoveGroupByBundle(CNotificationBundleOptionV2 option, const char* cGroupName) 312 { 313 NotificationBundleOption bundleOption; 314 if (!ParseBundleOption(option, bundleOption)) { 315 return ERROR_PARAM_INVALID; 316 } 317 std::string groupName(cGroupName); 318 int code = NotificationHelper::RemoveGroupByBundle(bundleOption, groupName); 319 return ErrorToExternal(code); 320 } 321 IsNotificationEnabled()322 RetDataBool NotificationManagerImplV2::IsNotificationEnabled() 323 { 324 RetDataBool ret = { .code = EINVAL, .data = false }; 325 IsEnableParams params {}; 326 bool allowed = false; 327 int errorCode; 328 if (params.hasBundleOption) { 329 LOGI("option.bundle : %{public}s option.uid : %{public}d", 330 params.option.GetBundleName().c_str(), 331 params.option.GetUid()); 332 errorCode = NotificationHelper::IsAllowedNotify(params.option, allowed); 333 } else if (params.hasUserId) { 334 LOGI("userId : %{public}d", params.userId); 335 errorCode = NotificationHelper::IsAllowedNotify(params.userId, allowed); 336 } else { 337 errorCode = NotificationHelper::IsAllowedNotifySelf(allowed); 338 } 339 ret.code = ErrorToExternal(errorCode); 340 ret.data = allowed; 341 LOGI("errorCode : %{public}d, allowed : %{public}d", 342 errorCode, allowed); 343 return ret; 344 } 345 SetBadgeNumber(int32_t badgeNumber)346 int NotificationManagerImplV2::SetBadgeNumber(int32_t badgeNumber) 347 { 348 int code = NotificationHelper::SetBadgeNumber(badgeNumber); 349 return ErrorToExternal(code); 350 } 351 RequestEnableNotification()352 int NotificationManagerImplV2::RequestEnableNotification() 353 { 354 IsEnableParams params {}; 355 std::string deviceId {""}; 356 sptr<AnsDialogHostClient> client = nullptr; 357 if (!AnsDialogHostClient::CreateIfNullptr(client)) { 358 LOGI("dialog is popping %{public}d.", ERR_ANS_DIALOG_IS_POPPING) 359 return ErrorToExternal(ERR_ANS_DIALOG_IS_POPPING); 360 } 361 int code = NotificationHelper::RequestEnableNotification(deviceId, client, params.callerToken); 362 LOGI("done, code is %{public}d.", code) 363 return ErrorToExternal(code); 364 } 365 RequestEnableNotificationWithContext(sptr<AbilityRuntime::CJAbilityContext> context)366 int NotificationManagerImplV2::RequestEnableNotificationWithContext(sptr<AbilityRuntime::CJAbilityContext> context) 367 { 368 IsEnableParams params {}; 369 sptr<IRemoteObject> callerToken = context->GetToken(); 370 params.callerToken = callerToken; 371 sptr<AnsDialogHostClient> client = nullptr; 372 params.hasCallerToken = true; 373 std::string deviceId {""}; 374 if (!AnsDialogHostClient::CreateIfNullptr(client)) { 375 LOGI("dialog is popping %{public}d.", ERR_ANS_DIALOG_IS_POPPING) 376 return ErrorToExternal(ERR_ANS_DIALOG_IS_POPPING); 377 } 378 int code = NotificationHelper::RequestEnableNotification(deviceId, client, params.callerToken); 379 LOGI("done, code is %{public}d.", code) 380 return ErrorToExternal(code); 381 } 382 IsDistributedEnabled()383 RetDataBool NotificationManagerImplV2::IsDistributedEnabled() 384 { 385 RetDataBool ret = { .code = EINVAL, .data = false }; 386 bool enable = false; 387 int code = NotificationHelper::IsDistributedEnabled(enable); 388 LOGI("IsDistributedEnabled enable = %{public}d", enable); 389 ret.code = code; 390 ret.data = enable; 391 return ret; 392 } 393 } // CJSystemapi 394 } // namespace OHOS