1 /*
2 * Copyright (c) 2023-2023 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 "common.h"
17 #include "ans_inner_errors.h"
18 #include "ans_log_wrapper.h"
19 #include "notification_constant.h"
20
21 namespace OHOS {
22 namespace NotificationNapi {
ContentTypeJSToC(const ContentType & inType,NotificationContent::Type & outType)23 bool Common::ContentTypeJSToC(const ContentType &inType, NotificationContent::Type &outType)
24 {
25 switch (inType) {
26 case ContentType::NOTIFICATION_CONTENT_BASIC_TEXT:
27 outType = NotificationContent::Type::BASIC_TEXT;
28 break;
29 case ContentType::NOTIFICATION_CONTENT_LONG_TEXT:
30 outType = NotificationContent::Type::LONG_TEXT;
31 break;
32 case ContentType::NOTIFICATION_CONTENT_MULTILINE:
33 outType = NotificationContent::Type::MULTILINE;
34 break;
35 case ContentType::NOTIFICATION_CONTENT_PICTURE:
36 outType = NotificationContent::Type::PICTURE;
37 break;
38 case ContentType::NOTIFICATION_CONTENT_CONVERSATION:
39 outType = NotificationContent::Type::CONVERSATION;
40 break;
41 case ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW:
42 outType = NotificationContent::Type::LOCAL_LIVE_VIEW;
43 break;
44 case ContentType::NOTIFICATION_CONTENT_LIVE_VIEW:
45 outType = NotificationContent::Type::LIVE_VIEW;
46 break;
47 default:
48 ANS_LOGE("ContentType %{public}d is an invalid value", inType);
49 return false;
50 }
51 return true;
52 }
53
ContentTypeCToJS(const NotificationContent::Type & inType,ContentType & outType)54 bool Common::ContentTypeCToJS(const NotificationContent::Type &inType, ContentType &outType)
55 {
56 switch (inType) {
57 case NotificationContent::Type::BASIC_TEXT:
58 outType = ContentType::NOTIFICATION_CONTENT_BASIC_TEXT;
59 break;
60 case NotificationContent::Type::LONG_TEXT:
61 outType = ContentType::NOTIFICATION_CONTENT_LONG_TEXT;
62 break;
63 case NotificationContent::Type::MULTILINE:
64 outType = ContentType::NOTIFICATION_CONTENT_MULTILINE;
65 break;
66 case NotificationContent::Type::PICTURE:
67 outType = ContentType::NOTIFICATION_CONTENT_PICTURE;
68 break;
69 case NotificationContent::Type::CONVERSATION:
70 outType = ContentType::NOTIFICATION_CONTENT_CONVERSATION;
71 break;
72 case NotificationContent::Type::LOCAL_LIVE_VIEW:
73 outType = ContentType::NOTIFICATION_CONTENT_LOCAL_LIVE_VIEW;
74 break;
75 case NotificationContent::Type::LIVE_VIEW:
76 outType = ContentType::NOTIFICATION_CONTENT_LIVE_VIEW;
77 break;
78 default:
79 ANS_LOGE("ContentType %{public}d is an invalid value", inType);
80 return false;
81 }
82 return true;
83 }
84
SlotTypeJSToC(const SlotType & inType,NotificationConstant::SlotType & outType)85 bool Common::SlotTypeJSToC(const SlotType &inType, NotificationConstant::SlotType &outType)
86 {
87 switch (inType) {
88 case SlotType::SOCIAL_COMMUNICATION:
89 outType = NotificationConstant::SlotType::SOCIAL_COMMUNICATION;
90 break;
91 case SlotType::SERVICE_INFORMATION:
92 outType = NotificationConstant::SlotType::SERVICE_REMINDER;
93 break;
94 case SlotType::CONTENT_INFORMATION:
95 outType = NotificationConstant::SlotType::CONTENT_INFORMATION;
96 break;
97 case SlotType::LIVE_VIEW:
98 outType = NotificationConstant::SlotType::LIVE_VIEW;
99 break;
100 case SlotType::CUSTOMER_SERVICE:
101 outType = NotificationConstant::SlotType::CUSTOMER_SERVICE;
102 break;
103 case SlotType::UNKNOWN_TYPE:
104 case SlotType::OTHER_TYPES:
105 outType = NotificationConstant::SlotType::OTHER;
106 break;
107 default:
108 ANS_LOGE("SlotType %{public}d is an invalid value", inType);
109 return false;
110 }
111 return true;
112 }
113
SlotTypeCToJS(const NotificationConstant::SlotType & inType,SlotType & outType)114 bool Common::SlotTypeCToJS(const NotificationConstant::SlotType &inType, SlotType &outType)
115 {
116 switch (inType) {
117 case NotificationConstant::SlotType::CUSTOM:
118 outType = SlotType::UNKNOWN_TYPE;
119 break;
120 case NotificationConstant::SlotType::SOCIAL_COMMUNICATION:
121 outType = SlotType::SOCIAL_COMMUNICATION;
122 break;
123 case NotificationConstant::SlotType::SERVICE_REMINDER:
124 outType = SlotType::SERVICE_INFORMATION;
125 break;
126 case NotificationConstant::SlotType::CONTENT_INFORMATION:
127 outType = SlotType::CONTENT_INFORMATION;
128 break;
129 case NotificationConstant::SlotType::LIVE_VIEW:
130 outType = SlotType::LIVE_VIEW;
131 break;
132 case NotificationConstant::SlotType::CUSTOMER_SERVICE:
133 outType = SlotType::CUSTOMER_SERVICE;
134 break;
135 case NotificationConstant::SlotType::OTHER:
136 outType = SlotType::OTHER_TYPES;
137 break;
138 default:
139 ANS_LOGE("SlotType %{public}d is an invalid value", inType);
140 return false;
141 }
142 return true;
143 }
144
SlotLevelJSToC(const SlotLevel & inLevel,NotificationSlot::NotificationLevel & outLevel)145 bool Common::SlotLevelJSToC(const SlotLevel &inLevel, NotificationSlot::NotificationLevel &outLevel)
146 {
147 switch (inLevel) {
148 case SlotLevel::LEVEL_NONE:
149 outLevel = NotificationSlot::NotificationLevel::LEVEL_NONE;
150 break;
151 case SlotLevel::LEVEL_MIN:
152 outLevel = NotificationSlot::NotificationLevel::LEVEL_MIN;
153 break;
154 case SlotLevel::LEVEL_LOW:
155 outLevel = NotificationSlot::NotificationLevel::LEVEL_LOW;
156 break;
157 case SlotLevel::LEVEL_DEFAULT:
158 outLevel = NotificationSlot::NotificationLevel::LEVEL_DEFAULT;
159 break;
160 case SlotLevel::LEVEL_HIGH:
161 outLevel = NotificationSlot::NotificationLevel::LEVEL_HIGH;
162 break;
163 default:
164 ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
165 return false;
166 }
167 return true;
168 }
169
LiveViewStatusJSToC(const LiveViewStatus & inType,NotificationLiveViewContent::LiveViewStatus & outType)170 bool Common::LiveViewStatusJSToC(const LiveViewStatus &inType, NotificationLiveViewContent::LiveViewStatus &outType)
171 {
172 switch (inType) {
173 case LiveViewStatus::LIVE_VIEW_CREATE:
174 outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE;
175 break;
176 case LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE:
177 outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE;
178 break;
179 case LiveViewStatus::LIVE_VIEW_END:
180 outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END;
181 break;
182 case LiveViewStatus::LIVE_VIEW_FULL_UPDATE:
183 outType = NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE;
184 break;
185 default:
186 ANS_LOGE("LiveViewStatus %{public}d is an invalid value", inType);
187 return false;
188 }
189
190 return true;
191 }
192
SlotLevelCToJS(const NotificationSlot::NotificationLevel & inLevel,SlotLevel & outLevel)193 bool Common::SlotLevelCToJS(const NotificationSlot::NotificationLevel &inLevel, SlotLevel &outLevel)
194 {
195 switch (inLevel) {
196 case NotificationSlot::NotificationLevel::LEVEL_NONE:
197 case NotificationSlot::NotificationLevel::LEVEL_UNDEFINED:
198 outLevel = SlotLevel::LEVEL_NONE;
199 break;
200 case NotificationSlot::NotificationLevel::LEVEL_MIN:
201 outLevel = SlotLevel::LEVEL_MIN;
202 break;
203 case NotificationSlot::NotificationLevel::LEVEL_LOW:
204 outLevel = SlotLevel::LEVEL_LOW;
205 break;
206 case NotificationSlot::NotificationLevel::LEVEL_DEFAULT:
207 outLevel = SlotLevel::LEVEL_DEFAULT;
208 break;
209 case NotificationSlot::NotificationLevel::LEVEL_HIGH:
210 outLevel = SlotLevel::LEVEL_HIGH;
211 break;
212 default:
213 ANS_LOGE("SlotLevel %{public}d is an invalid value", inLevel);
214 return false;
215 }
216 return true;
217 }
218
ReasonCToJS(const int & inType,int & outType)219 bool Common::ReasonCToJS(const int &inType, int &outType)
220 {
221 switch (inType) {
222 case NotificationConstant::CLICK_REASON_DELETE:
223 outType = static_cast<int32_t>(RemoveReason::CLICK_REASON_REMOVE);
224 break;
225 case NotificationConstant::CANCEL_REASON_DELETE:
226 outType = static_cast<int32_t>(RemoveReason::CANCEL_REASON_REMOVE);
227 break;
228 case NotificationConstant::CANCEL_ALL_REASON_DELETE:
229 outType = static_cast<int32_t>(RemoveReason::CANCEL_ALL_REASON_REMOVE);
230 break;
231 case NotificationConstant::ERROR_REASON_DELETE:
232 outType = static_cast<int32_t>(RemoveReason::ERROR_REASON_REMOVE);
233 break;
234 case NotificationConstant::PACKAGE_CHANGED_REASON_DELETE:
235 outType = static_cast<int32_t>(RemoveReason::PACKAGE_CHANGED_REASON_REMOVE);
236 break;
237 case NotificationConstant::USER_STOPPED_REASON_DELETE:
238 outType = static_cast<int32_t>(RemoveReason::USER_STOPPED_REASON_REMOVE);
239 break;
240 case NotificationConstant::APP_CANCEL_REASON_DELETE:
241 outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_REASON_REMOVE);
242 break;
243 case NotificationConstant::APP_CANCEL_ALL_REASON_DELETE:
244 outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_ALL_REASON_REMOVE);
245 break;
246 case NotificationConstant::APP_CANCEL_REASON_OTHER:
247 outType = static_cast<int32_t>(RemoveReason::APP_CANCEL_REASON_OTHER);
248 break;
249 default:
250 ANS_LOGE("Reason %{public}d is an invalid value", inType);
251 return false;
252 }
253 return true;
254 }
255
DoNotDisturbTypeJSToC(const DoNotDisturbType & inType,NotificationConstant::DoNotDisturbType & outType)256 bool Common::DoNotDisturbTypeJSToC(const DoNotDisturbType &inType, NotificationConstant::DoNotDisturbType &outType)
257 {
258 switch (inType) {
259 case DoNotDisturbType::TYPE_NONE:
260 outType = NotificationConstant::DoNotDisturbType::NONE;
261 break;
262 case DoNotDisturbType::TYPE_ONCE:
263 outType = NotificationConstant::DoNotDisturbType::ONCE;
264 break;
265 case DoNotDisturbType::TYPE_DAILY:
266 outType = NotificationConstant::DoNotDisturbType::DAILY;
267 break;
268 case DoNotDisturbType::TYPE_CLEARLY:
269 outType = NotificationConstant::DoNotDisturbType::CLEARLY;
270 break;
271 default:
272 ANS_LOGE("DoNotDisturbType %{public}d is an invalid value", inType);
273 return false;
274 }
275 return true;
276 }
277
DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType & inType,DoNotDisturbType & outType)278 bool Common::DoNotDisturbTypeCToJS(const NotificationConstant::DoNotDisturbType &inType, DoNotDisturbType &outType)
279 {
280 switch (inType) {
281 case NotificationConstant::DoNotDisturbType::NONE:
282 outType = DoNotDisturbType::TYPE_NONE;
283 break;
284 case NotificationConstant::DoNotDisturbType::ONCE:
285 outType = DoNotDisturbType::TYPE_ONCE;
286 break;
287 case NotificationConstant::DoNotDisturbType::DAILY:
288 outType = DoNotDisturbType::TYPE_DAILY;
289 break;
290 case NotificationConstant::DoNotDisturbType::CLEARLY:
291 outType = DoNotDisturbType::TYPE_CLEARLY;
292 break;
293 default:
294 ANS_LOGE("DoNotDisturbType %{public}d is an invalid value", inType);
295 return false;
296 }
297 return true;
298 }
299
DeviceRemindTypeCToJS(const NotificationConstant::RemindType & inType,DeviceRemindType & outType)300 bool Common::DeviceRemindTypeCToJS(const NotificationConstant::RemindType &inType, DeviceRemindType &outType)
301 {
302 switch (inType) {
303 case NotificationConstant::RemindType::DEVICE_IDLE_DONOT_REMIND:
304 outType = DeviceRemindType::IDLE_DONOT_REMIND;
305 break;
306 case NotificationConstant::RemindType::DEVICE_IDLE_REMIND:
307 outType = DeviceRemindType::IDLE_REMIND;
308 break;
309 case NotificationConstant::RemindType::DEVICE_ACTIVE_DONOT_REMIND:
310 outType = DeviceRemindType::ACTIVE_DONOT_REMIND;
311 break;
312 case NotificationConstant::RemindType::DEVICE_ACTIVE_REMIND:
313 outType = DeviceRemindType::ACTIVE_REMIND;
314 break;
315 default:
316 ANS_LOGE("DeviceRemindType %{public}d is an invalid value", inType);
317 return false;
318 }
319 return true;
320 }
321
SourceTypeCToJS(const NotificationConstant::SourceType & inType,SourceType & outType)322 bool Common::SourceTypeCToJS(const NotificationConstant::SourceType &inType, SourceType &outType)
323 {
324 switch (inType) {
325 case NotificationConstant::SourceType::TYPE_NORMAL:
326 outType = SourceType::TYPE_NORMAL;
327 break;
328 case NotificationConstant::SourceType::TYPE_CONTINUOUS:
329 outType = SourceType::TYPE_CONTINUOUS;
330 break;
331 case NotificationConstant::SourceType::TYPE_TIMER:
332 outType = SourceType::TYPE_TIMER;
333 break;
334 default:
335 ANS_LOGE("SourceType %{public}d is an invalid value", inType);
336 return false;
337 }
338 return true;
339 }
340
LiveViewStatusCToJS(const NotificationLiveViewContent::LiveViewStatus & inType,LiveViewStatus & outType)341 bool Common::LiveViewStatusCToJS(const NotificationLiveViewContent::LiveViewStatus &inType, LiveViewStatus &outType)
342 {
343 switch (inType) {
344 case NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE:
345 outType = LiveViewStatus::LIVE_VIEW_CREATE;
346 break;
347 case NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE:
348 outType = LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE;
349 break;
350 case NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END:
351 outType = LiveViewStatus::LIVE_VIEW_END;
352 break;
353 case NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_FULL_UPDATE:
354 outType = LiveViewStatus::LIVE_VIEW_FULL_UPDATE;
355 break;
356 default:
357 ANS_LOGE("LiveViewStatus %{public}d is an invalid value", inType);
358 return false;
359 }
360
361 return true;
362 }
363 }
364 }
365