/* * 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 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. *
*/ 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"; /** * 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 final boolean mAppProgressGenerationAllowed; /** * 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) { mType = type; mOnBackNavigationDone = onBackNavigationDone; mOnBackInvokedCallback = onBackInvokedCallback; mPrepareRemoteAnimation = isPrepareRemoteAnimation; mAnimationCallback = isAnimationCallback; mCustomAnimationInfo = customAnimationInfo; mLetterboxColor = letterboxColor; mTouchableRegion = new Rect(touchableRegion); mAppProgressGenerationAllowed = appProgressGenerationAllowed; } 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(); } /** @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); } /** * 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. *
* 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;
}
/**
* 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);
}
}
/**
* Callback to be called when the back gesture is finished in order to notify the server that
* it can ask app to start rendering.
* @hide
* @param triggerBack Boolean indicating if back gesture has been triggered.
*/
public void onBackGestureFinished(boolean triggerBack) {
if (mOnBackNavigationDone != null) {
Bundle result = new Bundle();
result.putBoolean(KEY_GESTURE_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