From 0f7f458ce0a8a1ab2d7dc33aabe8425b3afb7f20 Mon Sep 17 00:00:00 2001 From: Jake McGinty Date: Mon, 8 Jun 2015 15:11:05 -0700 Subject: [PATCH] fix keyboard-height-aware layout in lollipop fixes #3303 // FREEBIE --- .../components/KeyboardAwareLinearLayout.java | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java index 810f39d5fe..f6b2134fdc 100644 --- a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java +++ b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java @@ -21,15 +21,19 @@ import android.app.Activity; import android.content.Context; import android.graphics.Rect; import android.os.Build; +import android.os.Build.VERSION_CODES; import android.preference.PreferenceManager; import android.util.AttributeSet; import android.util.Log; import android.view.Surface; +import android.view.View; import android.view.WindowManager; import android.widget.LinearLayout; import org.thoughtcrime.securesms.R; +import java.lang.reflect.Field; + /** * LinearLayout that, when a view container, will report back when it thinks a soft keyboard * has been opened and what its height would be. @@ -61,7 +65,7 @@ public class KeyboardAwareLinearLayout extends LinearLayout { int res = getResources().getIdentifier("status_bar_height", "dimen", "android"); int statusBarHeight = res > 0 ? getResources().getDimensionPixelSize(res) : 0; - final int availableHeight = this.getRootView().getHeight() - statusBarHeight; + final int availableHeight = this.getRootView().getHeight() - statusBarHeight - getViewInset(); getWindowVisibleDisplayFrame(rect); final int keyboardHeight = availableHeight - (rect.bottom - rect.top); @@ -73,6 +77,29 @@ public class KeyboardAwareLinearLayout extends LinearLayout { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } + public int getViewInset() { + if (Build.VERSION.SDK_INT < VERSION_CODES.LOLLIPOP) { + return 0; + } + + try { + Field attachInfoField = View.class.getDeclaredField("mAttachInfo"); + attachInfoField.setAccessible(true); + Object attachInfo = attachInfoField.get(this); + if (attachInfo != null) { + Field stableInsetsField = attachInfo.getClass().getDeclaredField("mStableInsets"); + stableInsetsField.setAccessible(true); + Rect insets = (Rect)stableInsetsField.get(attachInfo); + return insets.bottom; + } + } catch (NoSuchFieldException nsfe) { + Log.w(TAG, "field reflection error when measuring view inset", nsfe); + } catch (IllegalAccessException iae) { + Log.w(TAG, "access reflection error when measuring view inset", iae); + } + return 0; + } + protected void onKeyboardShown(int keyboardHeight) { Log.w(TAG, "keyboard shown, height " + keyboardHeight);