Moving LinearLayoutSoftKeyboardDetect out into its own class and making it more plugin-like

This commit is contained in:
Joe Bowser 2012-01-23 13:49:55 -08:00
parent 490a13d3c4
commit c7e3b46e2e
4 changed files with 127 additions and 82 deletions

View File

@ -16,4 +16,5 @@
<plugin name="FileTransfer" value="com.phonegap.FileTransfer"/>
<plugin name="Capture" value="com.phonegap.Capture"/>
<plugin name="Battery" value="com.phonegap.BatteryListener"/>
<plugin name="Keyboard" value="com.phonegap.KeyboardHandler" />
</plugins>

View File

@ -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.

View File

@ -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');");
}
}
}

View File

@ -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;
}
}