commonroutesServer/www/plugins/ionic-plugin-keyboard/src/android/IonicKeyboard.java
www/plugins/ionic-plugin-keyboard/src/android/IonicKeyboard.javaRaw
1package io.ionic.keyboard;
2
3import org.apache.cordova.CallbackContext;
4import org.apache.cordova.CordovaInterface;
5import org.apache.cordova.CordovaPlugin;
6import org.apache.cordova.CordovaWebView;
7import org.apache.cordova.PluginResult;
8import org.apache.cordova.PluginResult.Status;
9import org.json.JSONArray;
10import org.json.JSONException;
11
12import android.content.Context;
13import android.graphics.Rect;
14import android.util.DisplayMetrics;
15import android.view.View;
16import android.view.ViewTreeObserver.OnGlobalLayoutListener;
17import android.view.inputmethod.InputMethodManager;
18
19// import additionally required classes for calculating screen height
20import android.view.Display;
21import android.graphics.Point;
22import android.os.Build;
23
24public class IonicKeyboard extends CordovaPlugin {
25
26 public void initialize(CordovaInterface cordova, CordovaWebView webView) {
27 super.initialize(cordova, webView);
28 }
29
30 public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
31 if ("close".equals(action)) {
32 cordova.getThreadPool().execute(new Runnable() {
33 public void run() {
34 //http://stackoverflow.com/a/7696791/1091751
35 InputMethodManager inputManager = (InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
36 View v = cordova.getActivity().getCurrentFocus();
37
38 if (v == null) {
39 callbackContext.error("No current focus");
40 } else {
41 inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
42 callbackContext.success(); // Thread-safe.
43 }
44 }
45 });
46 return true;
47 }
48 if ("show".equals(action)) {
49 cordova.getThreadPool().execute(new Runnable() {
50 public void run() {
51 ((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
52 callbackContext.success(); // Thread-safe.
53 }
54 });
55 return true;
56 }
57 if ("init".equals(action)) {
58 cordova.getThreadPool().execute(new Runnable() {
59 public void run() {
60 //calculate density-independent pixels (dp)
61 //http://developer.android.com/guide/practices/screens_support.html
62 DisplayMetrics dm = new DisplayMetrics();
63 cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
64 final float density = dm.density;
65
66 //http://stackoverflow.com/a/4737265/1091751 detect if keyboard is showing
67 final View rootView = cordova.getActivity().getWindow().getDecorView().findViewById(android.R.id.content).getRootView();
68 OnGlobalLayoutListener list = new OnGlobalLayoutListener() {
69 int previousHeightDiff = 0;
70 @Override
71 public void onGlobalLayout() {
72 Rect r = new Rect();
73 //r will be populated with the coordinates of your view that area still visible.
74 rootView.getWindowVisibleDisplayFrame(r);
75
76 PluginResult result;
77
78 // cache properties for later use
79 int rootViewHeight = rootView.getRootView().getHeight();
80 int resultBottom = r.bottom;
81
82 // calculate screen height differently for android versions >= 21: Lollipop 5.x, Marshmallow 6.x
83 //http://stackoverflow.com/a/29257533/3642890 beware of nexus 5
84 int screenHeight;
85
86 if (Build.VERSION.SDK_INT >= 21) {
87 Display display = cordova.getActivity().getWindowManager().getDefaultDisplay();
88 Point size = new Point();
89 display.getSize(size);
90 screenHeight = size.y;
91 } else {
92 screenHeight = rootViewHeight;
93 }
94
95 int heightDiff = screenHeight - resultBottom;
96
97 int pixelHeightDiff = (int)(heightDiff / density);
98 if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
99 String msg = "S" + Integer.toString(pixelHeightDiff);
100 result = new PluginResult(PluginResult.Status.OK, msg);
101 result.setKeepCallback(true);
102 callbackContext.sendPluginResult(result);
103 }
104 else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
105 String msg = "H";
106 result = new PluginResult(PluginResult.Status.OK, msg);
107 result.setKeepCallback(true);
108 callbackContext.sendPluginResult(result);
109 }
110 previousHeightDiff = pixelHeightDiff;
111 }
112 };
113
114 rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
115
116
117 PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
118 dataResult.setKeepCallback(true);
119 callbackContext.sendPluginResult(dataResult);
120 }
121 });
122 return true;
123 }
124 return false; // Returning false results in a "MethodNotFound" error.
125 }
126
127
128}
129
130