From c8c1fe79b4b6eda8fb3acde5000532767badfa28 Mon Sep 17 00:00:00 2001 From: sspanak Date: Sat, 1 Feb 2025 17:25:38 +0200 Subject: [PATCH] broken down the SoftKey into smaller files --- .../io/github/sspanak/tt9/ui/Vibration.java | 4 +- .../tt9/ui/main/keys/BaseClickableKey.java | 169 +++++++++ .../ui/main/keys/BaseSoftKeyWithIcons.java | 118 ++++++ .../ui/main/keys/BaseSoftKeyWithSideText.java | 27 ++ ...wipeableKey.java => BaseSwipeableKey.java} | 10 +- .../sspanak/tt9/ui/main/keys/SoftKey.java | 337 ++---------------- .../tt9/ui/main/keys/SoftKeyAddWord.java | 2 +- .../tt9/ui/main/keys/SoftKeyBackspace.java | 2 +- .../tt9/ui/main/keys/SoftKeyFilter.java | 2 +- .../sspanak/tt9/ui/main/keys/SoftKeyLF4.java | 2 +- .../tt9/ui/main/keys/SoftKeyNumber.java | 2 +- .../sspanak/tt9/ui/main/keys/SoftKeyRF3.java | 2 +- .../tt9/ui/main/keys/SoftKeySettings.java | 2 +- .../tt9/ui/main/keys/SoftKeyShift.java | 2 +- 14 files changed, 358 insertions(+), 323 deletions(-) create mode 100644 app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseClickableKey.java create mode 100644 app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithIcons.java create mode 100644 app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithSideText.java rename app/src/main/java/io/github/sspanak/tt9/ui/main/keys/{SwipeableKey.java => BaseSwipeableKey.java} (94%) diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/Vibration.java b/app/src/main/java/io/github/sspanak/tt9/ui/Vibration.java index efd3308f1..f05cc21cc 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/Vibration.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/Vibration.java @@ -7,7 +7,7 @@ import androidx.annotation.NonNull; import io.github.sspanak.tt9.preferences.settings.SettingsStore; -import io.github.sspanak.tt9.ui.main.keys.SoftKey; +import io.github.sspanak.tt9.ui.main.keys.BaseClickableKey; import io.github.sspanak.tt9.ui.main.keys.SoftKeyNumber; public class Vibration { @@ -23,7 +23,7 @@ public static int getNoVibration() { return -1; } - public static int getPressVibration(SoftKey key) { + public static int getPressVibration(BaseClickableKey key) { return key instanceof SoftKeyNumber ? HapticFeedbackConstants.KEYBOARD_TAP : HapticFeedbackConstants.VIRTUAL_KEY; } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseClickableKey.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseClickableKey.java new file mode 100644 index 000000000..7adee99fa --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseClickableKey.java @@ -0,0 +1,169 @@ +package io.github.sspanak.tt9.ui.main.keys; + +import android.content.Context; +import android.os.Handler; +import android.os.Looper; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; + +import io.github.sspanak.tt9.ime.TraditionalT9; +import io.github.sspanak.tt9.preferences.settings.SettingsStore; +import io.github.sspanak.tt9.ui.Vibration; +import io.github.sspanak.tt9.util.Logger; + +public class BaseClickableKey extends com.google.android.material.button.MaterialButton implements View.OnTouchListener, View.OnLongClickListener { + private final String LOG_TAG = getClass().getSimpleName(); + + protected TraditionalT9 tt9; + protected Vibration vibration; + + private boolean hold = false; + private boolean repeat = false; + private long lastLongClickTime = 0; + private final Handler repeatHandler = new Handler(Looper.getMainLooper()); + + private static int lastPressedKey = -1; + private boolean ignoreLastPressedKey = false; + + + public BaseClickableKey(Context context) { + super(context); + setHapticFeedbackEnabled(false); + setOnTouchListener(this); + setOnLongClickListener(this); + } + + + public BaseClickableKey(Context context, AttributeSet attrs) { + super(context, attrs); + setHapticFeedbackEnabled(false); + setOnTouchListener(this); + setOnLongClickListener(this); + } + + + public BaseClickableKey(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + setHapticFeedbackEnabled(false); + setOnTouchListener(this); + setOnLongClickListener(this); + } + + + public void setTT9(TraditionalT9 tt9) { + this.tt9 = tt9; + } + + + protected boolean validateTT9Handler() { + if (tt9 == null) { + Logger.w(LOG_TAG, "Traditional T9 handler is not set. Ignoring key press."); + return false; + } + + return true; + } + + + @Override + public boolean onTouch(View view, MotionEvent event) { + super.onTouchEvent(event); + + int action = (event.getAction() & MotionEvent.ACTION_MASK); + + if (action == MotionEvent.ACTION_DOWN) { + return handlePress(); + } else if (action == MotionEvent.ACTION_UP) { + if (!repeat || hold) { + hold = false; + repeat = false; + boolean result = handleRelease(); + lastPressedKey = ignoreLastPressedKey ? -1 : getId(); + return result; + } + repeat = false; + } + return false; + } + + + @Override + public boolean onLongClick(View view) { + // sometimes this gets called twice, so we debounce the call to the repeating function + final long now = System.currentTimeMillis(); + if (now - lastLongClickTime < SettingsStore.SOFT_KEY_DOUBLE_CLICK_DELAY) { + return false; + } + + hold = true; + lastLongClickTime = now; + repeatOnLongPress(); + return true; + } + + + /** + * repeatOnLongPress + * Repeatedly calls "handleHold()" upon holding the respective SoftKey, to simulate physical keyboard behavior. + */ + private void repeatOnLongPress() { + if (hold) { + repeat = true; + handleHold(); + lastPressedKey = ignoreLastPressedKey ? -1 : getId(); + repeatHandler.removeCallbacks(this::repeatOnLongPress); + repeatHandler.postDelayed(this::repeatOnLongPress, SettingsStore.SOFT_KEY_REPEAT_DELAY); + } + } + + + /** + * preventRepeat + * Prevents "handleHold()" from being called repeatedly when the SoftKey is being held. + */ + protected void preventRepeat() { + hold = false; + repeatHandler.removeCallbacks(this::repeatOnLongPress); + } + + + protected static int getLastPressedKey() { + return lastPressedKey; + } + + + protected void ignoreLastPressedKey() { + ignoreLastPressedKey = true; + } + + + protected boolean handlePress() { + if (validateTT9Handler()) { + vibrate(Vibration.getPressVibration(this)); + } + + return false; + } + + + protected void handleHold() {} + + + protected boolean handleRelease() { + return false; + } + + + public boolean isHoldEnabled() { + return true; + } + + + protected void vibrate(int vibrationType) { + if (tt9 != null) { + vibration = vibration == null ? new Vibration(tt9.getSettings(), this) : vibration; + vibration.vibrate(vibrationType); + } + } +} diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithIcons.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithIcons.java new file mode 100644 index 000000000..0da40475d --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithIcons.java @@ -0,0 +1,118 @@ +package io.github.sspanak.tt9.ui.main.keys; + +import android.content.Context; +import android.graphics.Color; +import android.graphics.drawable.Drawable; +import android.util.AttributeSet; +import android.view.View; +import android.widget.ImageView; +import android.widget.RelativeLayout; + +import androidx.annotation.Nullable; +import androidx.appcompat.content.res.AppCompatResources; + +public class BaseSoftKeyWithIcons extends SoftKey { + private Drawable icon = null; + private Drawable holdIcon = null; + + + public BaseSoftKeyWithIcons(Context context) { super(context); } + public BaseSoftKeyWithIcons(Context context, AttributeSet attrs) { super(context, attrs); } + public BaseSoftKeyWithIcons(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } + + + /** + * Returns the central icon resource ID. If the key does not have a central icon, return -1. The scale + */ + protected int getCentralIcon() { return -1; } + + + /** + * A fail-safe method to get the central icon drawable. + */ + private Drawable getCentralIconCompat() { + if (icon == null && getCentralIcon() > 0) { + icon = AppCompatResources.getDrawable(getContext(), getCentralIcon()); + } else if (getCentralIcon() <= 0) { + icon = null; + } + + return icon; + } + + + /** + * Same as getTitleScale(), but for keys that have icons instead of text. + */ + protected float getCentralIconScale() { + float keyboardSizeScale = Math.max(0.7f, Math.min(getTT9Width(), getTT9Height())); + keyboardSizeScale = Math.min(1.15f, keyboardSizeScale); + return keyboardSizeScale * Math.min(getScreenScaleX(), getScreenScaleY()); + } + + + /** + * Returns the hold icon resource ID. If the key does not have a hold icon, return -1. The scale + * is controlled by super.getHoldElementScale(). + */ + protected int getHoldIcon() { return -1; } + + + /** + * A fail-safe method to get the hold icon drawable. + */ + private Drawable getHoldIconCompat() { + if (holdIcon == null && getHoldIcon() > 0) { + holdIcon = AppCompatResources.getDrawable(getContext(), getHoldIcon()); + } else if (getHoldIcon() <= 0) { + holdIcon = null; + } + + return holdIcon; + } + + + protected void resetIconCache() { + icon = null; + holdIcon = null; + } + + + /** + * Renders one of the key icons. It could be either the central icon, in the place of the main title, + * or a hold icon, displayed in the upper right corner. + */ + private void renderOverlayDrawable(String elementTag, @Nullable Drawable drawable, float scale, boolean isEnabled) { + if (overlay == null) { + return; + } + + View element = ((RelativeLayout) getParent()).findViewWithTag(elementTag); + if (!(element instanceof ImageView el)) { + return; + } + + el.setImageDrawable(drawable); + if (!isEnabled) { + el.setColorFilter(Color.GRAY); + } else { + el.clearColorFilter(); + } + + if (drawable != null) { + el.setScaleX(scale); + el.setScaleY(scale); + } + } + + + public void render() { + boolean isKeyEnabled = isEnabled(); + + getOverlayWrapper(); + renderOverlayDrawable("overlay_icon", getCentralIconCompat(), getCentralIconScale(), isKeyEnabled); + renderOverlayDrawable("overlay_hold_icon", getHoldIconCompat(), getHoldElementScale(), isKeyEnabled && isHoldEnabled()); + + super.render(); + } +} diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithSideText.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithSideText.java new file mode 100644 index 000000000..168348eff --- /dev/null +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSoftKeyWithSideText.java @@ -0,0 +1,27 @@ +package io.github.sspanak.tt9.ui.main.keys; + +import android.content.Context; +import android.util.AttributeSet; + +public class BaseSoftKeyWithSideText extends BaseSoftKeyWithIcons { + public BaseSoftKeyWithSideText(Context context) { super(context); } + public BaseSoftKeyWithSideText(Context context, AttributeSet attrs) { super(context, attrs); } + public BaseSoftKeyWithSideText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } + + protected String getTopText() { return null; } + protected String getRightText() { return null; } + protected String getBottomText() { return null; } + protected String getLeftText() { return null; } + + @Override + public void render() { + boolean isKeyEnabled = isEnabled(); + + getOverlayWrapper(); + renderOverlayText("overlay_top_text", getTopText(), getHoldElementScale(), isKeyEnabled); + renderOverlayText("overlay_right_text", getRightText(), getHoldElementScale(), isKeyEnabled); + renderOverlayText("overlay_bottom_text", getBottomText(), getHoldElementScale(), isKeyEnabled); + renderOverlayText("overlay_left_text", getLeftText(), getHoldElementScale(), isKeyEnabled); + super.render(); + } +} diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSwipeableKey.java similarity index 94% rename from app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java rename to app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSwipeableKey.java index 4ff0be0cf..e8142c737 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SwipeableKey.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/BaseSwipeableKey.java @@ -11,8 +11,8 @@ import io.github.sspanak.tt9.util.Logger; import io.github.sspanak.tt9.util.Timer; -abstract public class SwipeableKey extends SoftKey { - private static final String LOG_TAG = SwipeableKey.class.getSimpleName(); +abstract public class BaseSwipeableKey extends BaseSoftKeyWithSideText { + private static final String LOG_TAG = BaseSwipeableKey.class.getSimpleName(); private float HOLD_DURATION_THRESHOLD; protected float SWIPE_X_THRESHOLD; @@ -32,17 +32,17 @@ abstract public class SwipeableKey extends SoftKey { private long swipeProcessingTimeAverage = 50; - public SwipeableKey(Context context) { + public BaseSwipeableKey(Context context) { super(context); } - public SwipeableKey(Context context, AttributeSet attrs) { + public BaseSwipeableKey(Context context, AttributeSet attrs) { super(context, attrs); } - public SwipeableKey(Context context, AttributeSet attrs, int defStyleAttr) { + public BaseSwipeableKey(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKey.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKey.java index 62742c181..95462ae0b 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKey.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKey.java @@ -1,91 +1,38 @@ package io.github.sspanak.tt9.ui.main.keys; import android.content.Context; -import android.graphics.Color; import android.graphics.Paint; -import android.graphics.drawable.Drawable; -import android.os.Handler; -import android.os.Looper; import android.text.SpannableString; import android.text.style.RelativeSizeSpan; import android.util.AttributeSet; -import android.view.MotionEvent; import android.view.View; import android.view.ViewParent; -import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import androidx.annotation.Nullable; -import androidx.appcompat.content.res.AppCompatResources; import io.github.sspanak.tt9.hacks.DeviceInfo; -import io.github.sspanak.tt9.ime.TraditionalT9; import io.github.sspanak.tt9.preferences.settings.SettingsStore; -import io.github.sspanak.tt9.ui.Vibration; -import io.github.sspanak.tt9.util.Logger; import io.github.sspanak.tt9.util.Text; import io.github.sspanak.tt9.util.chars.Characters; -public class SoftKey extends com.google.android.material.button.MaterialButton implements View.OnTouchListener, View.OnLongClickListener { - private final String LOG_TAG = getClass().getSimpleName(); - - protected TraditionalT9 tt9; - protected Vibration vibration; - - private boolean hold = false; - private boolean repeat = false; - private long lastLongClickTime = 0; - private final Handler repeatHandler = new Handler(Looper.getMainLooper()); - - private static int lastPressedKey = -1; - private boolean ignoreLastPressedKey = false; - - private Drawable icon = null; - private Drawable holdIcon = null; - private RelativeLayout overlay = null; +public class SoftKey extends BaseClickableKey { + protected RelativeLayout overlay = null; private static float screenScaleX = 0; private static float screenScaleY = 0; - public SoftKey(Context context) { - super(context); - setHapticFeedbackEnabled(false); - setOnTouchListener(this); - setOnLongClickListener(this); - } - - public SoftKey(Context context, AttributeSet attrs) { - super(context, attrs); - setHapticFeedbackEnabled(false); - setOnTouchListener(this); - setOnLongClickListener(this); - } - - public SoftKey(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - setHapticFeedbackEnabled(false); - setOnTouchListener(this); - setOnLongClickListener(this); - } - - - public void setTT9(TraditionalT9 tt9) { - this.tt9 = tt9; - } - - - protected boolean validateTT9Handler() { - if (tt9 == null) { - Logger.w(LOG_TAG, "Traditional T9 handler is not set. Ignoring key press."); - return false; - } - - return true; - } + public SoftKey(Context context) { super(context); } + public SoftKey(Context context, AttributeSet attrs) { super(context, attrs); } + public SoftKey(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } + /** + * Returns a scale factor for the screen width, used to adjust the key size and text size. Mostly, + * useful for tablets or larger devices, where the keys are too big but the text remains small. + */ protected float getScreenScaleX() { if (screenScaleX == 0) { boolean isLandscape = DeviceInfo.isLandscapeOrientation(getContext()); @@ -100,6 +47,9 @@ protected float getScreenScaleX() { } + /** + * Same as getScreenScaleX(), but used for the key height. + */ protected float getScreenScaleY() { if (screenScaleY == 0) { boolean isLandscape = DeviceInfo.isLandscapeOrientation(getContext()); @@ -124,107 +74,11 @@ protected float getTT9Height() { } - @Override - public boolean onTouch(View view, MotionEvent event) { - super.onTouchEvent(event); - - int action = (event.getAction() & MotionEvent.ACTION_MASK); - - if (action == MotionEvent.ACTION_DOWN) { - return handlePress(); - } else if (action == MotionEvent.ACTION_UP) { - if (!repeat || hold) { - hold = false; - repeat = false; - boolean result = handleRelease(); - lastPressedKey = ignoreLastPressedKey ? -1 : getId(); - return result; - } - repeat = false; - } - return false; - } - - - @Override - public boolean onLongClick(View view) { - // sometimes this gets called twice, so we debounce the call to the repeating function - final long now = System.currentTimeMillis(); - if (now - lastLongClickTime < SettingsStore.SOFT_KEY_DOUBLE_CLICK_DELAY) { - return false; - } - - hold = true; - lastLongClickTime = now; - repeatOnLongPress(); - return true; - } - - - /** - * repeatOnLongPress - * Repeatedly calls "handleHold()" upon holding the respective SoftKey, to simulate physical keyboard behavior. - */ - private void repeatOnLongPress() { - if (hold) { - repeat = true; - handleHold(); - lastPressedKey = ignoreLastPressedKey ? -1 : getId(); - repeatHandler.removeCallbacks(this::repeatOnLongPress); - repeatHandler.postDelayed(this::repeatOnLongPress, SettingsStore.SOFT_KEY_REPEAT_DELAY); - } - } - - - /** - * preventRepeat - * Prevents "handleHold()" from being called repeatedly when the SoftKey is being held. - */ - protected void preventRepeat() { - hold = false; - repeatHandler.removeCallbacks(this::repeatOnLongPress); - } - - - protected static int getLastPressedKey() { - return lastPressedKey; - } - - - protected void ignoreLastPressedKey() { - ignoreLastPressedKey = true; - } - - - protected boolean handlePress() { - if (validateTT9Handler()) { - vibrate(Vibration.getPressVibration(this)); - } - - return false; - } - - - protected void handleHold() {} - - - protected boolean handleRelease() { - return false; - } - - - public boolean isHoldEnabled() { - return true; - } - - /** * getTitle * Generates the name of the key, for example: "OK", "Backspace", "1", etc... */ - protected String getTitle() { - return null; - } + protected String getTitle() { return null; } /** @@ -235,60 +89,6 @@ protected String getTitle() { protected int getNoEmojiTitle() { return 0; } - protected int getCentralIcon() { - return -1; - } - - - protected int getHoldIcon() { - return -1; - } - - - protected void resetIconCache() { - icon = null; - holdIcon = null; - } - - - /** - * Generates a String describing the "hold" function of the key. The String will be displayed - * in the upper right corner. - */ - protected String getHoldText() { - return null; - } - - - /** - * Generates a String describing for the swipe up function of the key - */ - protected String getTopText() { - return null; - } - - /** - * Generates a String describing for the swipe right function of the key - */ - protected String getRightText() { - return null; - } - - /** - * Generates a String describing for the swipe down function of the key - */ - protected String getBottomText() { - return null; - } - - /** - * Generates a String describing for the swipe left function of the key - */ - protected String getLeftText() { - return null; - } - - /** * Returns a meaningful key title depending on the current emoji support. */ @@ -308,36 +108,8 @@ private String getTitleCompat() { /** - * A fail-safe method to get the central icon drawable. - */ - private Drawable getIconCompat() { - if (icon == null && getCentralIcon() > 0) { - icon = AppCompatResources.getDrawable(getContext(), getCentralIcon()); - } else if (getCentralIcon() <= 0) { - icon = null; - } - - return icon; - } - - - /** - * A fail-safe method to get the hold icon drawable. - */ - private Drawable getHoldIconCompat() { - if (holdIcon == null && getHoldIcon() > 0) { - holdIcon = AppCompatResources.getDrawable(getContext(), getHoldIcon()); - } else if (getHoldIcon() <= 0) { - holdIcon = null; - } - - return holdIcon; - } - - - /** - * Multiplier for the main text font size. Used for automatically adjusting the font size to fit - * the key when changing the keyboard dimensions, and to look good on different screen sizes. + * Returns a multiplier for scaling the central text size. Used to automatically fit the text + * when changing the keyboard dimensions, and to make it look good on different screen sizes. */ protected float getTitleScale() { float keyboardSizeScale = Math.max(0.7f, Math.min(getTT9Width(), getTT9Height())); @@ -347,15 +119,13 @@ protected float getTitleScale() { /** - * Same as getTitleScale(), but for keys that have icons instead of text. + * Generates a String describing the "hold" function of the key. The String will be displayed + * in the upper right corner. */ - protected float getCentralIconScale() { - float keyboardSizeScale = Math.max(0.7f, Math.min(getTT9Width(), getTT9Height())); - keyboardSizeScale = Math.min(1.15f, keyboardSizeScale); - return keyboardSizeScale * Math.min(getScreenScaleX(), getScreenScaleY()); + protected String getHoldText() { + return null; } - /** * Similar to getTitleScale(), adjusts the font size of the hold text or icon */ @@ -365,7 +135,7 @@ protected float getHoldElementScale() { } - private void getOverlayWrapper() { + protected void getOverlayWrapper() { if (overlay == null) { ViewParent parent = getParent(); if (parent instanceof RelativeLayout) { @@ -375,32 +145,6 @@ private void getOverlayWrapper() { } - /** - * render - * Sets the key labels and icons using "getTitle()", "getCenterIcon()", "getHoldText()", - * "getTopText()", "getRightText()", "getHoldIcon()", etc. Also takes care of styling the labels - * depending on "isEnabled()" and "isHoldEnabled()". - */ - public void render() { - boolean isKeyEnabled = isEnabled(); - boolean isHoldEnabled = isHoldEnabled(); - - renderTitle(isKeyEnabled); - - getOverlayWrapper(); - - renderOverlayDrawable("overlay_icon", getIconCompat(), getCentralIconScale(), isKeyEnabled); - - renderOverlayText("overlay_hold_text", getHoldText(), getHoldElementScale(), isKeyEnabled && isHoldEnabled); - renderOverlayDrawable("overlay_hold_icon", getHoldIconCompat(), getHoldElementScale(), isKeyEnabled && isHoldEnabled); - - renderOverlayText("overlay_top_text", getTopText(), getHoldElementScale(), isKeyEnabled); - renderOverlayText("overlay_right_text", getRightText(), getHoldElementScale(), isKeyEnabled); - renderOverlayText("overlay_bottom_text", getBottomText(), getHoldElementScale(), isKeyEnabled); - renderOverlayText("overlay_left_text", getLeftText(), getHoldElementScale(), isKeyEnabled); - } - - /** * Renders the central text of the key and styles it based on "isEnabled". */ @@ -428,7 +172,7 @@ private void renderTitle(boolean isEnabled) { * Renders text in the given overlay element, with optional scaling and alpha. The overlay * text elements are either the "hold" text or the "swipe" text. */ - private void renderOverlayText(String elementTag, @Nullable String text, float scale, boolean isEnabled) { + protected void renderOverlayText(String elementTag, @Nullable String text, float scale, boolean isEnabled) { if (overlay == null) { return; } @@ -452,37 +196,14 @@ private void renderOverlayText(String elementTag, @Nullable String text, float s /** - * Renders one of the key icons. It could be either the central icon, in the place of the main title, - * or a hold icon, displayed in the upper right corner. + * render + * Sets the key labels and icons using. Potentially, it can also adjust padding and margins and + * other visual properties of the key. */ - private void renderOverlayDrawable(String elementTag, @Nullable Drawable drawable, float scale, boolean isEnabled) { - if (overlay == null) { - return; - } - - View element = ((RelativeLayout) getParent()).findViewWithTag(elementTag); - if (!(element instanceof ImageView el)) { - return; - } - - el.setImageDrawable(drawable); - if (!isEnabled) { - el.setColorFilter(Color.GRAY); - } else { - el.clearColorFilter(); - } - - if (drawable != null) { - el.setScaleX(scale); - el.setScaleY(scale); - } - } - - - protected void vibrate(int vibrationType) { - if (tt9 != null) { - vibration = vibration == null ? new Vibration(tt9.getSettings(), this) : vibration; - vibration.vibrate(vibrationType); - } + public void render() { + boolean isKeyEnabled = isEnabled(); + renderTitle(isKeyEnabled); + getOverlayWrapper(); + renderOverlayText("overlay_hold_text", getHoldText(), getHoldElementScale(), isKeyEnabled && isHoldEnabled()); } } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyAddWord.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyAddWord.java index 3f5ad6161..9f7cf67f6 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyAddWord.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyAddWord.java @@ -5,7 +5,7 @@ import io.github.sspanak.tt9.R; -public class SoftKeyAddWord extends SoftKey { +public class SoftKeyAddWord extends BaseSoftKeyWithIcons { public SoftKeyAddWord(Context context) { super(context); } public SoftKeyAddWord(Context context, AttributeSet attrs) { super(context, attrs); } public SoftKeyAddWord(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java index 045636583..2744f313d 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyBackspace.java @@ -10,7 +10,7 @@ import io.github.sspanak.tt9.preferences.settings.SettingsStore; import io.github.sspanak.tt9.ui.Vibration; -public class SoftKeyBackspace extends SwipeableKey { +public class SoftKeyBackspace extends BaseSwipeableKey { private int repeat = 0; private boolean isActionPerformed = false; diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyFilter.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyFilter.java index 66e236736..8b0f20f3f 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyFilter.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyFilter.java @@ -7,7 +7,7 @@ import io.github.sspanak.tt9.languages.LanguageKind; import io.github.sspanak.tt9.ui.Vibration; -public class SoftKeyFilter extends SoftKey { +public class SoftKeyFilter extends BaseSoftKeyWithIcons { public SoftKeyFilter(Context context) { super(context); } public SoftKeyFilter(Context context, AttributeSet attrs) { super(context, attrs); } public SoftKeyFilter(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyLF4.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyLF4.java index f86630735..b7e9539be 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyLF4.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyLF4.java @@ -7,7 +7,7 @@ import io.github.sspanak.tt9.R; import io.github.sspanak.tt9.ui.Vibration; -public class SoftKeyLF4 extends SwipeableKey { +public class SoftKeyLF4 extends BaseSwipeableKey { public SoftKeyLF4(Context context) { super(context); } public SoftKeyLF4(Context context, AttributeSet attrs) { super(context, attrs); } public SoftKeyLF4(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyNumber.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyNumber.java index e76e2010d..93f881585 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyNumber.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyNumber.java @@ -10,7 +10,7 @@ import io.github.sspanak.tt9.languages.LanguageKind; import io.github.sspanak.tt9.ui.Vibration; -public class SoftKeyNumber extends SoftKey { +public class SoftKeyNumber extends BaseSoftKeyWithIcons { private final static SparseArray NUMBERS = new SparseArray<>() {{ put(R.id.soft_key_0, 0); put(R.id.soft_key_1, 1); diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyRF3.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyRF3.java index ab9ff2f83..433d60407 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyRF3.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyRF3.java @@ -5,7 +5,7 @@ import io.github.sspanak.tt9.R; -public class SoftKeyRF3 extends SoftKey { +public class SoftKeyRF3 extends BaseSoftKeyWithIcons { public SoftKeyRF3(Context context) { super(context); } public SoftKeyRF3(Context context, AttributeSet attrs) { super(context, attrs); } public SoftKeyRF3(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeySettings.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeySettings.java index 19448e7d3..46feaffb7 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeySettings.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeySettings.java @@ -6,7 +6,7 @@ import io.github.sspanak.tt9.R; import io.github.sspanak.tt9.ui.main.ResizableMainView; -public class SoftKeySettings extends SwipeableKey { +public class SoftKeySettings extends BaseSwipeableKey { private ResizableMainView mainView; public SoftKeySettings(Context context) { diff --git a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyShift.java b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyShift.java index b4c62776d..2cf6340f9 100644 --- a/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyShift.java +++ b/app/src/main/java/io/github/sspanak/tt9/ui/main/keys/SoftKeyShift.java @@ -5,7 +5,7 @@ import io.github.sspanak.tt9.R; -public class SoftKeyShift extends SoftKey { +public class SoftKeyShift extends BaseSoftKeyWithIcons { public SoftKeyShift(Context context) { super(context); }