blob: aa0f98763dc32dd879b186eb07ab15f1f1d11c0f [file] [log] [blame]
Tristan Matthews0a329cc2013-07-17 13:20:14 -04001/* $Id: MainActivity.java $ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20package org.pjsip.pjsua;
21
22import java.lang.ref.WeakReference;
23
24import android.app.Activity;
25import android.content.pm.ApplicationInfo;
26import android.os.Bundle;
27import android.util.Log;
28import android.os.Handler;
29import android.os.Message;
30import android.widget.TextView;
31
32class CONST {
33 public static final String LIB_FILENAME = "pjsua";
34 public static final String TAG = "pjsua";
35 public static final Boolean AUTOKILL_ON_FINISH = true;
36 public enum MSG_TYPE {
37 STR_DEBUG,
38 STR_INFO,
39 STR_ERROR,
40 CLI_STOP,
41 CLI_RESTART,
42 QUIT
43 };
44}
45
46class LOG {
47 public static void DEBUG(Handler h, String str) {
48 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_DEBUG.ordinal(),
49 str);
50 msg.sendToTarget();
51 }
52 public static void INFO(Handler h, String str) {
53 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_INFO.ordinal(),
54 str);
55 msg.sendToTarget();
56 }
57 public static void ERROR(Handler h, String str) {
58 Message msg = Message.obtain(h, CONST.MSG_TYPE.STR_ERROR.ordinal(),
59 str);
60 msg.sendToTarget();
61 }
62}
63
64public class MainActivity extends Activity {
65 private MyHandler ui_handler = new MyHandler(this);
66 private static MyCallback callback;
67
68 private static class MyHandler extends Handler {
69 private final WeakReference<MainActivity> mTarget;
70
71 public MyHandler(MainActivity target) {
72 mTarget = new WeakReference<MainActivity>(target);
73 }
74
75 @Override
76 public void handleMessage(Message m) {
77 MainActivity target = mTarget.get();
78 if (target == null)
79 return;
80
81 if (m.what == CONST.MSG_TYPE.STR_DEBUG.ordinal()) {
82 Log.d(CONST.TAG, (String)m.obj);
83 } else if (m.what == CONST.MSG_TYPE.STR_INFO.ordinal()) {
84 target.updateStatus((String)m.obj);
85 Log.i(CONST.TAG, (String)m.obj);
86 } else if (m.what == CONST.MSG_TYPE.STR_ERROR.ordinal()) {
87 target.updateStatus((String)m.obj);
88 Log.e(CONST.TAG, (String)m.obj);
89 } else if (m.what == CONST.MSG_TYPE.CLI_STOP.ordinal()) {
90 pjsua.pjsuaDestroy();
91 LOG.INFO(this, "Telnet Unavailable");
92 } else if (m.what == CONST.MSG_TYPE.CLI_RESTART.ordinal()) {
93 int status = pjsua.pjsuaRestart();
94 if (status != 0) {
95 LOG.INFO(this, "Failed restarting telnet");
96 }
97 } else if (m.what == CONST.MSG_TYPE.QUIT.ordinal()) {
98 target.finish();
99 System.gc();
100 android.os.Process.killProcess(android.os.Process.myPid());
101 }
102 }
103 }
104
105 /** Callback object **/
106 private static class MyCallback extends PjsuaAppCallback {
107 private WeakReference<Handler> ui_handler;
108
109 public MyCallback(Handler in_ui_handler) {
110 set_ui_handler(in_ui_handler);
111 }
112
113 public void set_ui_handler(Handler in_ui_handler) {
114 ui_handler = new WeakReference<Handler>(in_ui_handler);
115 }
116
117 @Override
118 public void onStarted(String msg) {
119 Handler ui = ui_handler.get();
120 LOG.INFO(ui, msg);
121 }
122
123 @Override
124 public void onStopped(int restart) {
125 Handler ui = ui_handler.get();
126 /** Use timer to stopped/restart **/
127 if (restart != 0) {
128 LOG.INFO(ui, "Telnet Restarting");
129 Message msg = Message.obtain(ui,
130 CONST.MSG_TYPE.CLI_RESTART.ordinal());
131 ui.sendMessageDelayed(msg, 100);
132 } else {
133 LOG.INFO(ui, "Telnet Stopping");
134 Message msg = Message.obtain(ui,
135 CONST.MSG_TYPE.CLI_STOP.ordinal());
136 ui.sendMessageDelayed(msg, 100);
137 }
138 }
139 }
140
141 private void updateStatus(String output) {
142 TextView tStatus = (TextView) findViewById(R.id.textStatus);
143 tStatus.setText(output);
144 }
145
146 @Override
147 protected void onCreate(Bundle savedInstanceState) {
148 LOG.DEBUG(ui_handler, "=== Activity::onCreate() ===");
149 super.onCreate(savedInstanceState);
150
151 init_view();
152
153 init_lib();
154 }
155
156 @Override
157 protected void onStart() {
158 LOG.DEBUG(ui_handler, "=== Activity::onStart() ===");
159 super.onStart();
160 }
161
162 @Override
163 protected void onRestart() {
164 LOG.DEBUG(ui_handler, "=== Activity::onRestart() ===");
165 super.onRestart();
166 }
167
168 @Override
169 protected void onResume() {
170 LOG.DEBUG(ui_handler, "=== Activity::onResume() ===");
171 super.onResume();
172 }
173
174 @Override
175 protected void onPause() {
176 LOG.DEBUG(ui_handler, "=== Activity::onPause() ===");
177 super.onPause();
178 }
179
180 @Override
181 protected void onStop() {
182 LOG.DEBUG(ui_handler, "=== Activity::onStop() ===");
183 super.onStop();
184 }
185
186 @Override
187 protected void onDestroy() {
188 LOG.DEBUG(ui_handler, "=== Activity::onDestroy() ===");
189 super.onDestroy();
190 }
191
192 @Override
193 protected void onSaveInstanceState(Bundle outState) {
194 super.onSaveInstanceState(outState);
195 }
196
197 @Override
198 protected void onRestoreInstanceState(Bundle savedInstanceState) {
199 super.onRestoreInstanceState(savedInstanceState);
200 }
201
202 private void init_view() {
203 setContentView(R.layout.activity_main);
204 }
205
206 private int init_lib() {
207 LOG.INFO(ui_handler, "Loading module...");
208 try {
209 System.loadLibrary(CONST.LIB_FILENAME);
210 } catch (UnsatisfiedLinkError e) {
211 LOG.ERROR(ui_handler, "UnsatisfiedLinkError: " + e.getMessage());
212 return -1;
213 }
214
215 // Wait for GDB to init
216 if ((getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0)
217 {
218 try {
219 Thread.sleep(5000);
220 } catch (InterruptedException e) {
221 LOG.ERROR(ui_handler, "InterruptedException: " +
222 e.getMessage());
223 }
224 }
225
226 // Set callback object
227 if (callback == null)
228 callback = new MyCallback(ui_handler);
229
230 pjsua.setCallbackObject(callback);
231
232 LOG.INFO(ui_handler, "Starting module..");
233
234 int rc = pjsua.pjsuaStart();
235
236 if (rc != 0) {
237 LOG.INFO(ui_handler, "Failed starting telnet");
238 }
239
240 return 0;
241 }
242}