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 */ 16package com.android.voicemail.impl.transcribe; 17 18import android.annotation.TargetApi; 19import android.content.ContentResolver; 20import android.content.ContentValues; 21import android.content.Context; 22import android.database.Cursor; 23import android.net.Uri; 24import android.os.Build.VERSION_CODES; 25import android.provider.VoicemailContract.Voicemails; 26import android.support.annotation.WorkerThread; 27import android.support.v4.os.BuildCompat; 28import android.util.Pair; 29import com.android.dialer.common.Assert; 30import com.android.dialer.common.LogUtil; 31 32/** Helper class for reading and writing transcription data in the database */ 33@TargetApi(VERSION_CODES.O) 34public class TranscriptionDbHelper { 35 private static final String[] PROJECTION = 36 new String[] { 37 Voicemails.TRANSCRIPTION, // 0 38 VoicemailCompat.TRANSCRIPTION_STATE // 1 39 }; 40 41 public static final int TRANSCRIPTION = 0; 42 public static final int TRANSCRIPTION_STATE = 1; 43 44 private final ContentResolver contentResolver; 45 private final Uri uri; 46 47 TranscriptionDbHelper(Context context, Uri uri) { 48 Assert.isNotNull(uri); 49 this.contentResolver = context.getContentResolver(); 50 this.uri = uri; 51 } 52 53 @WorkerThread 54 @TargetApi(VERSION_CODES.M) // used for try with resources 55 Pair<String, Integer> getTranscriptionAndState() { 56 Assert.checkArgument(BuildCompat.isAtLeastO()); 57 Assert.isWorkerThread(); 58 try (Cursor cursor = contentResolver.query(uri, PROJECTION, null, null, null)) { 59 if (cursor == null) { 60 LogUtil.e("TranscriptionDbHelper.getTranscriptionAndState", "query failed."); 61 return null; 62 } 63 64 if (cursor.moveToFirst()) { 65 String transcription = cursor.getString(TRANSCRIPTION); 66 int transcriptionState = cursor.getInt(TRANSCRIPTION_STATE); 67 return new Pair<>(transcription, transcriptionState); 68 } 69 } 70 LogUtil.i("TranscriptionDbHelper.getTranscriptionAndState", "query returned no results"); 71 return null; 72 } 73 74 @WorkerThread 75 void setTranscriptionState(int transcriptionState) { 76 Assert.isWorkerThread(); 77 LogUtil.i( 78 "TranscriptionDbHelper.setTranscriptionState", 79 "uri: " + uri + ", state: " + transcriptionState); 80 ContentValues values = new ContentValues(); 81 values.put(VoicemailCompat.TRANSCRIPTION_STATE, transcriptionState); 82 updateDatabase(values); 83 } 84 85 @WorkerThread 86 void setTranscriptionAndState(String transcription, int transcriptionState) { 87 Assert.isWorkerThread(); 88 LogUtil.i( 89 "TranscriptionDbHelper.setTranscriptionAndState", 90 "uri: " + uri + ", state: " + transcriptionState); 91 ContentValues values = new ContentValues(); 92 values.put(Voicemails.TRANSCRIPTION, transcription); 93 values.put(VoicemailCompat.TRANSCRIPTION_STATE, transcriptionState); 94 updateDatabase(values); 95 } 96 97 private void updateDatabase(ContentValues values) { 98 int updatedCount = contentResolver.update(uri, values, null, null); 99 if (updatedCount != 1) { 100 LogUtil.e( 101 "TranscriptionDbHelper.updateDatabase", 102 "Wrong row count, should have updated 1 row, was: " + updatedCount); 103 } 104 } 105} 106