diff options
author | mdooley <mdooley@google.com> | 2017-11-28 19:54:52 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-29 11:49:57 -0800 |
commit | 00d18e0efe89d7bbd4c69de6988ef6f9a62df782 (patch) | |
tree | 750103032e4860ab2be3f540eb5993f45e133365 /java/com/android/voicemail/impl/transcribe/TranscriptionTask.java | |
parent | 1306455ce5eb75c74790526ec3687293221a42ba (diff) |
Switching to alarms and exponential backoff while polling for transcription result
This cl switches the way asynchronous transcription tasks poll for their
results. it has been observed that sometimes the server side transcription
takes longer than expected (sometimes many minutes), so instead of blocking
a thread for all that time this cl schedules an alarm for the next time to
poll. it also uses an exponential backoff scheme to determine the poll times
and increases the maximum total polling time from 20 seconds to 20 minutes.
Bug: 66966157
Test: manual and unit tests
PiperOrigin-RevId: 177257577
Change-Id: Ib2998f03cc418d5241ccffec71ba3945c9fe4cbc
Diffstat (limited to 'java/com/android/voicemail/impl/transcribe/TranscriptionTask.java')
-rw-r--r-- | java/com/android/voicemail/impl/transcribe/TranscriptionTask.java | 99 |
1 files changed, 53 insertions, 46 deletions
diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java b/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java index 97cf89eef..d483f6fbd 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java @@ -21,7 +21,6 @@ import android.net.Uri; import android.support.annotation.MainThread; import android.support.annotation.VisibleForTesting; import android.telecom.PhoneAccountHandle; -import android.text.TextUtils; import android.util.Pair; import com.android.dialer.common.Assert; import com.android.dialer.common.concurrent.ThreadUtil; @@ -60,10 +59,10 @@ public abstract class TranscriptionTask implements Runnable { private final JobCallback callback; private final JobWorkItem workItem; private final TranscriptionClientFactory clientFactory; - private final Uri voicemailUri; + protected final Uri voicemailUri; protected final PhoneAccountHandle phoneAccountHandle; - private final TranscriptionDbHelper databaseHelper; protected final TranscriptionConfigProvider configProvider; + protected final TranscriptionDbHelper dbHelper; protected ByteString audioData; protected AudioFormat encoding; protected volatile boolean cancelled; @@ -86,7 +85,7 @@ public abstract class TranscriptionTask implements Runnable { this.voicemailUri = TranscriptionService.getVoicemailUri(workItem); this.phoneAccountHandle = TranscriptionService.getPhoneAccountHandle(workItem); this.configProvider = configProvider; - databaseHelper = new TranscriptionDbHelper(context, voicemailUri); + dbHelper = new TranscriptionDbHelper(context, voicemailUri); } @MainThread @@ -124,44 +123,7 @@ public abstract class TranscriptionTask implements Runnable { private void transcribeVoicemail() { VvmLog.i(TAG, "transcribeVoicemail"); - Pair<String, TranscriptionStatus> pair = getTranscription(); - String transcript = pair.first; - TranscriptionStatus status = pair.second; - if (!TextUtils.isEmpty(transcript)) { - updateTranscriptionAndState(transcript, VoicemailCompat.TRANSCRIPTION_AVAILABLE); - VvmLog.i(TAG, "transcribeVoicemail, got response"); - Logger.get(context).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_SUCCESS); - } else { - VvmLog.i(TAG, "transcribeVoicemail, transcription unsuccessful, " + status); - switch (status) { - case FAILED_NO_SPEECH_DETECTED: - updateTranscriptionAndState( - transcript, VoicemailCompat.TRANSCRIPTION_FAILED_NO_SPEECH_DETECTED); - Logger.get(context) - .logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_NO_SPEECH_DETECTED); - break; - case FAILED_LANGUAGE_NOT_SUPPORTED: - updateTranscriptionAndState( - transcript, VoicemailCompat.TRANSCRIPTION_FAILED_LANGUAGE_NOT_SUPPORTED); - Logger.get(context) - .logImpression( - DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_LANGUAGE_NOT_SUPPORTED); - break; - case EXPIRED: - updateTranscriptionAndState(transcript, VoicemailCompat.TRANSCRIPTION_FAILED); - Logger.get(context) - .logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_EXPIRED); - break; - default: - updateTranscriptionAndState( - transcript, - cancelled - ? VoicemailCompat.TRANSCRIPTION_NOT_STARTED - : VoicemailCompat.TRANSCRIPTION_FAILED); - Logger.get(context).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_EMPTY); - break; - } - } + recordResult(context, getTranscription(), dbHelper, cancelled); } protected TranscriptionResponse sendRequest(Request request) { @@ -213,12 +175,57 @@ public abstract class TranscriptionTask implements Runnable { } } - private void updateTranscriptionAndState(String transcript, int newState) { - databaseHelper.setTranscriptionAndState(transcript, newState); + protected void updateTranscriptionState(int newState) { + dbHelper.setTranscriptionState(newState); + } + + protected void updateTranscriptionAndState(String transcript, int newState) { + dbHelper.setTranscriptionAndState(transcript, newState); } - private void updateTranscriptionState(int newState) { - databaseHelper.setTranscriptionState(newState); + static void recordResult( + Context context, Pair<String, TranscriptionStatus> result, TranscriptionDbHelper dbHelper) { + recordResult(context, result, dbHelper, false); + } + + static void recordResult( + Context context, + Pair<String, TranscriptionStatus> result, + TranscriptionDbHelper dbHelper, + boolean cancelled) { + if (result.first != null) { + VvmLog.i(TAG, "recordResult, got transcription"); + dbHelper.setTranscriptionAndState(result.first, VoicemailCompat.TRANSCRIPTION_AVAILABLE); + Logger.get(context).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_SUCCESS); + } else if (result.second != null) { + VvmLog.i(TAG, "recordResult, failed to transcribe, reason: " + result.second); + switch (result.second) { + case FAILED_NO_SPEECH_DETECTED: + dbHelper.setTranscriptionState(VoicemailCompat.TRANSCRIPTION_FAILED_NO_SPEECH_DETECTED); + Logger.get(context) + .logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_NO_SPEECH_DETECTED); + break; + case FAILED_LANGUAGE_NOT_SUPPORTED: + dbHelper.setTranscriptionState( + VoicemailCompat.TRANSCRIPTION_FAILED_LANGUAGE_NOT_SUPPORTED); + Logger.get(context) + .logImpression( + DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_LANGUAGE_NOT_SUPPORTED); + break; + case EXPIRED: + dbHelper.setTranscriptionState(VoicemailCompat.TRANSCRIPTION_FAILED); + Logger.get(context) + .logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_EXPIRED); + break; + default: + dbHelper.setTranscriptionState( + cancelled + ? VoicemailCompat.TRANSCRIPTION_NOT_STARTED + : VoicemailCompat.TRANSCRIPTION_FAILED); + Logger.get(context).logImpression(DialerImpression.Type.VVM_TRANSCRIPTION_RESPONSE_EMPTY); + break; + } + } } private boolean readAndValidateAudioFile() { |