1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.wm.shell.transition;
18 
19 import android.annotation.NonNull;
20 import android.os.RemoteException;
21 import android.view.IRemoteAnimationFinishedCallback;
22 import android.view.IRemoteAnimationRunner;
23 import android.view.RemoteAnimationAdapter;
24 import android.view.RemoteAnimationTarget;
25 import android.view.SurfaceControl;
26 import android.view.WindowManager;
27 import android.window.IWindowContainerTransactionCallback;
28 
29 /**
30  * Utilities and interfaces for transition-like usage on top of the legacy app-transition and
31  * synctransaction tools.
32  */
33 public class LegacyTransitions {
34 
35     /**
36      * Interface for a "legacy" transition. Effectively wraps a sync callback + remoteAnimation
37      * into one callback.
38      */
39     public interface ILegacyTransition {
40         /**
41          * Called when both the associated sync transaction finishes and the remote animation is
42          * ready.
43          */
onAnimationStart(int transit, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback, SurfaceControl.Transaction t)44         void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
45                 RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
46                 IRemoteAnimationFinishedCallback finishedCallback, SurfaceControl.Transaction t);
47     }
48 
49     /**
50      * Makes sure that a remote animation and corresponding sync callback are called together
51      * such that the sync callback is called first. This assumes that both the callback receiver
52      * and the remoteanimation are in the same process so that order is preserved on both ends.
53      */
54     public static class LegacyTransition {
55         private final ILegacyTransition mLegacyTransition;
56         private int mSyncId = -1;
57         private SurfaceControl.Transaction mTransaction;
58         private int mTransit;
59         private RemoteAnimationTarget[] mApps;
60         private RemoteAnimationTarget[] mWallpapers;
61         private RemoteAnimationTarget[] mNonApps;
62         private IRemoteAnimationFinishedCallback mFinishCallback = null;
63         private boolean mCancelled = false;
64         private final SyncCallback mSyncCallback = new SyncCallback();
65         private final RemoteAnimationAdapter mAdapter =
66                 new RemoteAnimationAdapter(new RemoteAnimationWrapper(), 0, 0);
67 
LegacyTransition(@indowManager.TransitionType int type, @NonNull ILegacyTransition legacyTransition)68         public LegacyTransition(@WindowManager.TransitionType int type,
69                 @NonNull ILegacyTransition legacyTransition) {
70             mLegacyTransition = legacyTransition;
71             mTransit = type;
72         }
73 
getType()74         public @WindowManager.TransitionType int getType() {
75             return mTransit;
76         }
77 
getSyncCallback()78         public IWindowContainerTransactionCallback getSyncCallback() {
79             return mSyncCallback;
80         }
81 
getAdapter()82         public RemoteAnimationAdapter getAdapter() {
83             return mAdapter;
84         }
85 
86         private class SyncCallback extends IWindowContainerTransactionCallback.Stub {
87             @Override
onTransactionReady(int id, SurfaceControl.Transaction t)88             public void onTransactionReady(int id, SurfaceControl.Transaction t)
89                     throws RemoteException {
90                 mSyncId = id;
91                 mTransaction = t;
92                 checkApply();
93             }
94         }
95 
96         private class RemoteAnimationWrapper extends IRemoteAnimationRunner.Stub {
97             @Override
onAnimationStart(int transit, RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, IRemoteAnimationFinishedCallback finishedCallback)98             public void onAnimationStart(int transit, RemoteAnimationTarget[] apps,
99                     RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps,
100                     IRemoteAnimationFinishedCallback finishedCallback) throws RemoteException {
101                 mTransit = transit;
102                 mApps = apps;
103                 mWallpapers = wallpapers;
104                 mNonApps = nonApps;
105                 mFinishCallback = finishedCallback;
106                 checkApply();
107             }
108 
109             @Override
onAnimationCancelled()110             public void onAnimationCancelled() throws RemoteException {
111                 mCancelled = true;
112                 mApps = mWallpapers = mNonApps = null;
113                 checkApply();
114             }
115         }
116 
117 
checkApply()118         private void checkApply() throws RemoteException {
119             if (mSyncId < 0 || (mFinishCallback == null && !mCancelled)) return;
120             mLegacyTransition.onAnimationStart(mTransit, mApps, mWallpapers,
121                     mNonApps, mFinishCallback, mTransaction);
122         }
123     }
124 }
125