ContextCompatHelper.kt
/*
* Copyright 2022 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 androidx.window.layout.util
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.graphics.Rect
import android.inputmethodservice.InputMethodService
import android.os.Build
import android.view.WindowManager
import androidx.annotation.DoNotInline
import androidx.annotation.RequiresApi
import androidx.annotation.UiContext
import androidx.core.view.WindowInsetsCompat
import androidx.window.layout.WindowMetrics
internal object ContextCompatHelper {
/**
* Given a [UiContext], check if it is a [ContextWrapper]. If so, we need to unwrap it and
* return the actual [UiContext] within.
*/
@UiContext
internal fun unwrapUiContext(@UiContext context: Context): Context {
var iterator = context
while (iterator is ContextWrapper) {
if (iterator is Activity) {
// Activities are always ContextWrappers
return iterator
} else if (iterator is InputMethodService) {
// InputMethodService are always ContextWrappers
return iterator
} else if (iterator.baseContext == null) {
return iterator
}
iterator = iterator.baseContext
}
// TODO(b/259148796): This code path is not needed for APIs R and above. However, that is
// not clear and also not enforced anywhere. Once we move to version-based implementations,
// this ambiguity will no longer exist. Again for clarity, on APIs before R, UiContexts are
// Activities or InputMethodServices, so we should never reach this point.
throw IllegalArgumentException("Context $context is not a UiContext")
}
}
@RequiresApi(Build.VERSION_CODES.N)
internal object ContextCompatHelperApi24 {
fun isInMultiWindowMode(activity: Activity): Boolean {
return activity.isInMultiWindowMode
}
}
@RequiresApi(Build.VERSION_CODES.R)
internal object ContextCompatHelperApi30 {
fun currentWindowMetrics(@UiContext context: Context): WindowMetrics {
val wm = context.getSystemService(WindowManager::class.java)
val insets = WindowInsetsCompat.toWindowInsetsCompat(wm.currentWindowMetrics.windowInsets)
return WindowMetrics(wm.currentWindowMetrics.bounds, insets)
}
fun currentWindowBounds(@UiContext context: Context): Rect {
val wm = context.getSystemService(WindowManager::class.java)
return wm.currentWindowMetrics.bounds
}
fun maximumWindowBounds(@UiContext context: Context): Rect {
val wm = context.getSystemService(WindowManager::class.java)
return wm.maximumWindowMetrics.bounds
}
/**
* Computes the [WindowInsetsCompat] for platforms above [Build.VERSION_CODES.R], inclusive.
* @DoNotInline required for implementation-specific class method to prevent it from being
* inlined.
*
* @see androidx.window.layout.WindowMetrics.getWindowInsets
*/
@DoNotInline
fun currentWindowInsets(@UiContext context: Context): WindowInsetsCompat {
val platformInsets = context.getSystemService(WindowManager::class.java)
.currentWindowMetrics.windowInsets
return WindowInsetsCompat.toWindowInsetsCompat(platformInsets)
}
}