package com.ionic.keyboard; 

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView;

import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

public class IonicKeyboard extends CordovaPlugin{

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
        
        //calculate density-independent pixels (dp)
        //http://developer.android.com/guide/practices/screens_support.html
        DisplayMetrics dm = new DisplayMetrics();
        cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        final float density = dm.density;
        
        final CordovaWebView appView = webView;
        
        //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
        final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
        OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
            int previousHeightDiff = 0;
            @Override
            public void onGlobalLayout() {
            	Rect r = new Rect();
                //r will be populated with the coordinates of your view that area still visible.
                rootView.getWindowVisibleDisplayFrame(r);

                int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top);
                int pixelHeightDiff = (int)(heightDiff / density);
                if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
                    appView.sendJavascript("cordova.plugins.Keyboard.isVisible = true");
                    appView.sendJavascript("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");        

                    //deprecated
                    appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});");                   
                }
                else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
                    appView.sendJavascript("cordova.plugins.Keyboard.isVisible = false");
                    appView.sendJavascript("cordova.fireWindowEvent('native.keyboardhide')");

                    //deprecated
                    appView.sendJavascript("cordova.fireWindowEvent('native.hidekeyboard')");
                }
                previousHeightDiff = pixelHeightDiff;
             }
        }; 
        
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
    }
	
	

}

@@ -23,16 +23,59 @@
 oard; %0A%0A
+import org.apache.cordova.CallbackContext;%0A
 import o
@@ -193,16 +193,153 @@
 ebView;%0A
+import org.apache.cordova.PluginResult.Status;%0Aimport org.json.JSONArray;%0Aimport org.json.JSONException;%0A%0Aimport android.content.Context;
 %0Aimport 
@@ -483,16 +483,68 @@
 istener;
+%0Aimport android.view.inputmethod.InputMethodManager;
 %0A%0Apublic
@@ -3066,9 +3066,996 @@
 %7D%0A%09%0A
+    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException %7B%0A        if (%22close%22.equals(action)) %7B%0A            cordova.getThreadPool().execute(new Runnable() %7B%0A                public void run() %7B%0A                %09//http://stackoverflow.com/a/7696791/1091751%0A                %09InputMethodManager inputManager = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);%0A                %09View v = cordova.getActivity().getCurrentFocus();%0A                %09%0A                %09if (v == null) %7B%0A                %09%09callbackContext.error(%22No current focus%22);%0A                %09%7D%0A                %09inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);%0A                    callbackContext.success(); // Thread-safe.%0A                %7D%0A            %7D);%0A            return true;%0A        %7D%0A        return false;  // Returning false results in a %22MethodNotFound%22 error.%0A    %7D%0A
 %09%0A%0A%7D%0A
+%0A

add close() for Android