| 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | package android.window; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.app.Activity; |
| 21 | import android.app.Dialog; |
| 22 | import android.view.Window; |
| 23 | |
| 24 | /** |
| 25 | * Callback allowing applications to handle back events in place of the system. |
| 26 | * <p> |
| 27 | * Callback instances can be added to and removed from {@link OnBackInvokedDispatcher}, which |
| 28 | * is held at window level and accessible through {@link Activity#getOnBackInvokedDispatcher()}, |
| 29 | * {@link Dialog#getOnBackInvokedDispatcher()} and {@link Window#getOnBackInvokedDispatcher()}. |
| 30 | * <p> |
| 31 | * When back is triggered, callbacks on the in-focus window are invoked in reverse order in which |
| 32 | * they are added within the same priority. Between different priorities, callbacks with higher |
| 33 | * priority are invoked first. |
| 34 | * <p> |
| 35 | * This replaces {@link Activity#onBackPressed()}, {@link Dialog#onBackPressed()} and |
| 36 | * {@link android.view.KeyEvent#KEYCODE_BACK} |
| 37 | * <p> |
| 38 | * @see OnBackInvokedDispatcher#registerOnBackInvokedCallback(int, OnBackInvokedCallback) |
| 39 | * registerOnBackInvokedCallback(priority, OnBackInvokedCallback) |
| 40 | * to specify callback priority. |
| 41 | */ |
| 42 | @SuppressWarnings("deprecation") |
| 43 | public interface OnBackInvokedCallback { |
| 44 | /** |
| 45 | * Called when a back gesture has been started, or back button has been pressed down. |
| 46 | * |
| 47 | * @param backEvent The {@link BackEvent} containing information about the touch or |
| 48 | * button press. |
| 49 | * |
| 50 | * @hide |
| 51 | */ |
| 52 | default void onBackStarted(@NonNull BackEvent backEvent) {} |
| 53 | |
| 54 | /** |
| 55 | * Called when a back gesture has been progressed. |
| 56 | * |
| 57 | * @param backEvent The {@link BackEvent} containing information about the latest touch point |
| 58 | * and the progress that the back animation should seek to. |
| 59 | * |
| 60 | * @hide |
| 61 | */ |
| 62 | default void onBackProgressed(@NonNull BackEvent backEvent) {} |
| 63 | |
| 64 | /** |
| 65 | * Called when a back gesture has been completed and committed, or back button pressed |
| 66 | * has been released and committed. |
| 67 | */ |
| 68 | void onBackInvoked(); |
| 69 | |
| 70 | /** |
| 71 | * Called when a back gesture or button press has been cancelled. |
| 72 | * |
| 73 | * @hide |
| 74 | */ |
| 75 | default void onBackCancelled() {} |
| 76 | } |