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.phone; 18 19 import com.android.internal.telephony.CallManager; 20 import com.android.internal.telephony.Phone; 21 import com.android.internal.telephony.PhoneConstants; 22 import com.android.internal.telephony.PhoneFactory; 23 import com.android.internal.telephony.sip.SipPhone; 24 import com.android.phone.sip.SipProfileDb; 25 import com.android.phone.sip.SipSharedPreferences; 26 27 import android.content.BroadcastReceiver; 28 import android.content.Context; 29 import android.content.Intent; 30 import android.net.sip.SipAudioCall; 31 import android.net.sip.SipException; 32 import android.net.sip.SipManager; 33 import android.net.sip.SipProfile; 34 import android.provider.Settings; 35 import android.provider.Settings.SettingNotFoundException; 36 import android.util.Log; 37 38 import java.util.List; 39 40 /** 41 * Broadcast receiver that handles SIP-related intents. 42 */ 43 public class SipBroadcastReceiver extends BroadcastReceiver { 44 private static final String TAG = SipBroadcastReceiver.class.getSimpleName(); 45 private SipSharedPreferences mSipSharedPreferences; 46 47 @Override 48 public void onReceive(Context context, final Intent intent) { 49 String action = intent.getAction(); 50 51 if (!PhoneUtils.isVoipSupported()) { 52 Log.v(TAG, "SIP VOIP not supported: " + action); 53 return; 54 } 55 mSipSharedPreferences = new SipSharedPreferences(context); 56 57 if (action.equals(SipManager.ACTION_SIP_INCOMING_CALL)) { 58 takeCall(intent); 59 } else if (action.equals(SipManager.ACTION_SIP_ADD_PHONE)) { 60 String localSipUri = intent.getStringExtra(SipManager.EXTRA_LOCAL_URI); 61 SipPhone phone = PhoneFactory.makeSipPhone(localSipUri); 62 if (phone != null) { 63 CallManager.getInstance().registerPhone(phone); 64 } 65 Log.d(TAG, "new phone: " + localSipUri + " #phones=" 66 + CallManager.getInstance().getAllPhones().size()); 67 } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PHONE)) { 68 String localSipUri = intent.getStringExtra(SipManager.EXTRA_LOCAL_URI); 69 removeSipPhone(localSipUri); 70 Log.d(TAG, "removed phone: " + localSipUri + " #phones=" 71 + CallManager.getInstance().getAllPhones().size()); 72 } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP)) { 73 Log.v(TAG, "start auto registration"); 74 registerAllProfiles(); 75 } else { 76 Log.v(TAG, "action not processed: " + action); 77 return; 78 } 79 } 80 81 private void removeSipPhone(String sipUri) { 82 for (Phone phone : CallManager.getInstance().getAllPhones()) { 83 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_SIP) { 84 if (((SipPhone) phone).getSipUri().equals(sipUri)) { 85 CallManager.getInstance().unregisterPhone((SipPhone)phone); 86 return; 87 } 88 } 89 } 90 Log.v(TAG, "Remove phone failed:cannot find phone with uri " + sipUri); 91 } 92 93 private void takeCall(Intent intent) { 94 Context phoneContext = PhoneGlobals.getInstance(); 95 try { 96 SipAudioCall sipAudioCall = SipManager.newInstance(phoneContext) 97 .takeAudioCall(intent, null); 98 for (Phone phone : CallManager.getInstance().getAllPhones()) { 99 if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_SIP) { 100 if (((SipPhone) phone).canTake(sipAudioCall)) return; 101 } 102 } 103 Log.v(TAG, "drop SIP call: " + intent); 104 } catch (SipException e) { 105 Log.e(TAG, "process incoming SIP call", e); 106 } 107 } 108 109 private void registerAllProfiles() { 110 final Context context = PhoneGlobals.getInstance(); 111 new Thread(new Runnable() { 112 public void run() { 113 SipManager sipManager = SipManager.newInstance(context); 114 SipProfileDb profileDb = new SipProfileDb(context); 115 List<SipProfile> sipProfileList = 116 profileDb.retrieveSipProfileList(); 117 for (SipProfile profile : sipProfileList) { 118 try { 119 if (!profile.getAutoRegistration() && 120 !profile.getUriString().equals( 121 mSipSharedPreferences.getPrimaryAccount())) { 122 continue; 123 } 124 sipManager.open(profile, 125 SipUtil.createIncomingCallPendingIntent(), 126 null); 127 } catch (SipException e) { 128 Log.e(TAG, "failed" + profile.getProfileName(), e); 129 } 130 } 131 }} 132 ).start(); 133 } 134 } 135