blob: 273ef5caaf92001ccb65eb379322f1cb1164bb70 [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.ComponentName;
alision17052d42013-04-22 10:39:38 -040037import android.content.Context;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040038import 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;
Alexandre Savard4f42ade2012-10-24 18:03:31 -040043import android.support.v4.content.LocalBroadcastManager;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040044import android.util.Log;
45import android.view.View;
46import android.view.View.OnClickListener;
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040047import android.widget.TextView;
Alexandre Savard14323be2012-10-24 10:02:13 -040048
49import com.savoirfairelinux.sflphone.R;
alisionf76de3b2013-04-16 15:35:22 -040050import com.savoirfairelinux.sflphone.model.SipCall;
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040051import com.savoirfairelinux.sflphone.service.CallManagerCallBack;
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040052import com.savoirfairelinux.sflphone.service.ISipService;
53import com.savoirfairelinux.sflphone.service.SipService;
Alexandre Savard14323be2012-10-24 10:02:13 -040054
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040055public class CallActivity extends Activity implements OnClickListener
Alexandre Savard14323be2012-10-24 10:02:13 -040056{
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040057 static final String TAG = "CallActivity";
58 private ISipService service;
59 private SipCall mCall;
60
Alexandre Savard4f42ade2012-10-24 18:03:31 -040061 private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
62 @Override
63 public void onReceive(Context context, Intent intent) {
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040064 String signalName = intent.getStringExtra(CallManagerCallBack.SIGNAL_NAME);
Alexandre Savard4f42ade2012-10-24 18:03:31 -040065 Log.d(TAG, "Signal received: " + signalName);
66
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040067 if(signalName.equals(CallManagerCallBack.NEW_CALL_CREATED)) {
68 } else if(signalName.equals(CallManagerCallBack.CALL_STATE_CHANGED)) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -040069 processCallStateChangedSignal(intent);
Alexandre Savard3bdce7b2012-10-24 18:27:45 -040070 } else if(signalName.equals(CallManagerCallBack.INCOMING_CALL)) {
Alexandre Savard4f42ade2012-10-24 18:03:31 -040071 }
72 }
73 };
74
Alexandre Savard14323be2012-10-24 10:02:13 -040075 @Override
76 protected void onCreate(Bundle savedInstanceState)
77 {
78 super.onCreate(savedInstanceState);
alisionf76de3b2013-04-16 15:35:22 -040079 setContentView(R.layout.activity_call_layout);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040080
Alexandre Savard557a5742012-10-24 13:54:53 -040081 Bundle b = getIntent().getExtras();
82 // Parcelable value = b.getParcelable("CallInfo");
Alexandre Savardf17e3172012-10-25 16:09:09 -040083 SipCall.CallInfo info = b.getParcelable("CallInfo");
Alexandre Savard557a5742012-10-24 13:54:53 -040084 Log.i(TAG, "Starting activity for call " + info.mCallID);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040085 mCall = new SipCall(info);
Alexandre Savard557a5742012-10-24 13:54:53 -040086
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040087 Intent intent = new Intent(this, SipService.class);
88 bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040089
Alexandre Savardf17e3172012-10-25 16:09:09 -040090 findViewById(R.id.buttonanswer).setOnClickListener(this);
Alexandre Savard74c1cad2012-10-24 16:39:00 -040091 findViewById(R.id.buttonhangup).setOnClickListener(this);
Alexandre Savarde9dc8992012-10-26 12:12:27 -040092 findViewById(R.id.buttonhold).setOnClickListener(this);
93 findViewById(R.id.buttonunhold).setOnClickListener(this);
Alexandre Savarddf544262012-10-25 14:24:08 -040094
95 setCallStateDisplay(mCall.getCallStateString());
Alexandre Savard74c1cad2012-10-24 16:39:00 -040096
Alexandre Savard4f42ade2012-10-24 18:03:31 -040097 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("new-call-created"));
98 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("call-state-changed"));
99 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("incoming-call"));
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400100 }
101
102 @Override
103 protected void onDestroy() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400104 Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400105 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
106 unbindService(mConnection);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400107 super.onDestroy();
108 }
109
110 /** Defines callbacks for service binding, passed to bindService() */
111 private ServiceConnection mConnection = new ServiceConnection() {
112 @Override
113 public void onServiceConnected(ComponentName className, IBinder binder) {
114 service = ISipService.Stub.asInterface(binder);
115 }
116
117 @Override
118 public void onServiceDisconnected(ComponentName arg0) {
119 }
120 };
121
122 @Override
123 public void onClick(View view)
124 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400125 Log.i(TAG, "On click action");
Alexandre Savardf17e3172012-10-25 16:09:09 -0400126 switch(view.getId()) {
127 case R.id.buttonanswer:
128 mCall.notifyServiceAnswer(service);
129 break;
130 case R.id.buttonhangup:
Alexandre Savard08545ae2012-10-26 16:11:42 -0400131 if(mCall.notifyServiceHangup(service))
Alexandre Savard83869852012-10-26 09:14:12 -0400132 finish();
Alexandre Savardf17e3172012-10-25 16:09:09 -0400133 break;
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400134 case R.id.buttonhold:
Alexandre Savard08545ae2012-10-26 16:11:42 -0400135 mCall.notifyServiceHold(service);
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400136 break;
137 case R.id.buttonunhold:
Alexandre Savard08545ae2012-10-26 16:11:42 -0400138 mCall.notifyServiceUnhold(service);
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400139 break;
Alexandre Savardf17e3172012-10-25 16:09:09 -0400140 default:
141 Log.e(TAG, "Invalid button clicked");
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400142 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400143 }
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400144
145 private void processCallStateChangedSignal(Intent intent) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400146 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
147 String callID = bundle.getString("CallID");
148 String newState = bundle.getString("State");
149
150 if(newState.equals("INCOMING")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400151 mCall.setCallState(SipCall.CALL_STATE_INCOMING);
Alexandre Savarddf544262012-10-25 14:24:08 -0400152 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400153 } else if(newState.equals("RINGING")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400154 mCall.setCallState(SipCall.CALL_STATE_RINGING);
Alexandre Savarddf544262012-10-25 14:24:08 -0400155 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400156 } else if(newState.equals("CURRENT")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400157 mCall.setCallState(SipCall.CALL_STATE_CURRENT);
Alexandre Savarddf544262012-10-25 14:24:08 -0400158 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400159 } else if(newState.equals("HUNGUP")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400160 mCall.setCallState(SipCall.CALL_STATE_HUNGUP);
Alexandre Savarddf544262012-10-25 14:24:08 -0400161 setCallStateDisplay(newState);
Alexandre Savard27a51132012-10-25 18:35:14 -0400162 finish();
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400163 } else if(newState.equals("BUSY")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400164 mCall.setCallState(SipCall.CALL_STATE_BUSY);
Alexandre Savarddf544262012-10-25 14:24:08 -0400165 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400166 } else if(newState.equals("FAILURE")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400167 mCall.setCallState(SipCall.CALL_STATE_FAILURE);
Alexandre Savarddf544262012-10-25 14:24:08 -0400168 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400169 } else if(newState.equals("HOLD")) {
Alexandre Savarde9dc8992012-10-26 12:12:27 -0400170 mCall.setCallState(SipCall.CALL_STATE_HOLD);
Alexandre Savarddf544262012-10-25 14:24:08 -0400171 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400172 } else if(newState.equals("UNHOLD")) {
Alexandre Savarde41f5212012-10-26 14:23:50 -0400173 mCall.setCallState(SipCall.CALL_STATE_CURRENT);
174 setCallStateDisplay("CURRENT");
175 } else {
176 mCall.setCallState(SipCall.CALL_STATE_NONE);
Alexandre Savarddf544262012-10-25 14:24:08 -0400177 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400178 }
Alexandre Savarde41f5212012-10-26 14:23:50 -0400179
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400180 }
Alexandre Savarddf544262012-10-25 14:24:08 -0400181
182 private void setCallStateDisplay(String newState) {
183 TextView textView = (TextView)findViewById(R.id.callstate);
184 textView.setText("Call State: " + newState);
185 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400186}