blob: e5feab33594f1d7b44d385d552780211da9cddea [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>
Adrien Béraud71b2f812013-04-26 18:51:02 +10005 * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
Alexandre Savard14323be2012-10-24 10:02:13 -04006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 * Additional permission under GNU GPL version 3 section 7:
22 *
23 * If you modify this program, or any covered work, by linking or
24 * combining it with the OpenSSL project's OpenSSL library (or a
25 * modified version of that library), containing parts covered by the
26 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27 * grants you additional permission to convey the resulting work.
28 * Corresponding Source for a non-source form of such a combination
29 * shall include the source code for the parts of OpenSSL used as well
30 * as that of the covered work.
31 */
32
33package com.savoirfairelinux.sflphone.client;
34
35import android.app.Activity;
Adrien Béraud29556042013-04-26 17:35:43 +100036import android.app.Fragment;
Adrien Béraud71b2f812013-04-26 18:51:02 +100037import android.app.FragmentManager;
Adrien Béraud7b50ba22013-04-26 23:57:43 +100038import android.app.FragmentTransaction;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040039import android.content.BroadcastReceiver;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040040import android.content.ComponentName;
alision17052d42013-04-22 10:39:38 -040041import android.content.Context;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040042import android.content.Intent;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040043import android.content.IntentFilter;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040044import android.content.ServiceConnection;
Alexandre Savard14323be2012-10-24 10:02:13 -040045import android.os.Bundle;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040046import android.os.IBinder;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040047import android.support.v4.content.LocalBroadcastManager;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040048import android.util.Log;
Adrien Béraud29556042013-04-26 17:35:43 +100049import android.view.LayoutInflater;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040050import android.view.View;
Adrien Béraud29556042013-04-26 17:35:43 +100051import android.view.ViewGroup;
52import android.widget.EditText;
Alexandre Savard14323be2012-10-24 10:02:13 -040053
54import com.savoirfairelinux.sflphone.R;
alision4a0eb092013-05-07 13:52:03 -040055import com.savoirfairelinux.sflphone.fragments.IncomingCallFragment;
56import com.savoirfairelinux.sflphone.fragments.OngoingCallFragment;
alisionf76de3b2013-04-16 15:35:22 -040057import com.savoirfairelinux.sflphone.model.SipCall;
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040058import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040059import com.savoirfairelinux.sflphone.service.ISipService;
60import com.savoirfairelinux.sflphone.service.SipService;
Alexandre Savard14323be2012-10-24 10:02:13 -040061
Adrien Béraud71b2f812013-04-26 18:51:02 +100062public class CallActivity extends Activity //implements IncomingCallFragment.ICallActionListener, OngoingCallFragment.ICallActionListener //OnClickListener
Alexandre Savard14323be2012-10-24 10:02:13 -040063{
Adrien Béraudc99b8432013-04-25 16:27:46 +100064 static final String TAG = "CallActivity";
65 private ISipService service;
66 private SipCall mCall;
Adrien Béraud7b50ba22013-04-26 23:57:43 +100067
68 public interface CallFragment
69 {
Adrien Béraud71b2f812013-04-26 18:51:02 +100070 void setCall(SipCall c);
71 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040072
Adrien Béraudc99b8432013-04-25 16:27:46 +100073 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
74 @Override
75 public void onReceive(Context context, Intent intent)
76 {
77 String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME);
78 Log.d(TAG, "Signal received: " + signalName);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040079
Adrien Béraudc99b8432013-04-25 16:27:46 +100080 if (signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) {
81 } else if (signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) {
82 processCallStateChangedSignal(intent);
83 } else if (signalName.equals(CallManagerCallBack.INCOMING_CALL)) {
84 }
85 }
86 };
Alexandre Savard4f42ade2012-10-24 18:03:31 -040087
Adrien Béraudc99b8432013-04-25 16:27:46 +100088 @Override
89 protected void onCreate(Bundle savedInstanceState)
90 {
91 super.onCreate(savedInstanceState);
92 setContentView(R.layout.activity_call_layout);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040093
Adrien Béraudc99b8432013-04-25 16:27:46 +100094 Bundle b = getIntent().getExtras();
95 // Parcelable value = b.getParcelable("CallInfo");
96 SipCall.CallInfo info = b.getParcelable("CallInfo");
97 Log.i(TAG, "Starting activity for call " + info.mCallID);
98 mCall = new SipCall(info);
Alexandre Savard557a5742012-10-24 13:54:53 -040099
Adrien Béraudc99b8432013-04-25 16:27:46 +1000100 Intent intent = new Intent(this, SipService.class);
101 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Adrien Béraudc99b8432013-04-25 16:27:46 +1000102 setCallStateDisplay(mCall.getCallStateString());
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400103
Adrien Béraudc99b8432013-04-25 16:27:46 +1000104 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(CallManagerCallBack.NEW_CALL_CREATED));
105 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(CallManagerCallBack.CALL_STATE_CHANGED));
106 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(CallManagerCallBack.INCOMING_CALL));
107 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400108
Adrien Béraudc99b8432013-04-25 16:27:46 +1000109 @Override
110 protected void onDestroy()
111 {
112 Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
113 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
114 unbindService(mConnection);
115 super.onDestroy();
116 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400117
Adrien Béraudc99b8432013-04-25 16:27:46 +1000118 /** Defines callbacks for service binding, passed to bindService() */
119 private ServiceConnection mConnection = new ServiceConnection() {
120 @Override
121 public void onServiceConnected(ComponentName className, IBinder binder)
122 {
123 service = ISipService.Stub.asInterface(binder);
124 }
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400125
Adrien Béraudc99b8432013-04-25 16:27:46 +1000126 @Override
127 public void onServiceDisconnected(ComponentName arg0)
128 {
129 }
130 };
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000131
Adrien Béraudc99b8432013-04-25 16:27:46 +1000132 private void processCallStateChangedSignal(Intent intent)
133 {
134 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
135 String callID = bundle.getString("CallID");
136 String newState = bundle.getString("State");
Alexandre Savarde41f5212012-10-26 14:23:50 -0400137
Adrien Béraudc99b8432013-04-25 16:27:46 +1000138 if (newState.equals("INCOMING")) {
139 mCall.setCallState(SipCall.CALL_STATE_INCOMING);
140 setCallStateDisplay(newState);
141 } else if (newState.equals("RINGING")) {
142 mCall.setCallState(SipCall.CALL_STATE_RINGING);
143 setCallStateDisplay(newState);
144 } else if (newState.equals("CURRENT")) {
145 mCall.setCallState(SipCall.CALL_STATE_CURRENT);
146 setCallStateDisplay(newState);
147 } else if (newState.equals("HUNGUP")) {
148 mCall.setCallState(SipCall.CALL_STATE_HUNGUP);
149 setCallStateDisplay(newState);
150 finish();
151 } else if (newState.equals("BUSY")) {
152 mCall.setCallState(SipCall.CALL_STATE_BUSY);
153 setCallStateDisplay(newState);
154 } else if (newState.equals("FAILURE")) {
155 mCall.setCallState(SipCall.CALL_STATE_FAILURE);
156 setCallStateDisplay(newState);
157 } else if (newState.equals("HOLD")) {
158 mCall.setCallState(SipCall.CALL_STATE_HOLD);
159 setCallStateDisplay(newState);
160 } else if (newState.equals("UNHOLD")) {
161 mCall.setCallState(SipCall.CALL_STATE_CURRENT);
162 setCallStateDisplay("CURRENT");
163 } else {
164 mCall.setCallState(SipCall.CALL_STATE_NONE);
165 setCallStateDisplay(newState);
166 }
Alexandre Savarddf544262012-10-25 14:24:08 -0400167
Adrien Béraud29556042013-04-26 17:35:43 +1000168 Log.w(TAG, "processCallStateChangedSignal " + newState);
169
Adrien Béraudc99b8432013-04-25 16:27:46 +1000170 }
171
172 private void setCallStateDisplay(String newState)
173 {
Adrien Béraud29556042013-04-26 17:35:43 +1000174 if (newState == null || newState.equals("NULL")) {
175 newState = "INCOMING";
176 }
177
178 Log.w(TAG, "setCallStateDisplay " + newState);
Adrien Béraud71b2f812013-04-26 18:51:02 +1000179
Adrien Béraud29556042013-04-26 17:35:43 +1000180 mCall.printCallInfo();
181
Adrien Béraud71b2f812013-04-26 18:51:02 +1000182 FragmentManager fm = getFragmentManager();
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000183 Fragment newf, f = fm.findFragmentByTag("call_fragment");
Adrien Béraud71b2f812013-04-26 18:51:02 +1000184 boolean replace = true;
185 if (newState.equals("INCOMING") && !(f instanceof IncomingCallFragment)) {
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000186 newf = new IncomingCallFragment();
Adrien Béraud71b2f812013-04-26 18:51:02 +1000187 } else if (!newState.equals("INCOMING") && !(f instanceof OngoingCallFragment)) {
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000188 newf = new OngoingCallFragment();
Adrien Béraud71b2f812013-04-26 18:51:02 +1000189 } else {
190 replace = false;
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000191 newf = f;
Adrien Béraud29556042013-04-26 17:35:43 +1000192 }
Adrien Béraud7b50ba22013-04-26 23:57:43 +1000193
194 ((CallFragment) newf).setCall(mCall);
195
196 if (replace) {
197 FragmentTransaction ft = fm.beginTransaction();
198 if(f != null) // do not animate if there is no previous fragment
199 ft.setCustomAnimations(R.animator.slide_in, R.animator.slide_out);
200 //ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
201 ft.replace(R.id.fragment_layout, newf, "call_fragment").commit();
202 }
Adrien Béraudc99b8432013-04-25 16:27:46 +1000203 }
Adrien Béraud29556042013-04-26 17:35:43 +1000204
Adrien Béraud29556042013-04-26 17:35:43 +1000205 public void onCallAccepted()
206 {
207 mCall.notifyServiceAnswer(service);
208 }
209
Adrien Béraud29556042013-04-26 17:35:43 +1000210 public void onCallRejected()
211 {
212 if (mCall.notifyServiceHangup(service))
213 finish();
214 }
215
Adrien Béraud71b2f812013-04-26 18:51:02 +1000216 public void onCallEnded()
217 {
218 if (mCall.notifyServiceHangup(service))
219 finish();
220 }
221
222 public void onCallSuspended()
223 {
224 mCall.notifyServiceHold(service);
225 }
226
227 public void onCallResumed()
228 {
229 mCall.notifyServiceUnhold(service);
230 }
231
alision7f18fc82013-05-01 09:37:33 -0400232 public void onCalltransfered(String to) {
233 mCall.notifyServiceTransfer(service, to);
234
235 }
236
alision04a00182013-05-10 17:05:29 -0400237 public void onRecordCall() {
238 mCall.notifyServiceRecord(service);
239
240 }
241
242 public void onSendMessage(String msg) {
243 mCall.notifyServiceSendMsg(service,msg);
244
245 }
246
Alexandre Savard14323be2012-10-24 10:02:13 -0400247}