diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java index e4849c259..9f8796353 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java @@ -138,7 +138,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { return false; } - public void loadKeyboard(final EditorInfo editorInfo, final SettingsValues settingsValues, + private void loadKeyboard(final EditorInfo editorInfo, final SettingsValues settingsValues, final int currentAutoCapsState, final int currentRecapitalizeState) { final KeyboardLayoutSet.Builder builder = new KeyboardLayoutSet.Builder( mThemeContext, editorInfo); @@ -527,6 +527,10 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { if (mCurrentInputView == null) return; mEmojiPalettesView.clearKeyboardCache(); + reloadMainKeyboard(); + } + + public void reloadMainKeyboard() { loadKeyboard(mLatinIME.getCurrentInputEditorInfo(), Settings.getValues(), mLatinIME.getCurrentAutoCapsState(), mLatinIME.getCurrentRecapitalizeState()); } diff --git a/app/src/main/java/helium314/keyboard/keyboard/PopupKeysKeyboardView.java b/app/src/main/java/helium314/keyboard/keyboard/PopupKeysKeyboardView.java index 7d1735c08..4aa543201 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/PopupKeysKeyboardView.java +++ b/app/src/main/java/helium314/keyboard/keyboard/PopupKeysKeyboardView.java @@ -12,15 +12,15 @@ import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.util.AttributeSet; +import android.view.Gravity; import android.view.MotionEvent; import android.view.View; -import android.view.ViewGroup; import androidx.annotation.NonNull; import helium314.keyboard.accessibility.AccessibilityUtils; import helium314.keyboard.accessibility.PopupKeysKeyboardAccessibilityDelegate; -import helium314.keyboard.keyboard.emoji.OnKeyEventListener; +import helium314.keyboard.keyboard.emoji.EmojiViewCallback; import helium314.keyboard.keyboard.internal.KeyDrawParams; import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode; import helium314.keyboard.latin.R; @@ -39,7 +39,7 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane protected final KeyDetector mKeyDetector; private Controller mController = EMPTY_CONTROLLER; protected KeyboardActionListener mListener; - protected OnKeyEventListener mKeyEventListener; + protected EmojiViewCallback mEmojiViewCallback; private int mOriginX; private int mOriginY; private Key mCurrentKey; @@ -122,7 +122,7 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane public void showPopupKeysPanel(final View parentView, final Controller controller, final int pointX, final int pointY, final KeyboardActionListener listener) { mListener = listener; - mKeyEventListener = null; + mEmojiViewCallback = null; showPopupKeysPanelInternal(parentView, controller, pointX, pointY); } @@ -131,9 +131,9 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane */ @Override public void showPopupKeysPanel(final View parentView, final Controller controller, - final int pointX, final int pointY, final OnKeyEventListener listener) { + final int pointX, final int pointY, final EmojiViewCallback emojiViewCallback) { mListener = null; - mKeyEventListener = listener; + mEmojiViewCallback = emojiViewCallback; showPopupKeysPanelInternal(parentView, controller, pointX, pointY); } @@ -157,6 +157,9 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane mOriginX = x + container.getPaddingLeft(); mOriginY = y + container.getPaddingTop(); + var center = panelX + getMeasuredWidth() / 2; + // This is needed for cases where there's also a long text popup above this keyboard + controller.setLayoutGravity(center < pointX? Gravity.RIGHT : center > pointX? Gravity.LEFT : Gravity.CENTER_HORIZONTAL); controller.onShowPopupKeysPanel(this); final PopupKeysKeyboardAccessibilityDelegate accessibilityDelegate = mAccessibilityDelegate; if (accessibilityDelegate != null @@ -222,8 +225,8 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane false /* isKeyRepeat */); } } - } else if (mKeyEventListener != null) { - mKeyEventListener.onReleaseKey(key); + } else if (mEmojiViewCallback != null) { + mEmojiViewCallback.onReleaseKey(key); } } @@ -314,28 +317,4 @@ public class PopupKeysKeyboardView extends KeyboardView implements PopupKeysPane } return super.onHoverEvent(event); } - - private View getContainerView() { - return (View)getParent(); - } - - @Override - public void showInParent(final ViewGroup parentView) { - removeFromParent(); - parentView.addView(getContainerView()); - } - - @Override - public void removeFromParent() { - final View containerView = getContainerView(); - final ViewGroup currentParent = (ViewGroup)containerView.getParent(); - if (currentParent != null) { - currentParent.removeView(containerView); - } - } - - @Override - public boolean isShowingInParent() { - return (getContainerView().getParent() != null); - } } diff --git a/app/src/main/java/helium314/keyboard/keyboard/PopupKeysPanel.java b/app/src/main/java/helium314/keyboard/keyboard/PopupKeysPanel.java index 5a7e60e30..b511db9d3 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/PopupKeysPanel.java +++ b/app/src/main/java/helium314/keyboard/keyboard/PopupKeysPanel.java @@ -8,10 +8,17 @@ package helium314.keyboard.keyboard; import android.view.View; import android.view.ViewGroup; -import helium314.keyboard.keyboard.emoji.OnKeyEventListener; +import helium314.keyboard.keyboard.emoji.EmojiViewCallback; public interface PopupKeysPanel { interface Controller { + /** + * Set the layout gravity. + * @param layoutGravity requested by the popup + */ + default void setLayoutGravity(int layoutGravity) { + } + /** * Add the {@link PopupKeysPanel} to the target view. * @param panel the panel to be shown. @@ -59,19 +66,18 @@ public interface PopupKeysPanel { * Initializes the layout and event handling of this {@link PopupKeysPanel} and calls the * controller's onShowPopupKeysPanel to add the panel's container view. * Same as {@link PopupKeysPanel#showPopupKeysPanel(View, Controller, int, int, KeyboardActionListener)}, - * but with a {@link OnKeyEventListener}. + * but with a {@link EmojiViewCallback}. * * @param parentView the parent view of this {@link PopupKeysPanel} * @param controller the controller that can dismiss this {@link PopupKeysPanel} * @param pointX x coordinate of this {@link PopupKeysPanel} * @param pointY y coordinate of this {@link PopupKeysPanel} - * @param listener the listener that will receive keyboard action from this - * {@link PopupKeysPanel}. + * @param emojiViewCallback to receive keyboard actions from this {@link PopupKeysPanel}. */ // TODO: Currently the PopupKeysPanel is inside a container view that is added to the parent. // Consider the simpler approach of placing the PopupKeysPanel itself into the parent view. void showPopupKeysPanel(View parentView, Controller controller, int pointX, - int pointY, OnKeyEventListener listener); + int pointY, EmojiViewCallback emojiViewCallback); /** * Dismisses the popup keys panel and calls the controller's onDismissPopupKeysPanel to remove @@ -127,20 +133,35 @@ public interface PopupKeysPanel { */ int translateY(int y); + default View getContainerView() { + return (View) ((View) this).getParent(); + } + /** * Show this {@link PopupKeysPanel} in the parent view. * * @param parentView the {@link ViewGroup} that hosts this {@link PopupKeysPanel}. */ - void showInParent(ViewGroup parentView); + default void showInParent(ViewGroup parentView) { + removeFromParent(); + parentView.addView(getContainerView()); + } /** * Remove this {@link PopupKeysPanel} from the parent view. */ - void removeFromParent(); + default void removeFromParent() { + final View containerView = getContainerView(); + final ViewGroup currentParent = (ViewGroup)containerView.getParent(); + if (currentParent != null) { + currentParent.removeView(containerView); + } + } /** * Return whether the panel is currently being shown. */ - boolean isShowingInParent(); + default boolean isShowingInParent() { + return getContainerView().getParent() != null; + } } diff --git a/app/src/main/java/helium314/keyboard/keyboard/PopupTextView.java b/app/src/main/java/helium314/keyboard/keyboard/PopupTextView.java new file mode 100644 index 000000000..d20f62559 --- /dev/null +++ b/app/src/main/java/helium314/keyboard/keyboard/PopupTextView.java @@ -0,0 +1,121 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * modified + * SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-only + */ + +package helium314.keyboard.keyboard; + +import android.content.Context; +import android.graphics.Typeface; +import android.util.AttributeSet; +import android.util.TypedValue; +import android.view.Gravity; +import android.view.View; +import android.widget.TextView; +import helium314.keyboard.keyboard.emoji.EmojiViewCallback; +import helium314.keyboard.keyboard.internal.KeyDrawParams; +import helium314.keyboard.latin.R; +import helium314.keyboard.latin.common.ColorType; +import helium314.keyboard.latin.common.CoordinateUtils; +import helium314.keyboard.latin.settings.Settings; + + +/** + * A view that displays popup text. + */ +public class PopupTextView extends TextView implements PopupKeysPanel { + private final int[] mCoordinates = CoordinateUtils.newInstance(); + private final Typeface mTypeface; + private Controller mController = EMPTY_CONTROLLER; + private int mOriginX; + private int mOriginY; + private Key mKey; + private EmojiViewCallback mEmojiViewCallback; + + public PopupTextView(final Context context, final AttributeSet attrs) { + this(context, attrs, R.attr.popupKeysKeyboardViewStyle); + } + + public PopupTextView(final Context context, final AttributeSet attrs, + final int defStyle) { + super(context, attrs, defStyle); + mTypeface = Settings.getInstance().getCustomTypeface(); + } + + public void setKeyDrawParams(Key key, KeyDrawParams drawParams) { + mKey = key; + Settings.getValues().mColors.setBackground(this, ColorType.KEY_PREVIEW_BACKGROUND); + setTextColor(drawParams.mPreviewTextColor); + setTextSize(TypedValue.COMPLEX_UNIT_PX, key.selectHintTextSize(drawParams) << 1); + setTypeface(mTypeface == null ? key.selectTypeface(drawParams) : mTypeface); + } + + @Override + public void showPopupKeysPanel(final View parentView, final Controller controller, + final int pointX, final int pointY, final KeyboardActionListener listener) { + showPopupKeysPanelInternal(parentView, controller, pointX, pointY); + } + + @Override + public void showPopupKeysPanel(final View parentView, final Controller controller, + final int pointX, final int pointY, final EmojiViewCallback emojiViewCallback) { + mEmojiViewCallback = emojiViewCallback; + showPopupKeysPanelInternal(parentView, controller, pointX, pointY); + } + + private void showPopupKeysPanelInternal(final View parentView, final Controller controller, + final int pointX, final int pointY) { + mController = controller; + final View container = getContainerView(); + // The coordinates of panel's left-top corner in parentView's coordinate system. + // We need to consider background drawable paddings. + final int x = pointX - getMeasuredWidth() / 2 - container.getPaddingLeft() - getPaddingLeft(); + final int y = pointY - container.getMeasuredHeight() + container.getPaddingBottom() + + getPaddingBottom(); + + parentView.getLocationInWindow(mCoordinates); + // Ensure the horizontal position of the panel does not extend past the parentView edges. + final int maxX = parentView.getMeasuredWidth() - container.getMeasuredWidth(); + final int panelX = Math.max(0, Math.min(maxX, x)) + CoordinateUtils.x(mCoordinates); + final int panelY = y + CoordinateUtils.y(mCoordinates); + container.setX(panelX); + container.setY(panelY); + + mOriginX = x + container.getPaddingLeft(); + mOriginY = y + container.getPaddingTop(); + controller.setLayoutGravity(Gravity.NO_GRAVITY); + controller.onShowPopupKeysPanel(this); + } + + @Override + public void onDownEvent(final int x, final int y, final int pointerId, final long eventTime) { + } + + @Override + public void onMoveEvent(final int x, final int y, final int pointerId, final long eventTime) { + } + + @Override + public void onUpEvent(final int x, final int y, final int pointerId, final long eventTime) { + mEmojiViewCallback.onReleaseKey(mKey); + } + + @Override + public void dismissPopupKeysPanel() { + if (!isShowingInParent()) { + return; + } + mController.onDismissPopupKeysPanel(); + } + + @Override + public int translateX(final int x) { + return x - mOriginX; + } + + @Override + public int translateY(final int y) { + return y - mOriginY; + } +} diff --git a/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPageKeyboardView.java b/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPageKeyboardView.java index 47e0d3b83..cc1e0a359 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPageKeyboardView.java +++ b/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPageKeyboardView.java @@ -13,6 +13,9 @@ import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.os.Handler; import android.util.AttributeSet; +import android.view.Gravity; +import android.widget.LinearLayout; +import helium314.keyboard.keyboard.PopupTextView; import helium314.keyboard.latin.utils.Log; import android.view.LayoutInflater; import android.view.MotionEvent; @@ -53,14 +56,18 @@ public final class EmojiPageKeyboardView extends KeyboardView implements private static final long KEY_PRESS_DELAY_TIME = 250; // msec private static final long KEY_RELEASE_DELAY_TIME = 30; // msec - private static final OnKeyEventListener EMPTY_LISTENER = new OnKeyEventListener() { + private static final EmojiViewCallback EMPTY_EMOJI_VIEW_CALLBACK = new EmojiViewCallback() { @Override public void onPressKey(final Key key) {} @Override public void onReleaseKey(final Key key) {} + @Override + public String getDescription(String emoji) { + return null; + } }; - private OnKeyEventListener mListener = EMPTY_LISTENER; + private EmojiViewCallback mEmojiViewCallback = EMPTY_EMOJI_VIEW_CALLBACK; private final KeyDetector mKeyDetector = new KeyDetector(); private KeyboardAccessibilityDelegate mAccessibilityDelegate; @@ -74,6 +81,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements // More keys keyboard private final View mPopupKeysKeyboardContainer; + private final PopupTextView mDescriptionView; + private final PopupKeysKeyboardView mPopupKeysKeyboardView; private final WeakHashMap mPopupKeysKeyboardCache = new WeakHashMap<>(); private final boolean mConfigShowPopupKeysKeyboardAtTouchedPoint; private final ViewGroup mPopupKeysPlacerView; @@ -102,6 +111,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements final LayoutInflater inflater = LayoutInflater.from(getContext()); mPopupKeysKeyboardContainer = inflater.inflate(popupKeysKeyboardLayoutId, null); + mDescriptionView = mPopupKeysKeyboardContainer.findViewById(R.id.description_view); + mPopupKeysKeyboardView = mPopupKeysKeyboardContainer.findViewById(R.id.popup_keys_keyboard_view); } @Override @@ -146,8 +157,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements } } - public void setOnKeyEventListener(final OnKeyEventListener listener) { - mListener = listener; + public void setEmojiViewCallback(final EmojiViewCallback emojiViewCallback) { + mEmojiViewCallback = emojiViewCallback; } /** @@ -169,7 +180,8 @@ public final class EmojiPageKeyboardView extends KeyboardView implements } @Nullable - public PopupKeysPanel showPopupKeysKeyboard(@NonNull final Key key, final int lastX, final int lastY) { + private PopupKeysPanel showPopupKeysKeyboard(@NonNull final Key key) { + mPopupKeysKeyboardView.setVisibility(GONE); final PopupKeySpec[] popupKeys = key.getPopupKeys(); if (popupKeys == null) { return null; @@ -182,21 +194,9 @@ public final class EmojiPageKeyboardView extends KeyboardView implements mPopupKeysKeyboardCache.put(key, popupKeysKeyboard); } - final View container = mPopupKeysKeyboardContainer; - final PopupKeysKeyboardView popupKeysKeyboardView = container.findViewById(R.id.popup_keys_keyboard_view); - popupKeysKeyboardView.setKeyboard(popupKeysKeyboard); - container.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - - final int[] lastCoords = CoordinateUtils.newCoordinateArray(1, lastX, lastY); - // The popup keys keyboard is usually horizontally aligned with the center of the parent key. - // If showPopupKeysKeyboardAtTouchedPoint is true and the key preview is disabled, the more - // keys keyboard is placed at the touch point of the parent key. - final int pointX = mConfigShowPopupKeysKeyboardAtTouchedPoint - ? CoordinateUtils.x(lastCoords) - : key.getX() + key.getWidth() / 2; - final int pointY = key.getY(); - popupKeysKeyboardView.showPopupKeysPanel(this, this, pointX, pointY, mListener); - return popupKeysKeyboardView; + mPopupKeysKeyboardView.setKeyboard(popupKeysKeyboard); + mPopupKeysKeyboardView.setVisibility(VISIBLE); + return mPopupKeysKeyboardView; } private void dismissPopupKeysPanel() { @@ -209,6 +209,17 @@ public final class EmojiPageKeyboardView extends KeyboardView implements return mPopupKeysPanel != null; } + @Override + public void setLayoutGravity(int layoutGravity) { + var layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT); + layoutParams.gravity = mDescriptionView.getMeasuredWidth() > mPopupKeysKeyboardView.getMeasuredWidth()? + layoutGravity : Gravity.CENTER_HORIZONTAL; + mPopupKeysKeyboardContainer.setLayoutParams(layoutParams); + mDescriptionView.setLayoutParams(layoutParams); + mPopupKeysKeyboardView.setLayoutParams(layoutParams); + } + @Override public void onShowPopupKeysPanel(final PopupKeysPanel panel) { // install placer view only when needed instead of when this @@ -290,9 +301,11 @@ public final class EmojiPageKeyboardView extends KeyboardView implements return; } + var descriptionPanel = showDescription(key); + final PopupKeysPanel popupKeysPanel = showPopupKeysKeyboard(key); + final int x = mLastX; final int y = mLastY; - final PopupKeysPanel popupKeysPanel = showPopupKeysKeyboard(key, x, y); if (popupKeysPanel != null) { final int translatedX = popupKeysPanel.translateX(x); final int translatedY = popupKeysPanel.translateY(y); @@ -301,6 +314,34 @@ public final class EmojiPageKeyboardView extends KeyboardView implements // want any scroll to append during this entire input. disallowParentInterceptTouchEvent(true); } + + if (popupKeysPanel != null || descriptionPanel != null) { + mPopupKeysKeyboardContainer.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + + final int[] lastCoords = CoordinateUtils.newCoordinateArray(1, x, y); + // The popup keys keyboard is usually horizontally aligned with the center of the parent key. + // If showPopupKeysKeyboardAtTouchedPoint is true and the key preview is disabled, the more + // keys keyboard is placed at the touch point of the parent key. + final int pointX = mConfigShowPopupKeysKeyboardAtTouchedPoint + ? CoordinateUtils.x(lastCoords) + : key.getX() + key.getWidth() / 2; + final int pointY = key.getY() - getKeyboard().mVerticalGap; + (popupKeysPanel != null? popupKeysPanel : descriptionPanel) + .showPopupKeysPanel(this, this, pointX, pointY, mEmojiViewCallback); + } + } + + private PopupKeysPanel showDescription(Key key) { + mDescriptionView.setVisibility(GONE); + var description = mEmojiViewCallback.getDescription(key.getLabel()); + if (description == null) { + return null; + } + + mDescriptionView.setText(description); + mDescriptionView.setKeyDrawParams(key, getKeyDrawParams()); + mDescriptionView.setVisibility(VISIBLE); + return mDescriptionView; } private void registerPress(final Key key) { @@ -318,7 +359,7 @@ public final class EmojiPageKeyboardView extends KeyboardView implements releasedKey.onReleased(); invalidateKey(releasedKey); if (withKeyRegistering) { - mListener.onReleaseKey(releasedKey); + mEmojiViewCallback.onReleaseKey(releasedKey); } } @@ -326,7 +367,7 @@ public final class EmojiPageKeyboardView extends KeyboardView implements mPendingKeyDown = null; pressedKey.onPressed(); invalidateKey(pressedKey); - mListener.onPressKey(pressedKey); + mEmojiViewCallback.onPressKey(pressedKey); } public void releaseCurrentKey(final boolean withKeyRegistering) { diff --git a/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesAdapter.java b/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesAdapter.java index 7f7b63614..4172d3db9 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesAdapter.java +++ b/app/src/main/java/helium314/keyboard/keyboard/emoji/EmojiPalettesAdapter.java @@ -7,7 +7,6 @@ package helium314.keyboard.keyboard.emoji; import helium314.keyboard.latin.utils.Log; -import android.util.SparseArray; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -23,13 +22,13 @@ final class EmojiPalettesAdapter extends RecyclerView.Adapter, locale: Locale) { - if (!file.isFile) return - val header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(file) ?: return killDictionary(file) + val dictionary = getDictionary(file, locale) ?: return + if (dicts.any { it.mDictType == dictionary.mDictType }) { + dictionary.close() + return + } + dicts.add(dictionary) + } + + @JvmStatic + fun getDictionary( + file: File, + locale: Locale + ): Dictionary? { + if (!file.isFile) return null + val header = DictionaryInfoUtils.getDictionaryFileHeaderOrNull(file) + if (header == null) { + killDictionary(file) + return null + } val dictType = header.mIdString.split(":").first() - if (dicts.any { it.mDictType == dictType }) return val readOnlyBinaryDictionary = ReadOnlyBinaryDictionary( file.absolutePath, 0, file.length(), false, locale, dictType ) @@ -75,14 +91,13 @@ object DictionaryFactory { if (readOnlyBinaryDictionary.isValidDictionary) { if (locale.language == "ko") { // Use KoreanDictionary for Korean locale - dicts.add(KoreanDictionary(readOnlyBinaryDictionary)) - } else { - dicts.add(readOnlyBinaryDictionary) + return KoreanDictionary(readOnlyBinaryDictionary) } - } else { - readOnlyBinaryDictionary.close() - killDictionary(file) + return readOnlyBinaryDictionary } + readOnlyBinaryDictionary.close() + killDictionary(file) + return null } private fun killDictionary(file: File) { diff --git a/app/src/main/java/helium314/keyboard/latin/KoreanDictionary.java b/app/src/main/java/helium314/keyboard/latin/KoreanDictionary.java index 9b791148c..2b6722777 100644 --- a/app/src/main/java/helium314/keyboard/latin/KoreanDictionary.java +++ b/app/src/main/java/helium314/keyboard/latin/KoreanDictionary.java @@ -4,6 +4,7 @@ package helium314.keyboard.latin; import helium314.keyboard.event.HangulCombiner; import helium314.keyboard.latin.common.ComposedData; +import helium314.keyboard.latin.makedict.WordProperty; import helium314.keyboard.latin.settings.SettingsValuesForSuggestion; import java.text.Normalizer; @@ -72,6 +73,11 @@ public class KoreanDictionary extends Dictionary { return mDictionary.getMaxFrequencyOfExactMatches(processInput(word)); } + @Override + public WordProperty getWordProperty(String word, boolean isBeginningOfSentence) { + return mDictionary.getWordProperty(processInput(word), isBeginningOfSentence); + } + @Override protected boolean same(char[] word, int length, String typedWord) { word = processInput(new String(word)).toCharArray(); diff --git a/app/src/main/java/helium314/keyboard/latin/LatinIME.java b/app/src/main/java/helium314/keyboard/latin/LatinIME.java index 670e820bd..1f7a6221c 100644 --- a/app/src/main/java/helium314/keyboard/latin/LatinIME.java +++ b/app/src/main/java/helium314/keyboard/latin/LatinIME.java @@ -279,9 +279,7 @@ public class LatinIME extends InputMethodService implements msg.arg2 /* remainingTries */, this /* handler */)) { // If we were able to reset the caches, then we can reload the keyboard. // Otherwise, we'll do it when we can. - latinIme.mKeyboardSwitcher.loadKeyboard(latinIme.getCurrentInputEditorInfo(), - settingsValues, latinIme.getCurrentAutoCapsState(), - latinIme.getCurrentRecapitalizeState()); + latinIme.mKeyboardSwitcher.reloadMainKeyboard(); } break; case MSG_WAIT_FOR_DICTIONARY_LOAD: @@ -1071,7 +1069,7 @@ public class LatinIME extends InputMethodService implements if (isDifferentTextField) { mainKeyboardView.closing(); suggest.setAutoCorrectionThreshold(currentSettingsValues.mAutoCorrectionThreshold); - switcher.loadKeyboard(editorInfo, currentSettingsValues, getCurrentAutoCapsState(), getCurrentRecapitalizeState()); + switcher.reloadMainKeyboard(); if (needToCallLoadKeyboardLater) { // If we need to call loadKeyboard again later, we need to save its state now. The // later call will be done in #retryResetCaches. @@ -1763,8 +1761,7 @@ public class LatinIME extends InputMethodService implements loadSettings(); if (mKeyboardSwitcher.getMainKeyboardView() != null) { // Reload keyboard because the current language has been changed. - mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mSettings.getCurrent(), - getCurrentAutoCapsState(), getCurrentRecapitalizeState()); + mKeyboardSwitcher.reloadMainKeyboard(); } } diff --git a/app/src/main/java/helium314/keyboard/latin/ReadOnlyBinaryDictionary.java b/app/src/main/java/helium314/keyboard/latin/ReadOnlyBinaryDictionary.java index b4cfe5f3a..f757e87eb 100644 --- a/app/src/main/java/helium314/keyboard/latin/ReadOnlyBinaryDictionary.java +++ b/app/src/main/java/helium314/keyboard/latin/ReadOnlyBinaryDictionary.java @@ -10,6 +10,7 @@ import com.android.inputmethod.latin.BinaryDictionary; import helium314.keyboard.latin.SuggestedWords.SuggestedWordInfo; import helium314.keyboard.latin.common.ComposedData; +import helium314.keyboard.latin.makedict.WordProperty; import helium314.keyboard.latin.settings.SettingsValuesForSuggestion; import java.util.ArrayList; @@ -107,6 +108,18 @@ public final class ReadOnlyBinaryDictionary extends Dictionary { return NOT_A_PROBABILITY; } + @Override + public WordProperty getWordProperty(String word, boolean isBeginningOfSentence) { + if (mLock.readLock().tryLock()) { + try { + return mBinaryDictionary.getWordProperty(word, isBeginningOfSentence); + } finally { + mLock.readLock().unlock(); + } + } + return null; + } + @Override public void close() { mLock.writeLock().lock(); diff --git a/app/src/main/java/helium314/keyboard/latin/SingleDictionaryFacilitator.kt b/app/src/main/java/helium314/keyboard/latin/SingleDictionaryFacilitator.kt index e8941c459..4e2d62486 100644 --- a/app/src/main/java/helium314/keyboard/latin/SingleDictionaryFacilitator.kt +++ b/app/src/main/java/helium314/keyboard/latin/SingleDictionaryFacilitator.kt @@ -7,6 +7,7 @@ import helium314.keyboard.keyboard.Keyboard import helium314.keyboard.keyboard.KeyboardSwitcher import helium314.keyboard.latin.DictionaryFacilitator.DictionaryInitializationListener import helium314.keyboard.latin.common.ComposedData +import helium314.keyboard.latin.makedict.WordProperty import helium314.keyboard.latin.settings.SettingsValuesForSuggestion import helium314.keyboard.latin.utils.SuggestionResults import java.util.Locale @@ -47,6 +48,8 @@ class SingleDictionaryFacilitator(private val dict: Dictionary) : DictionaryFaci return suggestionResults } + fun getWordProperty(word: String): WordProperty? = dict.getWordProperty(word, false) + // ------------ dummy functionality ---------------- override fun setValidSpellingWordReadCache(cache: LruCache) {} diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt b/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt index 98e93a64f..a73760205 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt +++ b/app/src/main/java/helium314/keyboard/latin/settings/Defaults.kt @@ -58,6 +58,7 @@ object Defaults { const val PREF_VIBRATE_ON = false const val PREF_VIBRATE_IN_DND_MODE = false const val PREF_SOUND_ON = false + const val PREF_SHOW_EMOJI_DESCRIPTIONS = true @JvmField var PREF_POPUP_ON = true const val PREF_AUTO_CORRECTION = true diff --git a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java index ea6e8f59f..cc72c893c 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/Settings.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/Settings.java @@ -67,6 +67,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_VIBRATE_ON = "vibrate_on"; public static final String PREF_VIBRATE_IN_DND_MODE = "vibrate_in_dnd_mode"; public static final String PREF_SOUND_ON = "sound_on"; + public static final String PREF_SHOW_EMOJI_DESCRIPTIONS = "show_emoji_descriptions"; public static final String PREF_POPUP_ON = "popup_on"; public static final String PREF_AUTO_CORRECTION = "auto_correction"; public static final String PREF_MORE_AUTO_CORRECTION = "more_auto_correction"; diff --git a/app/src/main/java/helium314/keyboard/latin/settings/SettingsValues.java b/app/src/main/java/helium314/keyboard/latin/settings/SettingsValues.java index 689a63700..f750f1072 100644 --- a/app/src/main/java/helium314/keyboard/latin/settings/SettingsValues.java +++ b/app/src/main/java/helium314/keyboard/latin/settings/SettingsValues.java @@ -54,6 +54,7 @@ public class SettingsValues { public final boolean mVibrateOn; public final boolean mVibrateInDndMode; public final boolean mSoundOn; + public final boolean mShowEmojiDescriptions; public final boolean mKeyPreviewPopupOn; public final boolean mShowsVoiceInputKey; public final boolean mLanguageSwitchKeyToOtherImes; @@ -169,6 +170,7 @@ public class SettingsValues { mVibrateOn = Settings.readVibrationEnabled(prefs); mVibrateInDndMode = prefs.getBoolean(Settings.PREF_VIBRATE_IN_DND_MODE, Defaults.PREF_VIBRATE_IN_DND_MODE); mSoundOn = prefs.getBoolean(Settings.PREF_SOUND_ON, Defaults.PREF_SOUND_ON); + mShowEmojiDescriptions = prefs.getBoolean(Settings.PREF_SHOW_EMOJI_DESCRIPTIONS, Defaults.PREF_SHOW_EMOJI_DESCRIPTIONS); mKeyPreviewPopupOn = prefs.getBoolean(Settings.PREF_POPUP_ON, Defaults.PREF_POPUP_ON); mSlidingKeyInputPreviewEnabled = prefs.getBoolean( DebugSettings.PREF_SLIDING_KEY_INPUT_PREVIEW, Defaults.PREF_SLIDING_KEY_INPUT_PREVIEW); diff --git a/app/src/main/java/helium314/keyboard/latin/utils/DictionaryInfoUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/DictionaryInfoUtils.kt index 8715d2d4c..05417fdb3 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/DictionaryInfoUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/DictionaryInfoUtils.kt @@ -101,6 +101,10 @@ object DictionaryInfoUtils { return absoluteDirectoryName } + @JvmStatic + fun getCachedDictForLocaleAndType(locale: Locale, type: String, context: Context): File? = + getCachedDictsForLocale(locale, context).firstOrNull { it.name.substringBefore("_") == type } + fun getCachedDictsForLocale(locale: Locale, context: Context) = getCacheDirectoryForLocale(locale, context)?.let { File(it).listFiles() }.orEmpty() diff --git a/app/src/main/java/helium314/keyboard/settings/screens/PreferencesScreen.kt b/app/src/main/java/helium314/keyboard/settings/screens/PreferencesScreen.kt index 272757201..c41696059 100644 --- a/app/src/main/java/helium314/keyboard/settings/screens/PreferencesScreen.kt +++ b/app/src/main/java/helium314/keyboard/settings/screens/PreferencesScreen.kt @@ -57,6 +57,7 @@ fun PreferencesScreen( Settings.PREF_SOUND_ON, if (prefs.getBoolean(Settings.PREF_SOUND_ON, Defaults.PREF_SOUND_ON)) Settings.PREF_KEYPRESS_SOUND_VOLUME else null, + Settings.PREF_SHOW_EMOJI_DESCRIPTIONS, R.string.settings_category_additional_keys, Settings.PREF_SHOW_NUMBER_ROW, if (SubtypeSettings.getEnabledSubtypes(true).any { it.locale().language in localesWithLocalizedNumberRow }) @@ -111,6 +112,14 @@ fun createPreferencesSettings(context: Context) = listOf( Setting(context, Settings.PREF_SOUND_ON, R.string.sound_on_keypress) { SwitchPreference(it, Defaults.PREF_SOUND_ON) }, + Setting( + context, Settings.PREF_SHOW_EMOJI_DESCRIPTIONS, R.string.show_emoji_descriptions, + R.string.show_emoji_descriptions_summary + ) { + SwitchPreference(it, Defaults.PREF_SHOW_EMOJI_DESCRIPTIONS) { + KeyboardSwitcher.getInstance().reloadKeyboard() + } + }, Setting(context, Settings.PREF_ENABLE_CLIPBOARD_HISTORY, R.string.enable_clipboard_history, R.string.enable_clipboard_history_summary) { diff --git a/app/src/main/res/layout/popup_keys_keyboard.xml b/app/src/main/res/layout/popup_keys_keyboard.xml index a8bea57c4..e9e249482 100644 --- a/app/src/main/res/layout/popup_keys_keyboard.xml +++ b/app/src/main/res/layout/popup_keys_keyboard.xml @@ -10,6 +10,15 @@ android:layout_height="wrap_content" android:orientation="vertical" > + Vibrate in do not disturb mode Sound on keypress + + Show emoji description on long press + + Requires an emoji dictionary Popup on keypress