Commit de482a87f7daf56cf499cc60bd00f1f628a68d03

Authored by perry.werneck@gmail.com
1 parent 20980a6e

Android: Implementando diálogos de progresso quando o terminal está ocupado

android/lib3270NDK.cbp
... ... @@ -65,6 +65,7 @@
65 65 <Unit filename="jni/main.cpp" />
66 66 <Unit filename="jni/misc.cpp" />
67 67 <Unit filename="jni/text.cpp" />
  68 + <Unit filename="res/raw/jsmain.js" />
68 69 <Unit filename="src/br/com/bb/pw3270/PW3270Activity.java" />
69 70 <Unit filename="src/br/com/bb/pw3270/lib3270.java" />
70 71 <Extensions>
... ...
android/res/raw/jsmain.js
... ... @@ -33,10 +33,12 @@ function terminalUpdate()
33 33 {
34 34 document.getElementById("terminal").innerHTML = pw3270.getscreencontents();
35 35 setupWindow();
  36 + pw3270.ready();
36 37 }
37 38  
38 39 function pfkey(id)
39 40 {
  41 + pw3270.busy();
40 42 pw3270.pfkey(id);
41 43 return false;
42 44 }
... ... @@ -45,6 +47,8 @@ function xmit()
45 47 {
46 48 var form = document.getElementById("form3270");
47 49  
  50 + pw3270.busy();
  51 +
48 52 if(form != undefined)
49 53 {
50 54 for(var i=0;i < form.elements.length;i++)
... ...
android/src/br/com/bb/pw3270/PW3270Activity.java
... ... @@ -57,28 +57,22 @@ public class PW3270Activity extends Activity
57 57 dlgProgress.show();
58 58 }
59 59  
60   - protected void updateProgramMessage(int id)
  60 + protected void updateScreen()
61 61 {
62   - if(id == 0)
  62 + showProgressDialog("Aguarde...");
  63 + view.reload();
  64 + }
  65 +
  66 + protected void showProgramMessage(int id)
  67 + {
  68 + String message[] = res.getStringArray(R.array.program_msg);
  69 + try
63 70 {
64   - if(changed)
65   - {
66   - dlgProgress.setMessage("Aguarde...");
67   - changed = false;
68   - view.reload();
69   - hideProgressDialog();
70   - }
71   - }
72   - else
  71 + showProgressDialog(message[id]);
  72 + } catch(Exception e)
73 73 {
74   - String message[] = res.getStringArray(R.array.program_msg);
75   - try
76   - {
77   - showProgressDialog(message[id]);
78   - } catch(Exception e)
79   - {
80   - showProgressDialog("Aguarde...");
81   - }
  74 + Log.e(TAG,e.getLocalizedMessage());
  75 + showProgressDialog("?");
82 76 }
83 77 }
84 78  
... ... @@ -90,6 +84,7 @@ public class PW3270Activity extends Activity
90 84 d.setMessage(text);
91 85  
92 86 d.setCancelable(true);
  87 + hideProgressDialog();
93 88 d.show();
94 89 }
95 90  
... ...
android/src/br/com/bb/pw3270/lib3270.java
... ... @@ -10,422 +10,437 @@ import java.net.Socket;
10 10 import javax.net.ssl.SSLSocketFactory;
11 11 import java.io.DataInputStream;
12 12 import java.io.DataOutputStream;
  13 +
13 14 // import java.util.Vector;
14 15  
15   -public class lib3270
16   -{
17   - private NetworkThread mainloop;
  16 +public class lib3270 {
  17 + private NetworkThread mainloop = null;
18 18 private static final String TAG = "lib3270";
19 19  
20   - protected boolean changed;
21   - private boolean connected = false;
22   - private boolean refresh = true;
  20 + protected int screenState = 0;
  21 + private boolean connected = false;
  22 + private boolean refresh = true;
23 23  
24   - DataOutputStream outData = null;
25   - DataInputStream inData = null;
  24 + DataOutputStream outData = null;
  25 + DataInputStream inData = null;
26 26  
27   - protected String hostname = "3270.df.bb";
28   - protected int port = 8023;
29   - protected boolean ssl = false;
  27 + protected String hostname = "3270.df.bb";
  28 + protected int port = 8023;
  29 + protected boolean ssl = false;
30 30  
31   - static
32   - {
  31 + static {
33 32 System.loadLibrary("3270");
34 33 init();
35 34 }
36 35  
37   - lib3270()
38   - {
39   - changed = false;
  36 + lib3270() {
  37 + screenState = 0;
40 38 mainloop = null;
41 39 }
42 40  
43   - private class timer extends CountDownTimer
44   - {
45   - private long id;
46   - private lib3270 terminal;
  41 + private class timer extends CountDownTimer {
  42 + private long id;
  43 + private lib3270 terminal;
47 44  
48   - timer(lib3270 session, long timer_id, int msec)
49   - {
50   - super(msec,msec);
  45 + timer(lib3270 session, long timer_id, int msec) {
  46 + super(msec, msec);
51 47  
52   - terminal = session;
53   - id = timer_id;
  48 + terminal = session;
  49 + id = timer_id;
54 50  
55   - Log.d(TAG,"Timer " + id + " set to " + msec + " ms");
  51 + Log.d(TAG, "Timer " + id + " set to " + msec + " ms");
56 52  
57 53 this.start();
58 54 }
59 55  
60   - public void onTick(long millisUntilFinished)
61   - {
  56 + public void onTick(long millisUntilFinished) {
62 57 }
63 58  
64   - public void onFinish()
65   - {
66   - Log.d(TAG,"Timer " + id + " finished");
  59 + public void onFinish() {
  60 + Log.d(TAG, "Timer " + id + " finished");
67 61 terminal.timerFinish(id);
68 62 }
69 63  
70 64 }
71   - private class popupMessageInfo
72   - {
  65 +
  66 + private class popupMessageInfo {
73 67 public String title;
74 68 public String text;
75 69 public String info;
76 70  
77   - popupMessageInfo(String title, String text, String info)
78   - {
79   - this.title = title;
80   - this.text = text;
81   - this.info = info;
  71 + popupMessageInfo(String title, String text, String info) {
  72 + this.title = title;
  73 + this.text = text;
  74 + this.info = info;
82 75 }
83 76 }
84 77  
85   - private class byteMessage
86   - {
87   - byte[] msg;
88   - int sz;
89   -
90   - byteMessage(byte[] contents, int len)
91   - {
92   - msg = contents;
93   - sz = len;
94   - }
95   -
96   - byte[] getMessage()
97   - {
98   - return msg;
99   - }
100   -
101   - int getLength()
102   - {
103   - return sz;
104   - }
  78 + private class byteMessage {
  79 + byte[] msg;
  80 + int sz;
  81 +
  82 + byteMessage(byte[] contents, int len) {
  83 + msg = contents;
  84 + sz = len;
  85 + }
  86 +
  87 + byte[] getMessage() {
  88 + return msg;
  89 + }
  90 +
  91 + int getLength() {
  92 + return sz;
  93 + }
105 94 }
106 95  
107   - protected int send_data(byte[] data, int len)
108   - {
109   - Log.i(TAG,"Bytes a enviar: " + len);
  96 + protected int send_data(byte[] data, int len) {
  97 + Log.i(TAG, "Bytes a enviar: " + len);
110 98  
111   - try
112   - {
113   - outData.write(data,0,len);
  99 + try {
  100 + outData.write(data, 0, len);
114 101 outData.flush();
115 102 return len;
116   - } catch( Exception e )
117   - {
118   - String msg = e.getLocalizedMessage();
  103 + } catch (Exception e) {
  104 + String msg = e.getLocalizedMessage();
119 105  
120   - if(msg == null)
121   - msg = e.toString();
  106 + if (msg == null)
  107 + msg = e.toString();
122 108  
123   - if(msg == null)
124   - msg = "Erro indefinido";
  109 + if (msg == null)
  110 + msg = "Erro indefinido";
125 111  
126   - Log.i(TAG,"Erro ao enviar dados: " + msg);
  112 + Log.i(TAG, "Erro ao enviar dados: " + msg);
127 113  
128   - postPopup(0,"Erro na comunicação","Não foi possível enviar dados",msg);
  114 + postPopup(0, "Erro na comunicação",
  115 + "Não foi possível enviar dados", msg);
129 116  
130 117 }
131 118 return -1;
132 119 }
133 120  
134   -
135 121 // Main Thread
136   - private class NetworkThread extends Thread
137   - {
138   - Handler mHandler;
139   - Socket sock = null;
  122 + private class NetworkThread extends Thread {
  123 + Handler mHandler;
  124 + Socket sock = null;
140 125  
141   - NetworkThread(Handler h)
142   - {
143   - mHandler = h;
144   - }
  126 + NetworkThread(Handler h) {
  127 + mHandler = h;
  128 + }
145 129  
146   - private boolean connect()
147   - {
148   - // Connecta no host
  130 + private boolean connect() {
  131 + // Connecta no host
149 132 SocketFactory socketFactory;
150 133  
151   - if(hostname == "")
  134 + if (hostname == "")
152 135 return false;
153 136  
154   - if(ssl)
155   - {
  137 + postMessage(1, 14, 0);
  138 +
  139 + if (ssl) {
156 140 // Host é SSL
157   - socketFactory = SSLSocketFactory.getDefault();
158   - }
159   - else
160   - {
161   - socketFactory = SocketFactory.getDefault();
  141 + socketFactory = SSLSocketFactory.getDefault();
  142 + } else {
  143 + socketFactory = SocketFactory.getDefault();
162 144 }
163 145  
164   - try
165   - {
166   - sock = socketFactory.createSocket(hostname,port);
167   - outData = new DataOutputStream(sock.getOutputStream());
168   - inData = new DataInputStream(sock.getInputStream());
  146 + try {
  147 + sock = socketFactory.createSocket(hostname, port);
  148 + outData = new DataOutputStream(sock.getOutputStream());
  149 + inData = new DataInputStream(sock.getInputStream());
169 150  
170   - } catch( Exception e )
171   - {
172   - String msg = e.getLocalizedMessage();
  151 + } catch (Exception e) {
  152 + String msg = e.getLocalizedMessage();
173 153  
174   - if(msg == null)
175   - msg = e.toString();
  154 + if (msg == null)
  155 + msg = e.toString();
176 156  
177   - if(msg == null)
178   - msg = "Erro indefinido";
  157 + if (msg == null)
  158 + msg = "Erro indefinido";
179 159  
180   - Log.i(TAG,"Erro ao conectar: " + msg);
  160 + Log.i(TAG, "Erro ao conectar: " + msg);
181 161  
182   - postPopup(0,"Erro na conexão","Não foi possível conectar",msg);
  162 + postPopup(0, "Erro na conexão", "Não foi possível conectar",
  163 + msg);
183 164  
184   - postMessage(0,0,0);
  165 + postMessage(0, 0, 0);
185 166  
186   - return false;
187   - }
  167 + return false;
  168 + }
188 169  
189   - Log.i(TAG,"Conectado ao host");
190   - return true;
  170 + Log.i(TAG, "Conectado ao host");
  171 + return true;
191 172  
192 173 }
193 174  
194   - public void run()
195   - {
  175 + public void run() {
196 176  
197   - info(TAG,"Network thread started");
198   - connected = connect();
  177 + info(TAG, "Network thread started");
  178 + connected = connect();
199 179  
200   - if(connected)
201   - {
202   - postMessage(0,0,0);
  180 + if (connected) {
  181 + postMessage(0, 0, 0);
203 182  
204   - while(connected)
205   - {
206   - byte[] in = new byte[4096];
207   - int sz = -1;
  183 + while (connected) {
  184 + byte[] in = new byte[4096];
  185 + int sz = -1;
208 186  
209   - try
210   - {
211   - sz = inData.read(in,0,4096);
212   - } catch( Exception e ) { sz = -1; }
  187 + try {
  188 + sz = inData.read(in, 0, 4096);
  189 + } catch (Exception e) {
  190 + sz = -1;
  191 + }
213 192  
214   - if(sz < 0)
215   - {
  193 + if (sz < 0) {
216 194 connected = false;
217   - }
218   - else if(sz > 0)
219   - {
  195 + } else if (sz > 0) {
220 196 Message msg = mHandler.obtainMessage();
221 197 msg.what = 6;
222   - msg.obj = new byteMessage(in,sz);
  198 + msg.obj = new byteMessage(in, sz);
223 199  
224 200 mHandler.sendMessage(msg);
225 201 }
226 202 }
227 203 }
228 204  
229   - try
230   - {
  205 + try {
231 206 sock.close();
232   - } catch( Exception e ) { }
  207 + } catch (Exception e) {
  208 + }
233 209  
234 210 sock = null;
235 211 outData = null;
236 212 inData = null;
237 213  
238   - postMessage(0,0,0);
  214 + postMessage(0, 0, 0);
239 215  
240 216 mainloop = null;
241   - info(TAG,"Network thread stopped");
242   - }
243   -
244   - public void postMessage(int what, int arg1, int arg2)
245   - {
246   - Message msg = mHandler.obtainMessage();
247   - msg.what = what;
248   - msg.arg1 = arg1;
249   - msg.arg2 = arg2;
250   - mHandler.sendMessage(msg);
251   - }
252   -
253   - public void postPopup(int type, String title, String text, String info)
254   - {
255   - Message msg = mHandler.obtainMessage();
256   -
257   - msg.what = 3;
258   - msg.arg1 = type;
259   - msg.obj = new popupMessageInfo(title,text,info);
260   - mHandler.sendMessage(msg);
261   - }
  217 + info(TAG, "Network thread stopped");
  218 + }
  219 +
  220 + public void postMessage(int what, int arg1, int arg2) {
  221 + Message msg = mHandler.obtainMessage();
  222 + msg.what = what;
  223 + msg.arg1 = arg1;
  224 + msg.arg2 = arg2;
  225 + mHandler.sendMessage(msg);
  226 + }
  227 +
  228 + public void postPopup(int type, String title, String text, String info) {
  229 + Message msg = mHandler.obtainMessage();
  230 +
  231 + msg.what = 3;
  232 + msg.arg1 = type;
  233 + msg.obj = new popupMessageInfo(title, text, info);
  234 + mHandler.sendMessage(msg);
  235 + }
262 236  
263 237 }
264 238  
265   - public void postMessage(int what, int arg1, int arg2)
266   - {
267   - mainloop.postMessage(what, arg1, arg2);
268   - }
  239 + public void postMessage(int what, int arg1, int arg2) {
  240 + mainloop.postMessage(what, arg1, arg2);
  241 + }
269 242  
270   - public void postPopup(int type, String title, String text, String info)
271   - {
272   - Log.d(TAG,"Type:"+type);
273   - Log.d(TAG,"Title:"+title);
274   - Log.d(TAG,"Text:"+text);
275   - Log.d(TAG,"Info:"+info);
276   - mainloop.postPopup(type, title, text, info);
  243 + public void postPopup(int type, String title, String text, String info) {
  244 + Log.d(TAG, "Type:" + type);
  245 + Log.d(TAG, "Title:" + title);
  246 + Log.d(TAG, "Text:" + text);
  247 + Log.d(TAG, "Info:" + info);
  248 + mainloop.postPopup(type, title, text, info);
277 249 }
278 250  
279   - // Define the Handler that receives messages from the thread and update the progress
280   - final Handler handler = new Handler()
281   - {
282   - public void handleMessage(Message msg)
283   - {
284   - switch(msg.what)
285   - {
286   - case 0: // Connected/Disconnected
287   - set_connection_status(connected);
288   - Log.d(TAG,connected ? "Connected" : "Disconnected");
289   - break;
290   -
291   - case 1: // OIA message has changed
292   - updateProgramMessage(msg.arg1);
293   - break;
294   -
295   - case 2: // Screen changed
296   - Log.d(TAG,"Screen changed");
297   - changed = true;
298   - break;
299   -
300   - case 3: // Popup
301   - popupMessageInfo popup = (popupMessageInfo) msg.obj;
302   - popupMessage(msg.arg1, popup.title, popup.text, popup.info);
303   - break;
  251 + // Define the Handler that receives messages from the thread and update the
  252 + // progress
  253 + final Handler handler = new Handler() {
  254 + public void handleMessage(Message msg) {
  255 + switch (msg.what) {
  256 + case 0: // Connected/Disconnected
  257 + set_connection_status(connected);
  258 + Log.d(TAG, connected ? "Connected" : "Disconnected");
  259 + break;
  260 +
  261 + case 1: // OIA message has changed
  262 + updateProgramMessage(msg.arg1);
  263 + break;
  264 +
  265 + case 2: // Screen changed
  266 + Log.d(TAG, "Screen changed");
  267 + screenState = 1;
  268 + break;
  269 +
  270 + case 3: // Popup
  271 + popupMessageInfo popup = (popupMessageInfo) msg.obj;
  272 + popupMessage(msg.arg1, popup.title, popup.text, popup.info);
  273 + break;
304 274  
305 275 case 4: // erase
306   - changed = false;
307   - erase();
  276 + if(screenState != 2)
  277 + {
  278 + screenState = 2;
  279 + erase();
  280 + }
308 281 break;
309 282  
310 283 case 5: // ctlr_done
311   - Log.d(TAG,"ctlr_done");
  284 + Log.d(TAG, "ctlr_done");
312 285 break;
313 286  
314 287 case 6: // recv_data
315   - procRecvdata(((byteMessage) msg.obj).getMessage(),((byteMessage) msg.obj).getLength());
  288 + procRecvdata(((byteMessage) msg.obj).getMessage(),
  289 + ((byteMessage) msg.obj).getLength());
  290 + break;
  291 +
  292 + case 7: // ready
  293 + hideProgressDialog();
316 294 break;
317   - }
318   - }
319   - };
320 295  
321   - /*---[ Signal methods ]--------------------------------------------------*/
  296 + case 8: // busy
  297 + showProgressDialog("Aguarde...");
  298 + break;
  299 + }
  300 + }
  301 + };
  302 +
  303 + /*---[ Signal methods ]--------------------------------------------------*/
322 304  
323   - protected void updateProgramMessage(int id)
  305 + protected void showProgramMessage(int id)
324 306 {
  307 +
  308 + }
  309 +
  310 + private void updateProgramMessage(int id)
  311 + {
  312 + if(id == 0 && screenState != 0)
  313 + {
  314 + screenState = 0;
  315 + updateScreen();
  316 + }
  317 + else
  318 + {
  319 + showProgramMessage(id);
  320 + }
325 321 }
326 322  
327   - protected void popupMessage(int type, String title, String text, String info)
  323 + protected void popupMessage(int type, String title, String text, String info) {
  324 + }
  325 +
  326 + protected void info(String tag, String msg) {
  327 + Log.i(tag, msg);
  328 + }
  329 +
  330 + protected void error(String tag, String msg) {
  331 + Log.e(tag, msg);
  332 + }
  333 +
  334 + protected void erase()
328 335 {
329 336 }
330 337  
331   - protected void info(String tag, String msg)
  338 + protected void updateScreen()
332 339 {
333   - Log.i(tag,msg);
  340 + }
  341 +
  342 + public void pfkey(int id)
  343 + {
  344 + sendPFkey(id);
334 345 }
335 346  
336   - protected void error(String tag, String msg)
  347 + public void xmit()
337 348 {
338   - Log.e(tag,msg);
  349 + sendEnter();
339 350 }
340 351  
341   - protected void erase()
  352 + public void ready()
342 353 {
343   - Log.i(TAG,"Erase screen");
  354 + postMessage(7, 0, 0);
344 355 }
345 356  
346   - public void pfkey(int id)
  357 + public void busy()
347 358 {
348   - Log.d(TAG,"PF"+id);
349   - sendPFkey(id);
  359 + postMessage(8, 0, 0);
350 360 }
351 361  
352   - public void xmit()
  362 + public void hideProgressDialog()
353 363 {
354   - Log.d(TAG,"XMIT");
355   - sendEnter();
356 364 }
357 365  
358   - /*---[ External methods ]------------------------------------------------*/
359   -
360   - public int connect()
361   - {
362   - if(mainloop == null)
363   - {
364   - info(TAG,"Starting comm thread");
365   - mainloop = new NetworkThread(handler);
366   - mainloop.start();
367   - return 0;
368   - }
369   - error(TAG,"Comm thread already active during connect");
370   - return -1;
371   - }
372   -
373   - public int disconnect()
374   - {
375   - connected = false;
376   - return 0;
377   - }
378   -
379   - public void setStringAt(int offset, String str)
380   - {
381   - refresh = false;
382   - try
383   - {
384   - setTextAt(offset,str.getBytes(getEncoding()),str.length());
385   - } catch( Exception e ) { }
386   - refresh = true;
387   - }
388   -
389   -
390   - /*---[ Native calls ]----------------------------------------------------*/
391   - static private native int init();
392   - static private native int deinit();
393   -
394   - private native int processEvents();
395   -// private native int do_connect();
396   - private native void set_connection_status(boolean state);
  366 + public void showProgressDialog(String msg) {
  367 + }
  368 +
  369 + /*---[ External methods ]------------------------------------------------*/
  370 +
  371 + public int connect() {
  372 + if (mainloop == null) {
  373 + info(TAG, "Starting comm thread");
  374 + mainloop = new NetworkThread(handler);
  375 + mainloop.start();
  376 + return 0;
  377 + }
  378 + error(TAG, "Comm thread already active during connect");
  379 + return -1;
  380 + }
  381 +
  382 + public int disconnect() {
  383 + connected = false;
  384 + return 0;
  385 + }
  386 +
  387 + public void setStringAt(int offset, String str) {
  388 + refresh = false;
  389 + try {
  390 + setTextAt(offset, str.getBytes(getEncoding()), str.length());
  391 + } catch (Exception e) {
  392 + }
  393 + refresh = true;
  394 + }
  395 +
  396 + /*---[ Native calls ]----------------------------------------------------*/
  397 + static private native int init();
  398 +
  399 + static private native int deinit();
  400 +
  401 + private native int processEvents();
  402 +
  403 + // private native int do_connect();
  404 + private native void set_connection_status(boolean state);
397 405  
398 406 // Misc calls
399   - public native String getEncoding();
400   - public native String getVersion();
401   - public native String getRevision();
402   - public native void setToggle(String name, boolean state);
  407 + public native String getEncoding();
  408 +
  409 + public native String getVersion();
  410 +
  411 + public native String getRevision();
  412 +
  413 + public native void setToggle(String name, boolean state);
403 414  
404 415 // Network I/O
405   - public native void procRecvdata( byte[] data, int len);
  416 + public native void procRecvdata(byte[] data, int len);
406 417  
407 418 // Connect/Disconnect status
408   - public native void setHost(String host);
409   - public native String getHost();
410   - public native boolean isConnected();
411   - public native boolean isTerminalReady();
  419 + public native void setHost(String host);
  420 +
  421 + public native String getHost();
  422 +
  423 + public native boolean isConnected();
  424 +
  425 + public native boolean isTerminalReady();
412 426  
413 427 // Timers
414   - protected void newTimer(long id, int msec)
415   - {
416   - new timer(this,id,msec);
  428 + protected void newTimer(long id, int msec) {
  429 + new timer(this, id, msec);
417 430 }
418 431  
419   - private native void timerFinish(long id);
  432 + private native void timerFinish(long id);
420 433  
421 434 // Keyboard actions
422   - public native void sendEnter();
423   - public native void sendPFkey(int id);
  435 + public native void sendEnter();
  436 +
  437 + public native void sendPFkey(int id);
424 438  
425 439 // Get/Set screen contents
426   - public native byte[] getHTML();
427   - public native byte[] getText();
428   - public native void setTextAt(int offset, byte[] str, int len);
  440 + public native byte[] getHTML();
  441 +
  442 + public native byte[] getText();
429 443  
  444 + public native void setTextAt(int offset, byte[] str, int len);
430 445  
431 446 }
... ...