Merge "Do not crash if window is invalid" into rvc-dev
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
index a3faa80..7f51341 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/NotificationShadeDepthController.kt
@@ -20,6 +20,7 @@
 import android.animation.AnimatorListenerAdapter
 import android.animation.ValueAnimator
 import android.app.WallpaperManager
+import android.util.Log
 import android.view.Choreographer
 import android.view.View
 import androidx.annotation.VisibleForTesting
@@ -38,6 +39,7 @@
 import com.android.systemui.statusbar.policy.KeyguardStateController
 import java.io.FileDescriptor
 import java.io.PrintWriter
+import java.lang.IllegalArgumentException
 import javax.inject.Inject
 import javax.inject.Singleton
 import kotlin.math.max
@@ -58,6 +60,7 @@
 ) : PanelExpansionListener, Dumpable {
     companion object {
         private const val WAKE_UP_ANIMATION_ENABLED = true
+        private const val TAG = "DepthController"
     }
 
     lateinit var root: View
@@ -84,12 +87,18 @@
     /**
      * Callback that updates the window blur value and is called only once per frame.
      */
-    private val updateBlurCallback = Choreographer.FrameCallback {
+    @VisibleForTesting
+    val updateBlurCallback = Choreographer.FrameCallback {
         updateScheduled = false
 
         val blur = max(max(shadeSpring.radius, wakeAndUnlockBlurRadius), globalActionsSpring.radius)
         blurUtils.applyBlur(blurRoot?.viewRootImpl ?: root.viewRootImpl, blur)
-        wallpaperManager.setWallpaperZoomOut(root.windowToken, blurUtils.ratioOfBlurRadius(blur))
+        try {
+            wallpaperManager.setWallpaperZoomOut(root.windowToken,
+                    blurUtils.ratioOfBlurRadius(blur))
+        } catch (e: IllegalArgumentException) {
+            Log.w(TAG, "Can't set zoom. Window is gone: ${root.windowToken}", e)
+        }
         notificationShadeWindowController.setBackgroundBlurRadius(blur)
     }
 
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationShadeDepthControllerTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationShadeDepthControllerTest.kt
index f4583f9..956bfd0 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationShadeDepthControllerTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/NotificationShadeDepthControllerTest.kt
@@ -34,6 +34,7 @@
 import org.junit.Test
 import org.junit.runner.RunWith
 import org.mockito.ArgumentCaptor
+import org.mockito.ArgumentMatchers.anyInt
 import org.mockito.ArgumentMatchers.eq
 import org.mockito.Mock
 import org.mockito.Mockito.`when`
@@ -41,8 +42,10 @@
 import org.mockito.Mockito.anyFloat
 import org.mockito.Mockito.anyString
 import org.mockito.Mockito.clearInvocations
+import org.mockito.Mockito.doThrow
 import org.mockito.Mockito.verify
 import org.mockito.junit.MockitoJUnit
+import java.lang.IllegalArgumentException
 
 @RunWith(AndroidTestingRunner::class)
 @RunWithLooper
@@ -116,6 +119,21 @@
         verify(globalActionsSpring).animateTo(eq(maxBlur / 2), safeEq(root))
     }
 
+    @Test
+    fun updateBlurCallback_setsBlurAndZoom() {
+        notificationShadeDepthController.updateBlurCallback.doFrame(0)
+        verify(wallpaperManager).setWallpaperZoomOut(any(), anyFloat())
+        verify(blurUtils).applyBlur(any(), anyInt())
+    }
+
+    @Test
+    fun updateBlurCallback_invalidWindow() {
+        doThrow(IllegalArgumentException("test exception")).`when`(wallpaperManager)
+                .setWallpaperZoomOut(any(), anyFloat())
+        notificationShadeDepthController.updateBlurCallback.doFrame(0)
+        verify(wallpaperManager).setWallpaperZoomOut(any(), anyFloat())
+    }
+
     private fun <T : Any> safeEq(value: T): T {
         return eq(value) ?: value
     }