Modyfikacja wtyczki ionic-plugin-keyboard

Przy pisaniu aplikacji wymuszających tryb pełnoekranowy także po uruchomieniu (ionic.Platform.isFullScreen = true) może pojawić się problem z przycinaniem contentu przy widocznej klawiaturze. Problem konkretnie na urządzeniach z systemem Android.

Obejściem problemu jest modyfikacja kodu wtyczki (./plugins/ionic-plugin-keyboard/src/android/IonicKeyboard.java):

if ("init".equals(action)) {
    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
        	//calculate density-independent pixels (dp)
            //http://developer.android.com/guide/practices/screens_support.html
            DisplayMetrics dm = new DisplayMetrics();
            cordova.getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            final float density = dm.density;
            final int densityDpi = dm.densityDpi;

            //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() {
                int previousHeightDiff = 0;
                @Override
                public void onGlobalLayout() {
                    Rect r = new Rect();
                    //r will be populated with the coordinates of your view that area still visible.
                    rootView.getWindowVisibleDisplayFrame(r);
                    
                    PluginResult result;
                    
                    int statusHeight = 0;
                    if (densityDpi >= 240)
                    	statusHeight = (int)(38 * density);
                    else if (densityDpi >= 160)
                    	statusHeight = (int)(25 * density);
                    else
                    	statusHeight = (int)(19 * density);
                    
                    int heightDiff = rootView.getRootView().getHeight() - r.bottom + statusHeight;
                    int pixelHeightDiff = (int)(heightDiff / density);
                    if (pixelHeightDiff > 100 && pixelHeightDiff != previousHeightDiff) { // if more than 100 pixels, its probably a keyboard...
                    	String msg = "S" + Integer.toString(pixelHeightDiff);
                        result = new PluginResult(PluginResult.Status.OK, msg);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);
                    }
                    else if ( pixelHeightDiff != previousHeightDiff && ( previousHeightDiff - pixelHeightDiff ) > 100 ){
                    	String msg = "H";
                        result = new PluginResult(PluginResult.Status.OK, msg);
                        result.setKeepCallback(true);
                        callbackContext.sendPluginResult(result);
                    }
                    previousHeightDiff = pixelHeightDiff;
                 }
            };

            rootView.getViewTreeObserver().addOnGlobalLayoutListener(list);
        	
        	
            PluginResult dataResult = new PluginResult(PluginResult.Status.OK);
            dataResult.setKeepCallback(true);
            callbackContext.sendPluginResult(dataResult);
        }
    });
    return true;
}Code language: Java (java)

Opublikowano

w

przez

Komentarze

Leave a Reply