broken down the SoftKey into smaller files
This commit is contained in:
parent
7d32a3fe4e
commit
c8c1fe79b4
14 changed files with 358 additions and 323 deletions
|
|
@ -7,7 +7,7 @@ import android.view.View;
|
|||
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 class Vibration {
|
|||
return -1;
|
||||
}
|
||||
|
||||
public static int getPressVibration(SoftKey key) {
|
||||
public static int getPressVibration(BaseClickableKey key) {
|
||||
return key instanceof SoftKeyNumber ? HapticFeedbackConstants.KEYBOARD_TAP : HapticFeedbackConstants.VIRTUAL_KEY;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -11,8 +11,8 @@ import io.github.sspanak.tt9.preferences.settings.SettingsStore;
|
|||
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);
|
||||
}
|
||||
|
||||
|
|
@ -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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Same as getScreenScaleX(), but used for the key height.
|
||||
*/
|
||||
protected float getScreenScaleY() {
|
||||
if (screenScaleY == 0) {
|
||||
boolean isLandscape = DeviceInfo.isLandscapeOrientation(getContext());
|
||||
|
|
@ -124,107 +74,11 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
}
|
||||
|
||||
|
||||
@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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
|
||||
|
||||
/**
|
||||
* 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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
|
||||
|
||||
/**
|
||||
* 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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
}
|
||||
|
||||
|
||||
private void getOverlayWrapper() {
|
||||
protected void getOverlayWrapper() {
|
||||
if (overlay == null) {
|
||||
ViewParent parent = getParent();
|
||||
if (parent instanceof RelativeLayout) {
|
||||
|
|
@ -375,32 +145,6 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
* 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 @@ public class SoftKey extends com.google.android.material.button.MaterialButton i
|
|||
|
||||
|
||||
/**
|
||||
* 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());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import android.util.AttributeSet;
|
|||
|
||||
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); }
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import io.github.sspanak.tt9.languages.LanguageKind;
|
|||
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;
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import io.github.sspanak.tt9.R;
|
|||
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); }
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import android.view.Gravity;
|
|||
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); }
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import io.github.sspanak.tt9.ime.helpers.Key;
|
|||
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<Integer> NUMBERS = new SparseArray<>() {{
|
||||
put(R.id.soft_key_0, 0);
|
||||
put(R.id.soft_key_1, 1);
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import android.util.AttributeSet;
|
|||
|
||||
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); }
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import android.util.AttributeSet;
|
|||
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) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import android.util.AttributeSet;
|
|||
|
||||
import io.github.sspanak.tt9.R;
|
||||
|
||||
public class SoftKeyShift extends SoftKey {
|
||||
public class SoftKeyShift extends BaseSoftKeyWithIcons {
|
||||
public SoftKeyShift(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue