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 #include "rs_window_animation_stub.h"
17 
18 #include "rs_iwindow_animation_finished_callback.h"
19 #include "rs_window_animation_log.h"
20 #include "rs_window_animation_target.h"
21 
22 namespace OHOS {
23 namespace Rosen {
24 namespace {
25 static constexpr int MAX_FLOATING_WINDOW_NUMBER = 100;
26 static constexpr int MAX_WINDOW_NUMBER = 100;
27 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)28 int RSWindowAnimationStub::OnRemoteRequest(uint32_t code, MessageParcel& data,
29     MessageParcel& reply, MessageOption &option)
30 {
31     WALOGD("Window animation on remote request!");
32     if (data.ReadInterfaceToken() != GetDescriptor()) {
33         WALOGE("Failed to check interface token!");
34         return ERR_INVALID_STATE;
35     }
36 
37     switch (code) {
38         case RSIWindowAnimationController::ON_START_APP:
39             return StartApp(data, reply);
40         case RSIWindowAnimationController::ON_APP_TRANSITION:
41             return AppTransition(data, reply);
42         case RSIWindowAnimationController::ON_APP_BACK_TRANSITION:
43             return AppBackTransition(data, reply);
44         case RSIWindowAnimationController::ON_MINIMIZE_WINDOW:
45             return MinimizeWindow(data, reply);
46         case RSIWindowAnimationController::ON_MINIMIZE_ALLWINDOW:
47             return MinimizeAllWindow(data, reply);
48         case RSIWindowAnimationController::ON_CLOSE_WINDOW:
49             return CloseWindow(data, reply);
50         case RSIWindowAnimationController::ON_SCREEN_UNLOCK:
51             return ScreenUnlock(data, reply);
52         case RSIWindowAnimationController::ON_WINDOW_ANIMATION_TARGETS_UPDATE:
53             return WindowAnimationTargetsUpdate(data, reply);
54         case RSIWindowAnimationController::ON_WALLPAPER_UPDATE:
55             return WallpaperUpdate(data, reply);
56         default:
57             break;
58     }
59     WALOGE("Failed to find function handler!");
60     return ERR_UNKNOWN_TRANSACTION;
61 }
62 
StartApp(MessageParcel & data,MessageParcel & reply)63 int RSWindowAnimationStub::StartApp(MessageParcel& data, MessageParcel& reply)
64 {
65     WALOGD("Window animation start app!");
66     int32_t type = 0;
67     if (!data.ReadInt32(type)) {
68         WALOGE("Failed to read starting app type!");
69         return ERR_INVALID_DATA;
70     }
71     sptr<RSWindowAnimationTarget> startingWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
72     if (startingWindowTarget == nullptr) {
73         WALOGE("Failed to read starting window target!");
74         return ERR_INVALID_DATA;
75     }
76 
77     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
78     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
79         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
80     if (finishedCallback == nullptr) {
81         WALOGE("Failed to read animation finished callback!");
82         return ERR_INVALID_DATA;
83     }
84 
85     OnStartApp(static_cast<StartingAppType>(type), startingWindowTarget, finishedCallback);
86     return ERR_NONE;
87 }
88 
AppTransition(MessageParcel & data,MessageParcel & reply)89 int RSWindowAnimationStub::AppTransition(MessageParcel& data, MessageParcel& reply)
90 {
91     WALOGD("Window animation transition!");
92     sptr<RSWindowAnimationTarget> fromWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
93     if (fromWindowTarget == nullptr) {
94         WALOGE("AppTransition failed to read animation target from!");
95         return ERR_INVALID_DATA;
96     }
97 
98     sptr<RSWindowAnimationTarget> toWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
99     if (toWindowTarget == nullptr) {
100         WALOGE("AppTransition failed to read animation target to!");
101         return ERR_INVALID_DATA;
102     }
103 
104     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
105     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
106         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
107     if (finishedCallback == nullptr) {
108         WALOGE("AppTransition failed to read animation finished callback!");
109         return ERR_INVALID_DATA;
110     }
111 
112     OnAppTransition(fromWindowTarget, toWindowTarget, finishedCallback);
113     return ERR_NONE;
114 }
115 
AppBackTransition(MessageParcel & data,MessageParcel & reply)116 int RSWindowAnimationStub::AppBackTransition(MessageParcel& data, MessageParcel& reply)
117 {
118     WALOGD("Window animation back transition!");
119     sptr<RSWindowAnimationTarget> fromWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
120     if (fromWindowTarget == nullptr) {
121         WALOGE("AppBackTransition failed to read animation target from!");
122         return ERR_INVALID_DATA;
123     }
124 
125     sptr<RSWindowAnimationTarget> toWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
126     if (toWindowTarget == nullptr) {
127         WALOGE("AppBackTransition failed to read animation target to!");
128         return ERR_INVALID_DATA;
129     }
130 
131     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
132     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
133         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
134     if (finishedCallback == nullptr) {
135         WALOGE("AppBackTransition failed to read animation finished callback!");
136         return ERR_INVALID_DATA;
137     }
138 
139     OnAppBackTransition(fromWindowTarget, toWindowTarget, finishedCallback);
140     return ERR_NONE;
141 }
142 
MinimizeWindow(MessageParcel & data,MessageParcel & reply)143 int RSWindowAnimationStub::MinimizeWindow(MessageParcel& data, MessageParcel& reply)
144 {
145     WALOGD("Window animation minimize window!");
146     sptr<RSWindowAnimationTarget> minimizingWindow(data.ReadParcelable<RSWindowAnimationTarget>());
147     if (minimizingWindow == nullptr) {
148         WALOGE("Failed to read minimizing window!");
149         return ERR_INVALID_DATA;
150     }
151 
152     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
153     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
154         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
155     if (finishedCallback == nullptr) {
156         WALOGE("MinimizeWindow failed to read animation finished callback!");
157         return ERR_INVALID_DATA;
158     }
159 
160     OnMinimizeWindow(minimizingWindow, finishedCallback);
161     return ERR_NONE;
162 }
163 
MinimizeAllWindow(MessageParcel & data,MessageParcel & reply)164 int RSWindowAnimationStub::MinimizeAllWindow(MessageParcel& data, MessageParcel& reply)
165 {
166     WALOGD("Window animation minimize all window!");
167     uint32_t dataCount = 0;
168     if (!data.ReadUint32(dataCount)) {
169         WALOGE("Failed to read window count!");
170         return ERR_INVALID_DATA;
171     }
172     if (dataCount > MAX_WINDOW_NUMBER) {
173         WALOGE("Windows are too much!");
174         return ERR_INVALID_DATA;
175     }
176     std::vector<sptr<RSWindowAnimationTarget>> minimizingWindows;
177     for (uint32_t i = 0; i < dataCount; i++) {
178         sptr<RSWindowAnimationTarget> minimizingWindow(data.ReadParcelable<RSWindowAnimationTarget>());
179         if (minimizingWindow == nullptr) {
180             WALOGE("Failed to read minimizing window!");
181             return ERR_INVALID_DATA;
182         }
183         minimizingWindows.push_back(minimizingWindow);
184     }
185 
186     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
187     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
188         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
189     if (finishedCallback == nullptr) {
190         WALOGE("MinimizeAllWindow failed to read animation finished callback!");
191         return ERR_INVALID_DATA;
192     }
193 
194     OnMinimizeAllWindow(minimizingWindows, finishedCallback);
195     return ERR_NONE;
196 }
197 
CloseWindow(MessageParcel & data,MessageParcel & reply)198 int RSWindowAnimationStub::CloseWindow(MessageParcel& data, MessageParcel& reply)
199 {
200     WALOGD("Window animation close window!");
201     sptr<RSWindowAnimationTarget> closingWindow(data.ReadParcelable<RSWindowAnimationTarget>());
202     if (closingWindow == nullptr) {
203         WALOGE("Failed to read closing window!");
204         return ERR_INVALID_DATA;
205     }
206 
207     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
208     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
209         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
210     if (finishedCallback == nullptr) {
211         WALOGE("CloseWindow failed to read animation finished callback!");
212         return ERR_INVALID_DATA;
213     }
214 
215     OnCloseWindow(closingWindow, finishedCallback);
216     return ERR_NONE;
217 }
218 
ScreenUnlock(MessageParcel & data,MessageParcel & reply)219 int RSWindowAnimationStub::ScreenUnlock(MessageParcel& data, MessageParcel& reply)
220 {
221     WALOGD("Window animation screen unlock!");
222     sptr<IRemoteObject> finishcallbackObject = data.ReadRemoteObject();
223     sptr<RSIWindowAnimationFinishedCallback> finishedCallback =
224         iface_cast<RSIWindowAnimationFinishedCallback>(finishcallbackObject);
225     if (finishedCallback == nullptr) {
226         WALOGE("ScreenUnlock failed to read animation finished callback!");
227         return ERR_INVALID_DATA;
228     }
229 
230     OnScreenUnlock(finishedCallback);
231     return ERR_NONE;
232 }
233 
WindowAnimationTargetsUpdate(MessageParcel & data,MessageParcel & reply)234 int RSWindowAnimationStub::WindowAnimationTargetsUpdate(MessageParcel& data, MessageParcel& reply)
235 {
236     WALOGD("Window animation targets update!");
237     sptr<RSWindowAnimationTarget> fullScreenWindowTarget = nullptr;
238     bool isFullScreenWindowTarget;
239     if (!data.ReadBool(isFullScreenWindowTarget)) {
240         WALOGE("Failed to read whether there is full screen window target!");
241         return ERR_INVALID_DATA;
242     }
243 
244     if (isFullScreenWindowTarget) {
245         fullScreenWindowTarget = data.ReadParcelable<RSWindowAnimationTarget>();
246         if (fullScreenWindowTarget == nullptr) {
247             WALOGE("Failed to read full screen window animation target!");
248             return ERR_INVALID_DATA;
249         }
250     }
251 
252     size_t floatWindowSize = data.ReadUint32();
253     if (floatWindowSize > MAX_FLOATING_WINDOW_NUMBER) {
254         WALOGE("Floating windows are too much!");
255         return ERR_INVALID_DATA;
256     }
257     std::vector<sptr<RSWindowAnimationTarget>> floatingWindowTargets;
258     for (size_t i = 0; i < floatWindowSize; i++) {
259         sptr<RSWindowAnimationTarget> floatingWindowTarget(data.ReadParcelable<RSWindowAnimationTarget>());
260         if (floatingWindowTarget == nullptr) {
261             WALOGE("Failed to read floating window animation window!");
262             return ERR_INVALID_DATA;
263         }
264         floatingWindowTargets.push_back(floatingWindowTarget);
265     }
266 
267     OnWindowAnimationTargetsUpdate(fullScreenWindowTarget, floatingWindowTargets);
268     return ERR_NONE;
269 }
270 
WallpaperUpdate(MessageParcel & data,MessageParcel & reply)271 int RSWindowAnimationStub::WallpaperUpdate(MessageParcel& data, MessageParcel& reply)
272 {
273     WALOGD("Window animation wallpaper update!");
274     sptr<RSWindowAnimationTarget> wallpaperTarget(data.ReadParcelable<RSWindowAnimationTarget>());
275     OnWallpaperUpdate(wallpaperTarget);
276     return ERR_NONE;
277 }
278 } // namespace Rosen
279 } // namespace OHOS
280