Home | History | Annotate | Download | only in transcribe
      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.net.Uri;
     25 import android.support.annotation.WorkerThread;
     26 import android.support.v4.app.JobIntentService;
     27 import android.support.v4.os.BuildCompat;
     28 import android.telecom.PhoneAccountHandle;
     29 import com.android.dialer.common.LogUtil;
     30 import com.android.dialer.common.concurrent.ThreadUtil;
     31 import com.android.dialer.constants.ScheduledJobIds;
     32 import java.util.List;
     33 
     34 /**
     35  * JobScheduler service for transcribing old voicemails. This service does a database scan for
     36  * un-transcribed voicemails and schedules transcription tasks for them, once we have an un-metered
     37  * network connection.
     38  */
     39 public class TranscriptionBackfillService extends JobIntentService {
     40 
     41   /** Schedule a task to scan the database for untranscribed voicemails */
     42   public static boolean scheduleTask(Context context, PhoneAccountHandle account) {
     43     if (BuildCompat.isAtLeastO()) {
     44       LogUtil.enterBlock("TranscriptionBackfillService.transcribeOldVoicemails");
     45       ComponentName componentName = new ComponentName(context, TranscriptionBackfillService.class);
     46       JobInfo.Builder builder =
     47           new JobInfo.Builder(ScheduledJobIds.VVM_TRANSCRIPTION_BACKFILL_JOB, componentName)
     48               .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);
     49       JobScheduler scheduler = context.getSystemService(JobScheduler.class);
     50       return scheduler.enqueue(builder.build(), makeWorkItem(account))
     51           == JobScheduler.RESULT_SUCCESS;
     52     } else {
     53       LogUtil.i("TranscriptionBackfillService.transcribeOldVoicemails", "not supported");
     54       return false;
     55     }
     56   }
     57 
     58   private static JobWorkItem makeWorkItem(PhoneAccountHandle account) {
     59     Intent intent = new Intent();
     60     intent.putExtra(TranscriptionService.EXTRA_ACCOUNT_HANDLE, account);
     61     return new JobWorkItem(intent);
     62   }
     63 
     64   @Override
     65   @WorkerThread
     66   protected void onHandleWork(Intent intent) {
     67     LogUtil.enterBlock("TranscriptionBackfillService.onHandleWork");
     68 
     69     TranscriptionDbHelper dbHelper = new TranscriptionDbHelper(this);
     70     List<Uri> untranscribed = dbHelper.getUntranscribedVoicemails();
     71     LogUtil.i(
     72         "TranscriptionBackfillService.onHandleWork",
     73         "found " + untranscribed.size() + " untranscribed voicemails");
     74     // TODO(mdooley): Consider doing the actual transcriptions here instead of scheduling jobs.
     75     for (Uri uri : untranscribed) {
     76       ThreadUtil.postOnUiThread(
     77           () -> {
     78             TranscriptionService.scheduleNewVoicemailTranscriptionJob(this, uri, null, false);
     79           });
     80     }
     81   }
     82 
     83   @Override
     84   public void onDestroy() {
     85     LogUtil.enterBlock("TranscriptionBackfillService.onDestroy");
     86     super.onDestroy();
     87   }
     88 }
     89