blob: 4bf65b7189cc24a25418209dc67f6533912a67d9 [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 Savarddf544262012-10-25 14:24:08 -040093
94 setCallStateDisplay(mCall.getCallStateString());
Alexandre Savard74c1cad2012-10-24 16:39:00 -040095
Alexandre Savard4f42ade2012-10-24 18:03:31 -040096 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("new-call-created"));
97 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("call-state-changed"));
98 LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter("incoming-call"));
Alexandre Savard6d54bbc2012-10-24 11:04:23 -040099 }
100
101 @Override
102 protected void onDestroy() {
Alexandre Savarddf544262012-10-25 14:24:08 -0400103 Log.i(TAG, "Destroying Call Activity for call " + mCall.getCallId());
Alexandre Savard4f42ade2012-10-24 18:03:31 -0400104 LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
105 unbindService(mConnection);
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400106 super.onDestroy();
107 }
108
109 /** Defines callbacks for service binding, passed to bindService() */
110 private ServiceConnection mConnection = new ServiceConnection() {
111 @Override
112 public void onServiceConnected(ComponentName className, IBinder binder) {
113 service = ISipService.Stub.asInterface(binder);
114 }
115
116 @Override
117 public void onServiceDisconnected(ComponentName arg0) {
118 }
119 };
120
121 @Override
122 public void onClick(View view)
123 {
Alexandre Savard74c1cad2012-10-24 16:39:00 -0400124 Log.i(TAG, "On click action");
Alexandre Savardf17e3172012-10-25 16:09:09 -0400125 switch(view.getId()) {
126 case R.id.buttonanswer:
127 mCall.notifyServiceAnswer(service);
128 break;
129 case R.id.buttonhangup:
Alexandre Savard48240e82012-10-26 10:36:03 -0400130 if((mCall.getCallStateInt() == SipCall.CALL_STATE_NONE) ||
131 (mCall.getCallStateInt() == SipCall.CALL_STATE_CURRENT)) {
132
Alexandre Savard83869852012-10-26 09:14:12 -0400133 mCall.notifyServiceHangup(service);
134 finish();
135 }
136 else if(mCall.getCallStateInt() == SipCall.CALL_STATE_RINGING) {
137 mCall.notifyServiceRefuse(service);
138 finish();
139 }
Alexandre Savardf17e3172012-10-25 16:09:09 -0400140 break;
141 default:
142 Log.e(TAG, "Invalid button clicked");
Alexandre Savard6d54bbc2012-10-24 11:04:23 -0400143 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400144 }
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400145
146 private void processCallStateChangedSignal(Intent intent) {
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400147 Bundle bundle = intent.getBundleExtra("com.savoirfairelinux.sflphone.service.newstate");
148 String callID = bundle.getString("CallID");
149 String newState = bundle.getString("State");
150
151 if(newState.equals("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 Savarddf544262012-10-25 14:24:08 -0400154 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400155 } else if(newState.equals("CURRENT")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400156 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400157 } else if(newState.equals("HUNGUP")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400158 setCallStateDisplay(newState);
Alexandre Savard27a51132012-10-25 18:35:14 -0400159 finish();
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400160 } else if(newState.equals("BUSY")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400161 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400162 } else if(newState.equals("FAILURE")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400163 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400164 } else if(newState.equals("HOLD")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400165 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400166 } else if(newState.equals("UNHOLD")) {
Alexandre Savarddf544262012-10-25 14:24:08 -0400167 setCallStateDisplay(newState);
Alexandre Savardc58c0fd2012-10-25 10:44:08 -0400168 }
169 }
Alexandre Savarddf544262012-10-25 14:24:08 -0400170
171 private void setCallStateDisplay(String newState) {
172 TextView textView = (TextView)findViewById(R.id.callstate);
173 textView.setText("Call State: " + newState);
174 }
Alexandre Savard14323be2012-10-24 10:02:13 -0400175}