From 766d50e602644a8fa85801c602edd995c40b6759 Mon Sep 17 00:00:00 2001 From: Hocuri Date: Tue, 29 Sep 2020 12:11:44 +0200 Subject: [PATCH 1/2] Prevent exception (would be caught but is annoying) --- .../components/KeyboardAwareLinearLayout.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java index df1739332..c6e305147 100644 --- a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java +++ b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java @@ -120,20 +120,24 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { @TargetApi(VERSION_CODES.LOLLIPOP) private int getViewInset() { - 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; + // In Android 10 we can't use mAttatchInfo anymore, so we don't even need to try it + // https://developer.android.com/about/versions/10/non-sdk-q + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + 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); } - } 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; } From eaa891c8e7b7f27c6fb577cfa11bdf3f7dd791de Mon Sep 17 00:00:00 2001 From: Hocuri Date: Sat, 31 Oct 2020 18:34:50 +0100 Subject: [PATCH 2/2] Properly fix the problem --- .../components/KeyboardAwareLinearLayout.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java index c6e305147..d98dd158a 100644 --- a/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java +++ b/src/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java @@ -22,11 +22,13 @@ import android.graphics.Rect; import android.os.Build; import android.os.Build.VERSION_CODES; import android.preference.PreferenceManager; -import androidx.appcompat.widget.LinearLayoutCompat; import android.util.AttributeSet; import android.util.Log; import android.view.Surface; import android.view.View; +import android.view.WindowInsets; + +import androidx.appcompat.widget.LinearLayoutCompat; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.util.ServiceUtil; @@ -120,9 +122,13 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { @TargetApi(VERSION_CODES.LOLLIPOP) private int getViewInset() { - // In Android 10 we can't use mAttatchInfo anymore, so we don't even need to try it - // https://developer.android.com/about/versions/10/non-sdk-q - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { + // In Android 10 we can't use mAttatchInfo anymore, instead use the SDK interface: + if (Build.VERSION.SDK_INT >= VERSION_CODES.Q) { + WindowInsets insets = getRootWindowInsets(); + if (insets == null) return 0; + return insets.getStableInsetBottom(); + } else { + // Some older Android versions don't know `getRootViewInsets()` yet, so we still need the old way: try { Field attachInfoField = View.class.getDeclaredField("mAttachInfo"); attachInfoField.setAccessible(true);