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

Remove depreciated call to sendJavaScript for more recent method #40

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions src/android/IonicKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
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);
Expand All @@ -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() {
Expand All @@ -40,19 +39,20 @@ public void onGlobalLayout() {

int heightDiff = rootView.getRootView().getHeight() - (r.bottom);
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;
}
Expand Down Expand Up @@ -91,6 +91,19 @@ public void run() {
return false; // Returning false results in a "MethodNotFound" error.
}


@TargetApi(Build.VERSION_CODES.KITKAT)
private void sendJavascript(final String javascript) {

webView.getView().post(new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webView.sendJavascript(javascript);
} else {
webView.loadUrl("javascript:" + javascript);
}
}
});
}
}