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