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.sms; 17 18 import android.content.BroadcastReceiver; 19 import android.content.ContentUris; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.net.Uri; 23 import android.os.Bundle; 24 import android.os.UserManager; 25 import android.provider.VoicemailContract; 26 import android.telecom.PhoneAccountHandle; 27 import android.telecom.Voicemail; 28 import com.android.phone.settings.VisualVoicemailSettingsUtil; 29 import com.android.phone.vvm.omtp.ActivationTask; 30 import com.android.phone.vvm.omtp.OmtpConstants; 31 import com.android.phone.vvm.omtp.OmtpVvmCarrierConfigHelper; 32 import com.android.phone.vvm.omtp.VvmLog; 33 import com.android.phone.vvm.omtp.protocol.VisualVoicemailProtocol; 34 import com.android.phone.vvm.omtp.sync.OmtpVvmSyncService; 35 import com.android.phone.vvm.omtp.sync.SyncOneTask; 36 import com.android.phone.vvm.omtp.sync.SyncTask; 37 import com.android.phone.vvm.omtp.sync.VoicemailsQueryHelper; 38 import com.android.phone.vvm.omtp.utils.PhoneAccountHandleConverter; 39 40 /** 41 * Receive SMS messages and send for processing by the OMTP visual voicemail source. 42 */ 43 public class OmtpMessageReceiver extends BroadcastReceiver { 44 45 private static final String TAG = "OmtpMessageReceiver"; 46 47 private Context mContext; 48 49 @Override onReceive(Context context, Intent intent)50 public void onReceive(Context context, Intent intent) { 51 mContext = context; 52 int subId = intent.getExtras().getInt(VoicemailContract.EXTRA_VOICEMAIL_SMS_SUBID); 53 PhoneAccountHandle phone = PhoneAccountHandleConverter.fromSubId(subId); 54 55 if (phone == null) { 56 // This should never happen 57 VvmLog.i(TAG, "Received message for null phone account on subId " + subId); 58 return; 59 } 60 61 if (!UserManager.get(context).isUserUnlocked()) { 62 VvmLog.i(TAG, "Received message on locked device"); 63 // LegacyModeSmsHandler can handle new message notifications without storage access 64 LegacyModeSmsHandler.handle(context, intent, phone); 65 // A full sync will happen after the device is unlocked, so nothing else need to be 66 // done. 67 return; 68 } 69 70 OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, subId); 71 if (!VisualVoicemailSettingsUtil.isEnabled(mContext, phone)) { 72 if (helper.isLegacyModeEnabled()) { 73 LegacyModeSmsHandler.handle(context, intent, phone); 74 } else { 75 VvmLog.i(TAG, "Received vvm message for disabled vvm source."); 76 } 77 return; 78 } 79 80 String eventType = intent.getExtras() 81 .getString(VoicemailContract.EXTRA_VOICEMAIL_SMS_PREFIX); 82 Bundle data = intent.getExtras().getBundle(VoicemailContract.EXTRA_VOICEMAIL_SMS_FIELDS); 83 84 if (eventType == null || data == null) { 85 VvmLog.e(TAG, "Unparsable VVM SMS received, ignoring"); 86 return; 87 } 88 89 if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) { 90 SyncMessage message = new SyncMessage(data); 91 92 VvmLog.v(TAG, "Received SYNC sms for " + subId + 93 " with event " + message.getSyncTriggerEvent()); 94 processSync(phone, message); 95 } else if (eventType.equals(OmtpConstants.STATUS_SMS_PREFIX)) { 96 VvmLog.v(TAG, "Received Status sms for " + subId); 97 // If the STATUS SMS is initiated by ActivationTask the TaskSchedulerService will reject 98 // the follow request. Providing the data will also prevent ActivationTask from 99 // requesting another STATUS SMS. The following task will only run if the carrier 100 // spontaneous send a STATUS SMS, in that case, the VVM service should be reactivated. 101 ActivationTask.start(context, subId, data); 102 } else { 103 VvmLog.w(TAG, "Unknown prefix: " + eventType); 104 VisualVoicemailProtocol protocol = helper.getProtocol(); 105 if (protocol == null) { 106 return; 107 } 108 Bundle statusData = helper.getProtocol() 109 .translateStatusSmsBundle(helper, eventType, data); 110 if (statusData != null) { 111 VvmLog.i(TAG, "Protocol recognized the SMS as STATUS, activating"); 112 ActivationTask.start(context, subId, data); 113 } 114 } 115 } 116 117 /** 118 * A sync message has two purposes: to signal a new voicemail message, and to indicate the 119 * voicemails on the server have changed remotely (usually through the TUI). Save the new 120 * message to the voicemail provider if it is the former case and perform a full sync in the 121 * latter case. 122 * 123 * @param message The sync message to extract data from. 124 */ processSync(PhoneAccountHandle phone, SyncMessage message)125 private void processSync(PhoneAccountHandle phone, SyncMessage message) { 126 Intent serviceIntent = null; 127 switch (message.getSyncTriggerEvent()) { 128 case OmtpConstants.NEW_MESSAGE: 129 if (!OmtpConstants.VOICE.equals(message.getContentType())) { 130 VvmLog.i(TAG, "Non-voice message of type '" + message.getContentType() 131 + "' received, ignoring"); 132 return; 133 } 134 135 Voicemail.Builder builder = Voicemail.createForInsertion( 136 message.getTimestampMillis(), message.getSender()) 137 .setPhoneAccount(phone) 138 .setSourceData(message.getId()) 139 .setDuration(message.getLength()) 140 .setSourcePackage(mContext.getPackageName()); 141 Voicemail voicemail = builder.build(); 142 143 VoicemailsQueryHelper queryHelper = new VoicemailsQueryHelper(mContext); 144 if (queryHelper.isVoicemailUnique(voicemail)) { 145 Uri uri = VoicemailContract.Voicemails.insert(mContext, voicemail); 146 voicemail = builder.setId(ContentUris.parseId(uri)).setUri(uri).build(); 147 SyncOneTask.start(mContext, phone, voicemail); 148 } 149 break; 150 case OmtpConstants.MAILBOX_UPDATE: 151 SyncTask.start(mContext, phone, OmtpVvmSyncService.SYNC_DOWNLOAD_ONLY); 152 break; 153 case OmtpConstants.GREETINGS_UPDATE: 154 // Not implemented in V1 155 break; 156 default: 157 VvmLog.e(TAG, 158 "Unrecognized sync trigger event: " + message.getSyncTriggerEvent()); 159 break; 160 } 161 } 162 } 163