SipManager.java revision 38ee75e8c0722118d26a23afe5b509b6a09302f4
198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang/*
298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Copyright (C) 2010 The Android Open Source Project
398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang *
498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Licensed under the Apache License, Version 2.0 (the "License");
598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * you may not use this file except in compliance with the License.
698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * You may obtain a copy of the License at
798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang *
898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang *      http://www.apache.org/licenses/LICENSE-2.0
998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang *
1098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Unless required by applicable law or agreed to in writing, software
1198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * distributed under the License is distributed on an "AS IS" BASIS,
1298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * See the License for the specific language governing permissions and
1498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * limitations under the License.
1598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */
1698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
1798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangpackage android.net.sip;
1898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
19845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyanimport android.app.PendingIntent;
2098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.content.Context;
2198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.content.Intent;
2269d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyanimport android.content.pm.PackageManager;
2398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.os.IBinder;
2498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.os.Looper;
2598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.os.RemoteException;
2698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.os.ServiceManager;
27e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyanimport android.util.Log;
2898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
2998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport java.text.ParseException;
3098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
3198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang/**
3216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * Provides APIs for SIP tasks, such as initiating SIP connections, and provides access to related
3316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * SIP services. This class is the starting point for any SIP actions. You can acquire an instance
3416b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * of it with {@link #newInstance newInstance()}.</p>
3516b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <p>The APIs in this class allows you to:</p>
3698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * <ul>
3716b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Create a {@link SipSession} to get ready for making calls or listen for incoming calls. See
3816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link #createSipSession createSipSession()} and {@link #getSessionFor getSessionFor()}.</li>
3916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Initiate and receive generic SIP calls or audio-only SIP calls. Generic SIP calls may
4016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * be video, audio, or other, and are initiated with {@link #open open()}. Audio-only SIP calls
4116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * should be handled with a {@link SipAudioCall}, which you can acquire with {@link
4216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * #makeAudioCall makeAudioCall()} and {@link #takeAudioCall takeAudioCall()}.</li>
4316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Register and unregister with a SIP service provider, with
4416b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main *      {@link #register register()} and {@link #unregister unregister()}.</li>
4516b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Verify session connectivity, with {@link #isOpened isOpened()} and
4616b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main *      {@link #isRegistered isRegistered()}.</li>
4798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * </ul>
4816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <p class="note"><strong>Note:</strong> Not all Android-powered devices support VOIP calls using
4916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * SIP. You should always call {@link android.net.sip.SipManager#isVoipSupported
5016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * isVoipSupported()} to verify that the device supports VOIP calling and {@link
5116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * android.net.sip.SipManager#isApiSupported isApiSupported()} to verify that the device supports
5216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * the SIP APIs.<br/><br/>Your application must also request the {@link
5316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * android.Manifest.permission#INTERNET} and {@link android.Manifest.permission#USE_SIP}
5416b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * permissions.</p>
5598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */
5698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangpublic class SipManager {
573adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
58845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * The result code to be sent back with the incoming call
59845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link PendingIntent}.
60845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #open(SipProfile, PendingIntent, SipRegistrationListener)
61845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     */
62845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final int INCOMING_CALL_RESULT_CODE = 101;
63845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
644e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan    /**
654e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * Key to retrieve the call ID from an incoming call intent.
664e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @see #open(SipProfile, PendingIntent, SipRegistrationListener)
674e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     */
68845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final String EXTRA_CALL_ID = "android:sipCallID";
69845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
704e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan    /**
714e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * Key to retrieve the offered session description from an incoming call
724e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * intent.
734e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @see #open(SipProfile, PendingIntent, SipRegistrationListener)
744e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     */
75845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final String EXTRA_OFFER_SD = "android:sipOfferSD";
76845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
77845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    /**
7822523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * Action to broadcast when SipService is up.
7922523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * Internal use only.
8022523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * @hide
8122523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     */
8222523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan    public static final String ACTION_SIP_SERVICE_UP =
8322523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan            "android.net.sip.SIP_SERVICE_UP";
8422523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan    /**
853adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the incoming call intent for the Phone app.
863adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
873adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
883adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
893adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_INCOMING_CALL =
9098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_INCOMING_CALL";
913adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
923adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the add-phone intent.
933adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
943adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
953adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
963adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_ADD_PHONE =
9798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_ADD_PHONE";
983adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the remove-phone intent.
1003adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
1013adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
1023adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
1033adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_REMOVE_PHONE =
10498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_REMOVE_PHONE";
1053adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
1063adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Part of the ACTION_SIP_ADD_PHONE and ACTION_SIP_REMOVE_PHONE intents.
1073adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
1083adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
1093adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
1103adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String EXTRA_LOCAL_URI = "android:localSipUri";
11198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
112e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan    private static final String TAG = "SipManager";
113e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan
11498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private ISipService mSipService;
1153adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    private Context mContext;
11698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
11798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
1183adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Creates a manager instance. Returns null if SIP API is not supported.
11998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
1203adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @param context application context for creating the manager object
12169d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     * @return the manager instance or null if SIP API is not supported
12298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
1233adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static SipManager newInstance(Context context) {
1243adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return (isApiSupported(context) ? new SipManager(context) : null);
12569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    }
12669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan
12769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    /**
12869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     * Returns true if the SIP API is supported by the system.
12969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     */
13069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    public static boolean isApiSupported(Context context) {
13169d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        return context.getPackageManager().hasSystemFeature(
13269d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan                PackageManager.FEATURE_SIP);
13369d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    }
13469d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan
13569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    /**
13638ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     * Returns true if the system supports SIP-based VOIP API.
13769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     */
13869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    public static boolean isVoipSupported(Context context) {
13969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        return context.getPackageManager().hasSystemFeature(
14069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan                PackageManager.FEATURE_SIP_VOIP) && isApiSupported(context);
14198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
14298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
143d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    /**
144d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan     * Returns true if SIP is only available on WIFI.
145d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan     */
146d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    public static boolean isSipWifiOnly(Context context) {
147d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan        return context.getResources().getBoolean(
148d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan                com.android.internal.R.bool.config_sip_wifi_only);
149d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    }
150d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan
1513adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    private SipManager(Context context) {
1523adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        mContext = context;
15369d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        createSipService();
15498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
15598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
15669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    private void createSipService() {
15768144a84e3cd43ba4f62c73dbd2ce9c74d50e1a6Chung-yih Wang        IBinder b = ServiceManager.getService(Context.SIP_SERVICE);
15898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        mSipService = ISipService.Stub.asInterface(b);
15998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
16098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
16198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
16216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * Opens the profile for making generic SIP calls. The caller may make subsequent calls
1633adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * through {@link #makeAudioCall}. If one also wants to receive calls on the
164845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * profile, use
165845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)}
1663adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * instead.
16798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
16898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to make calls from
16998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if the profile contains incorrect settings or
17098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      calling the SIP service results in an error
17198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
17298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void open(SipProfile localProfile) throws SipException {
17398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
17498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.open(localProfile);
17598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
17698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("open()", e);
17798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
17898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
17998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
18098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
18116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * Opens the profile for making calls and/or receiving generic SIP calls. The caller may
1823adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * make subsequent calls through {@link #makeAudioCall}. If the
1833adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * auto-registration option is enabled in the profile, the SIP service
1843adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * will register the profile to the corresponding SIP provider periodically
1854e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * in order to receive calls from the provider. When the SIP service
1864e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * receives a new call, it will send out an intent with the provided action
1874e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * string. The intent contains a call ID extra and an offer session
1884e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * description string extra. Use {@link #getCallId} and
1894e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * {@link #getOfferSessionDescription} to retrieve those extras.
19098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
19198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to receive incoming calls for
192845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @param incomingCallPendingIntent When an incoming call is received, the
193845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      SIP service will call
194845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      {@link PendingIntent#send(Context, int, Intent)} to send back the
195845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      intent to the caller with {@link #INCOMING_CALL_RESULT_CODE} as the
196845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      result code and the intent to fill in the call ID and session
197845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      description information. It cannot be null.
19898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to registration events; can be null
199845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #getCallId
200845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #getOfferSessionDescription
201845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #takeAudioCall
202845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @throws NullPointerException if {@code incomingCallPendingIntent} is null
20398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if the profile contains incorrect settings or
20498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      calling the SIP service results in an error
2054e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @see #isIncomingCallIntent
2064e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @see #getCallId
2074e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @see #getOfferSessionDescription
20898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
20998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void open(SipProfile localProfile,
210845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            PendingIntent incomingCallPendingIntent,
21198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
212845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan        if (incomingCallPendingIntent == null) {
213845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            throw new NullPointerException(
214845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan                    "incomingCallPendingIntent cannot be null");
215845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan        }
21698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
217845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            mSipService.open3(localProfile, incomingCallPendingIntent,
218f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
21998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
22098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("open()", e);
22198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
22298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
22398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
22498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
22598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Sets the listener to listen to registration events. No effect if the
226845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * profile has not been opened to receive calls (see
227845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)}).
22898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
22998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile
23098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to registration events; can be null
23198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
23298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
23398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void setRegistrationListener(String localProfileUri,
23498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
23598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
23698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.setRegistrationListener(
237f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    localProfileUri, createRelay(listener, localProfileUri));
23898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
23998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("setRegistrationListener()", e);
24098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
24198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
24298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
24398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
24498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Closes the specified profile to not make/receive calls. All the resources
24598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * that were allocated to the profile are also released.
24698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
24798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile to close
24898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
24998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
25098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void close(String localProfileUri) throws SipException {
25198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
25298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.close(localProfileUri);
25398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
25498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("close()", e);
25598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
25698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
25798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
25898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
2593adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Checks if the specified profile is opened in the SIP service for
2603adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * making and/or receiving calls.
26198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
26298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile in question
26398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return true if the profile is enabled to receive calls
26498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
26598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
26698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public boolean isOpened(String localProfileUri) throws SipException {
26798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
26898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.isOpened(localProfileUri);
26998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
27098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("isOpened()", e);
27198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
27298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
27398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
27498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
2753adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Checks if the SIP service has successfully registered the profile to the
2763adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * SIP provider (specified in the profile) for receiving calls. Returning
2773adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * true from this method also implies the profile is opened
2783adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * ({@link #isOpened}).
27998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
28098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile in question
2813adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @return true if the profile is registered to the SIP provider; false if
2823adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        the profile has not been opened in the SIP service or the SIP
2833adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        service has not yet successfully registered the profile to the SIP
2843adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        provider
28598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
28698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
28798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public boolean isRegistered(String localProfileUri) throws SipException {
28898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
28998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.isRegistered(localProfileUri);
29098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
29198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("isRegistered()", e);
29298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
29398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
29498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
29598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
296f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * Creates a {@link SipAudioCall} to make a call. The attempt will be timed
297f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * out if the call is not established within {@code timeout} seconds and
29816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * {@link SipAudioCall.Listener#onError onError(SipAudioCall, SipErrorCode.TIME_OUT, String)}
299f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * will be called.
30098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
30198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to make the call from
30298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param peerProfile the SIP profile to make the call to
30398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
30498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
3054e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @param timeout the timeout value in seconds. Default value (defined by
3064e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     *        SIP protocol) is used if {@code timeout} is zero or negative.
30798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
30838ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     * @throws SipException if calling the SIP service results in an error or
30938ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     *      VOIP API is not supported by the device
31016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * @see SipAudioCall.Listener#onError
31138ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     * @see #isVoipSupported
31298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3133adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall makeAudioCall(SipProfile localProfile,
314f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan            SipProfile peerProfile, SipAudioCall.Listener listener, int timeout)
31598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
31638ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan        if (!isVoipSupported(mContext)) {
31738ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan            throw new SipException("VOIP API is not supported");
31838ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan        }
3193adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        SipAudioCall call = new SipAudioCall(mContext, localProfile);
32098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        call.setListener(listener);
3217a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        SipSession s = createSipSession(localProfile, null);
3227a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        call.makeCall(peerProfile, s, timeout);
32398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return call;
32498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
32598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
32698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
327845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * Creates a {@link SipAudioCall} to make an audio call. The attempt will be
328845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * timed out if the call is not established within {@code timeout} seconds
329845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * and
33016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * {@link SipAudioCall.Listener#onError onError(SipAudioCall, SipErrorCode.TIME_OUT, String)}
331f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * will be called.
33298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
33398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri URI of the SIP profile to make the call from
33498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param peerProfileUri URI of the SIP profile to make the call to
33598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
33698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
3374e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * @param timeout the timeout value in seconds. Default value (defined by
3384e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     *        SIP protocol) is used if {@code timeout} is zero or negative.
33998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
34038ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     * @throws SipException if calling the SIP service results in an error or
34138ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     *      VOIP API is not supported by the device
34216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * @see SipAudioCall.Listener#onError
34338ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan     * @see #isVoipSupported
34498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3453adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall makeAudioCall(String localProfileUri,
346f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan            String peerProfileUri, SipAudioCall.Listener listener, int timeout)
34798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
34838ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan        if (!isVoipSupported(mContext)) {
34938ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan            throw new SipException("VOIP API is not supported");
35038ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan        }
35198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
3523adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            return makeAudioCall(
35398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                    new SipProfile.Builder(localProfileUri).build(),
354f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan                    new SipProfile.Builder(peerProfileUri).build(), listener,
355f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan                    timeout);
35698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (ParseException e) {
35798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("build SipProfile", e);
35898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
35998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
36098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
36198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
36298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Creates a {@link SipAudioCall} to take an incoming call. Before the call
36398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * is returned, the listener will receive a
364901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * {@link SipAudioCall.Listener#onRinging}
36598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * callback.
36698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
36798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
36898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
36998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
37098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
37198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
37298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3733adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall takeAudioCall(Intent incomingCallIntent,
3746577ac4cc93ee3670a5aceceb9a95260d41fe570Hung-ying Tyan            SipAudioCall.Listener listener) throws SipException {
375b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan        if (incomingCallIntent == null) {
376b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            throw new SipException("Cannot retrieve session with null intent");
377b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan        }
37898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
37998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        String callId = getCallId(incomingCallIntent);
38098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (callId == null) {
38198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("Call ID missing in incoming call intent");
38298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
38398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
3846d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh        String offerSd = getOfferSessionDescription(incomingCallIntent);
38598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (offerSd == null) {
38698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("Session description missing in incoming "
38798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                    + "call intent");
38898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
38998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
39098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
39198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            ISipSession session = mSipService.getPendingSession(callId);
392b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            if (session == null) {
393b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan                throw new SipException("No pending session for the call");
394b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            }
3953adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipAudioCall call = new SipAudioCall(
3963adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan                    mContext, session.getLocalProfile());
3973adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            call.attachCall(new SipSession(session), offerSd);
39898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            call.setListener(listener);
39998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return call;
40098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (Throwable t) {
40198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("takeAudioCall()", t);
40298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
40398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
40498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
40598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
40698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Checks if the intent is an incoming call broadcast intent.
40798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
40898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param intent the intent in question
40998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return true if the intent is an incoming call broadcast intent
41098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
41198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public static boolean isIncomingCallIntent(Intent intent) {
41298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (intent == null) return false;
41398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        String callId = getCallId(intent);
4146d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh        String offerSd = getOfferSessionDescription(intent);
41598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return ((callId != null) && (offerSd != null));
41698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
41798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
41898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
41998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the call ID from the specified incoming call broadcast intent.
42098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
42198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
42298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the call ID or null if the intent does not contain it
42398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
42498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public static String getCallId(Intent incomingCallIntent) {
4253adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return incomingCallIntent.getStringExtra(EXTRA_CALL_ID);
42698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
42798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
42898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
42998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the offer session description from the specified incoming call
43098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * broadcast intent.
43198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
43298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
43398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the offer session description or null if the intent does not
43498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      have it
43598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
4366d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh    public static String getOfferSessionDescription(Intent incomingCallIntent) {
4373adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return incomingCallIntent.getStringExtra(EXTRA_OFFER_SD);
43898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
43998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
44098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
44198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Creates an incoming call broadcast intent.
44298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
44398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param callId the call ID of the incoming call
44498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param sessionDescription the session description of the incoming call
44598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the incoming call intent
44698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @hide
44798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
4483adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static Intent createIncomingCallBroadcast(String callId,
4493adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            String sessionDescription) {
4503adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        Intent intent = new Intent();
4513adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        intent.putExtra(EXTRA_CALL_ID, callId);
4523adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        intent.putExtra(EXTRA_OFFER_SD, sessionDescription);
45398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return intent;
45498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
45598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
45698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
4573adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Manually registers the profile to the corresponding SIP provider for
458845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * receiving calls.
459845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)} is
460845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * still needed to be called at least once in order for the SIP service to
46116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main     * notify the caller with the {@link android.app.PendingIntent} when an incoming call is
462845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * received.
46398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
46498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to register with
465901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * @param expiryTime registration expiration time (in seconds)
46698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the registration events
46798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
46898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
46998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void register(SipProfile localProfile, int expiryTime,
47098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
47198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
472f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            ISipSession session = mSipService.createSession(localProfile,
473f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
47498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            session.register(expiryTime);
47598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
47698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("register()", e);
47798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
47898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
47998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
48098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
4813adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Manually unregisters the profile from the corresponding SIP provider for
4823adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * stop receiving further calls. This may interference with the auto
4833adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * registration process in the SIP service if the auto-registration option
4843adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * in the profile is enabled.
48598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
48698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to register with
48798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the registration events
48898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
48998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
49098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void unregister(SipProfile localProfile,
49198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
49298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
493f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            ISipSession session = mSipService.createSession(localProfile,
494f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
49598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            session.unregister();
49698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
49798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("unregister()", e);
49898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
49998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
50098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
50198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
5024e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * Gets the {@link SipSession} that handles the incoming call. For audio
50398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * calls, consider to use {@link SipAudioCall} to handle the incoming call.
504901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * See {@link #takeAudioCall}. Note that the method may be called only once
505901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * for the same intent. For subsequent calls on the same intent, the method
506901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * returns null.
50798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
50898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
50998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the session object that handles the incoming call
51098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
5114e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan    public SipSession getSessionFor(Intent incomingCallIntent)
51298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
51398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
51498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            String callId = getCallId(incomingCallIntent);
5154e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan            ISipSession s = mSipService.getPendingSession(callId);
5164e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan            return new SipSession(s);
51798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
51898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("getSessionFor()", e);
51998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
52098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
52198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
52298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private static ISipSessionListener createRelay(
523f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            SipRegistrationListener listener, String uri) {
524f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        return ((listener == null) ? null : new ListenerRelay(listener, uri));
52598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
52698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
52798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
5284e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * Creates a {@link SipSession} with the specified profile. Use other
5294e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan     * methods, if applicable, instead of interacting with {@link SipSession}
53098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * directly.
53198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
53298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile the session is associated with
53398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to SIP session events
53498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
5353adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipSession createSipSession(SipProfile localProfile,
5363adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipSession.Listener listener) throws SipException {
53798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
5383adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            ISipSession s = mSipService.createSession(localProfile, null);
539b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            if (s == null) {
540b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan                throw new SipException(
541b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan                        "Failed to create SipSession; network unavailable?");
542b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            }
5433adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            return new SipSession(s, listener);
54498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
54598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("createSipSession()", e);
54698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
54798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
54898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
54998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
55098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the list of profiles hosted by the SIP service. The user information
55198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * (username, password and display name) are crossed out.
55298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @hide
55398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
55498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public SipProfile[] getListOfProfiles() {
55598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
55698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.getListOfProfiles();
55798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
558b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan            return new SipProfile[0];
55998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
56098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
56198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
56298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private static class ListenerRelay extends SipSessionAdapter {
56398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        private SipRegistrationListener mListener;
564f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        private String mUri;
56598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
56698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        // listener must not be null
567f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        public ListenerRelay(SipRegistrationListener listener, String uri) {
56898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener = listener;
569f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            mUri = uri;
57098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
57198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
57298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        private String getUri(ISipSession session) {
57398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            try {
574d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan                return ((session == null)
575f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                        ? mUri
576d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan                        : session.getLocalProfile().getUriString());
577e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan            } catch (Throwable e) {
578e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                // SipService died? SIP stack died?
579e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                Log.w(TAG, "getUri(): " + e);
580e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                return null;
58198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            }
58298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
58398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
58498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
58598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistering(ISipSession session) {
58698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistering(getUri(session));
58798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
58898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
58998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
59098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistrationDone(ISipSession session, int duration) {
59198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            long expiryTime = duration;
59298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            if (duration > 0) expiryTime += System.currentTimeMillis();
59398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistrationDone(getUri(session), expiryTime);
59498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
59598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
59698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
597a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan        public void onRegistrationFailed(ISipSession session, int errorCode,
59898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                String message) {
599a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan            mListener.onRegistrationFailed(getUri(session), errorCode, message);
60098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
60198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
60298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
60398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistrationTimeout(ISipSession session) {
60498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistrationFailed(getUri(session),
6051ab079168ccc185408a8691c6b804021d79f7376Hung-ying Tyan                    SipErrorCode.TIME_OUT, "registration timed out");
60698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
60798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
60898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang}
609