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 "mission/distributed_mission_info.h"
17 
18 #include "adapter/adapter_constant.h"
19 #include "mission/mission_constant.h"
20 #include "dtbschedmgr_log.h"
21 #include "parcel_helper.h"
22 #include "string_ex.h"
23 
24 namespace OHOS {
25 namespace DistributedSchedule {
26 using namespace Constants::Adapter;
27 namespace {
28 const std::string TAG = "DstbMissionInfo";
29 }
30 
ReadFromParcel(Parcel & parcel)31 bool DstbMissionInfo::ReadFromParcel(Parcel& parcel)
32 {
33     id = parcel.ReadInt32();
34     runingState = parcel.ReadInt32();
35     userId = parcel.ReadInt32();
36     missionStackId = parcel.ReadInt32();
37     if (!parcel.ReadInt32Vector(&combinedMissionIds)) {
38         return false;
39     }
40     windowMode = parcel.ReadInt32();
41     boundsLeft = parcel.ReadInt32();
42     boundsTop = parcel.ReadInt32();
43     boundsRight = parcel.ReadInt32();
44     boundsBottom = parcel.ReadInt32();
45     baseWant.reset(parcel.ReadParcelable<AAFwk::Want>());
46     topAbility.reset(parcel.ReadParcelable<AppExecFwk::ElementName>());
47     baseAbility.reset(parcel.ReadParcelable<AppExecFwk::ElementName>());
48     reservedAbility.reset(parcel.ReadParcelable<AppExecFwk::ElementName>());
49     lastMissionActiveTime = parcel.ReadInt64();
50     displayId = parcel.ReadInt32();
51     label = Str16ToStr8(parcel.ReadString16());
52     size = parcel.ReadInt32();
53     iconPath = Str16ToStr8(parcel.ReadString16());
54     continuable = parcel.ReadInt32();
55     windowType = parcel.ReadInt32();
56     lockedState = parcel.ReadBool();
57     missionType = parcel.ReadInt32();
58     windowTypeMode = parcel.ReadInt32();
59 
60     return true;
61 }
62 
Unmarshalling(Parcel & parcel)63 DstbMissionInfo* DstbMissionInfo::Unmarshalling(Parcel& parcel)
64 {
65     DstbMissionInfo* info = new DstbMissionInfo();
66     if (info != nullptr && !info->ReadFromParcel(parcel)) {
67         HILOGE("read from parcel failed!");
68         delete info;
69         info = nullptr;
70     }
71     return info;
72 }
73 
Marshalling(Parcel & parcel) const74 bool DstbMissionInfo::Marshalling(Parcel& parcel) const
75 {
76     PARCEL_WRITE_HELPER_RET(parcel, Int32, id, false);
77     PARCEL_WRITE_HELPER_RET(parcel, Int32, runingState, false);
78     PARCEL_WRITE_HELPER_RET(parcel, Int32, userId, false);
79     PARCEL_WRITE_HELPER_RET(parcel, Int32, missionStackId, false);
80     PARCEL_WRITE_HELPER_RET(parcel, Int32Vector, combinedMissionIds, false);
81     PARCEL_WRITE_HELPER_RET(parcel, Int32, windowMode, false);
82     PARCEL_WRITE_HELPER_RET(parcel, Int32, boundsLeft, false);
83     PARCEL_WRITE_HELPER_RET(parcel, Int32, boundsTop, false);
84     PARCEL_WRITE_HELPER_RET(parcel, Int32, boundsRight, false);
85     PARCEL_WRITE_HELPER_RET(parcel, Int32, boundsBottom, false);
86     PARCEL_WRITE_HELPER_RET(parcel, Parcelable, baseWant.get(), false);
87     PARCEL_WRITE_HELPER_RET(parcel, Parcelable, topAbility.get(), false);
88     PARCEL_WRITE_HELPER_RET(parcel, Parcelable, baseAbility.get(), false);
89     PARCEL_WRITE_HELPER_RET(parcel, Parcelable, reservedAbility.get(), false);
90     PARCEL_WRITE_HELPER_RET(parcel, Int64, lastMissionActiveTime, false);
91     PARCEL_WRITE_HELPER_RET(parcel, Int32, displayId, false);
92     PARCEL_WRITE_HELPER_RET(parcel, String16, Str8ToStr16(label), false);
93     PARCEL_WRITE_HELPER_RET(parcel, Int32, size, false);
94     PARCEL_WRITE_HELPER_RET(parcel, String16, Str8ToStr16(iconPath), false);
95     PARCEL_WRITE_HELPER_RET(parcel, Int32, continuable, false);
96     PARCEL_WRITE_HELPER_RET(parcel, Int32, windowType, false);
97     PARCEL_WRITE_HELPER_RET(parcel, Bool, lockedState, false);
98     PARCEL_WRITE_HELPER_RET(parcel, Int32, missionType, false);
99     PARCEL_WRITE_HELPER_RET(parcel, Int32, windowTypeMode, false);
100 
101     return true;
102 }
103 
ReadDstbMissionInfosFromParcel(Parcel & parcel,std::vector<DstbMissionInfo> & missionInfos)104 bool DstbMissionInfo::ReadDstbMissionInfosFromParcel(Parcel& parcel,
105     std::vector<DstbMissionInfo>& missionInfos)
106 {
107     int32_t empty = parcel.ReadInt32();
108     if (empty == VALUE_OBJECT) {
109         int32_t len = parcel.ReadInt32();
110         HILOGD("ReadDstbMissionInfosFromParcel::readLength is:%{public}d", len);
111         if (len < 0) {
112             return false;
113         }
114         size_t size = static_cast<size_t>(len);
115         if (size > Constants::Mission::GET_MAX_MISSIONS) {
116             HILOGE("Failed to read DstbMissionInfo vector, size = %{public}zu", size);
117             return false;
118         }
119         missionInfos.clear();
120         for (size_t i = 0; i < size; i++) {
121             DstbMissionInfo *ptr = parcel.ReadParcelable<DstbMissionInfo>();
122             if (ptr == nullptr) {
123                 return false;
124             }
125             HILOGD("read DstbMissionInfo is:%{public}s", ptr->ToString().c_str());
126             missionInfos.emplace_back(*ptr);
127             delete ptr;
128         }
129     }
130 
131     HILOGI("read ReadDstbMissionInfosFromParcel end. info size is:%{public}zu", missionInfos.size());
132     return true;
133 }
134 
WriteDstbMissionInfosToParcel(Parcel & parcel,const std::vector<DstbMissionInfo> & missionInfos)135 bool DstbMissionInfo::WriteDstbMissionInfosToParcel(Parcel& parcel,
136     const std::vector<DstbMissionInfo>& missionInfos)
137 {
138     size_t size = missionInfos.size();
139     if (size == 0) {
140         PARCEL_WRITE_HELPER_RET(parcel, Int32, VALUE_NULL, false);
141         return true;
142     }
143 
144     PARCEL_WRITE_HELPER_RET(parcel, Int32, VALUE_OBJECT, false);
145     PARCEL_WRITE_HELPER_RET(parcel, Int32, size, false);
146     for (auto& info : missionInfos) {
147         PARCEL_WRITE_HELPER_RET(parcel, Parcelable, &info, false);
148     }
149     return true;
150 }
151 
ToString() const152 std::string DstbMissionInfo::ToString() const
153 {
154     std::string str = "id: " + std::to_string(id);
155     str += " runingState: " + std::to_string(runingState);
156     str += " userId: " + std::to_string(userId);
157     str += " missionStackId: " + std::to_string(missionStackId);
158     str += " windowMode: " + std::to_string(windowMode);
159     str += " boundsLeft: " + std::to_string(boundsLeft);
160     str += " boundsTop: " + std::to_string(boundsTop);
161     str += " boundsRight: " + std::to_string(boundsRight);
162     str += " boundsBottom: " + std::to_string(boundsBottom);
163     if (baseWant == nullptr) {
164         str += " baseWant: nullptr";
165     } else {
166         str += " baseWant: " + baseWant->GetBundle();
167     }
168     if (topAbility == nullptr) {
169         str += " topAbility: nullptr";
170     } else {
171         str += " topAbility: " + topAbility->GetBundleName();
172     }
173     if (baseAbility == nullptr) {
174         str += " baseAbility: nullptr";
175     } else {
176         str += " baseAbility: " + baseAbility->GetBundleName();
177     }
178     if (reservedAbility == nullptr) {
179         str += " reservedAbility: nullptr";
180     } else {
181         str += " reservedAbility: " + reservedAbility->GetBundleName();
182     }
183     str += " lastMissionActiveTime: " + std::to_string(lastMissionActiveTime);
184     str += " displayId: " + std::to_string(displayId);
185     str += " label: " + label;
186     str += " size: " + std::to_string(size);
187     str += " iconPath: " + iconPath;
188     str += " continuable: " + std::to_string(continuable);
189     str += " windowType: " + std::to_string(windowType);
190     str += " lockedState: ";
191     str += lockedState ? "true" : "false";
192     str += " missionType: " + std::to_string(missionType);
193     str += " windowTypeMode: " + std::to_string(windowTypeMode);
194     return str;
195 }
196 } // namespace DistributedSchedule
197 } // namespace OHOS