Santos Cordon | 7d4ddf6 | 2013-07-10 11:58:08 -0700 | [diff] [blame] | 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.telephony.Rlog; |
| 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * Broadcast receiver that handles SIP-related intents. |
| 39 | */ |
| 40 | public class SipBroadcastReceiver extends BroadcastReceiver { |
| 41 | private static final String TAG = SipBroadcastReceiver.class.getSimpleName(); |
| 42 | private static final boolean DBG = true; |
| 43 | private SipSharedPreferences mSipSharedPreferences; |
| 44 | |
| 45 | @Override |
| 46 | public void onReceive(Context context, final Intent intent) { |
| 47 | String action = intent.getAction(); |
| 48 | |
| 49 | if (!PhoneUtils.isVoipSupported()) { |
| 50 | if (DBG) log("SIP VOIP not supported: " + action); |
| 51 | return; |
| 52 | } |
| 53 | mSipSharedPreferences = new SipSharedPreferences(context); |
| 54 | |
| 55 | if (action.equals(SipManager.ACTION_SIP_INCOMING_CALL)) { |
| 56 | takeCall(intent); |
| 57 | } else if (action.equals(SipManager.ACTION_SIP_ADD_PHONE)) { |
| 58 | String localSipUri = intent.getStringExtra(SipManager.EXTRA_LOCAL_URI); |
| 59 | SipPhone phone = PhoneFactory.makeSipPhone(localSipUri); |
| 60 | if (phone != null) { |
| 61 | CallManager.getInstance().registerPhone(phone); |
| 62 | } |
| 63 | if (DBG) log("onReceive: add phone" + localSipUri + " #phones=" |
| 64 | + CallManager.getInstance().getAllPhones().size()); |
| 65 | } else if (action.equals(SipManager.ACTION_SIP_REMOVE_PHONE)) { |
| 66 | String localSipUri = intent.getStringExtra(SipManager.EXTRA_LOCAL_URI); |
| 67 | removeSipPhone(localSipUri); |
| 68 | if (DBG) log("onReceive: remove phone: " + localSipUri + " #phones=" |
| 69 | + CallManager.getInstance().getAllPhones().size()); |
| 70 | } else if (action.equals(SipManager.ACTION_SIP_SERVICE_UP)) { |
| 71 | if (DBG) log("onReceive: start auto registration"); |
| 72 | registerAllProfiles(); |
| 73 | } else { |
| 74 | if (DBG) log("onReceive: action not processed: " + action); |
| 75 | return; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private void removeSipPhone(String sipUri) { |
| 80 | for (Phone phone : CallManager.getInstance().getAllPhones()) { |
| 81 | if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_SIP) { |
| 82 | if (((SipPhone) phone).getSipUri().equals(sipUri)) { |
| 83 | CallManager.getInstance().unregisterPhone(phone); |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | if (DBG) log("RemoveSipPhone: failed:cannot find phone with uri " + sipUri); |
| 89 | } |
| 90 | |
| 91 | private void takeCall(Intent intent) { |
| 92 | Context phoneContext = PhoneGlobals.getInstance(); |
| 93 | try { |
| 94 | SipAudioCall sipAudioCall = SipManager.newInstance(phoneContext) |
| 95 | .takeAudioCall(intent, null); |
| 96 | for (Phone phone : CallManager.getInstance().getAllPhones()) { |
| 97 | if (phone.getPhoneType() == PhoneConstants.PHONE_TYPE_SIP) { |
| 98 | if (((SipPhone) phone).canTake(sipAudioCall)) { |
| 99 | if (DBG) log("takeCall: SIP call: " + intent); |
| 100 | return; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | if (DBG) log("takeCall: not taken, drop SIP call: " + intent); |
| 105 | } catch (SipException e) { |
| 106 | loge("takeCall: error incoming SIP call", e); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | private void registerAllProfiles() { |
| 111 | final Context context = PhoneGlobals.getInstance(); |
| 112 | new Thread(new Runnable() { |
| 113 | @Override |
| 114 | public void run() { |
| 115 | SipManager sipManager = SipManager.newInstance(context); |
| 116 | SipProfileDb profileDb = new SipProfileDb(context); |
| 117 | List<SipProfile> sipProfileList = |
| 118 | profileDb.retrieveSipProfileList(); |
| 119 | for (SipProfile profile : sipProfileList) { |
| 120 | try { |
| 121 | if (!profile.getAutoRegistration() && |
| 122 | !profile.getUriString().equals( |
| 123 | mSipSharedPreferences.getPrimaryAccount())) { |
| 124 | continue; |
| 125 | } |
| 126 | sipManager.open(profile, |
| 127 | SipUtil.createIncomingCallPendingIntent(), |
| 128 | null); |
| 129 | if (DBG) log("registerAllProfiles: profile=" + profile); |
| 130 | } catch (SipException e) { |
| 131 | loge("registerAllProfiles: failed" + profile.getProfileName(), e); |
| 132 | } |
| 133 | } |
| 134 | }} |
| 135 | ).start(); |
| 136 | } |
| 137 | |
| 138 | private void log(String s) { |
| 139 | Rlog.d(TAG, s); |
| 140 | } |
| 141 | |
| 142 | private void loge(String s, Throwable t) { |
| 143 | Rlog.e(TAG, s, t); |
| 144 | } |
| 145 | } |