Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15747 anikendra 1
package com.ionic.keyboard;
2
 
3
import org.apache.cordova.CallbackContext;
4
import org.apache.cordova.CordovaInterface;
5
import org.apache.cordova.CordovaPlugin;
6
import org.apache.cordova.CordovaWebView;
7
import org.apache.cordova.PluginResult.Status;
8
import org.json.JSONArray;
9
import org.json.JSONException;
10
 
11
import android.content.Context;
12
import android.graphics.Rect;
13
import android.util.DisplayMetrics;
14
import android.view.View;
15
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
16
import android.view.inputmethod.InputMethodManager;
17
 
18
public class IonicKeyboard extends CordovaPlugin{
19
 
20
    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
21
        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 - r.top);
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);
62
    }
63
 
64
    public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
65
        if ("close".equals(action)) {
66
            cordova.getThreadPool().execute(new Runnable() {
67
                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
                    } else {
75
                        inputManager.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
76
                        callbackContext.success(); // Thread-safe.
77
                    }
78
                }
79
            });
80
            return true;
81
        }
82
        if ("show".equals(action)) {
83
            cordova.getThreadPool().execute(new Runnable() {
84
                public void run() {
85
                    ((InputMethodManager) cordova.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
86
                    callbackContext.success(); // Thread-safe.
87
                }
88
            });
89
            return true;
90
        }
91
        return false;  // Returning false results in a "MethodNotFound" error.
92
    }
93
 
94
 
95
}
96