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