Home | History | Annotate | Download | only in utils
      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    */
     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    */
     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   /**
     61    * Delete all the voicemails whose source_package field matches this package
     62    *
     63    * @return the number of voicemails deleted
     64    */
     65   public static int deleteAll(Context context) {
     66     ContentResolver contentResolver = context.getContentResolver();
     67     return contentResolver.delete(Voicemails.buildSourceUri(context.getPackageName()), null, null);
     68   }
     69 
     70   /** Maps structured {@link Voicemail} to {@link ContentValues} in content provider. */
     71   private static ContentValues getContentValues(Voicemail voicemail) {
     72     ContentValues contentValues = new ContentValues();
     73     contentValues.put(Voicemails.DATE, String.valueOf(voicemail.getTimestampMillis()));
     74     contentValues.put(Voicemails.NUMBER, voicemail.getNumber());
     75     contentValues.put(Voicemails.DURATION, String.valueOf(voicemail.getDuration()));
     76     contentValues.put(Voicemails.SOURCE_PACKAGE, voicemail.getSourcePackage());
     77     contentValues.put(Voicemails.SOURCE_DATA, voicemail.getSourceData());
     78     contentValues.put(Voicemails.IS_READ, voicemail.isRead() ? 1 : 0);
     79     contentValues.put(Voicemails.IS_OMTP_VOICEMAIL, 1);
     80 
     81     PhoneAccountHandle phoneAccount = voicemail.getPhoneAccount();
     82     if (phoneAccount != null) {
     83       contentValues.put(
     84           Voicemails.PHONE_ACCOUNT_COMPONENT_NAME,
     85           phoneAccount.getComponentName().flattenToString());
     86       contentValues.put(Voicemails.PHONE_ACCOUNT_ID, phoneAccount.getId());
     87     }
     88 
     89     if (voicemail.getTranscription() != null) {
     90       contentValues.put(Voicemails.TRANSCRIPTION, voicemail.getTranscription());
     91     }
     92 
     93     return contentValues;
     94   }
     95 }
     96