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.content.Context; 19 import android.net.Uri; 20 import com.android.dialer.common.concurrent.DialerExecutor; 21 import com.android.dialer.common.concurrent.DialerExecutorComponent; 22 import com.android.dialer.compat.android.provider.VoicemailCompat; 23 import com.google.internal.communications.voicemailtranscription.v1.SendTranscriptionFeedbackRequest; 24 import com.google.internal.communications.voicemailtranscription.v1.TranscriptionRating; 25 import com.google.internal.communications.voicemailtranscription.v1.TranscriptionRatingValue; 26 import com.google.protobuf.ByteString; 27 28 /** 29 * Send voicemail transcription rating feedback to the server and record the fact that feedback was 30 * provided in the local database. 31 */ 32 public class TranscriptionRatingHelper { 33 34 /** Callback invoked after the feedback has been recorded locally */ 35 public interface SuccessListener { 36 void onRatingSuccess(Uri voicemailUri); 37 } 38 39 /** Callback invoked if there was an error recording the feedback */ 40 public interface FailureListener { 41 void onRatingFailure(Throwable t); 42 } 43 44 /** 45 * Method for sending a user voicemail transcription feedback rating to the server and recording 46 * the fact that the voicemail was rated in the local database. 47 */ 48 public static void sendRating( 49 Context context, 50 TranscriptionRatingValue ratingValue, 51 Uri voicemailUri, 52 SuccessListener successListener, 53 FailureListener failureListener) { 54 DialerExecutorComponent.get(context) 55 .dialerExecutorFactory() 56 .createNonUiTaskBuilder(new RatingWorker(context, ratingValue, voicemailUri)) 57 .onSuccess(output -> successListener.onRatingSuccess(voicemailUri)) 58 .onFailure(e -> failureListener.onRatingFailure(e)) 59 .build() 60 .executeParallel(null); 61 } 62 63 /** Worker class used to record a user's quality rating of a voicemail transcription. */ 64 private static class RatingWorker implements DialerExecutor.Worker<Void, Void> { 65 private final Context context; 66 private final TranscriptionRatingValue ratingValue; 67 private final Uri voicemailUri; 68 69 private RatingWorker(Context context, TranscriptionRatingValue ratingValue, Uri voicemailUri) { 70 this.context = context; 71 this.ratingValue = ratingValue; 72 this.voicemailUri = voicemailUri; 73 } 74 75 @Override 76 public Void doInBackground(Void input) { 77 // Schedule a task to upload the feedback (requires network connectivity) 78 TranscriptionRatingService.scheduleTask(context, getFeedbackRequest()); 79 80 // Record the fact that the transcription has been rated 81 TranscriptionDbHelper dbHelper = new TranscriptionDbHelper(context, voicemailUri); 82 dbHelper.setTranscriptionState(VoicemailCompat.TRANSCRIPTION_AVAILABLE_AND_RATED); 83 return null; 84 } 85 86 private SendTranscriptionFeedbackRequest getFeedbackRequest() { 87 ByteString audioData = TranscriptionUtils.getAudioData(context, voicemailUri); 88 String salt = voicemailUri.toString(); 89 String voicemailId = TranscriptionUtils.getFingerprintFor(audioData, salt); 90 TranscriptionRating rating = 91 TranscriptionRating.newBuilder() 92 .setTranscriptionId(voicemailId) 93 .setRatingValue(ratingValue) 94 .build(); 95 return SendTranscriptionFeedbackRequest.newBuilder().addRating(rating).build(); 96 } 97 } 98 } 99