1 /*
2  * Copyright (C) 2018 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.server.wm;
18 
19 import android.annotation.Nullable;
20 import android.app.ActivityOptions;
21 import android.os.Handler;
22 import android.util.ArrayMap;
23 import android.view.RemoteAnimationAdapter;
24 
25 import com.android.server.am.ActivityManagerService;
26 
27 /**
28  * Registry to keep track of remote animations to be run for activity starts from a certain package.
29  *
30  * @see ActivityManagerService#registerRemoteAnimationForNextActivityStart
31  */
32 class PendingRemoteAnimationRegistry {
33 
34     private static final long TIMEOUT_MS = 3000;
35 
36     private final ArrayMap<String, Entry> mEntries = new ArrayMap<>();
37     private final Handler mHandler;
38     private final ActivityTaskManagerService mService;
39 
PendingRemoteAnimationRegistry(ActivityTaskManagerService service, Handler handler)40     PendingRemoteAnimationRegistry(ActivityTaskManagerService service, Handler handler) {
41         mService = service;
42         mHandler = handler;
43     }
44 
45     /**
46      * Adds a remote animation to be run for all activity starts originating from a certain package.
47      */
addPendingAnimation(String packageName, RemoteAnimationAdapter adapter)48     void addPendingAnimation(String packageName, RemoteAnimationAdapter adapter) {
49         mEntries.put(packageName, new Entry(packageName, adapter));
50     }
51 
52     /**
53      * Overrides the activity options with a registered remote animation for a certain calling
54      * package if such a remote animation is registered.
55      */
overrideOptionsIfNeeded(String callingPackage, @Nullable ActivityOptions options)56     ActivityOptions overrideOptionsIfNeeded(String callingPackage,
57             @Nullable ActivityOptions options) {
58         final Entry entry = mEntries.get(callingPackage);
59         if (entry == null) {
60             return options;
61         }
62         if (options == null) {
63             options = ActivityOptions.makeRemoteAnimation(entry.adapter);
64         } else {
65             options.setRemoteAnimationAdapter(entry.adapter);
66         }
67         mEntries.remove(callingPackage);
68         return options;
69     }
70 
71     private class Entry {
72         final String packageName;
73         final RemoteAnimationAdapter adapter;
74 
Entry(String packageName, RemoteAnimationAdapter adapter)75         Entry(String packageName, RemoteAnimationAdapter adapter) {
76             this.packageName = packageName;
77             this.adapter = adapter;
78             mHandler.postDelayed(() -> {
79                 synchronized (mService.mGlobalLock) {
80                     final Entry entry = mEntries.get(packageName);
81                     if (entry == this) {
82                         mEntries.remove(packageName);
83                     }
84                 }
85             }, TIMEOUT_MS);
86         }
87     }
88 }
89