Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2012 Savoir-Faire Linux Inc. |
| 3 | * |
| 4 | * Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
| 32 | package com.savoirfairelinux.sflphone.utils; |
| 33 | |
| 34 | import com.savoirfairelinux.sflphone.client.SipCall; |
| 35 | |
| 36 | import android.content.BroadcastReceiver; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 39 | import android.os.Bundle; |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 40 | import android.util.Log; |
| 41 | |
| 42 | import java.util.ArrayList; |
| 43 | |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 44 | import com.savoirfairelinux.sflphone.service.CallManagerCallBack; |
Alexandre Savard | a545d4f | 2012-10-25 16:25:38 -0400 | [diff] [blame] | 45 | import com.savoirfairelinux.sflphone.client.SFLPhoneHome; |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 46 | |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 47 | public class CallList extends BroadcastReceiver |
| 48 | { |
| 49 | static final String TAG = "CallList"; |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 50 | static ArrayList<SipCall> mList = new ArrayList<SipCall>(); |
Alexandre Savard | a545d4f | 2012-10-25 16:25:38 -0400 | [diff] [blame] | 51 | // Requires a reference to the main context when sending call activity |
Alexandre Savard | a949eec | 2012-10-25 17:30:49 -0400 | [diff] [blame] | 52 | static private SFLPhoneHome mHome = null; |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 53 | |
| 54 | private enum Signals { |
| 55 | NEW_CALL_CREATED, |
| 56 | INCOMING_CALL, |
| 57 | INCOMING_MESSAGE, |
| 58 | CALL_STATE_CHANGED |
| 59 | } |
| 60 | |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 61 | /** |
| 62 | * Factory method to create/retreive call instance |
| 63 | */ |
| 64 | public static SipCall getCallInstance(SipCall.CallInfo info) |
| 65 | { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 66 | if(mList.isEmpty()) { |
| 67 | SipCall call = new SipCall(info); |
| 68 | mList.add(call); |
| 69 | return call; |
| 70 | } |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 71 | |
| 72 | for(SipCall sipcall : mList) { |
| 73 | if(sipcall.mCallInfo.mCallID.equals(info.mCallID)) { |
| 74 | return sipcall; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | SipCall call = new SipCall(info); |
| 79 | mList.add(call); |
| 80 | |
| 81 | return call; |
| 82 | } |
| 83 | |
Alexandre Savard | a545d4f | 2012-10-25 16:25:38 -0400 | [diff] [blame] | 84 | public CallList(SFLPhoneHome home) { |
| 85 | mHome = home; |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 88 | @Override |
| 89 | public void onReceive(Context context, Intent intent) |
| 90 | { |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 91 | String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME); |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 92 | Log.d(TAG, "Signal received: " + signalName); |
| 93 | |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 94 | if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) { |
| 95 | } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) { |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 96 | processCallStateChangedSignal(intent); |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 97 | } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 98 | processIncomingCallSignal(intent); |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 99 | } |
| 100 | } |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 101 | |
| 102 | private void processCallStateChangedSignal(Intent intent) { |
| 103 | Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 104 | |
| 105 | SipCall.CallInfo info = new SipCall.CallInfo(); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 106 | info.mCallID = bundle.getString("CallID"); |
| 107 | |
| 108 | String newState = bundle.getString("State"); |
| 109 | |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 110 | SipCall call = getCallInstance(info); |
| 111 | if(newState.equals("INCOMING")) { |
| 112 | call.setCallState(SipCall.CALL_STATE_INCOMING); |
| 113 | } else if(newState.equals("RINGING")) { |
| 114 | call.setCallState(SipCall.CALL_STATE_RINGING); |
| 115 | } else if(newState.equals("CURRENT")) { |
| 116 | call.setCallState(SipCall.CALL_STATE_CURRENT); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 117 | } else if(newState.equals("HUNGUP")) { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 118 | call.setCallState(SipCall.CALL_STATE_HUNGUP); |
Alexandre Savard | a949eec | 2012-10-25 17:30:49 -0400 | [diff] [blame] | 119 | call.hangupUpdateUi(); |
Alexandre Savard | f17e317 | 2012-10-25 16:09:09 -0400 | [diff] [blame] | 120 | mList.remove(call); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 121 | } else if(newState.equals("BUSY")) { |
| 122 | call.setCallState(SipCall.CALL_STATE_BUSY); |
| 123 | } else if(newState.equals("FAILURE")) { |
| 124 | call.setCallState(SipCall.CALL_STATE_FAILURE); |
| 125 | } else if(newState.equals("HOLD")) { |
| 126 | call.setCallState(SipCall.CALL_STATE_HOLD); |
| 127 | } else if(newState.equals("UNHOLD")) { |
Alexandre Savard | e41f521 | 2012-10-26 14:23:50 -0400 | [diff] [blame] | 128 | call.setCallState(SipCall.CALL_STATE_CURRENT); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 129 | } else { |
Alexandre Savard | 27a5113 | 2012-10-25 18:35:14 -0400 | [diff] [blame] | 130 | call.setCallState(SipCall.CALL_STATE_NONE); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 131 | } |
| 132 | } |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 133 | |
| 134 | private void processIncomingCallSignal(Intent intent) { |
| 135 | Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall"); |
| 136 | String accountID = bundle.getString("AccountID"); |
| 137 | String callID = bundle.getString("CallID"); |
| 138 | String from = bundle.getString("From"); |
| 139 | |
| 140 | SipCall.CallInfo info = new SipCall.CallInfo(); |
| 141 | info.mCallID = callID; |
Alexandre Savard | 27a5113 | 2012-10-25 18:35:14 -0400 | [diff] [blame] | 142 | info.mAccountID = accountID; |
Alexandre Savard | 2f1ae54 | 2012-10-26 17:05:00 -0400 | [diff] [blame] | 143 | info.mDisplayName = "Cool Guy!"; |
Alexandre Savard | 27a5113 | 2012-10-25 18:35:14 -0400 | [diff] [blame] | 144 | info.mPhone = from; |
Alexandre Savard | 2f1ae54 | 2012-10-26 17:05:00 -0400 | [diff] [blame] | 145 | info.mEmail = "coolGuy@coolGuy.com"; |
| 146 | info.mCallType = SipCall.CALL_TYPE_INCOMING; |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 147 | |
| 148 | SipCall call = getCallInstance(info); |
Alexandre Savard | a949eec | 2012-10-25 17:30:49 -0400 | [diff] [blame] | 149 | call.receiveCallUpdateUi(); |
Alexandre Savard | a545d4f | 2012-10-25 16:25:38 -0400 | [diff] [blame] | 150 | call.launchCallActivity(mHome); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 151 | } |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 152 | } |