1 /* 2 * Copyright 2021 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 package androidx.window.layout.adapter 17 18 import android.app.Activity 19 import android.content.Context 20 import android.inputmethodservice.InputMethodService 21 import androidx.annotation.RestrictTo 22 import androidx.annotation.UiContext 23 import androidx.core.util.Consumer 24 import androidx.window.RequiresWindowSdkExtension 25 import androidx.window.layout.SupportedPosture 26 import androidx.window.layout.WindowLayoutInfo 27 import java.util.concurrent.Executor 28 29 /** 30 * Backing interface for [androidx.window.layout.WindowInfoTracker] instances that serve as the 31 * default information supplier. 32 */ 33 internal interface WindowBackend { 34 35 /** 36 * Registers a callback for layout changes of the window for the supplied [UiContext]. Must be 37 * called only after the it is attached to the window. The supplied [UiContext] should 38 * correspond to a window or an area on the screen. It must be either an [Activity] or a 39 * [UiContext] created with [Context#createWindowContext]. 40 * 41 * @throws IllegalArgumentException when [context] is not an [UiContext]. 42 */ registerLayoutChangeCallbacknull43 fun registerLayoutChangeCallback( 44 @UiContext context: Context, 45 executor: Executor, 46 callback: Consumer<WindowLayoutInfo> 47 ) 48 49 /** Unregisters a callback for window layout changes. */ 50 fun unregisterLayoutChangeCallback(callback: Consumer<WindowLayoutInfo>) 51 52 @RestrictTo(RestrictTo.Scope.LIBRARY) 53 fun hasRegisteredListeners(): Boolean { 54 return false 55 } 56 57 /** 58 * Returns a [List] of [SupportedPosture] for the device. 59 * 60 * @throws UnsupportedOperationException if the Window SDK version is less than 6. 61 */ 62 @RequiresWindowSdkExtension(version = 6) 63 @get:RequiresWindowSdkExtension(version = 6) 64 val supportedPostures: List<SupportedPosture> 65 66 /** 67 * Returns the current [WindowLayoutInfo] for the given [Context]. 68 * 69 * This API provides a convenient way to access the current [WindowLayoutInfo] without 70 * registering a listener via [registerLayoutChangeCallback]. It simplifies the retrieval of 71 * [WindowLayoutInfo] in scenarios like [Activity.onCreate]. 72 * 73 * @param context a [Context] that corresponds to a window or an area on the screen. This can be 74 * an [Activity], a [Context] created with [Context.createWindowContext], or an 75 * [InputMethodService]. 76 * @return the current [WindowLayoutInfo] for the given [Context]. 77 * @throws UnsupportedOperationException if the Window SDK extension version is less than 9. 78 * @throws IllegalArgumentException when [context] is not an [UiContext]. 79 */ 80 @RequiresWindowSdkExtension(version = 9) getCurrentWindowLayoutInfonull81 fun getCurrentWindowLayoutInfo(@UiContext context: Context): WindowLayoutInfo 82 } 83