summaryrefslogtreecommitdiff
path: root/java/com/android/voicemail/impl/transcribe/TranscriptionRatingHelper.java
blob: b721ba5b002e7b670d5612c0b97653eb5d4e8371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */
package com.android.voicemail.impl.transcribe;

import android.content.Context;
import android.net.Uri;
import com.android.dialer.common.concurrent.DialerExecutor;
import com.android.dialer.common.concurrent.DialerExecutorComponent;
import com.android.dialer.compat.android.provider.VoicemailCompat;
import com.google.internal.communications.voicemailtranscription.v1.SendTranscriptionFeedbackRequest;
import com.google.internal.communications.voicemailtranscription.v1.TranscriptionRating;
import com.google.internal.communications.voicemailtranscription.v1.TranscriptionRatingValue;
import com.google.protobuf.ByteString;

/**
 * Send voicemail transcription rating feedback to the server and record the fact that feedback was
 * provided in the local database.
 */
public class TranscriptionRatingHelper {

  /** Callback invoked after the feedback has been recorded locally */
  public interface SuccessListener {
    void onRatingSuccess(Uri voicemailUri);
  }

  /** Callback invoked if there was an error recording the feedback */
  public interface FailureListener {
    void onRatingFailure(Throwable t);
  }

  /**
   * Method for sending a user voicemail transcription feedback rating to the server and recording
   * the fact that the voicemail was rated in the local database.
   */
  public static void sendRating(
      Context context,
      TranscriptionRatingValue ratingValue,
      Uri voicemailUri,
      SuccessListener successListener,
      FailureListener failureListener) {
    DialerExecutorComponent.get(context)
        .dialerExecutorFactory()
        .createNonUiTaskBuilder(new RatingWorker(context, ratingValue, voicemailUri))
        .onSuccess(output -> successListener.onRatingSuccess(voicemailUri))
        .onFailure(e -> failureListener.onRatingFailure(e))
        .build()
        .executeParallel(null);
  }

  /** Worker class used to record a user's quality rating of a voicemail transcription. */
  private static class RatingWorker implements DialerExecutor.Worker<Void, Void> {
    private final Context context;
    private final TranscriptionRatingValue ratingValue;
    private final Uri voicemailUri;

    private RatingWorker(Context context, TranscriptionRatingValue ratingValue, Uri voicemailUri) {
      this.context = context;
      this.ratingValue = ratingValue;
      this.voicemailUri = voicemailUri;
    }

    @Override
    public Void doInBackground(Void input) {
      // Schedule a task to upload the feedback (requires network connectivity)
      TranscriptionRatingService.scheduleTask(context, getFeedbackRequest());

      // Record the fact that the transcription has been rated
      TranscriptionDbHelper dbHelper = new TranscriptionDbHelper(context, voicemailUri);
      dbHelper.setTranscriptionState(VoicemailCompat.TRANSCRIPTION_AVAILABLE_AND_RATED);
      return null;
    }

    private SendTranscriptionFeedbackRequest getFeedbackRequest() {
      ByteString audioData = TranscriptionUtils.getAudioData(context, voicemailUri);
      String salt = voicemailUri.toString();
      String voicemailId = TranscriptionUtils.getFingerprintFor(audioData, salt);
      TranscriptionRating rating =
          TranscriptionRating.newBuilder()
              .setTranscriptionId(voicemailId)
              .setRatingValue(ratingValue)
              .build();
      return SendTranscriptionFeedbackRequest.newBuilder().addRating(rating).build();
    }
  }
}