Skip to content
This repository was archived by the owner on Apr 2, 2018. It is now read-only.

Commit f710324

Browse files
committed
don't use sendJavascript
1 parent 66926f5 commit f710324

File tree

3 files changed

+80
-42
lines changed

3 files changed

+80
-42
lines changed

plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<issue>https://github.com/driftyco/ionic-plugins-keyboard/issues</issue>
1212

1313
<js-module src="www/keyboard.js" name="keyboard">
14+
<runs/>
1415
<clobbers target="cordova.plugins.Keyboard" />
1516
</js-module>
1617

src/android/IonicKeyboard.java

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.cordova.CordovaInterface;
55
import org.apache.cordova.CordovaPlugin;
66
import org.apache.cordova.CordovaWebView;
7+
import org.apache.cordova.PluginResult;
78
import org.apache.cordova.PluginResult.Status;
89
import org.json.JSONArray;
910
import org.json.JSONException;
@@ -15,50 +16,10 @@
1516
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
1617
import android.view.inputmethod.InputMethodManager;
1718

18-
public class IonicKeyboard extends CordovaPlugin{
19+
public class IonicKeyboard extends CordovaPlugin {
1920

2021
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
2122
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);
6223
}
6324

6425
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
@@ -88,9 +49,59 @@ public void run() {
8849
});
8950
return true;
9051
}
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+
}
91101
return false; // Returning false results in a "MethodNotFound" error.
92102
}
93103

94104

95105
}
96106

107+

www/keyboard.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
var argscheck = require('cordova/argscheck'),
33
utils = require('cordova/utils'),
4-
exec = require('cordova/exec');
4+
exec = require('cordova/exec'),
5+
channel = require('cordova/channel');
56

67

78
var Keyboard = function() {
@@ -31,6 +32,31 @@ Keyboard.styleDark = function(dark) {
3132

3233
Keyboard.isVisible = false;
3334

35+
channel.onCordovaReady.subscribe(function() {
36+
exec(success, null, 'Keyboard', 'init', []);
37+
38+
function success(msg) {
39+
var action = msg.charAt(0);
40+
if ( action === 'S' ) {
41+
var keyboardHeight = msg.substr(1);
42+
cordova.plugins.Keyboard.isVisible = true;
43+
cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight': + keyboardHeight });
44+
45+
//deprecated
46+
cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight': + keyboardHeight });
47+
}
48+
49+
if ( action === 'H' ) {
50+
cordova.plugins.Keyboard.isVisible = false;
51+
cordova.fireWindowEvent('native.keyboardhide');
52+
53+
//deprecated
54+
cordova.fireWindowEvent('native.hidekeyboard');
55+
}
56+
57+
}
58+
});
59+
3460
module.exports = Keyboard;
3561

3662

0 commit comments

Comments
 (0)