blob: adcc495461934e699306d59830cf2431bba7008a [file] [log] [blame]
Winson Chungc2baac02017-01-11 13:34:47 -08001/*
2 * Copyright (C) 2017 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.app;
18
Jeff Sharkey000ce802017-04-29 13:13:27 -060019import android.annotation.Nullable;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010020import android.annotation.UnsupportedAppUsage;
Winson Chung08f81892017-03-02 15:40:51 -080021import android.graphics.Rect;
Jeff Sharkey000ce802017-04-29 13:13:27 -060022import android.os.Parcel;
23import android.os.Parcelable;
Winson Chung709904f2017-04-25 11:00:48 -070024import android.util.Rational;
Winson Chungc2baac02017-01-11 13:34:47 -080025
26import java.util.ArrayList;
27import java.util.List;
28
Jeff Sharkey000ce802017-04-29 13:13:27 -060029/** @removed */
Winson Chung709904f2017-04-25 11:00:48 -070030@Deprecated
Jeff Sharkey000ce802017-04-29 13:13:27 -060031public final class PictureInPictureArgs implements Parcelable {
Winson Chungc2baac02017-01-11 13:34:47 -080032
33 /**
Jeff Sharkey000ce802017-04-29 13:13:27 -060034 * Builder class for {@link PictureInPictureArgs} objects.
Winson Chungc2baac02017-01-11 13:34:47 -080035 */
Jeff Sharkey000ce802017-04-29 13:13:27 -060036 public static class Builder {
Winson Chungc2baac02017-01-11 13:34:47 -080037
Jeff Sharkey000ce802017-04-29 13:13:27 -060038 @Nullable
39 private Rational mAspectRatio;
40
41 @Nullable
42 private List<RemoteAction> mUserActions;
43
44 @Nullable
45 private Rect mSourceRectHint;
46
47 /**
48 * Sets the aspect ratio. This aspect ratio is defined as the desired width / height, and
49 * does not change upon device rotation.
50 *
51 * @param aspectRatio the new aspect ratio for the activity in picture-in-picture, must be
52 * between 2.39:1 and 1:2.39 (inclusive).
53 *
54 * @return this builder instance.
55 */
56 public Builder setAspectRatio(Rational aspectRatio) {
57 mAspectRatio = aspectRatio;
58 return this;
59 }
60
61 /**
62 * Sets the user actions. If there are more than
63 * {@link Activity#getMaxNumPictureInPictureActions()} actions, then the input list
64 * will be truncated to that number.
65 *
66 * @param actions the new actions to show in the picture-in-picture menu.
67 *
68 * @return this builder instance.
69 *
70 * @see RemoteAction
71 */
72 public Builder setActions(List<RemoteAction> actions) {
73 if (mUserActions != null) {
74 mUserActions = null;
75 }
76 if (actions != null) {
77 mUserActions = new ArrayList<>(actions);
78 }
79 return this;
80 }
81
82 /**
83 * Sets the source bounds hint. These bounds are only used when an activity first enters
84 * picture-in-picture, and describe the bounds in window coordinates of activity entering
85 * picture-in-picture that will be visible following the transition. For the best effect,
86 * these bounds should also match the aspect ratio in the arguments.
87 *
88 * @param launchBounds window-coordinate bounds indicating the area of the activity that
89 * will still be visible following the transition into picture-in-picture (eg. the video
90 * view bounds in a video player)
91 *
92 * @return this builder instance.
93 */
94 public Builder setSourceRectHint(Rect launchBounds) {
95 if (launchBounds == null) {
96 mSourceRectHint = null;
97 } else {
98 mSourceRectHint = new Rect(launchBounds);
99 }
100 return this;
101 }
102
103 public PictureInPictureArgs build() {
104 PictureInPictureArgs args = new PictureInPictureArgs(mAspectRatio, mUserActions,
105 mSourceRectHint);
106 return args;
Winson Chung8802eac2017-04-17 14:21:44 -0700107 }
Winson Chungc2baac02017-01-11 13:34:47 -0800108 }
109
110 /**
Jeff Sharkey000ce802017-04-29 13:13:27 -0600111 * The expected aspect ratio of the picture-in-picture.
Winson Chungc2baac02017-01-11 13:34:47 -0800112 */
Jeff Sharkey000ce802017-04-29 13:13:27 -0600113 @Nullable
114 private Rational mAspectRatio;
115
116 /**
117 * The set of actions that are associated with this activity when in picture-in-picture.
118 */
119 @Nullable
120 private List<RemoteAction> mUserActions;
121
122 /**
123 * The source bounds hint used when entering picture-in-picture, relative to the window bounds.
124 * We can use this internally for the transition into picture-in-picture to ensure that a
125 * particular source rect is visible throughout the whole transition.
126 */
127 @Nullable
128 private Rect mSourceRectHint;
129
130 /**
131 * The content insets that are used with the source hint rect for the transition into PiP where
132 * the insets are removed at the beginning of the transition.
133 */
134 @Nullable
135 private Rect mSourceRectHintInsets;
136
Winson Chungca754782017-05-02 15:14:23 -0700137 /**
138 * @hide
139 */
140 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100141 @UnsupportedAppUsage
Winson Chungca754782017-05-02 15:14:23 -0700142 public PictureInPictureArgs() {
143 }
144
145 /**
146 * @hide
147 */
148 @Deprecated
149 public PictureInPictureArgs(float aspectRatio, List<RemoteAction> actions) {
150 setAspectRatio(aspectRatio);
151 setActions(actions);
Jeff Sharkey000ce802017-04-29 13:13:27 -0600152 }
153
154 private PictureInPictureArgs(Parcel in) {
155 if (in.readInt() != 0) {
156 mAspectRatio = new Rational(in.readInt(), in.readInt());
157 }
158 if (in.readInt() != 0) {
159 mUserActions = new ArrayList<>();
160 in.readParcelableList(mUserActions, RemoteAction.class.getClassLoader());
161 }
162 if (in.readInt() != 0) {
163 mSourceRectHint = Rect.CREATOR.createFromParcel(in);
164 }
Jeff Sharkey000ce802017-04-29 13:13:27 -0600165 }
166
167 private PictureInPictureArgs(Rational aspectRatio, List<RemoteAction> actions,
168 Rect sourceRectHint) {
169 mAspectRatio = aspectRatio;
170 mUserActions = actions;
171 mSourceRectHint = sourceRectHint;
Winson Chungc2baac02017-01-11 13:34:47 -0800172 }
173
174 /**
Winson Chungca754782017-05-02 15:14:23 -0700175 * @hide
176 */
177 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100178 @UnsupportedAppUsage
Winson Chungca754782017-05-02 15:14:23 -0700179 public void setAspectRatio(float aspectRatio) {
180 // Temporary workaround
181 mAspectRatio = new Rational((int) (aspectRatio * 1000000000), 1000000000);
182 }
183
184 /**
185 * @hide
186 */
187 @Deprecated
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100188 @UnsupportedAppUsage
Winson Chungca754782017-05-02 15:14:23 -0700189 public void setActions(List<RemoteAction> actions) {
190 if (mUserActions != null) {
191 mUserActions = null;
192 }
193 if (actions != null) {
194 mUserActions = new ArrayList<>(actions);
195 }
196 }
197
198 /**
199 * @hide
200 */
201 @Deprecated
202 public void setSourceRectHint(Rect launchBounds) {
203 if (launchBounds == null) {
204 mSourceRectHint = null;
205 } else {
206 mSourceRectHint = new Rect(launchBounds);
207 }
208 }
209
210 /**
Jeff Sharkey000ce802017-04-29 13:13:27 -0600211 * Copies the set parameters from the other picture-in-picture args.
212 * @hide
Winson Chungc2baac02017-01-11 13:34:47 -0800213 */
Jeff Sharkey000ce802017-04-29 13:13:27 -0600214 public void copyOnlySet(PictureInPictureArgs otherArgs) {
215 if (otherArgs.hasSetAspectRatio()) {
216 mAspectRatio = otherArgs.mAspectRatio;
Winson Chungc2baac02017-01-11 13:34:47 -0800217 }
Jeff Sharkey000ce802017-04-29 13:13:27 -0600218 if (otherArgs.hasSetActions()) {
219 mUserActions = otherArgs.mUserActions;
220 }
221 if (otherArgs.hasSourceBoundsHint()) {
222 mSourceRectHint = new Rect(otherArgs.getSourceRectHint());
223 }
Winson Chungc2baac02017-01-11 13:34:47 -0800224 }
225
226 /**
Jeff Sharkey000ce802017-04-29 13:13:27 -0600227 * @return the aspect ratio. If none is set, return 0.
228 * @hide
229 */
230 public float getAspectRatio() {
231 if (mAspectRatio != null) {
232 return mAspectRatio.floatValue();
233 }
234 return 0f;
235 }
236
237 /** {@hide} */
238 public Rational getAspectRatioRational() {
239 return mAspectRatio;
240 }
241
242 /**
243 * @return whether the aspect ratio is set.
244 * @hide
245 */
246 public boolean hasSetAspectRatio() {
247 return mAspectRatio != null;
248 }
249
250 /**
251 * @return the set of user actions.
252 * @hide
253 */
254 public List<RemoteAction> getActions() {
255 return mUserActions;
256 }
257
258 /**
259 * @return whether the user actions are set.
260 * @hide
261 */
262 public boolean hasSetActions() {
263 return mUserActions != null;
264 }
265
266 /**
267 * Truncates the set of actions to the given {@param size}.
268 * @hide
269 */
270 public void truncateActions(int size) {
271 if (hasSetActions()) {
272 mUserActions = mUserActions.subList(0, Math.min(mUserActions.size(), size));
273 }
274 }
275
276 /**
277 * Sets the insets to be used with the source rect hint bounds.
278 * @hide
Winson Chung08f81892017-03-02 15:40:51 -0800279 */
Winson Chung709904f2017-04-25 11:00:48 -0700280 @Deprecated
Jeff Sharkey000ce802017-04-29 13:13:27 -0600281 public void setSourceRectHintInsets(Rect insets) {
282 if (insets == null) {
283 mSourceRectHintInsets = null;
Winson Chung08f81892017-03-02 15:40:51 -0800284 } else {
Jeff Sharkey000ce802017-04-29 13:13:27 -0600285 mSourceRectHintInsets = new Rect(insets);
Winson Chung08f81892017-03-02 15:40:51 -0800286 }
287 }
Jeff Sharkey000ce802017-04-29 13:13:27 -0600288
289 /**
290 * @return the source rect hint
291 * @hide
292 */
293 public Rect getSourceRectHint() {
294 return mSourceRectHint;
295 }
296
297 /**
298 * @return the source rect hint insets.
299 * @hide
300 */
301 public Rect getSourceRectHintInsets() {
302 return mSourceRectHintInsets;
303 }
304
305 /**
306 * @return whether there are launch bounds set
307 * @hide
308 */
309 public boolean hasSourceBoundsHint() {
310 return mSourceRectHint != null && !mSourceRectHint.isEmpty();
311 }
312
313 /**
314 * @return whether there are source rect hint insets set
315 * @hide
316 */
317 public boolean hasSourceBoundsHintInsets() {
318 return mSourceRectHintInsets != null;
319 }
320
321 @Override
322 public int describeContents() {
323 return 0;
324 }
325
326 @Override
327 public void writeToParcel(Parcel out, int flags) {
328 if (mAspectRatio != null) {
329 out.writeInt(1);
330 out.writeInt(mAspectRatio.getNumerator());
331 out.writeInt(mAspectRatio.getDenominator());
332 } else {
333 out.writeInt(0);
334 }
335 if (mUserActions != null) {
336 out.writeInt(1);
337 out.writeParcelableList(mUserActions, 0);
338 } else {
339 out.writeInt(0);
340 }
341 if (mSourceRectHint != null) {
342 out.writeInt(1);
343 mSourceRectHint.writeToParcel(out, 0);
344 } else {
345 out.writeInt(0);
346 }
Jeff Sharkey000ce802017-04-29 13:13:27 -0600347 }
348
349 public static final Creator<PictureInPictureArgs> CREATOR =
350 new Creator<PictureInPictureArgs>() {
351 public PictureInPictureArgs createFromParcel(Parcel in) {
352 return new PictureInPictureArgs(in);
353 }
354 public PictureInPictureArgs[] newArray(int size) {
355 return new PictureInPictureArgs[size];
356 }
357 };
358
359 public static PictureInPictureArgs convert(PictureInPictureParams params) {
360 return new PictureInPictureArgs(params.getAspectRatioRational(), params.getActions(),
361 params.getSourceRectHint());
362 }
363
364 public static PictureInPictureParams convert(PictureInPictureArgs args) {
365 return new PictureInPictureParams(args.getAspectRatioRational(), args.getActions(),
366 args.getSourceRectHint());
367 }
368}