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 #include "ability_scheduler_proxy.h"
16 #include "abs_shared_result_set.h"
17 #include "data_ability_predicates.h"
18 #include "values_bucket.h"
19
20 #include "hilog_wrapper.h"
21 #include "ipc_types.h"
22 #include "ishared_result_set.h"
23 #include "pac_map.h"
24 #include "want.h"
25 #include "data_ability_observer_interface.h"
26 #include "data_ability_result.h"
27 #include "data_ability_operation.h"
28
29 namespace OHOS {
30 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)31 bool AbilitySchedulerProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33 if (!data.WriteInterfaceToken(AbilitySchedulerProxy::GetDescriptor())) {
34 HILOG_ERROR("write interface token failed");
35 return false;
36 }
37 return true;
38 }
39
ScheduleAbilityTransaction(const Want & want,const LifeCycleStateInfo & stateInfo)40 void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo)
41 {
42 MessageParcel data;
43 MessageParcel reply;
44 MessageOption option;
45 if (!WriteInterfaceToken(data)) {
46 return;
47 }
48 data.WriteParcelable(&want);
49 data.WriteParcelable(&stateInfo);
50 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, data, reply, option);
51 if (err != NO_ERROR) {
52 HILOG_ERROR("ScheduleAbilityTransaction fail to SendRequest. err: %d", err);
53 }
54 }
55
SendResult(int requestCode,int resultCode,const Want & resultWant)56 void AbilitySchedulerProxy::SendResult(int requestCode, int resultCode, const Want &resultWant)
57 {
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option;
61 if (!WriteInterfaceToken(data)) {
62 return;
63 }
64 data.WriteInt32(requestCode);
65 data.WriteInt32(resultCode);
66 if (!data.WriteParcelable(&resultWant)) {
67 HILOG_ERROR("fail to WriteParcelable");
68 return;
69 }
70 int32_t err = Remote()->SendRequest(IAbilityScheduler::SEND_RESULT, data, reply, option);
71 if (err != NO_ERROR) {
72 HILOG_ERROR("SendResult fail to SendRequest. err: %d", err);
73 }
74 }
75
ScheduleConnectAbility(const Want & want)76 void AbilitySchedulerProxy::ScheduleConnectAbility(const Want &want)
77 {
78 MessageParcel data;
79 MessageParcel reply;
80 MessageOption option;
81 if (!WriteInterfaceToken(data)) {
82 return;
83 }
84 if (!data.WriteParcelable(&want)) {
85 HILOG_ERROR("fail to WriteParcelable");
86 return;
87 }
88 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_CONNECT, data, reply, option);
89 if (err != NO_ERROR) {
90 HILOG_ERROR("ScheduleConnectAbility fail to SendRequest. err: %d", err);
91 }
92 }
93
ScheduleDisconnectAbility(const Want & want)94 void AbilitySchedulerProxy::ScheduleDisconnectAbility(const Want &want)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option;
99 if (!WriteInterfaceToken(data)) {
100 return;
101 }
102 if (!data.WriteParcelable(&want)) {
103 HILOG_ERROR("fail to WriteParcelable");
104 return;
105 }
106
107 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_DISCONNECT, data, reply, option);
108 if (err != NO_ERROR) {
109 HILOG_ERROR("ScheduleDisconnectAbility fail to SendRequest. err: %d", err);
110 }
111 }
112
ScheduleCommandAbility(const Want & want,bool restart,int startId)113 void AbilitySchedulerProxy::ScheduleCommandAbility(const Want &want, bool restart, int startId)
114 {
115 MessageParcel data;
116 MessageParcel reply;
117 MessageOption option;
118 if (!WriteInterfaceToken(data)) {
119 return;
120 }
121 if (!data.WriteParcelable(&want) && !data.WriteBool(restart) && !data.WriteInt32(startId)) {
122 HILOG_ERROR("fail to WriteParcelable");
123 return;
124 }
125
126 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND, data, reply, option);
127 if (err != NO_ERROR) {
128 HILOG_ERROR("ScheduleCommandAbility fail to SendRequest. err: %d", err);
129 }
130 }
131
ScheduleSaveAbilityState(PacMap & outState)132 void AbilitySchedulerProxy::ScheduleSaveAbilityState(PacMap &outState)
133 {
134 MessageParcel data;
135 MessageParcel reply;
136 MessageOption option;
137 if (!WriteInterfaceToken(data)) {
138 return;
139 }
140 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_SAVE_ABILITY_STATE, data, reply, option);
141 if (err != NO_ERROR) {
142 HILOG_ERROR("ScheduleSaveAbilityState fail to SendRequest. err: %d", err);
143 }
144 std::unique_ptr<PacMap> pacMap(reply.ReadParcelable<PacMap>());
145 if (!pacMap) {
146 HILOG_ERROR("readParcelableInfo<PacMap> failed");
147 return;
148 }
149 outState = *pacMap;
150 }
151
ScheduleRestoreAbilityState(const PacMap & inState)152 void AbilitySchedulerProxy::ScheduleRestoreAbilityState(const PacMap &inState)
153 {
154 MessageParcel data;
155 MessageParcel reply;
156 MessageOption option;
157 if (!WriteInterfaceToken(data)) {
158 return;
159 }
160 if (!data.WriteParcelable(&inState)) {
161 HILOG_ERROR("WriteParcelable error");
162 return;
163 }
164 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_RESTORE_ABILITY_STATE, data, reply, option);
165 if (err != NO_ERROR) {
166 HILOG_ERROR("ScheduleRestoreAbilityState fail to SendRequest. err: %d", err);
167 }
168 }
169
ScheduleUpdateConfiguration(const DummyConfiguration & config)170 void AbilitySchedulerProxy::ScheduleUpdateConfiguration(const DummyConfiguration &config)
171 {
172 MessageParcel data;
173 MessageParcel reply;
174 MessageOption option;
175 if (!WriteInterfaceToken(data)) {
176 return;
177 }
178 if (!data.WriteParcelable(&config)) {
179 HILOG_ERROR("fail to WriteParcelable");
180 return;
181 }
182 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UPDATE_CONFIGURATION, data, reply, option);
183 if (err != NO_ERROR) {
184 HILOG_ERROR("ScheduleRestoreAbilityState fail to SendRequest. err: %d", err);
185 }
186 }
187
188 /**
189 * @brief Obtains the MIME types of files supported.
190 *
191 * @param uri Indicates the path of the files to obtain.
192 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
193 *
194 * @return Returns the matched MIME types. If there is no match, null is returned.
195 */
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)196 std::vector<std::string> AbilitySchedulerProxy::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
197 {
198 std::vector<std::string> types;
199
200 MessageParcel data;
201 MessageParcel reply;
202 MessageOption option;
203
204 if (!WriteInterfaceToken(data)) {
205 return types;
206 }
207
208 if (!data.WriteParcelable(&uri)) {
209 HILOG_ERROR("fail to WriteParcelable uri");
210 return types;
211 }
212
213 if (!data.WriteString(mimeTypeFilter)) {
214 HILOG_ERROR("fail to WriteString mimeTypeFilter");
215 return types;
216 }
217
218 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_GETFILETYPES, data, reply, option);
219 if (err != NO_ERROR) {
220 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %d", err);
221 }
222
223 if (!reply.ReadStringVector(&types)) {
224 HILOG_ERROR("fail to ReadStringVector types");
225 }
226
227 return types;
228 }
229
230 /**
231 * @brief Opens a file in a specified remote path.
232 *
233 * @param uri Indicates the path of the file to open.
234 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
235 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
236 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
237 * or "rwt" for read and write access that truncates any existing file.
238 *
239 * @return Returns the file descriptor.
240 */
OpenFile(const Uri & uri,const std::string & mode)241 int AbilitySchedulerProxy::OpenFile(const Uri &uri, const std::string &mode)
242 {
243 int fd = -1;
244
245 MessageParcel data;
246 MessageParcel reply;
247 MessageOption option;
248
249 if (!WriteInterfaceToken(data)) {
250 return fd;
251 }
252
253 if (!data.WriteParcelable(&uri)) {
254 HILOG_ERROR("fail to WriteParcelable uri");
255 return fd;
256 }
257
258 if (!data.WriteString(mode)) {
259 HILOG_ERROR("fail to WriteString mode");
260 return fd;
261 }
262
263 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_OPENFILE, data, reply, option);
264 if (err != NO_ERROR) {
265 HILOG_ERROR("OpenFile fail to SendRequest. err: %d", err);
266 return err;
267 }
268
269 fd = reply.ReadFileDescriptor();
270 if (fd == -1) {
271 HILOG_ERROR("fail to ReadInt32 fd");
272 return fd;
273 }
274
275 return fd;
276 }
277
278 /**
279 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
280 * inside of their .hap.
281 *
282 * @param uri Indicates the path of the file to open.
283 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
284 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
285 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
286 * data, or "rwt" for read and write access that truncates any existing file.
287 *
288 * @return Returns the RawFileDescriptor object containing file descriptor.
289 */
OpenRawFile(const Uri & uri,const std::string & mode)290 int AbilitySchedulerProxy::OpenRawFile(const Uri &uri, const std::string &mode)
291 {
292 int fd = -1;
293
294 MessageParcel data;
295 MessageParcel reply;
296 MessageOption option;
297
298 if (!WriteInterfaceToken(data)) {
299 return fd;
300 }
301
302 if (!data.WriteParcelable(&uri)) {
303 HILOG_ERROR("fail to WriteParcelable uri");
304 return fd;
305 }
306
307 if (!data.WriteString(mode)) {
308 HILOG_ERROR("fail to WriteString mode");
309 return fd;
310 }
311
312 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_OPENRAWFILE, data, reply, option);
313 if (err != NO_ERROR) {
314 HILOG_ERROR("OpenFile fail to SendRequest. err: %d", err);
315 }
316
317 if (!reply.ReadInt32(fd)) {
318 HILOG_ERROR("fail to ReadInt32 fd");
319 return fd;
320 }
321
322 return fd;
323 }
324
325 /**
326 * @brief Inserts a single data record into the database.
327 *
328 * @param uri Indicates the path of the data to operate.
329 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
330 *
331 * @return Returns the index of the inserted data record.
332 */
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)333 int AbilitySchedulerProxy::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
334 {
335 int index = -1;
336
337 MessageParcel data;
338 MessageParcel reply;
339 MessageOption option;
340
341 if (!WriteInterfaceToken(data)) {
342 return index;
343 }
344
345 if (!data.WriteParcelable(&uri)) {
346 HILOG_ERROR("fail to WriteParcelable uri");
347 return index;
348 }
349
350 if (!data.WriteParcelable(&value)) {
351 HILOG_ERROR("fail to WriteParcelable value");
352 return index;
353 }
354
355 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_INSERT, data, reply, option);
356 if (err != NO_ERROR) {
357 HILOG_ERROR("Insert fail to SendRequest. err: %d", err);
358 return err;
359 }
360
361 if (!reply.ReadInt32(index)) {
362 HILOG_ERROR("fail to ReadInt32 index");
363 return index;
364 }
365
366 return index;
367 }
368
369 /**
370 * @brief Updates data records in the database.
371 *
372 * @param uri Indicates the path of data to update.
373 * @param value Indicates the data to update. This parameter can be null.
374 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
375 *
376 * @return Returns the number of data records updated.
377 */
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)378 int AbilitySchedulerProxy::Update(const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates)
379 {
380 int index = -1;
381
382 MessageParcel data;
383 MessageParcel reply;
384 MessageOption option;
385
386 if (!WriteInterfaceToken(data)) {
387 return index;
388 }
389
390 if (!data.WriteParcelable(&uri)) {
391 HILOG_ERROR("fail to WriteParcelable uri");
392 return index;
393 }
394
395 if (!data.WriteParcelable(&value)) {
396 HILOG_ERROR("fail to WriteParcelable value");
397 return index;
398 }
399
400 if (!data.WriteParcelable(&predicates)) {
401 HILOG_ERROR("fail to WriteParcelable predicates");
402 return index;
403 }
404
405 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UPDATE, data, reply, option);
406 if (err != NO_ERROR) {
407 HILOG_ERROR("Update fail to SendRequest. err: %d", err);
408 return err;
409 }
410
411 if (!reply.ReadInt32(index)) {
412 HILOG_ERROR("fail to ReadInt32 index");
413 return index;
414 }
415
416 return index;
417 }
418
419 /**
420 * @brief Deletes one or more data records from the database.
421 *
422 * @param uri Indicates the path of the data to operate.
423 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
424 *
425 * @return Returns the number of data records deleted.
426 */
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)427 int AbilitySchedulerProxy::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
428 {
429 int index = -1;
430
431 MessageParcel data;
432 MessageParcel reply;
433 MessageOption option;
434
435 if (!WriteInterfaceToken(data)) {
436 return index;
437 }
438
439 if (!data.WriteParcelable(&uri)) {
440 HILOG_ERROR("fail to WriteParcelable uri");
441 return index;
442 }
443
444 if (!data.WriteParcelable(&predicates)) {
445 HILOG_ERROR("fail to WriteParcelable predicates");
446 return index;
447 }
448
449 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_DELETE, data, reply, option);
450 if (err != NO_ERROR) {
451 HILOG_ERROR("Delete fail to SendRequest. err: %d", err);
452 }
453
454 if (!reply.ReadInt32(index)) {
455 HILOG_ERROR("fail to ReadInt32 index");
456 return index;
457 }
458
459 return index;
460 }
461
462 /**
463 * @brief Deletes one or more data records from the database.
464 *
465 * @param uri Indicates the path of data to query.
466 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
467 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
468 *
469 * @return Returns the query result.
470 */
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)471 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilitySchedulerProxy::Query(
472 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
473 {
474 MessageParcel data;
475 MessageParcel reply;
476 MessageOption option;
477
478 if (!WriteInterfaceToken(data)) {
479 return nullptr;
480 }
481
482 if (!data.WriteParcelable(&uri)) {
483 HILOG_ERROR("fail to WriteParcelable uri");
484 return nullptr;
485 }
486
487 if (!data.WriteStringVector(columns)) {
488 HILOG_ERROR("fail to WriteStringVector columns");
489 return nullptr;
490 }
491
492 if (!data.WriteParcelable(&predicates)) {
493 HILOG_ERROR("fail to WriteParcelable predicates");
494 return nullptr;
495 }
496
497 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_QUERY, data, reply, option);
498 if (err != NO_ERROR) {
499 HILOG_ERROR("Query fail to SendRequest. err: %d", err);
500 return nullptr;
501 }
502 return OHOS::NativeRdb::ISharedResultSet::ReadFromParcel(reply);
503 }
504
505 /**
506 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
507 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
508 *
509 * @param uri Indicates the URI of the data.
510 *
511 * @return Returns the MIME type that matches the data specified by uri.
512 */
GetType(const Uri & uri)513 std::string AbilitySchedulerProxy::GetType(const Uri &uri)
514 {
515 std::string type;
516
517 MessageParcel data;
518 MessageParcel reply;
519 MessageOption option;
520
521 if (!WriteInterfaceToken(data)) {
522 return type;
523 }
524
525 if (!data.WriteParcelable(&uri)) {
526 HILOG_ERROR("fail to WriteParcelable uri");
527 return type;
528 }
529
530 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_GETTYPE, data, reply, option);
531 if (err != NO_ERROR) {
532 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %d", err);
533 }
534
535 type = reply.ReadString();
536 if (type.empty()) {
537 HILOG_ERROR("fail to ReadString type");
538 return type;
539 }
540
541 return type;
542 }
543
544 /**
545 * @brief Reloads data in the database.
546 *
547 * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
548 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
549 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
550 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
551 *
552 * @return Returns true if the data is successfully reloaded; returns false otherwise.
553 */
Reload(const Uri & uri,const PacMap & extras)554 bool AbilitySchedulerProxy::Reload(const Uri &uri, const PacMap &extras)
555 {
556 bool ret = false;
557
558 MessageParcel data;
559 MessageParcel reply;
560 MessageOption option;
561
562 if (!WriteInterfaceToken(data)) {
563 return ret;
564 }
565
566 if (!data.WriteParcelable(&uri)) {
567 HILOG_ERROR("fail to WriteParcelable uri");
568 return ret;
569 }
570
571 if (!data.WriteParcelable(&extras)) {
572 HILOG_ERROR("fail to WriteParcelable extras");
573 return ret;
574 }
575
576 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_RELOAD, data, reply, option);
577 if (err != NO_ERROR) {
578 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %d", err);
579 }
580
581 ret = reply.ReadBool();
582 if (!ret) {
583 HILOG_ERROR("fail to ReadBool ret");
584 return ret;
585 }
586
587 return ret;
588 }
589
590 /**
591 * @brief Inserts multiple data records into the database.
592 *
593 * @param uri Indicates the path of the data to operate.
594 * @param values Indicates the data records to insert.
595 *
596 * @return Returns the number of data records inserted.
597 */
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)598 int AbilitySchedulerProxy::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
599 {
600 int ret = -1;
601
602 MessageParcel data;
603 MessageParcel reply;
604 MessageOption option;
605
606 if (!WriteInterfaceToken(data)) {
607 return ret;
608 }
609
610 if (!data.WriteParcelable(&uri)) {
611 HILOG_ERROR("fail to WriteParcelable uri");
612 return ret;
613 }
614
615 int count = values.size();
616 if (!data.WriteInt32(count)) {
617 HILOG_ERROR("fail to WriteInt32 ret");
618 return ret;
619 }
620
621 for (int i = 0; i < count; i++) {
622 if (!data.WriteParcelable(&values[i])) {
623 HILOG_ERROR("fail to WriteParcelable ret, index = %{public}d", i);
624 return ret;
625 }
626 }
627
628 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_BATCHINSERT, data, reply, option);
629 if (err != NO_ERROR) {
630 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %d", err);
631 }
632
633 if (!reply.ReadInt32(ret)) {
634 HILOG_ERROR("fail to ReadInt32 index");
635 return ret;
636 }
637
638 return ret;
639 }
640
641 /**
642 * @brief notify multi window mode changed.
643 *
644 * @param winModeKey Indicates ability Window display mode.
645 * @param flag Indicates this ability has been enter this mode.
646 */
NotifyMultiWinModeChanged(int32_t winModeKey,bool flag)647 void AbilitySchedulerProxy::NotifyMultiWinModeChanged(int32_t winModeKey, bool flag)
648 {
649 MessageParcel data;
650 MessageParcel reply;
651 MessageOption option;
652 if (!WriteInterfaceToken(data)) {
653 return;
654 }
655 if (!data.WriteInt32(winModeKey)) {
656 HILOG_ERROR("fail to WriteParcelable");
657 return;
658 }
659 if (!data.WriteBool(flag)) {
660 HILOG_ERROR("fail to WriteBool");
661 return;
662 }
663 int32_t err = Remote()->SendRequest(IAbilityScheduler::MULTI_WIN_CHANGED, data, reply, option);
664 if (err != NO_ERROR) {
665 HILOG_ERROR("NotifyMultiWinModeChanged fail to SendRequest. err: %d", err);
666 }
667 }
668
669 /**
670 * @brief Registers an observer to DataObsMgr specified by the given Uri.
671 *
672 * @param uri, Indicates the path of the data to operate.
673 * @param dataObserver, Indicates the IDataAbilityObserver object.
674 */
ScheduleRegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)675 bool AbilitySchedulerProxy::ScheduleRegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
676 {
677 MessageParcel data;
678 MessageParcel reply;
679 MessageOption option;
680
681 if (!WriteInterfaceToken(data)) {
682 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
683 return false;
684 }
685
686 if (!data.WriteParcelable(&uri)) {
687 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
688 return false;
689 }
690
691 if (!data.WriteParcelable(dataObserver->AsObject())) {
692 HILOG_ERROR("%{public}s failed to WriteParcelable dataObserver ", __func__);
693 return false;
694 }
695
696 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_REGISTEROBSERVER, data, reply, option);
697 if (result == ERR_NONE) {
698 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
699 return true;
700 } else {
701 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
702 return false;
703 }
704 }
705
706 /**
707 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
708 *
709 * @param uri, Indicates the path of the data to operate.
710 * @param dataObserver, Indicates the IDataAbilityObserver object.
711 */
ScheduleUnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)712 bool AbilitySchedulerProxy::ScheduleUnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
713 {
714 MessageParcel data;
715 MessageParcel reply;
716 MessageOption option;
717
718 if (!WriteInterfaceToken(data)) {
719 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
720 return false;
721 }
722
723 if (!data.WriteParcelable(&uri)) {
724 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
725 return false;
726 }
727
728 if (!data.WriteParcelable(dataObserver->AsObject())) {
729 HILOG_ERROR("%{public}s failed to WriteParcelable dataObserver ", __func__);
730 return false;
731 }
732
733 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UNREGISTEROBSERVER, data, reply, option);
734 if (result == ERR_NONE) {
735 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
736 return true;
737 } else {
738 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
739 return false;
740 }
741 }
742
743 /**
744 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
745 *
746 * @param uri, Indicates the path of the data to operate.
747 */
ScheduleNotifyChange(const Uri & uri)748 bool AbilitySchedulerProxy::ScheduleNotifyChange(const Uri &uri)
749 {
750 MessageParcel data;
751 MessageParcel reply;
752 MessageOption option;
753
754 if (!WriteInterfaceToken(data)) {
755 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
756 return false;
757 }
758
759 if (!data.WriteParcelable(&uri)) {
760 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
761 return false;
762 }
763
764 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_NOTIFYCHANGE, data, reply, option);
765 if (result == ERR_NONE) {
766 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
767 return true;
768 } else {
769 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
770 return false;
771 }
772 }
773
774 /**
775 * @brief notify this ability is top active ability.
776 *
777 * @param flag true: Indicates this ability is top active ability
778 */
NotifyTopActiveAbilityChanged(bool flag)779 void AbilitySchedulerProxy::NotifyTopActiveAbilityChanged(bool flag)
780 {
781 MessageParcel data;
782 MessageParcel reply;
783 MessageOption option;
784 if (!WriteInterfaceToken(data)) {
785 return;
786 }
787 if (!data.WriteBool(flag)) {
788 HILOG_ERROR("fail to WriteBool");
789 return;
790 }
791 int32_t err = Remote()->SendRequest(IAbilityScheduler::TOP_ACTIVE_ABILITY_CHANGED, data, reply, option);
792 if (err != NO_ERROR) {
793 HILOG_ERROR("NotifyTopActiveAbilityChanged fail to SendRequest. err: %d", err);
794 }
795 }
796
797 /**
798 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
799 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
800 * context has changed. If you implement URI normalization for a Data ability, you must also implement
801 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
802 * any method that is called on the Data ability must require normalization verification and denormalization. The
803 * default implementation of this method returns null, indicating that this Data ability does not support URI
804 * normalization.
805 *
806 * @param uri Indicates the Uri object to normalize.
807 *
808 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
809 */
NormalizeUri(const Uri & uri)810 Uri AbilitySchedulerProxy::NormalizeUri(const Uri &uri)
811 {
812 Uri urivalue("");
813
814 MessageParcel data;
815 MessageParcel reply;
816 MessageOption option;
817
818 if (!WriteInterfaceToken(data)) {
819 return urivalue;
820 }
821
822 if (!data.WriteParcelable(&uri)) {
823 HILOG_ERROR("fail to WriteParcelable uri");
824 return urivalue;
825 }
826
827 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_NORMALIZEURI, data, reply, option);
828 if (err != NO_ERROR) {
829 HILOG_ERROR("NormalizeUri fail to SendRequest. err: %d", err);
830 }
831
832 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
833 if (!info) {
834 HILOG_ERROR("ReadParcelable value is nullptr.");
835 return Uri("");
836 }
837 return *info;
838 }
839
840 /**
841 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
842 * The default implementation of this method returns the original URI passed to it.
843 *
844 * @param uri uri Indicates the Uri object to denormalize.
845 *
846 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
847 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
848 * in the current environment.
849 */
DenormalizeUri(const Uri & uri)850 Uri AbilitySchedulerProxy::DenormalizeUri(const Uri &uri)
851 {
852 Uri urivalue("");
853
854 MessageParcel data;
855 MessageParcel reply;
856 MessageOption option;
857
858 if (!WriteInterfaceToken(data)) {
859 return urivalue;
860 }
861
862 if (!data.WriteParcelable(&uri)) {
863 HILOG_ERROR("fail to WriteParcelable uri");
864 return urivalue;
865 }
866
867 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_DENORMALIZEURI, data, reply, option);
868 if (err != NO_ERROR) {
869 HILOG_ERROR("DenormalizeUri fail to SendRequest. err: %d", err);
870 }
871
872 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
873 if (!info) {
874 HILOG_ERROR("ReadParcelable value is nullptr.");
875 return Uri("");
876 }
877 return *info;
878 }
879
ExecuteBatch(const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> & operations)880 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> AbilitySchedulerProxy::ExecuteBatch(
881 const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> &operations)
882 {
883 HILOG_INFO("AbilitySchedulerProxy::ExecuteBatch start");
884 MessageParcel data;
885 MessageParcel reply;
886 MessageOption option;
887
888 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> results;
889 results.clear();
890
891 if (!WriteInterfaceToken(data)) {
892 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to Writer token");
893 return results;
894 }
895
896 int count = operations.size();
897 if (!data.WriteInt32(count)) {
898 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to WriteInt32 ret");
899 return results;
900 }
901
902 for (int i = 0; i < count; i++) {
903 if (!data.WriteParcelable(operations[i].get())) {
904 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to WriteParcelable ret, index = %{public}d", i);
905 return results;
906 }
907 }
908
909 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_EXECUTEBATCH, data, reply, option);
910 if (err != NO_ERROR) {
911 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to SendRequest. err: %{public}d", err);
912 }
913
914 int total = 0;
915 if (!reply.ReadInt32(total)) {
916 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to ReadInt32 count %{public}d", total);
917 return results;
918 }
919
920 for (int i = 0; i < total; i++) {
921 AppExecFwk::DataAbilityResult *result = reply.ReadParcelable<AppExecFwk::DataAbilityResult>();
922 if (result == nullptr) {
923 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch result is nullptr, index = %{public}d", i);
924 return results;
925 }
926 std::shared_ptr<AppExecFwk::DataAbilityResult> dataAbilityResult(result);
927 results.push_back(dataAbilityResult);
928 }
929 HILOG_INFO("AbilitySchedulerProxy::ExecuteBatch end %{public}d", total);
930 return results;
931 }
932 } // namespace AAFwk
933 } // namespace OHOS