Commit 39a32f62d2dcf99cbdb4ca1f6f4d8f145888a8af

Authored by perry.werneck@gmail.com
1 parent 981b1118

Android - Simplificando classe 3270

android/Makefile
@@ -86,11 +86,8 @@ pw3270-release-unsigned.apk: icons libs/armeabi/lib3270.so $(JAVASRCDIR)/*.java @@ -86,11 +86,8 @@ pw3270-release-unsigned.apk: icons libs/armeabi/lib3270.so $(JAVASRCDIR)/*.java
86 libs/armeabi/lib3270.so: jni/lib3270jni.h jni/*.cpp 86 libs/armeabi/lib3270.so: jni/lib3270jni.h jni/*.cpp
87 @$(NDKBUILD) 87 @$(NDKBUILD)
88 88
89 -icons: $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) \  
90 - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_connect.png) \  
91 - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_disconnect.png) \  
92 - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_settings.png)  
93 - 89 +icons: $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png)
  90 +
94 clean: 91 clean:
95 @rm -fr bin 92 @rm -fr bin
96 @rm -fr obj 93 @rm -fr obj
android/res/layout/menu.xml
@@ -4,12 +4,11 @@ @@ -4,12 +4,11 @@
4 android:layout_height="match_parent" > 4 android:layout_height="match_parent" >
5 5
6 <item android:id="@+id/connect" 6 <item android:id="@+id/connect"
7 - android:icon="@drawable/ic_connect"  
8 android:title="Conectar" /> 7 android:title="Conectar" />
9 <item android:id="@+id/disconnect" 8 <item android:id="@+id/disconnect"
10 - android:icon="@drawable/ic_disconnect"  
11 android:title="Desconectar" /> 9 android:title="Desconectar" />
  10 + <item android:id="@+id/reload"
  11 + android:title="Recarregar" />
12 <item android:id="@+id/settings" 12 <item android:id="@+id/settings"
13 - android:icon="@drawable/ic_settings"  
14 android:title="Configurar" /> 13 android:title="Configurar" />
15 </menu> 14 </menu>
android/src/br/com/bb/pw3270/PW3270Activity.java
@@ -32,16 +32,16 @@ package br.com.bb.pw3270; @@ -32,16 +32,16 @@ package br.com.bb.pw3270;
32 32
33 import android.app.Activity; 33 import android.app.Activity;
34 import android.os.Bundle; 34 import android.os.Bundle;
35 -import android.preference.PreferenceManager; 35 +// import android.preference.PreferenceManager;
36 import android.util.Log; 36 import android.util.Log;
37 import android.content.Intent; 37 import android.content.Intent;
38 -import android.content.SharedPreferences; 38 +//import android.content.SharedPreferences;
39 import android.content.res.*; 39 import android.content.res.*;
40 -import android.app.AlertDialog; 40 +// import android.app.AlertDialog;
41 import android.webkit.WebView; 41 import android.webkit.WebView;
42 -import android.webkit.WebViewClient;  
43 -import android.webkit.WebResourceResponse;  
44 -import android.webkit.WebChromeClient; 42 +// import android.webkit.WebViewClient;
  43 +// import android.webkit.WebResourceResponse;
  44 +// import android.webkit.WebChromeClient;
45 import android.app.ProgressDialog; 45 import android.app.ProgressDialog;
46 import android.view.Menu; 46 import android.view.Menu;
47 import android.view.MenuInflater; 47 import android.view.MenuInflater;
@@ -54,176 +54,45 @@ import android.view.MenuItem; @@ -54,176 +54,45 @@ import android.view.MenuItem;
54 public class PW3270Activity extends Activity 54 public class PW3270Activity extends Activity
55 { 55 {
56 private static final String TAG = "pw3270"; 56 private static final String TAG = "pw3270";
57 -  
58 - private static terminal host; 57 + private static lib3270 host = null;
59 58
60 - private Resources res ;  
61 - private WebView view;  
62 - private Activity mainact = this;  
63 - private ProgressDialog dlgSysMessage;  
64 -  
65 - private class terminal extends lib3270  
66 - {  
67 -  
68 - terminal(SharedPreferences settings)  
69 - {  
70 - super(settings);  
71 - }  
72 -  
73 - public void hideProgressDialog()  
74 - {  
75 - dlgSysMessage.hide();  
76 - }  
77 -  
78 - protected void updateScreen()  
79 - {  
80 - view.reload();  
81 - }  
82 -  
83 - protected boolean showProgramMessage(int id)  
84 - {  
85 - if(!super.showProgramMessage(id))  
86 - {  
87 - String message[] = res.getStringArray(R.array.program_msg);  
88 - try  
89 - {  
90 - dlgSysMessage.setMessage(message[id]);  
91 - } catch(Exception e)  
92 - {  
93 - dlgSysMessage.setMessage(e.getLocalizedMessage());  
94 - }  
95 - dlgSysMessage.show();  
96 - }  
97 - return true;  
98 - }  
99 -  
100 - protected void showPopupMessage(int type, String title, String text, String info)  
101 - {  
102 - Log.v(TAG,"Popup Message:");  
103 - Log.v(TAG,title);  
104 - Log.v(TAG,text);  
105 - Log.v(TAG,info);  
106 -  
107 - AlertDialog d = new AlertDialog.Builder(mainact).create();  
108 -  
109 - if(title != "")  
110 - d.setTitle(title);  
111 -  
112 - if(text != "")  
113 - d.setMessage(text);  
114 -  
115 - d.setCancelable(true);  
116 - hideProgressDialog();  
117 - d.show();  
118 - }  
119 -  
120 - @SuppressWarnings("unused")  
121 - public String getscreencontents()  
122 - {  
123 - String text;  
124 -  
125 - try  
126 - {  
127 - text = new String(getHTML(),getEncoding());  
128 - }  
129 - catch(Exception e)  
130 - {  
131 - Log.e(TAG,e.getLocalizedMessage());  
132 - return "";  
133 - }  
134 -  
135 - return text;  
136 - }  
137 -  
138 - };  
139 59
140 /** Called when the activity is first created. */ 60 /** Called when the activity is first created. */
141 @Override 61 @Override
142 public void onCreate(Bundle savedInstanceState) 62 public void onCreate(Bundle savedInstanceState)
143 { 63 {
144 super.onCreate(savedInstanceState); 64 super.onCreate(savedInstanceState);
145 - res = getResources();  
146 - initUI(); 65 + initUI();
147 } 66 }
148 67
149 protected void initUI() 68 protected void initUI()
150 { 69 {
151 - boolean autoconnect = false;  
152 - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);  
153 - 70 + if(host == null)
  71 + host = new lib3270(this);
  72 + else
  73 + host.setActivity(this);
  74 +
154 Log.d(TAG, "Initializing UI"); 75 Log.d(TAG, "Initializing UI");
155 76
156 - if(dlgSysMessage == null)  
157 - { 77 +// if(host.dlgSysMessage == null)
  78 +// {
158 // Cria dialogo para as mensagems de sistema 79 // Cria dialogo para as mensagems de sistema
159 - Log.d(TAG, "Creating dlgSysMessage");  
160 - dlgSysMessage = new ProgressDialog(this);  
161 - dlgSysMessage.setCancelable(false);  
162 - dlgSysMessage.setTitle(res.getString(R.string.wait));  
163 - } 80 +// Log.d(TAG, "Creating dlgSysMessage");
  81 + host.dlgSysMessage = new ProgressDialog(this);
  82 + host.dlgSysMessage.setCancelable(false);
  83 + host.dlgSysMessage.setTitle(host.res.getString(R.string.wait));
  84 +// }
164 85
165 - if(host == null)  
166 - {  
167 - Log.d(TAG, "Creating terminal object");  
168 - host = new terminal(settings);  
169 - autoconnect = settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false);  
170 - } 86 +// Log.d(TAG, "Creating terminal object");
  87 +// autoconnect = settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false);
171 88
172 - if(view == null)  
173 - {  
174 - // Reference:  
175 - // http://developer.android.com/reference/android/webkit/WebView.html  
176 - Log.d(TAG, "Creating Webview");  
177 - view = new WebView(this);  
178 -  
179 - view.addJavascriptInterface(host, "pw3270");  
180 -  
181 - view.setWebChromeClient(new WebChromeClient());  
182 -  
183 - view.getSettings().setBuiltInZoomControls(true);  
184 - view.getSettings().setSupportZoom(true);  
185 - view.getSettings().setUseWideViewPort(true);  
186 - view.getSettings().setLoadWithOverviewMode(true);  
187 - view.getSettings().setJavaScriptEnabled(true);  
188 -  
189 - view.setWebViewClient(new WebViewClient()  
190 - {  
191 -  
192 - @Override  
193 - public WebResourceResponse shouldInterceptRequest(WebView view, String url)  
194 - {  
195 - int id = R.raw.index;  
196 - String mime = "text/html";  
197 - int pos = url.lastIndexOf("/");  
198 -  
199 - if(pos >=0 )  
200 - url = url.substring(pos+1);  
201 -  
202 - Log.i(TAG,"Loading [" + url + "]");  
203 -  
204 - if(url.equalsIgnoreCase("jsmain.js"))  
205 - {  
206 - id = R.raw.jsmain;  
207 - }  
208 - else if(url.equalsIgnoreCase("theme.css"))  
209 - {  
210 - mime = "text/css";  
211 - id = R.raw.theme;  
212 - }  
213 -  
214 - // http://developer.android.com/reference/android/webkit/WebResourceResponse.html  
215 - return new WebResourceResponse(mime,"utf-8",getResources().openRawResource(id));  
216 - }  
217 -  
218 - });  
219 - }  
220 -  
221 - setContentView(view);  
222 - view.loadUrl("file:index.html");  
223 -  
224 - if(autoconnect)  
225 - host.connect(); 89 + // Reference:
  90 + // http://developer.android.com/reference/android/webkit/WebView.html
  91 + Log.d(TAG, "Creating Webview");
  92 + setContentView(host.setView(new WebView(this)));
226 93
  94 + host.view.loadUrl("file:index.html");
  95 + host.initialize();
227 } 96 }
228 97
229 @Override 98 @Override
@@ -250,10 +119,14 @@ public class PW3270Activity extends Activity @@ -250,10 +119,14 @@ public class PW3270Activity extends Activity
250 break; 119 break;
251 120
252 case R.id.settings: 121 case R.id.settings:
253 - Intent myIntent = new Intent(view.getContext(), SettingsActivity.class); 122 + Intent myIntent = new Intent(host.view.getContext(), SettingsActivity.class);
254 startActivityForResult(myIntent, 0); 123 startActivityForResult(myIntent, 0);
255 break; 124 break;
256 125
  126 + case R.id.reload:
  127 + host.view.reload();
  128 + break;
  129 +
257 default: 130 default:
258 return super.onOptionsItemSelected(item); 131 return super.onOptionsItemSelected(item);
259 } 132 }
@@ -274,7 +147,7 @@ public class PW3270Activity extends Activity @@ -274,7 +147,7 @@ public class PW3270Activity extends Activity
274 { 147 {
275 super.onSaveInstanceState(outState); 148 super.onSaveInstanceState(outState);
276 // Save the state of the WebView 149 // Save the state of the WebView
277 - view.saveState(outState); 150 + host.view.saveState(outState);
278 } 151 }
279 152
280 @Override 153 @Override
@@ -282,9 +155,8 @@ public class PW3270Activity extends Activity @@ -282,9 +155,8 @@ public class PW3270Activity extends Activity
282 { 155 {
283 super.onRestoreInstanceState(savedInstanceState); 156 super.onRestoreInstanceState(savedInstanceState);
284 // Restore the state of the WebView 157 // Restore the state of the WebView
285 - view.restoreState(savedInstanceState); 158 + host.view.restoreState(savedInstanceState);
  159 + host.view.reload();
286 } 160 }
287 -  
288 -  
289 161
290 } 162 }
android/src/br/com/bb/pw3270/lib3270.java
@@ -30,11 +30,20 @@ package br.com.bb.pw3270; @@ -30,11 +30,20 @@ package br.com.bb.pw3270;
30 30
31 import java.lang.Thread; 31 import java.lang.Thread;
32 32
  33 +import android.app.Activity;
  34 +import android.app.AlertDialog;
  35 +import android.app.ProgressDialog;
33 import android.content.SharedPreferences; 36 import android.content.SharedPreferences;
  37 +import android.content.res.Resources;
34 import android.os.Handler; 38 import android.os.Handler;
35 import android.os.Message; 39 import android.os.Message;
36 import android.os.CountDownTimer; 40 import android.os.CountDownTimer;
  41 +import android.preference.PreferenceManager;
37 import android.util.Log; 42 import android.util.Log;
  43 +import android.webkit.WebChromeClient;
  44 +import android.webkit.WebResourceResponse;
  45 +import android.webkit.WebView;
  46 +import android.webkit.WebViewClient;
38 import javax.net.SocketFactory; 47 import javax.net.SocketFactory;
39 import java.net.Socket; 48 import java.net.Socket;
40 import javax.net.ssl.SSLSocketFactory; 49 import javax.net.ssl.SSLSocketFactory;
@@ -45,23 +54,28 @@ import java.io.DataOutputStream; @@ -45,23 +54,28 @@ import java.io.DataOutputStream;
45 54
46 public class lib3270 55 public class lib3270
47 { 56 {
48 - private static final String TAG = "lib3270"; 57 + private static final String TAG = "lib3270";
49 58
  59 + protected int screenState = -1;
50 60
51 - protected int screenState = -1;  
52 -  
53 - private lib3270 hSession = this; 61 + private lib3270 hSession = this;
  62 +
  63 + public ProgressDialog dlgSysMessage = null;
  64 + public WebView view = null;
  65 + public Resources res = null;
  66 + public Activity mainact = null;
54 67
55 - private static NetworkThread mainloop = null;  
56 - private static boolean connected = false;  
57 - private static boolean reconnect = false;  
58 - private static boolean refresh = true;  
59 - private static Socket sock = null; 68 + private static boolean initialized = false;
  69 + private static NetworkThread mainloop = null;
  70 + private static boolean connected = false;
  71 + private static boolean reconnect = false;
  72 + private static boolean refresh = true;
  73 + private static Socket sock = null;
60 74
61 - private static DataOutputStream outData = null;  
62 - private static DataInputStream inData = null; 75 + private static DataOutputStream outData = null;
  76 + private static DataInputStream inData = null;
63 77
64 - protected SharedPreferences settings; 78 + protected SharedPreferences settings;
65 79
66 // Define the Handler that receives messages from the thread 80 // Define the Handler that receives messages from the thread
67 final Handler mHandler = new Handler() 81 final Handler mHandler = new Handler()
@@ -71,7 +85,10 @@ public class lib3270 @@ -71,7 +85,10 @@ public class lib3270
71 switch (msg.what) 85 switch (msg.what)
72 { 86 {
73 case 0: // Reconnect 87 case 0: // Reconnect
74 - Log.d(TAG,"----------------------------Reconnecting"); 88 + if(!hSession.isConnected())
  89 + {
  90 + Log.d(TAG,"------------------------------------Connecting");
  91 + }
75 break; 92 break;
76 93
77 case 1: // OIA message has changed 94 case 1: // OIA message has changed
@@ -101,7 +118,7 @@ public class lib3270 @@ -101,7 +118,7 @@ public class lib3270
101 break; 118 break;
102 119
103 case 7: // ready 120 case 7: // ready
104 - hideProgressDialog(); 121 + dlgSysMessage.hide();
105 break; 122 break;
106 123
107 case 8: // busy 124 case 8: // busy
@@ -122,11 +139,11 @@ public class lib3270 @@ -122,11 +139,11 @@ public class lib3270
122 System.loadLibrary("3270"); 139 System.loadLibrary("3270");
123 } 140 }
124 141
125 - lib3270(SharedPreferences settings) 142 + lib3270(Activity act)
126 { 143 {
127 String toggle[] = { "dstrace", "screentrace", "eventtrace", "reconnect" }; 144 String toggle[] = { "dstrace", "screentrace", "eventtrace", "reconnect" };
128 145
129 - this.settings = settings; 146 + setActivity(act);
130 this.screenState = -1; 147 this.screenState = -1;
131 148
132 for(int f = 0; f < toggle.length; f++) 149 for(int f = 0; f < toggle.length; f++)
@@ -215,7 +232,7 @@ public class lib3270 @@ -215,7 +232,7 @@ public class lib3270
215 // Connecta no host 232 // Connecta no host
216 SocketFactory socketFactory; 233 SocketFactory socketFactory;
217 String hostname = settings.getString("hostname",""); 234 String hostname = settings.getString("hostname","");
218 - Integer port = new Integer(settings.getString("port","23")); 235 + Integer port = Integer.valueOf(settings.getString("port", "23"));
219 236
220 if (hostname == "" || port == 0) 237 if (hostname == "" || port == 0)
221 return false; 238 return false;
@@ -339,9 +356,76 @@ public class lib3270 @@ -339,9 +356,76 @@ public class lib3270
339 mHandler.sendMessage(msg); 356 mHandler.sendMessage(msg);
340 } 357 }
341 358
  359 + void initialize()
  360 + {
  361 + if(initialized)
  362 + return;
  363 +
  364 + initialized = true;
  365 +
  366 + if(settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false))
  367 + connect();
  368 + }
  369 +
  370 + void setActivity(Activity act)
  371 + {
  372 + this.mainact = act;
  373 + this.settings = PreferenceManager.getDefaultSharedPreferences(act);
  374 + this.res = act.getResources();
  375 + }
  376 +
  377 + WebView setView(WebView v)
  378 + {
  379 + this.view = v;
  380 +
  381 + view.addJavascriptInterface(this, "pw3270");
  382 +
  383 + view.setWebChromeClient(new WebChromeClient());
  384 +
  385 + view.getSettings().setBuiltInZoomControls(true);
  386 + view.getSettings().setSupportZoom(true);
  387 + view.getSettings().setUseWideViewPort(true);
  388 + view.getSettings().setLoadWithOverviewMode(true);
  389 + view.getSettings().setJavaScriptEnabled(true);
  390 +
  391 + view.setWebViewClient(new WebViewClient()
  392 + {
  393 +
  394 + @Override
  395 + public WebResourceResponse shouldInterceptRequest(WebView view, String url)
  396 + {
  397 + int id = R.raw.index;
  398 + String mime = "text/html";
  399 + int pos = url.lastIndexOf("/");
  400 +
  401 + if(pos >=0 )
  402 + url = url.substring(pos+1);
  403 +
  404 + Log.i(TAG,"Loading [" + url + "]");
  405 +
  406 + if(url.equalsIgnoreCase("jsmain.js"))
  407 + {
  408 + id = R.raw.jsmain;
  409 + }
  410 + else if(url.equalsIgnoreCase("theme.css"))
  411 + {
  412 + mime = "text/css";
  413 + id = R.raw.theme;
  414 + }
  415 +
  416 + // http://developer.android.com/reference/android/webkit/WebResourceResponse.html
  417 + return new WebResourceResponse(mime,"utf-8",mainact.getResources().openRawResource(id));
  418 + }
  419 +
  420 + });
  421 +
  422 + return view;
  423 +
  424 + }
  425 +
342 /*---[ Signal methods ]--------------------------------------------------*/ 426 /*---[ Signal methods ]--------------------------------------------------*/
343 427
344 - protected boolean showProgramMessage(int id) 428 + protected void showProgramMessage(int id)
345 { 429 {
346 switch(id) 430 switch(id)
347 { 431 {
@@ -350,7 +434,7 @@ public class lib3270 @@ -350,7 +434,7 @@ public class lib3270
350 { 434 {
351 screenState = 0; 435 screenState = 0;
352 Log.v(TAG, "Status changed to NONE"); 436 Log.v(TAG, "Status changed to NONE");
353 - updateScreen(); 437 + view.reload();
354 } 438 }
355 break; 439 break;
356 440
@@ -379,14 +463,36 @@ public class lib3270 @@ -379,14 +463,36 @@ public class lib3270
379 // 14 LIB3270_MESSAGE_CONNECTING 463 // 14 LIB3270_MESSAGE_CONNECTING
380 464
381 default: 465 default:
382 - return false; 466 + String message[] = res.getStringArray(R.array.program_msg);
  467 + try
  468 + {
  469 + dlgSysMessage.setMessage(message[id]);
  470 + } catch(Exception e)
  471 + {
  472 + dlgSysMessage.setMessage(e.getLocalizedMessage());
  473 + }
  474 + dlgSysMessage.show();
383 } 475 }
384 -  
385 - return true;  
386 } 476 }
387 477
388 protected void showPopupMessage(int type, String title, String text, String info) 478 protected void showPopupMessage(int type, String title, String text, String info)
389 { 479 {
  480 + Log.v(TAG,"Popup Message:");
  481 + Log.v(TAG,title);
  482 + Log.v(TAG,text);
  483 + Log.v(TAG,info);
  484 +
  485 + AlertDialog d = new AlertDialog.Builder(mainact).create();
  486 +
  487 + if(title != "")
  488 + d.setTitle(title);
  489 +
  490 + if(text != "")
  491 + d.setMessage(text);
  492 +
  493 + d.setCancelable(true);
  494 + dlgSysMessage.hide();
  495 + d.show();
390 } 496 }
391 497
392 protected void info(String tag, String msg) 498 protected void info(String tag, String msg)
@@ -403,10 +509,6 @@ public class lib3270 @@ -403,10 +509,6 @@ public class lib3270
403 { 509 {
404 } 510 }
405 511
406 - protected void updateScreen()  
407 - {  
408 - }  
409 -  
410 public void pfkey(int id) 512 public void pfkey(int id)
411 { 513 {
412 sendPFkey(id); 514 sendPFkey(id);
@@ -427,10 +529,6 @@ public class lib3270 @@ -427,10 +529,6 @@ public class lib3270
427 postMessage(8, 0, 0); 529 postMessage(8, 0, 0);
428 } 530 }
429 531
430 - public void hideProgressDialog()  
431 - {  
432 - }  
433 -  
434 public void showProgressDialog(String msg) 532 public void showProgressDialog(String msg)
435 { 533 {
436 } 534 }
@@ -477,6 +575,25 @@ public class lib3270 @@ -477,6 +575,25 @@ public class lib3270
477 refresh = true; 575 refresh = true;
478 } 576 }
479 577
  578 +// @SuppressWarnings("unused")
  579 + public String getscreencontents()
  580 + {
  581 + String text;
  582 +
  583 + try
  584 + {
  585 + text = new String(getHTML(),getEncoding());
  586 + }
  587 + catch(Exception e)
  588 + {
  589 + Log.e(TAG,e.getLocalizedMessage());
  590 + return "";
  591 + }
  592 +
  593 + return text;
  594 + }
  595 +
  596 +
480 /*---[ Native calls ]----------------------------------------------------*/ 597 /*---[ Native calls ]----------------------------------------------------*/
481 private native int processEvents(); 598 private native int processEvents();
482 599
src/include/lib3270/action_table.h
@@ -26,7 +26,6 @@ @@ -26,7 +26,6 @@
26 * erico.mendonca@gmail.com (Erico Mascarenhas de Mendonça) 26 * erico.mendonca@gmail.com (Erico Mascarenhas de Mendonça)
27 * licinio@bb.com.br (Licínio Luis Branco) 27 * licinio@bb.com.br (Licínio Luis Branco)
28 * kraucer@bb.com.br (Kraucer Fernandes Mazuco) 28 * kraucer@bb.com.br (Kraucer Fernandes Mazuco)
29 - * macmiranda@bb.com.br (Marco Aurélio Caldas Miranda)  
30 * 29 *
31 */ 30 */
32 31