1- package com .ionic .keyboard ;
1+ package com .ionic .keyboard ;
22
33import org .apache .cordova .CallbackContext ;
44import org .apache .cordova .CordovaInterface ;
@@ -19,33 +19,33 @@ public class IonicKeyboard extends CordovaPlugin{
1919
2020 public void initialize (CordovaInterface cordova , CordovaWebView webView ) {
2121 super .initialize (cordova , webView );
22-
22+
2323 //calculate density-independent pixels (dp)
2424 //http://developer.android.com/guide/practices/screens_support.html
2525 DisplayMetrics dm = new DisplayMetrics ();
2626 cordova .getActivity ().getWindowManager ().getDefaultDisplay ().getMetrics (dm );
2727 final float density = dm .density ;
28-
28+
2929 final CordovaWebView appView = webView ;
30-
30+
3131 //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
3232 final View rootView = cordova .getActivity ().getWindow ().getDecorView ().findViewById (android .R .id .content ).getRootView ();
3333 OnGlobalLayoutListener list = new OnGlobalLayoutListener () {
3434 int previousHeightDiff = 0 ;
3535 @ Override
3636 public void onGlobalLayout () {
37- Rect r = new Rect ();
37+ Rect r = new Rect ();
3838 //r will be populated with the coordinates of your view that area still visible.
3939 rootView .getWindowVisibleDisplayFrame (r );
4040
4141 int heightDiff = rootView .getRootView ().getHeight () - (r .bottom - r .top );
4242 int pixelHeightDiff = (int )(heightDiff / density );
4343 if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff ) { // if more than 100 pixels, its probably a keyboard...
4444 appView .sendJavascript ("cordova.plugins.Keyboard.isVisible = true" );
45- appView .sendJavascript ("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer .toString (pixelHeightDiff )+"});" );
45+ appView .sendJavascript ("cordova.fireWindowEvent('native.keyboardshow', { 'keyboardHeight':" + Integer .toString (pixelHeightDiff )+"});" );
4646
4747 //deprecated
48- appView .sendJavascript ("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer .toString (pixelHeightDiff )+"});" );
48+ appView .sendJavascript ("cordova.fireWindowEvent('native.showkeyboard', { 'keyboardHeight':" + Integer .toString (pixelHeightDiff )+"});" );
4949 }
5050 else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
5151 appView .sendJavascript ("cordova.plugins.Keyboard.isVisible = false" );
@@ -56,31 +56,40 @@ else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelH
5656 }
5757 previousHeightDiff = pixelHeightDiff ;
5858 }
59- };
60-
59+ };
60+
6161 rootView .getViewTreeObserver ().addOnGlobalLayoutListener (list );
6262 }
63-
63+
6464 public boolean execute (String action , JSONArray args , final CallbackContext callbackContext ) throws JSONException {
6565 if ("close" .equals (action )) {
6666 cordova .getThreadPool ().execute (new Runnable () {
6767 public void run () {
68- //http://stackoverflow.com/a/7696791/1091751
69- InputMethodManager inputManager = (InputMethodManager ) cordova .getActivity ().getSystemService (Context .INPUT_METHOD_SERVICE );
70- View v = cordova .getActivity ().getCurrentFocus ();
71-
72- if (v == null ) {
73- callbackContext .error ("No current focus" );
74- }
75- inputManager .hideSoftInputFromWindow (v .getWindowToken (), InputMethodManager .HIDE_NOT_ALWAYS );
68+ //http://stackoverflow.com/a/7696791/1091751
69+ InputMethodManager inputManager = (InputMethodManager ) cordova .getActivity ().getSystemService (Context .INPUT_METHOD_SERVICE );
70+ View v = cordova .getActivity ().getCurrentFocus ();
71+
72+ if (v == null ) {
73+ callbackContext .error ("No current focus" );
74+ }
75+ inputManager .hideSoftInputFromWindow (v .getWindowToken (), InputMethodManager .HIDE_NOT_ALWAYS );
76+ callbackContext .success (); // Thread-safe.
77+ }
78+ });
79+ return true ;
80+ }
81+ if ("show" .equals (action )) {
82+ cordova .getThreadPool ().execute (new Runnable () {
83+ public void run () {
84+ ((InputMethodManager ) cordova .getActivity ().getSystemService (Context .INPUT_METHOD_SERVICE )).toggleSoftInput (0 , InputMethodManager .HIDE_IMPLICIT_ONLY );
7685 callbackContext .success (); // Thread-safe.
7786 }
7887 });
7988 return true ;
8089 }
8190 return false ; // Returning false results in a "MethodNotFound" error.
8291 }
83-
92+
8493
8594}
8695
0 commit comments