Remove height offset workarounds / config

* Use WindowInsetsListener to get nav bar height and use that
      instead for calculating adjusted height for extra keys view
This commit is contained in:
David Kramer
2021-01-28 06:51:23 -06:00
committed by Leonid Pliushch
parent b5d491a54c
commit 096dedffb1
3 changed files with 29 additions and 100 deletions

View File

@@ -1,13 +1,8 @@
package com.termux.app;
import android.content.Context;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import static com.termux.terminal.EmulatorDebug.LOG_TAG;
/**
* Work around for fullscreen mode in Termux to fix ExtraKeysView not being visible.
@@ -18,49 +13,23 @@ import static com.termux.terminal.EmulatorDebug.LOG_TAG;
* For more information, see https://issuetracker.google.com/issues/36911528
*/
public class FullScreenWorkAround {
/**
* TODO add documentation so user can know available options and know they may need to experiment
* for their specific device
*/
private static final int WORK_AROUND_METHOD_1 = 1;
private static final int WORK_AROUND_METHOD_2 = 2;
private static final int WORK_AROUND_METHOD_3 = 3;
private int mWorkAroundMethod;
private View mChildOfContent;
private int mUsableHeightPrevious;
private ViewGroup.LayoutParams mViewGroupLayoutParams;
private int mStatusBarHeight;
private int mExtraKeysHeight;
private int mNavBarHeight;
public static void apply(TermuxActivity activity, ExtraKeysView view, int workAroundMethod) {
new FullScreenWorkAround(activity, view, workAroundMethod);
public static void apply(TermuxActivity activity) {
new FullScreenWorkAround(activity);
}
private FullScreenWorkAround(TermuxActivity activity, ExtraKeysView view, int workAroundMethod) {
private FullScreenWorkAround(TermuxActivity activity) {
ViewGroup content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mViewGroupLayoutParams = mChildOfContent.getLayoutParams();
mStatusBarHeight = lookupStatusBarHeight(activity);
mWorkAroundMethod = workAroundMethod;
// ExtraKeysView height is 0 at this point, so we need this to get actual size
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mExtraKeysHeight = view.getMeasuredHeight();
// only needed one time so we can remove
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// now that we have ExtraKeysView height, we can apply this work-around so that we can see ExtraKeysView when keyboard
// is shown and dismissed
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(FullScreenWorkAround.this::possiblyResizeChildOfContent);
}
});
mNavBarHeight = activity.getNavBarHeight();
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
}
private void possiblyResizeChildOfContent() {
@@ -73,30 +42,7 @@ public class FullScreenWorkAround {
// ensures that usable layout space does not extend behind the
// soft keyboard, causing the extra keys to not be visible
int adjustedHeight = mViewGroupLayoutParams.height;
// height adjustment does not behave consistently between devices so you may need to do
// a bit of trial and error to see if one of these works for your device
int workAroundMethod = getWorkAroundMethod();
switch (workAroundMethod) {
case WORK_AROUND_METHOD_1:
// tested w/ Pixel (emulator)
adjustedHeight = (usableHeightSansKeyboard - heightDifference) + getExtraKeysHeight() + (getStatusBarHeight() / 2);
break;
case WORK_AROUND_METHOD_2:
// tested w/ Samsung S7 and OnePlus 6T
adjustedHeight = (usableHeightSansKeyboard - heightDifference);
break;
case WORK_AROUND_METHOD_3:
// tested w/ Sony Xperia XZ
adjustedHeight = (usableHeightSansKeyboard - heightDifference) + getExtraKeysHeight();
break;
default:
Log.w(LOG_TAG, "Unknown / Unsupported workAroundMethod for height offset: [" + workAroundMethod + "]");
// TODO may need to add more cases for other devices as they're tested..
}
mViewGroupLayoutParams.height = adjustedHeight;
mViewGroupLayoutParams.height = (usableHeightSansKeyboard - heightDifference) + getNavBarHeight();
} else {
// keyboard probably just became hidden
mViewGroupLayoutParams.height = usableHeightSansKeyboard;
@@ -106,31 +52,15 @@ public class FullScreenWorkAround {
}
}
private int getNavBarHeight() {
return mNavBarHeight;
}
private int computeUsableHeight() {
Rect r = new Rect();
mChildOfContent.getWindowVisibleDisplayFrame(r);
return (r.bottom - r.top);
}
private int getExtraKeysHeight() {
return mExtraKeysHeight;
}
private int getStatusBarHeight() {
return mStatusBarHeight;
}
private int lookupStatusBarHeight(Context context) {
int height = 0;
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = context.getResources().getDimensionPixelSize(resourceId);
}
return height;
}
private int getWorkAroundMethod() {
return mWorkAroundMethod;
}
}

View File

@@ -134,6 +134,8 @@ public final class TermuxActivity extends Activity implements ServiceConnection
boolean mIsUsingBlackUI;
int mNavBarHeight;
final SoundPool mBellSoundPool = new SoundPool.Builder().setMaxStreams(1).setAudioAttributes(
new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION).build()).build();
@@ -214,18 +216,25 @@ public final class TermuxActivity extends Activity implements ServiceConnection
super.onCreate(bundle);
setContentView(R.layout.drawer_layout);
View content = findViewById(android.R.id.content);
content.setOnApplyWindowInsetsListener((v, insets) -> {
mNavBarHeight = insets.getSystemWindowInsetBottom();
return insets;
});
if (mSettings.isUsingFullScreen()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
if (mIsUsingBlackUI) {
findViewById(R.id.left_drawer).setBackgroundColor(
getResources().getColor(android.R.color.background_dark)
);
}
if (mSettings.isUsingFullScreen()) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
mTerminalView = findViewById(R.id.terminal_view);
mTerminalView.setOnKeyListener(new TermuxViewClient(this));
@@ -263,7 +272,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
// apply extra keys fix if enabled in prefs
if (mSettings.isUsingFullScreen() && mSettings.isUsingFullScreenWorkAround()) {
FullScreenWorkAround.apply(TermuxActivity.this, mExtraKeysView, mSettings.getFullScreenWorkAroundMethod());
FullScreenWorkAround.apply(TermuxActivity.this);
}
} else {
@@ -341,6 +350,10 @@ public final class TermuxActivity extends Activity implements ServiceConnection
sendOpenedBroadcast();
}
public int getNavBarHeight() {
return mNavBarHeight;
}
/**
* Send a broadcast notifying Termux app has been opened
*/

View File

@@ -71,7 +71,6 @@ final class TermuxPreferences {
private boolean mUseFullScreen;
private boolean mUseFullScreenWorkAround;
private int mFullScreenWorkAroundMethod;
@AsciiBellBehaviour
int mBellBehaviour = BELL_VIBRATE;
@@ -151,10 +150,6 @@ final class TermuxPreferences {
return mUseFullScreenWorkAround;
}
int getFullScreenWorkAroundMethod() {
return mFullScreenWorkAroundMethod;
}
void setScreenAlwaysOn(Context context, boolean newValue) {
mScreenAlwaysOn = newValue;
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(SCREEN_ALWAYS_ON_KEY, newValue).apply();
@@ -232,15 +227,6 @@ final class TermuxPreferences {
mUseFullScreenWorkAround = false;
}
String workAroundMethodStr = props.getProperty("fullscreen-workaround-method", "");
if (workAroundMethodStr != null && !workAroundMethodStr.isEmpty()) {
try {
mFullScreenWorkAroundMethod = Integer.parseInt(workAroundMethodStr);
} catch (Exception ex) {
Log.e(LOG_TAG, "fullscreen-workaround-method error: " + ex.getMessage());
}
}
String defaultExtraKeys = "[[ESC, TAB, CTRL, ALT, {key: '-', popup: '|'}, DOWN, UP]]";
try {