• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.back;
18 
19 import static android.view.WindowManager.TRANSIT_OLD_UNSET;
20 
21 import android.annotation.NonNull;
22 import android.os.RemoteException;
23 import android.util.Log;
24 import android.view.IRemoteAnimationFinishedCallback;
25 import android.view.IRemoteAnimationRunner;
26 import android.view.RemoteAnimationTarget;
27 import android.window.IBackAnimationRunner;
28 import android.window.IOnBackInvokedCallback;
29 
30 /**
31  * Used to register the animation callback and runner, it will trigger result if gesture was finish
32  * before it received IBackAnimationRunner#onAnimationStart, so the controller could continue
33  * trigger the real back behavior.
34  */
35 class BackAnimationRunner {
36     private static final String TAG = "ShellBackPreview";
37 
38     private final IOnBackInvokedCallback mCallback;
39     private final IRemoteAnimationRunner mRunner;
40 
41     // Whether we are waiting to receive onAnimationStart
42     private boolean mWaitingAnimation;
43 
44     /** True when the back animation is cancelled */
45     private boolean mAnimationCancelled;
46 
BackAnimationRunner(@onNull IOnBackInvokedCallback callback, @NonNull IRemoteAnimationRunner runner)47     BackAnimationRunner(@NonNull IOnBackInvokedCallback callback,
48             @NonNull IRemoteAnimationRunner runner) {
49         mCallback = callback;
50         mRunner = runner;
51     }
52 
53     /** Returns the registered animation runner */
getRunner()54     IRemoteAnimationRunner getRunner() {
55         return mRunner;
56     }
57 
58     /** Returns the registered animation callback */
getCallback()59     IOnBackInvokedCallback getCallback() {
60         return mCallback;
61     }
62 
63     /**
64      * Called from {@link IBackAnimationRunner}, it will deliver these
65      * {@link RemoteAnimationTarget}s to the corresponding runner.
66      */
startAnimation(RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers, RemoteAnimationTarget[] nonApps, Runnable finishedCallback)67     void startAnimation(RemoteAnimationTarget[] apps, RemoteAnimationTarget[] wallpapers,
68             RemoteAnimationTarget[] nonApps, Runnable finishedCallback) {
69         final IRemoteAnimationFinishedCallback callback =
70                 new IRemoteAnimationFinishedCallback.Stub() {
71                     @Override
72                     public void onAnimationFinished() {
73                         finishedCallback.run();
74                     }
75                 };
76         mWaitingAnimation = false;
77         try {
78             getRunner().onAnimationStart(TRANSIT_OLD_UNSET, apps, wallpapers,
79                     nonApps, callback);
80         } catch (RemoteException e) {
81             Log.w(TAG, "Failed call onAnimationStart", e);
82         }
83     }
84 
startGesture()85     void startGesture() {
86         mWaitingAnimation = true;
87         mAnimationCancelled = false;
88     }
89 
isWaitingAnimation()90     boolean isWaitingAnimation() {
91         return mWaitingAnimation;
92     }
93 
cancelAnimation()94     void cancelAnimation() {
95         mWaitingAnimation = false;
96         mAnimationCancelled = true;
97     }
98 
isAnimationCancelled()99     boolean isAnimationCancelled() {
100         return mAnimationCancelled;
101     }
102 
resetWaitingAnimation()103     void resetWaitingAnimation() {
104         mWaitingAnimation = false;
105     }
106 }
107