1 /*
2  * Copyright (C) 2010 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.services.telephony.sip;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.net.sip.SipManager;
23 import android.os.Bundle;
24 import android.os.UserHandle;
25 import android.telecom.PhoneAccount;
26 import android.telecom.PhoneAccountHandle;
27 import android.telecom.TelecomManager;
28 import android.text.TextUtils;
29 import android.util.Log;
30 
31 /**
32  * Broadcast receiver that handles explicit incoming call intents
33  */
34 public class SipIncomingCallReceiver extends BroadcastReceiver {
35     private static final String PREFIX = "[SipIncomingCallReceiver] ";
36     private static final boolean VERBOSE = false; /* STOP SHIP if true */
37 
38     @Override
onReceive(Context context, final Intent intent)39     public void onReceive(Context context, final Intent intent) {
40         String action = intent.getAction();
41 
42         if (TextUtils.isEmpty(action)) {
43             log("SipIncomingCallReceiver called with no action");
44             return;
45         }
46 
47         if (!isRunningInSystemUser()) {
48             if (VERBOSE) log("SipIncomingCallReceiver only run in system user, ignore " + action);
49             return;
50         }
51 
52         if (!SipUtil.isVoipSupported(context)) {
53             if (VERBOSE) log("SIP VOIP not supported: " + action);
54             return;
55         }
56 
57         if (action.equals(SipManager.ACTION_SIP_INCOMING_CALL)) {
58             takeCall(context, intent);
59         } else {
60             if (VERBOSE) log("onReceive, action not processed: " + action);
61         }
62     }
63 
takeCall(Context context, Intent intent)64     private void takeCall(Context context, Intent intent) {
65         if (VERBOSE) log("takeCall, intent: " + intent);
66         PhoneAccountHandle accountHandle = null;
67         try {
68             accountHandle = intent.getParcelableExtra(SipUtil.EXTRA_PHONE_ACCOUNT);
69         } catch (ClassCastException e) {
70             log("takeCall, Bad account handle detected. Bailing!");
71             return;
72         }
73         if (accountHandle != null) {
74             Bundle extras = new Bundle();
75             extras.putParcelable(SipUtil.EXTRA_INCOMING_CALL_INTENT, intent);
76             TelecomManager tm = context.getSystemService(TelecomManager.class);
77             PhoneAccount phoneAccount = tm.getPhoneAccount(accountHandle);
78             if (phoneAccount != null && phoneAccount.isEnabled()) {
79                 tm.addNewIncomingCall(accountHandle, extras);
80             } else {
81                 log("takeCall, PhoneAccount is disabled. Not accepting incoming call...");
82             }
83         }
84     }
85 
isRunningInSystemUser()86     private boolean isRunningInSystemUser() {
87         return UserHandle.myUserId() == UserHandle.SYSTEM.getIdentifier();
88     }
89 
log(String msg)90     private static void log(String msg) {
91         Log.d(SipUtil.LOG_TAG, PREFIX + msg);
92     }
93 }
94