|
| 1 | +package com.ionic.keyboard; |
| 2 | + |
| 3 | +import org.apache.cordova.CordovaInterface; |
| 4 | +import org.apache.cordova.CordovaPlugin; |
| 5 | +import org.apache.cordova.CordovaWebView; |
| 6 | +import org.json.JSONException; |
| 7 | +import org.json.JSONObject; |
| 8 | + |
| 9 | +import android.graphics.Rect; |
| 10 | +import android.util.DisplayMetrics; |
| 11 | +import android.view.View; |
| 12 | +import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
| 13 | + |
| 14 | +public class IonicKeyboard extends CordovaPlugin{ |
| 15 | + |
| 16 | + public void initialize(CordovaInterface cordova, CordovaWebView webView) { |
| 17 | + super.initialize(cordova, webView); |
| 18 | + |
| 19 | + //calculate density-independent pixels (dp) |
| 20 | + //http://developer.android.com/guide/practices/screens_support.html |
| 21 | + DisplayMetrics dm = new DisplayMetrics(); |
| 22 | + cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); |
| 23 | + final int density = (int)(dm.density); |
| 24 | + |
| 25 | + final CordovaWebView appView = webView; |
| 26 | + |
| 27 | + final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView(); |
| 28 | + OnGlobalLayoutListener list = new OnGlobalLayoutListener() { |
| 29 | + int previousHeightDiff = 0; |
| 30 | + @Override |
| 31 | + public void onGlobalLayout() { |
| 32 | + Rect r = new Rect(); |
| 33 | + //r will be populated with the coordinates of your view that area still visible. |
| 34 | + rootView.getWindowVisibleDisplayFrame(r); |
| 35 | + |
| 36 | + int heightDiff = rootView.getRootView().getHeight() - (r.bottom - r.top); |
| 37 | + if (heightDiff > 200 && heightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard... |
| 38 | + int keyboardHeight = (int)(heightDiff / density); |
| 39 | + appView.sendJavascript("cordova.fireWindowEvent('showkeyboard', { 'keyboardHeight':" + Integer.toString(keyboardHeight)+"});"); |
| 40 | + } |
| 41 | + previousHeightDiff = heightDiff; |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + rootView.getViewTreeObserver().addOnGlobalLayoutListener(list); |
| 46 | + } |
| 47 | + |
| 48 | +} |
0 commit comments