1
0
Fork 0

automatic height adjustment to prevent eating up too much screen space

This commit is contained in:
sspanak 2024-06-27 19:08:08 +03:00 committed by Dimo Karaivanov
parent c2563112ec
commit 78832f2bd3
5 changed files with 72 additions and 18 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools"
android:versionCode="580"
android:versionName="34.7"
android:versionCode="583"
android:versionName="34.10"
xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/> <!-- allows displaying notifications on Android >= 13 -->

View file

@ -10,6 +10,14 @@ import android.view.KeyEvent;
import androidx.annotation.NonNull;
public class DeviceInfo {
public static boolean isLandscapeOrientation(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
}
public static int getScreenHeight(Context context) {
return context.getResources().getDisplayMetrics().heightPixels;
}
public static boolean noKeyboard(Context context) {
// all Xiaomi phones are only touchscreen, but some of them report they have a keyboard
// See: https://github.com/sspanak/tt9/issues/549

View file

@ -1,5 +1,7 @@
package io.github.sspanak.tt9.ime;
import android.view.OrientationEventListener;
import androidx.annotation.Nullable;
import io.github.sspanak.tt9.ime.modes.ModeABC;
@ -8,8 +10,26 @@ import io.github.sspanak.tt9.languages.Language;
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
import io.github.sspanak.tt9.ui.main.ResizableMainView;
/**
* Informational methods for the on-screen keyboard
**/
abstract public class MainViewHandler extends HotkeyHandler {
/**** Informational methods for the on-screen keyboard ****/
@Override
protected void onInit() {
super.onInit();
OrientationEventListener orientationListener = new OrientationEventListener(getApplicationContext()) {
@Override
public void onOrientationChanged(int orientation) {
mainView.onOrientationChanged();
}
};
if (orientationListener.canDetectOrientation()) {
orientationListener.enable();
}
}
public int getTextCase() {
return mInputMode.getTextCase();
}

View file

@ -11,6 +11,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import io.github.sspanak.tt9.R;
import io.github.sspanak.tt9.hacks.DeviceInfo;
import io.github.sspanak.tt9.ime.TraditionalT9;
import io.github.sspanak.tt9.ui.main.keys.SoftKey;
import io.github.sspanak.tt9.ui.main.keys.SoftKeySettings;
@ -64,6 +65,25 @@ class MainLayoutNumpad extends BaseMainLayout {
}
/**
* Uses the key height from the settings, but if it takes up too much of the screen, it will
* be adjusted so that the entire Main View would take up around 50% of the screen in landscape mode
* and 75% in portrait mode. Returns the adjusted height of a single key.
*/
private int getKeyHeightCompat() {
int keyHeight = tt9.getSettings().getNumpadKeyHeight();
int screenHeight = DeviceInfo.getScreenHeight(tt9.getApplicationContext());
boolean isLandscape = DeviceInfo.isLandscapeOrientation(tt9.getApplicationContext());
double maxScreenHeight = isLandscape ? screenHeight * 0.75 : screenHeight * 0.8;
double maxKeyHeight = isLandscape ? screenHeight * 0.115 : screenHeight * 0.125;
// it's all very approximate but when it comes to screen dimensions,
// accuracy is not that important
return keyHeight * 5 > maxScreenHeight ? (int) Math.round(maxKeyHeight) : keyHeight;
}
void setKeyHeight(int height) {
if (view == null || height <= 0) {
return;
@ -86,7 +106,7 @@ class MainLayoutNumpad extends BaseMainLayout {
int getHeight() {
if (height <= 0) {
Resources resources = tt9.getResources();
height = tt9.getSettings().getNumpadKeyHeight() * 4
height = getKeyHeightCompat() * 4
+ resources.getDimensionPixelSize(R.dimen.numpad_candidate_height)
+ resources.getDimensionPixelSize(R.dimen.numpad_padding_bottom) * 4;
}
@ -103,7 +123,7 @@ class MainLayoutNumpad extends BaseMainLayout {
@Override
void render() {
getView();
setKeyHeight(tt9 != null ? tt9.getSettings().getNumpadKeyHeight() : -1);
setKeyHeight(getKeyHeightCompat());
enableClickHandlers();
for (SoftKey key : getKeys()) {
key.render();

View file

@ -68,6 +68,16 @@ public class ResizableMainView extends MainView implements View.OnAttachStateCha
}
}
@Override public void onViewAttachedToWindow(@NonNull View v) { onCreateAdjustHeight(); }
@Override public void onViewDetachedFromWindow(@NonNull View v) {}
public void onOrientationChanged() {
calculateSnapHeights();
calculateInitialHeight();
render();
}
public void onResizeStart(float startY) {
resizeStartY = startY;
@ -86,6 +96,15 @@ public class ResizableMainView extends MainView implements View.OnAttachStateCha
}
public void onResizeThrottled(float currentY) {
long now = System.currentTimeMillis();
if (now - lastResizeTime > SettingsStore.RESIZE_THROTTLING_TIME) {
lastResizeTime = now;
onResize(currentY);
}
}
public void onSnap() {
SettingsStore settings = tt9.getSettings();
@ -99,15 +118,6 @@ public class ResizableMainView extends MainView implements View.OnAttachStateCha
}
public void onResizeThrottled(float currentY) {
long now = System.currentTimeMillis();
if (now - lastResizeTime > SettingsStore.RESIZE_THROTTLING_TIME) {
lastResizeTime = now;
onResize(currentY);
}
}
private void expand(int delta) {
SettingsStore settings = tt9.getSettings();
@ -193,8 +203,4 @@ public class ResizableMainView extends MainView implements View.OnAttachStateCha
main.getView().performHapticFeedback(Vibration.getPressVibration(null));
}
}
@Override public void onViewAttachedToWindow(@NonNull View v) { onCreateAdjustHeight(); }
@Override public void onViewDetachedFromWindow(@NonNull View v) {}
}