• 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 (!VisualVoicemailSettingsUtil.isEnabled(mContext, phone)) {
75       if (helper.isLegacyModeEnabled()) {
76         LegacyModeSmsHandler.handle(context, sms);
77       } else {
78         VvmLog.i(TAG, "Received vvm message for disabled vvm source.");
79       }
80       return;
81     }
82 
83     String eventType = sms.getPrefix();
84     Bundle data = sms.getFields();
85 
86     if (eventType == null || data == null) {
87       VvmLog.e(TAG, "Unparsable VVM SMS received, ignoring");
88       return;
89     }
90 
91     if (eventType.equals(OmtpConstants.SYNC_SMS_PREFIX)) {
92       SyncMessage message = new SyncMessage(data);
93 
94       VvmLog.v(
95           TAG, "Received SYNC sms for " + phone + " with event " + message.getSyncTriggerEvent());
96       processSync(phone, message);
97     } else if (eventType.equals(OmtpConstants.STATUS_SMS_PREFIX)) {
98       VvmLog.v(TAG, "Received Status sms for " + phone);
99       // If the STATUS SMS is initiated by ActivationTask the TaskSchedulerService will reject
100       // the follow request. Providing the data will also prevent ActivationTask from
101       // requesting another STATUS SMS. The following task will only run if the carrier
102       // spontaneous send a STATUS SMS, in that case, the VVM service should be reactivated.
103       ActivationTask.start(context, phone, data);
104     } else {
105       VvmLog.w(TAG, "Unknown prefix: " + eventType);
106       VisualVoicemailProtocol protocol = helper.getProtocol();
107       if (protocol == null) {
108         return;
109       }
110       Bundle statusData = helper.getProtocol().translateStatusSmsBundle(helper, eventType, data);
111       if (statusData != null) {
112         VvmLog.i(TAG, "Protocol recognized the SMS as STATUS, activating");
113         ActivationTask.start(context, phone, data);
114       }
115     }
116   }
117 
118   /**
119    * A sync message has two purposes: to signal a new voicemail message, and to indicate the
120    * voicemails on the server have changed remotely (usually through the TUI). Save the new message
121    * to the voicemail provider if it is the former case and perform a full sync in the 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     switch (message.getSyncTriggerEvent()) {
127       case OmtpConstants.NEW_MESSAGE:
128         if (!OmtpConstants.VOICE.equals(message.getContentType())) {
129           VvmLog.i(
130               TAG,
131               "Non-voice message of type '" + message.getContentType() + "' received, ignoring");
132           return;
133         }
134 
135         Builder builder =
136             Voicemail.createForInsertion(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 = VoicemailDatabaseUtil.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, "Unrecognized sync trigger event: " + message.getSyncTriggerEvent());
158         break;
159     }
160   }
161 }
162