diff options
author | mdooley <mdooley@google.com> | 2017-10-27 16:20:32 -0700 |
---|---|---|
committer | Eric Erfanian <erfanian@google.com> | 2017-10-30 08:28:27 -0700 |
commit | ec1a970dec22d68773f8ae9dbde6f17df6154e03 (patch) | |
tree | 9c668c93b4008bb9c4bb2cf8bf4da1e68898e8f2 /java/com/android/voicemail/impl/transcribe | |
parent | e39af763e7bf598306feab4a3d2e1164c9f0421a (diff) |
internal change
Bug: 62423454
Test: manual and updated unit test
PiperOrigin-RevId: 173731907
Change-Id: Ic73600197b1c4fa6ac0937a8c38b048cd8faded8
Diffstat (limited to 'java/com/android/voicemail/impl/transcribe')
5 files changed, 54 insertions, 10 deletions
diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionBackfillService.java b/java/com/android/voicemail/impl/transcribe/TranscriptionBackfillService.java index f3c6e64f4..f7f643932 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionBackfillService.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionBackfillService.java @@ -72,7 +72,7 @@ public class TranscriptionBackfillService extends JobIntentService { for (Uri uri : untranscribed) { ThreadUtil.postOnUiThread( () -> { - TranscriptionService.scheduleNewVoicemailTranscriptionJob(this, uri, false); + TranscriptionService.scheduleNewVoicemailTranscriptionJob(this, uri, null, false); }); } } diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionConfigProvider.java b/java/com/android/voicemail/impl/transcribe/TranscriptionConfigProvider.java index 4bb9a261b..5da450312 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionConfigProvider.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionConfigProvider.java @@ -75,6 +75,11 @@ public class TranscriptionConfigProvider { .getLong("voicemail_transcription_get_transcript_poll_interval_millis", 1000L); } + public boolean isVoicemailDonationEnabled() { + return ConfigProviderBindings.get(context) + .getBoolean("voicemail_transcription_donation_enabled", false); + } + @Override public String toString() { return String.format( diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionService.java b/java/com/android/voicemail/impl/transcribe/TranscriptionService.java index b733928d7..79e1a017c 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionService.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionService.java @@ -25,8 +25,10 @@ import android.content.Context; import android.content.Intent; import android.net.Uri; import android.support.annotation.MainThread; +import android.support.annotation.Nullable; import android.support.annotation.VisibleForTesting; import android.support.v4.os.BuildCompat; +import android.telecom.PhoneAccountHandle; import android.text.TextUtils; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; @@ -44,6 +46,7 @@ import java.util.concurrent.Executors; */ public class TranscriptionService extends JobService { @VisibleForTesting static final String EXTRA_VOICEMAIL_URI = "extra_voicemail_uri"; + @VisibleForTesting static final String EXTRA_ACCOUNT_HANDLE = "extra_account_handle"; private ExecutorService executorService; private JobParameters jobParameters; @@ -58,10 +61,14 @@ public class TranscriptionService extends JobService { } // Schedule a task to transcribe the indicated voicemail, return true if transcription task was - // scheduled. + // scheduled. If the PhoneAccountHandle is null then the voicemail will not be considered for + // donation. @MainThread public static boolean scheduleNewVoicemailTranscriptionJob( - Context context, Uri voicemailUri, boolean highPriority) { + Context context, + Uri voicemailUri, + @Nullable PhoneAccountHandle account, + boolean highPriority) { Assert.isMainThread(); if (BuildCompat.isAtLeastO()) { LogUtil.i( @@ -80,7 +87,7 @@ public class TranscriptionService extends JobService { builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED); } JobScheduler scheduler = context.getSystemService(JobScheduler.class); - JobWorkItem workItem = makeWorkItem(voicemailUri); + JobWorkItem workItem = makeWorkItem(voicemailUri, account); return scheduler.enqueue(builder.build(), workItem) == JobScheduler.RESULT_SUCCESS; } else { LogUtil.i("TranscriptionService.scheduleNewVoicemailTranscriptionJob", "not supported"); @@ -193,6 +200,10 @@ public class TranscriptionService extends JobService { return workItem.getIntent().getParcelableExtra(EXTRA_VOICEMAIL_URI); } + static PhoneAccountHandle getPhoneAccountHandle(JobWorkItem workItem) { + return workItem.getIntent().getParcelableExtra(EXTRA_ACCOUNT_HANDLE); + } + private ExecutorService getExecutorService() { if (executorService == null) { // The common use case is transcribing a single voicemail so just use a single thread executor @@ -219,9 +230,12 @@ public class TranscriptionService extends JobService { } } - private static JobWorkItem makeWorkItem(Uri voicemailUri) { + private static JobWorkItem makeWorkItem(Uri voicemailUri, PhoneAccountHandle account) { Intent intent = new Intent(); intent.putExtra(EXTRA_VOICEMAIL_URI, voicemailUri); + if (account != null) { + intent.putExtra(EXTRA_ACCOUNT_HANDLE, account); + } return new JobWorkItem(intent); } diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java b/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java index a93c65151..f3b1d587f 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionTask.java @@ -20,6 +20,8 @@ import android.app.job.JobWorkItem; import android.content.Context; 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; @@ -62,6 +64,7 @@ public abstract class TranscriptionTask implements Runnable { private final JobWorkItem workItem; private final TranscriptionClientFactory clientFactory; private final Uri voicemailUri; + protected final PhoneAccountHandle phoneAccountHandle; private final TranscriptionDbHelper databaseHelper; protected final TranscriptionConfigProvider configProvider; protected ByteString audioData; @@ -86,6 +89,7 @@ public abstract class TranscriptionTask implements Runnable { this.workItem = workItem; this.clientFactory = clientFactory; this.voicemailUri = TranscriptionService.getVoicemailUri(workItem); + this.phoneAccountHandle = TranscriptionService.getPhoneAccountHandle(workItem); this.configProvider = configProvider; databaseHelper = new TranscriptionDbHelper(context, voicemailUri); } @@ -240,14 +244,24 @@ public abstract class TranscriptionTask implements Runnable { return false; } - if (audioData.startsWith(ByteString.copyFromUtf8(AMR_PREFIX))) { - encoding = AudioFormat.AMR_NB_8KHZ; - } else { + encoding = getAudioFormat(audioData); + if (encoding == AudioFormat.AUDIO_FORMAT_UNSPECIFIED) { VvmLog.i(TAG, "Transcriber.readAndValidateAudioFile, unknown encoding"); - encoding = AudioFormat.AUDIO_FORMAT_UNSPECIFIED; return false; } return true; } + + private static AudioFormat getAudioFormat(ByteString audioData) { + return audioData != null && audioData.startsWith(ByteString.copyFromUtf8(AMR_PREFIX)) + ? AudioFormat.AMR_NB_8KHZ + : AudioFormat.AUDIO_FORMAT_UNSPECIFIED; + } + + @VisibleForTesting + void setAudioDataForTesting(ByteString audioData) { + this.audioData = audioData; + encoding = getAudioFormat(audioData); + } } diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionTaskAsync.java b/java/com/android/voicemail/impl/transcribe/TranscriptionTaskAsync.java index e75728014..f946607b5 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionTaskAsync.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionTaskAsync.java @@ -21,11 +21,13 @@ import android.util.Pair; import com.android.dialer.common.Assert; import com.android.dialer.logging.DialerImpression; import com.android.dialer.logging.Logger; +import com.android.voicemail.VoicemailComponent; import com.android.voicemail.impl.VvmLog; import com.android.voicemail.impl.transcribe.TranscriptionService.JobCallback; import com.android.voicemail.impl.transcribe.grpc.GetTranscriptResponseAsync; import com.android.voicemail.impl.transcribe.grpc.TranscriptionClientFactory; import com.android.voicemail.impl.transcribe.grpc.TranscriptionResponseAsync; +import com.google.internal.communications.voicemailtranscription.v1.DonationPreference; import com.google.internal.communications.voicemailtranscription.v1.GetTranscriptRequest; import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailAsyncRequest; import com.google.internal.communications.voicemailtranscription.v1.TranscriptionStatus; @@ -119,13 +121,22 @@ public class TranscriptionTaskAsync extends TranscriptionTask { return new Pair<>(null, TranscriptionStatus.FAILED_NO_RETRY); } - private TranscribeVoicemailAsyncRequest getUploadRequest() { + TranscribeVoicemailAsyncRequest getUploadRequest() { return TranscribeVoicemailAsyncRequest.newBuilder() .setVoicemailData(audioData) .setAudioFormat(encoding) + .setDonationPreference( + isDonationEnabled() ? DonationPreference.DONATE : DonationPreference.DO_NOT_DONATE) .build(); } + private boolean isDonationEnabled() { + return phoneAccountHandle != null + && VoicemailComponent.get(context) + .getVoicemailClient() + .isVoicemailDonationEnabled(context, phoneAccountHandle); + } + private GetTranscriptRequest getGetTranscriptRequest(TranscriptionResponseAsync uploadResponse) { Assert.checkArgument(uploadResponse.getTranscriptionId() != null); return GetTranscriptRequest.newBuilder() |