From e244922ababf64fbac64c2ab375de54fa3a83f0a Mon Sep 17 00:00:00 2001 From: Andy Hung Date: Fri, 12 Jun 2015 22:49:15 -0700 Subject: AudioRecord.Builder should throw exception on failure. Should throw UnsupportedOperationException instead of returning an uninitialized record object. Bug: 21890643 Change-Id: I9c05a4cff9f5e1d5513c76acace09699a567008f --- media/java/android/media/AudioRecord.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'media') diff --git a/media/java/android/media/AudioRecord.java b/media/java/android/media/AudioRecord.java index 3cbc405..974b62e 100644 --- a/media/java/android/media/AudioRecord.java +++ b/media/java/android/media/AudioRecord.java @@ -527,10 +527,11 @@ public class AudioRecord } /** - * @return a new {@link AudioRecord} instance initialized with all the parameters set - * on this Builder + * @return a new {@link AudioRecord} instance successfully initialized with all + * the parameters set on this Builder. * @throws UnsupportedOperationException if the parameters set on the Builder - * were incompatible, or if they are not supported by the device. + * were incompatible, or if they are not supported by the device, + * or if the device was not available. */ public AudioRecord build() throws UnsupportedOperationException { if (mFormat == null) { @@ -564,7 +565,13 @@ public class AudioRecord mBufferSizeInBytes = mFormat.getChannelCount() * mFormat.getBytesPerSample(mFormat.getEncoding()); } - return new AudioRecord(mAttributes, mFormat, mBufferSizeInBytes, mSessionId); + final AudioRecord record = new AudioRecord( + mAttributes, mFormat, mBufferSizeInBytes, mSessionId); + if (record.getState() == STATE_UNINITIALIZED) { + // release is not necessary + throw new UnsupportedOperationException("Cannot create AudioRecord"); + } + return record; } catch (IllegalArgumentException e) { throw new UnsupportedOperationException(e.getMessage()); } -- cgit v1.1