1 /*
2  * Copyright (c) 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 #ifndef OHOS_ABILITY_RUNTIME_WINDOW_INFO_H
17 #define OHOS_ABILITY_RUNTIME_WINDOW_INFO_H
18 
19 #ifdef SUPPORT_GRAPHICS
20 #include <typeinfo>
21 
22 #include "ability_info.h"
23 #include "iremote_object.h"
24 #include "parcel.h"
25 
26 namespace OHOS {
27 namespace AAFwk {
28 namespace {
29     constexpr int32_t WINDOW_MODE_MAX_SIZE = 4;
30 }
31 
32 enum class TransitionReason : uint32_t {
33     MINIMIZE = 0,
34     CLOSE,
35     ABILITY_TRANSITION,
36     BACK_TRANSITION,
37     CLOSE_BUTTON,
38     BACKGROUND_TRANSITION,
39 };
40 
41 struct AbilityTransitionInfo : public Parcelable {
42     std::string bundleName_;
43     std::string abilityName_;
44     uint32_t mode_ = 0;
45     std::vector<AppExecFwk::SupportWindowMode> windowModes_;
46     sptr<IRemoteObject> abilityToken_ = nullptr;
47     uint64_t displayId_ = 0;
48     bool isShowWhenLocked_ = false;
49     bool isRecent_ = false;
50     double maxWindowRatio_;
51     double minWindowRatio_;
52     uint32_t maxWindowWidth_;
53     uint32_t minWindowWidth_;
54     uint32_t maxWindowHeight_;
55     uint32_t minWindowHeight_;
56     int32_t missionId_;
57     TransitionReason reason_ = TransitionReason::ABILITY_TRANSITION;
58     AppExecFwk::DisplayOrientation orientation_ = AppExecFwk::DisplayOrientation::UNSPECIFIED;
59     uint32_t apiCompatibleVersion_ = 0;
60 
MarshallingAbilityTransitionInfo61     virtual bool Marshalling(Parcel& parcel) const override
62     {
63         if (!parcel.WriteString(bundleName_)) {
64             return false;
65         }
66 
67         if (!parcel.WriteString(abilityName_)) {
68             return false;
69         }
70 
71         if (!parcel.WriteUint32(mode_)) {
72             return false;
73         }
74 
75         if (!WriteAbilityToken(parcel)) {
76             return false;
77         }
78 
79         if (!(parcel.WriteUint64(displayId_) && parcel.WriteBool(isShowWhenLocked_) && parcel.WriteBool(isRecent_))) {
80             return false;
81         }
82 
83         auto size = windowModes_.size();
84         if (size > 0 && size <= WINDOW_MODE_MAX_SIZE) {
85             if (!parcel.WriteUint32(static_cast<uint32_t>(size))) {
86                 return false;
87             }
88             for (decltype(size) i = 0; i < size; i++) {
89                 if (!parcel.WriteUint32(static_cast<uint32_t>(windowModes_[i]))) {
90                     return false;
91                 }
92             }
93         } else {
94             if (!parcel.WriteUint32(0)) {
95                 return false;
96             }
97         }
98 
99         if (!WriteWindowInfo(parcel)) {
100             return false;
101         }
102 
103         if (!parcel.WriteInt32(missionId_)) {
104             return false;
105         }
106 
107         if (!parcel.WriteUint32(static_cast<uint32_t>(reason_))) {
108             return false;
109         }
110 
111         if (!parcel.WriteUint32(static_cast<uint32_t>(orientation_))) {
112             return false;
113         }
114 
115         if (!parcel.WriteUint32(static_cast<uint32_t>(apiCompatibleVersion_))) {
116             return false;
117         }
118         return true;
119     }
120 
WriteAbilityTokenAbilityTransitionInfo121     bool WriteAbilityToken(Parcel& parcel) const
122     {
123         if (!abilityToken_) {
124             if (!parcel.WriteBool(false)) {
125                 return false;
126             }
127         } else {
128             if (!parcel.WriteBool(true)) {
129                 return false;
130             }
131             if (!parcel.WriteRemoteObject(abilityToken_)) {
132                 return false;
133             }
134         }
135 
136         return true;
137     }
138 
WriteWindowInfoAbilityTransitionInfo139     bool WriteWindowInfo(Parcel& parcel) const
140     {
141         return (parcel.WriteDouble(maxWindowRatio_) && parcel.WriteDouble(minWindowRatio_) &&
142             parcel.WriteUint32(maxWindowWidth_) && parcel.WriteUint32(minWindowWidth_) &&
143             parcel.WriteUint32(maxWindowHeight_) && parcel.WriteUint32(minWindowHeight_));
144     }
145 
UnmarshallingAbilityTransitionInfo146     static AbilityTransitionInfo* Unmarshalling(Parcel& parcel)
147     {
148         AbilityTransitionInfo* info = new AbilityTransitionInfo();
149         info->bundleName_ = parcel.ReadString();
150         info->abilityName_ = parcel.ReadString();
151         info->mode_ = parcel.ReadUint32();
152         if (parcel.ReadBool()) {
153             info->abilityToken_ = (static_cast<MessageParcel*>(&parcel))->ReadRemoteObject();
154         }
155         info->displayId_ = parcel.ReadUint64();
156         info->isShowWhenLocked_ = parcel.ReadBool();
157         info->isRecent_ = parcel.ReadBool();
158         auto size = parcel.ReadUint32();
159         if (size > 0 && size <= WINDOW_MODE_MAX_SIZE) {
160             for (decltype(size) i = 0; i < size; i++) {
161                 info->windowModes_.push_back(static_cast<AppExecFwk::SupportWindowMode>(parcel.ReadUint32()));
162             }
163         }
164         info->maxWindowRatio_ = parcel.ReadDouble();
165         info->minWindowRatio_ = parcel.ReadDouble();
166         info->maxWindowWidth_ = parcel.ReadUint32();
167         info->minWindowWidth_ = parcel.ReadUint32();
168         info->maxWindowHeight_ = parcel.ReadUint32();
169         info->minWindowHeight_ = parcel.ReadUint32();
170         info->missionId_ = parcel.ReadInt32();
171         info->reason_ = static_cast<TransitionReason>(parcel.ReadUint32());
172         info->orientation_ = static_cast<AppExecFwk::DisplayOrientation>(parcel.ReadUint32());
173         info->apiCompatibleVersion_ = parcel.ReadUint32();
174         return info;
175     }
176 };
177 } // namespace AAFwk
178 } // namespace OHOS
179 #endif
180 #endif // OHOS_ABILITY_RUNTIME_WINDOW_INFO_H
181