summaryrefslogtreecommitdiff
path: root/pjsip-apps/src/pjsua/android/src/org/pjsip/pjsua/MainActivity.java
blob: aa0f98763dc32dd879b186eb07ab15f1f1d11c0f (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* $Id: MainActivity.java $ */
/*
 * Copyright (C) 2008-2011 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.pjsua;

import java.lang.ref.WeakReference;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.util.Log;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

class CONST {
	public static final String LIB_FILENAME = "pjsua";
	public static final String TAG = "pjsua";
	public static final Boolean AUTOKILL_ON_FINISH = true;
	public enum MSG_TYPE {
		STR_DEBUG,
		STR_INFO,
		STR_ERROR,
		CLI_STOP,
		CLI_RESTART,
		QUIT
	};	
}

class LOG {
	public static void DEBUG(Handler h, String str) {
		Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_DEBUG.ordinal(), 
									 str);
		msg.sendToTarget();
	}	
	public static void INFO(Handler h, String str) {
		Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_INFO.ordinal(), 
									 str);
		msg.sendToTarget();
	}
	public static void ERROR(Handler h, String str) {
		Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_ERROR.ordinal(), 
									 str);
		msg.sendToTarget();
	}
}

public class MainActivity extends Activity {
	private MyHandler ui_handler = new MyHandler(this);
	private static MyCallback callback;
	
	private static class MyHandler extends Handler {
		private final WeakReference<MainActivity> mTarget;
		
		public MyHandler(MainActivity target) {
			mTarget = new WeakReference<MainActivity>(target); 
		}
		
		@Override
		public void handleMessage(Message m) {
			MainActivity target = mTarget.get();
			if (target == null)
				return;
			
			if (m.what == CONST.MSG_TYPE.STR_DEBUG.ordinal()) {
				Log.d(CONST.TAG, (String)m.obj);	
			} else if (m.what == CONST.MSG_TYPE.STR_INFO.ordinal()) {
				target.updateStatus((String)m.obj);
				Log.i(CONST.TAG, (String)m.obj);				
			} else if (m.what == CONST.MSG_TYPE.STR_ERROR.ordinal()) {
				target.updateStatus((String)m.obj);
				Log.e(CONST.TAG, (String)m.obj);
			} else if (m.what == CONST.MSG_TYPE.CLI_STOP.ordinal()) {
				pjsua.pjsuaDestroy();
				LOG.INFO(this, "Telnet Unavailable");
			} else if (m.what == CONST.MSG_TYPE.CLI_RESTART.ordinal()) {
				int status = pjsua.pjsuaRestart();
				if (status != 0) {
					LOG.INFO(this, "Failed restarting telnet");
				}
			} else if (m.what == CONST.MSG_TYPE.QUIT.ordinal()) {
				target.finish();
				System.gc();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}
    }
	
	/** Callback object **/
	private static class MyCallback extends PjsuaAppCallback {
		private WeakReference<Handler> ui_handler;
		
		public MyCallback(Handler in_ui_handler) {
			set_ui_handler(in_ui_handler);			
		}
		
		public void set_ui_handler(Handler in_ui_handler) {
			ui_handler = new WeakReference<Handler>(in_ui_handler);
		}		
		
		@Override
	    public void onStarted(String msg) {
			Handler ui = ui_handler.get();
			LOG.INFO(ui, msg);			
		}
		
		@Override
	    public void onStopped(int restart) {
			Handler ui = ui_handler.get();
			/** Use timer to stopped/restart **/
			if (restart != 0) {
				LOG.INFO(ui, "Telnet Restarting");
				Message msg = Message.obtain(ui, 
										  CONST.MSG_TYPE.CLI_RESTART.ordinal());
				ui.sendMessageDelayed(msg, 100);
			} else {
				LOG.INFO(ui, "Telnet Stopping");
				Message msg = Message.obtain(ui, 
											 CONST.MSG_TYPE.CLI_STOP.ordinal());
				ui.sendMessageDelayed(msg, 100);
			}
		}
	}
	
	private void updateStatus(String output) {
        TextView tStatus = (TextView) findViewById(R.id.textStatus);
        tStatus.setText(output);        
	}
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	LOG.DEBUG(ui_handler, "=== Activity::onCreate() ===");
    	super.onCreate(savedInstanceState);
    	
    	init_view();

    	init_lib();
	}

    @Override
    protected void onStart() {
    	LOG.DEBUG(ui_handler, "=== Activity::onStart() ===");
    	super.onStart();
    }
    
    @Override
    protected void onRestart() {
    	LOG.DEBUG(ui_handler, "=== Activity::onRestart() ===");
    	super.onRestart();
    }

    @Override
    protected void onResume() {
    	LOG.DEBUG(ui_handler, "=== Activity::onResume() ===");
    	super.onResume();
    }

    @Override
    protected void onPause() {
    	LOG.DEBUG(ui_handler, "=== Activity::onPause() ===");
    	super.onPause();
    }

    @Override
    protected void onStop() {
    	LOG.DEBUG(ui_handler, "=== Activity::onStop() ===");
    	super.onStop();
    }

    @Override
    protected void onDestroy() {
    	LOG.DEBUG(ui_handler, "=== Activity::onDestroy() ===");
    	super.onDestroy();
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
    }
    
	private void init_view() {
    	setContentView(R.layout.activity_main);
	}
	
	private int init_lib() {        
		LOG.INFO(ui_handler, "Loading module...");
		try {
			System.loadLibrary(CONST.LIB_FILENAME);
		} catch (UnsatisfiedLinkError e) {
			LOG.ERROR(ui_handler, "UnsatisfiedLinkError: " + e.getMessage());
			return -1;
		}
		
		// Wait for GDB to init
		if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) 
		{
			try {
				Thread.sleep(5000);
	        } catch (InterruptedException e) {
	        	LOG.ERROR(ui_handler, "InterruptedException: " + 
	        			  e.getMessage());
	        }
		}
		
		// Set callback object
		if (callback == null)
			callback = new MyCallback(ui_handler);
		
		pjsua.setCallbackObject(callback);

		LOG.INFO(ui_handler, "Starting module..");

		int rc = pjsua.pjsuaStart();
        
		if (rc != 0) {
			LOG.INFO(ui_handler, "Failed starting telnet");
		}
		
		return 0;
	}
}