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 ptr = nullptr;
129 }
130 }
131
132 HILOGI("read ReadDstbMissionInfosFromParcel end. info size is:%{public}zu", missionInfos.size());
133 return true;
134 }
135
WriteDstbMissionInfosToParcel(Parcel & parcel,const std::vector<DstbMissionInfo> & missionInfos)136 bool DstbMissionInfo::WriteDstbMissionInfosToParcel(Parcel& parcel,
137 const std::vector<DstbMissionInfo>& missionInfos)
138 {
139 size_t size = missionInfos.size();
140 if (size == 0) {
141 PARCEL_WRITE_HELPER_RET(parcel, Int32, VALUE_NULL, false);
142 return true;
143 }
144
145 PARCEL_WRITE_HELPER_RET(parcel, Int32, VALUE_OBJECT, false);
146 PARCEL_WRITE_HELPER_RET(parcel, Int32, size, false);
147 for (auto& info : missionInfos) {
148 PARCEL_WRITE_HELPER_RET(parcel, Parcelable, &info, false);
149 }
150 return true;
151 }
152
ToString() const153 std::string DstbMissionInfo::ToString() const
154 {
155 std::string str = "id: " + std::to_string(id);
156 str += " runingState: " + std::to_string(runingState);
157 str += " userId: " + std::to_string(userId);
158 str += " missionStackId: " + std::to_string(missionStackId);
159 str += " windowMode: " + std::to_string(windowMode);
160 str += " boundsLeft: " + std::to_string(boundsLeft);
161 str += " boundsTop: " + std::to_string(boundsTop);
162 str += " boundsRight: " + std::to_string(boundsRight);
163 str += " boundsBottom: " + std::to_string(boundsBottom);
164 if (baseWant == nullptr) {
165 str += " baseWant: nullptr";
166 } else {
167 str += " baseWant: " + baseWant->GetBundle();
168 }
169 if (topAbility == nullptr) {
170 str += " topAbility: nullptr";
171 } else {
172 str += " topAbility: " + topAbility->GetBundleName();
173 }
174 if (baseAbility == nullptr) {
175 str += " baseAbility: nullptr";
176 } else {
177 str += " baseAbility: " + baseAbility->GetBundleName();
178 }
179 if (reservedAbility == nullptr) {
180 str += " reservedAbility: nullptr";
181 } else {
182 str += " reservedAbility: " + reservedAbility->GetBundleName();
183 }
184 str += " lastMissionActiveTime: " + std::to_string(lastMissionActiveTime);
185 str += " displayId: " + std::to_string(displayId);
186 str += " label: " + label;
187 str += " size: " + std::to_string(size);
188 str += " iconPath: " + iconPath;
189 str += " continuable: " + std::to_string(continuable);
190 str += " windowType: " + std::to_string(windowType);
191 str += " lockedState: ";
192 str += lockedState ? "true" : "false";
193 str += " missionType: " + std::to_string(missionType);
194 str += " windowTypeMode: " + std::to_string(windowTypeMode);
195 return str;
196 }
197 } // namespace DistributedSchedule
198 } // namespace OHOS