From d629345e44c4715d32f2fa85beaca02f9492d1f9 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 28 Oct 2014 15:32:57 +0100 Subject: [PATCH 1/3] Remove depreciated call to sendJavaScript for more recent method --- src/android/IonicKeyboard.java | 40 +++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/android/IonicKeyboard.java b/src/android/IonicKeyboard.java index 0d12680..9ab8c8c 100644 --- a/src/android/IonicKeyboard.java +++ b/src/android/IonicKeyboard.java @@ -4,19 +4,20 @@ import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; -import org.apache.cordova.PluginResult.Status; import org.json.JSONArray; import org.json.JSONException; +import android.annotation.TargetApi; import android.content.Context; import android.graphics.Rect; +import android.os.Build; import android.util.DisplayMetrics; import android.view.View; import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.inputmethod.InputMethodManager; -public class IonicKeyboard extends CordovaPlugin{ - +public class IonicKeyboard extends CordovaPlugin { + public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); @@ -25,9 +26,7 @@ public void initialize(CordovaInterface cordova, CordovaWebView webView) { 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() { @@ -40,19 +39,20 @@ public void onGlobalLayout() { 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)+"});"); + sendJavascript("cordova.plugins.Keyboard.isVisible = true"); + sendJavascript("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});"); //deprecated - appView.sendJavascript("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer.toString(pixelHeightDiff)+"});"); + 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')"); + sendJavascript("cordova.plugins.Keyboard.isVisible = false"); + sendJavascript("cordova.fireWindowEvent('native.keyboardhide')"); //deprecated - appView.sendJavascript("cordova.fireWindowEvent('native.hidekeyboard')"); + sendJavascript("cordova.fireWindowEvent('native.hidekeyboard')"); } previousHeightDiff = pixelHeightDiff; } @@ -90,6 +90,20 @@ public void run() { return false; // Returning false results in a "MethodNotFound" error. } - + @TargetApi(Build.VERSION_CODES.KITKAT) + private void sendJavascript(final String javascript) { + + webView.post(new Runnable() { + @Override + public void run() { + // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/src/com/google/chrome/android/example/jsinterface/MainActivity.java + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + webView.evaluateJavascript(javascript, null); + } else { + webView.loadUrl("javascript:" + javascript); + } + } + }); + } } From 4f873fbf635c00e69615ce4aab725f7468e53d44 Mon Sep 17 00:00:00 2001 From: Maxime Date: Tue, 28 Oct 2014 15:34:42 +0100 Subject: [PATCH 2/3] Replace depreciated method to call JS from native to newer method --- src/android/IonicKeyboard.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/android/IonicKeyboard.java b/src/android/IonicKeyboard.java index 9ab8c8c..2844b01 100644 --- a/src/android/IonicKeyboard.java +++ b/src/android/IonicKeyboard.java @@ -17,7 +17,7 @@ import android.view.inputmethod.InputMethodManager; public class IonicKeyboard extends CordovaPlugin { - + public void initialize(CordovaInterface cordova, CordovaWebView webView) { super.initialize(cordova, webView); From 3a0d5e367865db1c6de5cd343bfd526025d55d0a Mon Sep 17 00:00:00 2001 From: Maxime Date: Wed, 24 Jun 2015 16:51:39 +0200 Subject: [PATCH 3/3] Update code for cordova 5 --- src/android/IonicKeyboard.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/android/IonicKeyboard.java b/src/android/IonicKeyboard.java index c880f7b..eab9480 100644 --- a/src/android/IonicKeyboard.java +++ b/src/android/IonicKeyboard.java @@ -94,12 +94,11 @@ public void run() { @TargetApi(Build.VERSION_CODES.KITKAT) private void sendJavascript(final String javascript) { - webView.post(new Runnable() { + webView.getView().post(new Runnable() { @Override public void run() { - // See: https://github.com/GoogleChrome/chromium-webview-samples/blob/master/jsinterface-example/src/com/google/chrome/android/example/jsinterface/MainActivity.java if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { - webView.evaluateJavascript(javascript, null); + webView.sendJavascript(javascript); } else { webView.loadUrl("javascript:" + javascript); }