From 0feba780889cb1e67df64a457109fc6134111261 Mon Sep 17 00:00:00 2001 From: mdooley Date: Wed, 16 Aug 2017 13:25:50 -0700 Subject: Transcribe old voicemails This cl adds a transcription backfill service to transcribe old voicemails. This service queries the database for any voicemails without a transcription and whose transcription_state column has not been set and schedules a transcription task to update them. This service is only run once, after the user accepts the voicemail TOS and we have an un-metered network connection. Bug: 62423649 Test: manual and updated unit tests PiperOrigin-RevId: 165486104 Change-Id: Ic85c9d728937411638074fec07cf44bb83862acb --- .../impl/transcribe/TranscriptionDbHelper.java | 44 +++++++++++++++++++--- 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'java/com/android/voicemail/impl/transcribe/TranscriptionDbHelper.java') diff --git a/java/com/android/voicemail/impl/transcribe/TranscriptionDbHelper.java b/java/com/android/voicemail/impl/transcribe/TranscriptionDbHelper.java index cbc5cb8a0..9d3c2e4a7 100644 --- a/java/com/android/voicemail/impl/transcribe/TranscriptionDbHelper.java +++ b/java/com/android/voicemail/impl/transcribe/TranscriptionDbHelper.java @@ -17,29 +17,36 @@ package com.android.voicemail.impl.transcribe; import android.annotation.TargetApi; import android.content.ContentResolver; +import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Build.VERSION_CODES; import android.provider.VoicemailContract.Voicemails; +import android.support.annotation.VisibleForTesting; import android.support.annotation.WorkerThread; import android.support.v4.os.BuildCompat; import android.util.Pair; import com.android.dialer.common.Assert; import com.android.dialer.common.LogUtil; +import java.util.ArrayList; +import java.util.List; /** Helper class for reading and writing transcription data in the database */ @TargetApi(VERSION_CODES.O) public class TranscriptionDbHelper { - private static final String[] PROJECTION = + @VisibleForTesting + static final String[] PROJECTION = new String[] { - Voicemails.TRANSCRIPTION, // 0 - VoicemailCompat.TRANSCRIPTION_STATE // 1 + Voicemails._ID, // 0 + Voicemails.TRANSCRIPTION, // 1 + VoicemailCompat.TRANSCRIPTION_STATE // 2 }; - public static final int TRANSCRIPTION = 0; - public static final int TRANSCRIPTION_STATE = 1; + static final int ID = 0; + static final int TRANSCRIPTION = 1; + static final int TRANSCRIPTION_STATE = 2; private final ContentResolver contentResolver; private final Uri uri; @@ -50,10 +57,14 @@ public class TranscriptionDbHelper { this.uri = uri; } + TranscriptionDbHelper(Context context) { + this(context, Voicemails.buildSourceUri(context.getPackageName())); + } + @WorkerThread @TargetApi(VERSION_CODES.M) // used for try with resources Pair getTranscriptionAndState() { - Assert.checkArgument(BuildCompat.isAtLeastO()); + Assert.checkState(BuildCompat.isAtLeastO()); Assert.isWorkerThread(); try (Cursor cursor = contentResolver.query(uri, PROJECTION, null, null, null)) { if (cursor == null) { @@ -71,6 +82,27 @@ public class TranscriptionDbHelper { return null; } + @WorkerThread + @TargetApi(VERSION_CODES.M) // used for try with resources + List getUntranscribedVoicemails() { + Assert.checkArgument(BuildCompat.isAtLeastO()); + Assert.isWorkerThread(); + List untranscribed = new ArrayList<>(); + String whereClause = + Voicemails.TRANSCRIPTION + " is NULL AND " + VoicemailCompat.TRANSCRIPTION_STATE + "=?"; + String[] whereArgs = {String.valueOf(VoicemailCompat.TRANSCRIPTION_NOT_STARTED)}; + try (Cursor cursor = contentResolver.query(uri, PROJECTION, whereClause, whereArgs, null)) { + if (cursor == null) { + LogUtil.e("TranscriptionDbHelper.getUntranscribedVoicemails", "query failed."); + } else { + while (cursor.moveToNext()) { + untranscribed.add(ContentUris.withAppendedId(uri, cursor.getLong(ID))); + } + } + } + return untranscribed; + } + @WorkerThread void setTranscriptionState(int transcriptionState) { Assert.isWorkerThread(); -- cgit v1.2.3