From 412ee220ce0c22d95e696a1cac54077c6e14e8ac Mon Sep 17 00:00:00 2001 From: Cody Henthorne Date: Wed, 9 Jun 2021 16:18:55 -0400 Subject: [PATCH] Improve keyboard sizing in bubbled conversations. --- .../components/KeyboardAwareLinearLayout.java | 74 +++++++++++++------ .../conversation/ConversationActivity.java | 1 + app/src/main/res/values/dimens.xml | 1 + 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java b/app/src/main/java/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java index 5ce4dd2a9..dcda73426 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java +++ b/app/src/main/java/org/thoughtcrime/securesms/components/KeyboardAwareLinearLayout.java @@ -1,16 +1,16 @@ /** * Copyright (C) 2014 Open Whisper Systems - * + *

* 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 . */ @@ -23,6 +23,7 @@ import android.os.Build; import android.os.Build.VERSION_CODES; import android.preference.PreferenceManager; import android.util.AttributeSet; +import android.util.DisplayMetrics; import android.view.Surface; import android.view.View; import android.view.WindowInsets; @@ -46,21 +47,25 @@ import java.util.Set; public class KeyboardAwareLinearLayout extends LinearLayoutCompat { private static final String TAG = Log.tag(KeyboardAwareLinearLayout.class); - private final Rect rect = new Rect(); - private final Set hiddenListeners = new HashSet<>(); - private final Set shownListeners = new HashSet<>(); - private final int minKeyboardSize; - private final int minCustomKeyboardSize; - private final int defaultCustomKeyboardSize; - private final int minCustomKeyboardTopMarginPortrait; - private final int minCustomKeyboardTopMarginLandscape; - private final int statusBarHeight; + private final Rect rect = new Rect(); + private final Set hiddenListeners = new HashSet<>(); + private final Set shownListeners = new HashSet<>(); + private final DisplayMetrics displayMetrics = new DisplayMetrics(); + + private final int minKeyboardSize; + private final int minCustomKeyboardSize; + private final int defaultCustomKeyboardSize; + private final int minCustomKeyboardTopMarginPortrait; + private final int minCustomKeyboardTopMarginLandscape; + private final int minCustomKeyboardTopMarginLandscapeBubble; + private final int statusBarHeight; private int viewInset; private boolean keyboardOpen = false; private int rotation = -1; private boolean isFullscreen = false; + private boolean isBubble = false; public KeyboardAwareLinearLayout(Context context) { this(context, null); @@ -72,13 +77,14 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { public KeyboardAwareLinearLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); - minKeyboardSize = getResources().getDimensionPixelSize(R.dimen.min_keyboard_size); - minCustomKeyboardSize = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_size); - defaultCustomKeyboardSize = getResources().getDimensionPixelSize(R.dimen.default_custom_keyboard_size); - minCustomKeyboardTopMarginPortrait = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_top_margin_portrait); - minCustomKeyboardTopMarginLandscape = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_top_margin_portrait); - statusBarHeight = ViewUtil.getStatusBarHeight(this); - viewInset = getViewInset(); + minKeyboardSize = getResources().getDimensionPixelSize(R.dimen.min_keyboard_size); + minCustomKeyboardSize = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_size); + defaultCustomKeyboardSize = getResources().getDimensionPixelSize(R.dimen.default_custom_keyboard_size); + minCustomKeyboardTopMarginPortrait = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_top_margin_portrait); + minCustomKeyboardTopMarginLandscape = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_top_margin_portrait); + minCustomKeyboardTopMarginLandscapeBubble = getResources().getDimensionPixelSize(R.dimen.min_custom_keyboard_top_margin_landscape_bubble); + statusBarHeight = ViewUtil.getStatusBarHeight(this); + viewInset = getViewInset(); } @Override @@ -88,6 +94,10 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } + public void setIsBubble(boolean isBubble) { + this.isBubble = isBubble; + } + private void updateRotation() { int oldRotation = rotation; rotation = getDeviceRotation(); @@ -149,7 +159,7 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { if (attachInfo != null) { Field stableInsetsField = attachInfo.getClass().getDeclaredField("mStableInsets"); stableInsetsField.setAccessible(true); - Rect insets = (Rect)stableInsetsField.get(attachInfo); + Rect insets = (Rect) stableInsetsField.get(attachInfo); if (insets != null) { return insets.bottom; } @@ -197,28 +207,50 @@ public class KeyboardAwareLinearLayout extends LinearLayoutCompat { int rotation = getDeviceRotation(); return rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270; } + private int getDeviceRotation() { - return ServiceUtil.getWindowManager(getContext()).getDefaultDisplay().getRotation(); + if (Build.VERSION.SDK_INT >= 30) { + getContext().getDisplay().getRealMetrics(displayMetrics); + } else { + ServiceUtil.getWindowManager(getContext()).getDefaultDisplay().getRealMetrics(displayMetrics); + } + return displayMetrics.widthPixels > displayMetrics.heightPixels ? Surface.ROTATION_90 : Surface.ROTATION_0; } private int getKeyboardLandscapeHeight() { + if (isBubble) { + return getRootView().getHeight() - minCustomKeyboardTopMarginLandscapeBubble; + } + int keyboardHeight = PreferenceManager.getDefaultSharedPreferences(getContext()) .getInt("keyboard_height_landscape", defaultCustomKeyboardSize); return Util.clamp(keyboardHeight, minCustomKeyboardSize, getRootView().getHeight() - minCustomKeyboardTopMarginLandscape); } private int getKeyboardPortraitHeight() { + if (isBubble) { + return getRootView().getHeight() - minCustomKeyboardTopMarginPortrait; + } + int keyboardHeight = PreferenceManager.getDefaultSharedPreferences(getContext()) .getInt("keyboard_height_portrait", defaultCustomKeyboardSize); return Util.clamp(keyboardHeight, minCustomKeyboardSize, getRootView().getHeight() - minCustomKeyboardTopMarginPortrait); } private void setKeyboardPortraitHeight(int height) { + if (isBubble) { + return; + } + PreferenceManager.getDefaultSharedPreferences(getContext()) .edit().putInt("keyboard_height_portrait", height).apply(); } private void setKeyboardLandscapeHeight(int height) { + if (isBubble) { + return; + } + PreferenceManager.getDefaultSharedPreferences(getContext()) .edit().putInt("keyboard_height_landscape", height).apply(); } diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java index 9eb321be2..137b6d253 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -1974,6 +1974,7 @@ public class ConversationActivity extends PassphraseRequiredActivity cancelJoinRequest = findViewById(R.id.conversation_cancel_request); joinGroupCallButton = findViewById(R.id.conversation_group_call_join); + container.setIsBubble(isInBubble()); container.addOnKeyboardShownListener(this); inputPanel.setListener(this); inputPanel.setMediaListener(this); diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 455e22c5e..89208ebde 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -5,6 +5,7 @@ 220dp 110dp 170dp + 56dp 48dp 16sp 12sp