Home | History | Annotate | Download | only in fetch
      1 /*
      2  * Copyright (C) 2015 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.phone.vvm.omtp.fetch;
     17 
     18 import android.content.ContentResolver;
     19 import android.content.ContentValues;
     20 import android.content.Context;
     21 import android.net.Uri;
     22 import android.provider.VoicemailContract.Voicemails;
     23 import android.util.Log;
     24 
     25 import com.android.phone.vvm.omtp.imap.VoicemailPayload;
     26 
     27 import libcore.io.IoUtils;
     28 
     29 import java.io.IOException;
     30 import java.io.OutputStream;
     31 
     32 /**
     33  * Callback for when a voicemail payload is fetched. It copies the returned stream to the data
     34  * file corresponding to the voicemail.
     35  */
     36 public class VoicemailFetchedCallback {
     37     private static final String TAG = "VoicemailFetchedCallback";
     38 
     39     private ContentResolver mContentResolver;
     40     private Uri mUri;
     41 
     42     public VoicemailFetchedCallback(Context context, Uri uri) {
     43         mContentResolver = context.getContentResolver();
     44         mUri = uri;
     45     }
     46 
     47     /**
     48      * Saves the voicemail payload data into the voicemail provider then sets the "has_content" bit
     49      * of the voicemail to "1".
     50      *
     51      * @param voicemailPayload The object containing the content data for the voicemail
     52      */
     53     public void setVoicemailContent(VoicemailPayload voicemailPayload) {
     54         Log.d(TAG, String.format("Writing new voicemail content: %s", mUri));
     55         OutputStream outputStream = null;
     56 
     57         try {
     58             outputStream = mContentResolver.openOutputStream(mUri);
     59             byte[] inputBytes = voicemailPayload.getBytes();
     60             if (inputBytes != null) {
     61                 outputStream.write(inputBytes);
     62             }
     63         } catch (IOException e) {
     64             Log.w(TAG, String.format("File not found for %s", mUri));
     65             return;
     66         } finally {
     67             IoUtils.closeQuietly(outputStream);
     68         }
     69 
     70         // Update mime_type & has_content after we are done with file update.
     71         ContentValues values = new ContentValues();
     72         values.put(Voicemails.MIME_TYPE, voicemailPayload.getMimeType());
     73         values.put(Voicemails.HAS_CONTENT, true);
     74         int updatedCount = mContentResolver.update(mUri, values, null, null);
     75         if (updatedCount != 1) {
     76             Log.e(TAG, "Updating voicemail should have updated 1 row, was: " + updatedCount);
     77         }
     78     }
     79 }
     80