• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.voicemail.impl.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.support.annotation.Nullable;
24 import android.telecom.PhoneAccountHandle;
25 import android.telecom.TelecomManager;
26 import com.android.voicemail.impl.R;
27 import com.android.voicemail.impl.VvmLog;
28 import com.android.voicemail.impl.imap.VoicemailPayload;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import org.apache.commons.io.IOUtils;
32 
33 /**
34  * Callback for when a voicemail payload is fetched. It copies the returned stream to the data file
35  * corresponding to the voicemail.
36  */
37 public class VoicemailFetchedCallback {
38   private static final String TAG = "VoicemailFetchedCallback";
39 
40   private final Context mContext;
41   private final ContentResolver mContentResolver;
42   private final Uri mUri;
43   private final PhoneAccountHandle mPhoneAccountHandle;
44 
VoicemailFetchedCallback(Context context, Uri uri, PhoneAccountHandle phoneAccountHandle)45   public VoicemailFetchedCallback(Context context, Uri uri, PhoneAccountHandle phoneAccountHandle) {
46     mContext = context;
47     mContentResolver = context.getContentResolver();
48     mUri = uri;
49     mPhoneAccountHandle = phoneAccountHandle;
50   }
51 
52   /**
53    * Saves the voicemail payload data into the voicemail provider then sets the "has_content" bit of
54    * the voicemail to "1".
55    *
56    * @param voicemailPayload The object containing the content data for the voicemail
57    */
setVoicemailContent(@ullable VoicemailPayload voicemailPayload)58   public void setVoicemailContent(@Nullable VoicemailPayload voicemailPayload) {
59     if (voicemailPayload == null) {
60       VvmLog.i(TAG, "Payload not found, message has unsupported format");
61       ContentValues values = new ContentValues();
62       values.put(
63           Voicemails.TRANSCRIPTION,
64           mContext.getString(
65               R.string.vvm_unsupported_message_format,
66               mContext
67                   .getSystemService(TelecomManager.class)
68                   .getVoiceMailNumber(mPhoneAccountHandle)));
69       updateVoicemail(values);
70       return;
71     }
72 
73     VvmLog.d(TAG, String.format("Writing new voicemail content: %s", mUri));
74     OutputStream outputStream = null;
75 
76     try {
77       outputStream = mContentResolver.openOutputStream(mUri);
78       byte[] inputBytes = voicemailPayload.getBytes();
79       if (inputBytes != null) {
80         outputStream.write(inputBytes);
81       }
82     } catch (IOException e) {
83       VvmLog.w(TAG, String.format("File not found for %s", mUri));
84       return;
85     } finally {
86       IOUtils.closeQuietly(outputStream);
87     }
88 
89     // Update mime_type & has_content after we are done with file update.
90     ContentValues values = new ContentValues();
91     values.put(Voicemails.MIME_TYPE, voicemailPayload.getMimeType());
92     values.put(Voicemails.HAS_CONTENT, true);
93     updateVoicemail(values);
94   }
95 
updateVoicemail(ContentValues values)96   private void updateVoicemail(ContentValues values) {
97     int updatedCount = mContentResolver.update(mUri, values, null, null);
98     if (updatedCount != 1) {
99       VvmLog.e(TAG, "Updating voicemail should have updated 1 row, was: " + updatedCount);
100     }
101   }
102 }
103