/** * Wire * Copyright (C) 2016 Wire Swiss GmbH * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.waz.zclient.ui.utils; import android.app.Activity; import android.content.Context; import android.graphics.Rect; import android.os.IBinder; import android.view.View; import android.view.inputmethod.InputMethodManager; public class KeyboardUtils { private KeyboardUtils() { } public static boolean isKeyboardVisible(Context context) { return getInputMethodManager(context).isActive(); } public static void closeKeyboardIfShown(Activity activity) { if (isKeyboardVisible(activity)) { hideKeyboard(activity); } } public static boolean keyboardIsVisible(View contentView) { Rect r = new Rect(); contentView.getWindowVisibleDisplayFrame(r); int screenHeight = contentView.getRootView().getHeight(); int keyboardHeight = screenHeight - r.bottom; return keyboardHeight > 0; } public static void hideKeyboard(Context context, IBinder token) { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(token, 0); } public static void hideKeyboard(Activity activity) { if (activity.getCurrentFocus() != null) { getInputMethodManager(activity).hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); } } public static boolean showKeyboard(Activity activity) { if (activity != null && activity.getCurrentFocus() != null) { getInputMethodManager(activity).showSoftInput(activity.getCurrentFocus(), InputMethodManager.SHOW_IMPLICIT); return true; } return false; } private static InputMethodManager getInputMethodManager(Context context) { return (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); } public static int getKeyboardHeight(View contentView) { Rect r = new Rect(); contentView.getWindowVisibleDisplayFrame(r); int screenHeight = contentView.getRootView().getHeight(); return screenHeight - r.bottom; } }