From c7e3b46e2ed2a6128a91a8536216c2041962e4bc Mon Sep 17 00:00:00 2001 From: Joe Bowser Date: Mon, 23 Jan 2012 13:49:55 -0800 Subject: [PATCH] Moving LinearLayoutSoftKeyboardDetect out into its own class and making it more plugin-like --- framework/res/xml/plugins.xml | 1 + framework/src/com/phonegap/DroidGap.java | 82 ----------------- .../src/com/phonegap/KeyboardHandler.java | 34 +++++++ .../LinearLayoutSoftKeyboardDetect.java | 92 +++++++++++++++++++ 4 files changed, 127 insertions(+), 82 deletions(-) create mode 100644 framework/src/com/phonegap/KeyboardHandler.java create mode 100644 framework/src/com/phonegap/LinearLayoutSoftKeyboardDetect.java diff --git a/framework/res/xml/plugins.xml b/framework/res/xml/plugins.xml index 73169370..4d84f599 100644 --- a/framework/res/xml/plugins.xml +++ b/framework/res/xml/plugins.xml @@ -16,4 +16,5 @@ + diff --git a/framework/src/com/phonegap/DroidGap.java b/framework/src/com/phonegap/DroidGap.java index 59e4a15d..1d13e59c 100755 --- a/framework/src/com/phonegap/DroidGap.java +++ b/framework/src/com/phonegap/DroidGap.java @@ -1774,88 +1774,6 @@ public class DroidGap extends PhonegapActivity { }); } - /** - * We are providing this class to detect when the soft keyboard is shown - * and hidden in the web view. - */ - class LinearLayoutSoftKeyboardDetect extends LinearLayout { - - private static final String TAG = "SoftKeyboardDetect"; - - private int oldHeight = 0; // Need to save the old height as not to send redundant events - private int oldWidth = 0; // Need to save old width for orientation change - private int screenWidth = 0; - private int screenHeight = 0; - - public LinearLayoutSoftKeyboardDetect(Context context, int width, int height) { - super(context); - screenWidth = width; - screenHeight = height; - } - - @Override - /** - * Start listening to new measurement events. Fire events when the height - * gets smaller fire a show keyboard event and when height gets bigger fire - * a hide keyboard event. - * - * Note: We are using callbackServer.sendJavascript() instead of - * this.appView.loadUrl() as changing the URL of the app would cause the - * soft keyboard to go away. - * - * @param widthMeasureSpec - * @param heightMeasureSpec - */ - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { - super.onMeasure(widthMeasureSpec, heightMeasureSpec); - - LOG.v(TAG, "We are in our onMeasure method"); - - // Get the current height of the visible part of the screen. - // This height will not included the status bar. - int height = MeasureSpec.getSize(heightMeasureSpec); - int width = MeasureSpec.getSize(widthMeasureSpec); - - LOG.v(TAG, "Old Height = %d", oldHeight); - LOG.v(TAG, "Height = %d", height); - LOG.v(TAG, "Old Width = %d", oldWidth); - LOG.v(TAG, "Width = %d", width); - - // If the oldHeight = 0 then this is the first measure event as the app starts up. - // If oldHeight == height then we got a measurement change that doesn't affect us. - if (oldHeight == 0 || oldHeight == height) { - LOG.d(TAG, "Ignore this event"); - } - // Account for orientation change and ignore this event/Fire orientation change - else if(screenHeight == width) - { - int tmp_var = screenHeight; - screenHeight = screenWidth; - screenWidth = tmp_var; - LOG.v(TAG, "Orientation Change"); - } - // If the height as gotten bigger then we will assume the soft keyboard has - // gone away. - else if (height > oldHeight) { - if (callbackServer != null) { - LOG.v(TAG, "Throw hide keyboard event"); - callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('hidekeyboard');"); - } - } - // If the height as gotten smaller then we will assume the soft keyboard has - // been displayed. - else if (height < oldHeight) { - if (callbackServer != null) { - LOG.v(TAG, "Throw show keyboard event"); - callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');"); - } - } - - // Update the old height for the next event - oldHeight = height; - oldWidth = width; - } - } /** * Load PhoneGap configuration from res/xml/phonegap.xml. diff --git a/framework/src/com/phonegap/KeyboardHandler.java b/framework/src/com/phonegap/KeyboardHandler.java new file mode 100644 index 00000000..953ac717 --- /dev/null +++ b/framework/src/com/phonegap/KeyboardHandler.java @@ -0,0 +1,34 @@ +package com.phonegap; + +import org.json.JSONArray; + +import com.phonegap.api.Plugin; +import com.phonegap.api.PluginResult; + +public class KeyboardHandler extends Plugin { + + + /* + * This will never be called! + * (non-Javadoc) + * @see com.phonegap.api.Plugin#execute(java.lang.String, org.json.JSONArray, java.lang.String) + */ + @Override + public PluginResult execute(String action, JSONArray args, String callbackId) { + // TODO Auto-generated method stub + return null; + } + + @Override + public void onMessage(String id, Object data) + { + if(id.equals("keyboardHidden")) + { + super.sendJavascript("PhoneGap.fireDocumentEvent('hidekeyboard');"); + } + else if(id.equals("keyboardVisible")) + { + super.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');"); + } + } +} diff --git a/framework/src/com/phonegap/LinearLayoutSoftKeyboardDetect.java b/framework/src/com/phonegap/LinearLayoutSoftKeyboardDetect.java new file mode 100644 index 00000000..32a7f6e0 --- /dev/null +++ b/framework/src/com/phonegap/LinearLayoutSoftKeyboardDetect.java @@ -0,0 +1,92 @@ +package com.phonegap; +import com.phonegap.api.LOG; + +import android.content.Context; +import android.view.View.MeasureSpec; +import android.widget.LinearLayout; + + +public class LinearLayoutSoftKeyboardDetect extends LinearLayout { + + private static final String TAG = "SoftKeyboardDetect"; + + private int oldHeight = 0; // Need to save the old height as not to send redundant events + private int oldWidth = 0; // Need to save old width for orientation change + private int screenWidth = 0; + private int screenHeight = 0; + private DroidGap app = null; + + public LinearLayoutSoftKeyboardDetect(Context context, int width, int height) { + super(context); + screenWidth = width; + screenHeight = height; + if(context.getClass().getSimpleName().equals("DroidGap")) + app = (DroidGap) app; + } + + @Override + /** + * Start listening to new measurement events. Fire events when the height + * gets smaller fire a show keyboard event and when height gets bigger fire + * a hide keyboard event. + * + * Note: We are using callbackServer.sendJavascript() instead of + * this.appView.loadUrl() as changing the URL of the app would cause the + * soft keyboard to go away. + * + * @param widthMeasureSpec + * @param heightMeasureSpec + */ + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + + LOG.v(TAG, "We are in our onMeasure method"); + + // Get the current height of the visible part of the screen. + // This height will not included the status bar. + int height = MeasureSpec.getSize(heightMeasureSpec); + int width = MeasureSpec.getSize(widthMeasureSpec); + + LOG.v(TAG, "Old Height = %d", oldHeight); + LOG.v(TAG, "Height = %d", height); + LOG.v(TAG, "Old Width = %d", oldWidth); + LOG.v(TAG, "Width = %d", width); + + // If the oldHeight = 0 then this is the first measure event as the app starts up. + // If oldHeight == height then we got a measurement change that doesn't affect us. + if (oldHeight == 0 || oldHeight == height) { + LOG.d(TAG, "Ignore this event"); + } + // Account for orientation change and ignore this event/Fire orientation change + else if(screenHeight == width) + { + int tmp_var = screenHeight; + screenHeight = screenWidth; + screenWidth = tmp_var; + LOG.v(TAG, "Orientation Change"); + } + // If the height as gotten bigger then we will assume the soft keyboard has + // gone away. + else if (height > oldHeight) { + if(app != null) + app.postMessage("hideKeyboard", null); + } + // If the height as gotten smaller then we will assume the soft keyboard has + // been displayed. + else if (height < oldHeight) { + if(app != null) + app.postMessage("keyboardVisible", null); + /* + if (callbackServer != null) { + LOG.v(TAG, "Throw show keyboard event"); + callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');"); + } + */ + } + + // Update the old height for the next event + oldHeight = height; + oldWidth = width; + } + +}