blob: 8693482e5880258e72478ca51587d5a6fc857453 [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.client;
33
34import android.app.Activity;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040035import android.content.BroadcastReceiver;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040036import android.content.Context;
37import android.content.ComponentName;
38import android.content.Intent;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040039import android.content.IntentFilter;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040040import android.content.ServiceConnection;
Alexandre Savard14323be2012-10-24 10:02:13 -040041import android.os.Bundle;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040042import android.os.IBinder;
43import android.os.RemoteException;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040044import android.support.v4.content.LocalBroadcastManager;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040045import android.util.Log;
46import android.view.View;
47import android.view.View.OnClickListener;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040048import android.widget.TextView;
Alexandre Savard14323be2012-10-24 10:02:13 -040049
50import com.savoirfairelinux.sflphone.R;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040051import com.savoirfairelinux.sflphone.client.SipCall;
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040052import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040053import com.savoirfairelinux.sflphone.service.ISipService;
54import com.savoirfairelinux.sflphone.service.SipService;
Alexandre Savard14323be2012-10-24 10:02:13 -040055
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040056public class CallActivity extends Activity implements OnClickListener
Alexandre Savard14323be2012-10-24 10:02:13 -040057{
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040058 static final String TAG = "CallActivity";
59 private ISipService service;
60 private SipCall mCall;
61
Alexandre Savard4f42ade2012-10-24 18:03:31 -040062 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
63 @Override
64 public void onReceive(Context context, Intent intent) {
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040065 String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040066 Log.d(TAG, "Signal received: " + signalName);
67
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040068 if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) {
69 } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040070 processCallStateChangedSignal(intent);
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040071 } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) {
Alexandre Savard4f42ade2012-10-24 18:03:31 -040072 }
73 }
74 };
75
Alexandre Savard14323be2012-10-24 10:02:13 -040076 @Override
77 protected void onCreate(Bundle savedInstanceState)
78 {
79 super.onCreate(savedInstanceState);
80 setContentView(R.layout.call_activity_layout);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040081
Alexandre Savard557a5742012-10-24 13:54:53 -040082 Bundle b = getIntent().getExtras();
83 // Parcelable value = b.getParcelable("CallInfo");
Alexandre Savardf17e3172012-10-25 16:09:09 -040084 SipCall.CallInfo info = b.getParcelable("CallInfo");
Alexandre Savard557a5742012-10-24 13:54:53 -040085 Log.i(TAG, "Starting activity for call " + info.mCallID);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040086 mCall = new SipCall(info);
Alexandre Savard557a5742012-10-24 13:54:53 -040087
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040088 Intent intent = new Intent(this, SipService.class);
89 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040090
Alexandre Savardf17e3172012-10-25 16:09:09 -040091 findViewById(R.id.buttonanswer).setOnClickListener(this);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040092 findViewById(R.id.buttonhangup).setOnClickListener(this);
Alexandre Savarde9dc8992012-10-26 12:12:27 -040093 findViewById(R.id.buttonhold).setOnClickListener(this);
94 findViewById(R.id.buttonunhold).setOnClickListener(this);
Alexandre Savarddf544262012-10-25 14:24:08 -040095
96 setCallStateDisplay(mCall.getCallStateString());
Alexandre Savard74c1cad2012-10-24 16:39:00 -040097
Alexandre Savard4f42ade2012-10-24 18:03:31 -040098 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("new-call-created"));
99 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("call-state-changed"));
100 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("incoming-call"));
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400101 }
102
103 @Override
104 protected void onDestroy() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400105 Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400106 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
107 unbindService(mConnection);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400108 super.onDestroy();
109 }
110
111 /** Defines callbacks for service binding, passed to bindService() */
112 private ServiceConnection mConnection = new ServiceConnection() {
113 @Override
114 public void onServiceConnected(ComponentName className, IBinder binder) {
115 service = ISipService.Stub.asInterface(binder);
116 }
117
118 @Override
119 public void onServiceDisconnected(ComponentName arg0) {
120 }
121 };
122
123 @Override
124 public void onClick(View view)
125 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400126 Log.i(TAG, "On click action");
Alexandre Savardf17e3172012-10-25 16:09:09 -0400127 switch(view.getId()) {
128 case R.id.buttonanswer:
129 mCall.notifyServiceAnswer(service);
130 break;
131 case R.id.buttonhangup:
Alexandre Savard48240e82012-10-26 10:36:03 -0400132 if((mCall.getCallStateInt() == SipCall.CALL_STATE_NONE) ||
133 (mCall.getCallStateInt() == SipCall.CALL_STATE_CURRENT)) {
Alexandre Savard83869852012-10-26 09:14:12 -0400134 mCall.notifyServiceHangup(service);
135 finish();
136 }
137 else if(mCall.getCallStateInt() == SipCall.CALL_STATE_RINGING) {
138 mCall.notifyServiceRefuse(service);
139 finish();
140 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400141 break;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400142 case R.id.buttonhold:
143 Log.i(TAG, "HOLDBUTTONCLICK call state " + mCall.getCallStateString());
144 if(mCall.getCallStateInt() == SipCall.CALL_STATE_CURRENT) {
145 mCall.notifyServiceHold(service);
146 }
147 break;
148 case R.id.buttonunhold:
149 Log.i(TAG, "UNHOLDBUTTONCLICK call state " + mCall.getCallStateString());
150 if(mCall.getCallStateInt() == SipCall.CALL_STATE_HOLD) {
151 mCall.notifyServiceUnhold(service);
152 }
153 break;
Alexandre Savardf17e3172012-10-25 16:09:09 -0400154 default:
155 Log.e(TAG, "Invalid button clicked");
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400156 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400157 }
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400158
159 private void processCallStateChangedSignal(Intent intent) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400160 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
161 String callID = bundle.getString("CallID");
162 String newState = bundle.getString("State");
163
164 if(newState.equals("INCOMING")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400165 mCall.setCallState(SipCall.CALL_STATE_INCOMING);
Alexandre Savarddf544262012-10-25 14:24:08 -0400166 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400167 } else if(newState.equals("RINGING")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400168 mCall.setCallState(SipCall.CALL_STATE_RINGING);
Alexandre Savarddf544262012-10-25 14:24:08 -0400169 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400170 } else if(newState.equals("CURRENT")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400171 mCall.setCallState(SipCall.CALL_STATE_CURRENT);
Alexandre Savarddf544262012-10-25 14:24:08 -0400172 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400173 } else if(newState.equals("HUNGUP")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400174 mCall.setCallState(SipCall.CALL_STATE_HUNGUP);
Alexandre Savarddf544262012-10-25 14:24:08 -0400175 setCallStateDisplay(newState);
Alexandre Savard27a51132012-10-25 18:35:14 -0400176 finish();
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400177 } else if(newState.equals("BUSY")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400178 mCall.setCallState(SipCall.CALL_STATE_BUSY);
Alexandre Savarddf544262012-10-25 14:24:08 -0400179 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400180 } else if(newState.equals("FAILURE")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400181 mCall.setCallState(SipCall.CALL_STATE_FAILURE);
Alexandre Savarddf544262012-10-25 14:24:08 -0400182 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400183 } else if(newState.equals("HOLD")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400184 mCall.setCallState(SipCall.CALL_STATE_HOLD);
Alexandre Savarddf544262012-10-25 14:24:08 -0400185 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400186 } else if(newState.equals("UNHOLD")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400187 mCall.setCallState(SipCall.CALL_STATE_UNHOLD);
Alexandre Savarddf544262012-10-25 14:24:08 -0400188 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400189 }
190 }
Alexandre Savarddf544262012-10-25 14:24:08 -0400191
192 private void setCallStateDisplay(String newState) {
193 TextView textView = (TextView)findViewById(R.id.callstate);
194 textView.setText("Call State: " + newState);
195 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400196}