SipManager.java revision 22523a59d879cf47f1e9c202d001d569fad4f69e
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/**
3298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * The class provides API for various SIP related tasks. Specifically, the API
33901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * allows an application to:
3498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * <ul>
353adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * <li>open a {@link SipProfile} to get ready for making outbound calls or have
363adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan *      the background SIP service listen to incoming calls and broadcast them
373adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan *      with registered command string. See
38845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan *      {@link #open(SipProfile, PendingIntent, SipRegistrationListener)},
39901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #open(SipProfile)}, {@link #close}, {@link #isOpened} and
40901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #isRegistered}. It also facilitates handling of the incoming call
41901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      broadcast intent. See
42901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #isIncomingCallIntent}, {@link #getCallId},
43901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #getOfferSessionDescription} and {@link #takeAudioCall}.</li>
4498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * <li>make/take SIP-based audio calls. See
45901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #makeAudioCall} and {@link #takeAudioCall}.</li>
463adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * <li>register/unregister with a SIP service provider manually. See
47901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #register} and {@link #unregister}.</li>
483adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * <li>process SIP events directly with a {@link SipSession} created by
49901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan *      {@link #createSipSession}.</li>
5098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * </ul>
5198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @hide
5298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */
5398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangpublic class SipManager {
543adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
55845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * The result code to be sent back with the incoming call
56845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link PendingIntent}.
57845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #open(SipProfile, PendingIntent, SipRegistrationListener)
58845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     */
59845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final int INCOMING_CALL_RESULT_CODE = 101;
60845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
61845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    /** Part of the incoming call intent. */
62845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final String EXTRA_CALL_ID = "android:sipCallID";
63845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
64845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    /** Part of the incoming call intent. */
65845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    public static final String EXTRA_OFFER_SD = "android:sipOfferSD";
66845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan
67845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan    /**
6822523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * Action to broadcast when SipService is up.
6922523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * Internal use only.
7022523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     * @hide
7122523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan     */
7222523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan    public static final String ACTION_SIP_SERVICE_UP =
7322523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan            "android.net.sip.SIP_SERVICE_UP";
7422523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan    /**
753adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the incoming call intent for the Phone app.
763adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
773adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
783adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
793adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_INCOMING_CALL =
8098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_INCOMING_CALL";
813adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
823adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the add-phone intent.
833adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
843adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
853adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
863adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_ADD_PHONE =
8798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_ADD_PHONE";
883adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
893adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Action string for the remove-phone intent.
903adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
913adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
923adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
933adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String ACTION_SIP_REMOVE_PHONE =
9498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            "com.android.phone.SIP_REMOVE_PHONE";
953adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    /**
963adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Part of the ACTION_SIP_ADD_PHONE and ACTION_SIP_REMOVE_PHONE intents.
973adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Internal use only.
983adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @hide
993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     */
1003adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static final String EXTRA_LOCAL_URI = "android:localSipUri";
10198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
102e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan    private static final String TAG = "SipManager";
103e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan
10498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private ISipService mSipService;
1053adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    private Context mContext;
10698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
10798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
1083adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Creates a manager instance. Returns null if SIP API is not supported.
10998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
1103adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @param context application context for creating the manager object
11169d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     * @return the manager instance or null if SIP API is not supported
11298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
1133adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static SipManager newInstance(Context context) {
1143adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return (isApiSupported(context) ? new SipManager(context) : null);
11569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    }
11669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan
11769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    /**
11869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     * Returns true if the SIP API is supported by the system.
11969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     */
12069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    public static boolean isApiSupported(Context context) {
1210d889a979d7e2e6f21de72de0c45a9c8ffa930e6Hung-ying Tyan        return true;
1223adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        /* TODO: uncomment this before ship
12369d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        return context.getPackageManager().hasSystemFeature(
12469d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan                PackageManager.FEATURE_SIP);
1250d889a979d7e2e6f21de72de0c45a9c8ffa930e6Hung-ying Tyan         */
12669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    }
12769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan
12869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    /**
12969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     * Returns true if the system supports SIP-based VoIP.
13069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan     */
13169d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    public static boolean isVoipSupported(Context context) {
1320d889a979d7e2e6f21de72de0c45a9c8ffa930e6Hung-ying Tyan        return true;
1333adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        /* TODO: uncomment this before ship
13469d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        return context.getPackageManager().hasSystemFeature(
13569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan                PackageManager.FEATURE_SIP_VOIP) && isApiSupported(context);
1360d889a979d7e2e6f21de72de0c45a9c8ffa930e6Hung-ying Tyan         */
13798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
13898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
139d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    /**
140d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan     * Returns true if SIP is only available on WIFI.
141d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan     */
142d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    public static boolean isSipWifiOnly(Context context) {
143d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan        return context.getResources().getBoolean(
144d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan                com.android.internal.R.bool.config_sip_wifi_only);
145d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan    }
146d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan
1473adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    private SipManager(Context context) {
1483adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        mContext = context;
14969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan        createSipService();
15098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
15198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
15269d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan    private void createSipService() {
15368144a84e3cd43ba4f62c73dbd2ce9c74d50e1a6Chung-yih Wang        IBinder b = ServiceManager.getService(Context.SIP_SERVICE);
15498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        mSipService = ISipService.Stub.asInterface(b);
15598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
15698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
15798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
1583adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Opens the profile for making calls. The caller may make subsequent calls
1593adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * through {@link #makeAudioCall}. If one also wants to receive calls on the
160845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * profile, use
161845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)}
1623adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * instead.
16398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
16498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to make calls from
16598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if the profile contains incorrect settings or
16698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      calling the SIP service results in an error
16798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
16898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void open(SipProfile localProfile) throws SipException {
16998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
17098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.open(localProfile);
17198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
17298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("open()", e);
17398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
17498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
17598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
17698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
1773adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Opens the profile for making calls and/or receiving calls. The caller may
1783adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * make subsequent calls through {@link #makeAudioCall}. If the
1793adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * auto-registration option is enabled in the profile, the SIP service
1803adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * will register the profile to the corresponding SIP provider periodically
1813adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * in order to receive calls from the provider.
18298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
18398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to receive incoming calls for
184845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @param incomingCallPendingIntent When an incoming call is received, the
185845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      SIP service will call
186845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      {@link PendingIntent#send(Context, int, Intent)} to send back the
187845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      intent to the caller with {@link #INCOMING_CALL_RESULT_CODE} as the
188845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      result code and the intent to fill in the call ID and session
189845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     *      description information. It cannot be null.
19098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to registration events; can be null
191845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #getCallId
192845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #getOfferSessionDescription
193845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @see #takeAudioCall
194845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * @throws NullPointerException if {@code incomingCallPendingIntent} is null
19598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if the profile contains incorrect settings or
19698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      calling the SIP service results in an error
19798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
19898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void open(SipProfile localProfile,
199845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            PendingIntent incomingCallPendingIntent,
20098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
201845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan        if (incomingCallPendingIntent == null) {
202845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            throw new NullPointerException(
203845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan                    "incomingCallPendingIntent cannot be null");
204845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan        }
20598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
206845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan            mSipService.open3(localProfile, incomingCallPendingIntent,
207f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
20898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
20998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("open()", e);
21098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
21198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
21298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
21398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
21498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Sets the listener to listen to registration events. No effect if the
215845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * profile has not been opened to receive calls (see
216845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)}).
21798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
21898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile
21998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to registration events; can be null
22098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
22198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
22298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void setRegistrationListener(String localProfileUri,
22398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
22498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
22598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.setRegistrationListener(
226f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    localProfileUri, createRelay(listener, localProfileUri));
22798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
22898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("setRegistrationListener()", e);
22998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
23098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
23198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
23298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
23398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Closes the specified profile to not make/receive calls. All the resources
23498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * that were allocated to the profile are also released.
23598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
23698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile to close
23798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
23898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
23998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void close(String localProfileUri) throws SipException {
24098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
24198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mSipService.close(localProfileUri);
24298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
24398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("close()", e);
24498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
24598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
24698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
24798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
2483adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Checks if the specified profile is opened in the SIP service for
2493adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * making and/or receiving calls.
25098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
25198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile in question
25298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return true if the profile is enabled to receive calls
25398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
25498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
25598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public boolean isOpened(String localProfileUri) throws SipException {
25698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
25798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.isOpened(localProfileUri);
25898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
25998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("isOpened()", e);
26098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
26198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
26298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
26398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
2643adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Checks if the SIP service has successfully registered the profile to the
2653adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * SIP provider (specified in the profile) for receiving calls. Returning
2663adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * true from this method also implies the profile is opened
2673adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * ({@link #isOpened}).
26898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
26998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri the URI of the profile in question
2703adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @return true if the profile is registered to the SIP provider; false if
2713adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        the profile has not been opened in the SIP service or the SIP
2723adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        service has not yet successfully registered the profile to the SIP
2733adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     *        provider
27498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
27598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
27698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public boolean isRegistered(String localProfileUri) throws SipException {
27798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
27898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.isRegistered(localProfileUri);
27998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
28098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("isRegistered()", e);
28198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
28298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
28398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
28498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
285f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * Creates a {@link SipAudioCall} to make a call. The attempt will be timed
286f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * out if the call is not established within {@code timeout} seconds and
287f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * {@code SipAudioCall.Listener.onError(SipAudioCall, SipErrorCode.TIME_OUT, String)}
288f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * will be called.
28998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
29098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to make the call from
29198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param peerProfile the SIP profile to make the call to
29298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
29398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
294f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * @param timeout the timeout value in seconds
29598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
29698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
297f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * @see SipAudioCall.Listener.onError
29898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
2993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall makeAudioCall(SipProfile localProfile,
300f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan            SipProfile peerProfile, SipAudioCall.Listener listener, int timeout)
30198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
3023adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        SipAudioCall call = new SipAudioCall(mContext, localProfile);
30398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        call.setListener(listener);
3047a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        SipSession s = createSipSession(localProfile, null);
3057a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        if (s == null) {
3067a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan            throw new SipException(
3077a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan                    "Failed to create SipSession; network available?");
3087a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        }
3097a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan        call.makeCall(peerProfile, s, timeout);
31098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return call;
31198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
31298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
31398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
314845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * Creates a {@link SipAudioCall} to make an audio call. The attempt will be
315845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * timed out if the call is not established within {@code timeout} seconds
316845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * and
317f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * {@code SipAudioCall.Listener.onError(SipAudioCall, SipErrorCode.TIME_OUT, String)}
318f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * will be called.
31998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
32098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfileUri URI of the SIP profile to make the call from
32198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param peerProfileUri URI of the SIP profile to make the call to
32298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
32398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
324f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * @param timeout the timeout value in seconds
32598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
32698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
327f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan     * @see SipAudioCall.Listener.onError
32898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3293adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall makeAudioCall(String localProfileUri,
330f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan            String peerProfileUri, SipAudioCall.Listener listener, int timeout)
33198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
33298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
3333adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            return makeAudioCall(
33498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                    new SipProfile.Builder(localProfileUri).build(),
335f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan                    new SipProfile.Builder(peerProfileUri).build(), listener,
336f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan                    timeout);
33798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (ParseException e) {
33898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("build SipProfile", e);
33998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
34098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
34198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
34298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
3433adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * The method calls {@code takeAudioCall(incomingCallIntent,
34498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * listener, true}.
34598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
3463adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * @see #takeAudioCall(Intent, SipAudioCall.Listener, boolean)
34798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3483adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall takeAudioCall(Intent incomingCallIntent,
3493adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipAudioCall.Listener listener) throws SipException {
3503adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return takeAudioCall(incomingCallIntent, listener, true);
35198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
35298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
35398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
35498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Creates a {@link SipAudioCall} to take an incoming call. Before the call
35598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * is returned, the listener will receive a
356901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * {@link SipAudioCall.Listener#onRinging}
35798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * callback.
35898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
35998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
36098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the call events from {@link SipAudioCall};
36198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      can be null
36298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return a {@link SipAudioCall} object
36398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
36498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
3653adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipAudioCall takeAudioCall(Intent incomingCallIntent,
3663adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipAudioCall.Listener listener, boolean ringtoneEnabled)
3673adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            throws SipException {
36898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (incomingCallIntent == null) return null;
36998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
37098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        String callId = getCallId(incomingCallIntent);
37198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (callId == null) {
37298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("Call ID missing in incoming call intent");
37398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
37498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
3756d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh        String offerSd = getOfferSessionDescription(incomingCallIntent);
37698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (offerSd == null) {
37798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("Session description missing in incoming "
37898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                    + "call intent");
37998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
38098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
38198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
38298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            ISipSession session = mSipService.getPendingSession(callId);
38398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            if (session == null) return null;
3843adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipAudioCall call = new SipAudioCall(
3853adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan                    mContext, session.getLocalProfile());
38698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            call.setRingtoneEnabled(ringtoneEnabled);
3873adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            call.attachCall(new SipSession(session), offerSd);
38898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            call.setListener(listener);
38998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return call;
39098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (Throwable t) {
39198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("takeAudioCall()", t);
39298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
39398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
39498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
39598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
39698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Checks if the intent is an incoming call broadcast intent.
39798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
39898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param intent the intent in question
39998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return true if the intent is an incoming call broadcast intent
40098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
40198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public static boolean isIncomingCallIntent(Intent intent) {
40298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        if (intent == null) return false;
40398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        String callId = getCallId(intent);
4046d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh        String offerSd = getOfferSessionDescription(intent);
40598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return ((callId != null) && (offerSd != null));
40698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
40798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
40898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
40998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the call ID from the specified incoming call broadcast intent.
41098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
41198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
41298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the call ID or null if the intent does not contain it
41398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
41498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public static String getCallId(Intent incomingCallIntent) {
4153adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return incomingCallIntent.getStringExtra(EXTRA_CALL_ID);
41698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
41798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
41898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
41998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the offer session description from the specified incoming call
42098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * broadcast intent.
42198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
42298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
42398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the offer session description or null if the intent does not
42498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *      have it
42598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
4266d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh    public static String getOfferSessionDescription(Intent incomingCallIntent) {
4273adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        return incomingCallIntent.getStringExtra(EXTRA_OFFER_SD);
42898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
42998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
43098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
43198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Creates an incoming call broadcast intent.
43298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
43398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param callId the call ID of the incoming call
43498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param sessionDescription the session description of the incoming call
43598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the incoming call intent
43698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @hide
43798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
4383adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public static Intent createIncomingCallBroadcast(String callId,
4393adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            String sessionDescription) {
4403adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        Intent intent = new Intent();
4413adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        intent.putExtra(EXTRA_CALL_ID, callId);
4423adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan        intent.putExtra(EXTRA_OFFER_SD, sessionDescription);
44398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        return intent;
44498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
44598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
44698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
4473adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Manually registers the profile to the corresponding SIP provider for
448845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * receiving calls.
449845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)} is
450845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * still needed to be called at least once in order for the SIP service to
451845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * notify the caller with the {@code PendingIntent} when an incoming call is
452845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan     * received.
45398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
45498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to register with
455901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * @param expiryTime registration expiration time (in seconds)
45698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the registration events
45798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
45898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
45998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void register(SipProfile localProfile, int expiryTime,
46098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
46198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
462f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            ISipSession session = mSipService.createSession(localProfile,
463f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
46498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            session.register(expiryTime);
46598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
46698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("register()", e);
46798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
46898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
46998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
47098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
4713adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * Manually unregisters the profile from the corresponding SIP provider for
4723adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * stop receiving further calls. This may interference with the auto
4733adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * registration process in the SIP service if the auto-registration option
4743adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan     * in the profile is enabled.
47598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
47698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile to register with
47798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to the registration events
47898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @throws SipException if calling the SIP service results in an error
47998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
48098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public void unregister(SipProfile localProfile,
48198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            SipRegistrationListener listener) throws SipException {
48298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
483f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            ISipSession session = mSipService.createSession(localProfile,
484f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                    createRelay(listener, localProfile.getUriString()));
48598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            session.unregister();
48698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
48798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("unregister()", e);
48898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
48998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
49098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
49198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
49298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the {@link ISipSession} that handles the incoming call. For audio
49398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * calls, consider to use {@link SipAudioCall} to handle the incoming call.
494901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * See {@link #takeAudioCall}. Note that the method may be called only once
495901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * for the same intent. For subsequent calls on the same intent, the method
496901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan     * returns null.
49798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
49898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param incomingCallIntent the incoming call broadcast intent
49998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @return the session object that handles the incoming call
50098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
50198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public ISipSession getSessionFor(Intent incomingCallIntent)
50298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throws SipException {
50398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
50498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            String callId = getCallId(incomingCallIntent);
50598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.getPendingSession(callId);
50698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
50798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("getSessionFor()", e);
50898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
50998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
51098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
51198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private static ISipSessionListener createRelay(
512f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            SipRegistrationListener listener, String uri) {
513f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        return ((listener == null) ? null : new ListenerRelay(listener, uri));
51498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
51598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
51698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
51798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Creates a {@link ISipSession} with the specified profile. Use other
51898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * methods, if applicable, instead of interacting with {@link ISipSession}
51998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * directly.
52098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     *
52198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param localProfile the SIP profile the session is associated with
52298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @param listener to listen to SIP session events
52398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
5243adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan    public SipSession createSipSession(SipProfile localProfile,
5253adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            SipSession.Listener listener) throws SipException {
52698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
5273adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            ISipSession s = mSipService.createSession(localProfile, null);
5283adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan            return new SipSession(s, listener);
52998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
53098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            throw new SipException("createSipSession()", e);
53198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
53298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
53398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
53498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    /**
53598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * Gets the list of profiles hosted by the SIP service. The user information
53698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * (username, password and display name) are crossed out.
53798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     * @hide
53898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang     */
53998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    public SipProfile[] getListOfProfiles() {
54098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        try {
54198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return mSipService.getListOfProfiles();
54298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        } catch (RemoteException e) {
54398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            return null;
54498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
54598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
54698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
54798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    private static class ListenerRelay extends SipSessionAdapter {
54898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        private SipRegistrationListener mListener;
549f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        private String mUri;
55098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
55198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        // listener must not be null
552f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan        public ListenerRelay(SipRegistrationListener listener, String uri) {
55398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener = listener;
554f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan            mUri = uri;
55598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
55698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
55798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        private String getUri(ISipSession session) {
55898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            try {
559d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan                return ((session == null)
560f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan                        ? mUri
561d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan                        : session.getLocalProfile().getUriString());
562e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan            } catch (Throwable e) {
563e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                // SipService died? SIP stack died?
564e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                Log.w(TAG, "getUri(): " + e);
565e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan                return null;
56698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            }
56798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
56898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
56998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
57098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistering(ISipSession session) {
57198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistering(getUri(session));
57298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
57398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
57498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
57598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistrationDone(ISipSession session, int duration) {
57698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            long expiryTime = duration;
57798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            if (duration > 0) expiryTime += System.currentTimeMillis();
57898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistrationDone(getUri(session), expiryTime);
57998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
58098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
58198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
582a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan        public void onRegistrationFailed(ISipSession session, int errorCode,
58398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang                String message) {
584a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan            mListener.onRegistrationFailed(getUri(session), errorCode, message);
58598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
58698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang
58798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        @Override
58898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        public void onRegistrationTimeout(ISipSession session) {
58998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang            mListener.onRegistrationFailed(getUri(session),
5901ab079168ccc185408a8691c6b804021d79f7376Hung-ying Tyan                    SipErrorCode.TIME_OUT, "registration timed out");
59198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang        }
59298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang    }
59398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang}
594