/*
 * Copyright 2015 Michael Bel
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.app.material;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.view.WindowManager;

import java.util.Hashtable;

public class AndroidUtilities {

    public static float density = 1;
    public static Point displaySize = new Point();
    public static boolean usingHardwareInput;
    public static volatile Handler applicationHandler;
    public static DisplayMetrics displayMetrics = new DisplayMetrics();
    private static final Hashtable<String, Typeface> typefaceCache = new Hashtable<>();

    public static int dp (Context context, float value) {
        if (value == 0) {
            return 0;
        }

        return (int) Math.ceil(context.getResources().getDisplayMetrics().density * value);
    }

    public static float dpf2(Context context, float value) {
        if (value == 0) {
            return 0;
        }
        return context.getResources().getDisplayMetrics().density * value;
    }

    public static float getDensity(Context context) {
        density = context.getResources().getDisplayMetrics().density;
        return density;
    }

    public static void runOnUIThread(Context context, Runnable runnable) {
        runOnUIThread(context, runnable, 0);
    }

    public static void runOnUIThread(Context context, Runnable runnable, long delay) {
        if (delay == 0) {
            applicationHandler = new Handler(context.getMainLooper());
            applicationHandler.post(runnable);
        } else {
            applicationHandler.postDelayed(runnable, delay);
        }
    }

    public static void cancelRunOnUIThread(Context context, Runnable runnable) {
        applicationHandler = new Handler(context.getMainLooper()); // ???
        applicationHandler.removeCallbacks(runnable);
    }

    public static void checkDisplaySize(Context context) {
        checkDisplaySize(context);

        try {
            Configuration configuration = context.getResources().getConfiguration();
            usingHardwareInput = configuration.keyboard != Configuration.KEYBOARD_NOKEYS && configuration.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO;
            WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            if (manager != null) {
                Display display = manager.getDefaultDisplay();
                if (display != null) {
                    display.getMetrics(displayMetrics);
                    if (Build.VERSION.SDK_INT < 13) {
                        displaySize.set(display.getWidth(), display.getHeight());
                    } else {
                        display.getSize(displaySize);
                    }
                }
            }
        } catch (Exception ignored) {}
    }

    public static boolean isPortrait(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    }

    public static boolean isLandscape(Context context) {
        return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
    }

    public static Drawable getIcon(Context context, int resource, int colorFilter) {
        Drawable iconDrawable = context.getResources().getDrawable(resource, null);

        if (iconDrawable != null) {
            iconDrawable.mutate().setColorFilter(colorFilter, PorterDuff.Mode.MULTIPLY);
        }

        return iconDrawable;
    }

    public static Typeface getTypeface(Context context, String assetPath) {
        synchronized (typefaceCache) {
            if (!typefaceCache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(context.getAssets(), assetPath);
                    typefaceCache.put(assetPath, t);
                } catch (Exception e) {
                    return null;
                }
            }

            return typefaceCache.get(assetPath);
        }
    }

    public static int getContextColor(Context context, int androidAttr) {
        TypedValue typedValue = new TypedValue();
        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{androidAttr});
        int color = a.getColor(0, 0);
        a.recycle();
        return color;
    }

    public static int selectableItemBackground(Context context) {
        int[] attrs = new int[]{org.app.material.R.attr.selectableItemBackground};
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int backgroundResource = typedArray.getResourceId(0, 0);
        typedArray.recycle();

        return backgroundResource;
    }

    public static int selectableItemBackgroundBorderless(Context context) {
        int[] attrs = new int[]{R.attr.selectableItemBackgroundBorderless};
        TypedArray typedArray = context.obtainStyledAttributes(attrs);
        int backgroundResource = typedArray.getResourceId(0, 0);
        typedArray.recycle();

        return backgroundResource;
    }

    public static Drawable customSelector(Context context) {
        return context.getDrawable(R.drawable.list_selector);
    }

    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");

        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }

        return result ;
    }
}
@@ -1041,22 +1041,100 @@
 id.view.
-Window
+View;%0Aimport android.view.WindowManager;%0Aimport android.view.inputmethod.InputMethod
 Manager;
@@ -6452,10 +6452,986 @@
  ;%0A    %7D
+%0A%0A    public static void showKeyboard(View view) %7B%0A        if (view == null) %7B%0A            return;%0A        %7D%0A        InputMethodManager inputManager = (InputMethodManager)view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);%0A        inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);%0A    %7D%0A%0A    public static boolean isKeyboardShowed(View view) %7B%0A        if (view == null) %7B%0A            return false;%0A        %7D%0A        InputMethodManager inputManager = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);%0A        return inputManager.isActive(view);%0A    %7D%0A%0A    public static void hideKeyboard(View view) %7B%0A        if (view == null) %7B%0A            return;%0A        %7D%0A        InputMethodManager imm = (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);%0A        if (!imm.isActive()) %7B%0A            return;%0A        %7D%0A        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);%0A    %7D
 %0A%7D

add methods to display and hide the keyboard