summaryrefslogtreecommitdiff
path: root/core/java/android/window/BackNavigationInfo.java
blob: 0d392d4101a221b9c362e2d1c1e6ce58441fda45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.window;

import static android.app.ActivityTaskManager.INVALID_TASK_ID;

import android.annotation.AnimRes;
import android.annotation.ColorInt;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.TestApi;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteCallback;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * Information to be sent to SysUI about a back event.
 *
 * @hide
 */
@TestApi
public final class BackNavigationInfo implements Parcelable {

    /**
     * The target of the back navigation is undefined.
     */
    public static final int TYPE_UNDEFINED = -1;

    /**
     * Navigating back will close the currently visible dialog
     */
    public static final int TYPE_DIALOG_CLOSE = 0;

    /**
     * Navigating back will bring the user back to the home screen
     */
    public static final int TYPE_RETURN_TO_HOME = 1;

    /**
     * Navigating back will bring the user to the previous activity in the same Task
     */
    public static final int TYPE_CROSS_ACTIVITY = 2;

    /**
     * Navigating back will bring the user to the previous activity in the previous Task
     */
    public static final int TYPE_CROSS_TASK = 3;

    /**
     * A {@link OnBackInvokedCallback} is available and needs to be called.
     * <p>
     */
    public static final int TYPE_CALLBACK = 4;

    /**
     * Key to access the boolean value passed in {#mOnBackNavigationDone} result bundle
     * that represents if back navigation has been triggered.
     * @hide
     */
    public static final String KEY_NAVIGATION_FINISHED = "NavigationFinished";

    /**
     * Key to access the boolean value passed in {#mOnBackNavigationDone} result bundle
     * that represents if back gesture has been triggered.
     * @hide
     */
    public static final String KEY_GESTURE_FINISHED = "GestureFinished";

    /**
     * Touch gestured has transferred to embedded window, Shell should pilfer pointers so the
     * embedded won't receive motion events.
     * @hide
     */
    public static final String KEY_TOUCH_GESTURE_TRANSFERRED = "TouchGestureTransferred";

    /**
     * Key to access the displayId passed in result bundle that represents on which display
     * the back gesture has been triggered.
     * @hide
     */
    public static final String KEY_DISPLAY_ID = "DisplayId";


    /**
     * Defines the type of back destinations a back even can lead to. This is used to define the
     * type of animation that need to be run on SystemUI.
     * @hide
     */
    @IntDef(prefix = "TYPE_", value = {
            TYPE_UNDEFINED,
            TYPE_DIALOG_CLOSE,
            TYPE_RETURN_TO_HOME,
            TYPE_CROSS_ACTIVITY,
            TYPE_CROSS_TASK,
            TYPE_CALLBACK
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface BackTargetType {
    }

    private final int mType;
    @Nullable
    private final RemoteCallback mOnBackNavigationDone;
    @Nullable
    private final IOnBackInvokedCallback mOnBackInvokedCallback;
    private final boolean mPrepareRemoteAnimation;
    private final boolean mAnimationCallback;
    @Nullable
    private final CustomAnimationInfo mCustomAnimationInfo;

    private final int mLetterboxColor;
    @NonNull
    private final Rect mTouchableRegion;

    private boolean mAppProgressGenerationAllowed;
    private final int mFocusedTaskId;

    /**
     * Create a new {@link BackNavigationInfo} instance.
     *
     * @param type                  The {@link BackTargetType} of the destination (what will be
     * @param onBackNavigationDone  The callback to be called once the client is done with the
     *                              back preview.
     * @param onBackInvokedCallback The back callback registered by the current top level window.
     */
    private BackNavigationInfo(@BackTargetType int type,
            @Nullable RemoteCallback onBackNavigationDone,
            @Nullable IOnBackInvokedCallback onBackInvokedCallback,
            boolean isPrepareRemoteAnimation,
            boolean isAnimationCallback,
            @Nullable CustomAnimationInfo customAnimationInfo,
            int letterboxColor,
            @Nullable Rect touchableRegion,
            boolean appProgressGenerationAllowed,
            int focusedTaskId) {
        mType = type;
        mOnBackNavigationDone = onBackNavigationDone;
        mOnBackInvokedCallback = onBackInvokedCallback;
        mPrepareRemoteAnimation = isPrepareRemoteAnimation;
        mAnimationCallback = isAnimationCallback;
        mCustomAnimationInfo = customAnimationInfo;
        mLetterboxColor = letterboxColor;
        mTouchableRegion = new Rect(touchableRegion);
        mAppProgressGenerationAllowed = appProgressGenerationAllowed;
        mFocusedTaskId = focusedTaskId;
    }

    private BackNavigationInfo(@NonNull Parcel in) {
        mType = in.readInt();
        mOnBackNavigationDone = in.readTypedObject(RemoteCallback.CREATOR);
        mOnBackInvokedCallback = IOnBackInvokedCallback.Stub.asInterface(in.readStrongBinder());
        mPrepareRemoteAnimation = in.readBoolean();
        mAnimationCallback = in.readBoolean();
        mCustomAnimationInfo = in.readTypedObject(CustomAnimationInfo.CREATOR);
        mLetterboxColor = in.readInt();
        mTouchableRegion = in.readTypedObject(Rect.CREATOR);
        mAppProgressGenerationAllowed = in.readBoolean();
        mFocusedTaskId = in.readInt();
    }

    /** @hide */
    @Override
    public void writeToParcel(@NonNull Parcel dest, int flags) {
        dest.writeInt(mType);
        dest.writeTypedObject(mOnBackNavigationDone, flags);
        dest.writeStrongInterface(mOnBackInvokedCallback);
        dest.writeBoolean(mPrepareRemoteAnimation);
        dest.writeBoolean(mAnimationCallback);
        dest.writeTypedObject(mCustomAnimationInfo, flags);
        dest.writeInt(mLetterboxColor);
        dest.writeTypedObject(mTouchableRegion, flags);
        dest.writeBoolean(mAppProgressGenerationAllowed);
        dest.writeInt(mFocusedTaskId);
    }

    /**
     * Returns the type of back navigation that is about to happen.
     * @hide
     * @see BackTargetType
     */
    public @BackTargetType int getType() {
        return mType;
    }

    /**
     * Returns the {@link OnBackInvokedCallback} of the top level window or null if
     * the client didn't register a callback.
     * <p>
     * This is never null when {@link #getType} returns {@link #TYPE_CALLBACK}.
     * @hide
     * @see OnBackInvokedCallback
     * @see OnBackInvokedDispatcher
     */
    @Nullable
    public IOnBackInvokedCallback getOnBackInvokedCallback() {
        return mOnBackInvokedCallback;
    }

    /**
     * Return true if the core is preparing a back gesture animation.
     * @hide
     */
    public boolean isPrepareRemoteAnimation() {
        return mPrepareRemoteAnimation;
    }

    /**
     * Return true if the callback is {@link OnBackAnimationCallback}.
     * @hide
     */
    public boolean isAnimationCallback() {
        return mAnimationCallback;
    }

    /**
     * @return Letterbox color
     * @hide
     */
    public int getLetterboxColor() {
        return mLetterboxColor;
    }

    /**
     * @return The app window region where the client can handle touch event.
     * @hide
     */
    @NonNull
    public Rect getTouchableRegion() {
        return mTouchableRegion;
    }

    /**
     * @return The client side view is able to intercept back progress event.
     * @hide
     */
    public boolean isAppProgressGenerationAllowed() {
        return mAppProgressGenerationAllowed;
    }

    /**
     * @return The focused task id when back gesture start.
     * @hide
     */
    public int getFocusedTaskId() {
        return mFocusedTaskId;
    }

    /**
     * Force disable app to intercept back progress event.
     * @hide
     */
    public void disableAppProgressGenerationAllowed() {
        mAppProgressGenerationAllowed = false;
    }

    /**
     * Callback to be called when the back preview is finished in order to notify the server that
     * it can clean up the resources created for the animation.
     * @hide
     * @param triggerBack Boolean indicating if back navigation has been triggered.
     */
    public void onBackNavigationFinished(boolean triggerBack) {
        if (mOnBackNavigationDone != null) {
            Bundle result = new Bundle();
            result.putBoolean(KEY_NAVIGATION_FINISHED, triggerBack);
            mOnBackNavigationDone.sendResult(result);
        }
    }

    /**
     * Get customize animation info.
     * @hide
     */
    @Nullable
    public CustomAnimationInfo getCustomAnimationInfo() {
        return mCustomAnimationInfo;
    }

    /** @hide */
    @Override
    public int describeContents() {
        return 0;
    }

    @NonNull
    public static final Creator<BackNavigationInfo> CREATOR = new Creator<BackNavigationInfo>() {
        @Override
        public BackNavigationInfo createFromParcel(Parcel in) {
            return new BackNavigationInfo(in);
        }

        @Override
        public BackNavigationInfo[] newArray(int size) {
            return new BackNavigationInfo[size];
        }
    };

    @Override
    public String toString() {
        return "BackNavigationInfo{"
                + "mType=" + typeToString(mType) + " (" + mType + ")"
                + ", mOnBackNavigationDone=" + mOnBackNavigationDone
                + ", mOnBackInvokedCallback=" + mOnBackInvokedCallback
                + ", mPrepareRemoteAnimation=" + mPrepareRemoteAnimation
                + ", mAnimationCallback=" + mAnimationCallback
                + ", mCustomizeAnimationInfo=" + mCustomAnimationInfo
                + '}';
    }

    /**
     * Translates the {@link BackNavigationInfo} integer type to its String representation
     */
    @NonNull
    public static String typeToString(@BackTargetType int type) {
        switch (type) {
            case TYPE_UNDEFINED:
                return "TYPE_UNDEFINED";
            case TYPE_DIALOG_CLOSE:
                return "TYPE_DIALOG_CLOSE";
            case TYPE_RETURN_TO_HOME:
                return "TYPE_RETURN_TO_HOME";
            case TYPE_CROSS_ACTIVITY:
                return "TYPE_CROSS_ACTIVITY";
            case TYPE_CROSS_TASK:
                return "TYPE_CROSS_TASK";
            case TYPE_CALLBACK:
                return "TYPE_CALLBACK";
        }
        return String.valueOf(type);
    }

    /**
     * Information for customize back animation.
     * @hide
     */
    public static final class CustomAnimationInfo implements Parcelable {
        private final String mPackageName;
        private int mWindowAnimations;
        @AnimRes private int mCustomExitAnim;
        @AnimRes private int mCustomEnterAnim;
        @ColorInt private int mCustomBackground;

        /**
         * The package name of the windowAnimations.
         */
        @NonNull
        public String getPackageName() {
            return mPackageName;
        }

        /**
         * The resource Id of window animations.
         */
        public int getWindowAnimations() {
            return mWindowAnimations;
        }

        /**
         * The exit animation resource Id of customize activity transition.
         */
        public int getCustomExitAnim() {
            return mCustomExitAnim;
        }

        /**
         * The entering animation resource Id of customize activity transition.
         */
        public int getCustomEnterAnim() {
            return mCustomEnterAnim;
        }

        /**
         * The background color of customize activity transition.
         */
        public int getCustomBackground() {
            return mCustomBackground;
        }

        public CustomAnimationInfo(@NonNull String packageName) {
            this.mPackageName = packageName;
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(@NonNull Parcel dest, int flags) {
            dest.writeString8(mPackageName);
            dest.writeInt(mWindowAnimations);
            dest.writeInt(mCustomEnterAnim);
            dest.writeInt(mCustomExitAnim);
            dest.writeInt(mCustomBackground);
        }

        private CustomAnimationInfo(@NonNull Parcel in) {
            mPackageName = in.readString8();
            mWindowAnimations = in.readInt();
            mCustomEnterAnim = in.readInt();
            mCustomExitAnim = in.readInt();
            mCustomBackground = in.readInt();
        }

        @Override
        public String toString() {
            return "CustomAnimationInfo, package name= " + mPackageName;
        }

        @NonNull
        public static final Creator<CustomAnimationInfo> CREATOR = new Creator<>() {
            @Override
            public CustomAnimationInfo createFromParcel(Parcel in) {
                return new CustomAnimationInfo(in);
            }

            @Override
            public CustomAnimationInfo[] newArray(int size) {
                return new CustomAnimationInfo[size];
            }
        };
    }
    /**
     * @hide
     */
    @SuppressWarnings("UnusedReturnValue") // Builder pattern
    public static class Builder {
        private int mType = TYPE_UNDEFINED;
        @Nullable
        private RemoteCallback mOnBackNavigationDone = null;
        @Nullable
        private IOnBackInvokedCallback mOnBackInvokedCallback = null;
        private boolean mPrepareRemoteAnimation;
        private CustomAnimationInfo mCustomAnimationInfo;
        private boolean mAnimationCallback = false;

        private int mLetterboxColor = Color.TRANSPARENT;
        private Rect mTouchableRegion;
        private boolean mAppProgressGenerationAllowed;
        private int mFocusedTaskId = INVALID_TASK_ID;

        /**
         * @see BackNavigationInfo#getType()
         */
        public Builder setType(@BackTargetType int type) {
            mType = type;
            return this;
        }

        /**
         * @see BackNavigationInfo#onBackNavigationFinished(boolean)
         */
        public Builder setOnBackNavigationDone(@Nullable RemoteCallback onBackNavigationDone) {
            mOnBackNavigationDone = onBackNavigationDone;
            return this;
        }

        /**
         * @see BackNavigationInfo#getOnBackInvokedCallback
         */
        public Builder setOnBackInvokedCallback(
                @Nullable IOnBackInvokedCallback onBackInvokedCallback) {
            mOnBackInvokedCallback = onBackInvokedCallback;
            return this;
        }

        /**
         * @param prepareRemoteAnimation Whether core prepare animation for shell.
         */
        public Builder setPrepareRemoteAnimation(boolean prepareRemoteAnimation) {
            mPrepareRemoteAnimation = prepareRemoteAnimation;
            return this;
        }

        /**
         * Set windowAnimations for customize animation.
         */
        public Builder setWindowAnimations(String packageName, int windowAnimations) {
            if (mCustomAnimationInfo == null) {
                mCustomAnimationInfo = new CustomAnimationInfo(packageName);
            }
            mCustomAnimationInfo.mWindowAnimations = windowAnimations;
            return this;
        }

        /**
         * Set resources ids for customize activity animation.
         */
        public Builder setCustomAnimation(String packageName, @AnimRes int enterResId,
                @AnimRes int exitResId, @ColorInt int backgroundColor) {
            if (mCustomAnimationInfo == null) {
                mCustomAnimationInfo = new CustomAnimationInfo(packageName);
            }
            mCustomAnimationInfo.mCustomExitAnim = exitResId;
            mCustomAnimationInfo.mCustomEnterAnim = enterResId;
            mCustomAnimationInfo.mCustomBackground = backgroundColor;
            return this;
        }

        /**
         * @param isAnimationCallback whether the callback is {@link OnBackAnimationCallback}
         */
        public Builder setAnimationCallback(boolean isAnimationCallback) {
            mAnimationCallback = isAnimationCallback;
            return this;
        }

        /**
         * @param color Non-transparent if there contain letterbox color.
         */
        public Builder setLetterboxColor(int color) {
            mLetterboxColor = color;
            return this;
        }

        /**
         * @param rect Non-empty for frame of current focus window.
         */
        public Builder setTouchableRegion(Rect rect) {
            mTouchableRegion = new Rect(rect);
            return this;
        }

        /**
         * @param allowed Whether client side view able to intercept back progress event.
         */
        public Builder setAppProgressAllowed(boolean allowed) {
            mAppProgressGenerationAllowed = allowed;
            return this;
        }

        /**
         * @param focusedTaskId The current focused taskId when back gesture start.
         */
        public Builder setFocusedTaskId(int focusedTaskId) {
            mFocusedTaskId = focusedTaskId;
            return this;
        }

        /**
         * Builds and returns an instance of {@link BackNavigationInfo}
         */
        public BackNavigationInfo build() {
            return new BackNavigationInfo(mType, mOnBackNavigationDone,
                    mOnBackInvokedCallback,
                    mPrepareRemoteAnimation,
                    mAnimationCallback,
                    mCustomAnimationInfo,
                    mLetterboxColor,
                    mTouchableRegion,
                    mAppProgressGenerationAllowed,
                    mFocusedTaskId);
        }
    }
}