Fixes to TextToSpeechService#synthesizeToFile
- Fixes a strict mode violation, defers file validity checks to when the engine starts synthesizing audio. - Fixes some log spam when done() is called twice. bug:6215680 bug:5415258 Change-Id: I4001be848b5208422d4091b7398e94ed311c649f
This commit is contained in:
committed by
Android (Google) Code Review
parent
ed4e541a20
commit
9c3d7a888d
@@ -95,6 +95,22 @@ class FileSynthesisCallback extends AbstractSynthesisCallback {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a given file exists, and deletes it if it does.
|
||||
*/
|
||||
private boolean maybeCleanupExistingFile(File file) {
|
||||
if (file.exists()) {
|
||||
Log.v(TAG, "File " + file + " exists, deleting.");
|
||||
if (!file.delete()) {
|
||||
Log.e(TAG, "Failed to delete " + file);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getMaxBufferSize() {
|
||||
return MAX_AUDIO_BUFFER_SIZE;
|
||||
@@ -120,6 +136,11 @@ class FileSynthesisCallback extends AbstractSynthesisCallback {
|
||||
cleanUp();
|
||||
throw new IllegalArgumentException("FileSynthesisRequest.start() called twice");
|
||||
}
|
||||
|
||||
if (!maybeCleanupExistingFile(mFileName)) {
|
||||
return TextToSpeech.ERROR;
|
||||
}
|
||||
|
||||
mSampleRateInHz = sampleRateInHz;
|
||||
mAudioFormat = audioFormat;
|
||||
mChannelCount = channelCount;
|
||||
@@ -166,6 +187,12 @@ class FileSynthesisCallback extends AbstractSynthesisCallback {
|
||||
public int done() {
|
||||
if (DBG) Log.d(TAG, "FileSynthesisRequest.done()");
|
||||
synchronized (mStateLock) {
|
||||
if (mDone) {
|
||||
if (DBG) Log.d(TAG, "Duplicate call to done()");
|
||||
// This preserves existing behaviour. Earlier, if done was called twice
|
||||
// we'd return ERROR because mFile == null and we'd add to logspam.
|
||||
return TextToSpeech.ERROR;
|
||||
}
|
||||
if (mStopped) {
|
||||
if (DBG) Log.d(TAG, "Request has been aborted.");
|
||||
return TextToSpeech.ERROR;
|
||||
|
||||
@@ -549,7 +549,7 @@ public abstract class TextToSpeechService extends Service {
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
if (mText == null) {
|
||||
Log.wtf(TAG, "Got null text");
|
||||
Log.e(TAG, "null synthesis text");
|
||||
return false;
|
||||
}
|
||||
if (mText.length() >= MAX_SPEECH_ITEM_CHAR_LENGTH) {
|
||||
@@ -640,14 +640,6 @@ public abstract class TextToSpeechService extends Service {
|
||||
mFile = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
if (!super.isValid()) {
|
||||
return false;
|
||||
}
|
||||
return checkFile(mFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected AbstractSynthesisCallback createSynthesisCallback() {
|
||||
return new FileSynthesisCallback(mFile);
|
||||
@@ -664,33 +656,6 @@ public abstract class TextToSpeechService extends Service {
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks that the given file can be used for synthesis output.
|
||||
*/
|
||||
private boolean checkFile(File file) {
|
||||
try {
|
||||
if (file.exists()) {
|
||||
Log.v(TAG, "File " + file + " exists, deleting.");
|
||||
if (!file.delete()) {
|
||||
Log.e(TAG, "Failed to delete " + file);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!file.createNewFile()) {
|
||||
Log.e(TAG, "Can't create file " + file);
|
||||
return false;
|
||||
}
|
||||
if (!file.delete()) {
|
||||
Log.e(TAG, "Failed to delete " + file);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Can't use " + file + " due to exception " + e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class AudioSpeechItem extends SpeechItem {
|
||||
|
||||
Reference in New Issue
Block a user