1 /*
2  * Copyright (C) 2020 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.launcher3;
18 
19 import com.android.systemui.shared.system.RemoteAnimationTargetCompat;
20 
21 import java.lang.ref.WeakReference;
22 
23 /**
24  * This class is needed to wrap any animation runner that is a part of the
25  * RemoteAnimationDefinition:
26  * - Launcher creates a new instance of the LauncherAppTransitionManagerImpl whenever it is
27  *   created, which in turn registers a new definition
28  * - When the definition is registered, window manager retains a strong binder reference to the
29  *   runner passed in
30  * - If the Launcher activity is recreated, the new definition registered will replace the old
31  *   reference in the system's activity record, but until the system server is GC'd, the binder
32  *   reference will still exist, which references the runner in the Launcher process, which
33  *   references the (old) Launcher activity through this class
34  *
35  * Instead we make the runner provided to the definition static only holding a weak reference to
36  * the runner implementation.  When this animation manager is destroyed, we remove the Launcher
37  * reference to the runner, leaving only the weak ref from the runner.
38  */
39 public class WrappedLauncherAnimationRunner<R extends WrappedAnimationRunnerImpl>
40         extends LauncherAnimationRunner {
41     private WeakReference<R> mImpl;
42 
WrappedLauncherAnimationRunner(R animationRunnerImpl, boolean startAtFrontOfQueue)43     public WrappedLauncherAnimationRunner(R animationRunnerImpl, boolean startAtFrontOfQueue) {
44         super(animationRunnerImpl.getHandler(), startAtFrontOfQueue);
45         mImpl = new WeakReference<>(animationRunnerImpl);
46     }
47 
48     @Override
onCreateAnimation(RemoteAnimationTargetCompat[] appTargets, RemoteAnimationTargetCompat[] wallpaperTargets, AnimationResult result)49     public void onCreateAnimation(RemoteAnimationTargetCompat[] appTargets,
50             RemoteAnimationTargetCompat[] wallpaperTargets, AnimationResult result) {
51         R animationRunnerImpl = mImpl.get();
52         if (animationRunnerImpl != null) {
53             animationRunnerImpl.onCreateAnimation(appTargets, wallpaperTargets, result);
54         }
55     }
56 }
57