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