package utils;

import android.app.Activity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * Created by layer on 6/5/2015.
 */
public class KeyboardManager {
    private Activity activity;

    private KeyboardManager(Activity activity) {
        this.activity = activity;

        setupUI(activity.getWindow().getDecorView().getRootView());
    }

    public static void on(Activity activity) {
        new KeyboardManager(activity);
    }

    private void setupUI(View view) {
        //Set up touch listener for non-text box views to hide keyboard.
        if(!(view instanceof EditText)) {
            view.setOnTouchListener(new View.OnTouchListener() {
                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard(activity);
                    return false;
                }
            });
        }

        //If a layout container, iterate over children and seed recursion.
        if (view instanceof ViewGroup) {
            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
                View innerView = ((ViewGroup) view).getChildAt(i);

                setupUI(innerView);
            }
        }
    }

    private void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

    public static void hideKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }
}

@@ -1359,36 +1359,80 @@
 activity) %7B%0A    
-    
+%09if(activity.getCurrentFocus()!=null)%7B%0A        %09
 InputMethodManag
@@ -1534,32 +1534,33 @@
 RVICE);%0A        
+%09
 inputMethodManag
@@ -1631,24 +1631,34 @@
 oken(), 0);%0A
+        %7D%0A
     %7D%0A%0A    p
@@ -1713,20 +1713,64 @@
 ) %7B%0A    
-    
+%09if(activity.getCurrentFocus()!=null)%7B%0A        %09
 InputMet
@@ -1888,16 +1888,17 @@
         
+%09
 inputMet
@@ -1977,20 +1977,30 @@
 oken(), 0);%0A
+        %7D%0A
     %7D%0A%7D%0A

update KeyboardManager