From e357f84572b127288dbc7b6c0e87ec4a0f48420b Mon Sep 17 00:00:00 2001 From: codokie <151087174+codokie@users.noreply.github.com> Date: Fri, 31 May 2024 18:01:50 +0300 Subject: [PATCH] Add toast notification when copying from clipboard (#752) and use the toast for showing dictionary on double-long-press word: switch from one ugly workaround (popup menu) to another (hiding more suggestions panel) Co-authored-by: codokie <@> Co-authored-by: Helium314 --- .../keyboard/keyboard/KeyboardSwitcher.java | 50 ++++++++++++++++++- .../keyboard/latin/RichInputConnection.java | 6 +++ .../suggestions/SuggestionStripView.java | 11 ++-- app/src/main/res/anim/fade_in.xml | 5 ++ app/src/main/res/anim/fade_out.xml | 5 ++ .../main/res/drawable/toast_background.xml | 6 +++ app/src/main/res/layout/fake_toast.xml | 18 +++++++ app/src/main/res/layout/input_view.xml | 3 ++ app/src/main/res/values-night/colors.xml | 1 + app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 2 + 11 files changed, 101 insertions(+), 7 deletions(-) create mode 100644 app/src/main/res/anim/fade_in.xml create mode 100644 app/src/main/res/anim/fade_out.xml create mode 100644 app/src/main/res/drawable/toast_background.xml create mode 100644 app/src/main/res/layout/fake_toast.xml diff --git a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java index 1adb45216..6fd68514b 100644 --- a/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java +++ b/app/src/main/java/helium314/keyboard/keyboard/KeyboardSwitcher.java @@ -10,14 +10,19 @@ import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Configuration; import android.content.res.Resources; -import helium314.keyboard.latin.utils.Log; +import android.graphics.drawable.Drawable; +import android.os.Build; import android.view.ContextThemeWrapper; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; +import android.view.animation.AnimationUtils; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodSubtype; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; +import android.widget.TextView; +import android.widget.Toast; import androidx.annotation.NonNull; @@ -36,6 +41,7 @@ import helium314.keyboard.latin.settings.Settings; import helium314.keyboard.latin.settings.SettingsValues; import helium314.keyboard.latin.utils.CapsModeUtils; import helium314.keyboard.latin.utils.LanguageOnSpacebarUtils; +import helium314.keyboard.latin.utils.Log; import helium314.keyboard.latin.utils.RecapitalizeStatus; import helium314.keyboard.latin.utils.ResourceUtils; import helium314.keyboard.latin.utils.ScriptUtils; @@ -53,6 +59,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { private HorizontalScrollView mClipboardStripScrollView; private View mSuggestionStripView; private ClipboardHistoryView mClipboardHistoryView; + private TextView mFakeToastView; private LatinIME mLatinIME; private RichInputMethodManager mRichImm; private boolean mIsHardwareAcceleratedDrawingEnabled; @@ -467,6 +474,46 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { Settings.getInstance().writeOneHandedModeGravity(mKeyboardViewWrapper.getOneHandedGravity()); } + /** + * Displays a toast message. + * + * @param text The text to display in the toast message. + * @param briefToast If true, the toast duration will be short; otherwise, it will last longer. + */ + public void showToast(final String text, final boolean briefToast){ + // In API 32 and below, toasts can be shown without a notification permission. + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { + final int toastLength = briefToast ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG; + final Toast toast = Toast.makeText(mLatinIME, text, toastLength); + toast.setGravity(Gravity.CENTER, 0, 0); + toast.show(); + } else { + final int toastLength = briefToast ? 2000 : 3500; + showFakeToast(text, toastLength); + } + } + + // Displays a toast-like message with the provided text for a specified duration. + public void showFakeToast(final String text, final int timeMillis) { + if (mFakeToastView.getVisibility() == View.VISIBLE) return; + + final Drawable appIcon = mFakeToastView.getCompoundDrawables()[0]; + if (appIcon != null) { + final int bound = mFakeToastView.getLineHeight(); + appIcon.setBounds(0, 0, bound, bound); + mFakeToastView.setCompoundDrawables(appIcon, null, null, null); + } + mFakeToastView.setText(text); + mFakeToastView.setVisibility(View.VISIBLE); + mFakeToastView.bringToFront(); + mFakeToastView.startAnimation(AnimationUtils.loadAnimation(mLatinIME, R.anim.fade_in)); + + mFakeToastView.postDelayed(() -> { + mFakeToastView.startAnimation(AnimationUtils.loadAnimation(mLatinIME, R.anim.fade_out)); + mFakeToastView.setVisibility(View.GONE); + }, timeMillis); + } + // Implements {@link KeyboardState.SwitchActions}. @Override public boolean isInDoubleTapShiftKeyTimeout() { @@ -562,6 +609,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions { mMainKeyboardFrame = mCurrentInputView.findViewById(R.id.main_keyboard_frame); mEmojiPalettesView = mCurrentInputView.findViewById(R.id.emoji_palettes_view); mClipboardHistoryView = mCurrentInputView.findViewById(R.id.clipboard_history_view); + mFakeToastView = mCurrentInputView.findViewById(R.id.fakeToast); mKeyboardViewWrapper = mCurrentInputView.findViewById(R.id.keyboard_view_wrapper); mKeyboardViewWrapper.setKeyboardActionListener(mLatinIME.mKeyboardActionListener); diff --git a/app/src/main/java/helium314/keyboard/latin/RichInputConnection.java b/app/src/main/java/helium314/keyboard/latin/RichInputConnection.java index 8e4dc0571..f4711f5f6 100644 --- a/app/src/main/java/helium314/keyboard/latin/RichInputConnection.java +++ b/app/src/main/java/helium314/keyboard/latin/RichInputConnection.java @@ -10,11 +10,14 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; import android.inputmethodservice.InputMethodService; +import android.os.Build; import android.os.Bundle; import android.os.SystemClock; import android.text.SpannableStringBuilder; import android.text.TextUtils; import android.text.style.CharacterStyle; + +import helium314.keyboard.keyboard.KeyboardSwitcher; import helium314.keyboard.latin.utils.Log; import android.view.KeyEvent; import android.view.inputmethod.CompletionInfo; @@ -671,6 +674,9 @@ public final class RichInputConnection implements PrivateCommandPerformer { if (text == null || text.length() == 0) return; final ClipboardManager cm = (ClipboardManager) mParent.getSystemService(Context.CLIPBOARD_SERVICE); cm.setPrimaryClip(ClipData.newPlainText("copied text", text)); + if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.S_V2) { + KeyboardSwitcher.getInstance().showToast(mParent.getString(R.string.toast_msg_clipboard_copy), true); + } } public void commitCorrection(final CorrectionInfo correctionInfo) { diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java index af6699cba..c63cd2d6f 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.java @@ -41,6 +41,7 @@ import android.widget.TextView; import helium314.keyboard.accessibility.AccessibilityUtils; import helium314.keyboard.keyboard.Keyboard; +import helium314.keyboard.keyboard.KeyboardSwitcher; import helium314.keyboard.keyboard.MainKeyboardView; import helium314.keyboard.keyboard.PopupKeysPanel; import helium314.keyboard.keyboard.internal.keyboard_parser.floris.KeyCode; @@ -445,12 +446,10 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick final SuggestedWordInfo info = mSuggestedWords.getInfo(index); if (!info.getWord().equals(word)) return; final String text = info.mSourceDict.mDictType + ":" + info.mSourceDict.mLocale; - // apparently toast is not working on some Android versions, probably - // Android 13 with the notification permission - // Toast.makeText(getContext(), text, Toast.LENGTH_LONG).show(); - final PopupMenu uglyWorkaround = new PopupMenu(DialogUtilsKt.getPlatformDialogThemeContext(getContext()), wordView); - uglyWorkaround.getMenu().add(Menu.NONE, 1, Menu.NONE, text); - uglyWorkaround.show(); + if (isShowingMoreSuggestionPanel()) { + mMoreSuggestionsView.dismissPopupKeysPanel(); + } + KeyboardSwitcher.getInstance().showToast(text, true); } private void removeSuggestion(TextView wordView) { diff --git a/app/src/main/res/anim/fade_in.xml b/app/src/main/res/anim/fade_in.xml new file mode 100644 index 000000000..875cd5f97 --- /dev/null +++ b/app/src/main/res/anim/fade_in.xml @@ -0,0 +1,5 @@ + + diff --git a/app/src/main/res/anim/fade_out.xml b/app/src/main/res/anim/fade_out.xml new file mode 100644 index 000000000..cf78f0d3a --- /dev/null +++ b/app/src/main/res/anim/fade_out.xml @@ -0,0 +1,5 @@ + + diff --git a/app/src/main/res/drawable/toast_background.xml b/app/src/main/res/drawable/toast_background.xml new file mode 100644 index 000000000..4e0da5a4b --- /dev/null +++ b/app/src/main/res/drawable/toast_background.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/app/src/main/res/layout/fake_toast.xml b/app/src/main/res/layout/fake_toast.xml new file mode 100644 index 000000000..7d41e0004 --- /dev/null +++ b/app/src/main/res/layout/fake_toast.xml @@ -0,0 +1,18 @@ + + + diff --git a/app/src/main/res/layout/input_view.xml b/app/src/main/res/layout/input_view.xml index c719d55ab..0bd874eee 100644 --- a/app/src/main/res/layout/input_view.xml +++ b/app/src/main/res/layout/input_view.xml @@ -10,6 +10,9 @@ android:layout_width="match_parent" android:layout_height="wrap_content" style="?attr/inputViewStyle"> + diff --git a/app/src/main/res/values-night/colors.xml b/app/src/main/res/values-night/colors.xml index 332735c97..c9aafc8c6 100644 --- a/app/src/main/res/values-night/colors.xml +++ b/app/src/main/res/values-night/colors.xml @@ -6,6 +6,7 @@ #FFF #BBB #666 + #444444 @color/keyboard_background_dark diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3d3bcfd53..8e1aaab24 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -68,6 +68,7 @@ #000 #555 #AAA + #666666 #1A73E8 @color/keyboard_background_light diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e0ec40ce..21806019e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -830,4 +830,6 @@ New dictionary: Variable toolbar direction Reverse direction when a right-to-left keyboard subtype is selected + + Content copied