Merge "Catch exception when muxing fails to start" into udc-dev

This commit is contained in:
Beth Thibodeau
2023-05-18 14:33:29 +00:00
committed by Android (Google) Code Review
5 changed files with 31 additions and 10 deletions

View File

@@ -295,9 +295,8 @@
<string name="screenrecord_save_title">Screen recording saved</string>
<!-- Subtext for a notification shown after saving a screen recording to prompt the user to view it [CHAR_LIMIT=100] -->
<string name="screenrecord_save_text">Tap to view</string>
<!-- A toast message shown when there is an error deleting a screen recording [CHAR LIMIT=NONE] -->
<string name="screenrecord_delete_error">Error deleting screen recording</string>
<!-- A toast message shown when the screen recording cannot be started due to insufficient permissions [CHAR LIMIT=NONE] -->
<!-- A toast message shown when there is an error saving a screen recording [CHAR LIMIT=NONE] -->
<string name="screenrecord_save_error">Error saving screen recording</string>
<!-- A toast message shown when the screen recording cannot be started due to a generic error [CHAR LIMIT=NONE] -->
<string name="screenrecord_start_error">Error starting screen recording</string>

View File

@@ -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);
}
});

View File

@@ -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());

View File

@@ -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<MediaExtractor, Integer> pair: mExtractorIndexToMuxerIndex.keySet()) {
MediaExtractor extractor = pair.first;
extractor.selectTrack(pair.second);

View File

@@ -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<Runnable> 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());
}
}