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