Home | History | Annotate | Download | only in grpc
      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.grpc;
     17 
     18 import android.support.annotation.Nullable;
     19 import android.support.annotation.VisibleForTesting;
     20 import com.android.dialer.common.Assert;
     21 import com.google.internal.communications.voicemailtranscription.v1.GetTranscriptResponse;
     22 import com.google.internal.communications.voicemailtranscription.v1.TranscriptionStatus;
     23 import io.grpc.Status;
     24 
     25 /** Container for response and status objects for an asynchronous get-transcript request */
     26 public class GetTranscriptResponseAsync extends TranscriptionResponse {
     27   @Nullable private final GetTranscriptResponse response;
     28 
     29   @VisibleForTesting
     30   public GetTranscriptResponseAsync(GetTranscriptResponse response) {
     31     Assert.checkArgument(response != null);
     32     this.response = response;
     33   }
     34 
     35   @VisibleForTesting
     36   public GetTranscriptResponseAsync(Status status) {
     37     super(status);
     38     this.response = null;
     39   }
     40 
     41   public @Nullable String getTranscript() {
     42     if (response != null) {
     43       return response.getTranscript();
     44     }
     45     return null;
     46   }
     47 
     48   public @Nullable String getErrorDescription() {
     49     if (!hasRecoverableError() && !hasFatalError()) {
     50       return null;
     51     }
     52     if (status != null) {
     53       return "Grpc error: " + status;
     54     }
     55     if (response != null) {
     56       return "Transcription error: " + response.getStatus();
     57     }
     58     Assert.fail("Impossible state");
     59     return null;
     60   }
     61 
     62   public TranscriptionStatus getTranscriptionStatus() {
     63     if (response == null) {
     64       return TranscriptionStatus.TRANSCRIPTION_STATUS_UNSPECIFIED;
     65     } else {
     66       return response.getStatus();
     67     }
     68   }
     69 
     70   public boolean isTranscribing() {
     71     return response != null && response.getStatus() == TranscriptionStatus.PENDING;
     72   }
     73 
     74   @Override
     75   public boolean hasRecoverableError() {
     76     if (super.hasRecoverableError()) {
     77       return true;
     78     }
     79 
     80     if (response != null) {
     81       return response.getStatus() == TranscriptionStatus.EXPIRED
     82           || response.getStatus() == TranscriptionStatus.FAILED_RETRY;
     83     }
     84 
     85     return false;
     86   }
     87 
     88   @Override
     89   public boolean hasFatalError() {
     90     if (super.hasFatalError()) {
     91       return true;
     92     }
     93 
     94     if (response != null) {
     95       return response.getStatus() == TranscriptionStatus.FAILED_NO_RETRY
     96           || response.getStatus() == TranscriptionStatus.FAILED_LANGUAGE_NOT_SUPPORTED
     97           || response.getStatus() == TranscriptionStatus.FAILED_NO_SPEECH_DETECTED;
     98     }
     99 
    100     return false;
    101   }
    102 }
    103