Skip to content

Commit

Permalink
Updates captureToBitmap in ViewCapture to treat API 34 the same way t…
Browse files Browse the repository at this point in the history
…hat all previous API works.

Because API 34 wasn't getting the surface, it was including the action bar and messing up the screenshots. By removing it, it will use the surface instead and it works perfectly.

API 28 still does not work right with dialog boxes, but that is a known issue that we don't have a fix for yet.

To test this, I added tests for every API level and fixed a bug where the action bar wasn't showing up in the test.

PiperOrigin-RevId: 588915479
  • Loading branch information
copybara-androidxtest committed Dec 19, 2023
1 parent d9655d6 commit 56f67d2
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/CHANGELOG.md
Expand Up @@ -6,6 +6,8 @@

**Bug Fixes**

* ViewCapture.captureToBitmap API 34 now uses the surface directly to take the screenshot.

**New Features**

**Breaking Changes**
Expand Down
3 changes: 0 additions & 3 deletions core/java/androidx/test/core/view/ViewCapture.kt
Expand Up @@ -60,7 +60,6 @@ import java.util.function.Consumer
* This API is currently experimental and subject to change or removal.
*/
@ExperimentalTestApi
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
fun View.captureToBitmap(rect: Rect? = null): ListenableFuture<Bitmap> {
val bitmapFuture: ResolvableFuture<Bitmap> = ResolvableFuture.create()
val mainExecutor = HandlerExecutor(Handler(Looper.getMainLooper()))
Expand Down Expand Up @@ -91,7 +90,6 @@ fun View.captureToBitmap(rect: Rect? = null): ListenableFuture<Bitmap> {
* @return a [ListenableFuture] that will be complete once ui drawing is complete
*/
// NoClassDefFoundError occurs on API 15
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN)
// @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
@ExperimentalTestApi
fun View.forceRedraw(): ListenableFuture<Void> {
Expand Down Expand Up @@ -128,7 +126,6 @@ private fun View.generateBitmap(bitmapFuture: ResolvableFuture<Bitmap>, rect: Re
val destBitmap = Bitmap.createBitmap(rectWidth, rectHeight, Bitmap.Config.ARGB_8888)
when {
Build.VERSION.SDK_INT < 26 -> generateBitmapFromDraw(destBitmap, bitmapFuture, rect)
Build.VERSION.SDK_INT >= 34 -> generateBitmapFromPixelCopy(destBitmap, bitmapFuture, rect)
this is SurfaceView -> generateBitmapFromSurfaceViewPixelCopy(destBitmap, bitmapFuture, rect)
else -> generateBitmapFromPixelCopy(this.getSurface(), destBitmap, bitmapFuture, rect)
}
Expand Down

0 comments on commit 56f67d2

Please sign in to comment.