• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "ability_manager_proxy.h"
17 
18 #include "errors.h"
19 #include "string_ex.h"
20 
21 #include "ability_connect_callback_proxy.h"
22 #include "ability_connect_callback_stub.h"
23 #include "ability_manager_errors.h"
24 #include "ability_scheduler_proxy.h"
25 #include "ability_scheduler_stub.h"
26 #include "ability_util.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 constexpr int32_t CYCLE_LIMIT = 1000;
WriteInterfaceToken(MessageParcel & data)31 bool AbilityManagerProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33     if (!data.WriteInterfaceToken(AbilityManagerProxy::GetDescriptor())) {
34         HILOG_ERROR("write interface token failed.");
35         return false;
36     }
37     return true;
38 }
39 
StartAbility(const Want & want,int32_t userId,int requestCode)40 int AbilityManagerProxy::StartAbility(const Want &want, int32_t userId, int requestCode)
41 {
42     int error;
43     MessageParcel data;
44     MessageParcel reply;
45     MessageOption option;
46 
47     if (!WriteInterfaceToken(data)) {
48         return INNER_ERR;
49     }
50     if (!data.WriteParcelable(&want)) {
51         HILOG_ERROR("want write failed.");
52         return INNER_ERR;
53     }
54 
55     if (!data.WriteInt32(userId)) {
56         HILOG_ERROR("userId write failed.");
57         return INNER_ERR;
58     }
59 
60     if (!data.WriteInt32(requestCode)) {
61         HILOG_ERROR("requestCode write failed.");
62         return INNER_ERR;
63     }
64 
65     error = Remote()->SendRequest(IAbilityManager::START_ABILITY, data, reply, option);
66     if (error != NO_ERROR) {
67         HILOG_ERROR("Send request error: %{public}d", error);
68         return error;
69     }
70     return reply.ReadInt32();
71 }
72 
GetTopAbility()73 AppExecFwk::ElementName AbilityManagerProxy::GetTopAbility()
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option;
78     if (!WriteInterfaceToken(data)) {
79         return {};
80     }
81 
82     int error = Remote()->SendRequest(IAbilityManager::GET_TOP_ABILITY, data, reply, option);
83     if (error != NO_ERROR) {
84         HILOG_ERROR("Send request error: %{public}d", error);
85         return {};
86     }
87     std::unique_ptr<AppExecFwk::ElementName> name(reply.ReadParcelable<AppExecFwk::ElementName>());
88     if (!name) {
89         HILOG_ERROR("Read info failed.");
90         return {};
91     }
92     AppExecFwk::ElementName result = *name;
93     return result;
94 }
95 
StartAbility(const Want & want,const AbilityStartSetting & abilityStartSetting,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)96 int AbilityManagerProxy::StartAbility(const Want &want, const AbilityStartSetting &abilityStartSetting,
97     const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode)
98 {
99     int error;
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103     if (!WriteInterfaceToken(data)) {
104         return INNER_ERR;
105     }
106     if (!data.WriteParcelable(&want)) {
107         HILOG_ERROR("want write failed.");
108         return INNER_ERR;
109     }
110     if (!data.WriteParcelable(&abilityStartSetting)) {
111         HILOG_ERROR("abilityStartSetting write failed.");
112         return INNER_ERR;
113     }
114     if (callerToken) {
115         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
116             HILOG_ERROR("flag and callerToken write failed.");
117             return INNER_ERR;
118         }
119     } else {
120         if (!data.WriteBool(false)) {
121             HILOG_ERROR("flag write failed.");
122             return INNER_ERR;
123         }
124     }
125     if (!data.WriteInt32(userId)) {
126         HILOG_ERROR("userId write failed.");
127         return INNER_ERR;
128     }
129     if (!data.WriteInt32(requestCode)) {
130         HILOG_ERROR("requestCode write failed.");
131         return INNER_ERR;
132     }
133     error = Remote()->SendRequest(IAbilityManager::START_ABILITY_FOR_SETTINGS, data, reply, option);
134     if (error != NO_ERROR) {
135         HILOG_ERROR("Send request error: %{public}d", error);
136         return error;
137     }
138     return reply.ReadInt32();
139 }
140 
StartAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)141 int AbilityManagerProxy::StartAbility(
142     const Want &want, const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode)
143 {
144     int error;
145     MessageParcel data;
146     MessageParcel reply;
147     MessageOption option;
148 
149     if (!WriteInterfaceToken(data)) {
150         return INNER_ERR;
151     }
152     if (!data.WriteParcelable(&want)) {
153         HILOG_ERROR("want write failed.");
154         return INNER_ERR;
155     }
156     if (callerToken) {
157         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
158             HILOG_ERROR("callerToken and flag write failed.");
159             return INNER_ERR;
160         }
161     } else {
162         if (!data.WriteBool(false)) {
163             HILOG_ERROR("flag write failed.");
164             return INNER_ERR;
165         }
166     }
167     if (!data.WriteInt32(userId)) {
168         HILOG_ERROR("userId write failed.");
169         return INNER_ERR;
170     }
171     if (!data.WriteInt32(requestCode)) {
172         HILOG_ERROR("requestCode write failed.");
173         return INNER_ERR;
174     }
175 
176     error = Remote()->SendRequest(IAbilityManager::START_ABILITY_ADD_CALLER, data, reply, option);
177     if (error != NO_ERROR) {
178         HILOG_ERROR("Send request error: %{public}d", error);
179         return error;
180     }
181     return reply.ReadInt32();
182 }
183 
StartAbility(const Want & want,const StartOptions & startOptions,const sptr<IRemoteObject> & callerToken,int32_t userId,int requestCode)184 int AbilityManagerProxy::StartAbility(const Want &want, const StartOptions &startOptions,
185     const sptr<IRemoteObject> &callerToken, int32_t userId, int requestCode)
186 {
187     int error;
188     MessageParcel data;
189     MessageParcel reply;
190     MessageOption option;
191     if (!WriteInterfaceToken(data)) {
192         return INNER_ERR;
193     }
194     if (!data.WriteParcelable(&want)) {
195         HILOG_ERROR("want write failed.");
196         return INNER_ERR;
197     }
198     if (!data.WriteParcelable(&startOptions)) {
199         HILOG_ERROR("startOptions write failed.");
200         return INNER_ERR;
201     }
202     if (callerToken) {
203         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
204             HILOG_ERROR("flag and callerToken write failed.");
205             return INNER_ERR;
206         }
207     } else {
208         if (!data.WriteBool(false)) {
209             HILOG_ERROR("flag write failed.");
210             return INNER_ERR;
211         }
212     }
213     if (!data.WriteInt32(userId)) {
214         HILOG_ERROR("userId write failed.");
215         return INNER_ERR;
216     }
217     if (!data.WriteInt32(requestCode)) {
218         HILOG_ERROR("requestCode write failed.");
219         return INNER_ERR;
220     }
221     error = Remote()->SendRequest(IAbilityManager::START_ABILITY_FOR_OPTIONS, data, reply, option);
222     if (error != NO_ERROR) {
223         HILOG_ERROR("Send request error: %{public}d", error);
224         return error;
225     }
226     return reply.ReadInt32();
227 }
228 
StartExtensionAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,AppExecFwk::ExtensionAbilityType extensionType)229 int AbilityManagerProxy::StartExtensionAbility(const Want &want, const sptr<IRemoteObject> &callerToken,
230     int32_t userId, AppExecFwk::ExtensionAbilityType extensionType)
231 {
232     int error;
233     MessageParcel data;
234     MessageParcel reply;
235     MessageOption option;
236     if (!WriteInterfaceToken(data)) {
237         return INNER_ERR;
238     }
239     if (!data.WriteParcelable(&want)) {
240         HILOG_ERROR("want write failed.");
241         return INNER_ERR;
242     }
243     if (callerToken) {
244         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
245             HILOG_ERROR("flag and callerToken write failed.");
246             return INNER_ERR;
247         }
248     } else {
249         if (!data.WriteBool(false)) {
250             HILOG_ERROR("flag write failed.");
251             return INNER_ERR;
252         }
253     }
254     if (!data.WriteInt32(userId)) {
255         HILOG_ERROR("StartExtensionAbility, userId write failed.");
256         return INNER_ERR;
257     }
258     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
259         HILOG_ERROR("StartExtensionAbility, extensionType write failed.");
260         return INNER_ERR;
261     }
262     if (!Remote()) {
263         HILOG_ERROR("StartExtensionAbility, Remote error.");
264         return INNER_ERR;
265     }
266     error = Remote()->SendRequest(IAbilityManager::START_EXTENSION_ABILITY, data, reply, option);
267     if (error != NO_ERROR) {
268         HILOG_ERROR("StartExtensionAbility, Send request error: %{public}d", error);
269         return error;
270     }
271     return reply.ReadInt32();
272 }
273 
StopExtensionAbility(const Want & want,const sptr<IRemoteObject> & callerToken,int32_t userId,AppExecFwk::ExtensionAbilityType extensionType)274 int AbilityManagerProxy::StopExtensionAbility(const Want &want, const sptr<IRemoteObject> &callerToken,
275     int32_t userId, AppExecFwk::ExtensionAbilityType extensionType)
276 {
277     int error;
278     MessageParcel data;
279     MessageParcel reply;
280     MessageOption option;
281     if (!WriteInterfaceToken(data)) {
282         return INNER_ERR;
283     }
284     if (!data.WriteParcelable(&want)) {
285         HILOG_ERROR("want write failed.");
286         return INNER_ERR;
287     }
288     if (callerToken) {
289         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
290             HILOG_ERROR("flag and callerToken write failed.");
291             return INNER_ERR;
292         }
293     } else {
294         if (!data.WriteBool(false)) {
295             HILOG_ERROR("flag write failed.");
296             return INNER_ERR;
297         }
298     }
299     if (!data.WriteInt32(userId)) {
300         HILOG_ERROR("userId write failed.");
301         return INNER_ERR;
302     }
303     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
304         HILOG_ERROR("extensionType write failed.");
305         return INNER_ERR;
306     }
307     if (!Remote()) {
308         HILOG_ERROR("Remote error.");
309         return INNER_ERR;
310     }
311     error = Remote()->SendRequest(IAbilityManager::STOP_EXTENSION_ABILITY, data, reply, option);
312     if (error != NO_ERROR) {
313         HILOG_ERROR("Send request error: %{public}d", error);
314         return error;
315     }
316     return reply.ReadInt32();
317 }
318 
TerminateAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant)319 int AbilityManagerProxy::TerminateAbility(const sptr<IRemoteObject> &token, int resultCode, const Want *resultWant)
320 {
321     return TerminateAbility(token, resultCode, resultWant, true);
322 }
323 
TerminateAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant,bool flag)324 int AbilityManagerProxy::TerminateAbility(const sptr<IRemoteObject> &token,
325     int resultCode, const Want *resultWant, bool flag)
326 {
327     int error;
328     MessageParcel data;
329     MessageParcel reply;
330     MessageOption option;
331 
332     if (!WriteInterfaceToken(data)) {
333         return INNER_ERR;
334     }
335     if (token) {
336         if (!data.WriteBool(true) || !data.WriteRemoteObject(token)) {
337             HILOG_ERROR("flag and token write failed.");
338             return INNER_ERR;
339         }
340     } else {
341         if (!data.WriteBool(false)) {
342             HILOG_ERROR("flag write failed.");
343             return INNER_ERR;
344         }
345     }
346     if (!data.WriteInt32(resultCode) || !data.WriteParcelable(resultWant)) {
347         HILOG_ERROR("data write failed.");
348         return INNER_ERR;
349     }
350     if (!data.WriteBool(flag)) {
351         HILOG_ERROR("data write flag failed.");
352         return INNER_ERR;
353     }
354     error = Remote()->SendRequest(IAbilityManager::TERMINATE_ABILITY, data, reply, option);
355     if (error != NO_ERROR) {
356         HILOG_ERROR("Send request error: %{public}d", error);
357         return error;
358     }
359     return reply.ReadInt32();
360 }
361 
SendResultToAbility(int32_t requestCode,int32_t resultCode,Want & resultWant)362 int AbilityManagerProxy::SendResultToAbility(int32_t requestCode, int32_t resultCode, Want& resultWant)
363 {
364     int error;
365     MessageParcel data;
366     MessageParcel reply;
367     MessageOption option;
368 
369     if (!WriteInterfaceToken(data)) {
370         return INNER_ERR;
371     }
372     if (!data.WriteInt32(requestCode)) {
373         HILOG_ERROR("requestCode write failed.");
374         return INNER_ERR;
375     }
376     if (!data.WriteInt32(resultCode) || !data.WriteParcelable(&resultWant)) {
377         HILOG_ERROR("data write failed.");
378         return INNER_ERR;
379     }
380     error = Remote()->SendRequest(IAbilityManager::SEND_RESULT_TO_ABILITY, data, reply, option);
381     if (error != NO_ERROR) {
382         HILOG_ERROR("Send request error: %{public}d", error);
383         return error;
384     }
385     return reply.ReadInt32();
386 }
387 
TerminateAbilityByCaller(const sptr<IRemoteObject> & callerToken,int requestCode)388 int AbilityManagerProxy::TerminateAbilityByCaller(const sptr<IRemoteObject> &callerToken, int requestCode)
389 {
390     int error;
391     MessageParcel data;
392     MessageParcel reply;
393     MessageOption option;
394 
395     if (!WriteInterfaceToken(data)) {
396         return INNER_ERR;
397     }
398     if (callerToken) {
399         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
400             HILOG_ERROR("flag and callerToken write failed.");
401             return INNER_ERR;
402         }
403     } else {
404         if (!data.WriteBool(false)) {
405             HILOG_ERROR("flag write failed.");
406             return INNER_ERR;
407         }
408     }
409     if (!data.WriteInt32(requestCode)) {
410         HILOG_ERROR("data write failed.");
411         return INNER_ERR;
412     }
413     error = Remote()->SendRequest(IAbilityManager::TERMINATE_ABILITY_BY_CALLER, data, reply, option);
414     if (error != NO_ERROR) {
415         HILOG_ERROR("Send request error: %{public}d", error);
416         return error;
417     }
418     return reply.ReadInt32();
419 }
420 
CloseAbility(const sptr<IRemoteObject> & token,int resultCode,const Want * resultWant)421 int AbilityManagerProxy::CloseAbility(const sptr<IRemoteObject> &token, int resultCode, const Want *resultWant)
422 {
423     return TerminateAbility(token, resultCode, resultWant, false);
424 }
425 
ConnectAbility(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,int32_t userId)426 int AbilityManagerProxy::ConnectAbility(
427     const Want &want, const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken, int32_t userId)
428 {
429     return ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::SERVICE, userId);
430 }
431 
ConnectAbilityCommon(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken,AppExecFwk::ExtensionAbilityType extensionType,int32_t userId)432 int AbilityManagerProxy::ConnectAbilityCommon(
433     const Want &want, const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken,
434     AppExecFwk::ExtensionAbilityType extensionType, int32_t userId)
435 {
436     MessageParcel data;
437     MessageParcel reply;
438     MessageOption option;
439 
440     if (!WriteInterfaceToken(data)) {
441         return INNER_ERR;
442     }
443     if (!data.WriteParcelable(&want)) {
444         HILOG_ERROR("want write failed.");
445         return ERR_INVALID_VALUE;
446     }
447     CHECK_POINTER_AND_RETURN_LOG(connect, ERR_INVALID_VALUE, "connect ability fail, connect is nullptr");
448     if (connect->AsObject()) {
449         if (!data.WriteBool(true) || !data.WriteRemoteObject(connect->AsObject())) {
450             HILOG_ERROR("flag and connect write failed.");
451             return ERR_INVALID_VALUE;
452         }
453     } else {
454         if (!data.WriteBool(false)) {
455             HILOG_ERROR("flag write failed.");
456             return ERR_INVALID_VALUE;
457         }
458     }
459     if (callerToken) {
460         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
461             HILOG_ERROR("flag and callerToken write failed.");
462             return ERR_INVALID_VALUE;
463         }
464     } else {
465         if (!data.WriteBool(false)) {
466             HILOG_ERROR("flag write failed.");
467             return ERR_INVALID_VALUE;
468         }
469     }
470     if (!data.WriteInt32(userId)) {
471         HILOG_ERROR("%{public}s, userId write failed.", __func__);
472         return INNER_ERR;
473     }
474     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
475         HILOG_ERROR("%{public}s, extensionType write failed.", __func__);
476         return INNER_ERR;
477     }
478     CHECK_POINTER_AND_RETURN_LOG(Remote(), INNER_ERR, "connect ability fail, remote is nullptr");
479     int error = Remote()->SendRequest(IAbilityManager::CONNECT_ABILITY_WITH_TYPE, data, reply, option);
480     if (error != NO_ERROR) {
481         HILOG_ERROR("%{public}s, Send request error: %{public}d", __func__, error);
482         return error;
483     }
484     return reply.ReadInt32();
485 }
486 
DisconnectAbility(const sptr<IAbilityConnection> & connect)487 int AbilityManagerProxy::DisconnectAbility(const sptr<IAbilityConnection> &connect)
488 {
489     int error;
490     MessageParcel data;
491     MessageParcel reply;
492     MessageOption option;
493     if (connect == nullptr) {
494         HILOG_ERROR("disconnect ability fail, connect is nullptr");
495         return ERR_INVALID_VALUE;
496     }
497     if (!WriteInterfaceToken(data)) {
498         return INNER_ERR;
499     }
500     if (!data.WriteRemoteObject(connect->AsObject())) {
501         HILOG_ERROR("connect write failed.");
502         return ERR_INVALID_VALUE;
503     }
504 
505     error = Remote()->SendRequest(IAbilityManager::DISCONNECT_ABILITY, data, reply, option);
506     if (error != NO_ERROR) {
507         HILOG_ERROR("Send request error: %{public}d", error);
508         return error;
509     }
510     return reply.ReadInt32();
511 }
512 
AcquireDataAbility(const Uri & uri,bool tryBind,const sptr<IRemoteObject> & callerToken)513 sptr<IAbilityScheduler> AbilityManagerProxy::AcquireDataAbility(
514     const Uri &uri, bool tryBind, const sptr<IRemoteObject> &callerToken)
515 {
516     int error;
517     MessageParcel data;
518     MessageParcel reply;
519     MessageOption option;
520 
521     if (!callerToken) {
522         HILOG_ERROR("invalid parameters for acquire data ability.");
523         return nullptr;
524     }
525     if (!WriteInterfaceToken(data)) {
526         return nullptr;
527     }
528     if (!data.WriteString(uri.ToString()) || !data.WriteBool(tryBind) || !data.WriteRemoteObject(callerToken)) {
529         HILOG_ERROR("data write failed.");
530         return nullptr;
531     }
532     error = Remote()->SendRequest(IAbilityManager::ACQUIRE_DATA_ABILITY, data, reply, option);
533     if (error != NO_ERROR) {
534         HILOG_ERROR("Send request error: %{public}d", error);
535         return nullptr;
536     }
537 
538     return iface_cast<IAbilityScheduler>(reply.ReadRemoteObject());
539 }
540 
ReleaseDataAbility(sptr<IAbilityScheduler> dataAbilityScheduler,const sptr<IRemoteObject> & callerToken)541 int AbilityManagerProxy::ReleaseDataAbility(
542     sptr<IAbilityScheduler> dataAbilityScheduler, const sptr<IRemoteObject> &callerToken)
543 {
544     int error;
545     MessageParcel data;
546     MessageParcel reply;
547     MessageOption option;
548 
549     if (!dataAbilityScheduler || !callerToken) {
550         return ERR_INVALID_VALUE;
551     }
552     if (!WriteInterfaceToken(data)) {
553         return INNER_ERR;
554     }
555     if (!data.WriteRemoteObject(dataAbilityScheduler->AsObject()) || !data.WriteRemoteObject(callerToken)) {
556         HILOG_ERROR("data write failed.");
557         return INNER_ERR;
558     }
559     error = Remote()->SendRequest(IAbilityManager::RELEASE_DATA_ABILITY, data, reply, option);
560     if (error != NO_ERROR) {
561         HILOG_ERROR("Send request error: %{public}d", error);
562         return error;
563     }
564     return reply.ReadInt32();
565 }
566 
AttachAbilityThread(const sptr<IAbilityScheduler> & scheduler,const sptr<IRemoteObject> & token)567 int AbilityManagerProxy::AttachAbilityThread(const sptr<IAbilityScheduler> &scheduler, const sptr<IRemoteObject> &token)
568 {
569     int error;
570     MessageParcel data;
571     MessageParcel reply;
572     MessageOption option;
573     if (scheduler == nullptr) {
574         return ERR_INVALID_VALUE;
575     }
576     if (!WriteInterfaceToken(data)) {
577         return INNER_ERR;
578     }
579     if (!data.WriteRemoteObject(scheduler->AsObject()) || !data.WriteRemoteObject(token)) {
580         HILOG_ERROR("data write failed.");
581         return ERR_INVALID_VALUE;
582     }
583     error = Remote()->SendRequest(IAbilityManager::ATTACH_ABILITY_THREAD, data, reply, option);
584     if (error != NO_ERROR) {
585         HILOG_ERROR("Send request error: %{public}d", error);
586         return error;
587     }
588     return reply.ReadInt32();
589 }
590 
AbilityTransitionDone(const sptr<IRemoteObject> & token,int state,const PacMap & saveData)591 int AbilityManagerProxy::AbilityTransitionDone(const sptr<IRemoteObject> &token, int state, const PacMap &saveData)
592 {
593     int error;
594     MessageParcel data;
595     MessageParcel reply;
596     MessageOption option;
597 
598     if (!WriteInterfaceToken(data)) {
599         return INNER_ERR;
600     }
601     if (!data.WriteRemoteObject(token) || !data.WriteInt32(state)) {
602         HILOG_ERROR("token or state write failed.");
603         return ERR_INVALID_VALUE;
604     }
605     if (!data.WriteParcelable(&saveData)) {
606         HILOG_ERROR("saveData write failed.");
607         return INNER_ERR;
608     }
609     error = Remote()->SendRequest(IAbilityManager::ABILITY_TRANSITION_DONE, data, reply, option);
610     if (error != NO_ERROR) {
611         HILOG_ERROR("Send request error: %{public}d", error);
612         return error;
613     }
614     return reply.ReadInt32();
615 }
616 
ScheduleConnectAbilityDone(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & remoteObject)617 int AbilityManagerProxy::ScheduleConnectAbilityDone(
618     const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &remoteObject)
619 {
620     int error;
621     MessageParcel data;
622     MessageParcel reply;
623     MessageOption option;
624 
625     if (!WriteInterfaceToken(data)) {
626         return INNER_ERR;
627     }
628 
629     if (token) {
630         if (!data.WriteBool(true) || !data.WriteRemoteObject(token)) {
631             HILOG_ERROR("Failed to write flag and token.");
632             return ERR_INVALID_VALUE;
633         }
634     } else {
635         if (!data.WriteBool(false)) {
636             HILOG_ERROR("Failed to write flag.");
637             return ERR_INVALID_VALUE;
638         }
639     }
640 
641     if (remoteObject) {
642         if (!data.WriteBool(true) || !data.WriteRemoteObject(remoteObject)) {
643             HILOG_ERROR("Failed to write flag and remoteObject.");
644             return ERR_INVALID_VALUE;
645         }
646     } else {
647         if (!data.WriteBool(false)) {
648             HILOG_ERROR("Failed to write flag.");
649             return ERR_INVALID_VALUE;
650         }
651     }
652 
653     error = Remote()->SendRequest(IAbilityManager::CONNECT_ABILITY_DONE, data, reply, option);
654     if (error != NO_ERROR) {
655         HILOG_ERROR("Send request error: %{public}d", error);
656         return error;
657     }
658     return reply.ReadInt32();
659 }
660 
ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> & token)661 int AbilityManagerProxy::ScheduleDisconnectAbilityDone(const sptr<IRemoteObject> &token)
662 {
663     int error;
664     MessageParcel data;
665     MessageParcel reply;
666     MessageOption option;
667 
668     if (!WriteInterfaceToken(data)) {
669         return INNER_ERR;
670     }
671     if (!data.WriteRemoteObject(token)) {
672         HILOG_ERROR("token write failed.");
673         return ERR_INVALID_VALUE;
674     }
675 
676     error = Remote()->SendRequest(IAbilityManager::DISCONNECT_ABILITY_DONE, data, reply, option);
677     if (error != NO_ERROR) {
678         HILOG_ERROR("Send request error: %{public}d", error);
679         return error;
680     }
681     return reply.ReadInt32();
682 }
683 
ScheduleCommandAbilityDone(const sptr<IRemoteObject> & token)684 int AbilityManagerProxy::ScheduleCommandAbilityDone(const sptr<IRemoteObject> &token)
685 {
686     int error;
687     MessageParcel data;
688     MessageParcel reply;
689     MessageOption option;
690 
691     if (!WriteInterfaceToken(data)) {
692         return INNER_ERR;
693     }
694     if (!data.WriteRemoteObject(token)) {
695         HILOG_ERROR("token write failed.");
696         return ERR_INVALID_VALUE;
697     }
698 
699     error = Remote()->SendRequest(IAbilityManager::COMMAND_ABILITY_DONE, data, reply, option);
700     if (error != NO_ERROR) {
701         HILOG_ERROR("Send request error: %{public}d", error);
702         return error;
703     }
704     return reply.ReadInt32();
705 }
706 
DumpSysState(const std::string & args,std::vector<std::string> & state,bool isClient,bool isUserId,int UserId)707 void AbilityManagerProxy::DumpSysState(
708     const std::string& args, std::vector<std::string>& state, bool isClient, bool isUserId, int UserId)
709 {
710     int error;
711     MessageParcel data;
712     MessageParcel reply;
713     MessageOption option;
714 
715     if (!WriteInterfaceToken(data)) {
716         return;
717     }
718     data.WriteString16(Str8ToStr16(args));
719 
720     if (!data.WriteBool(isClient)) {
721         HILOG_ERROR("data write failed.");
722         return ;
723     }
724     if (!data.WriteBool(isUserId)) {
725         HILOG_ERROR("data write failed.");
726         return ;
727     }
728     if (!data.WriteInt32(UserId)) {
729         HILOG_ERROR("data write failed.");
730         return ;
731     }
732     error = Remote()->SendRequest(IAbilityManager::DUMPSYS_STATE, data, reply, option);
733     if (error != NO_ERROR) {
734         HILOG_ERROR("AbilityManagerProxy: SendRequest err %{public}d", error);
735         return;
736     }
737     int32_t stackNum = reply.ReadInt32();
738     for (int i = 0; i < stackNum; i++) {
739         std::string stac = Str16ToStr8(reply.ReadString16());
740         state.emplace_back(stac);
741     }
742 }
743 
DumpState(const std::string & args,std::vector<std::string> & state)744 void AbilityManagerProxy::DumpState(const std::string &args, std::vector<std::string> &state)
745 {
746     int error;
747     MessageParcel data;
748     MessageParcel reply;
749     MessageOption option;
750 
751     if (!WriteInterfaceToken(data)) {
752         return;
753     }
754     data.WriteString16(Str8ToStr16(args));
755     error = Remote()->SendRequest(IAbilityManager::DUMP_STATE, data, reply, option);
756     if (error != NO_ERROR) {
757         HILOG_ERROR("AbilityManagerProxy: SendRequest err %{public}d", error);
758         return;
759     }
760     int32_t stackNum = reply.ReadInt32();
761     for (int i = 0; i < stackNum; i++) {
762         std::string stac = Str16ToStr8(reply.ReadString16());
763         state.emplace_back(stac);
764     }
765 }
766 
TerminateAbilityResult(const sptr<IRemoteObject> & token,int startId)767 int AbilityManagerProxy::TerminateAbilityResult(const sptr<IRemoteObject> &token, int startId)
768 {
769     int error;
770     MessageParcel data;
771     MessageParcel reply;
772     MessageOption option;
773 
774     if (!WriteInterfaceToken(data)) {
775         return INNER_ERR;
776     }
777     if (!data.WriteRemoteObject(token) || !data.WriteInt32(startId)) {
778         HILOG_ERROR("data write failed.");
779         return ERR_INVALID_VALUE;
780     }
781     error = Remote()->SendRequest(IAbilityManager::TERMINATE_ABILITY_RESULT, data, reply, option);
782     if (error != NO_ERROR) {
783         HILOG_ERROR("Send request error: %{public}d", error);
784         return error;
785     }
786     return reply.ReadInt32();
787 }
788 
MinimizeAbility(const sptr<IRemoteObject> & token,bool fromUser)789 int AbilityManagerProxy::MinimizeAbility(const sptr<IRemoteObject> &token, bool fromUser)
790 {
791     int error;
792     MessageParcel data;
793     MessageParcel reply;
794     MessageOption option;
795 
796     if (!WriteInterfaceToken(data)) {
797         return INNER_ERR;
798     }
799     if (!data.WriteRemoteObject(token)) {
800         HILOG_ERROR("token write failed.");
801         return ERR_INVALID_VALUE;
802     }
803     if (!data.WriteBool(fromUser)) {
804         HILOG_ERROR("data write failed.");
805         return ERR_INVALID_VALUE;
806     }
807     error = Remote()->SendRequest(IAbilityManager::MINIMIZE_ABILITY, data, reply, option);
808     if (error != NO_ERROR) {
809         HILOG_ERROR("Send request error: %{public}d", error);
810         return error;
811     }
812     return reply.ReadInt32();
813 }
814 
StopServiceAbility(const Want & want,int32_t userId)815 int AbilityManagerProxy::StopServiceAbility(const Want &want, int32_t userId)
816 {
817     int error;
818     MessageParcel data;
819     MessageParcel reply;
820     MessageOption option;
821 
822     if (!WriteInterfaceToken(data)) {
823         return INNER_ERR;
824     }
825     if (!data.WriteParcelable(&want)) {
826         HILOG_ERROR("want write failed.");
827         return INNER_ERR;
828     }
829     if (!data.WriteInt32(userId)) {
830         HILOG_ERROR("userId write failed.");
831         return INNER_ERR;
832     }
833     error = Remote()->SendRequest(IAbilityManager::STOP_SERVICE_ABILITY, data, reply, option);
834     if (error != NO_ERROR) {
835         HILOG_ERROR("Send request error: %{public}d", error);
836         return error;
837     }
838     return reply.ReadInt32();
839 }
840 
841 template <typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)842 int AbilityManagerProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
843 {
844     int32_t infoSize = reply.ReadInt32();
845     if (infoSize > CYCLE_LIMIT) {
846         HILOG_ERROR("infoSize is too large");
847         return ERR_INVALID_VALUE;
848     }
849 
850     for (int32_t i = 0; i < infoSize; i++) {
851         std::unique_ptr<T> info(reply.ReadParcelable<T>());
852         if (!info) {
853             HILOG_ERROR("Read Parcelable infos failed.");
854             return ERR_INVALID_VALUE;
855         }
856         parcelableInfos.emplace_back(*info);
857     }
858     return NO_ERROR;
859 }
860 
GetMissionSnapshot(const std::string & deviceId,int32_t missionId,MissionSnapshot & snapshot,bool isLowResolution)861 int AbilityManagerProxy::GetMissionSnapshot(const std::string& deviceId, int32_t missionId,
862     MissionSnapshot& snapshot, bool isLowResolution)
863 {
864     int error;
865     MessageParcel data;
866     MessageParcel reply;
867     MessageOption option;
868 
869     if (!WriteInterfaceToken(data)) {
870         return INNER_ERR;
871     }
872     if (!data.WriteString(deviceId)) {
873         HILOG_ERROR("deviceId write failed.");
874         return INNER_ERR;
875     }
876     if (!data.WriteInt32(missionId)) {
877         HILOG_ERROR("missionId write failed.");
878         return ERR_INVALID_VALUE;
879     }
880     if (!data.WriteBool(isLowResolution)) {
881         HILOG_ERROR("isLowResolution write failed.");
882         return ERR_INVALID_VALUE;
883     }
884     error = Remote()->SendRequest(IAbilityManager::GET_MISSION_SNAPSHOT_INFO, data, reply, option);
885     if (error != NO_ERROR) {
886         HILOG_ERROR("Send request error: %{public}d", error);
887         return error;
888     }
889     std::unique_ptr<MissionSnapshot> info(reply.ReadParcelable<MissionSnapshot>());
890     if (!info) {
891         HILOG_ERROR("readParcelableInfo failed.");
892         return ERR_UNKNOWN_OBJECT;
893     }
894     snapshot = *info;
895     return reply.ReadInt32();
896 }
897 
UpdateMissionSnapShot(const sptr<IRemoteObject> & token)898 void AbilityManagerProxy::UpdateMissionSnapShot(const sptr<IRemoteObject>& token)
899 {
900     int error;
901     MessageParcel data;
902     MessageParcel reply;
903     MessageOption option;
904 
905     if (!WriteInterfaceToken(data)) {
906         return;
907     }
908     if (!data.WriteRemoteObject(token)) {
909         HILOG_ERROR("token write failed.");
910         return;
911     }
912     error = Remote()->SendRequest(IAbilityManager::UPDATE_MISSION_SNAPSHOT, data, reply, option);
913     if (error != NO_ERROR) {
914         HILOG_ERROR("Send request error: %{public}d", error);
915         return;
916     }
917     return;
918 }
919 
EnableRecoverAbility(const sptr<IRemoteObject> & token)920 void AbilityManagerProxy::EnableRecoverAbility(const sptr<IRemoteObject>& token)
921 {
922     int error;
923     MessageParcel data;
924     MessageParcel reply;
925     MessageOption option(MessageOption::TF_ASYNC);
926 
927     if (!WriteInterfaceToken(data)) {
928         HILOG_ERROR("AppRecovery WriteInterfaceToken failed.");
929         return;
930     }
931 
932     if (!data.WriteRemoteObject(token)) {
933         HILOG_ERROR("AppRecovery WriteRemoteObject failed.");
934         return;
935     }
936 
937     auto remote = Remote();
938     if (remote == nullptr) {
939         return;
940     }
941     error = remote->SendRequest(IAbilityManager::ABILITY_RECOVERY_ENABLE, data, reply, option);
942     if (error != NO_ERROR) {
943         HILOG_ERROR("AppRecovery Send request error: %{public}d", error);
944         return;
945     }
946     return;
947 }
948 
ScheduleRecoverAbility(const sptr<IRemoteObject> & token,int32_t reason)949 void AbilityManagerProxy::ScheduleRecoverAbility(const sptr<IRemoteObject>& token, int32_t reason)
950 {
951     int error;
952     MessageParcel data;
953     MessageParcel reply;
954     MessageOption option(MessageOption::TF_ASYNC);
955 
956     if (!WriteInterfaceToken(data)) {
957         HILOG_ERROR("AppRecovery WriteInterfaceToken failed.");
958         return;
959     }
960 
961     if (!data.WriteRemoteObject(token)) {
962         HILOG_ERROR("AppRecovery WriteRemoteObject failed.");
963         return;
964     }
965 
966     data.WriteInt32(reason);
967 
968     auto remote = Remote();
969     if (remote == nullptr) {
970         return;
971     }
972     error = remote->SendRequest(IAbilityManager::ABILITY_RECOVERY, data, reply, option);
973     if (error != NO_ERROR) {
974         HILOG_ERROR("AppRecovery Send request error: %{public}d", error);
975         return;
976     }
977     return;
978 }
979 
KillProcess(const std::string & bundleName)980 int AbilityManagerProxy::KillProcess(const std::string &bundleName)
981 {
982     MessageParcel data;
983     MessageParcel reply;
984     MessageOption option;
985 
986     if (!WriteInterfaceToken(data)) {
987         return INNER_ERR;
988     }
989     if (!data.WriteString16(Str8ToStr16(bundleName))) {
990         HILOG_ERROR("bundleName write failed.");
991         return ERR_INVALID_VALUE;
992     }
993     int error = Remote()->SendRequest(IAbilityManager::KILL_PROCESS, data, reply, option);
994     if (error != NO_ERROR) {
995         HILOG_ERROR("Send request error: %{public}d", error);
996         return error;
997     }
998     return reply.ReadInt32();
999 }
1000 
1001 #ifdef ABILITY_COMMAND_FOR_TEST
ForceTimeoutForTest(const std::string & abilityName,const std::string & state)1002 int AbilityManagerProxy::ForceTimeoutForTest(const std::string &abilityName, const std::string &state)
1003 {
1004     MessageParcel data;
1005     MessageParcel reply;
1006     MessageOption option;
1007 
1008     if (!WriteInterfaceToken(data)) {
1009         return INNER_ERR;
1010     }
1011     if (!data.WriteString16(Str8ToStr16(abilityName))) {
1012         HILOG_ERROR("abilityName write failed.");
1013         return ERR_INVALID_VALUE;
1014     }
1015     if (!data.WriteString16(Str8ToStr16(state))) {
1016         HILOG_ERROR("abilityName write failed.");
1017         return ERR_INVALID_VALUE;
1018     }
1019     int error = Remote()->SendRequest(IAbilityManager::FORCE_TIMEOUT, data, reply, option);
1020     if (error != NO_ERROR) {
1021         HILOG_ERROR("Send request error: %{public}d", error);
1022         return error;
1023     }
1024     return reply.ReadInt32();
1025 }
1026 #endif
1027 
ClearUpApplicationData(const std::string & bundleName)1028 int AbilityManagerProxy::ClearUpApplicationData(const std::string &bundleName)
1029 {
1030     MessageParcel data;
1031     MessageParcel reply;
1032     MessageOption option;
1033 
1034     if (!WriteInterfaceToken(data)) {
1035         return INNER_ERR;
1036     }
1037     if (!data.WriteString16(Str8ToStr16(bundleName))) {
1038         HILOG_ERROR("bundleName write failed.");
1039         return ERR_INVALID_VALUE;
1040     }
1041     int error = Remote()->SendRequest(IAbilityManager::CLEAR_UP_APPLICATION_DATA, data, reply, option);
1042     if (error != NO_ERROR) {
1043         HILOG_ERROR("Send request error: %{public}d", error);
1044         return error;
1045     }
1046     return reply.ReadInt32();
1047 }
1048 
UninstallApp(const std::string & bundleName,int32_t uid)1049 int AbilityManagerProxy::UninstallApp(const std::string &bundleName, int32_t uid)
1050 {
1051     MessageParcel data;
1052     MessageParcel reply;
1053     MessageOption option;
1054 
1055     if (!WriteInterfaceToken(data)) {
1056         return INNER_ERR;
1057     }
1058     if (!data.WriteString16(Str8ToStr16(bundleName))) {
1059         HILOG_ERROR("bundleName write failed.");
1060         return ERR_INVALID_VALUE;
1061     }
1062     if (!data.WriteInt32(uid)) {
1063         HILOG_ERROR("uid write failed.");
1064         return ERR_INVALID_VALUE;
1065     }
1066     int error = Remote()->SendRequest(IAbilityManager::UNINSTALL_APP, data, reply, option);
1067     if (error != NO_ERROR) {
1068         HILOG_ERROR("Send request error: %{public}d", error);
1069         return error;
1070     }
1071     return reply.ReadInt32();
1072 }
1073 
GetWantSender(const WantSenderInfo & wantSenderInfo,const sptr<IRemoteObject> & callerToken)1074 sptr<IWantSender> AbilityManagerProxy::GetWantSender(
1075     const WantSenderInfo &wantSenderInfo, const sptr<IRemoteObject> &callerToken)
1076 {
1077     MessageParcel data;
1078     MessageParcel reply;
1079     MessageOption option;
1080     if (!WriteInterfaceToken(data)) {
1081         return nullptr;
1082     }
1083     if (!data.WriteParcelable(&wantSenderInfo)) {
1084         HILOG_ERROR("wantSenderInfo write failed.");
1085         return nullptr;
1086     }
1087     if (callerToken) {
1088         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
1089             HILOG_ERROR("flag and callerToken write failed.");
1090             return nullptr;
1091         }
1092     } else {
1093         if (!data.WriteBool(false)) {
1094             HILOG_ERROR("flag write failed.");
1095             return nullptr;
1096         }
1097     }
1098 
1099     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_SENDER, data, reply, option);
1100     if (error != NO_ERROR) {
1101         HILOG_ERROR("Send request error: %{public}d", error);
1102         return nullptr;
1103     }
1104     sptr<IWantSender> wantSender = iface_cast<IWantSender>(reply.ReadRemoteObject());
1105     if (!wantSender) {
1106         return nullptr;
1107     }
1108     return wantSender;
1109 }
1110 
SendWantSender(const sptr<IWantSender> & target,const SenderInfo & senderInfo)1111 int AbilityManagerProxy::SendWantSender(const sptr<IWantSender> &target, const SenderInfo &senderInfo)
1112 {
1113     MessageParcel data;
1114     MessageParcel reply;
1115     MessageOption option;
1116     if (!WriteInterfaceToken(data)) {
1117         return INNER_ERR;
1118     }
1119     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1120         HILOG_ERROR("target write failed.");
1121         return INNER_ERR;
1122     }
1123     if (!data.WriteParcelable(&senderInfo)) {
1124         HILOG_ERROR("senderInfo write failed.");
1125         return INNER_ERR;
1126     }
1127 
1128     auto error = Remote()->SendRequest(IAbilityManager::SEND_PENDING_WANT_SENDER, data, reply, option);
1129     if (error != NO_ERROR) {
1130         HILOG_ERROR("Send request error: %{public}d", error);
1131         return error;
1132     }
1133     return reply.ReadInt32();
1134 }
1135 
CancelWantSender(const sptr<IWantSender> & sender)1136 void AbilityManagerProxy::CancelWantSender(const sptr<IWantSender> &sender)
1137 {
1138     MessageParcel data;
1139     MessageParcel reply;
1140     MessageOption option;
1141     if (!WriteInterfaceToken(data)) {
1142         return;
1143     }
1144     if (sender == nullptr || !data.WriteRemoteObject(sender->AsObject())) {
1145         HILOG_ERROR("sender write failed.");
1146         return;
1147     }
1148     auto error = Remote()->SendRequest(IAbilityManager::CANCEL_PENDING_WANT_SENDER, data, reply, option);
1149     if (error != NO_ERROR) {
1150         HILOG_ERROR("Send request error: %{public}d", error);
1151         return;
1152     }
1153 }
1154 
GetPendingWantUid(const sptr<IWantSender> & target)1155 int AbilityManagerProxy::GetPendingWantUid(const sptr<IWantSender> &target)
1156 {
1157     MessageParcel data;
1158     MessageParcel reply;
1159     MessageOption option;
1160     if (!WriteInterfaceToken(data)) {
1161         return INNER_ERR;
1162     }
1163     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1164         HILOG_ERROR("target write failed.");
1165         return ERR_INVALID_VALUE;
1166     }
1167     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_UID, data, reply, option);
1168     if (error != NO_ERROR) {
1169         HILOG_ERROR("Send request error: %{public}d", error);
1170         return INNER_ERR;
1171     }
1172     return reply.ReadInt32();
1173 }
1174 
GetPendingWantUserId(const sptr<IWantSender> & target)1175 int AbilityManagerProxy::GetPendingWantUserId(const sptr<IWantSender> &target)
1176 {
1177     MessageParcel data;
1178     MessageParcel reply;
1179     MessageOption option;
1180     if (!WriteInterfaceToken(data)) {
1181         return INNER_ERR;
1182     }
1183     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1184         HILOG_ERROR("target write failed.");
1185         return ERR_INVALID_VALUE;
1186     }
1187     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_USERID, data, reply, option);
1188     if (error != NO_ERROR) {
1189         HILOG_ERROR("Send request error: %{public}d", error);
1190         return INNER_ERR;
1191     }
1192     return reply.ReadInt32();
1193 }
1194 
GetPendingWantBundleName(const sptr<IWantSender> & target)1195 std::string AbilityManagerProxy::GetPendingWantBundleName(const sptr<IWantSender> &target)
1196 {
1197     MessageParcel data;
1198     MessageParcel reply;
1199     MessageOption option;
1200     if (!WriteInterfaceToken(data)) {
1201         return "";
1202     }
1203     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1204         HILOG_ERROR("target write failed.");
1205         return "";
1206     }
1207     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_BUNDLENAME, data, reply, option);
1208     if (error != NO_ERROR) {
1209         HILOG_ERROR("Send request error: %{public}d", error);
1210         return "";
1211     }
1212     return Str16ToStr8(reply.ReadString16());
1213 }
1214 
GetPendingWantCode(const sptr<IWantSender> & target)1215 int AbilityManagerProxy::GetPendingWantCode(const sptr<IWantSender> &target)
1216 {
1217     MessageParcel data;
1218     MessageParcel reply;
1219     MessageOption option;
1220     if (!WriteInterfaceToken(data)) {
1221         return INNER_ERR;
1222     }
1223     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1224         HILOG_ERROR("target write failed.");
1225         return ERR_INVALID_VALUE;
1226     }
1227     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_CODE, data, reply, option);
1228     if (error != NO_ERROR) {
1229         HILOG_ERROR("Send request error: %{public}d", error);
1230         return INNER_ERR;
1231     }
1232     return reply.ReadInt32();
1233 }
1234 
GetPendingWantType(const sptr<IWantSender> & target)1235 int AbilityManagerProxy::GetPendingWantType(const sptr<IWantSender> &target)
1236 {
1237     MessageParcel data;
1238     MessageParcel reply;
1239     MessageOption option;
1240     if (!WriteInterfaceToken(data)) {
1241         return INNER_ERR;
1242     }
1243     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1244         HILOG_ERROR("target write failed.");
1245         return ERR_INVALID_VALUE;
1246     }
1247     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_TYPE, data, reply, option);
1248     if (error != NO_ERROR) {
1249         HILOG_ERROR("Send request error: %{public}d", error);
1250         return INNER_ERR;
1251     }
1252     return reply.ReadInt32();
1253 }
1254 
RegisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)1255 void AbilityManagerProxy::RegisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver)
1256 {
1257     MessageParcel data;
1258     MessageParcel reply;
1259     MessageOption option;
1260     if (!WriteInterfaceToken(data)) {
1261         return;
1262     }
1263     if (sender == nullptr || !data.WriteRemoteObject(sender->AsObject())) {
1264         HILOG_ERROR("sender write failed.");
1265         return;
1266     }
1267     if (receiver == nullptr || !data.WriteRemoteObject(receiver->AsObject())) {
1268         HILOG_ERROR("receiver write failed.");
1269         return;
1270     }
1271     auto error = Remote()->SendRequest(IAbilityManager::REGISTER_CANCEL_LISTENER, data, reply, option);
1272     if (error != NO_ERROR) {
1273         HILOG_ERROR("Send request error: %{public}d", error);
1274         return;
1275     }
1276 }
1277 
UnregisterCancelListener(const sptr<IWantSender> & sender,const sptr<IWantReceiver> & receiver)1278 void AbilityManagerProxy::UnregisterCancelListener(const sptr<IWantSender> &sender, const sptr<IWantReceiver> &receiver)
1279 {
1280     MessageParcel data;
1281     MessageParcel reply;
1282     MessageOption option;
1283     if (!WriteInterfaceToken(data)) {
1284         return;
1285     }
1286     if (sender == nullptr || !data.WriteRemoteObject(sender->AsObject())) {
1287         HILOG_ERROR("sender write failed.");
1288         return;
1289     }
1290     if (receiver == nullptr || !data.WriteRemoteObject(receiver->AsObject())) {
1291         HILOG_ERROR("receiver write failed.");
1292         return;
1293     }
1294     auto error = Remote()->SendRequest(IAbilityManager::UNREGISTER_CANCEL_LISTENER, data, reply, option);
1295     if (error != NO_ERROR) {
1296         HILOG_ERROR("Send request error: %{public}d", error);
1297         return;
1298     }
1299 }
1300 
GetPendingRequestWant(const sptr<IWantSender> & target,std::shared_ptr<Want> & want)1301 int AbilityManagerProxy::GetPendingRequestWant(const sptr<IWantSender> &target, std::shared_ptr<Want> &want)
1302 {
1303     MessageParcel data;
1304     MessageParcel reply;
1305     MessageOption option;
1306     if (!WriteInterfaceToken(data)) {
1307         return INNER_ERR;
1308     }
1309     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1310         HILOG_ERROR("target write failed.");
1311         return INNER_ERR;
1312     }
1313     if (want == nullptr || !data.WriteParcelable(want.get())) {
1314         HILOG_ERROR("want write failed.");
1315         return INNER_ERR;
1316     }
1317     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_REQUEST_WANT, data, reply, option);
1318     if (error != NO_ERROR) {
1319         HILOG_ERROR("Send request error: %{public}d", error);
1320         return error;
1321     }
1322     std::unique_ptr<Want> wantInfo(reply.ReadParcelable<Want>());
1323     if (!wantInfo) {
1324         HILOG_ERROR("readParcelableInfo failed");
1325         return INNER_ERR;
1326     }
1327     want = std::move(wantInfo);
1328 
1329     return NO_ERROR;
1330 }
1331 
GetWantSenderInfo(const sptr<IWantSender> & target,std::shared_ptr<WantSenderInfo> & info)1332 int AbilityManagerProxy::GetWantSenderInfo(const sptr<IWantSender> &target, std::shared_ptr<WantSenderInfo> &info)
1333 {
1334     MessageParcel data;
1335     MessageParcel reply;
1336     MessageOption option;
1337     if (!WriteInterfaceToken(data)) {
1338         return INNER_ERR;
1339     }
1340     if (target == nullptr || !data.WriteRemoteObject(target->AsObject())) {
1341         HILOG_ERROR("target write failed.");
1342         return INNER_ERR;
1343     }
1344     if (info == nullptr || !data.WriteParcelable(info.get())) {
1345         HILOG_ERROR("info write failed.");
1346         return INNER_ERR;
1347     }
1348     auto error = Remote()->SendRequest(IAbilityManager::GET_PENDING_WANT_SENDER_INFO, data, reply, option);
1349     if (error != NO_ERROR) {
1350         HILOG_ERROR("Send request error: %{public}d", error);
1351         return error;
1352     }
1353     std::unique_ptr<WantSenderInfo> wantSenderInfo(reply.ReadParcelable<WantSenderInfo>());
1354     if (!wantSenderInfo) {
1355         HILOG_ERROR("readParcelable Info failed");
1356         return INNER_ERR;
1357     }
1358     info = std::move(wantSenderInfo);
1359 
1360     return NO_ERROR;
1361 }
1362 
GetAppMemorySize()1363 int AbilityManagerProxy::GetAppMemorySize()
1364 {
1365     MessageParcel data;
1366     MessageParcel reply;
1367     MessageOption option;
1368     if (!WriteInterfaceToken(data)) {
1369         HILOG_ERROR("WriteInterfaceToken faild");
1370         return INNER_ERR;
1371     }
1372     auto error = Remote()->SendRequest(IAbilityManager::GET_APP_MEMORY_SIZE, data, reply, option);
1373     if (error != NO_ERROR) {
1374         HILOG_ERROR("Send request error: %{public}d", error);
1375         return error;
1376     }
1377     return reply.ReadInt32();
1378 }
1379 
IsRamConstrainedDevice()1380 bool AbilityManagerProxy::IsRamConstrainedDevice()
1381 {
1382     MessageParcel data;
1383     MessageParcel reply;
1384     MessageOption option;
1385     if (!WriteInterfaceToken(data)) {
1386         HILOG_ERROR("WriteInterfaceToken faild");
1387         return false;
1388     }
1389     auto error = Remote()->SendRequest(IAbilityManager::IS_RAM_CONSTRAINED_DEVICE, data, reply, option);
1390     if (error != NO_ERROR) {
1391         HILOG_ERROR("Send request error: %{public}d", error);
1392         return false;
1393     }
1394     return reply.ReadBool();
1395 }
1396 
ContinueMission(const std::string & srcDeviceId,const std::string & dstDeviceId,int32_t missionId,const sptr<IRemoteObject> & callBack,AAFwk::WantParams & wantParams)1397 int AbilityManagerProxy::ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId,
1398     int32_t missionId, const sptr<IRemoteObject> &callBack, AAFwk::WantParams &wantParams)
1399 {
1400     MessageParcel data;
1401     MessageParcel reply;
1402     MessageOption option;
1403     if (!WriteInterfaceToken(data)) {
1404         return INNER_ERR;
1405     }
1406     if (!data.WriteString(srcDeviceId)) {
1407         HILOG_ERROR("srcDeviceId write failed.");
1408         return INNER_ERR;
1409     }
1410     if (!data.WriteString(dstDeviceId)) {
1411         HILOG_ERROR("dstDeviceId write failed.");
1412         return INNER_ERR;
1413     }
1414     if (!data.WriteInt32(missionId)) {
1415         HILOG_ERROR("missionId write failed.");
1416         return INNER_ERR;
1417     }
1418     if (!data.WriteRemoteObject(callBack)) {
1419         HILOG_ERROR("callBack write failed.");
1420         return INNER_ERR;
1421     }
1422     if (!data.WriteParcelable(&wantParams)) {
1423         HILOG_ERROR("wantParams write failed.");
1424         return INNER_ERR;
1425     }
1426 
1427     auto error = Remote()->SendRequest(IAbilityManager::CONTINUE_MISSION, data, reply, option);
1428     if (error != NO_ERROR) {
1429         HILOG_ERROR("Send request error: %{public}d", error);
1430         return error;
1431     }
1432     return reply.ReadInt32();
1433 }
1434 
ContinueAbility(const std::string & deviceId,int32_t missionId,uint32_t versionCode)1435 int AbilityManagerProxy::ContinueAbility(const std::string &deviceId, int32_t missionId, uint32_t versionCode)
1436 {
1437     MessageParcel data;
1438     MessageParcel reply;
1439     MessageOption option;
1440     if (!WriteInterfaceToken(data)) {
1441         return INNER_ERR;
1442     }
1443     if (!data.WriteString(deviceId)) {
1444         HILOG_ERROR("deviceId write failed.");
1445         return INNER_ERR;
1446     }
1447     if (!data.WriteInt32(missionId)) {
1448         HILOG_ERROR("missionId write failed.");
1449         return INNER_ERR;
1450     }
1451     if (!data.WriteUint32(versionCode)) {
1452         HILOG_ERROR("versionCode write failed.");
1453         return INNER_ERR;
1454     }
1455 
1456     auto error = Remote()->SendRequest(IAbilityManager::CONTINUE_ABILITY, data, reply, option);
1457     if (error != NO_ERROR) {
1458         HILOG_ERROR("Send request error: %{public}d", error);
1459         return error;
1460     }
1461     return reply.ReadInt32();
1462 }
1463 
StartContinuation(const Want & want,const sptr<IRemoteObject> & abilityToken,int32_t status)1464 int AbilityManagerProxy::StartContinuation(const Want &want, const sptr<IRemoteObject> &abilityToken, int32_t status)
1465 {
1466     MessageParcel data;
1467     MessageParcel reply;
1468     MessageOption option;
1469     if (!WriteInterfaceToken(data)) {
1470         return INNER_ERR;
1471     }
1472     if (!data.WriteParcelable(&want)) {
1473         HILOG_ERROR("want write failed.");
1474         return INNER_ERR;
1475     }
1476     if (!data.WriteRemoteObject(abilityToken)) {
1477         HILOG_ERROR("abilityToken write failed.");
1478         return INNER_ERR;
1479     }
1480     if (!data.WriteInt32(status)) {
1481         HILOG_ERROR("status write failed.");
1482         return INNER_ERR;
1483     }
1484     auto error = Remote()->SendRequest(IAbilityManager::START_CONTINUATION, data, reply, option);
1485     if (error != NO_ERROR) {
1486         HILOG_ERROR("Send request error: %{public}d", error);
1487         return error;
1488     }
1489     return reply.ReadInt32();
1490 }
1491 
NotifyCompleteContinuation(const std::string & deviceId,int32_t sessionId,bool isSuccess)1492 void AbilityManagerProxy::NotifyCompleteContinuation(const std::string &deviceId, int32_t sessionId, bool isSuccess)
1493 {
1494     MessageParcel data;
1495     MessageParcel reply;
1496     MessageOption option;
1497     if (!WriteInterfaceToken(data)) {
1498         return;
1499     }
1500     if (!data.WriteString(deviceId)) {
1501         HILOG_ERROR("deviceId write failed.");
1502         return;
1503     }
1504     if (!data.WriteInt32(sessionId)) {
1505         HILOG_ERROR("sessionId write failed.");
1506         return;
1507     }
1508     if (!data.WriteBool(isSuccess)) {
1509         HILOG_ERROR("result write failed.");
1510         return;
1511     }
1512 
1513     auto error = Remote()->SendRequest(IAbilityManager::NOTIFY_COMPLETE_CONTINUATION, data, reply, option);
1514     if (error != NO_ERROR) {
1515         HILOG_ERROR("Send request error: %{public}d", error);
1516         return;
1517     }
1518 }
1519 
NotifyContinuationResult(int32_t missionId,int32_t result)1520 int AbilityManagerProxy::NotifyContinuationResult(int32_t missionId, int32_t result)
1521 {
1522     MessageParcel data;
1523     MessageParcel reply;
1524     MessageOption option;
1525     if (!WriteInterfaceToken(data)) {
1526         return INNER_ERR;
1527     }
1528     if (!data.WriteInt32(missionId)) {
1529         HILOG_ERROR("missionId write failed.");
1530         return INNER_ERR;
1531     }
1532     if (!data.WriteInt32(result)) {
1533         HILOG_ERROR("result write failed.");
1534         return INNER_ERR;
1535     }
1536 
1537     auto error = Remote()->SendRequest(IAbilityManager::NOTIFY_CONTINUATION_RESULT, data, reply, option);
1538     if (error != NO_ERROR) {
1539         HILOG_ERROR("Send request error: %{public}d", error);
1540         return error;
1541     }
1542     return reply.ReadInt32();
1543 }
1544 
LockMissionForCleanup(int32_t missionId)1545 int AbilityManagerProxy::LockMissionForCleanup(int32_t missionId)
1546 {
1547     int error;
1548     MessageParcel data;
1549     MessageParcel reply;
1550     MessageOption option;
1551 
1552     if (!WriteInterfaceToken(data)) {
1553         return INNER_ERR;
1554     }
1555     if (!data.WriteInt32(missionId)) {
1556         HILOG_ERROR("lock mission by id , WriteInt32 fail.");
1557         return ERR_INVALID_VALUE;
1558     }
1559     error = Remote()->SendRequest(IAbilityManager::LOCK_MISSION_FOR_CLEANUP, data, reply, option);
1560     if (error != NO_ERROR) {
1561         HILOG_ERROR("lock mission by id , error: %d", error);
1562         return error;
1563     }
1564     return reply.ReadInt32();
1565 }
1566 
UnlockMissionForCleanup(int32_t missionId)1567 int AbilityManagerProxy::UnlockMissionForCleanup(int32_t missionId)
1568 {
1569     int error;
1570     MessageParcel data;
1571     MessageParcel reply;
1572     MessageOption option;
1573 
1574     if (!WriteInterfaceToken(data)) {
1575         return INNER_ERR;
1576     }
1577     if (!data.WriteInt32(missionId)) {
1578         HILOG_ERROR("unlock mission by id , WriteInt32 fail.");
1579         return ERR_INVALID_VALUE;
1580     }
1581     error = Remote()->SendRequest(IAbilityManager::UNLOCK_MISSION_FOR_CLEANUP, data, reply, option);
1582     if (error != NO_ERROR) {
1583         HILOG_ERROR("unlock mission by id , error: %d", error);
1584         return error;
1585     }
1586     return reply.ReadInt32();
1587 }
1588 
RegisterMissionListener(const sptr<IMissionListener> & listener)1589 int AbilityManagerProxy::RegisterMissionListener(const sptr<IMissionListener> &listener)
1590 {
1591     int error;
1592     MessageParcel data;
1593     MessageParcel reply;
1594     MessageOption option;
1595     if (!listener) {
1596         HILOG_ERROR("register mission listener, listener is nullptr");
1597         return ERR_INVALID_VALUE;
1598     }
1599 
1600     if (!WriteInterfaceToken(data)) {
1601         return INNER_ERR;
1602     }
1603     if (!data.WriteRemoteObject(listener->AsObject())) {
1604         HILOG_ERROR("write mission listener failed when register mission listener.");
1605         return ERR_INVALID_VALUE;
1606     }
1607 
1608     error = Remote()->SendRequest(IAbilityManager::REGISTER_MISSION_LISTENER, data, reply, option);
1609     if (error != NO_ERROR) {
1610         HILOG_ERROR("Send request error: %{public}d", error);
1611         return error;
1612     }
1613     return reply.ReadInt32();
1614 }
1615 
RegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)1616 int AbilityManagerProxy::RegisterMissionListener(const std::string &deviceId,
1617     const sptr<IRemoteMissionListener> &listener)
1618 {
1619     MessageParcel data;
1620     MessageParcel reply;
1621     MessageOption option;
1622     if (!WriteInterfaceToken(data)) {
1623         return INNER_ERR;
1624     }
1625     if (!data.WriteString(deviceId)) {
1626         HILOG_ERROR("deviceId write failed.");
1627         return INNER_ERR;
1628     }
1629     if (!data.WriteRemoteObject(listener->AsObject())) {
1630         HILOG_ERROR("listener write failed.");
1631         return INNER_ERR;
1632     }
1633 
1634     auto error = Remote()->SendRequest(IAbilityManager::REGISTER_REMOTE_MISSION_LISTENER, data, reply, option);
1635     if (error != NO_ERROR) {
1636         HILOG_ERROR("Send request error: %{public}d", error);
1637         return error;
1638     }
1639     return reply.ReadInt32();
1640 }
1641 
UnRegisterMissionListener(const sptr<IMissionListener> & listener)1642 int AbilityManagerProxy::UnRegisterMissionListener(const sptr<IMissionListener> &listener)
1643 {
1644     int error;
1645     MessageParcel data;
1646     MessageParcel reply;
1647     MessageOption option;
1648     if (!listener) {
1649         HILOG_ERROR("unregister mission listener, listener is nullptr");
1650         return ERR_INVALID_VALUE;
1651     }
1652 
1653     if (!WriteInterfaceToken(data)) {
1654         return INNER_ERR;
1655     }
1656     if (!data.WriteRemoteObject(listener->AsObject())) {
1657         HILOG_ERROR("write mission listener failed when unregister mission listener.");
1658         return ERR_INVALID_VALUE;
1659     }
1660 
1661     error = Remote()->SendRequest(IAbilityManager::UNREGISTER_MISSION_LISTENER, data, reply, option);
1662     if (error != NO_ERROR) {
1663         HILOG_ERROR("Send request error: %{public}d", error);
1664         return error;
1665     }
1666     return reply.ReadInt32();
1667 }
1668 
GetMissionInfos(const std::string & deviceId,int32_t numMax,std::vector<MissionInfo> & missionInfos)1669 int AbilityManagerProxy::GetMissionInfos(const std::string& deviceId, int32_t numMax,
1670     std::vector<MissionInfo> &missionInfos)
1671 {
1672     int error;
1673     MessageParcel data;
1674     MessageParcel reply;
1675     MessageOption option;
1676     if (!WriteInterfaceToken(data)) {
1677         return INNER_ERR;
1678     }
1679     if (!data.WriteString16(Str8ToStr16(deviceId))) {
1680         HILOG_ERROR("write deviceId failed when GetMissionInfos.");
1681         return ERR_INVALID_VALUE;
1682     }
1683     if (!data.WriteInt32(numMax)) {
1684         HILOG_ERROR("GetMissionInfos numMax write failed.");
1685         return ERR_INVALID_VALUE;
1686     }
1687     error = Remote()->SendRequest(IAbilityManager::GET_MISSION_INFOS, data, reply, option);
1688     if (error != NO_ERROR) {
1689         HILOG_ERROR("GetMissionInfos Send request error: %{public}d", error);
1690         return error;
1691     }
1692     error = GetParcelableInfos<MissionInfo>(reply, missionInfos);
1693     if (error != NO_ERROR) {
1694         HILOG_ERROR("GetMissionInfos error: %{public}d", error);
1695         return error;
1696     }
1697     return reply.ReadInt32();
1698 }
1699 
GetMissionInfo(const std::string & deviceId,int32_t missionId,MissionInfo & missionInfo)1700 int AbilityManagerProxy::GetMissionInfo(const std::string& deviceId, int32_t missionId,
1701     MissionInfo &missionInfo)
1702 {
1703     int error;
1704     MessageParcel data;
1705     MessageParcel reply;
1706     MessageOption option;
1707     if (!WriteInterfaceToken(data)) {
1708         return INNER_ERR;
1709     }
1710     if (!data.WriteString16(Str8ToStr16(deviceId))) {
1711         HILOG_ERROR("write deviceId failed when GetMissionInfo.");
1712         return ERR_INVALID_VALUE;
1713     }
1714     if (!data.WriteInt32(missionId)) {
1715         HILOG_ERROR("GetMissionInfo write missionId failed.");
1716         return ERR_INVALID_VALUE;
1717     }
1718     error = Remote()->SendRequest(IAbilityManager::GET_MISSION_INFO_BY_ID, data, reply, option);
1719     if (error != NO_ERROR) {
1720         HILOG_ERROR("GetMissionInfo Send request error: %{public}d", error);
1721         return error;
1722     }
1723 
1724     std::unique_ptr<MissionInfo> info(reply.ReadParcelable<MissionInfo>());
1725     if (!info) {
1726         HILOG_ERROR("read missioninfo failed.");
1727         return ERR_UNKNOWN_OBJECT;
1728     }
1729     missionInfo = *info;
1730     return reply.ReadInt32();
1731 }
1732 
CleanMission(int32_t missionId)1733 int AbilityManagerProxy::CleanMission(int32_t missionId)
1734 {
1735     int error;
1736     MessageParcel data;
1737     MessageParcel reply;
1738     MessageOption option;
1739 
1740     if (!WriteInterfaceToken(data)) {
1741         return INNER_ERR;
1742     }
1743     if (!data.WriteInt32(missionId)) {
1744         HILOG_ERROR("clean mission by id , WriteInt32 fail.");
1745         return ERR_INVALID_VALUE;
1746     }
1747     error = Remote()->SendRequest(IAbilityManager::CLEAN_MISSION, data, reply, option);
1748     if (error != NO_ERROR) {
1749         HILOG_ERROR("clean mission by id , error: %d", error);
1750         return error;
1751     }
1752     return reply.ReadInt32();
1753 }
1754 
CleanAllMissions()1755 int AbilityManagerProxy::CleanAllMissions()
1756 {
1757     int error;
1758     MessageParcel data;
1759     MessageParcel reply;
1760     MessageOption option;
1761 
1762     if (!WriteInterfaceToken(data)) {
1763         return INNER_ERR;
1764     }
1765     error = Remote()->SendRequest(IAbilityManager::CLEAN_ALL_MISSIONS, data, reply, option);
1766     if (error != NO_ERROR) {
1767         HILOG_ERROR("lock mission by id ,SendRequest error: %d", error);
1768         return error;
1769     }
1770     return reply.ReadInt32();
1771 }
1772 
MoveMissionToFront(int32_t missionId)1773 int AbilityManagerProxy::MoveMissionToFront(int32_t missionId)
1774 {
1775     int error;
1776     MessageParcel data;
1777     MessageParcel reply;
1778     MessageOption option;
1779 
1780     if (!WriteInterfaceToken(data)) {
1781         return INNER_ERR;
1782     }
1783     if (!data.WriteInt32(missionId)) {
1784         HILOG_ERROR("move mission to front , WriteInt32 fail.");
1785         return ERR_INVALID_VALUE;
1786     }
1787     error = Remote()->SendRequest(IAbilityManager::MOVE_MISSION_TO_FRONT, data, reply, option);
1788     if (error != NO_ERROR) {
1789         HILOG_ERROR("move mission to front, SendRequest error: %d", error);
1790         return error;
1791     }
1792     return reply.ReadInt32();
1793 }
1794 
MoveMissionToFront(int32_t missionId,const StartOptions & startOptions)1795 int AbilityManagerProxy::MoveMissionToFront(int32_t missionId, const StartOptions &startOptions)
1796 {
1797     int error;
1798     MessageParcel data;
1799     MessageParcel reply;
1800     MessageOption option;
1801 
1802     if (!WriteInterfaceToken(data)) {
1803         return INNER_ERR;
1804     }
1805     if (!data.WriteInt32(missionId)) {
1806         HILOG_ERROR("move mission to front , WriteInt32 fail.");
1807         return ERR_INVALID_VALUE;
1808     }
1809     if (!data.WriteParcelable(&startOptions)) {
1810         HILOG_ERROR("startOptions write failed.");
1811         return INNER_ERR;
1812     }
1813     error = Remote()->SendRequest(IAbilityManager::MOVE_MISSION_TO_FRONT_BY_OPTIONS, data, reply, option);
1814     if (error != NO_ERROR) {
1815         HILOG_ERROR("move mission to front, SendRequest error: %d", error);
1816         return error;
1817     }
1818     return reply.ReadInt32();
1819 }
1820 
StartUser(int userId)1821 int AbilityManagerProxy::StartUser(int userId)
1822 {
1823     int error;
1824     MessageParcel data;
1825     MessageParcel reply;
1826     MessageOption option;
1827 
1828     if (!WriteInterfaceToken(data)) {
1829         return INNER_ERR;
1830     }
1831     if (!data.WriteInt32(userId)) {
1832         HILOG_ERROR("StartUser:WriteInt32 fail.");
1833         return ERR_INVALID_VALUE;
1834     }
1835     error = Remote()->SendRequest(IAbilityManager::START_USER, data, reply, option);
1836     if (error != NO_ERROR) {
1837         HILOG_ERROR("StartUser:SendRequest error: %d", error);
1838         return error;
1839     }
1840     return reply.ReadInt32();
1841 }
1842 
StopUser(int userId,const sptr<IStopUserCallback> & callback)1843 int AbilityManagerProxy::StopUser(int userId, const sptr<IStopUserCallback> &callback)
1844 {
1845     int error;
1846     MessageParcel data;
1847     MessageParcel reply;
1848     MessageOption option;
1849 
1850     if (!WriteInterfaceToken(data)) {
1851         return INNER_ERR;
1852     }
1853     if (!data.WriteInt32(userId)) {
1854         HILOG_ERROR("StopUser:WriteInt32 fail.");
1855         return ERR_INVALID_VALUE;
1856     }
1857 
1858     if (!callback) {
1859         data.WriteBool(false);
1860     } else {
1861         data.WriteBool(true);
1862         if (!data.WriteRemoteObject(callback->AsObject())) {
1863             HILOG_ERROR("StopUser:write IStopUserCallback fail.");
1864             return ERR_INVALID_VALUE;
1865         }
1866     }
1867     error = Remote()->SendRequest(IAbilityManager::STOP_USER, data, reply, option);
1868     if (error != NO_ERROR) {
1869         HILOG_ERROR("StopUser:SendRequest error: %d", error);
1870         return error;
1871     }
1872     return reply.ReadInt32();
1873 }
1874 
1875 #ifdef SUPPORT_GRAPHICS
SetMissionLabel(const sptr<IRemoteObject> & token,const std::string & label)1876 int AbilityManagerProxy::SetMissionLabel(const sptr<IRemoteObject> &token, const std::string &label)
1877 {
1878     MessageParcel data;
1879     MessageParcel reply;
1880     MessageOption option;
1881     if (!WriteInterfaceToken(data)) {
1882         return INNER_ERR;
1883     }
1884     if (!data.WriteRemoteObject(token)) {
1885         HILOG_ERROR("SetMissionLabel write token failed.");
1886         return ERR_INVALID_VALUE;
1887     }
1888     if (!data.WriteString16(Str8ToStr16(label))) {
1889         HILOG_ERROR("SetMissionLabel write label failed.");
1890         return ERR_INVALID_VALUE;
1891     }
1892     auto error = Remote()->SendRequest(IAbilityManager::SET_MISSION_LABEL, data, reply, option);
1893     if (error != NO_ERROR) {
1894         HILOG_ERROR("SetMissionLabel Send request error: %{public}d", error);
1895         return error;
1896     }
1897     return reply.ReadInt32();
1898 }
1899 
SetMissionIcon(const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::Media::PixelMap> & icon)1900 int AbilityManagerProxy::SetMissionIcon(const sptr<IRemoteObject> &token,
1901     const std::shared_ptr<OHOS::Media::PixelMap> &icon)
1902 {
1903     if (!token || !icon) {
1904         HILOG_ERROR("SetMissionIcon abilitytoken or icon is invalid.");
1905         return ERR_INVALID_VALUE;
1906     }
1907 
1908     MessageParcel data;
1909     MessageParcel reply;
1910     MessageOption option;
1911     if (!WriteInterfaceToken(data)) {
1912         return INNER_ERR;
1913     }
1914     if (!data.WriteRemoteObject(token)) {
1915         HILOG_ERROR("SetMissionIcon write token failed.");
1916         return ERR_INVALID_VALUE;
1917     }
1918 
1919     if (!data.WriteParcelable(icon.get())) {
1920         HILOG_ERROR("SetMissionIcon write icon failed.");
1921         return ERR_INVALID_VALUE;
1922     }
1923 
1924     auto error = Remote()->SendRequest(IAbilityManager::SET_MISSION_ICON, data, reply, option);
1925     if (error != NO_ERROR) {
1926         HILOG_ERROR("SetMissionIcon Send request error: %{public}d", error);
1927         return error;
1928     }
1929     return reply.ReadInt32();
1930 }
1931 
RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler> & handler)1932 int AbilityManagerProxy::RegisterWindowManagerServiceHandler(const sptr<IWindowManagerServiceHandler>& handler)
1933 {
1934     if (!handler) {
1935         HILOG_ERROR("%{public}s: handler is nullptr.", __func__);
1936         return INNER_ERR;
1937     }
1938     MessageParcel data;
1939     if (!WriteInterfaceToken(data)) {
1940         HILOG_ERROR("%{public}s: write interface token failed.", __func__);
1941         return INNER_ERR;
1942     }
1943     if (!data.WriteRemoteObject(handler->AsObject())) {
1944         HILOG_ERROR("%{public}s: handler write failed.", __func__);
1945         return INNER_ERR;
1946     }
1947     MessageOption option;
1948     MessageParcel reply;
1949     auto error = Remote()->SendRequest(IAbilityManager::REGISTER_WMS_HANDLER, data, reply, option);
1950     if (error != NO_ERROR) {
1951         HILOG_ERROR("%{public}s: send request error: %{public}d", __func__, error);
1952         return error;
1953     }
1954     return reply.ReadInt32();
1955 }
1956 
CompleteFirstFrameDrawing(const sptr<IRemoteObject> & abilityToken)1957 void AbilityManagerProxy::CompleteFirstFrameDrawing(const sptr<IRemoteObject> &abilityToken)
1958 {
1959     MessageParcel data;
1960     if (!WriteInterfaceToken(data)) {
1961         HILOG_ERROR("%{public}s: write interface token failed.", __func__);
1962         return;
1963     }
1964     if (!data.WriteRemoteObject(abilityToken)) {
1965         HILOG_ERROR("%{public}s: abilityToken write failed.", __func__);
1966         return;
1967     }
1968     MessageOption option;
1969     MessageParcel reply;
1970     auto error = Remote()->SendRequest(IAbilityManager::COMPLETEFIRSTFRAMEDRAWING, data, reply, option);
1971     if (error != NO_ERROR) {
1972         HILOG_ERROR("%{public}s: send request error: %{public}d", __func__, error);
1973     }
1974 }
1975 #endif
1976 
GetAbilityRunningInfos(std::vector<AbilityRunningInfo> & info)1977 int AbilityManagerProxy::GetAbilityRunningInfos(std::vector<AbilityRunningInfo> &info)
1978 {
1979     MessageParcel data;
1980     MessageParcel reply;
1981     MessageOption option;
1982 
1983     if (!WriteInterfaceToken(data)) {
1984         return INNER_ERR;
1985     }
1986 
1987     auto error = Remote()->SendRequest(IAbilityManager::GET_ABILITY_RUNNING_INFO, data, reply, option);
1988     if (error != NO_ERROR) {
1989         HILOG_ERROR("Get ability running info, error: %{public}d", error);
1990         return error;
1991     }
1992     error = GetParcelableInfos<AbilityRunningInfo>(reply, info);
1993     if (error != NO_ERROR) {
1994         HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
1995         return error;
1996     }
1997     return reply.ReadInt32();
1998 }
1999 
GetExtensionRunningInfos(int upperLimit,std::vector<ExtensionRunningInfo> & info)2000 int AbilityManagerProxy::GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info)
2001 {
2002     MessageParcel data;
2003     MessageParcel reply;
2004     MessageOption option;
2005 
2006     if (!WriteInterfaceToken(data)) {
2007         return INNER_ERR;
2008     }
2009 
2010     if (!data.WriteInt32(upperLimit)) {
2011         HILOG_ERROR("upperLimit write failed.");
2012         return INNER_ERR;
2013     }
2014 
2015     auto error = Remote()->SendRequest(IAbilityManager::GET_EXTENSION_RUNNING_INFO, data, reply, option);
2016     if (error != NO_ERROR) {
2017         HILOG_ERROR("Get extension running info failed., error: %{public}d", error);
2018         return error;
2019     }
2020     error = GetParcelableInfos<ExtensionRunningInfo>(reply, info);
2021     if (error != NO_ERROR) {
2022         HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
2023         return error;
2024     }
2025     return reply.ReadInt32();
2026 }
2027 
GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> & info)2028 int AbilityManagerProxy::GetProcessRunningInfos(std::vector<AppExecFwk::RunningProcessInfo> &info)
2029 {
2030     MessageParcel data;
2031     MessageParcel reply;
2032     MessageOption option;
2033 
2034     if (!WriteInterfaceToken(data)) {
2035         return INNER_ERR;
2036     }
2037 
2038     auto error = Remote()->SendRequest(IAbilityManager::GET_PROCESS_RUNNING_INFO, data, reply, option);
2039     if (error != NO_ERROR) {
2040         HILOG_ERROR("Get process running info, error: %{public}d", error);
2041         return error;
2042     }
2043     error = GetParcelableInfos<AppExecFwk::RunningProcessInfo>(reply, info);
2044     if (error != NO_ERROR) {
2045         HILOG_ERROR("GetParcelableInfos fail, error: %{public}d", error);
2046         return error;
2047     }
2048     return reply.ReadInt32();
2049 }
2050 
StartSyncRemoteMissions(const std::string & devId,bool fixConflict,int64_t tag)2051 int AbilityManagerProxy::StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag)
2052 {
2053     HILOG_INFO("called");
2054     MessageParcel data;
2055     MessageParcel reply;
2056     MessageOption option;
2057 
2058     if (!WriteInterfaceToken(data)) {
2059         HILOG_ERROR("WriteInterfaceToken failed");
2060         return ERR_INVALID_VALUE;
2061     }
2062     if (!data.WriteString(devId)) {
2063         HILOG_ERROR("write deviceId fail.");
2064         return ERR_INVALID_VALUE;
2065     }
2066 
2067     if (!data.WriteBool(fixConflict)) {
2068         HILOG_ERROR("WriteBool fail.");
2069         return ERR_INVALID_VALUE;
2070     }
2071 
2072     if (!data.WriteInt64(tag)) {
2073         HILOG_ERROR("WriteInt64 fail.");
2074         return ERR_INVALID_VALUE;
2075     }
2076 
2077     auto error = Remote()->SendRequest(IAbilityManager::START_SYNC_MISSIONS, data, reply, option);
2078     if (error != NO_ERROR) {
2079         HILOG_ERROR("Send request error: %{public}d", error);
2080         return error;
2081     }
2082     return reply.ReadInt32();
2083 }
2084 
StopSyncRemoteMissions(const std::string & devId)2085 int32_t AbilityManagerProxy::StopSyncRemoteMissions(const std::string& devId)
2086 {
2087     HILOG_INFO("called");
2088     MessageParcel data;
2089     MessageParcel reply;
2090     MessageOption option;
2091 
2092     if (!WriteInterfaceToken(data)) {
2093         HILOG_ERROR("WriteInterfaceToken failed");
2094         return ERR_INVALID_VALUE;
2095     }
2096     if (!data.WriteString(devId)) {
2097         HILOG_ERROR("write deviceId fail.");
2098         return ERR_INVALID_VALUE;
2099     }
2100     auto error = Remote()->SendRequest(IAbilityManager::STOP_SYNC_MISSIONS, data, reply, option);
2101     if (error != NO_ERROR) {
2102         HILOG_ERROR("Send request error: %{public}d", error);
2103         return error;
2104     }
2105     return reply.ReadInt32();
2106 }
2107 
UnRegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)2108 int AbilityManagerProxy::UnRegisterMissionListener(const std::string &deviceId,
2109     const sptr<IRemoteMissionListener> &listener)
2110 {
2111     MessageParcel data;
2112     MessageParcel reply;
2113     MessageOption option;
2114     if (!WriteInterfaceToken(data)) {
2115         return INNER_ERR;
2116     }
2117     if (!data.WriteString(deviceId)) {
2118         HILOG_ERROR("deviceId write failed.");
2119         return INNER_ERR;
2120     }
2121     if (!data.WriteRemoteObject(listener->AsObject())) {
2122         HILOG_ERROR("listener write failed.");
2123         return INNER_ERR;
2124     }
2125 
2126     auto error = Remote()->SendRequest(IAbilityManager::UNREGISTER_REMOTE_MISSION_LISTENER, data, reply, option);
2127     if (error != NO_ERROR) {
2128         HILOG_ERROR("Send request error: %{public}d", error);
2129         return error;
2130     }
2131     return reply.ReadInt32();
2132 }
2133 
StartAbilityByCall(const Want & want,const sptr<IAbilityConnection> & connect,const sptr<IRemoteObject> & callerToken)2134 int AbilityManagerProxy::StartAbilityByCall(
2135     const Want &want, const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken)
2136 {
2137     HILOG_DEBUG("AbilityManagerProxy::StartAbilityByCall begin.");
2138     int error;
2139     MessageParcel data;
2140     MessageParcel reply;
2141     MessageOption option;
2142 
2143     if (!WriteInterfaceToken(data)) {
2144         return INNER_ERR;
2145     }
2146     if (!data.WriteParcelable(&want)) {
2147         HILOG_ERROR("want write failed.");
2148         return ERR_INVALID_VALUE;
2149     }
2150     if (connect == nullptr) {
2151         HILOG_ERROR("resolve ability fail, connect is nullptr");
2152         return ERR_INVALID_VALUE;
2153     }
2154     if (!data.WriteRemoteObject(connect->AsObject())) {
2155         HILOG_ERROR("resolve write failed.");
2156         return ERR_INVALID_VALUE;
2157     }
2158     if (callerToken) {
2159         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
2160             HILOG_ERROR("Failed to write flag and callerToken.");
2161             return ERR_INVALID_VALUE;
2162         }
2163     } else {
2164         if (!data.WriteBool(false)) {
2165             HILOG_ERROR("Failed to write flag.");
2166             return ERR_INVALID_VALUE;
2167         }
2168     }
2169 
2170     HILOG_DEBUG("AbilityManagerProxy::StartAbilityByCall SendRequest Call.");
2171     error = Remote()->SendRequest(IAbilityManager::START_CALL_ABILITY, data, reply, option);
2172     if (error != NO_ERROR) {
2173         HILOG_ERROR("Send request error: %{public}d", error);
2174         return error;
2175     }
2176     HILOG_DEBUG("AbilityManagerProxy::StartAbilityByCall end.");
2177     return reply.ReadInt32();
2178 }
2179 
CallRequestDone(const sptr<IRemoteObject> & token,const sptr<IRemoteObject> & callStub)2180 void AbilityManagerProxy::CallRequestDone(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &callStub)
2181 {
2182     MessageParcel data;
2183     MessageParcel reply;
2184     MessageOption option(MessageOption::TF_ASYNC);
2185 
2186     if (token == nullptr) {
2187         HILOG_ERROR("Call request done fail, ability token is nullptr.");
2188         return;
2189     }
2190     if (callStub == nullptr) {
2191         HILOG_ERROR("Call request done fail, callStub is nullptr.");
2192         return;
2193     }
2194 
2195     if (!WriteInterfaceToken(data)) {
2196         return;
2197     }
2198     if (!data.WriteRemoteObject(token)) {
2199         HILOG_ERROR("WriteRemoteObject fail, write token fail.");
2200         return;
2201     }
2202     if (!data.WriteRemoteObject(callStub)) {
2203         HILOG_ERROR("WriteRemoteObject fail, write callStub fail.");
2204         return;
2205     }
2206     if (Remote() == nullptr) {
2207         HILOG_ERROR("Call request done fail, Remote() is nullptr.");
2208         return;
2209     }
2210     auto error = Remote()->SendRequest(IAbilityManager::CALL_REQUEST_DONE, data, reply, option);
2211     if (error != NO_ERROR) {
2212         HILOG_ERROR("Send request error: %{public}d", error);
2213         return;
2214     }
2215 }
2216 
ReleaseCall(const sptr<IAbilityConnection> & connect,const AppExecFwk::ElementName & element)2217 int AbilityManagerProxy::ReleaseCall(
2218     const sptr<IAbilityConnection> &connect, const AppExecFwk::ElementName &element)
2219 {
2220     int error;
2221     MessageParcel data;
2222     MessageParcel reply;
2223     MessageOption option;
2224     if (connect == nullptr) {
2225         HILOG_ERROR("release calll ability fail, connect is nullptr");
2226         return ERR_INVALID_VALUE;
2227     }
2228     if (!WriteInterfaceToken(data)) {
2229         return INNER_ERR;
2230     }
2231     if (!data.WriteRemoteObject(connect->AsObject())) {
2232         HILOG_ERROR("release ability connect write failed.");
2233         return ERR_INVALID_VALUE;
2234     }
2235     if (!data.WriteParcelable(&element)) {
2236         HILOG_ERROR("element error.");
2237         return ERR_INVALID_VALUE;
2238     }
2239 
2240     error = Remote()->SendRequest(IAbilityManager::RELEASE_CALL_ABILITY, data, reply, option);
2241     if (error != NO_ERROR) {
2242         HILOG_ERROR("Send request error: %{public}d", error);
2243         return error;
2244     }
2245     return reply.ReadInt32();
2246 }
2247 
RegisterSnapshotHandler(const sptr<ISnapshotHandler> & handler)2248 int AbilityManagerProxy::RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler)
2249 {
2250     MessageParcel data;
2251     MessageParcel reply;
2252     MessageOption option;
2253     if (!WriteInterfaceToken(data)) {
2254         return INNER_ERR;
2255     }
2256     if (!data.WriteRemoteObject(handler->AsObject())) {
2257         HILOG_ERROR("snapshot: handler write failed.");
2258         return INNER_ERR;
2259     }
2260     auto error = Remote()->SendRequest(IAbilityManager::REGISTER_SNAPSHOT_HANDLER, data, reply, option);
2261     if (error != NO_ERROR) {
2262         HILOG_ERROR("snapshot: send request error: %{public}d", error);
2263         return error;
2264     }
2265     return reply.ReadInt32();
2266 }
2267 
SetAbilityController(const sptr<AppExecFwk::IAbilityController> & abilityController,bool imAStabilityTest)2268 int AbilityManagerProxy::SetAbilityController(const sptr<AppExecFwk::IAbilityController> &abilityController,
2269     bool imAStabilityTest)
2270 {
2271     if (!abilityController) {
2272         HILOG_ERROR("abilityController nullptr");
2273         return ERR_INVALID_VALUE;
2274     }
2275     MessageParcel data;
2276     MessageParcel reply;
2277     MessageOption option;
2278     if (!WriteInterfaceToken(data)) {
2279         return INNER_ERR;
2280     }
2281     if (!data.WriteRemoteObject(abilityController->AsObject())) {
2282         HILOG_ERROR("abilityController write failed.");
2283         return ERR_INVALID_VALUE;
2284     }
2285     if (!data.WriteBool(imAStabilityTest)) {
2286         HILOG_ERROR("imAStabilityTest write failed.");
2287         return ERR_INVALID_VALUE;
2288     }
2289     auto error = Remote()->SendRequest(IAbilityManager::SET_ABILITY_CONTROLLER, data, reply, option);
2290     if (error != NO_ERROR) {
2291         HILOG_ERROR("Send request error: %{public}d", error);
2292         return error;
2293     }
2294     return reply.ReadInt32();
2295 }
2296 
SetComponentInterception(const sptr<AppExecFwk::IComponentInterception> & componentInterception)2297 int AbilityManagerProxy::SetComponentInterception(const sptr<AppExecFwk::IComponentInterception> &componentInterception)
2298 {
2299     if (!componentInterception) {
2300         HILOG_ERROR("componentInterception nullptr");
2301         return ERR_INVALID_VALUE;
2302     }
2303     MessageParcel data;
2304     MessageParcel reply;
2305     MessageOption option;
2306     if (!WriteInterfaceToken(data)) {
2307         return INNER_ERR;
2308     }
2309     if (!data.WriteRemoteObject(componentInterception->AsObject())) {
2310         HILOG_ERROR("componentInterception write failed.");
2311         return ERR_INVALID_VALUE;
2312     }
2313     auto error = Remote()->SendRequest(IAbilityManager::SET_COMPONENT_INTERCEPTION, data, reply, option);
2314     if (error != NO_ERROR) {
2315         HILOG_ERROR("Send request error: %{public}d", error);
2316         return error;
2317     }
2318     return reply.ReadInt32();
2319 }
2320 
SendResultToAbilityByToken(const Want & want,const sptr<IRemoteObject> & abilityToken,int32_t requestCode,int32_t resultCode,int32_t userId)2321 int32_t AbilityManagerProxy::SendResultToAbilityByToken(const Want &want, const sptr<IRemoteObject> &abilityToken,
2322     int32_t requestCode, int32_t resultCode, int32_t userId)
2323 {
2324     MessageParcel data;
2325     MessageParcel reply;
2326     MessageOption option;
2327 
2328     if (!WriteInterfaceToken(data)) {
2329         return INNER_ERR;
2330     }
2331     if (!data.WriteParcelable(&want)) {
2332         HILOG_ERROR("want write failed.");
2333         return INNER_ERR;
2334     }
2335     if (!data.WriteRemoteObject(abilityToken)) {
2336         HILOG_ERROR("observer write failed.");
2337         return INNER_ERR;
2338     }
2339     if (!data.WriteInt32(requestCode)) {
2340         HILOG_ERROR("requestCode write failed.");
2341         return ERR_INVALID_VALUE;
2342     }
2343     if (!data.WriteInt32(resultCode)) {
2344         HILOG_ERROR("resultCode write failed.");
2345         return ERR_INVALID_VALUE;
2346     }
2347     if (!data.WriteInt32(userId)) {
2348         HILOG_ERROR("userId write failed.");
2349         return ERR_INVALID_VALUE;
2350     }
2351     auto error = Remote()->SendRequest(IAbilityManager::SEND_ABILITY_RESULT_BY_TOKEN, data, reply, option);
2352     if (error != NO_ERROR) {
2353         HILOG_ERROR("Send request error: %{public}d", error);
2354         return error;
2355     }
2356     return reply.ReadInt32();
2357 }
2358 
IsRunningInStabilityTest()2359 bool AbilityManagerProxy::IsRunningInStabilityTest()
2360 {
2361     MessageParcel data;
2362     MessageParcel reply;
2363     MessageOption option;
2364     if (!WriteInterfaceToken(data)) {
2365         return false;
2366     }
2367     auto error = Remote()->SendRequest(IAbilityManager::IS_USER_A_STABILITY_TEST, data, reply, option);
2368     if (error != NO_ERROR) {
2369         HILOG_ERROR("Send request error: %{public}d", error);
2370         return false;
2371     }
2372     return reply.ReadBool();
2373 }
2374 
StartUserTest(const Want & want,const sptr<IRemoteObject> & observer)2375 int AbilityManagerProxy::StartUserTest(const Want &want, const sptr<IRemoteObject> &observer)
2376 {
2377     MessageParcel data;
2378     MessageParcel reply;
2379     MessageOption option;
2380 
2381     if (!WriteInterfaceToken(data)) {
2382         return INNER_ERR;
2383     }
2384     if (!data.WriteParcelable(&want)) {
2385         HILOG_ERROR("want write failed.");
2386         return INNER_ERR;
2387     }
2388     if (!data.WriteRemoteObject(observer)) {
2389         HILOG_ERROR("observer write failed.");
2390         return INNER_ERR;
2391     }
2392     auto error = Remote()->SendRequest(IAbilityManager::START_USER_TEST, data, reply, option);
2393     if (error != NO_ERROR) {
2394         HILOG_ERROR("Send request error: %{public}d", error);
2395         return error;
2396     }
2397     return reply.ReadInt32();
2398 }
2399 
FinishUserTest(const std::string & msg,const int64_t & resultCode,const std::string & bundleName)2400 int AbilityManagerProxy::FinishUserTest(
2401     const std::string &msg, const int64_t &resultCode, const std::string &bundleName)
2402 {
2403     MessageParcel data;
2404     MessageParcel reply;
2405     MessageOption option;
2406 
2407     if (!WriteInterfaceToken(data)) {
2408         return INNER_ERR;
2409     }
2410     if (!data.WriteString(msg)) {
2411         HILOG_ERROR("msg write failed.");
2412         return ERR_INVALID_VALUE;
2413     }
2414     if (!data.WriteInt64(resultCode)) {
2415         HILOG_ERROR("resultCode:WriteInt64 fail.");
2416         return ERR_INVALID_VALUE;
2417     }
2418     if (!data.WriteString(bundleName)) {
2419         HILOG_ERROR("bundleName write failed.");
2420         return ERR_INVALID_VALUE;
2421     }
2422 
2423     auto error = Remote()->SendRequest(IAbilityManager::FINISH_USER_TEST, data, reply, option);
2424     if (error != NO_ERROR) {
2425         HILOG_ERROR("Send request error: %{public}d", error);
2426         return error;
2427     }
2428     return reply.ReadInt32();
2429 }
2430 
GetTopAbility(sptr<IRemoteObject> & token)2431 int AbilityManagerProxy::GetTopAbility(sptr<IRemoteObject> &token)
2432 {
2433     MessageParcel data;
2434     MessageParcel reply;
2435     MessageOption option;
2436 
2437     if (!WriteInterfaceToken(data)) {
2438         return INNER_ERR;
2439     }
2440 
2441     auto error = Remote()->SendRequest(IAbilityManager::GET_TOP_ABILITY_TOKEN, data, reply, option);
2442     if (error != NO_ERROR) {
2443         HILOG_ERROR("Send request error: %{public}d", error);
2444         return error;
2445     }
2446 
2447     token = sptr<IRemoteObject>(reply.ReadRemoteObject());
2448     if (!token) {
2449         HILOG_ERROR("read IRemoteObject failed.");
2450         return ERR_UNKNOWN_OBJECT;
2451     }
2452 
2453     return reply.ReadInt32();
2454 }
2455 
DelegatorDoAbilityForeground(const sptr<IRemoteObject> & token)2456 int AbilityManagerProxy::DelegatorDoAbilityForeground(const sptr<IRemoteObject> &token)
2457 {
2458     MessageParcel data;
2459     MessageParcel reply;
2460     MessageOption option;
2461 
2462     if (!WriteInterfaceToken(data)) {
2463         return INNER_ERR;
2464     }
2465 
2466     if (!data.WriteRemoteObject(token)) {
2467         HILOG_ERROR("data write failed.");
2468         return ERR_INVALID_VALUE;
2469     }
2470 
2471     auto error = Remote()->SendRequest(IAbilityManager::DELEGATOR_DO_ABILITY_FOREGROUND, data, reply, option);
2472     if (error != NO_ERROR) {
2473         HILOG_ERROR("Send request error: %{public}d", error);
2474         return error;
2475     }
2476 
2477     return reply.ReadInt32();
2478 }
2479 
DelegatorDoAbilityBackground(const sptr<IRemoteObject> & token)2480 int AbilityManagerProxy::DelegatorDoAbilityBackground(const sptr<IRemoteObject> &token)
2481 {
2482     MessageParcel data;
2483     MessageParcel reply;
2484     MessageOption option;
2485 
2486     if (!WriteInterfaceToken(data)) {
2487         return INNER_ERR;
2488     }
2489 
2490     if (!data.WriteRemoteObject(token)) {
2491         HILOG_ERROR("data write failed.");
2492         return ERR_INVALID_VALUE;
2493     }
2494 
2495     auto error = Remote()->SendRequest(IAbilityManager::DELEGATOR_DO_ABILITY_BACKGROUND, data, reply, option);
2496     if (error != NO_ERROR) {
2497         HILOG_ERROR("Send request error: %{public}d", error);
2498         return error;
2499     }
2500 
2501     return reply.ReadInt32();
2502 }
2503 
DoAbilityForeground(const sptr<IRemoteObject> & token,uint32_t flag)2504 int AbilityManagerProxy::DoAbilityForeground(const sptr<IRemoteObject> &token, uint32_t flag)
2505 {
2506     MessageParcel data;
2507     MessageParcel reply;
2508     MessageOption option;
2509 
2510     if (!WriteInterfaceToken(data)) {
2511         return INNER_ERR;
2512     }
2513 
2514     if (!data.WriteRemoteObject(token)) {
2515         HILOG_ERROR("data write failed.");
2516         return ERR_INVALID_VALUE;
2517     }
2518 
2519     if (!data.WriteUint32(flag)) {
2520         HILOG_ERROR("flag write failed.");
2521         return ERR_INVALID_VALUE;
2522     }
2523 
2524     auto error = Remote()->SendRequest(IAbilityManager::DO_ABILITY_FOREGROUND, data, reply, option);
2525     if (error != NO_ERROR) {
2526         HILOG_ERROR("Send request error: %{public}d", error);
2527         return error;
2528     }
2529 
2530     return reply.ReadInt32();
2531 }
2532 
DoAbilityBackground(const sptr<IRemoteObject> & token,uint32_t flag)2533 int AbilityManagerProxy::DoAbilityBackground(const sptr<IRemoteObject> &token, uint32_t flag)
2534 {
2535     MessageParcel data;
2536     MessageParcel reply;
2537     MessageOption option;
2538 
2539     if (!WriteInterfaceToken(data)) {
2540         return INNER_ERR;
2541     }
2542 
2543     if (!data.WriteRemoteObject(token)) {
2544         HILOG_ERROR("data write failed.");
2545         return ERR_INVALID_VALUE;
2546     }
2547 
2548     if (!data.WriteUint32(flag)) {
2549         HILOG_ERROR("flag write failed.");
2550         return ERR_INVALID_VALUE;
2551     }
2552 
2553     auto error = Remote()->SendRequest(IAbilityManager::DO_ABILITY_BACKGROUND, data, reply, option);
2554     if (error != NO_ERROR) {
2555         HILOG_ERROR("Send request error: %{public}d", error);
2556         return error;
2557     }
2558 
2559     return reply.ReadInt32();
2560 }
2561 
SendANRProcessID(int pid)2562 int AbilityManagerProxy::SendANRProcessID(int pid)
2563 {
2564     MessageParcel data;
2565     MessageParcel reply;
2566     MessageOption option;
2567     if (!WriteInterfaceToken(data)) {
2568         return INNER_ERR;
2569     }
2570     if (!data.WriteInt32(pid)) {
2571         HILOG_ERROR("pid WriteInt32 fail.");
2572         return ERR_INVALID_VALUE;
2573     }
2574     auto error = Remote()->SendRequest(IAbilityManager::SEND_APP_NOT_RESPONSE_PROCESS_ID, data, reply, option);
2575     if (error != NO_ERROR) {
2576         HILOG_ERROR("SendANRProcessID error: %d", error);
2577         return error;
2578     }
2579     return reply.ReadInt32();
2580 }
2581 
GetMissionIdByToken(const sptr<IRemoteObject> & token)2582 int32_t AbilityManagerProxy::GetMissionIdByToken(const sptr<IRemoteObject> &token)
2583 {
2584     if (!token) {
2585         HILOG_ERROR("token is nullptr.");
2586         return -1;
2587     }
2588 
2589     MessageParcel data;
2590     MessageParcel reply;
2591     MessageOption option;
2592     if (!WriteInterfaceToken(data)) {
2593         HILOG_ERROR("data interface token failed.");
2594         return -1;
2595     }
2596 
2597     if (!data.WriteRemoteObject(token)) {
2598         HILOG_ERROR("data write failed.");
2599         return -1;
2600     }
2601 
2602     auto error = Remote()->SendRequest(IAbilityManager::GET_MISSION_ID_BY_ABILITY_TOKEN, data, reply, option);
2603     if (error != NO_ERROR) {
2604         HILOG_ERROR("Send request error: %{public}d", error);
2605         return -1;
2606     }
2607 
2608     return reply.ReadInt32();
2609 }
2610 
2611 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAmsService()2612 int AbilityManagerProxy::BlockAmsService()
2613 {
2614     MessageParcel data;
2615     MessageParcel reply;
2616     MessageOption option;
2617     if (!WriteInterfaceToken(data)) {
2618         return INNER_ERR;
2619     }
2620     auto error = Remote()->SendRequest(IAbilityManager::BLOCK_AMS_SERVICE, data, reply, option);
2621     if (error != NO_ERROR) {
2622         HILOG_ERROR("BlockAmsService error: %d", error);
2623         return error;
2624     }
2625     return reply.ReadInt32();
2626 }
2627 
BlockAbility(int32_t abilityRecordId)2628 int AbilityManagerProxy::BlockAbility(int32_t abilityRecordId)
2629 {
2630     MessageParcel data;
2631     MessageParcel reply;
2632     MessageOption option;
2633     if (!WriteInterfaceToken(data)) {
2634         return INNER_ERR;
2635     }
2636     if (!data.WriteInt32(abilityRecordId)) {
2637         HILOG_ERROR("pid WriteInt32 fail.");
2638         return ERR_INVALID_VALUE;
2639     }
2640     auto error = Remote()->SendRequest(IAbilityManager::BLOCK_ABILITY, data, reply, option);
2641     if (error != NO_ERROR) {
2642         HILOG_ERROR("BlockAbility error: %d", error);
2643         return error;
2644     }
2645     return reply.ReadInt32();
2646 }
2647 
BlockAppService()2648 int AbilityManagerProxy::BlockAppService()
2649 {
2650     MessageParcel data;
2651     MessageParcel reply;
2652     MessageOption option;
2653     if (!WriteInterfaceToken(data)) {
2654         return INNER_ERR;
2655     }
2656     auto error = Remote()->SendRequest(IAbilityManager::BLOCK_APP_SERVICE, data, reply, option);
2657     if (error != NO_ERROR) {
2658         HILOG_ERROR("BlockAmsService error: %d", error);
2659         return error;
2660     }
2661     return reply.ReadInt32();
2662 }
2663 #endif
FreeInstallAbilityFromRemote(const Want & want,const sptr<IRemoteObject> & callback,int32_t userId,int requestCode)2664 int AbilityManagerProxy::FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback,
2665     int32_t userId, int requestCode)
2666 {
2667     MessageParcel data;
2668     MessageParcel reply;
2669     MessageOption option;
2670     if (!WriteInterfaceToken(data)) {
2671         HILOG_ERROR("write interface token failed.");
2672         return INNER_ERR;
2673     }
2674 
2675     if (!data.WriteParcelable(&want)) {
2676         HILOG_ERROR("want write failed.");
2677         return INNER_ERR;
2678     }
2679 
2680     if (!data.WriteRemoteObject(callback)) {
2681         HILOG_ERROR("callback write failed.");
2682         return INNER_ERR;
2683     }
2684 
2685     if (!data.WriteInt32(userId)) {
2686         HILOG_ERROR("userId write failed.");
2687         return INNER_ERR;
2688     }
2689 
2690     if (!data.WriteInt32(requestCode)) {
2691         HILOG_ERROR("requestCode write failed.");
2692         return INNER_ERR;
2693     }
2694 
2695     auto error = Remote()->SendRequest(IAbilityManager::FREE_INSTALL_ABILITY_FROM_REMOTE, data, reply, option);
2696     if (error != NO_ERROR) {
2697         HILOG_ERROR("Send request error: %{public}d", error);
2698         return error;
2699     }
2700 
2701     return reply.ReadInt32();
2702 }
2703 
DumpAbilityInfoDone(std::vector<std::string> & infos,const sptr<IRemoteObject> & callerToken)2704 int AbilityManagerProxy::DumpAbilityInfoDone(std::vector<std::string> &infos, const sptr<IRemoteObject> &callerToken)
2705 {
2706     MessageParcel data;
2707     MessageParcel reply;
2708     MessageOption option;
2709     if (!WriteInterfaceToken(data)) {
2710         HILOG_ERROR("write interface token failed.");
2711         return INNER_ERR;
2712     }
2713 
2714     if (!data.WriteStringVector(infos)) {
2715         HILOG_ERROR("infos write failed.");
2716         return INNER_ERR;
2717     }
2718 
2719     if (!data.WriteRemoteObject(callerToken)) {
2720         HILOG_ERROR("infos write failed.");
2721         return INNER_ERR;
2722     }
2723 
2724     if (!Remote()) {
2725         HILOG_ERROR("Remote nullptr.");
2726         return INNER_ERR;
2727     }
2728 
2729     auto error = Remote()->SendRequest(IAbilityManager::DUMP_ABILITY_INFO_DONE, data, reply, option);
2730     if (error != NO_ERROR) {
2731         HILOG_ERROR("Send request error: %{public}d", error);
2732         return error;
2733     }
2734 
2735     return reply.ReadInt32();
2736 }
2737 }  // namespace AAFwk
2738 }  // namespace OHOS
2739