Skip to content

Commit

Permalink
Revert Android Studio refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
LionZXY authored Jul 19, 2017
1 parent a1102b7 commit 7df4a75
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions KeyboardUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* Based on the following Stackoverflow answer:
* http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android
*/
public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener {
public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener
{
private final static int MAGIC_NUMBER = 200;

private SoftKeyboardToggleListener mCallback;
Expand All @@ -22,18 +23,20 @@ public class KeyboardUtils implements ViewTreeObserver.OnGlobalLayoutListener {
private float mScreenDensity = 1;
private static HashMap<SoftKeyboardToggleListener, KeyboardUtils> sListenerMap = new HashMap<>();

public interface SoftKeyboardToggleListener {
public interface SoftKeyboardToggleListener
{
void onToggleSoftKeyboard(boolean isVisible);
}


@Override
public void onGlobalLayout() {
public void onGlobalLayout()
{
Rect r = new Rect();
mRootView.getWindowVisibleDisplayFrame(r);

int heightDiff = mRootView.getRootView().getHeight() - (r.bottom - r.top);
float dp = heightDiff / mScreenDensity;
float dp = heightDiff/ mScreenDensity;
boolean isVisible = dp > MAGIC_NUMBER;

if (mCallback != null && (prevValue == null || isVisible != prevValue)) {
Expand All @@ -43,52 +46,57 @@ public void onGlobalLayout() {
}


public static void addKeyboardToggleListener(Activity act, SoftKeyboardToggleListener listener) {
public static void addKeyboardToggleListener(Activity act, SoftKeyboardToggleListener listener)
{
removeKeyboardToggleListener(listener);

sListenerMap.put(listener, new KeyboardUtils(act, listener));
}

public static void removeKeyboardToggleListener(SoftKeyboardToggleListener listener) {
if (sListenerMap.containsKey(listener)) {
public static void removeKeyboardToggleListener(SoftKeyboardToggleListener listener)
{
if(sListenerMap.containsKey(listener))
{
KeyboardUtils k = sListenerMap.get(listener);
k.removeListener();

sListenerMap.remove(listener);
}
}

public static void removeAllKeyboardToggleListeners() {
for (SoftKeyboardToggleListener l : sListenerMap.keySet())
public static void removeAllKeyboardToggleListeners()
{
for(SoftKeyboardToggleListener l : sListenerMap.keySet())
sListenerMap.get(l).removeListener();

sListenerMap.clear();
}

public static void toggleKeyboardVisibility(Context context) {
public static void toggleKeyboardVisibility(Context context)
{
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}

/**
* Closes the keyboard
*
* @param activeView the view with the keyboard focus
*/
public static void forceCloseKeyboard(View activeView) {
public static void forceCloseKeyboard(View activeView)
{
InputMethodManager inputMethodManager = (InputMethodManager) activeView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activeView.getWindowToken(), 0);
}

private void removeListener() {
private void removeListener()
{
mCallback = null;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
mRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}

private KeyboardUtils(Activity act, SoftKeyboardToggleListener listener) {
private KeyboardUtils(Activity act, SoftKeyboardToggleListener listener)
{
mCallback = listener;

mRootView = ((ViewGroup) act.findViewById(android.R.id.content)).getChildAt(0);
Expand Down

0 comments on commit 7df4a75

Please sign in to comment.