code: public static Bitmap createScreenshot(Context context) {
          if (!hasScreenshotPermission(context)) {
              LogUtils.log(ScreenshotUtils.class, Log.ERROR, "Screenshot permission denied.");
              return null;
          }
  
          final WindowManager windowManager =
                  (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  
          final Bitmap bitmap = SurfaceControlCompatUtils.screenshot(0, 0);
  
          // Bail if we couldn't take the screenshot.
          if (bitmap == null) {
              LogUtils.log(ScreenshotUtils.class, Log.ERROR, "Failed to take screenshot.");
              return null;
          }
  
          final int width = bitmap.getWidth();
          final int height = bitmap.getHeight();
          final int rotation = windowManager.getDefaultDisplay().getRotation();
  
          final int outWidth;
          final int outHeight;
          final float rotationDegrees;
  
          switch (rotation) {
              case Surface.ROTATION_90:
                  outWidth = height;
                  outHeight = width;
                  rotationDegrees = 90;
                  break;
              case Surface.ROTATION_180:
                  outWidth = width;
                  outHeight = height;
                  rotationDegrees = 180;
                  break;
              case Surface.ROTATION_270:
                  outWidth = height;
                  outHeight = width;
                  rotationDegrees = 270;
                  break;
              default:
                  return bitmap;
          }
  
          // Rotate the screenshot to match the screen orientation.
          final Bitmap rotatedBitmap =
                  Bitmap.createBitmap(outWidth, outHeight, Bitmap.Config.RGB_565);
          final Canvas c = new Canvas(rotatedBitmap);
  
          c.translate(outWidth / 2.0f, outHeight / 2.0f);
          c.rotate(-rotationDegrees);
          c.translate(-width / 2.0f, -height / 2.0f);
          c.drawBitmap(bitmap, 0, 0, null);
  
          bitmap.recycle();
  
          return rotatedBitmap;
      }
docs: Returns a screenshot with the contents of the current display that
  matches the current display rotation.
  
  @param context The current context.
  @return A bitmap of the screenshot.