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 int density = (int)(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);
                if (heightDiff > 200 && heightDiff != previousHeightDiff) { // if more than 200 pixels, its probably a keyboard...
                	 int keyboardHeight = (int)(heightDiff / density);
                	 appView.sendJavascript("cordova.plugins.Keyboard.isVisible = true");
                     appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(keyboardHeight)+"});");                   
                }
                else if (heightDiff != previousHeightDiff && (previousHeightDiff - heightDiff) > 200){
                	appView.sendJavascript("cordova.plugins.Keyboard.isVisible = false");
                	appView.sendJavascript("cordova.fireWindowEvent('native.hidekeyboard')");
                }
                previousHeightDiff = heightDiff;
             }
        }; 
        
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
    }
	
	

}

@@ -356,17 +356,20 @@
 lugin%7B%0A%0A
-%09
+    
 public v
@@ -1153,17 +1153,20 @@
         
-%09
+    
 int prev
@@ -1677,17 +1677,19 @@
         
-%09
+   
  int key
@@ -1747,17 +1747,19 @@
         
+ 
-%09
+  
  appView
@@ -1816,25 +1816,24 @@
 e = true%22);%0A
- 
             
@@ -2033,24 +2033,25 @@
    else if (
+ 
 heightDiff !
@@ -2075,16 +2075,17 @@
 iff && (
+ 
 previous
@@ -2111,15 +2111,17 @@
 Diff
+ 
 ) %3E 200
+ 
 )%7B%0A 
@@ -2127,33 +2127,36 @@
                 
-%09
+    
 appView.sendJava
@@ -2225,17 +2225,20 @@
         
-%09
+    
 appView.

fix formatting