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.FlaggedApi; 20 import android.annotation.IntDef; 21 import android.annotation.IntRange; 22 import android.annotation.NonNull; 23 import android.annotation.SuppressLint; 24 25 import com.android.window.flags.Flags; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 30 /** 31 * Dispatcher to register {@link OnBackInvokedCallback} instances for handling 32 * back invocations. 33 * 34 * It also provides interfaces to update the attributes of {@link OnBackInvokedCallback}. 35 * Attribute updates are proactively pushed to the window manager if they change the dispatch 36 * target (a.k.a. the callback to be invoked next), or its behavior. 37 */ 38 public interface OnBackInvokedDispatcher { 39 /** @hide */ 40 String TAG = "OnBackInvokedDispatcher"; 41 42 /** @hide */ 43 boolean DEBUG = false; 44 45 /** @hide */ 46 @IntDef({ 47 PRIORITY_DEFAULT, 48 PRIORITY_OVERLAY, 49 PRIORITY_SYSTEM_NAVIGATION_OBSERVER, 50 }) 51 @Retention(RetentionPolicy.SOURCE) 52 @interface Priority{} 53 54 /** 55 * Priority level of {@link OnBackInvokedCallback}s for overlays such as menus and 56 * navigation drawers that should receive back dispatch before non-overlays. 57 */ 58 int PRIORITY_OVERLAY = 1000000; 59 60 /** 61 * Default priority level of {@link OnBackInvokedCallback}s. 62 */ 63 int PRIORITY_DEFAULT = 0; 64 65 /** 66 * Priority level of {@link OnBackInvokedCallback}s registered by the system. 67 * 68 * System back animation will play when the callback to receive dispatch has this priority. 69 * @hide 70 */ 71 int PRIORITY_SYSTEM = -1; 72 73 /** 74 * Priority level of {@link OnBackInvokedCallback}s designed to observe system-level back 75 * handling. 76 * 77 * <p>Callbacks registered with this priority do not consume back events. They receive back 78 * events whenever the system handles a back navigation and have no impact on the normal back 79 * navigation flow. Useful for logging or analytics. 80 * 81 * <p>Only one callback with {@link #PRIORITY_SYSTEM_NAVIGATION_OBSERVER} can be registered at a 82 * time. 83 */ 84 @FlaggedApi(Flags.FLAG_PREDICTIVE_BACK_PRIORITY_SYSTEM_NAVIGATION_OBSERVER) 85 int PRIORITY_SYSTEM_NAVIGATION_OBSERVER = -2; 86 87 /** 88 * Registers a {@link OnBackInvokedCallback}. 89 * 90 * Within the same priority level, callbacks are invoked in the reverse order in which 91 * they are registered. Higher priority callbacks are invoked before lower priority ones. 92 * 93 * @param priority The priority of the callback. 94 * @param callback The callback to be registered. If the callback instance has been already 95 * registered, the existing instance (no matter its priority) will be 96 * unregistered and registered again. 97 * @throws IllegalArgumentException if the priority is negative. 98 */ 99 @SuppressLint({"ExecutorRegistration"}) registerOnBackInvokedCallback( @riority @ntRangefrom = 0) int priority, @NonNull OnBackInvokedCallback callback)100 void registerOnBackInvokedCallback( 101 @Priority @IntRange(from = 0) int priority, @NonNull OnBackInvokedCallback callback); 102 103 /** 104 * Unregisters a {@link OnBackInvokedCallback}. 105 * 106 * @param callback The callback to be unregistered. Does nothing if the callback has not been 107 * registered. 108 */ unregisterOnBackInvokedCallback(@onNull OnBackInvokedCallback callback)109 void unregisterOnBackInvokedCallback(@NonNull OnBackInvokedCallback callback); 110 111 /** 112 * Registers a {@link OnBackInvokedCallback} with system priority. 113 * @hide 114 */ registerSystemOnBackInvokedCallback(@onNull OnBackInvokedCallback callback)115 default void registerSystemOnBackInvokedCallback(@NonNull OnBackInvokedCallback callback) { } 116 117 118 /** 119 * Sets an {@link ImeOnBackInvokedDispatcher} to forward {@link OnBackInvokedCallback}s 120 * from IME to the app process to be registered on the app window. 121 * 122 * Only call this on the IME window. Create the {@link ImeOnBackInvokedDispatcher} from 123 * the application process and override 124 * {@link ImeOnBackInvokedDispatcher#getReceivingDispatcher()} to point to the app 125 * window's {@link WindowOnBackInvokedDispatcher}. 126 * 127 * @hide 128 */ setImeOnBackInvokedDispatcher( @onNull ImeOnBackInvokedDispatcher imeDispatcher)129 default void setImeOnBackInvokedDispatcher( 130 @NonNull ImeOnBackInvokedDispatcher imeDispatcher) { } 131 } 132