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 androidx.window.extensions.embedding;
18 
19 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_CLOSE;
20 import static android.view.WindowManager.TRANSIT_OLD_ACTIVITY_OPEN;
21 import static android.view.WindowManager.TRANSIT_OLD_TASK_FRAGMENT_CHANGE;
22 import static android.view.WindowManager.TRANSIT_OLD_TASK_FRAGMENT_CLOSE;
23 import static android.view.WindowManager.TRANSIT_OLD_TASK_FRAGMENT_OPEN;
24 
25 import android.util.Log;
26 import android.view.RemoteAnimationAdapter;
27 import android.view.RemoteAnimationDefinition;
28 import android.window.TaskFragmentOrganizer;
29 
30 import androidx.annotation.NonNull;
31 
32 import com.android.internal.annotations.VisibleForTesting;
33 
34 /** Controls the TaskFragment remote animations. */
35 class TaskFragmentAnimationController {
36 
37     private static final String TAG = "TaskFragAnimationCtrl";
38     static final boolean DEBUG = false;
39 
40     private final TaskFragmentOrganizer mOrganizer;
41     private final TaskFragmentAnimationRunner mRemoteRunner = new TaskFragmentAnimationRunner();
42     @VisibleForTesting
43     final RemoteAnimationDefinition mDefinition;
44     private boolean mIsRegistered;
45 
TaskFragmentAnimationController(@onNull TaskFragmentOrganizer organizer)46     TaskFragmentAnimationController(@NonNull TaskFragmentOrganizer organizer) {
47         mOrganizer = organizer;
48         mDefinition = new RemoteAnimationDefinition();
49         final RemoteAnimationAdapter animationAdapter =
50                 new RemoteAnimationAdapter(mRemoteRunner, 0, 0, true /* changeNeedsSnapshot */);
51         mDefinition.addRemoteAnimation(TRANSIT_OLD_ACTIVITY_OPEN, animationAdapter);
52         mDefinition.addRemoteAnimation(TRANSIT_OLD_TASK_FRAGMENT_OPEN, animationAdapter);
53         mDefinition.addRemoteAnimation(TRANSIT_OLD_ACTIVITY_CLOSE, animationAdapter);
54         mDefinition.addRemoteAnimation(TRANSIT_OLD_TASK_FRAGMENT_CLOSE, animationAdapter);
55         mDefinition.addRemoteAnimation(TRANSIT_OLD_TASK_FRAGMENT_CHANGE, animationAdapter);
56     }
57 
registerRemoteAnimations()58     void registerRemoteAnimations() {
59         if (DEBUG) {
60             Log.v(TAG, "registerRemoteAnimations");
61         }
62         if (mIsRegistered) {
63             return;
64         }
65         mOrganizer.registerRemoteAnimations(mDefinition);
66         mIsRegistered = true;
67     }
68 
unregisterRemoteAnimations()69     void unregisterRemoteAnimations() {
70         if (DEBUG) {
71             Log.v(TAG, "unregisterRemoteAnimations");
72         }
73         if (!mIsRegistered) {
74             return;
75         }
76         mOrganizer.unregisterRemoteAnimations();
77         mIsRegistered = false;
78     }
79 }
80