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.JobInfo; 19 import android.app.job.JobScheduler; 20 import android.app.job.JobWorkItem; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.os.Build; 25 import android.support.annotation.WorkerThread; 26 import android.support.v4.app.JobIntentService; 27 import com.android.dialer.common.LogUtil; 28 import com.android.dialer.constants.ScheduledJobIds; 29 import com.android.voicemail.impl.transcribe.grpc.TranscriptionClientFactory; 30 import com.google.internal.communications.voicemailtranscription.v1.SendTranscriptionFeedbackRequest; 31 import com.google.protobuf.InvalidProtocolBufferException; 32 33 /** 34 * JobScheduler service for uploading transcription feedback. This service requires a network 35 * connection. 36 */ 37 public class TranscriptionRatingService extends JobIntentService { 38 private static final String FEEDBACK_REQUEST_EXTRA = "feedback_request_extra"; 39 40 /** Schedule a task to upload transcription rating feedback */ scheduleTask(Context context, SendTranscriptionFeedbackRequest request)41 public static boolean scheduleTask(Context context, SendTranscriptionFeedbackRequest request) { 42 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 43 LogUtil.enterBlock("TranscriptionRatingService.scheduleTask"); 44 ComponentName componentName = new ComponentName(context, TranscriptionRatingService.class); 45 JobInfo.Builder builder = 46 new JobInfo.Builder(ScheduledJobIds.VVM_TRANSCRIPTION_RATING_JOB, componentName) 47 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); 48 JobScheduler scheduler = context.getSystemService(JobScheduler.class); 49 return scheduler.enqueue(builder.build(), makeWorkItem(request)) 50 == JobScheduler.RESULT_SUCCESS; 51 } else { 52 LogUtil.i("TranscriptionRatingService.scheduleTask", "not supported"); 53 return false; 54 } 55 } 56 TranscriptionRatingService()57 public TranscriptionRatingService() {} 58 makeWorkItem(SendTranscriptionFeedbackRequest request)59 private static JobWorkItem makeWorkItem(SendTranscriptionFeedbackRequest request) { 60 Intent intent = new Intent(); 61 intent.putExtra(FEEDBACK_REQUEST_EXTRA, request.toByteArray()); 62 return new JobWorkItem(intent); 63 } 64 65 @Override 66 @WorkerThread onHandleWork(Intent intent)67 protected void onHandleWork(Intent intent) { 68 LogUtil.enterBlock("TranscriptionRatingService.onHandleWork"); 69 70 TranscriptionConfigProvider configProvider = new TranscriptionConfigProvider(this); 71 TranscriptionClientFactory factory = new TranscriptionClientFactory(this, configProvider); 72 try { 73 // Send rating to server 74 SendTranscriptionFeedbackRequest request = 75 SendTranscriptionFeedbackRequest.parseFrom( 76 intent.getByteArrayExtra(FEEDBACK_REQUEST_EXTRA)); 77 factory.getClient().sendTranscriptFeedbackRequest(request); 78 } catch (InvalidProtocolBufferException e) { 79 LogUtil.e("TranscriptionRatingService.onHandleWork", "failed to send feedback", e); 80 } finally { 81 factory.shutdown(); 82 } 83 } 84 85 @Override onDestroy()86 public void onDestroy() { 87 LogUtil.enterBlock("TranscriptionRatingService.onDestroy"); 88 super.onDestroy(); 89 } 90 } 91