Add additional checks when clipping bounds

Returns early from clipping bounds when the content view is not
a SurfaceView, or when the surfaceView's surface control is
null.

Test: Manual
Bug: 367030479
Bug: 367030480
Change-Id: Iff4705991c8f88942378f75f67369d572c53382f
diff --git a/privacysandbox/ui/ui-client/src/main/java/androidx/privacysandbox/ui/client/view/SandboxedSdkView.kt b/privacysandbox/ui/ui-client/src/main/java/androidx/privacysandbox/ui/client/view/SandboxedSdkView.kt
index c66933f..f662792 100644
--- a/privacysandbox/ui/ui-client/src/main/java/androidx/privacysandbox/ui/client/view/SandboxedSdkView.kt
+++ b/privacysandbox/ui/ui-client/src/main/java/androidx/privacysandbox/ui/client/view/SandboxedSdkView.kt
@@ -714,7 +714,11 @@
                 checkNotNull(contentView)
                 check(isAttachedToWindow)
 
-                val surfaceView: SurfaceView = contentView as SurfaceView
+                val surfaceView: SurfaceView? = contentView as? SurfaceView
+                // TODO(b/339377737): Remove the need for this check with better listener handling.
+                if (surfaceView?.surfaceControl == null) {
+                    return
+                }
                 val attachedSurfaceControl =
                     checkNotNull(surfaceView.rootSurfaceControl) {
                         "attachedSurfaceControl should be non-null if the window is attached"