1
0
Fork 0

fixed the random zero insets on Android 15 properly this time

This commit is contained in:
sspanak 2025-02-23 12:46:37 +02:00 committed by Dimo Karaivanov
parent 9e4658d405
commit ade7075fd7
5 changed files with 41 additions and 18 deletions

View file

@ -21,6 +21,8 @@ import io.github.sspanak.tt9.ui.main.keys.SoftKey;
import io.github.sspanak.tt9.util.ThemedContextBuilder; import io.github.sspanak.tt9.util.ThemedContextBuilder;
abstract class BaseMainLayout { abstract class BaseMainLayout {
private static ViewGroup.MarginLayoutParams edgeToEdgeMargins = null;
protected final TraditionalT9 tt9; protected final TraditionalT9 tt9;
private final int xml; private final int xml;
@ -74,28 +76,26 @@ abstract class BaseMainLayout {
@RequiresApi(api = Build.VERSION_CODES.VANILLA_ICE_CREAM) @RequiresApi(api = Build.VERSION_CODES.VANILLA_ICE_CREAM)
protected WindowInsets preventEdgeToEdge(@NonNull View v, @NonNull WindowInsets windowInsets) { protected WindowInsets preventEdgeToEdge(@NonNull View v, @NonNull WindowInsets windowInsets) {
Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()); Insets insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars());
ViewGroup.MarginLayoutParams layout = (ViewGroup.MarginLayoutParams) v.getLayoutParams(); ViewGroup.MarginLayoutParams marginParams = setMargins(v, insets.right, insets.bottom, insets.left);
if (layout == null) { if (marginParams != null) {
return windowInsets; edgeToEdgeMargins = marginParams;
return WindowInsets.CONSUMED;
} }
layout.rightMargin = insets.right; return windowInsets;
layout.bottomMargin = insets.bottom;
layout.leftMargin = insets.left;
v.setLayoutParams(layout);
return WindowInsets.CONSUMED;
} }
protected void preventEdgeToEdge() { /**
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM || tt9 == null || tt9.isInputLimited()) { * Similar to the above method, but reuses the last known margins. Useful for when the Main View
* is re-created and it is not yet possible to get the new window insets.
*/
public void preventEdgeToEdge() {
if (view == null || edgeToEdgeMargins == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
return; return;
} }
WindowInsets insets = view != null ? view.getRootWindowInsets() : null; setMargins(view, edgeToEdgeMargins.rightMargin, edgeToEdgeMargins.bottomMargin, edgeToEdgeMargins.leftMargin);
if (insets != null) {
preventEdgeToEdge(view, insets);
}
} }
@ -129,6 +129,23 @@ abstract class BaseMainLayout {
} }
private ViewGroup.MarginLayoutParams setMargins(View v, int right, int bottom, int left) {
if (v == null) {
return null;
}
ViewGroup.MarginLayoutParams layout = (ViewGroup.MarginLayoutParams) v.getLayoutParams();
if (layout != null) {
layout.rightMargin = right;
layout.bottomMargin = bottom;
layout.leftMargin = left;
v.setLayoutParams(layout);
}
return layout;
}
int getHeight(boolean forceRecalculate) { int getHeight(boolean forceRecalculate) {
return 0; return 0;
} }

View file

@ -273,6 +273,7 @@ class MainLayoutNumpad extends BaseMainLayout {
getView(); getView();
enableClickHandlers(); enableClickHandlers();
setKeyHeight(defaultKeyHeight); setKeyHeight(defaultKeyHeight);
preventEdgeToEdge();
setWidth(tt9.getSettings().getWidthPercent(), tt9.getSettings().getAlignment()); setWidth(tt9.getSettings().getWidthPercent(), tt9.getSettings().getAlignment());
setKeyColumnWidth(tt9.getSettings().getNumpadFnKeyScale()); setKeyColumnWidth(tt9.getSettings().getNumpadFnKeyScale());
showLongSpace( showLongSpace(
@ -282,7 +283,5 @@ class MainLayoutNumpad extends BaseMainLayout {
for (SoftKey key : getKeys()) { for (SoftKey key : getKeys()) {
key.render(); key.render();
} }
preventEdgeToEdge();
} }
} }

View file

@ -86,6 +86,7 @@ class MainLayoutTray extends BaseMainLayout {
void render() { void render() {
getView(); getView();
setSoftKeysVisibility(); setSoftKeysVisibility();
preventEdgeToEdge();
setWidth(tt9.getSettings().getWidthPercent(), tt9.getSettings().getAlignment()); setWidth(tt9.getSettings().getWidthPercent(), tt9.getSettings().getAlignment());
enableClickHandlers(); enableClickHandlers();
for (SoftKey key : getKeys()) { for (SoftKey key : getKeys()) {

View file

@ -48,7 +48,10 @@ public class MainView {
} }
public void forceCreate() { public void forceCreate() {
destroy(); if (main != null) {
Logger.w(getClass().getSimpleName(), "MainView already exists. Re-creating by request.");
destroy();
}
if (!create()) { if (!create()) {
Logger.w(getClass().getSimpleName(), "Invalid MainView setting. Creating default."); Logger.w(getClass().getSimpleName(), "Invalid MainView setting. Creating default.");
main = new MainLayoutSmall(tt9); main = new MainLayoutSmall(tt9);

View file

@ -61,8 +61,11 @@ public class ResizableMainView extends MainView implements View.OnAttachStateCha
} }
@Override public void onViewAttachedToWindow(@NonNull View v) { setHeight(height, heightSmall, heightNumpad); }
@Override public void onViewDetachedFromWindow(@NonNull View v) {} @Override public void onViewDetachedFromWindow(@NonNull View v) {}
@Override public void onViewAttachedToWindow(@NonNull View v) {
setHeight(height, heightSmall, heightNumpad);
main.preventEdgeToEdge();
}
public void onOrientationChanged() { public void onOrientationChanged() {