|
4 | 4 | import org.apache.cordova.CordovaInterface; |
5 | 5 | import org.apache.cordova.CordovaPlugin; |
6 | 6 | import org.apache.cordova.CordovaWebView; |
| 7 | +import org.apache.cordova.PluginResult; |
7 | 8 | import org.apache.cordova.PluginResult.Status; |
8 | 9 | import org.json.JSONArray; |
9 | 10 | import org.json.JSONException; |
|
15 | 16 | import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
16 | 17 | import android.view.inputmethod.InputMethodManager; |
17 | 18 |
|
18 | | -public class IonicKeyboard extends CordovaPlugin{ |
| 19 | +public class IonicKeyboard extends CordovaPlugin { |
19 | 20 |
|
20 | 21 | public void initialize(CordovaInterface cordova, CordovaWebView webView) { |
21 | 22 | super.initialize(cordova, webView); |
22 | | - |
23 | | - //calculate density-independent pixels (dp) |
24 | | - //http://developer.android.com/guide/practices/screens_support.html |
25 | | - DisplayMetrics dm = new DisplayMetrics(); |
26 | | - cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); |
27 | | - final float density = dm.density; |
28 | | - |
29 | | - final CordovaWebView appView = webView; |
30 | | - |
31 | | - //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing |
32 | | - final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView(); |
33 | | - OnGlobalLayoutListener list = new OnGlobalLayoutListener() { |
34 | | - int previousHeightDiff = 0; |
35 | | - @Override |
36 | | - public void onGlobalLayout() { |
37 | | - Rect r = new Rect(); |
38 | | - //r will be populated with the coordinates of your view that area still visible. |
39 | | - rootView.getWindowVisibleDisplayFrame(r); |
40 | | - |
41 | | - int heightDiff = rootView.getRootView().getHeight() - (r.bottom); |
42 | | - int pixelHeightDiff = (int)(heightDiff / density); |
43 | | - if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard... |
44 | | - appView.sendJavascript("cordova.plugins.Keyboard.isVisible = true"); |
45 | | - appView.sendJavascript("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});"); |
46 | | - |
47 | | - //deprecated |
48 | | - appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});"); |
49 | | - } |
50 | | - else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){ |
51 | | - appView.sendJavascript("cordova.plugins.Keyboard.isVisible = false"); |
52 | | - appView.sendJavascript("cordova.fireWindowEvent('native.keyboardhide')"); |
53 | | - |
54 | | - //deprecated |
55 | | - appView.sendJavascript("cordova.fireWindowEvent('native.hidekeyboard')"); |
56 | | - } |
57 | | - previousHeightDiff = pixelHeightDiff; |
58 | | - } |
59 | | - }; |
60 | | - |
61 | | - rootView.getViewTreeObserver().addOnGlobalLayoutListener(list); |
62 | 23 | } |
63 | 24 |
|
64 | 25 | public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { |
@@ -88,9 +49,59 @@ public void run() { |
88 | 49 | }); |
89 | 50 | return true; |
90 | 51 | } |
| 52 | + if ("init".equals(action)) { |
| 53 | + cordova.getThreadPool().execute(new Runnable() { |
| 54 | + public void run() { |
| 55 | + //calculate density-independent pixels (dp) |
| 56 | + //http://developer.android.com/guide/practices/screens_support.html |
| 57 | + DisplayMetrics dm = new DisplayMetrics(); |
| 58 | + cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm); |
| 59 | + final float density = dm.density; |
| 60 | + |
| 61 | + //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing |
| 62 | + final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView(); |
| 63 | + OnGlobalLayoutListener list = new OnGlobalLayoutListener() { |
| 64 | + int previousHeightDiff = 0; |
| 65 | + @Override |
| 66 | + public void onGlobalLayout() { |
| 67 | + Rect r = new Rect(); |
| 68 | + //r will be populated with the coordinates of your view that area still visible. |
| 69 | + rootView.getWindowVisibleDisplayFrame(r); |
| 70 | + |
| 71 | + PluginResult result; |
| 72 | + |
| 73 | + int heightDiff = rootView.getRootView().getHeight() - r.bottom; |
| 74 | + int pixelHeightDiff = (int)(heightDiff / density); |
| 75 | + if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard... |
| 76 | + String msg = "S" + Integer.toString(pixelHeightDiff); |
| 77 | + result = new PluginResult(PluginResult.Status.OK, msg); |
| 78 | + result.setKeepCallback(true); |
| 79 | + callbackContext.sendPluginResult(result); |
| 80 | + } |
| 81 | + else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){ |
| 82 | + String msg = "H"; |
| 83 | + result = new PluginResult(PluginResult.Status.OK, msg); |
| 84 | + result.setKeepCallback(true); |
| 85 | + callbackContext.sendPluginResult(result); |
| 86 | + } |
| 87 | + previousHeightDiff = pixelHeightDiff; |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + rootView.getViewTreeObserver().addOnGlobalLayoutListener(list); |
| 92 | + |
| 93 | + |
| 94 | + PluginResult dataResult = new PluginResult(PluginResult.Status.OK); |
| 95 | + dataResult.setKeepCallback(true); |
| 96 | + callbackContext.sendPluginResult(dataResult); |
| 97 | + } |
| 98 | + }); |
| 99 | + return true; |
| 100 | + } |
91 | 101 | return false; // Returning false results in a "MethodNotFound" error. |
92 | 102 | } |
93 | 103 |
|
94 | 104 |
|
95 | 105 | } |
96 | 106 |
|
| 107 | + |
0 commit comments