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
17package com.android.phone.vvm;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.provider.VoicemailContract;
23import android.telephony.SubscriptionManager;
24import 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 */
31public class VvmSmsReceiver extends BroadcastReceiver {
32
33    private static final String TAG = "VvmSmsReceiver";
34
35    @Override
36    public void onReceive(Context context, Intent intent) {
37        VisualVoicemailSms sms = intent.getExtras()
38                .getParcelable(VoicemailContract.EXTRA_VOICEMAIL_SMS);
39
40        if (sms.getPhoneAccountHandle() == null) {
41            // This should never happen
42            VvmLog.e(TAG, "Received message for null phone account");
43            return;
44        }
45
46        int subId = PhoneAccountHandleConverter.toSubId(sms.getPhoneAccountHandle());
47        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
48            VvmLog.e(TAG, "Received message for invalid subId");
49            return;
50        }
51
52        if (RemoteVvmTaskManager.hasRemoteService(context, subId)) {
53            VvmLog.i(TAG, "Sending SMS received event to remote service");
54            RemoteVvmTaskManager.startSmsReceived(context, sms);
55        } else {
56            VvmLog.w(TAG, "Sending SMS received event to remote service");
57        };
58    }
59}
60