blob: 884cae440bed092c1ca64fd64aab36fbf965d5ff [file] [log] [blame]
Jorim Jaggif84e2f62018-01-16 14:17:59 +01001/*
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
17package android.view;
18
Jorim Jaggi77be1572018-03-09 16:48:24 +010019import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
20
Jorim Jaggif84e2f62018-01-16 14:17:59 +010021import android.annotation.Nullable;
Mathew Inwooda570dee2018-08-17 14:56:00 +010022import android.annotation.UnsupportedAppUsage;
Jorim Jaggi77be1572018-03-09 16:48:24 +010023import android.app.WindowConfiguration;
24import android.app.WindowConfiguration.ActivityType;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010025import android.os.Parcel;
26import android.os.Parcelable;
Jorim Jaggi77be1572018-03-09 16:48:24 +010027import android.util.ArraySet;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010028import android.util.SparseArray;
29import android.view.WindowManager.TransitionType;
30
31/**
32 * Defines which animation types should be overridden by which remote animation.
33 *
34 * @hide
35 */
36public class RemoteAnimationDefinition implements Parcelable {
37
Jorim Jaggi77be1572018-03-09 16:48:24 +010038 private final SparseArray<RemoteAnimationAdapterEntry> mTransitionAnimationMap;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010039
Mathew Inwooda570dee2018-08-17 14:56:00 +010040 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +010041 public RemoteAnimationDefinition() {
42 mTransitionAnimationMap = new SparseArray<>();
43 }
44
45 /**
46 * Registers a remote animation for a specific transition.
47 *
48 * @param transition The transition type. Must be one of WindowManager.TRANSIT_* values.
Jorim Jaggi77be1572018-03-09 16:48:24 +010049 * @param activityTypeFilter The remote animation only runs if an activity with type of this
50 * parameter is involved in the transition.
51 * @param adapter The adapter that described how to run the remote animation.
52 */
Mathew Inwooda570dee2018-08-17 14:56:00 +010053 @UnsupportedAppUsage
Jorim Jaggi77be1572018-03-09 16:48:24 +010054 public void addRemoteAnimation(@TransitionType int transition,
55 @ActivityType int activityTypeFilter, RemoteAnimationAdapter adapter) {
56 mTransitionAnimationMap.put(transition,
57 new RemoteAnimationAdapterEntry(adapter, activityTypeFilter));
58 }
59
60 /**
61 * Registers a remote animation for a specific transition without defining an activity type
62 * filter.
63 *
64 * @param transition The transition type. Must be one of WindowManager.TRANSIT_* values.
Jorim Jaggif84e2f62018-01-16 14:17:59 +010065 * @param adapter The adapter that described how to run the remote animation.
66 */
Mathew Inwooda570dee2018-08-17 14:56:00 +010067 @UnsupportedAppUsage
Jorim Jaggif84e2f62018-01-16 14:17:59 +010068 public void addRemoteAnimation(@TransitionType int transition, RemoteAnimationAdapter adapter) {
Jorim Jaggi77be1572018-03-09 16:48:24 +010069 addRemoteAnimation(transition, ACTIVITY_TYPE_UNDEFINED, adapter);
Jorim Jaggif84e2f62018-01-16 14:17:59 +010070 }
71
72 /**
73 * Checks whether a remote animation for specific transition is defined.
74 *
75 * @param transition The transition type. Must be one of WindowManager.TRANSIT_* values.
Jorim Jaggi77be1572018-03-09 16:48:24 +010076 * @param activityTypes The set of activity types of activities that are involved in the
77 * transition. Will be used for filtering.
Jorim Jaggif84e2f62018-01-16 14:17:59 +010078 * @return Whether this definition has defined a remote animation for the specified transition.
79 */
Jorim Jaggi77be1572018-03-09 16:48:24 +010080 public boolean hasTransition(@TransitionType int transition, ArraySet<Integer> activityTypes) {
81 return getAdapter(transition, activityTypes) != null;
Jorim Jaggif84e2f62018-01-16 14:17:59 +010082 }
83
84 /**
85 * Retrieves the remote animation for a specific transition.
86 *
87 * @param transition The transition type. Must be one of WindowManager.TRANSIT_* values.
Jorim Jaggi77be1572018-03-09 16:48:24 +010088 * @param activityTypes The set of activity types of activities that are involved in the
89 * transition. Will be used for filtering.
Jorim Jaggif84e2f62018-01-16 14:17:59 +010090 * @return The remote animation adapter for the specified transition.
91 */
Jorim Jaggi77be1572018-03-09 16:48:24 +010092 public @Nullable RemoteAnimationAdapter getAdapter(@TransitionType int transition,
93 ArraySet<Integer> activityTypes) {
94 final RemoteAnimationAdapterEntry entry = mTransitionAnimationMap.get(transition);
95 if (entry == null) {
96 return null;
97 }
98 if (entry.activityTypeFilter == ACTIVITY_TYPE_UNDEFINED
99 || activityTypes.contains(entry.activityTypeFilter)) {
100 return entry.adapter;
101 } else {
102 return null;
103 }
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100104 }
105
106 public RemoteAnimationDefinition(Parcel in) {
Jorim Jaggi77be1572018-03-09 16:48:24 +0100107 final int size = in.readInt();
108 mTransitionAnimationMap = new SparseArray<>(size);
109 for (int i = 0; i < size; i++) {
110 final int transition = in.readInt();
111 final RemoteAnimationAdapterEntry entry = in.readTypedObject(
112 RemoteAnimationAdapterEntry.CREATOR);
113 mTransitionAnimationMap.put(transition, entry);
114 }
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100115 }
116
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100117 /**
118 * To be called by system_server to keep track which pid is running the remote animations inside
119 * this definition.
120 */
121 public void setCallingPid(int pid) {
122 for (int i = mTransitionAnimationMap.size() - 1; i >= 0; i--) {
Jorim Jaggi77be1572018-03-09 16:48:24 +0100123 mTransitionAnimationMap.valueAt(i).adapter.setCallingPid(pid);
Jorim Jaggibc2aabe2018-03-08 17:27:43 +0100124 }
125 }
126
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100127 @Override
128 public int describeContents() {
129 return 0;
130 }
131
132 @Override
133 public void writeToParcel(Parcel dest, int flags) {
Jorim Jaggi77be1572018-03-09 16:48:24 +0100134 final int size = mTransitionAnimationMap.size();
135 dest.writeInt(size);
136 for (int i = 0; i < size; i++) {
137 dest.writeInt(mTransitionAnimationMap.keyAt(i));
138 dest.writeTypedObject(mTransitionAnimationMap.valueAt(i), flags);
139 }
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100140 }
141
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700142 public static final @android.annotation.NonNull Creator<RemoteAnimationDefinition> CREATOR =
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100143 new Creator<RemoteAnimationDefinition>() {
144 public RemoteAnimationDefinition createFromParcel(Parcel in) {
145 return new RemoteAnimationDefinition(in);
146 }
147
148 public RemoteAnimationDefinition[] newArray(int size) {
149 return new RemoteAnimationDefinition[size];
150 }
151 };
Jorim Jaggi77be1572018-03-09 16:48:24 +0100152
153 private static class RemoteAnimationAdapterEntry implements Parcelable {
154
155 final RemoteAnimationAdapter adapter;
156
157 /**
158 * Only run the transition if one of the activities matches the filter.
159 * {@link WindowConfiguration.ACTIVITY_TYPE_UNDEFINED} means no filter
160 */
161 @ActivityType final int activityTypeFilter;
162
163 RemoteAnimationAdapterEntry(RemoteAnimationAdapter adapter, int activityTypeFilter) {
164 this.adapter = adapter;
165 this.activityTypeFilter = activityTypeFilter;
166 }
167
168 private RemoteAnimationAdapterEntry(Parcel in) {
169 adapter = in.readParcelable(RemoteAnimationAdapter.class.getClassLoader());
170 activityTypeFilter = in.readInt();
171 }
172
173 @Override
174 public void writeToParcel(Parcel dest, int flags) {
175 dest.writeParcelable(adapter, flags);
176 dest.writeInt(activityTypeFilter);
177 }
178
179 @Override
180 public int describeContents() {
181 return 0;
182 }
183
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700184 private static final @android.annotation.NonNull Creator<RemoteAnimationAdapterEntry> CREATOR
Jorim Jaggi77be1572018-03-09 16:48:24 +0100185 = new Creator<RemoteAnimationAdapterEntry>() {
186
187 @Override
188 public RemoteAnimationAdapterEntry createFromParcel(Parcel in) {
189 return new RemoteAnimationAdapterEntry(in);
190 }
191
192 @Override
193 public RemoteAnimationAdapterEntry[] newArray(int size) {
194 return new RemoteAnimationAdapterEntry[size];
195 }
196 };
197 }
Jorim Jaggif84e2f62018-01-16 14:17:59 +0100198}