summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/swig/java/android/src/org/pjsip/pjsua2/app/CallActivity.java
blob: 48e1bad3c7ed79cfff3245a5eb20a4a08cb958fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* $Id$ */
/*
 * Copyright (C) 2013 Teluu Inc. (http://www.teluu.com)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.pjsip.pjsua2.app;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.app.Activity;

import org.pjsip.pjsua2.*;

public class CallActivity extends Activity implements Handler.Callback {
	
	public static Handler handler_;

	private final Handler handler = new Handler(this);
	private static CallInfo lastCallInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_call);
		
		handler_ = handler;
		if (MainActivity.currentCall != null) {
			try {
				lastCallInfo = MainActivity.currentCall.getInfo();
				updateCallState(lastCallInfo);
			} catch (Exception e) {
				System.out.println(e);
			}
		} else {
			updateCallState(lastCallInfo);
		}
	}

    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	handler_ = null;
    }
	
	public void acceptCall(View view) {
		CallOpParam prm = new CallOpParam();
		prm.setStatusCode(pjsip_status_code.PJSIP_SC_OK);
		try {
			MainActivity.currentCall.answer(prm);
		} catch (Exception e) {
			System.out.println(e);
		}
		
		view.setVisibility(View.GONE);
	}

	public void hangupCall(View view) {
		handler_ = null;
		finish();
		
		if (MainActivity.currentCall != null) {
			CallOpParam prm = new CallOpParam();
			prm.setStatusCode(pjsip_status_code.PJSIP_SC_DECLINE);
			try {
				MainActivity.currentCall.hangup(prm);
			} catch (Exception e) {
				System.out.println(e);
			}

			MainActivity.currentCall = null;
		}
	}
	
	@Override
	public boolean handleMessage(Message m) {
		
		if (m.what == MainActivity.MSG_TYPE.CALL_STATE) {
			
			lastCallInfo = (CallInfo) m.obj;
			updateCallState(lastCallInfo);
			
		} else {
			
			/* Message not handled */
			return false;
			
		}
			
		return true;
	}
	
	private void updateCallState(CallInfo ci) {
		TextView tvPeer  = (TextView) findViewById(R.id.textViewPeer);
		TextView tvState = (TextView) findViewById(R.id.textViewCallState);
		Button buttonHangup = (Button) findViewById(R.id.buttonHangup);
		Button buttonAccept = (Button) findViewById(R.id.buttonAccept);
		String call_state = "";
		
		if (ci.getRole() == pjsip_role_e.PJSIP_ROLE_UAC) {
			buttonAccept.setVisibility(View.GONE);
		}
				
		if (ci.getState().swigValue() < pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED.swigValue())
		{
			if (ci.getRole() == pjsip_role_e.PJSIP_ROLE_UAS) {
				call_state = "Incoming call..";
				/* Default button texts are already 'Accept' & 'Reject' */
			} else {
				buttonHangup.setText("Cancel");
				call_state = ci.getStateText();
			}
		}
		else if (ci.getState().swigValue() >= pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED.swigValue())
		{
			buttonAccept.setVisibility(View.GONE);
			call_state = ci.getStateText();
			if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_CONFIRMED) {
				buttonHangup.setText("Hangup");
			} else if (ci.getState() == pjsip_inv_state.PJSIP_INV_STATE_DISCONNECTED) {
				buttonHangup.setText("OK");
				call_state = "Call disconnected: " + ci.getLastReason();
				MainActivity.currentCall = null;
			}
		}
		
		tvPeer.setText(ci.getRemoteUri());
		tvState.setText(call_state);
	}
}