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; |
| 45 | |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 46 | public class CallList extends BroadcastReceiver |
| 47 | { |
| 48 | static final String TAG = "CallList"; |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 49 | static ArrayList<SipCall> mList = new ArrayList<SipCall>(); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 50 | private Context mContext = null; |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 51 | |
| 52 | private enum Signals { |
| 53 | NEW_CALL_CREATED, |
| 54 | INCOMING_CALL, |
| 55 | INCOMING_MESSAGE, |
| 56 | CALL_STATE_CHANGED |
| 57 | } |
| 58 | |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 59 | /** |
| 60 | * Factory method to create/retreive call instance |
| 61 | */ |
| 62 | public static SipCall getCallInstance(SipCall.CallInfo info) |
| 63 | { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 64 | if(mList.isEmpty()) { |
| 65 | SipCall call = new SipCall(info); |
| 66 | mList.add(call); |
| 67 | return call; |
| 68 | } |
Alexandre Savard | 74c1cad | 2012-10-24 16:39:00 -0400 | [diff] [blame] | 69 | |
| 70 | for(SipCall sipcall : mList) { |
| 71 | if(sipcall.mCallInfo.mCallID.equals(info.mCallID)) { |
| 72 | return sipcall; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | SipCall call = new SipCall(info); |
| 77 | mList.add(call); |
| 78 | |
| 79 | return call; |
| 80 | } |
| 81 | |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 82 | public CallList(Context context) { |
| 83 | mContext = context; |
| 84 | } |
| 85 | |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 86 | @Override |
| 87 | public void onReceive(Context context, Intent intent) |
| 88 | { |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 89 | String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME); |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 90 | Log.d(TAG, "Signal received: " + signalName); |
| 91 | |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 92 | if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) { |
| 93 | } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) { |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 94 | processCallStateChangedSignal(intent); |
Alexandre Savard | 3bdce7b | 2012-10-24 18:27:45 -0400 | [diff] [blame] | 95 | } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 96 | processIncomingCallSignal(intent); |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 97 | } |
| 98 | } |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 99 | |
| 100 | private void processCallStateChangedSignal(Intent intent) { |
| 101 | Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate"); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 102 | |
| 103 | SipCall.CallInfo info = new SipCall.CallInfo(); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 104 | info.mCallID = bundle.getString("CallID"); |
| 105 | |
| 106 | String newState = bundle.getString("State"); |
| 107 | |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 108 | SipCall call = getCallInstance(info); |
| 109 | if(newState.equals("INCOMING")) { |
| 110 | call.setCallState(SipCall.CALL_STATE_INCOMING); |
| 111 | } else if(newState.equals("RINGING")) { |
| 112 | call.setCallState(SipCall.CALL_STATE_RINGING); |
| 113 | } else if(newState.equals("CURRENT")) { |
| 114 | call.setCallState(SipCall.CALL_STATE_CURRENT); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 115 | } else if(newState.equals("HUNGUP")) { |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 116 | call.setCallState(SipCall.CALL_STATE_HUNGUP); |
Alexandre Savard | f17e317 | 2012-10-25 16:09:09 -0400 | [diff] [blame^] | 117 | call.hangup(); |
| 118 | mList.remove(call); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 119 | } else if(newState.equals("BUSY")) { |
| 120 | call.setCallState(SipCall.CALL_STATE_BUSY); |
| 121 | } else if(newState.equals("FAILURE")) { |
| 122 | call.setCallState(SipCall.CALL_STATE_FAILURE); |
| 123 | } else if(newState.equals("HOLD")) { |
| 124 | call.setCallState(SipCall.CALL_STATE_HOLD); |
| 125 | } else if(newState.equals("UNHOLD")) { |
| 126 | call.setCallState(SipCall.CALL_STATE_UNHOLD); |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 127 | } else { |
| 128 | call.setCallState(SipCall.CALL_STATE_NULL); |
Alexandre Savard | c58c0fd | 2012-10-25 10:44:08 -0400 | [diff] [blame] | 129 | } |
| 130 | } |
Alexandre Savard | 73bc56f | 2012-10-25 13:35:54 -0400 | [diff] [blame] | 131 | |
| 132 | private void processIncomingCallSignal(Intent intent) { |
| 133 | Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newcall"); |
| 134 | String accountID = bundle.getString("AccountID"); |
| 135 | String callID = bundle.getString("CallID"); |
| 136 | String from = bundle.getString("From"); |
| 137 | |
| 138 | SipCall.CallInfo info = new SipCall.CallInfo(); |
| 139 | info.mCallID = callID; |
| 140 | |
| 141 | SipCall call = getCallInstance(info); |
| 142 | call.placeCall(); |
| 143 | call.launchCallActivity(mContext); |
| 144 | } |
Alexandre Savard | 14323be | 2012-10-24 10:02:13 -0400 | [diff] [blame] | 145 | } |