Home | History | Annotate | Download | only in transcribe
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License
     15  */
     16 package com.android.voicemail.impl.transcribe;
     17 
     18 import android.app.job.JobWorkItem;
     19 import android.content.Context;
     20 import android.util.Pair;
     21 import com.android.dialer.logging.DialerImpression;
     22 import com.android.voicemail.impl.VvmLog;
     23 import com.android.voicemail.impl.transcribe.TranscriptionService.JobCallback;
     24 import com.android.voicemail.impl.transcribe.grpc.TranscriptionClientFactory;
     25 import com.android.voicemail.impl.transcribe.grpc.TranscriptionResponseSync;
     26 import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailRequest;
     27 import com.google.internal.communications.voicemailtranscription.v1.TranscriptionStatus;
     28 
     29 /** Background task to get a voicemail transcription using the synchronous API */
     30 public class TranscriptionTaskSync extends TranscriptionTask {
     31   private static final String TAG = "TranscriptionTaskSync";
     32 
     33   public TranscriptionTaskSync(
     34       Context context,
     35       JobCallback callback,
     36       JobWorkItem workItem,
     37       TranscriptionClientFactory clientFactory,
     38       TranscriptionConfigProvider configProvider) {
     39     super(context, callback, workItem, clientFactory, configProvider);
     40   }
     41 
     42   @Override
     43   protected Pair<String, TranscriptionStatus> getTranscription() {
     44     VvmLog.i(TAG, "getTranscription");
     45 
     46     TranscriptionResponseSync response =
     47         (TranscriptionResponseSync)
     48             sendRequest((client) -> client.sendSyncRequest(getSyncRequest()));
     49     if (response == null) {
     50       VvmLog.i(TAG, "getTranscription, failed to transcribe voicemail.");
     51       return new Pair<>(null, TranscriptionStatus.FAILED_NO_RETRY);
     52     } else {
     53       VvmLog.i(TAG, "getTranscription, got transcription");
     54       return new Pair<>(response.getTranscript(), TranscriptionStatus.SUCCESS);
     55     }
     56   }
     57 
     58   @Override
     59   protected DialerImpression.Type getRequestSentImpression() {
     60     return DialerImpression.Type.VVM_TRANSCRIPTION_REQUEST_SENT;
     61   }
     62 
     63   private TranscribeVoicemailRequest getSyncRequest() {
     64     return TranscribeVoicemailRequest.newBuilder()
     65         .setVoicemailData(audioData)
     66         .setAudioFormat(encoding)
     67         .build();
     68   }
     69 }
     70