| 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | package org.chromium.ui; |
| 6 | |
| 7 | import android.content.Context; |
| 8 | import android.graphics.Rect; |
| 9 | import android.view.View; |
| 10 | import android.view.ViewGroup; |
| 11 | import android.view.inputmethod.InputMethodManager; |
| 12 | |
| 13 | /** |
| 14 | * Utility functions for common Android UI tasks. |
| 15 | * This class is not supposed to be instantiated. |
| 16 | */ |
| 17 | public class UiUtils { |
| 18 | /** |
| 19 | * Guards this class from being instantiated. |
| 20 | */ |
| 21 | private UiUtils() { |
| 22 | } |
| 23 | |
| 24 | /** The minimum size of the bottom margin below the app to detect a keyboard. */ |
| 25 | private static float KEYBOARD_DETECT_BOTTOM_THRESHOLD_DP = 100; |
| 26 | |
| 27 | /** |
| 28 | * Shows the software keyboard if necessary. |
| 29 | * @param view The currently focused {@link View}, which would receive soft keyboard input. |
| 30 | */ |
| 31 | public static void showKeyboard(View view) { |
| 32 | InputMethodManager imm = |
| 33 | (InputMethodManager) view.getContext().getSystemService( |
| 34 | Context.INPUT_METHOD_SERVICE); |
| 35 | // Only shows soft keyboard if there isn't an open physical keyboard. |
| 36 | imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Hides the keyboard. |
| 41 | * @param view The {@link View} that is currently accepting input. |
| 42 | * @return Whether the keyboard was visible before. |
| 43 | */ |
| 44 | public static boolean hideKeyboard(View view) { |
| 45 | InputMethodManager imm = |
| 46 | (InputMethodManager) view.getContext().getSystemService( |
| 47 | Context.INPUT_METHOD_SERVICE); |
| 48 | return imm.hideSoftInputFromWindow(view.getWindowToken(), 0); |
| 49 | } |
| 50 | |
| 51 | public static boolean isKeyboardShowing(Context context, View view) { |
| 52 | View rootView = view.getRootView(); |
| 53 | if (rootView == null) return false; |
| 54 | Rect appRect = new Rect(); |
| 55 | rootView.getWindowVisibleDisplayFrame(appRect); |
| 56 | final float screenHeight = context.getResources().getDisplayMetrics().heightPixels; |
| 57 | final float bottomMargin = Math.abs(appRect.bottom - screenHeight); |
| 58 | final float density = context.getResources().getDisplayMetrics().density; |
| 59 | return bottomMargin > KEYBOARD_DETECT_BOTTOM_THRESHOLD_DP * density; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Inserts a {@link View} into a {@link ViewGroup} after directly before a given {@View}. |
| 64 | * @param container The {@link View} to add newView to. |
| 65 | * @param newView The new {@link View} to add. |
| 66 | * @param existingView The {@link View} to insert the newView before. |
| 67 | * @return The index where newView was inserted, or -1 if it was not inserted. |
| 68 | */ |
| 69 | public static int insertBefore(ViewGroup container, View newView, View existingView) { |
| 70 | return insertView(container, newView, existingView, false); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Inserts a {@link View} into a {@link ViewGroup} after directly after a given {@View}. |
| 75 | * @param container The {@link View} to add newView to. |
| 76 | * @param newView The new {@link View} to add. |
| 77 | * @param existingView The {@link View} to insert the newView after. |
| 78 | * @return The index where newView was inserted, or -1 if it was not inserted. |
| 79 | */ |
| 80 | public static int insertAfter(ViewGroup container, View newView, View existingView) { |
| 81 | return insertView(container, newView, existingView, true); |
| 82 | } |
| 83 | |
| 84 | private static int insertView( |
| 85 | ViewGroup container, View newView, View existingView, boolean after) { |
| 86 | // See if the view has already been added. |
| 87 | int index = container.indexOfChild(newView); |
| 88 | if (index >= 0) return index; |
| 89 | |
| 90 | // Find the location of the existing view. |
| 91 | index = container.indexOfChild(existingView); |
| 92 | if (index < 0) return -1; |
| 93 | |
| 94 | // Add the view. |
| 95 | if (after) index++; |
| 96 | container.addView(newView, index); |
| 97 | return index; |
| 98 | } |
| 99 | } |