1 /*
2  * Copyright (C) 2016 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 
17 package com.android.phone.vvm;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.provider.VoicemailContract;
23 import android.telephony.SubscriptionManager;
24 import android.telephony.VisualVoicemailSms;
25 
26 /**
27  * Receives the SMS filtered by {@link com.android.internal.telephony.VisualVoicemailSmsFilter} and
28  * redirect it to the visual voicemail client. The redirection is required to let telephony service
29  * handle tasks with {@link RemoteVvmTaskManager}
30  */
31 public class VvmSmsReceiver extends BroadcastReceiver {
32 
33     private static final String TAG = "VvmSmsReceiver";
34 
35     @Override
onReceive(Context context, Intent intent)36     public void onReceive(Context context, Intent intent) {
37         VisualVoicemailSms sms = intent.getExtras()
38                 .getParcelable(VoicemailContract.EXTRA_VOICEMAIL_SMS);
39         if (sms.getPhoneAccountHandle() == null) {
40             // This should never happen
41             VvmLog.e(TAG, "Received message for null phone account");
42             return;
43         }
44 
45         int subId = PhoneAccountHandleConverter.toSubId(sms.getPhoneAccountHandle());
46         if (!SubscriptionManager.isValidSubscriptionId(subId)) {
47             VvmLog.e(TAG, "Received message for invalid subId");
48             return;
49         }
50 
51         String targetPackage = intent.getExtras().getString(VoicemailContract.EXTRA_TARGET_PACKAGE);
52         if (RemoteVvmTaskManager.hasRemoteService(context, subId, targetPackage)) {
53             VvmLog.i(TAG, "Sending SMS received event to remote service");
54             RemoteVvmTaskManager.startSmsReceived(context, sms, targetPackage);
55         } else {
56             VvmLog.w(TAG, "No remote service to handle SMS received event");
57         }
58     }
59 }
60