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 "request_box.h"
17
18 #include "ans_image_util.h"
19 #include "ans_log_wrapper.h"
20 #include "distributed_local_config.h"
21
22 namespace OHOS {
23 namespace Notification {
24
NotifticationRequestBox()25 NotifticationRequestBox::NotifticationRequestBox()
26 {
27 if (box_ == nullptr) {
28 return;
29 }
30 box_->SetMessageType(PUBLISH_NOTIFICATION);
31 }
32
NotifticationRequestBox(std::shared_ptr<TlvBox> box)33 NotifticationRequestBox::NotifticationRequestBox(std::shared_ptr<TlvBox> box) : BoxBase(box)
34 {
35 }
36
SetNotificationHashCode(const std::string & hasdCode)37 bool NotifticationRequestBox::SetNotificationHashCode(const std::string& hasdCode)
38 {
39 if (box_ == nullptr) {
40 return false;
41 }
42 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_HASHCODE, hasdCode));
43 }
44
SetSlotType(int32_t type)45 bool NotifticationRequestBox::SetSlotType(int32_t type)
46 {
47 if (box_ == nullptr) {
48 return false;
49 }
50 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_SLOT_TYPE, type));
51 }
52
SetContentType(int32_t type)53 bool NotifticationRequestBox::SetContentType(int32_t type)
54 {
55 if (box_ == nullptr) {
56 return false;
57 }
58 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_CONTENT_TYPE, type));
59 }
60
SetReminderFlag(int32_t flag)61 bool NotifticationRequestBox::SetReminderFlag(int32_t flag)
62 {
63 if (box_ == nullptr) {
64 return false;
65 }
66 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_REMINDERFLAG, flag));
67 }
68
SetCreatorBundleName(const std::string & bundleName)69 bool NotifticationRequestBox::SetCreatorBundleName(const std::string& bundleName)
70 {
71 if (box_ == nullptr) {
72 return false;
73 }
74 return box_->PutValue(std::make_shared<TlvItem>(BUNDLE_NAME, bundleName));
75 }
76
SetNotificationTitle(const std::string & title)77 bool NotifticationRequestBox::SetNotificationTitle(const std::string& title)
78 {
79 if (box_ == nullptr) {
80 return false;
81 }
82 uint32_t maxLength = static_cast<uint32_t>(DistributedLocalConfig::GetInstance().GetTitleLength());
83 if (title.size() > maxLength) {
84 ANS_LOGI("SetNotificationTitle truncate %{public}d %{public}d", (int32_t)(title.size()), (int32_t)(maxLength));
85 std::string subTitle = title.substr(0, maxLength);
86 ANS_LOGI("SetNotificationTitle truncate %{public}s %{public}s", subTitle.c_str(), title.c_str());
87 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_TITLE, subTitle));
88 }
89 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_TITLE, title));
90 }
91
SetNotificationText(const std::string & text)92 bool NotifticationRequestBox::SetNotificationText(const std::string& text)
93 {
94 if (box_ == nullptr) {
95 return false;
96 }
97 uint32_t maxLength = static_cast<uint32_t>(DistributedLocalConfig::GetInstance().GetContentLength());
98 if (text.size() > maxLength) {
99 ANS_LOGI("SetNotificationText truncate %{public}d %{public}d", (int32_t)(text.size()), (int32_t)(maxLength));
100 std::string subText = text.substr(0, maxLength);
101 ANS_LOGI("SetNotificationTitle truncate %{public}s %{public}s", subText.c_str(), text.c_str());
102 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_CONTENT, subText));
103 }
104 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_CONTENT, text));
105 }
106
SetNotificationAdditionalText(const std::string & text)107 bool NotifticationRequestBox::SetNotificationAdditionalText(const std::string& text)
108 {
109 if (box_ == nullptr) {
110 return false;
111 }
112 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_ADDITIONAL_TEXT, text));
113 }
114
SetNotificationBriefText(const std::string & text)115 bool NotifticationRequestBox::SetNotificationBriefText(const std::string& text)
116 {
117 if (box_ == nullptr) {
118 return false;
119 }
120 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_BRIEF_TEXT, text));
121 }
122
SetNotificationExpandedTitle(const std::string & text)123 bool NotifticationRequestBox::SetNotificationExpandedTitle(const std::string& text)
124 {
125 if (box_ == nullptr) {
126 return false;
127 }
128 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_EXPANDED_TITLE, text));
129 }
130
SetNotificationLongText(const std::string & text)131 bool NotifticationRequestBox::SetNotificationLongText(const std::string& text)
132 {
133 if (box_ == nullptr) {
134 return false;
135 }
136 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_LONG_TITLE, text));
137 }
138
SetNotificationAllLines(const std::vector<std::string> & allLines)139 bool NotifticationRequestBox::SetNotificationAllLines(const std::vector<std::string>& allLines)
140 {
141 return true;
142 }
143
SetNotificationBigPicture(const std::shared_ptr<Media::PixelMap> & bigPicture)144 bool NotifticationRequestBox::SetNotificationBigPicture(const std::shared_ptr<Media::PixelMap>& bigPicture)
145 {
146 return true;
147 }
148
SetNotificationActionName(const std::string & actionName)149 bool NotifticationRequestBox::SetNotificationActionName(const std::string& actionName)
150 {
151 if (box_ == nullptr) {
152 return false;
153 }
154 return box_->PutValue(std::make_shared<TlvItem>(ACTION_BUTTON_NAME, actionName));
155 }
156
SetNotificationUserInput(const std::string & userInput)157 bool NotifticationRequestBox::SetNotificationUserInput(const std::string& userInput)
158 {
159 if (box_ == nullptr) {
160 return false;
161 }
162 return box_->PutValue(std::make_shared<TlvItem>(ACTION_USER_INPUT, userInput));
163 }
164
SetBigIcon(const std::shared_ptr<Media::PixelMap> & bigIcon)165 bool NotifticationRequestBox::SetBigIcon(const std::shared_ptr<Media::PixelMap>& bigIcon)
166 {
167 if (box_ == nullptr) {
168 return false;
169 }
170
171 std::string copyIcon = AnsImageUtil::PackImage(bigIcon);
172 auto copyPixelMap = AnsImageUtil::UnPackImage(copyIcon);
173 if (!AnsImageUtil::ImageScale(copyPixelMap, DEFAULT_ICON_WITHE, DEFAULT_ICON_HEIGHT)) {
174 return false;
175 }
176 std::string icon = AnsImageUtil::PackImage(copyPixelMap);
177 ANS_LOGI("SetBigIcon %{public}d, %{public}d", (int32_t)(copyIcon.size()), (int32_t)(icon.size()));
178 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_BIG_ICON, icon));
179 }
180
SetOverlayIcon(const std::shared_ptr<Media::PixelMap> & overlayIcon)181 bool NotifticationRequestBox::SetOverlayIcon(const std::shared_ptr<Media::PixelMap>& overlayIcon)
182 {
183 if (box_ == nullptr) {
184 return false;
185 }
186 std::string copyIcon = AnsImageUtil::PackImage(overlayIcon);
187 auto copyPixelMap = AnsImageUtil::UnPackImage(copyIcon);
188 if (!AnsImageUtil::ImageScale(copyPixelMap, DEFAULT_ICON_WITHE, DEFAULT_ICON_HEIGHT)) {
189 return false;
190 }
191 std::string icon = AnsImageUtil::PackImage(copyPixelMap);
192 ANS_LOGI("SetOverlayIcon %{public}d, %{public}d", (int32_t)(copyIcon.size()), (int32_t)(icon.size()));
193 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_OVERLAY_ICON, icon));
194 }
195
SetCommonLiveView(const std::vector<uint8_t> & byteSequence)196 bool NotifticationRequestBox::SetCommonLiveView(const std::vector<uint8_t>& byteSequence)
197 {
198 if (box_ == nullptr) {
199 return false;
200 }
201 const unsigned char* begin = byteSequence.data();
202 return box_->PutValue(std::make_shared<TlvItem>(NOTIFICATION_COMMON_LIVEVIEW,
203 begin, byteSequence.size()));
204 }
205
SetFinishTime(int64_t time)206 bool NotifticationRequestBox::SetFinishTime(int64_t time)
207 {
208 if (box_ == nullptr) {
209 return false;
210 }
211 return box_->PutValue(std::make_shared<TlvItem>(FINISH_DEADLINE_TIME, time));
212 }
213
SetAutoDeleteTime(int64_t time)214 bool NotifticationRequestBox::SetAutoDeleteTime(int64_t time)
215 {
216 if (box_ == nullptr) {
217 return false;
218 }
219 return box_->PutValue(std::make_shared<TlvItem>(AUTO_DELETE_TIME, time));
220 }
221
GetNotificationHashCode(std::string & hasdCode) const222 bool NotifticationRequestBox::GetNotificationHashCode(std::string& hasdCode) const
223 {
224 if (box_ == nullptr) {
225 return false;
226 }
227 return box_->GetStringValue(NOTIFICATION_HASHCODE, hasdCode);
228 }
229
GetSlotType(int32_t & type) const230 bool NotifticationRequestBox::GetSlotType(int32_t& type) const
231 {
232 if (box_ == nullptr) {
233 return false;
234 }
235 return box_->GetInt32Value(NOTIFICATION_SLOT_TYPE, type);
236 }
237
GetContentType(int32_t & type) const238 bool NotifticationRequestBox::GetContentType(int32_t& type) const
239 {
240 if (box_ == nullptr) {
241 return false;
242 }
243 return box_->GetInt32Value(NOTIFICATION_CONTENT_TYPE, type);
244 }
245
GetCreatorBundleName(std::string & bundleName) const246 bool NotifticationRequestBox::GetCreatorBundleName(std::string& bundleName) const
247 {
248 if (box_ == nullptr) {
249 return false;
250 }
251 return box_->GetStringValue(BUNDLE_NAME, bundleName);
252 }
253
GetReminderFlag(int32_t & flag) const254 bool NotifticationRequestBox::GetReminderFlag(int32_t& flag) const
255 {
256 if (box_ == nullptr) {
257 return false;
258 }
259 return box_->GetInt32Value(NOTIFICATION_REMINDERFLAG, flag);
260 }
261
GetNotificationTitle(std::string & title) const262 bool NotifticationRequestBox::GetNotificationTitle(std::string& title) const
263 {
264 if (box_ == nullptr) {
265 return false;
266 }
267 return box_->GetStringValue(NOTIFICATION_TITLE, title);
268 }
269
GetNotificationText(std::string & text) const270 bool NotifticationRequestBox::GetNotificationText(std::string& text) const
271 {
272 if (box_ == nullptr) {
273 return false;
274 }
275 return box_->GetStringValue(NOTIFICATION_CONTENT, text);
276 }
277
GetNotificationAdditionalText(std::string & text) const278 bool NotifticationRequestBox::GetNotificationAdditionalText(std::string& text) const
279 {
280 if (box_ == nullptr) {
281 return false;
282 }
283 return box_->GetStringValue(NOTIFICATION_ADDITIONAL_TEXT, text);
284 }
285
GetNotificationBriefText(std::string & text) const286 bool NotifticationRequestBox::GetNotificationBriefText(std::string& text) const
287 {
288 if (box_ == nullptr) {
289 return false;
290 }
291 return box_->GetStringValue(NOTIFICATION_BRIEF_TEXT, text);
292 }
293
GetNotificationExpandedTitle(std::string & text) const294 bool NotifticationRequestBox::GetNotificationExpandedTitle(std::string& text) const
295 {
296 if (box_ == nullptr) {
297 return false;
298 }
299 return box_->GetStringValue(NOTIFICATION_EXPANDED_TITLE, text);
300 }
301
GetNotificationLongText(std::string & text) const302 bool NotifticationRequestBox::GetNotificationLongText(std::string& text) const
303 {
304 if (box_ == nullptr) {
305 return false;
306 }
307 return box_->GetStringValue(NOTIFICATION_LONG_TITLE, text);
308 }
309
GetNotificationAllLines(std::vector<std::string> & allLines) const310 bool NotifticationRequestBox::GetNotificationAllLines(std::vector<std::string>& allLines) const
311 {
312 return true;
313 }
314
GetNotificationBigPicture(std::shared_ptr<Media::PixelMap> & bigPicture) const315 bool NotifticationRequestBox::GetNotificationBigPicture(std::shared_ptr<Media::PixelMap>& bigPicture) const
316 {
317 return true;
318 }
319
GetNotificationActionName(std::string & actionName) const320 bool NotifticationRequestBox::GetNotificationActionName(std::string& actionName) const
321 {
322 if (box_ == nullptr) {
323 return false;
324 }
325 return box_->GetStringValue(ACTION_BUTTON_NAME, actionName);
326 }
327
GetNotificationUserInput(std::string & userInput) const328 bool NotifticationRequestBox::GetNotificationUserInput(std::string& userInput) const
329 {
330 if (box_ == nullptr) {
331 return false;
332 }
333 return box_->GetStringValue(ACTION_USER_INPUT, userInput);
334 }
335
GetBigIcon(std::shared_ptr<Media::PixelMap> & bigIcon) const336 bool NotifticationRequestBox::GetBigIcon(std::shared_ptr<Media::PixelMap>& bigIcon) const
337 {
338 if (box_ == nullptr) {
339 return false;
340 }
341 std::string bigIconContent;
342 if (!box_->GetStringValue(NOTIFICATION_BIG_ICON, bigIconContent)) {
343 return false;
344 }
345 ANS_LOGI("GetBigIcon %{public}d", (int32_t)(bigIconContent.size()));
346 bigIcon = AnsImageUtil::UnPackImage(bigIconContent);
347 return true;
348 }
349
GetOverlayIcon(std::shared_ptr<Media::PixelMap> & overlayIcon) const350 bool NotifticationRequestBox::GetOverlayIcon(std::shared_ptr<Media::PixelMap>& overlayIcon) const
351 {
352 if (box_ == nullptr) {
353 return false;
354 }
355 std::string overlayContent;
356 if (!box_->GetStringValue(NOTIFICATION_OVERLAY_ICON, overlayContent)) {
357 return false;
358 }
359 ANS_LOGI("GetOverlayIcon %{public}d", (int32_t)(overlayContent.size()));
360 overlayIcon = AnsImageUtil::UnPackImage(overlayContent);
361 return true;
362 }
363
GetCommonLiveView(std::vector<uint8_t> & byteSequence) const364 bool NotifticationRequestBox::GetCommonLiveView(std::vector<uint8_t>& byteSequence) const
365 {
366 if (box_ == nullptr) {
367 return false;
368 }
369 return box_->GetBytes(NOTIFICATION_COMMON_LIVEVIEW, byteSequence);
370 }
371
GetFinishTime(int64_t & time) const372 bool NotifticationRequestBox::GetFinishTime(int64_t& time) const
373 {
374 if (box_ == nullptr) {
375 return false;
376 }
377 return box_->GetInt64Value(FINISH_DEADLINE_TIME, time);
378 }
379
GetAutoDeleteTime(int64_t & time) const380 bool NotifticationRequestBox::GetAutoDeleteTime(int64_t& time) const
381 {
382 if (box_ == nullptr) {
383 return false;
384 }
385 return box_->GetInt64Value(AUTO_DELETE_TIME, time);
386 }
387 }
388 }
389