Fixed
Status Update
Comments
ra...@google.com <ra...@google.com>
ni...@google.com <ni...@google.com> #2
Diego, currently computeCurrentWindowMetrics
requires a @UiContext
and will crash if the provided context cannot be unwrapped to an Activity
or InputMethodService
.
For views that are created without a UiContext, such as in a robolectric test, or when inflated using the applicationContext (for example to be added to window manager directly with WM#addView), should computeCurrentWindowMetrics implement some fallback behavior (such as size of the ViewRootImpl, or default display size)? If not then consumers of this API will need to define their own fallbacks which will lead to inconsistency
ap...@google.com <ap...@google.com> #3
Do you mind providing code samples and then I can look into it further.
ap...@google.com <ap...@google.com> #4
Sure, something like this: (make sure to run it on a device below R)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(Button(this).apply {
setText("Hello world")
setOnClickListener {
addOverlayView()
}
})
}
fun addOverlayView() {
val view = MyCustomView(applicationContext).apply {
updateWindowSizeClass()
}
val params = WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
).apply { token =
window.decorView.getRootView().windowToken
}
val windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
windowManager.addView(view, params)
}
}
class MyCustomView(context: Context) : TextView(context) {
fun updateWindowSizeClass() {
val metrics = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(context)
setText("Width = ${metrics.bounds.width()} height = ${metrics.bounds.height()}")
}
}
Description
Bug to track TODOs in the codebase to remove @Experimental APIs from 1.1