From 6b2cbce0f2b9ee3c0f55a9851540ae58bef0d685 Mon Sep 17 00:00:00 2001 From: Beth Thibodeau Date: Tue, 16 May 2023 16:03:16 -0500 Subject: [PATCH] Catch exception when muxing fails to start Catch the IllegalStateException and show an error toast for the user if there is a muxer error when saving the recording Bug: 281803221 Test: manual Test: atest RecordingServiceTest Change-Id: I72f922de99d67334a668df3654c9b1049ef6e2bc --- packages/SystemUI/res/values/strings.xml | 5 ++--- .../screenrecord/RecordingService.java | 4 ++-- .../screenrecord/ScreenMediaRecorder.java | 5 +++-- .../screenrecord/ScreenRecordingMuxer.java | 8 +++++--- .../screenrecord/RecordingServiceTest.java | 19 +++++++++++++++++++ 5 files changed, 31 insertions(+), 10 deletions(-) diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 7dc8afe08b61a..003f9b08c11db 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -295,9 +295,8 @@ Screen recording saved Tap to view - - Error deleting screen recording - + + Error saving screen recording Error starting screen recording diff --git a/packages/SystemUI/src/com/android/systemui/screenrecord/RecordingService.java b/packages/SystemUI/src/com/android/systemui/screenrecord/RecordingService.java index 84f358c303ca6..e1ac0fd1fd169 100644 --- a/packages/SystemUI/src/com/android/systemui/screenrecord/RecordingService.java +++ b/packages/SystemUI/src/com/android/systemui/screenrecord/RecordingService.java @@ -453,9 +453,9 @@ public class RecordingService extends Service implements ScreenMediaRecorderList postGroupNotification(currentUser); mNotificationManager.notifyAsUser(null, mNotificationId, notification, currentUser); - } catch (IOException e) { + } catch (IOException | IllegalStateException e) { Log.e(TAG, "Error saving screen recording: " + e.getMessage()); - showErrorToast(R.string.screenrecord_delete_error); + showErrorToast(R.string.screenrecord_save_error); mNotificationManager.cancelAsUser(null, mNotificationId, currentUser); } }); diff --git a/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenMediaRecorder.java b/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenMediaRecorder.java index b8d96f774e028..b80a01212ca0a 100644 --- a/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenMediaRecorder.java +++ b/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenMediaRecorder.java @@ -52,8 +52,9 @@ import android.view.Surface; import android.view.WindowManager; import com.android.systemui.media.MediaProjectionCaptureTarget; -import java.io.File; + import java.io.Closeable; +import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.nio.file.Files; @@ -321,7 +322,7 @@ public class ScreenMediaRecorder extends MediaProjection.Callback { /** * Store recorded video */ - protected SavedRecording save() throws IOException { + protected SavedRecording save() throws IOException, IllegalStateException { String fileName = new SimpleDateFormat("'screen-'yyyyMMdd-HHmmss'.mp4'") .format(new Date()); diff --git a/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenRecordingMuxer.java b/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenRecordingMuxer.java index 7ffcfd46a1a48..dc3310dd3c9fc 100644 --- a/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenRecordingMuxer.java +++ b/packages/SystemUI/src/com/android/systemui/screenrecord/ScreenRecordingMuxer.java @@ -52,9 +52,8 @@ public class ScreenRecordingMuxer { /** * RUN IN THE BACKGROUND THREAD! */ - public void mux() throws IOException { - MediaMuxer muxer = null; - muxer = new MediaMuxer(mOutFile, mFormat); + public void mux() throws IOException, IllegalStateException { + MediaMuxer muxer = new MediaMuxer(mOutFile, mFormat); // Add extractors for (String file: mFiles) { MediaExtractor extractor = new MediaExtractor(); @@ -74,7 +73,10 @@ public class ScreenRecordingMuxer { } } + // This may throw IllegalStateException if no tracks were added above + // Let the error propagate up so we can notify the user. muxer.start(); + for (Pair pair: mExtractorIndexToMuxerIndex.keySet()) { MediaExtractor extractor = pair.first; extractor.selectTrack(pair.second); diff --git a/packages/SystemUI/tests/src/com/android/systemui/screenrecord/RecordingServiceTest.java b/packages/SystemUI/tests/src/com/android/systemui/screenrecord/RecordingServiceTest.java index 7c30843bb70b7..3def6ba1fc583 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/screenrecord/RecordingServiceTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/screenrecord/RecordingServiceTest.java @@ -46,6 +46,8 @@ import com.android.systemui.statusbar.phone.KeyguardDismissUtil; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.MockitoAnnotations; @@ -73,6 +75,8 @@ public class RecordingServiceTest extends SysuiTestCase { private Handler mHandler; @Mock private UserContextProvider mUserContextTracker; + @Captor + private ArgumentCaptor mRunnableCaptor; private KeyguardDismissUtil mKeyguardDismissUtil = new KeyguardDismissUtil() { public void executeWhenUnlocked(ActivityStarter.OnDismissAction action, boolean requiresShadeOpen) { @@ -209,4 +213,19 @@ public class RecordingServiceTest extends SysuiTestCase { verify(mScreenMediaRecorder).release(); } + + @Test + public void testOnErrorSaving() throws IOException { + // When the screen recording does not save properly + doThrow(new IllegalStateException("fail")).when(mScreenMediaRecorder).save(); + + Intent startIntent = RecordingService.getStopIntent(mContext); + mRecordingService.onStartCommand(startIntent, 0, 0); + verify(mExecutor).execute(mRunnableCaptor.capture()); + mRunnableCaptor.getValue().run(); + + // Then the state is set to not recording and we cancel the notification + verify(mController).updateState(false); + verify(mNotificationManager).cancelAsUser(any(), anyInt(), any()); + } }