1 /*
2 * Copyright (c) 2021 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 "ohos/aafwk/content/intent.h"
17 #include "ohos/aafwk/base/array_wrapper.h"
18 #include "ohos/aafwk/base/base_object.h"
19 #include "ohos/aafwk/base/bool_wrapper.h"
20 #include "ohos/aafwk/base/zchar_wrapper.h"
21 #include "ohos/aafwk/base/byte_wrapper.h"
22 #include "ohos/aafwk/base/short_wrapper.h"
23 #include "ohos/aafwk/base/int_wrapper.h"
24 #include "ohos/aafwk/base/long_wrapper.h"
25 #include "ohos/aafwk/base/float_wrapper.h"
26 #include "ohos/aafwk/base/double_wrapper.h"
27 #include "ohos/aafwk/base/string_wrapper.h"
28 #include "ohos/aafwk/base/zchar_wrapper.h"
29 #include "string_ex.h"
30
31 #include <climits>
32 #include <securec.h>
33
34 #include "parcel.h"
35
36 using OHOS::AppExecFwk::ElementName;
37
38 namespace OHOS {
39 namespace AAFwk {
40
41 const std::string Intent::ACTION_PLAY("action.system.play");
42
43 const std::string Intent::ENTITY_HOME("entity.system.home");
44 const std::string Intent::ENTITY_VIDEO("entity.system.video");
45
46 const std::string Intent::OCT_EQUALSTO("075"); // '='
47 const std::string Intent::OCT_SEMICOLON("073"); // ';'
48
49 static constexpr int HEX_STRING_BUF_LEN = 12;
50 static constexpr int HEX_STRING_LEN = 10; // "0xffffffff"
51
Intent()52 Intent::Intent() : flags_(0)
53 {}
54
GetAction() const55 std::string Intent::GetAction() const
56 {
57 return action_;
58 }
59
SetAction(const std::string & action)60 Intent &Intent::SetAction(const std::string &action)
61 {
62 action_ = action;
63 return *this;
64 }
65
GetEntity() const66 std::string Intent::GetEntity() const
67 {
68 return entity_;
69 }
70
SetEntity(const std::string & entity)71 Intent &Intent::SetEntity(const std::string &entity)
72 {
73 entity_ = entity;
74 return *this;
75 }
76
GetFlags() const77 unsigned int Intent::GetFlags() const
78 {
79 return flags_;
80 }
81
SetFlag(const unsigned int flag)82 Intent &Intent::SetFlag(const unsigned int flag)
83 {
84 flags_ = flag;
85 return *this;
86 }
87
AddFlag(const unsigned int flag)88 Intent &Intent::AddFlag(const unsigned int flag)
89 {
90 flags_ |= flag;
91 return *this;
92 }
93
RemoveFlags(const unsigned int flag)94 void Intent::RemoveFlags(const unsigned int flag)
95 {
96 flags_ &= ~flag;
97 }
98
GetElement() const99 ElementName Intent::GetElement() const
100 {
101 return element_;
102 }
103
SetElement(const ElementName & element)104 Intent &Intent::SetElement(const ElementName &element)
105 {
106 element_ = element;
107 return *this;
108 }
109
ParseFlag(const std::string & content,Intent & intent)110 bool Intent::ParseFlag(const std::string &content, Intent &intent)
111 {
112 std::string contentLower = LowerStr(content);
113 std::string prefix = "0x";
114 if (!contentLower.empty()) {
115 if (contentLower.find(prefix) != 0) {
116 return false;
117 }
118
119 for (std::size_t i = prefix.length(); i < contentLower.length(); i++) {
120 if (!isxdigit(contentLower[i])) {
121 return false;
122 }
123 }
124 int base = 16; // hex string
125 unsigned int flag = (unsigned int)std::stoi(contentLower, nullptr, base);
126 intent.SetFlag(flag);
127 }
128 return true;
129 }
130
Decode(const std::string & str)131 std::string Intent::Decode(const std::string &str)
132 {
133 std::string decode;
134
135 for (std::size_t i = 0; i < str.length();) {
136 if (str[i] == '\\') {
137 if (++i >= str.length()) {
138 decode += "\\";
139 break;
140 }
141 if (str[i] == '\\') {
142 decode += "\\";
143 i++;
144 } else if (str[i] == '0') {
145 if (str.compare(i, OCT_EQUALSTO.length(), OCT_EQUALSTO) == 0) {
146 decode += "=";
147 i += OCT_EQUALSTO.length();
148 } else if (str.compare(i, OCT_SEMICOLON.length(), OCT_SEMICOLON) == 0) {
149 decode += ";";
150 i += OCT_SEMICOLON.length();
151 } else {
152 decode += "\\" + str.substr(i, 1);
153 i++;
154 }
155 } else {
156 decode += "\\" + str.substr(i, 1);
157 i++;
158 }
159 } else {
160 decode += str[i];
161 i++;
162 }
163 }
164
165 return decode;
166 }
167
Encode(const std::string & str)168 std::string Intent::Encode(const std::string &str)
169 {
170 std::string encode;
171
172 for (std::size_t i = 0; i < str.length(); i++) {
173 if (str[i] == '\\') {
174 encode += "\\\\";
175 } else if (str[i] == '=') {
176 encode += "\\" + OCT_EQUALSTO;
177 } else if (str[i] == ';') {
178 encode += "\\" + OCT_SEMICOLON;
179 } else {
180 encode += str[i];
181 }
182 }
183
184 return encode;
185 }
186
ParseContent(const std::string & content,std::string & prop,std::string & value)187 bool Intent::ParseContent(const std::string &content, std::string &prop, std::string &value)
188 {
189 std::string subString;
190 std::size_t pos = content.find("=");
191 if (pos != std::string::npos) {
192 subString = content.substr(0, pos);
193 prop = Decode(subString);
194 subString = content.substr(pos + 1, content.length() - pos - 1);
195 value = Decode(subString);
196 return true;
197 }
198 return false;
199 }
200
ParseUriInternal(const std::string & content,ElementName & element,Intent & intent)201 bool Intent::ParseUriInternal(const std::string &content, ElementName &element, Intent &intent)
202 {
203 static constexpr int TYPE_TAG_SIZE = 2;
204
205 std::string prop;
206 std::string value;
207
208 if (content.empty() || content[0] == '=') {
209 return true;
210 }
211
212 if (!ParseContent(content, prop, value)) {
213 return false;
214 }
215
216 if (value.empty()) {
217 return true;
218 }
219
220 if (prop == "action") {
221 intent.SetAction(value);
222 } else if (prop == "entity") {
223 intent.SetEntity(value);
224 } else if (prop == "flag") {
225 if (!ParseFlag(value, intent)) {
226 return false;
227 }
228 } else if (prop == "device") {
229 element.SetDeviceID(value);
230 } else if (prop == "bundle") {
231 element.SetBundleName(value);
232 } else if (prop == "ability") {
233 element.SetAbilityName(value);
234 } else if (prop.length() > TYPE_TAG_SIZE) {
235 std::string key = prop.substr(TYPE_TAG_SIZE);
236 if (prop[0] == String::SIGNATURE && prop[1] == '.') {
237 sptr<IString> valueObj = String::Parse(value);
238 if (valueObj == nullptr) {
239 return false;
240 }
241 intent.parameters_.SetParam(key, valueObj);
242 } else if (prop[0] == Boolean::SIGNATURE && prop[1] == '.') {
243 sptr<IBoolean> valueObj = Boolean::Parse(value);
244 if (valueObj == nullptr) {
245 return false;
246 }
247 intent.parameters_.SetParam(key, valueObj);
248 } else if (prop[0] == Char::SIGNATURE && prop[1] == '.') {
249 sptr<IChar> valueObj = Char::Parse(value);
250 if (valueObj == nullptr) {
251 return false;
252 }
253 intent.parameters_.SetParam(key, valueObj);
254 } else if (prop[0] == Byte::SIGNATURE && prop[1] == '.') {
255 sptr<IByte> valueObj = Byte::Parse(value);
256 if (valueObj == nullptr) {
257 return false;
258 }
259 intent.parameters_.SetParam(key, valueObj);
260 } else if (prop[0] == Short::SIGNATURE && prop[1] == '.') {
261 sptr<IShort> valueObj = Short::Parse(value);
262 if (valueObj == nullptr) {
263 return false;
264 }
265 intent.parameters_.SetParam(key, valueObj);
266 } else if (prop[0] == Integer::SIGNATURE && prop[1] == '.') {
267 sptr<IInteger> valueObj = Integer::Parse(value);
268 if (valueObj == nullptr) {
269 return false;
270 }
271 intent.parameters_.SetParam(key, valueObj);
272 } else if (prop[0] == Long::SIGNATURE && prop[1] == '.') {
273 sptr<ILong> valueObj = Long::Parse(value);
274 if (valueObj == nullptr) {
275 return false;
276 }
277 intent.parameters_.SetParam(key, valueObj);
278 } else if (prop[0] == Float::SIGNATURE && prop[1] == '.') {
279 sptr<IFloat> valueObj = Float::Parse(value);
280 if (valueObj == nullptr) {
281 return false;
282 }
283 intent.parameters_.SetParam(key, valueObj);
284 } else if (prop[0] == Double::SIGNATURE && prop[1] == '.') {
285 sptr<IDouble> valueObj = Double::Parse(value);
286 if (valueObj == nullptr) {
287 return false;
288 }
289 intent.parameters_.SetParam(key, valueObj);
290 } else if (prop[0] == Array::SIGNATURE && prop[1] == '.') {
291 sptr<IArray> valueObj = Array::Parse(value);
292 if (valueObj == nullptr) {
293 return false;
294 }
295 intent.parameters_.SetParam(key, valueObj);
296 }
297 }
298
299 return true;
300 }
301
ParseUri(const std::string & uri)302 Intent *Intent::ParseUri(const std::string &uri)
303 {
304 if (uri.length() <= 0) {
305 return nullptr;
306 }
307
308 std::string head = "#Intent;";
309 std::string end = ";end";
310
311 if (uri.find(head) != 0) {
312 return nullptr;
313 }
314
315 if (uri.rfind(end) != (uri.length() - end.length())) {
316 return nullptr;
317 }
318
319 bool ret = true;
320 std::string content;
321 std::size_t pos;
322 std::size_t begin = head.length();
323 ElementName element;
324 Intent *intent = new Intent();
325
326 pos = uri.find_first_of(";", begin);
327 do {
328 if (pos != std::string::npos) {
329 content = uri.substr(begin, pos - begin);
330 ret = ParseUriInternal(content, element, *intent);
331 if (!ret) {
332 break;
333 }
334 begin = pos + 1;
335 pos = uri.find(";", begin);
336 if (pos == std::string::npos) {
337 break;
338 }
339 } else {
340 break;
341 }
342 } while (true);
343
344 if (ret) {
345 intent->SetElement(element);
346 } else {
347 delete intent;
348 intent = nullptr;
349 }
350
351 return intent;
352 }
353
ToUri()354 std::string Intent::ToUri()
355 {
356 std::string uriString = "#Intent;";
357
358 if (action_.length() > 0) {
359 uriString += "action=" + Encode(action_) + ";";
360 }
361
362 if (entity_.length() > 0) {
363 uriString += "entity=" + Encode(entity_) + ";";
364 }
365
366 if (flags_ != 0) {
367 uriString += "flag=";
368 char buf[HEX_STRING_BUF_LEN];
369 int len = snprintf_s(buf, sizeof(buf), HEX_STRING_LEN, "0x%08x", flags_);
370 if (len == HEX_STRING_LEN) {
371 std::string flag = buf;
372 uriString += Encode(flag);
373 uriString += ";";
374 }
375 }
376
377 std::string device = element_.GetDeviceID();
378 if (device.length() > 0) {
379 uriString += "device=" + Encode(device) + ";";
380 }
381
382 std::string bundle = element_.GetBundleName();
383 if (bundle.length() > 0) {
384 uriString += "bundle=" + Encode(bundle) + ";";
385 }
386
387 std::string ability = element_.GetAbilityName();
388 if (ability.length() > 0) {
389 uriString += "ability=" + Encode(ability) + ";";
390 }
391
392 auto params = parameters_.GetParams();
393 auto iter = params.cbegin();
394 while (iter != params.cend()) {
395 sptr<IInterface> o = iter->second;
396 if (IString::Query(o) != nullptr) {
397 uriString += String::SIGNATURE;
398 } else if (IBoolean::Query(o) != nullptr) {
399 uriString += Boolean::SIGNATURE;
400 } else if (IChar::Query(o) != nullptr) {
401 uriString += Char::SIGNATURE;
402 } else if (IByte::Query(o) != nullptr) {
403 uriString += Byte::SIGNATURE;
404 } else if (IShort::Query(o) != nullptr) {
405 uriString += Short::SIGNATURE;
406 } else if (IInteger::Query(o) != nullptr) {
407 uriString += Integer::SIGNATURE;
408 } else if (ILong::Query(o) != nullptr) {
409 uriString += Long::SIGNATURE;
410 } else if (IFloat::Query(o) != nullptr) {
411 uriString += Float::SIGNATURE;
412 } else if (IDouble::Query(o) != nullptr) {
413 uriString += Double::SIGNATURE;
414 } else if (IArray::Query(o) != nullptr) {
415 uriString += Array::SIGNATURE;
416 }
417 uriString += "." + Encode(iter->first) + "=" + Encode(Object::ToString(*(o.GetRefPtr()))) + ";";
418 iter++;
419 }
420
421 uriString += "end";
422
423 return uriString;
424 }
425
426 /*
427 * Intent format in Parcel. Java side should keep consistent.
428 * +----------+----+-----+------+-------+----+-----------+----
429 * | Action | E1 | Uri | Type | Flags | E2 | Category1 | ...
430 * +----------+----+-----+------+-------+----+-----------+----
431 * ----+-----------+---------+----+---------+----+------------+
432 * ... | CategoryN | Package | E3 | Element | E4 | Parameters |
433 * ----+-----------+---------+----+---------+----+------------+
434 * E1: If -1, no Uri after it.
435 * E2: Category count. If -1, no Category after it.
436 * E3: if -1, no element after it.
437 * E4: if -1, no parameters after it.
438 */
Marshalling(Parcel & parcel) const439 bool Intent::Marshalling(Parcel &parcel) const
440 {
441 // write action
442 if (!parcel.WriteString16(Str8ToStr16(action_))) {
443 return false;
444 }
445
446 // write entity
447 if (!parcel.WriteString16(Str8ToStr16(entity_))) {
448 return false;
449 }
450
451 // write flags
452 if (!parcel.WriteUint32(flags_)) {
453 return false;
454 }
455
456 // write element
457 ElementName emptyElement;
458 if (element_ == emptyElement) {
459 if (!parcel.WriteInt32(VALUE_NULL)) {
460 return false;
461 }
462 } else {
463 if (!parcel.WriteInt32(VALUE_OBJECT)) {
464 return false;
465 }
466 if (!parcel.WriteParcelable(&element_)) {
467 return false;
468 }
469 }
470
471 // write parameters
472 if (parameters_.GetParams().size() == 0) {
473 if (!parcel.WriteInt32(VALUE_NULL)) {
474 return false;
475 }
476 } else {
477 if (!parcel.WriteInt32(VALUE_OBJECT)) {
478 return false;
479 }
480 if (!parcel.WriteParcelable(¶meters_)) {
481 return false;
482 }
483 }
484
485 return true;
486 }
487
ReadFromParcel(Parcel & parcel)488 bool Intent::ReadFromParcel(Parcel &parcel)
489 {
490 // read action
491 action_ = Str16ToStr8(parcel.ReadString16());
492
493 // read entity
494 entity_ = Str16ToStr8(parcel.ReadString16());
495
496 // read flags
497 if (!parcel.ReadUint32(flags_)) {
498 return false;
499 }
500
501 int empty;
502 // read element
503 empty = VALUE_NULL;
504 if (!parcel.ReadInt32(empty)) {
505 return false;
506 }
507
508 if (empty == VALUE_OBJECT) {
509 auto element = parcel.ReadParcelable<ElementName>();
510 if (element != nullptr) {
511 element_ = *element;
512 delete element;
513 } else {
514 return false;
515 }
516 }
517
518 // read parameters
519 empty = VALUE_NULL;
520 if (!parcel.ReadInt32(empty)) {
521 return false;
522 }
523
524 if (empty == VALUE_OBJECT) {
525 auto params = parcel.ReadParcelable<IntentParams>();
526 if (params != nullptr) {
527 parameters_ = *params;
528 delete params;
529 } else {
530 return false;
531 }
532 }
533
534 return true;
535 }
536
Unmarshalling(Parcel & parcel)537 Intent *Intent::Unmarshalling(Parcel &parcel)
538 {
539 Intent *intent = new Intent();
540 if (intent && !intent->ReadFromParcel(parcel)) {
541 delete intent;
542 intent = nullptr;
543 }
544 return intent;
545 }
546
HasParameter(const std::string & key) const547 bool Intent::HasParameter(const std::string &key) const
548 {
549 return parameters_.HasParam(key);
550 }
551
GetBoolParam(const std::string & key,const bool defaultValue)552 bool Intent::GetBoolParam(const std::string &key, const bool defaultValue)
553 {
554 auto value = parameters_.GetParam(key);
555 IBoolean *bo = IBoolean::Query(value);
556 if (bo != nullptr) {
557 return Boolean::Unbox(bo);
558 }
559 return defaultValue;
560 }
561
SetBoolParam(const std::string & key,const bool value)562 Intent &Intent::SetBoolParam(const std::string &key, const bool value)
563 {
564 parameters_.SetParam(key, Boolean::Box(value));
565 return *this;
566 }
567
GetBoolArrayParam(const std::string & key)568 std::vector<bool> Intent::GetBoolArrayParam(const std::string &key)
569 {
570 std::vector<bool> array;
571 auto value = parameters_.GetParam(key);
572 IArray *ao = IArray::Query(value);
573 if (ao != nullptr && Array::IsBooleanArray(ao)) {
574 auto func = [&](IInterface *object) { array.push_back(Boolean::Unbox(IBoolean::Query(object))); };
575 Array::ForEach(ao, func);
576 }
577 return array;
578 }
579
SetBoolArrayParam(const std::string & key,const std::vector<bool> & value)580 Intent &Intent::SetBoolArrayParam(const std::string &key, const std::vector<bool> &value)
581 {
582 size_t size = value.size();
583 sptr<IArray> ao = new Array(size, g_IID_IBoolean);
584 for (size_t i = 0; i < size; i++) {
585 ao->Set(i, Boolean::Box(value[i]));
586 }
587 parameters_.SetParam(key, ao);
588 return *this;
589 }
590
SetCharParam(const std::string & key,const zchar value)591 Intent &Intent::SetCharParam(const std::string &key, const zchar value)
592 {
593 parameters_.SetParam(key, Char::Box(value));
594 return *this;
595 }
596
SetCharArrayParam(const std::string & key,const std::vector<zchar> & value)597 Intent &Intent::SetCharArrayParam(const std::string &key, const std::vector<zchar> &value)
598 {
599 size_t size = value.size();
600 sptr<IArray> ao = new Array(size, g_IID_IChar);
601 for (size_t i = 0; i < size; i++) {
602 ao->Set(i, Char::Box(value[i]));
603 }
604 parameters_.SetParam(key, ao);
605 return *this;
606 }
607
GetCharParam(const std::string & key,const zchar defaultValue)608 zchar Intent::GetCharParam(const std::string &key, const zchar defaultValue)
609 {
610 auto value = parameters_.GetParam(key);
611 IChar *co = IChar::Query(value);
612 if (co != nullptr) {
613 return Char::Unbox(co);
614 }
615 return defaultValue;
616 }
617
GetCharArrayParam(const std::string & key)618 std::vector<zchar> Intent::GetCharArrayParam(const std::string &key)
619 {
620 std::vector<zchar> array;
621 auto value = parameters_.GetParam(key);
622 IArray *ao = IArray::Query(value);
623 if (ao != nullptr && Array::IsCharArray(ao)) {
624 auto func = [&](IInterface *object) { array.push_back(Char::Unbox(IChar::Query(object))); };
625 Array::ForEach(ao, func);
626 }
627 return array;
628 }
629
SetByteParam(const std::string & key,const byte value)630 Intent &Intent::SetByteParam(const std::string &key, const byte value)
631 {
632 parameters_.SetParam(key, Byte::Box(value));
633 return *this;
634 }
635
SetByteArrayParam(const std::string & key,const std::vector<byte> & value)636 Intent &Intent::SetByteArrayParam(const std::string &key, const std::vector<byte> &value)
637 {
638 size_t size = value.size();
639 sptr<IArray> ao = new Array(size, g_IID_IByte);
640 for (size_t i = 0; i < size; i++) {
641 ao->Set(i, Byte::Box(value[i]));
642 }
643 parameters_.SetParam(key, ao);
644 return *this;
645 }
646
GetByteParam(const std::string & key,const byte defaultValue)647 byte Intent::GetByteParam(const std::string &key, const byte defaultValue)
648 {
649 auto value = parameters_.GetParam(key);
650 IByte *bo = IByte::Query(value);
651 if (bo != nullptr) {
652 return Byte::Unbox(bo);
653 }
654 return defaultValue;
655 }
656
GetByteArrayParam(const std::string & key)657 std::vector<byte> Intent::GetByteArrayParam(const std::string &key)
658 {
659 std::vector<byte> array;
660 auto value = parameters_.GetParam(key);
661 IArray *ao = IArray::Query(value);
662 if (ao != nullptr && Array::IsByteArray(ao)) {
663 auto func = [&](IInterface *object) { array.push_back(Byte::Unbox(IByte::Query(object))); };
664 Array::ForEach(ao, func);
665 }
666 return array;
667 }
668
SetShortParam(const std::string & key,const short value)669 Intent &Intent::SetShortParam(const std::string &key, const short value)
670 {
671 parameters_.SetParam(key, Short::Box(value));
672 return *this;
673 }
674
SetShortArrayParam(const std::string & key,const std::vector<short> & value)675 Intent &Intent::SetShortArrayParam(const std::string &key, const std::vector<short> &value)
676 {
677 size_t size = value.size();
678 sptr<IArray> ao = new Array(size, g_IID_IShort);
679 for (size_t i = 0; i < size; i++) {
680 ao->Set(i, Short::Box(value[i]));
681 }
682 parameters_.SetParam(key, ao);
683 return *this;
684 }
685
GetShortParam(const std::string & key,const short defaultValue)686 short Intent::GetShortParam(const std::string &key, const short defaultValue)
687 {
688 auto value = parameters_.GetParam(key);
689 IShort *so = IShort::Query(value);
690 if (so != nullptr) {
691 return Short::Unbox(so);
692 }
693 return defaultValue;
694 }
695
GetShortArrayParam(const std::string & key)696 std::vector<short> Intent::GetShortArrayParam(const std::string &key)
697 {
698 std::vector<short> array;
699 auto value = parameters_.GetParam(key);
700 IArray *ao = IArray::Query(value);
701 if (ao != nullptr && Array::IsShortArray(ao)) {
702 auto func = [&](IInterface *object) { array.push_back(Short::Unbox(IShort::Query(object))); };
703 Array::ForEach(ao, func);
704 }
705 return array;
706 }
707
SetIntParam(const std::string & key,const int value)708 Intent &Intent::SetIntParam(const std::string &key, const int value)
709 {
710 parameters_.SetParam(key, Integer::Box(value));
711 return *this;
712 }
713
SetIntArrayParam(const std::string & key,const std::vector<int> & value)714 Intent &Intent::SetIntArrayParam(const std::string &key, const std::vector<int> &value)
715 {
716 size_t size = value.size();
717 sptr<IArray> ao = new Array(size, g_IID_IInteger);
718 for (size_t i = 0; i < size; i++) {
719 ao->Set(i, Integer::Box(value[i]));
720 }
721 parameters_.SetParam(key, ao);
722 return *this;
723 }
724
GetIntParam(const std::string & key,const int defaultValue)725 int Intent::GetIntParam(const std::string &key, const int defaultValue)
726 {
727 auto value = parameters_.GetParam(key);
728 IInteger *io = IInteger::Query(value);
729 if (io != nullptr) {
730 return Integer::Unbox(io);
731 }
732 return defaultValue;
733 }
734
GetIntArrayParam(const std::string & key)735 std::vector<int> Intent::GetIntArrayParam(const std::string &key)
736 {
737 std::vector<int> array;
738 auto value = parameters_.GetParam(key);
739 IArray *ao = IArray::Query(value);
740 if (ao != nullptr && Array::IsIntegerArray(ao)) {
741 auto func = [&](IInterface *object) { array.push_back(Integer::Unbox(IInteger::Query(object))); };
742 Array::ForEach(ao, func);
743 }
744 return array;
745 }
746
GetLongParam(const std::string & key,const long defaultValue)747 long Intent::GetLongParam(const std::string &key, const long defaultValue)
748 {
749 auto value = parameters_.GetParam(key);
750 ILong *lo = ILong::Query(value);
751 if (lo != nullptr) {
752 return Long::Unbox(lo);
753 }
754 return defaultValue;
755 }
756
SetLongParam(const std::string & key,const long value)757 Intent &Intent::SetLongParam(const std::string &key, const long value)
758 {
759 parameters_.SetParam(key, Long::Box(value));
760 return *this;
761 }
762
GetLongArrayParam(const std::string & key)763 std::vector<long> Intent::GetLongArrayParam(const std::string &key)
764 {
765 std::vector<long> array;
766 auto value = parameters_.GetParam(key);
767 IArray *ao = IArray::Query(value);
768 if (ao != nullptr && Array::IsLongArray(ao)) {
769 auto func = [&](IInterface *object) { array.push_back(Long::Unbox(ILong::Query(object))); };
770 Array::ForEach(ao, func);
771 }
772 return array;
773 }
774
SetLongArrayParam(const std::string & key,const std::vector<long> & value)775 Intent &Intent::SetLongArrayParam(const std::string &key, const std::vector<long> &value)
776 {
777 size_t size = value.size();
778 sptr<IArray> ao = new Array(size, g_IID_ILong);
779 for (size_t i = 0; i < size; i++) {
780 ao->Set(i, Long::Box(value[i]));
781 }
782 parameters_.SetParam(key, ao);
783 return *this;
784 }
785
GetFloatParam(const std::string & key,const float defaultValue)786 float Intent::GetFloatParam(const std::string &key, const float defaultValue)
787 {
788 auto value = parameters_.GetParam(key);
789 IFloat *of = IFloat::Query(value);
790 if (of != nullptr) {
791 return Float::Unbox(of);
792 }
793 return defaultValue;
794 }
795
SetFloatParam(const std::string & key,const float value)796 Intent &Intent::SetFloatParam(const std::string &key, const float value)
797 {
798 parameters_.SetParam(key, Float::Box(value));
799 return *this;
800 }
801
GetFloatArrayParam(const std::string & key)802 std::vector<float> Intent::GetFloatArrayParam(const std::string &key)
803 {
804 std::vector<float> array;
805 auto value = parameters_.GetParam(key);
806 IArray *ao = IArray::Query(value);
807 if (ao != nullptr && Array::IsFloatArray(ao)) {
808 auto func = [&](IInterface *object) { array.push_back(Float::Unbox(IFloat::Query(object))); };
809 Array::ForEach(ao, func);
810 }
811 return array;
812 }
813
SetFloatArrayParam(const std::string & key,const std::vector<float> & value)814 Intent &Intent::SetFloatArrayParam(const std::string &key, const std::vector<float> &value)
815 {
816 size_t size = value.size();
817 sptr<IArray> ao = new Array(size, g_IID_IFloat);
818 for (size_t i = 0; i < size; i++) {
819 ao->Set(i, Float::Box(value[i]));
820 }
821 parameters_.SetParam(key, ao);
822 return *this;
823 }
824
GetDoubleParam(const std::string & key,const double defaultValue)825 double Intent::GetDoubleParam(const std::string &key, const double defaultValue)
826 {
827 auto value = parameters_.GetParam(key);
828 IDouble *dbo = IDouble::Query(value);
829 if (dbo != nullptr) {
830 return Double::Unbox(dbo);
831 }
832 return defaultValue;
833 }
834
SetDoubleParam(const std::string & key,const double value)835 Intent &Intent::SetDoubleParam(const std::string &key, const double value)
836 {
837 parameters_.SetParam(key, Double::Box(value));
838 return *this;
839 }
840
GetDoubleArrayParam(const std::string & key)841 std::vector<double> Intent::GetDoubleArrayParam(const std::string &key)
842 {
843 std::vector<double> array;
844 auto value = parameters_.GetParam(key);
845 IArray *ao = IArray::Query(value);
846 if (ao != nullptr && Array::IsDoubleArray(ao)) {
847 auto func = [&](IInterface *object) { array.push_back(Double::Unbox(IDouble::Query(object))); };
848 Array::ForEach(ao, func);
849 }
850 return array;
851 }
852
SetDoubleArrayParam(const std::string & key,const std::vector<double> & value)853 Intent &Intent::SetDoubleArrayParam(const std::string &key, const std::vector<double> &value)
854 {
855 size_t size = value.size();
856 sptr<IArray> ao = new Array(size, g_IID_IDouble);
857 for (size_t i = 0; i < size; i++) {
858 ao->Set(i, Double::Box(value[i]));
859 }
860 parameters_.SetParam(key, ao);
861 return *this;
862 }
863
GetStringParam(const std::string & key)864 std::string Intent::GetStringParam(const std::string &key)
865 {
866 auto value = parameters_.GetParam(key);
867 IString *so = IString::Query(value);
868 if (so != nullptr) {
869 return String::Unbox(so);
870 }
871 return std::string();
872 }
873
SetStringParam(const std::string & key,const std::string & value)874 Intent &Intent::SetStringParam(const std::string &key, const std::string &value)
875 {
876 parameters_.SetParam(key, String::Box(value));
877 return *this;
878 }
879
GetStringArrayParam(const std::string & key)880 std::vector<std::string> Intent::GetStringArrayParam(const std::string &key)
881 {
882 std::vector<std::string> array;
883 auto value = parameters_.GetParam(key);
884 IArray *ao = IArray::Query(value);
885 if (ao != nullptr && Array::IsStringArray(ao)) {
886 auto func = [&](IInterface *object) { array.push_back(String::Unbox(IString::Query(object))); };
887 Array::ForEach(ao, func);
888 }
889 return array;
890 }
891
SetStringArrayParam(const std::string & key,const std::vector<std::string> & value)892 Intent &Intent::SetStringArrayParam(const std::string &key, const std::vector<std::string> &value)
893 {
894 size_t size = value.size();
895 sptr<IArray> ao = new Array(size, g_IID_IString);
896 for (size_t i = 0; i < size; i++) {
897 ao->Set(i, String::Box(value[i]));
898 }
899 parameters_.SetParam(key, ao);
900 return *this;
901 }
902
903 } // namespace AAFwk
904 } // namespace OHOS
905