• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.WorkerThread;
20 import com.android.dialer.common.Assert;
21 import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailRequest;
22 import com.google.internal.communications.voicemailtranscription.v1.TranscribeVoicemailResponse;
23 import com.google.internal.communications.voicemailtranscription.v1.VoicemailTranscriptionServiceGrpc;
24 import io.grpc.Status;
25 import io.grpc.StatusRuntimeException;
26 
27 /** Wrapper around Grpc transcription server stub */
28 public class TranscriptionClient {
29 
30   private final VoicemailTranscriptionServiceGrpc.VoicemailTranscriptionServiceBlockingStub stub;
31 
32   /** Wraps the server response and status objects, either of which may be null. */
33   public static class TranscriptionResponseWrapper {
34     public final TranscribeVoicemailResponse response;
35     public final Status status;
36 
TranscriptionResponseWrapper( @ullable TranscribeVoicemailResponse response, @Nullable Status status)37     public TranscriptionResponseWrapper(
38         @Nullable TranscribeVoicemailResponse response, @Nullable Status status) {
39       Assert.checkArgument(!(response == null && status == null));
40       this.response = response;
41       this.status = status;
42     }
43   }
44 
TranscriptionClient( VoicemailTranscriptionServiceGrpc.VoicemailTranscriptionServiceBlockingStub stub)45   TranscriptionClient(
46       VoicemailTranscriptionServiceGrpc.VoicemailTranscriptionServiceBlockingStub stub) {
47     this.stub = stub;
48   }
49 
50   @WorkerThread
transcribeVoicemail(TranscribeVoicemailRequest request)51   public TranscriptionResponseWrapper transcribeVoicemail(TranscribeVoicemailRequest request) {
52     TranscribeVoicemailResponse response = null;
53     Status status = null;
54     try {
55       response = stub.transcribeVoicemail(request);
56     } catch (StatusRuntimeException e) {
57       status = e.getStatus();
58     }
59     return new TranscriptionClient.TranscriptionResponseWrapper(response, status);
60   }
61 }
62