1788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal/* 2788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * Copyright (C) 2010 The Android Open Source Project 3788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * 4788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * Licensed under the Apache License, Version 2.0 (the "License"); 5788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * you may not use this file except in compliance with the License. 6788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * You may obtain a copy of the License at 7788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * 8788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * http://www.apache.org/licenses/LICENSE-2.0 9788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * 10788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * Unless required by applicable law or agreed to in writing, software 11788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * distributed under the License is distributed on an "AS IS" BASIS, 12788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * See the License for the specific language governing permissions and 14788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal * limitations under the License. 15788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal */ 16788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 17788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalpackage com.android.services.telephony.sip; 18788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 19788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalimport android.content.BroadcastReceiver; 20788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalimport android.content.Context; 21788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalimport android.content.Intent; 22788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalimport android.net.sip.SipManager; 231344f67ea331f9a485f54c4b5e26d62a5cfad3fbSailesh Nepalimport android.os.Bundle; 246bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Makimport android.os.UserHandle; 25d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebingerimport android.telecom.PhoneAccount; 264d45d1cf58a2003378fd35912d6d73a00001bf06Tyler Gunnimport android.telecom.PhoneAccountHandle; 274d45d1cf58a2003378fd35912d6d73a00001bf06Tyler Gunnimport android.telecom.TelecomManager; 28788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepalimport android.util.Log; 29788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 30788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal/** 31286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebinger * Broadcast receiver that handles explicit incoming call intents 32788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal */ 33286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebingerpublic class SipIncomingCallReceiver extends BroadcastReceiver { 34286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebinger private static final String PREFIX = "[SipIncomingCallReceiver] "; 355ad698f06592baf77a439007e0f084df85916a54Santos Cordon private static final boolean VERBOSE = false; /* STOP SHIP if true */ 36788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 37788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal @Override 38788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal public void onReceive(Context context, final Intent intent) { 39788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal String action = intent.getAction(); 40788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 416bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak if (!isRunningInSystemUser()) { 42286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebinger if (VERBOSE) log("SipIncomingCallReceiver only run in system user, ignore " + action); 436bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak return; 446bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak } 456bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak 46788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal if (!SipUtil.isVoipSupported(context)) { 47788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal if (VERBOSE) log("SIP VOIP not supported: " + action); 48788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal return; 49788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } 50788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 51286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebinger if (action.equals(SipManager.ACTION_SIP_INCOMING_CALL)) { 52788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal takeCall(context, intent); 53788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } else { 54788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal if (VERBOSE) log("onReceive, action not processed: " + action); 55788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } 56788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } 57788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 58788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal private void takeCall(Context context, Intent intent) { 59788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal if (VERBOSE) log("takeCall, intent: " + intent); 6092046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius PhoneAccountHandle accountHandle = null; 6192046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius try { 6292046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius accountHandle = intent.getParcelableExtra(SipUtil.EXTRA_PHONE_ACCOUNT); 6392046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius } catch (ClassCastException e) { 6492046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius log("takeCall, Bad account handle detected. Bailing!"); 6592046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius return; 6692046786888fffed209db0dc7d8ab5653fc4e8f6Roshan Pius } 676556a09daab949853c384b385bc7618a6c75d9ddSantos Cordon if (accountHandle != null) { 686556a09daab949853c384b385bc7618a6c75d9ddSantos Cordon Bundle extras = new Bundle(); 696556a09daab949853c384b385bc7618a6c75d9ddSantos Cordon extras.putParcelable(SipUtil.EXTRA_INCOMING_CALL_INTENT, intent); 70d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger TelecomManager tm = TelecomManager.from(context); 71d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger PhoneAccount phoneAccount = tm.getPhoneAccount(accountHandle); 72286261208d91f50797ff34dcd7cafbf7bfc8411eBrad Ebinger if (phoneAccount != null && phoneAccount.isEnabled()) { 73d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger tm.addNewIncomingCall(accountHandle, extras); 74d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger } else { 75d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger log("takeCall, PhoneAccount is disabled. Not accepting incoming call..."); 76d3edc2292d969ecf8df96281c50e1a8f7a3fc777Brad Ebinger } 776556a09daab949853c384b385bc7618a6c75d9ddSantos Cordon } 78788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } 79788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal 806bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak private boolean isRunningInSystemUser() { 816bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak return UserHandle.myUserId() == UserHandle.USER_SYSTEM; 826bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak } 836bfe96aa1398cd5eb2cd8b454976fa456858b119Tony Mak 84788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal private static void log(String msg) { 85788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal Log.d(SipUtil.LOG_TAG, PREFIX + msg); 86788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal } 87788959e2d798da2d8a34cf89779421966d200f3dSailesh Nepal} 88