• 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.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.OmtpVvmSyncService;
39 import com.android.voicemail.impl.sync.SyncOneTask;
40 import com.android.voicemail.impl.sync.SyncTask;
41 import com.android.voicemail.impl.sync.VoicemailsQueryHelper;
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 mContext;
51 
52   @Override
onReceive(Context context, Intent intent)53   public void onReceive(Context context, Intent intent) {
54     mContext = 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     OmtpVvmCarrierConfigHelper helper = new OmtpVvmCarrierConfigHelper(mContext, phone);
74     if (!helper.isValid()) {
75       VvmLog.e(TAG, "vvm config no longer valid");
76       return;
77     }
78     if (!VisualVoicemailSettingsUtil.isEnabled(mContext, phone)) {
79       if (helper.isLegacyModeEnabled()) {
80         LegacyModeSmsHandler.handle(context, sms);
81       } else {
82         VvmLog.i(TAG, "Received vvm message for disabled vvm source.");
83       }
84       return;
85     }
86 
87     String eventType = sms.getPrefix();
88     Bundle data = sms.getFields();
89 
90     if (eventType == null || data == null) {
91       VvmLog.e(TAG, "Unparsable VVM SMS received, ignoring");
92       return;
93     }
94 
95     if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) {
96       SyncMessage message = new SyncMessage(data);
97 
98       VvmLog.v(
99           TAG, "Received SYNC sms for " + phone + " with event " + message.getSyncTriggerEvent());
100       processSync(phone, message);
101     } else if (eventType.equals(OmtpConstants.STATUS_SMS_PREFIX)) {
102       VvmLog.v(TAG, "Received Status sms for " + phone);
103       // If the STATUS SMS is initiated by ActivationTask the TaskSchedulerService will reject
104       // the follow request. Providing the data will also prevent ActivationTask from
105       // requesting another STATUS SMS. The following task will only run if the carrier
106       // spontaneous send a STATUS SMS, in that case, the VVM service should be reactivated.
107       ActivationTask.start(context, phone, data);
108     } else {
109       VvmLog.w(TAG, "Unknown prefix: " + eventType);
110       VisualVoicemailProtocol protocol = helper.getProtocol();
111       if (protocol == null) {
112         return;
113       }
114       Bundle statusData = helper.getProtocol().translateStatusSmsBundle(helper, eventType, data);
115       if (statusData != null) {
116         VvmLog.i(TAG, "Protocol recognized the SMS as STATUS, activating");
117         ActivationTask.start(context, phone, data);
118       }
119     }
120   }
121 
122   /**
123    * A sync message has two purposes: to signal a new voicemail message, and to indicate the
124    * voicemails on the server have changed remotely (usually through the TUI). Save the new message
125    * to the voicemail provider if it is the former case and perform a full sync in the latter case.
126    *
127    * @param message The sync message to extract data from.
128    */
processSync(PhoneAccountHandle phone, SyncMessage message)129   private void processSync(PhoneAccountHandle phone, SyncMessage message) {
130     switch (message.getSyncTriggerEvent()) {
131       case OmtpConstants.NEW_MESSAGE:
132         if (!OmtpConstants.VOICE.equals(message.getContentType())) {
133           VvmLog.i(
134               TAG,
135               "Non-voice message of type '" + message.getContentType() + "' received, ignoring");
136           return;
137         }
138 
139         Builder builder =
140             Voicemail.createForInsertion(message.getTimestampMillis(), message.getSender())
141                 .setPhoneAccount(phone)
142                 .setSourceData(message.getId())
143                 .setDuration(message.getLength())
144                 .setSourcePackage(mContext.getPackageName());
145         Voicemail voicemail = builder.build();
146 
147         VoicemailsQueryHelper queryHelper = new VoicemailsQueryHelper(mContext);
148         if (queryHelper.isVoicemailUnique(voicemail)) {
149           Uri uri = VoicemailDatabaseUtil.insert(mContext, voicemail);
150           voicemail = builder.setId(ContentUris.parseId(uri)).setUri(uri).build();
151           SyncOneTask.start(mContext, phone, voicemail);
152         }
153         break;
154       case OmtpConstants.MAILBOX_UPDATE:
155         SyncTask.start(mContext, phone, OmtpVvmSyncService.SYNC_DOWNLOAD_ONLY);
156         break;
157       case OmtpConstants.GREETINGS_UPDATE:
158         // Not implemented in V1
159         break;
160       default:
161         VvmLog.e(TAG, "Unrecognized sync trigger event: " + message.getSyncTriggerEvent());
162         break;
163     }
164   }
165 }
166