From 527b2124a1ff8ba30552c959eb83c3dae18744ae Mon Sep 17 00:00:00 2001 From: Miranda Kephart Date: Mon, 25 Jul 2022 10:06:25 -0400 Subject: Catch failure to set screenshot sound In some cases, MediaPlayer.create fails and propagates up an IllegalStateException, which causes the screenshot process to crash. Catch this error (and just don't play the screenshot sound if it fails to load). Bug: 239236526 Fix: 239236526 Test: forced the setDataSource to throw an IOException (which is silently swallowed by MediaPlayer; see bug; and then thrown as an IllegalStateException since the data source was never actually set) Change-Id: I7ae6ae4cc69b8e3eccfbb7df7558787e50db755f --- .../systemui/screenshot/ScreenshotController.java | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java index 89a15f65e98f..82de389060ae 100644 --- a/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java +++ b/packages/SystemUI/src/com/android/systemui/screenshot/ScreenshotController.java @@ -843,14 +843,20 @@ public class ScreenshotController { // The media player creation is slow and needs on the background thread. return CallbackToFutureAdapter.getFuture((completer) -> { mBgExecutor.execute(() -> { - MediaPlayer player = MediaPlayer.create(mContext, - Uri.fromFile(new File(mContext.getResources().getString( - com.android.internal.R.string.config_cameraShutterSound))), null, - new AudioAttributes.Builder() - .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) - .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) - .build(), AudioSystem.newAudioSessionId()); - completer.set(player); + try { + MediaPlayer player = MediaPlayer.create(mContext, + Uri.fromFile(new File(mContext.getResources().getString( + com.android.internal.R.string.config_cameraShutterSound))), + null, + new AudioAttributes.Builder() + .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION) + .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) + .build(), AudioSystem.newAudioSessionId()); + completer.set(player); + } catch (IllegalStateException e) { + Log.w(TAG, "Screenshot sound initialization failed", e); + completer.set(null); + } }); return "ScreenshotController#loadCameraSound"; }); -- cgit v1.2.3-59-g8ed1b