From 39a32f62d2dcf99cbdb4ca1f6f4d8f145888a8af Mon Sep 17 00:00:00 2001 From: perry.werneck@gmail.com Date: Mon, 20 Aug 2012 19:45:19 +0000 Subject: [PATCH] Android - Simplificando classe 3270 --- android/Makefile | 7 ++----- android/res/layout/menu.xml | 5 ++--- android/src/br/com/bb/pw3270/PW3270Activity.java | 200 ++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------- android/src/br/com/bb/pw3270/lib3270.java | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------ src/include/lib3270/action_table.h | 1 - 5 files changed, 187 insertions(+), 203 deletions(-) diff --git a/android/Makefile b/android/Makefile index a1e2937..0940012 100644 --- a/android/Makefile +++ b/android/Makefile @@ -86,11 +86,8 @@ pw3270-release-unsigned.apk: icons libs/armeabi/lib3270.so $(JAVASRCDIR)/*.java libs/armeabi/lib3270.so: jni/lib3270jni.h jni/*.cpp @$(NDKBUILD) -icons: $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) \ - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_connect.png) \ - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_disconnect.png) \ - $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_settings.png) - +icons: $(foreach DIR, $(RESOLUTION), res/drawable-$(DIR)/ic_launcher.png) + clean: @rm -fr bin @rm -fr obj diff --git a/android/res/layout/menu.xml b/android/res/layout/menu.xml index 4d761d5..95c12aa 100644 --- a/android/res/layout/menu.xml +++ b/android/res/layout/menu.xml @@ -4,12 +4,11 @@ android:layout_height="match_parent" > + diff --git a/android/src/br/com/bb/pw3270/PW3270Activity.java b/android/src/br/com/bb/pw3270/PW3270Activity.java index fe52b57..946bd21 100644 --- a/android/src/br/com/bb/pw3270/PW3270Activity.java +++ b/android/src/br/com/bb/pw3270/PW3270Activity.java @@ -32,16 +32,16 @@ package br.com.bb.pw3270; import android.app.Activity; import android.os.Bundle; -import android.preference.PreferenceManager; +// import android.preference.PreferenceManager; import android.util.Log; import android.content.Intent; -import android.content.SharedPreferences; +//import android.content.SharedPreferences; import android.content.res.*; -import android.app.AlertDialog; +// import android.app.AlertDialog; import android.webkit.WebView; -import android.webkit.WebViewClient; -import android.webkit.WebResourceResponse; -import android.webkit.WebChromeClient; +// import android.webkit.WebViewClient; +// import android.webkit.WebResourceResponse; +// import android.webkit.WebChromeClient; import android.app.ProgressDialog; import android.view.Menu; import android.view.MenuInflater; @@ -54,176 +54,45 @@ import android.view.MenuItem; public class PW3270Activity extends Activity { private static final String TAG = "pw3270"; - - private static terminal host; + private static lib3270 host = null; - private Resources res ; - private WebView view; - private Activity mainact = this; - private ProgressDialog dlgSysMessage; - - private class terminal extends lib3270 - { - - terminal(SharedPreferences settings) - { - super(settings); - } - - public void hideProgressDialog() - { - dlgSysMessage.hide(); - } - - protected void updateScreen() - { - view.reload(); - } - - protected boolean showProgramMessage(int id) - { - if(!super.showProgramMessage(id)) - { - String message[] = res.getStringArray(R.array.program_msg); - try - { - dlgSysMessage.setMessage(message[id]); - } catch(Exception e) - { - dlgSysMessage.setMessage(e.getLocalizedMessage()); - } - dlgSysMessage.show(); - } - return true; - } - - protected void showPopupMessage(int type, String title, String text, String info) - { - Log.v(TAG,"Popup Message:"); - Log.v(TAG,title); - Log.v(TAG,text); - Log.v(TAG,info); - - AlertDialog d = new AlertDialog.Builder(mainact).create(); - - if(title != "") - d.setTitle(title); - - if(text != "") - d.setMessage(text); - - d.setCancelable(true); - hideProgressDialog(); - d.show(); - } - - @SuppressWarnings("unused") - public String getscreencontents() - { - String text; - - try - { - text = new String(getHTML(),getEncoding()); - } - catch(Exception e) - { - Log.e(TAG,e.getLocalizedMessage()); - return ""; - } - - return text; - } - - }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - res = getResources(); - initUI(); + initUI(); } protected void initUI() { - boolean autoconnect = false; - SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); - + if(host == null) + host = new lib3270(this); + else + host.setActivity(this); + Log.d(TAG, "Initializing UI"); - if(dlgSysMessage == null) - { +// if(host.dlgSysMessage == null) +// { // Cria dialogo para as mensagems de sistema - Log.d(TAG, "Creating dlgSysMessage"); - dlgSysMessage = new ProgressDialog(this); - dlgSysMessage.setCancelable(false); - dlgSysMessage.setTitle(res.getString(R.string.wait)); - } +// Log.d(TAG, "Creating dlgSysMessage"); + host.dlgSysMessage = new ProgressDialog(this); + host.dlgSysMessage.setCancelable(false); + host.dlgSysMessage.setTitle(host.res.getString(R.string.wait)); +// } - if(host == null) - { - Log.d(TAG, "Creating terminal object"); - host = new terminal(settings); - autoconnect = settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false); - } +// Log.d(TAG, "Creating terminal object"); +// autoconnect = settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false); - if(view == null) - { - // Reference: - // http://developer.android.com/reference/android/webkit/WebView.html - Log.d(TAG, "Creating Webview"); - view = new WebView(this); - - view.addJavascriptInterface(host, "pw3270"); - - view.setWebChromeClient(new WebChromeClient()); - - view.getSettings().setBuiltInZoomControls(true); - view.getSettings().setSupportZoom(true); - view.getSettings().setUseWideViewPort(true); - view.getSettings().setLoadWithOverviewMode(true); - view.getSettings().setJavaScriptEnabled(true); - - view.setWebViewClient(new WebViewClient() - { - - @Override - public WebResourceResponse shouldInterceptRequest(WebView view, String url) - { - int id = R.raw.index; - String mime = "text/html"; - int pos = url.lastIndexOf("/"); - - if(pos >=0 ) - url = url.substring(pos+1); - - Log.i(TAG,"Loading [" + url + "]"); - - if(url.equalsIgnoreCase("jsmain.js")) - { - id = R.raw.jsmain; - } - else if(url.equalsIgnoreCase("theme.css")) - { - mime = "text/css"; - id = R.raw.theme; - } - - // http://developer.android.com/reference/android/webkit/WebResourceResponse.html - return new WebResourceResponse(mime,"utf-8",getResources().openRawResource(id)); - } - - }); - } - - setContentView(view); - view.loadUrl("file:index.html"); - - if(autoconnect) - host.connect(); + // Reference: + // http://developer.android.com/reference/android/webkit/WebView.html + Log.d(TAG, "Creating Webview"); + setContentView(host.setView(new WebView(this))); + host.view.loadUrl("file:index.html"); + host.initialize(); } @Override @@ -250,10 +119,14 @@ public class PW3270Activity extends Activity break; case R.id.settings: - Intent myIntent = new Intent(view.getContext(), SettingsActivity.class); + Intent myIntent = new Intent(host.view.getContext(), SettingsActivity.class); startActivityForResult(myIntent, 0); break; + case R.id.reload: + host.view.reload(); + break; + default: return super.onOptionsItemSelected(item); } @@ -274,7 +147,7 @@ public class PW3270Activity extends Activity { super.onSaveInstanceState(outState); // Save the state of the WebView - view.saveState(outState); + host.view.saveState(outState); } @Override @@ -282,9 +155,8 @@ public class PW3270Activity extends Activity { super.onRestoreInstanceState(savedInstanceState); // Restore the state of the WebView - view.restoreState(savedInstanceState); + host.view.restoreState(savedInstanceState); + host.view.reload(); } - - } diff --git a/android/src/br/com/bb/pw3270/lib3270.java b/android/src/br/com/bb/pw3270/lib3270.java index c2a1410..88ada0c 100644 --- a/android/src/br/com/bb/pw3270/lib3270.java +++ b/android/src/br/com/bb/pw3270/lib3270.java @@ -30,11 +30,20 @@ package br.com.bb.pw3270; import java.lang.Thread; +import android.app.Activity; +import android.app.AlertDialog; +import android.app.ProgressDialog; import android.content.SharedPreferences; +import android.content.res.Resources; import android.os.Handler; import android.os.Message; import android.os.CountDownTimer; +import android.preference.PreferenceManager; import android.util.Log; +import android.webkit.WebChromeClient; +import android.webkit.WebResourceResponse; +import android.webkit.WebView; +import android.webkit.WebViewClient; import javax.net.SocketFactory; import java.net.Socket; import javax.net.ssl.SSLSocketFactory; @@ -45,23 +54,28 @@ import java.io.DataOutputStream; public class lib3270 { - private static final String TAG = "lib3270"; + private static final String TAG = "lib3270"; + protected int screenState = -1; - protected int screenState = -1; - - private lib3270 hSession = this; + private lib3270 hSession = this; + + public ProgressDialog dlgSysMessage = null; + public WebView view = null; + public Resources res = null; + public Activity mainact = null; - private static NetworkThread mainloop = null; - private static boolean connected = false; - private static boolean reconnect = false; - private static boolean refresh = true; - private static Socket sock = null; + private static boolean initialized = false; + private static NetworkThread mainloop = null; + private static boolean connected = false; + private static boolean reconnect = false; + private static boolean refresh = true; + private static Socket sock = null; - private static DataOutputStream outData = null; - private static DataInputStream inData = null; + private static DataOutputStream outData = null; + private static DataInputStream inData = null; - protected SharedPreferences settings; + protected SharedPreferences settings; // Define the Handler that receives messages from the thread final Handler mHandler = new Handler() @@ -71,7 +85,10 @@ public class lib3270 switch (msg.what) { case 0: // Reconnect - Log.d(TAG,"----------------------------Reconnecting"); + if(!hSession.isConnected()) + { + Log.d(TAG,"------------------------------------Connecting"); + } break; case 1: // OIA message has changed @@ -101,7 +118,7 @@ public class lib3270 break; case 7: // ready - hideProgressDialog(); + dlgSysMessage.hide(); break; case 8: // busy @@ -122,11 +139,11 @@ public class lib3270 System.loadLibrary("3270"); } - lib3270(SharedPreferences settings) + lib3270(Activity act) { String toggle[] = { "dstrace", "screentrace", "eventtrace", "reconnect" }; - this.settings = settings; + setActivity(act); this.screenState = -1; for(int f = 0; f < toggle.length; f++) @@ -215,7 +232,7 @@ public class lib3270 // Connecta no host SocketFactory socketFactory; String hostname = settings.getString("hostname",""); - Integer port = new Integer(settings.getString("port","23")); + Integer port = Integer.valueOf(settings.getString("port", "23")); if (hostname == "" || port == 0) return false; @@ -339,9 +356,76 @@ public class lib3270 mHandler.sendMessage(msg); } + void initialize() + { + if(initialized) + return; + + initialized = true; + + if(settings.getString("hostname","") != "" && settings.getBoolean("autoconnect",false)) + connect(); + } + + void setActivity(Activity act) + { + this.mainact = act; + this.settings = PreferenceManager.getDefaultSharedPreferences(act); + this.res = act.getResources(); + } + + WebView setView(WebView v) + { + this.view = v; + + view.addJavascriptInterface(this, "pw3270"); + + view.setWebChromeClient(new WebChromeClient()); + + view.getSettings().setBuiltInZoomControls(true); + view.getSettings().setSupportZoom(true); + view.getSettings().setUseWideViewPort(true); + view.getSettings().setLoadWithOverviewMode(true); + view.getSettings().setJavaScriptEnabled(true); + + view.setWebViewClient(new WebViewClient() + { + + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, String url) + { + int id = R.raw.index; + String mime = "text/html"; + int pos = url.lastIndexOf("/"); + + if(pos >=0 ) + url = url.substring(pos+1); + + Log.i(TAG,"Loading [" + url + "]"); + + if(url.equalsIgnoreCase("jsmain.js")) + { + id = R.raw.jsmain; + } + else if(url.equalsIgnoreCase("theme.css")) + { + mime = "text/css"; + id = R.raw.theme; + } + + // http://developer.android.com/reference/android/webkit/WebResourceResponse.html + return new WebResourceResponse(mime,"utf-8",mainact.getResources().openRawResource(id)); + } + + }); + + return view; + + } + /*---[ Signal methods ]--------------------------------------------------*/ - protected boolean showProgramMessage(int id) + protected void showProgramMessage(int id) { switch(id) { @@ -350,7 +434,7 @@ public class lib3270 { screenState = 0; Log.v(TAG, "Status changed to NONE"); - updateScreen(); + view.reload(); } break; @@ -379,14 +463,36 @@ public class lib3270 // 14 LIB3270_MESSAGE_CONNECTING default: - return false; + String message[] = res.getStringArray(R.array.program_msg); + try + { + dlgSysMessage.setMessage(message[id]); + } catch(Exception e) + { + dlgSysMessage.setMessage(e.getLocalizedMessage()); + } + dlgSysMessage.show(); } - - return true; } protected void showPopupMessage(int type, String title, String text, String info) { + Log.v(TAG,"Popup Message:"); + Log.v(TAG,title); + Log.v(TAG,text); + Log.v(TAG,info); + + AlertDialog d = new AlertDialog.Builder(mainact).create(); + + if(title != "") + d.setTitle(title); + + if(text != "") + d.setMessage(text); + + d.setCancelable(true); + dlgSysMessage.hide(); + d.show(); } protected void info(String tag, String msg) @@ -403,10 +509,6 @@ public class lib3270 { } - protected void updateScreen() - { - } - public void pfkey(int id) { sendPFkey(id); @@ -427,10 +529,6 @@ public class lib3270 postMessage(8, 0, 0); } - public void hideProgressDialog() - { - } - public void showProgressDialog(String msg) { } @@ -477,6 +575,25 @@ public class lib3270 refresh = true; } +// @SuppressWarnings("unused") + public String getscreencontents() + { + String text; + + try + { + text = new String(getHTML(),getEncoding()); + } + catch(Exception e) + { + Log.e(TAG,e.getLocalizedMessage()); + return ""; + } + + return text; + } + + /*---[ Native calls ]----------------------------------------------------*/ private native int processEvents(); diff --git a/src/include/lib3270/action_table.h b/src/include/lib3270/action_table.h index 31de63c..eefe172 100644 --- a/src/include/lib3270/action_table.h +++ b/src/include/lib3270/action_table.h @@ -26,7 +26,6 @@ * erico.mendonca@gmail.com (Erico Mascarenhas de Mendonça) * licinio@bb.com.br (Licínio Luis Branco) * kraucer@bb.com.br (Kraucer Fernandes Mazuco) - * macmiranda@bb.com.br (Marco Aurélio Caldas Miranda) * */ -- libgit2 0.21.2