• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "local_ability_manager_proxy.h"
17 
18 #include "ipc_types.h"
19 #include "iremote_object.h"
20 #include "message_option.h"
21 #include "message_parcel.h"
22 #include "nlohmann/json.hpp"
23 #include "refbase.h"
24 
25 using namespace std;
26 using namespace OHOS::HiviewDFX;
27 
28 namespace OHOS {
StartAbility(int32_t systemAbilityId,const std::string & eventStr)29 bool LocalAbilityManagerProxy::StartAbility(int32_t systemAbilityId, const std::string& eventStr)
30 {
31     if (systemAbilityId <= 0) {
32         HiLog::Warn(label_, "StartAbility systemAbilityId invalid.");
33         return false;
34     }
35 
36     if (eventStr.empty()) {
37         HiLog::Warn(label_, "StartAbility eventStr invalid.");
38         return false;
39     }
40 
41     sptr<IRemoteObject> iro = Remote();
42     if (iro == nullptr) {
43         HiLog::Error(label_, "StartAbility Remote return null");
44         return false;
45     }
46 
47     MessageParcel data;
48     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
49         HiLog::Warn(label_, "StartAbility interface token check failed");
50         return false;
51     }
52     bool ret = data.WriteInt32(systemAbilityId);
53     if (!ret) {
54         HiLog::Warn(label_, "StartAbility write systemAbilityId failed!");
55         return false;
56     }
57     ret = data.WriteString(eventStr);
58     if (!ret) {
59         HiLog::Warn(label_, "StartAbility write eventStr failed!");
60         return false;
61     }
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_ASYNC);
64     int32_t status = iro->SendRequest(
65         static_cast<uint32_t>(SafwkInterfaceCode::START_ABILITY_TRANSACTION), data, reply, option);
66     if (status != NO_ERROR) {
67         HiLog::Error(label_, "StartAbility SendRequest failed, return value : %{public}d", status);
68         return false;
69     }
70     return true;
71 }
72 
StopAbility(int32_t systemAbilityId,const std::string & eventStr)73 bool LocalAbilityManagerProxy::StopAbility(int32_t systemAbilityId, const std::string& eventStr)
74 {
75     if (systemAbilityId <= 0) {
76         HiLog::Warn(label_, "StopAbility systemAbilityId invalid.");
77         return false;
78     }
79 
80     if (eventStr.empty()) {
81         HiLog::Warn(label_, "StartAbility eventStr invalid.");
82         return false;
83     }
84 
85     sptr<IRemoteObject> iro = Remote();
86     if (iro == nullptr) {
87         HiLog::Error(label_, "StopAbility Remote return null");
88         return false;
89     }
90 
91     MessageParcel data;
92     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
93         HiLog::Warn(label_, "StopAbility interface token check failed");
94         return false;
95     }
96     bool ret = data.WriteInt32(systemAbilityId);
97     if (!ret) {
98         HiLog::Warn(label_, "StopAbility write systemAbilityId failed!");
99         return false;
100     }
101     ret = data.WriteString(eventStr);
102     if (!ret) {
103         HiLog::Warn(label_, "StopAbility write eventStr failed!");
104         return false;
105     }
106     MessageParcel reply;
107     MessageOption option(MessageOption::TF_ASYNC);
108     int32_t status = iro->SendRequest(
109         static_cast<uint32_t>(SafwkInterfaceCode::STOP_ABILITY_TRANSACTION), data, reply, option);
110     if (status != NO_ERROR) {
111         HiLog::Error(label_, "StopAbility SendRequest failed, return value : %{public}d", status);
112         return false;
113     }
114     return true;
115 }
116 
ActiveAbility(int32_t systemAbilityId,const nlohmann::json & activeReason)117 bool LocalAbilityManagerProxy::ActiveAbility(int32_t systemAbilityId,
118     const nlohmann::json& activeReason)
119 {
120     if (systemAbilityId <= 0) {
121         HiLog::Warn(label_, "ActiveAbility systemAbilityId invalid.");
122         return false;
123     }
124 
125     sptr<IRemoteObject> iro = Remote();
126     if (iro == nullptr) {
127         HiLog::Error(label_, "ActiveAbility Remote return null");
128         return false;
129     }
130 
131     MessageParcel data;
132     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
133         HiLog::Warn(label_, "ActiveAbility interface token check failed");
134         return false;
135     }
136     if (!data.WriteInt32(systemAbilityId)) {
137         HiLog::Warn(label_, "ActiveAbility write systemAbilityId failed!");
138         return false;
139     }
140     if (!data.WriteString(activeReason.dump())) {
141         HiLog::Warn(label_, "ActiveAbility write activeReason failed!");
142         return false;
143     }
144 
145     MessageParcel reply;
146     MessageOption option;
147     int32_t status = iro->SendRequest(
148         static_cast<uint32_t>(SafwkInterfaceCode::ACTIVE_ABILITY_TRANSACTION), data, reply, option);
149     if (status != NO_ERROR) {
150         HiLog::Error(label_, "ActiveAbility SendRequest failed, return value : %{public}d", status);
151         return false;
152     }
153     bool result = false;
154     if (!reply.ReadBool(result)) {
155         HiLog::Warn(label_, "ActiveAbility read result failed!");
156         return false;
157     }
158     return result;
159 }
160 
IdleAbility(int32_t systemAbilityId,const nlohmann::json & idleReason,int32_t & delayTime)161 bool LocalAbilityManagerProxy::IdleAbility(int32_t systemAbilityId,
162     const nlohmann::json& idleReason, int32_t& delayTime)
163 {
164     if (systemAbilityId <= 0) {
165         HiLog::Warn(label_, "IdleAbility systemAbilityId invalid.");
166         return false;
167     }
168 
169     sptr<IRemoteObject> iro = Remote();
170     if (iro == nullptr) {
171         HiLog::Error(label_, "IdleAbility Remote return null");
172         return false;
173     }
174 
175     MessageParcel data;
176     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
177         HiLog::Warn(label_, "IdleAbility interface token check failed");
178         return false;
179     }
180     if (!data.WriteInt32(systemAbilityId)) {
181         HiLog::Warn(label_, "IdleAbility write systemAbilityId failed!");
182         return false;
183     }
184     if (!data.WriteString(idleReason.dump())) {
185         HiLog::Warn(label_, "IdleAbility write ildeReason failed!");
186         return false;
187     }
188 
189     MessageParcel reply;
190     MessageOption option;
191     int32_t status = iro->SendRequest(
192         static_cast<uint32_t>(SafwkInterfaceCode::IDLE_ABILITY_TRANSACTION), data, reply, option);
193     if (status != NO_ERROR) {
194         HiLog::Error(label_, "IdleAbility SendRequest failed, return value : %{public}d", status);
195         return false;
196     }
197     bool result = false;
198     if (!reply.ReadBool(result)) {
199         HiLog::Warn(label_, "IdleAbility read result failed!");
200         return false;
201     }
202     if (!reply.ReadInt32(delayTime)) {
203         HiLog::Warn(label_, "IdleAbility read delayTime failed!");
204         return false;
205     }
206     return result;
207 }
208 
SendStrategyToSA(int32_t type,int32_t systemAbilityId,int32_t level,std::string & action)209 bool LocalAbilityManagerProxy::SendStrategyToSA(int32_t type, int32_t systemAbilityId,
210     int32_t level, std::string& action)
211 {
212     if (systemAbilityId <= 0) {
213         HiLog::Warn(label_, "SendStrategy systemAbilityId invalid.");
214         return false;
215     }
216 
217     sptr<IRemoteObject> iro = Remote();
218     if (iro == nullptr) {
219         HiLog::Error(label_, "SendStrategy Remote return null");
220         return false;
221     }
222 
223     MessageParcel data;
224     if (!data.WriteInterfaceToken(LOCAL_ABILITY_MANAGER_INTERFACE_TOKEN)) {
225         HiLog::Warn(label_, "SendStrategy interface token check failed");
226         return false;
227     }
228     bool ret = data.WriteInt32(type);
229     if (!ret) {
230         HiLog::Warn(label_, "SendStrategy write type failed!");
231         return false;
232     }
233     ret = data.WriteInt32(systemAbilityId);
234     if (!ret) {
235         HiLog::Warn(label_, "SendStrategy write systemAbilityId failed!");
236         return false;
237     }
238     ret = data.WriteInt32(level);
239     if (!ret) {
240         HiLog::Warn(label_, "SendStrategy write level failed!");
241         return false;
242     }
243     ret = data.WriteString(action);
244     if (!ret) {
245         HiLog::Warn(label_, "SendStrategy write action failed!");
246         return false;
247     }
248 
249     MessageParcel reply;
250     MessageOption option(MessageOption::TF_ASYNC);
251     int32_t status = iro->SendRequest(
252         static_cast<uint32_t>(SafwkInterfaceCode::SEND_STRATEGY_TO_SA_TRANSACTION), data, reply, option);
253     if (status != NO_ERROR) {
254         HiLog::Error(label_, "SendStrategy SendRequest failed, return value : %{public}d", status);
255         return false;
256     }
257     bool result = false;
258     if (!reply.ReadBool(result)) {
259         HiLog::Warn(label_, "SendStrategy read result failed!");
260         return false;
261     }
262     return result;
263 }
264 }
265