blob: 1bf60e0e037314aedabeb1952970cf4477b93d1b [file] [log] [blame]
Alexandre Savard14323be2012-10-24 10:02:13 -04001/*
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
32package com.savoirfairelinux.sflphone.utils;
33
34import com.savoirfairelinux.sflphone.client.SipCall;
35
36import android.content.BroadcastReceiver;
37import android.content.Context;
38import android.content.Intent;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040039import android.os.Bundle;
Alexandre Savard14323be2012-10-24 10:02:13 -040040import android.util.Log;
41
42import java.util.ArrayList;
43
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040044import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savarda545d4f2012-10-25 16:25:38 -040045import com.savoirfairelinux.sflphone.client.SFLPhoneHome;
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040046
Alexandre Savard14323be2012-10-24 10:02:13 -040047public class CallList extends BroadcastReceiver
48{
49 static final String TAG = "CallList";
Alexandre Savard74c1cad2012-10-24 16:39:00 -040050 static ArrayList<SipCall> mList = new ArrayList<SipCall>();
Alexandre Savarda545d4f2012-10-25 16:25:38 -040051 // Requires a reference to the main context when sending call activity
Alexandre Savarda949eec2012-10-25 17:30:49 -040052 static private SFLPhoneHome mHome = null;
Alexandre Savard14323be2012-10-24 10:02:13 -040053
54 private enum Signals {
55 NEW_CALL_CREATED,
56 INCOMING_CALL,
57 INCOMING_MESSAGE,
58 CALL_STATE_CHANGED
59 }
60
Alexandre Savard74c1cad2012-10-24 16:39:00 -040061 /**
62 * Factory method to create/retreive call instance
63 */
64 public static SipCall getCallInstance(SipCall.CallInfo info)
65 {
Alexandre Savard73bc56f2012-10-25 13:35:54 -040066 if(mList.isEmpty()) {
67 SipCall call = new SipCall(info);
68 mList.add(call);
69 return call;
70 }
Alexandre Savard74c1cad2012-10-24 16:39:00 -040071
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 Savarda545d4f2012-10-25 16:25:38 -040084 public CallList(SFLPhoneHome home) {
85 mHome = home;
Alexandre Savard73bc56f2012-10-25 13:35:54 -040086 }
87
Alexandre Savard14323be2012-10-24 10:02:13 -040088 @Override
89 public void onReceive(Context context, Intent intent)
90 {
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040091 String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME);
Alexandre Savard14323be2012-10-24 10:02:13 -040092 Log.d(TAG, "Signal received: " + signalName);
93
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040094 if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) {
95 } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040096 processCallStateChangedSignal(intent);
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040097 } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) {
Alexandre Savard73bc56f2012-10-25 13:35:54 -040098 processIncomingCallSignal(intent);
Alexandre Savard14323be2012-10-24 10:02:13 -040099 }
100 }
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400101
102 private void processCallStateChangedSignal(Intent intent) {
103 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400104
105 SipCall.CallInfo info = new SipCall.CallInfo();
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400106 info.mCallID = bundle.getString("CallID");
107
108 String newState = bundle.getString("State");
109
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400110 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 Savard73bc56f2012-10-25 13:35:54 -0400117 } else if(newState.equals("HUNGUP")) {
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400118 call.setCallState(SipCall.CALL_STATE_HUNGUP);
Alexandre Savarda949eec2012-10-25 17:30:49 -0400119 call.hangupUpdateUi();
Alexandre Savardf17e3172012-10-25 16:09:09 -0400120 mList.remove(call);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400121 } 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 Savarde41f5212012-10-26 14:23:50 -0400128 call.setCallState(SipCall.CALL_STATE_CURRENT);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400129 } else {
Alexandre Savard27a51132012-10-25 18:35:14 -0400130 call.setCallState(SipCall.CALL_STATE_NONE);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400131 }
132 }
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400133
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 Savard27a51132012-10-25 18:35:14 -0400142 info.mAccountID = accountID;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400143 info.mDisplayName = "Cool Guy!";
Alexandre Savard27a51132012-10-25 18:35:14 -0400144 info.mPhone = from;
Alexandre Savard2f1ae542012-10-26 17:05:00 -0400145 info.mEmail = "coolGuy@coolGuy.com";
146 info.mCallType = SipCall.CALL_TYPE_INCOMING;
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400147
148 SipCall call = getCallInstance(info);
Alexandre Savarda949eec2012-10-25 17:30:49 -0400149 call.receiveCallUpdateUi();
Alexandre Savarda545d4f2012-10-25 16:25:38 -0400150 call.launchCallActivity(mHome);
Alexandre Savard73bc56f2012-10-25 13:35:54 -0400151 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400152}