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.RemoteException; 2598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport android.os.ServiceManager; 269329db04f13480ccdff013dcc00cdb96f12a921cWink Savilleimport android.telephony.Rlog; 2798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 2898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangimport java.text.ParseException; 2998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 3098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang/** 3116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * Provides APIs for SIP tasks, such as initiating SIP connections, and provides access to related 3216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * SIP services. This class is the starting point for any SIP actions. You can acquire an instance 3316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * of it with {@link #newInstance newInstance()}.</p> 3416b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <p>The APIs in this class allows you to:</p> 3598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * <ul> 3616b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Create a {@link SipSession} to get ready for making calls or listen for incoming calls. See 3716b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link #createSipSession createSipSession()} and {@link #getSessionFor getSessionFor()}.</li> 3816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Initiate and receive generic SIP calls or audio-only SIP calls. Generic SIP calls may 3916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * be video, audio, or other, and are initiated with {@link #open open()}. Audio-only SIP calls 4016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * should be handled with a {@link SipAudioCall}, which you can acquire with {@link 4116b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * #makeAudioCall makeAudioCall()} and {@link #takeAudioCall takeAudioCall()}.</li> 4216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Register and unregister with a SIP service provider, with 4316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link #register register()} and {@link #unregister unregister()}.</li> 4416b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <li>Verify session connectivity, with {@link #isOpened isOpened()} and 4516b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link #isRegistered isRegistered()}.</li> 4698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * </ul> 4716b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * <p class="note"><strong>Note:</strong> Not all Android-powered devices support VOIP calls using 4816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * SIP. You should always call {@link android.net.sip.SipManager#isVoipSupported 4916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * isVoipSupported()} to verify that the device supports VOIP calling and {@link 5016b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * android.net.sip.SipManager#isApiSupported isApiSupported()} to verify that the device supports 51b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * the SIP APIs. Your application must also request the {@link 5216b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * android.Manifest.permission#INTERNET} and {@link android.Manifest.permission#USE_SIP} 5316b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * permissions.</p> 54b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * 55b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * <div class="special reference"> 56b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * <h3>Developer Guides</h3> 57b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * <p>For more information about using SIP, read the 58b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * <a href="{@docRoot}guide/topics/network/sip.html">Session Initiation Protocol</a> 59b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * developer guide.</p> 60b4074803f775e5a4616287804f3405fd17eecab3Joe Fernandez * </div> 6198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 6298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wangpublic class SipManager { 633adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan /** 64845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * The result code to be sent back with the incoming call 65845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * {@link PendingIntent}. 66845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @see #open(SipProfile, PendingIntent, SipRegistrationListener) 67845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan */ 68845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan public static final int INCOMING_CALL_RESULT_CODE = 101; 69845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan 704e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan /** 714e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * Key to retrieve the call ID from an incoming call intent. 724e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @see #open(SipProfile, PendingIntent, SipRegistrationListener) 734e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan */ 74845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan public static final String EXTRA_CALL_ID = "android:sipCallID"; 75845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan 764e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan /** 774e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * Key to retrieve the offered session description from an incoming call 784e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * intent. 794e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @see #open(SipProfile, PendingIntent, SipRegistrationListener) 804e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan */ 81845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan public static final String EXTRA_OFFER_SD = "android:sipOfferSD"; 82845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan 83845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan /** 8422523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan * Action to broadcast when SipService is up. 8522523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan * Internal use only. 8622523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan * @hide 8722523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan */ 8822523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan public static final String ACTION_SIP_SERVICE_UP = 8922523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan "android.net.sip.SIP_SERVICE_UP"; 9022523a59d879cf47f1e9c202d001d569fad4f69eHung-ying Tyan /** 913adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Action string for the incoming call intent for the Phone app. 923adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Internal use only. 933adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @hide 943adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan */ 953adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static final String ACTION_SIP_INCOMING_CALL = 9698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang "com.android.phone.SIP_INCOMING_CALL"; 973adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan /** 983adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Action string for the add-phone intent. 993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Internal use only. 1003adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @hide 1013adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan */ 1023adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static final String ACTION_SIP_ADD_PHONE = 10398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang "com.android.phone.SIP_ADD_PHONE"; 1043adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan /** 1053adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Action string for the remove-phone intent. 1063adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Internal use only. 1073adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @hide 1083adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan */ 1093adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static final String ACTION_SIP_REMOVE_PHONE = 11098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang "com.android.phone.SIP_REMOVE_PHONE"; 111cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn 112cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn /** 113cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn * Action string for the SIP call option configuration changed intent. 114cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn * This is used to communicate change to the SIP call option, triggering re-registration of 115cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn * the SIP phone accounts. 116cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn * Internal use only. 117cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn * @hide 118cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn */ 119cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn public static final String ACTION_SIP_CALL_OPTION_CHANGED = 120cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn "com.android.phone.SIP_CALL_OPTION_CHANGED"; 121cf38b8464007ebf626fc0d61a646491fc3d794cdTyler Gunn 1223adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan /** 1233adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Part of the ACTION_SIP_ADD_PHONE and ACTION_SIP_REMOVE_PHONE intents. 1243adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Internal use only. 1253adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @hide 1263adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan */ 1273adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static final String EXTRA_LOCAL_URI = "android:localSipUri"; 12898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 129e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan private static final String TAG = "SipManager"; 130e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan 13198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang private ISipService mSipService; 1323adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan private Context mContext; 13398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 13498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 1353adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Creates a manager instance. Returns null if SIP API is not supported. 13698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 1373adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @param context application context for creating the manager object 13869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan * @return the manager instance or null if SIP API is not supported 13998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 1403adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static SipManager newInstance(Context context) { 1413adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan return (isApiSupported(context) ? new SipManager(context) : null); 14269d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan } 14369d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan 14469d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan /** 14569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan * Returns true if the SIP API is supported by the system. 14669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan */ 14769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan public static boolean isApiSupported(Context context) { 14869d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan return context.getPackageManager().hasSystemFeature( 14969d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan PackageManager.FEATURE_SIP); 15069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan } 15169d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan 15269d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan /** 15338ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * Returns true if the system supports SIP-based VOIP API. 15469d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan */ 15569d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan public static boolean isVoipSupported(Context context) { 15669d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan return context.getPackageManager().hasSystemFeature( 15769d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan PackageManager.FEATURE_SIP_VOIP) && isApiSupported(context); 15898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 15998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 160d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan /** 161d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan * Returns true if SIP is only available on WIFI. 162d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan */ 163d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan public static boolean isSipWifiOnly(Context context) { 164d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan return context.getResources().getBoolean( 165d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan com.android.internal.R.bool.config_sip_wifi_only); 166d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan } 167d96284491dfc918c72258f84b47a6554c21db92eHung-ying Tyan 1683adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan private SipManager(Context context) { 1693adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan mContext = context; 17069d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan createSipService(); 17198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 17298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 17369d66b75112e2763bf8bce718c22c7baa1efac7dHung-ying Tyan private void createSipService() { 17468144a84e3cd43ba4f62c73dbd2ce9c74d50e1a6Chung-yih Wang IBinder b = ServiceManager.getService(Context.SIP_SERVICE); 17598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mSipService = ISipService.Stub.asInterface(b); 17698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 17798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 17898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 17916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * Opens the profile for making generic SIP calls. The caller may make subsequent calls 1803adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * through {@link #makeAudioCall}. If one also wants to receive calls on the 181845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * profile, use 182845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)} 1833adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * instead. 18498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 18598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile to make calls from 18698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if the profile contains incorrect settings or 18798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * calling the SIP service results in an error 18898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 18998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void open(SipProfile localProfile) throws SipException { 19098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 19198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mSipService.open(localProfile); 19298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 19398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("open()", e); 19498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 19598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 19698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 19798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 19816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * Opens the profile for making calls and/or receiving generic SIP calls. The caller may 1993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * make subsequent calls through {@link #makeAudioCall}. If the 2003adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * auto-registration option is enabled in the profile, the SIP service 2013adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * will register the profile to the corresponding SIP provider periodically 2024e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * in order to receive calls from the provider. When the SIP service 2034e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * receives a new call, it will send out an intent with the provided action 2044e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * string. The intent contains a call ID extra and an offer session 2054e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * description string extra. Use {@link #getCallId} and 2064e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * {@link #getOfferSessionDescription} to retrieve those extras. 20798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 20898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile to receive incoming calls for 209845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @param incomingCallPendingIntent When an incoming call is received, the 210845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * SIP service will call 211845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * {@link PendingIntent#send(Context, int, Intent)} to send back the 212845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * intent to the caller with {@link #INCOMING_CALL_RESULT_CODE} as the 213845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * result code and the intent to fill in the call ID and session 214845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * description information. It cannot be null. 21598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to registration events; can be null 216845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @see #getCallId 217845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @see #getOfferSessionDescription 218845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @see #takeAudioCall 219845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * @throws NullPointerException if {@code incomingCallPendingIntent} is null 22098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if the profile contains incorrect settings or 22198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * calling the SIP service results in an error 2224e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @see #isIncomingCallIntent 2234e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @see #getCallId 2244e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @see #getOfferSessionDescription 22598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 22698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void open(SipProfile localProfile, 227845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan PendingIntent incomingCallPendingIntent, 22898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang SipRegistrationListener listener) throws SipException { 229845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan if (incomingCallPendingIntent == null) { 230845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan throw new NullPointerException( 231845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan "incomingCallPendingIntent cannot be null"); 232845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan } 23398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 234845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan mSipService.open3(localProfile, incomingCallPendingIntent, 235f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan createRelay(listener, localProfile.getUriString())); 23698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 23798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("open()", e); 23898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 23998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 24098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 24198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 24298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Sets the listener to listen to registration events. No effect if the 243845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * profile has not been opened to receive calls (see 244845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)}). 24598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 24698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfileUri the URI of the profile 24798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to registration events; can be null 24898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 24998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 25098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void setRegistrationListener(String localProfileUri, 25198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang SipRegistrationListener listener) throws SipException { 25298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 25398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mSipService.setRegistrationListener( 254f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan localProfileUri, createRelay(listener, localProfileUri)); 25598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 25698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("setRegistrationListener()", e); 25798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 25898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 25998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 26098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 26198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Closes the specified profile to not make/receive calls. All the resources 26298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * that were allocated to the profile are also released. 26398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 26498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfileUri the URI of the profile to close 26598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 26698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 26798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void close(String localProfileUri) throws SipException { 26898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 26998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mSipService.close(localProfileUri); 27098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 27198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("close()", e); 27298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 27398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 27498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 27598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 2763adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Checks if the specified profile is opened in the SIP service for 2773adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * making and/or receiving calls. 27898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 27998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfileUri the URI of the profile in question 28098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return true if the profile is enabled to receive calls 28198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 28298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 28398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public boolean isOpened(String localProfileUri) throws SipException { 28498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 28598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return mSipService.isOpened(localProfileUri); 28698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 28798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("isOpened()", e); 28898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 28998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 29098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 29198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 2923adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Checks if the SIP service has successfully registered the profile to the 2933adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * SIP provider (specified in the profile) for receiving calls. Returning 2943adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * true from this method also implies the profile is opened 2953adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * ({@link #isOpened}). 29698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 29798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfileUri the URI of the profile in question 2983adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * @return true if the profile is registered to the SIP provider; false if 2993adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * the profile has not been opened in the SIP service or the SIP 3003adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * service has not yet successfully registered the profile to the SIP 3013adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * provider 30298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 30398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 30498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public boolean isRegistered(String localProfileUri) throws SipException { 30598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 30698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return mSipService.isRegistered(localProfileUri); 30798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 30898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("isRegistered()", e); 30998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 31098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 31198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 31298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 313f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan * Creates a {@link SipAudioCall} to make a call. The attempt will be timed 314f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan * out if the call is not established within {@code timeout} seconds and 31516b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link SipAudioCall.Listener#onError onError(SipAudioCall, SipErrorCode.TIME_OUT, String)} 316f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan * will be called. 31798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 31898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile to make the call from 31998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param peerProfile the SIP profile to make the call to 32098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to the call events from {@link SipAudioCall}; 32198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * can be null 3224e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @param timeout the timeout value in seconds. Default value (defined by 3234e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * SIP protocol) is used if {@code timeout} is zero or negative. 32498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return a {@link SipAudioCall} object 32538ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * @throws SipException if calling the SIP service results in an error or 32638ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * VOIP API is not supported by the device 32716b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * @see SipAudioCall.Listener#onError 32838ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * @see #isVoipSupported 32998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 3303adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public SipAudioCall makeAudioCall(SipProfile localProfile, 331f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan SipProfile peerProfile, SipAudioCall.Listener listener, int timeout) 33298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throws SipException { 33338ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan if (!isVoipSupported(mContext)) { 33438ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan throw new SipException("VOIP API is not supported"); 33538ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan } 3363adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan SipAudioCall call = new SipAudioCall(mContext, localProfile); 33798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang call.setListener(listener); 3387a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan SipSession s = createSipSession(localProfile, null); 3397a4ec7268fbfb56e22f1cb8497d37ed733d8aa8eHung-ying Tyan call.makeCall(peerProfile, s, timeout); 34098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return call; 34198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 34298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 34398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 344845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * Creates a {@link SipAudioCall} to make an audio call. The attempt will be 345845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * timed out if the call is not established within {@code timeout} seconds 346845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * and 34716b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * {@link SipAudioCall.Listener#onError onError(SipAudioCall, SipErrorCode.TIME_OUT, String)} 348f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan * will be called. 34998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 35098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfileUri URI of the SIP profile to make the call from 35198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param peerProfileUri URI of the SIP profile to make the call to 35298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to the call events from {@link SipAudioCall}; 35398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * can be null 3544e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * @param timeout the timeout value in seconds. Default value (defined by 3554e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * SIP protocol) is used if {@code timeout} is zero or negative. 35698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return a {@link SipAudioCall} object 35738ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * @throws SipException if calling the SIP service results in an error or 35838ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * VOIP API is not supported by the device 35916b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * @see SipAudioCall.Listener#onError 36038ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan * @see #isVoipSupported 36198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 3623adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public SipAudioCall makeAudioCall(String localProfileUri, 363f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan String peerProfileUri, SipAudioCall.Listener listener, int timeout) 36498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throws SipException { 36538ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan if (!isVoipSupported(mContext)) { 36638ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan throw new SipException("VOIP API is not supported"); 36738ee75e8c0722118d26a23afe5b509b6a09302f4Hung-ying Tyan } 36898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 3693adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan return makeAudioCall( 37098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang new SipProfile.Builder(localProfileUri).build(), 371f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan new SipProfile.Builder(peerProfileUri).build(), listener, 372f498e98d2e2910e866ec0728cebbe676dd475d9eHung-ying Tyan timeout); 37398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (ParseException e) { 37498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("build SipProfile", e); 37598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 37698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 37798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 37898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 37998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Creates a {@link SipAudioCall} to take an incoming call. Before the call 38098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * is returned, the listener will receive a 381901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * {@link SipAudioCall.Listener#onRinging} 38298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * callback. 38398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 38498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param incomingCallIntent the incoming call broadcast intent 38598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to the call events from {@link SipAudioCall}; 38698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * can be null 38798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return a {@link SipAudioCall} object 38898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 38998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 3903adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public SipAudioCall takeAudioCall(Intent incomingCallIntent, 3916577ac4cc93ee3670a5aceceb9a95260d41fe570Hung-ying Tyan SipAudioCall.Listener listener) throws SipException { 392b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan if (incomingCallIntent == null) { 393b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan throw new SipException("Cannot retrieve session with null intent"); 394b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan } 39598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 39698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang String callId = getCallId(incomingCallIntent); 39798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang if (callId == null) { 39898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("Call ID missing in incoming call intent"); 39998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 40098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 4016d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh String offerSd = getOfferSessionDescription(incomingCallIntent); 40298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang if (offerSd == null) { 40398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("Session description missing in incoming " 40498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang + "call intent"); 40598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 40698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 40798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 40898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang ISipSession session = mSipService.getPendingSession(callId); 409b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan if (session == null) { 410b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan throw new SipException("No pending session for the call"); 411b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan } 4123adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan SipAudioCall call = new SipAudioCall( 4133adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan mContext, session.getLocalProfile()); 4143adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan call.attachCall(new SipSession(session), offerSd); 41598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang call.setListener(listener); 41698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return call; 41798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (Throwable t) { 41898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("takeAudioCall()", t); 41998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 42098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 42198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 42298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 42398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Checks if the intent is an incoming call broadcast intent. 42498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 42598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param intent the intent in question 42698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return true if the intent is an incoming call broadcast intent 42798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 42898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public static boolean isIncomingCallIntent(Intent intent) { 42998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang if (intent == null) return false; 43098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang String callId = getCallId(intent); 4316d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh String offerSd = getOfferSessionDescription(intent); 43298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return ((callId != null) && (offerSd != null)); 43398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 43498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 43598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 43698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Gets the call ID from the specified incoming call broadcast intent. 43798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 43898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param incomingCallIntent the incoming call broadcast intent 43998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return the call ID or null if the intent does not contain it 44098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 44198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public static String getCallId(Intent incomingCallIntent) { 4423adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan return incomingCallIntent.getStringExtra(EXTRA_CALL_ID); 44398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 44498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 44598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 44698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Gets the offer session description from the specified incoming call 44798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * broadcast intent. 44898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 44998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param incomingCallIntent the incoming call broadcast intent 45098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return the offer session description or null if the intent does not 45198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * have it 45298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 4536d0ef774a2492b0996ded3a43c300c7f72a94897Chia-chi Yeh public static String getOfferSessionDescription(Intent incomingCallIntent) { 4543adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan return incomingCallIntent.getStringExtra(EXTRA_OFFER_SD); 45598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 45698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 45798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 45898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Creates an incoming call broadcast intent. 45998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 46098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param callId the call ID of the incoming call 46198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param sessionDescription the session description of the incoming call 46298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return the incoming call intent 46398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @hide 46498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 4653adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public static Intent createIncomingCallBroadcast(String callId, 4663adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan String sessionDescription) { 4673adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan Intent intent = new Intent(); 4683adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan intent.putExtra(EXTRA_CALL_ID, callId); 4693adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan intent.putExtra(EXTRA_OFFER_SD, sessionDescription); 47098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return intent; 47198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 47298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 47398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 4743adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Manually registers the profile to the corresponding SIP provider for 475845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * receiving calls. 476845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * {@link #open(SipProfile, PendingIntent, SipRegistrationListener)} is 477845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * still needed to be called at least once in order for the SIP service to 47816b441b4ad92c6a5cbc6f27cb3705eaaaaee20c1Scott Main * notify the caller with the {@link android.app.PendingIntent} when an incoming call is 479845f7332f04864c5483b3e63da5db076fc7a888aHung-ying Tyan * received. 48098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 48198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile to register with 482901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * @param expiryTime registration expiration time (in seconds) 48398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to the registration events 48498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 48598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 48698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void register(SipProfile localProfile, int expiryTime, 48798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang SipRegistrationListener listener) throws SipException { 48898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 489f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan ISipSession session = mSipService.createSession(localProfile, 490f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan createRelay(listener, localProfile.getUriString())); 491088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo if (session == null) { 492088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo throw new SipException( 493088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo "SipService.createSession() returns null"); 494088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo } 49598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang session.register(expiryTime); 49698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 49798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("register()", e); 49898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 49998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 50098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 50198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 5023adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * Manually unregisters the profile from the corresponding SIP provider for 5033adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * stop receiving further calls. This may interference with the auto 5043adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * registration process in the SIP service if the auto-registration option 5053adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan * in the profile is enabled. 50698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 50798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile to register with 50898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to the registration events 50998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @throws SipException if calling the SIP service results in an error 51098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 51198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void unregister(SipProfile localProfile, 51298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang SipRegistrationListener listener) throws SipException { 51398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 514f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan ISipSession session = mSipService.createSession(localProfile, 515f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan createRelay(listener, localProfile.getUriString())); 516088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo if (session == null) { 517088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo throw new SipException( 518088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo "SipService.createSession() returns null"); 519088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo } 52098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang session.unregister(); 52198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 52298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("unregister()", e); 52398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 52498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 52598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 52698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 5274e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * Gets the {@link SipSession} that handles the incoming call. For audio 52898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * calls, consider to use {@link SipAudioCall} to handle the incoming call. 529901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * See {@link #takeAudioCall}. Note that the method may be called only once 530901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * for the same intent. For subsequent calls on the same intent, the method 531901503e1cf8b6cfac1540a22c325eb5cdd879429Hung-ying Tyan * returns null. 53298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 53398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param incomingCallIntent the incoming call broadcast intent 53498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @return the session object that handles the incoming call 53598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 5364e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan public SipSession getSessionFor(Intent incomingCallIntent) 53798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throws SipException { 53898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 53998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang String callId = getCallId(incomingCallIntent); 5404e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan ISipSession s = mSipService.getPendingSession(callId); 541088ef46d8b5a849d80a257670bb2a809713b7bf0Masahiko Endo return ((s == null) ? null : new SipSession(s)); 54298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 54398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("getSessionFor()", e); 54498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 54598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 54698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 54798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang private static ISipSessionListener createRelay( 548f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan SipRegistrationListener listener, String uri) { 549f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan return ((listener == null) ? null : new ListenerRelay(listener, uri)); 55098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 55198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 55298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 5534e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * Creates a {@link SipSession} with the specified profile. Use other 5544e22f5d6687c18440e2f38c7e242d66ad834d1d7Hung-ying Tyan * methods, if applicable, instead of interacting with {@link SipSession} 55598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * directly. 55698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * 55798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param localProfile the SIP profile the session is associated with 55898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @param listener to listen to SIP session events 55998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 5603adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan public SipSession createSipSession(SipProfile localProfile, 5613adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan SipSession.Listener listener) throws SipException { 56298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 5633adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan ISipSession s = mSipService.createSession(localProfile, null); 564b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan if (s == null) { 565b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan throw new SipException( 566b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan "Failed to create SipSession; network unavailable?"); 567b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan } 5683adf1946e78b52686fa5458e24645b05da57dc22Hung-ying Tyan return new SipSession(s, listener); 56998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 57098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang throw new SipException("createSipSession()", e); 57198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 57298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 57398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 57498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang /** 57598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * Gets the list of profiles hosted by the SIP service. The user information 57698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * (username, password and display name) are crossed out. 57798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang * @hide 57898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang */ 57998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public SipProfile[] getListOfProfiles() { 58098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 58198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang return mSipService.getListOfProfiles(); 58298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } catch (RemoteException e) { 583b030e52a52acaf28ce68b1010aac007af909478dHung-ying Tyan return new SipProfile[0]; 58498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 58598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 58698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 58798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang private static class ListenerRelay extends SipSessionAdapter { 58898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang private SipRegistrationListener mListener; 589f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan private String mUri; 59098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 59198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang // listener must not be null 592f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan public ListenerRelay(SipRegistrationListener listener, String uri) { 59398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mListener = listener; 594f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan mUri = uri; 59598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 59698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 59798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang private String getUri(ISipSession session) { 59898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang try { 599d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan return ((session == null) 600f3d1e1fd0f86eb956c82634c075b167dc367757cHung-ying Tyan ? mUri 601d8d3b15f408314ac88201eee3e401a35556ba669Hung-ying Tyan : session.getLocalProfile().getUriString()); 602e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan } catch (Throwable e) { 603e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan // SipService died? SIP stack died? 6049329db04f13480ccdff013dcc00cdb96f12a921cWink Saville Rlog.e(TAG, "getUri(): ", e); 605e8782e2fdfd256296a0d1fd2a1c3f98d58103eaaHung-ying Tyan return null; 60698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 60798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 60898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 60998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang @Override 61098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void onRegistering(ISipSession session) { 61198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mListener.onRegistering(getUri(session)); 61298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 61398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 61498cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang @Override 61598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void onRegistrationDone(ISipSession session, int duration) { 61698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang long expiryTime = duration; 61798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang if (duration > 0) expiryTime += System.currentTimeMillis(); 61898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mListener.onRegistrationDone(getUri(session), expiryTime); 61998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 62098cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 62198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang @Override 622a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan public void onRegistrationFailed(ISipSession session, int errorCode, 62398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang String message) { 624a0171082cfc4b860a82dcf5ebbd498b253f1032fHung-ying Tyan mListener.onRegistrationFailed(getUri(session), errorCode, message); 62598cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 62698cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang 62798cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang @Override 62898cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang public void onRegistrationTimeout(ISipSession session) { 62998cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang mListener.onRegistrationFailed(getUri(session), 6301ab079168ccc185408a8691c6b804021d79f7376Hung-ying Tyan SipErrorCode.TIME_OUT, "registration timed out"); 63198cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 63298cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang } 63398cee0ce2354234e72bafb836864ec10a490ea4dChung-yih Wang} 634