Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 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 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 17 | package com.android.server.wm; |
Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 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 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 25 | import com.android.server.am.ActivityManagerService; |
| 26 | |
Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 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; |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 38 | private final ActivityTaskManagerService mService; |
Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 39 | |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 40 | PendingRemoteAnimationRegistry(ActivityTaskManagerService service, Handler handler) { |
Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | |
| 75 | Entry(String packageName, RemoteAnimationAdapter adapter) { |
| 76 | this.packageName = packageName; |
| 77 | this.adapter = adapter; |
| 78 | mHandler.postDelayed(() -> { |
Wale Ogunwale | c9e57de | 2018-05-08 14:28:07 -0700 | [diff] [blame] | 79 | synchronized (mService.mGlobalLock) { |
Jorim Jaggi | 04dc596 | 2018-01-29 18:54:13 +0100 | [diff] [blame] | 80 | final Entry entry = mEntries.get(packageName); |
| 81 | if (entry == this) { |
| 82 | mEntries.remove(packageName); |
| 83 | } |
| 84 | } |
| 85 | }, TIMEOUT_MS); |
| 86 | } |
| 87 | } |
| 88 | } |