1 /* 2 * Copyright (C) 2016 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 17 package com.android.voicemail.impl.utils; 18 19 import android.content.ContentResolver; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.net.Uri; 23 import android.provider.VoicemailContract.Voicemails; 24 import android.telecom.PhoneAccountHandle; 25 import com.android.voicemail.impl.Voicemail; 26 import java.util.List; 27 28 public class VoicemailDatabaseUtil { 29 30 /** 31 * Inserts a new voicemail into the voicemail content provider. 32 * 33 * @param context The context of the app doing the inserting 34 * @param voicemail Data to be inserted 35 * @return {@link Uri} of the newly inserted {@link Voicemail} 36 * @hide 37 */ insert(Context context, Voicemail voicemail)38 public static Uri insert(Context context, Voicemail voicemail) { 39 ContentResolver contentResolver = context.getContentResolver(); 40 ContentValues contentValues = getContentValues(voicemail); 41 return contentResolver.insert( 42 Voicemails.buildSourceUri(context.getPackageName()), contentValues); 43 } 44 45 /** 46 * Inserts a list of voicemails into the voicemail content provider. 47 * 48 * @param context The context of the app doing the inserting 49 * @param voicemails Data to be inserted 50 * @return the number of voicemails inserted 51 * @hide 52 */ insert(Context context, List<Voicemail> voicemails)53 public static int insert(Context context, List<Voicemail> voicemails) { 54 for (Voicemail voicemail : voicemails) { 55 insert(context, voicemail); 56 } 57 return voicemails.size(); 58 } 59 60 /** Maps structured {@link Voicemail} to {@link ContentValues} in content provider. */ getContentValues(Voicemail voicemail)61 private static ContentValues getContentValues(Voicemail voicemail) { 62 ContentValues contentValues = new ContentValues(); 63 contentValues.put(Voicemails.DATE, String.valueOf(voicemail.getTimestampMillis())); 64 contentValues.put(Voicemails.NUMBER, voicemail.getNumber()); 65 contentValues.put(Voicemails.DURATION, String.valueOf(voicemail.getDuration())); 66 contentValues.put(Voicemails.SOURCE_PACKAGE, voicemail.getSourcePackage()); 67 contentValues.put(Voicemails.SOURCE_DATA, voicemail.getSourceData()); 68 contentValues.put(Voicemails.IS_READ, voicemail.isRead() ? 1 : 0); 69 contentValues.put(Voicemails.IS_OMTP_VOICEMAIL, 1); 70 71 PhoneAccountHandle phoneAccount = voicemail.getPhoneAccount(); 72 if (phoneAccount != null) { 73 contentValues.put( 74 Voicemails.PHONE_ACCOUNT_COMPONENT_NAME, 75 phoneAccount.getComponentName().flattenToString()); 76 contentValues.put(Voicemails.PHONE_ACCOUNT_ID, phoneAccount.getId()); 77 } 78 79 if (voicemail.getTranscription() != null) { 80 contentValues.put(Voicemails.TRANSCRIPTION, voicemail.getTranscription()); 81 } 82 83 return contentValues; 84 } 85 } 86