Skip to content

Commit

Permalink
dismiss clipboard suggestion. better toolbar auto toggle. 3 min reten…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
codokie committed Apr 10, 2024
1 parent 7e6cb52 commit 779cab4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ class ClipboardHistoryManager(
historyEntries.removeAt(index)
}

fun removeEntry(content: String) {
val index = historyEntries.indexOfFirst { it.content.toString() == content }
removeEntry(index)
}

private fun sortHistoryEntries() {
historyEntries.sort()
}
Expand Down Expand Up @@ -139,13 +134,13 @@ class ClipboardHistoryManager(
return clipData.getItemAt(0)?.coerceToText(latinIME) ?: ""
}

fun retrieveRecentClipboardContent(updateEntry: Boolean): String {
fun retrieveRecentClipboardContent(): String {
val clipContent = retrieveClipboardContent().toString()
val now = System.currentTimeMillis()
val isNewEntry = recentEntry != clipContent
val isRecent = (now - recentTimestamp) < TWO_MINUTES_MILLIS
val isRecent = (now - recentTimestamp) < SUGGESTION_INTERVAL
return if (isNewEntry || isRecent && !suggestionPicked) {
if (updateEntry && isNewEntry) {
if (isNewEntry) {
suggestionPicked = false
recentEntry = clipContent
recentTimestamp = now
Expand Down Expand Up @@ -180,9 +175,9 @@ class ClipboardHistoryManager(
companion object {
// store pinned clips in companion object so they survive a keyboard switch (which destroys the current instance)
private val historyEntries: MutableList<ClipboardHistoryEntry> = ArrayList()
private const val TWO_MINUTES_MILLIS = 2 * 60 * 1000L
private var recentEntry: String = ""
private var recentTimestamp: Long = 0L
private var suggestionPicked: Boolean = false
private const val SUGGESTION_INTERVAL = 3 * 60 * 1000L
}
}
13 changes: 4 additions & 9 deletions app/src/main/java/helium314/keyboard/latin/LatinIME.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void postResumeSuggestions(final boolean shouldDelay) {
}
if (!latinIme.mSettings.getCurrent().isSuggestionsEnabledPerUserSettings()
&& (!latinIme.mSettings.getCurrent().mSuggestClipboardContent
|| latinIme.mClipboardHistoryManager.retrieveRecentClipboardContent(false).isEmpty())) {
|| latinIme.mClipboardHistoryManager.retrieveRecentClipboardContent().isEmpty())) {
return;
}
removeMessages(MSG_RESUME_SUGGESTIONS);
Expand Down Expand Up @@ -1623,6 +1623,7 @@ public void showSuggestionStrip(final SuggestedWords suggestedWords) {
setNeutralSuggestionStrip();
} else {
setSuggestedWords(suggestedWords);
mSuggestionStripView.setToolbarVisibility(false);
}
// Cache the auto-correction in accessibility code so we can speak it if the user
// touches a key that will insert it.
Expand All @@ -1641,13 +1642,6 @@ public void pickSuggestionManually(final SuggestedWordInfo suggestionInfo) {
updateStateAfterInputTransaction(completeInputTransaction);
}

@Override
public void onClipboardSuggestionRemoved(String clipContent){
if (mSettings.getCurrent().mClipboardHistoryEnabled) {
mClipboardHistoryManager.removeEntry(clipContent);
}
}

@Override
public void onClipboardSuggestionPicked(){
mClipboardHistoryManager.markSuggestionAsPicked();
Expand All @@ -1662,13 +1656,14 @@ public void onClipboardSuggestionPicked(){
public void setNeutralSuggestionStrip() {
final SettingsValues currentSettings = mSettings.getCurrent();
if (currentSettings.mSuggestClipboardContent) {
final String clipContent = mClipboardHistoryManager.retrieveRecentClipboardContent(true);
final String clipContent = mClipboardHistoryManager.retrieveRecentClipboardContent();
if (!clipContent.isEmpty()) {
final EditorInfo editorInfo = getCurrentInputEditorInfo();
final int inputType = (editorInfo != null) ? editorInfo.inputType : InputType.TYPE_NULL;
// make sure content that is not a number is not suggested in a number input type
if (!InputTypeUtils.isNumberInputType(inputType) || StringUtilsKt.isValidNumber(clipContent)) {
setSuggestedWords(mInputLogic.getClipboardSuggestion(clipContent, inputType));
mSuggestionStripView.setToolbarVisibility(false);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import android.widget.TextView;

import helium314.keyboard.accessibility.AccessibilityUtils;
import helium314.keyboard.compat.ClipboardManagerCompat;
import helium314.keyboard.keyboard.Keyboard;
import helium314.keyboard.keyboard.MainKeyboardView;
import helium314.keyboard.keyboard.PopupKeysPanel;
Expand Down Expand Up @@ -80,7 +79,6 @@ public interface Listener {
void onCodeInput(int primaryCode, int x, int y, boolean isKeyRepeat);
void onTextInput(final String rawText);
void removeSuggestion(final String word);
void onClipboardSuggestionRemoved(String clipContent);
void onClipboardSuggestionPicked();
CharSequence getSelection();
}
Expand All @@ -94,6 +92,7 @@ public interface Listener {
private final Drawable mIncognitoIcon;
private final Drawable mToolbarArrowIcon;
private final Drawable mBinIcon;
private final Drawable mCloseIcon;
private final ViewGroup mToolbar;
private final View mToolbarContainer;
private final ViewGroup mPinnedKeys;
Expand Down Expand Up @@ -194,6 +193,7 @@ public SuggestionStripView(final Context context, final AttributeSet attrs, fina
mIncognitoIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconIncognitoKey);
mToolbarArrowIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconToolbarKey);
mBinIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconBin);
mCloseIcon = keyboardAttr.getDrawable(R.styleable.Keyboard_iconClose);

final LinearLayout.LayoutParams toolbarKeyLayoutParams = new LinearLayout.LayoutParams(
getResources().getDimensionPixelSize(R.dimen.config_suggestions_strip_edge_key_width),
Expand Down Expand Up @@ -284,7 +284,6 @@ public void setSuggestions(final SuggestedWords suggestedWords, final boolean is
mStartIndexOfMoreSuggestions = mLayoutHelper.layoutAndReturnStartIndexOfMoreSuggestions(
getContext(), mSuggestedWords, mSuggestionsStrip, this);
setInlineSuggestionsView(mCurrentInlineAutofillSuggestionsView);
setToolbarVisibility(false);
}

public void setInlineSuggestionsView(final View view) {
Expand Down Expand Up @@ -435,10 +434,10 @@ private boolean onLongClickSuggestion(final TextView wordView) {
showIcon = false;
}
if (showIcon) {
final Drawable icon = mBinIcon;
final Drawable icon = mSuggestedWords.isClipboardSuggestion() ? mCloseIcon : mBinIcon;
Settings.getInstance().getCurrent().mColors.setColor(icon, ColorType.SUGGESTION_ICONS);
int w = icon.getIntrinsicWidth();
int h = icon.getIntrinsicWidth();
int w = wordView.getWidth();
int h = wordView.getHeight();
wordView.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
wordView.setEllipsize(TextUtils.TruncateAt.END);
AtomicBoolean downOk = new AtomicBoolean(false);
Expand Down Expand Up @@ -486,33 +485,13 @@ private void showSourceDict(final TextView wordView) {
uglyWorkaround.show();
}

private void removeClipboardSuggestion(final String content) {
final ClipboardManager clipboardManager = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
final ClipData clipData = clipboardManager.getPrimaryClip();
final boolean isContentVisible = mSuggestedWords.mInputStyle != SuggestedWords.INPUT_STYLE_PASSWORD;
if (clipData != null && clipData.getItemCount() > 0) {
final String clipContent = clipData.getItemAt(0).coerceToText(getContext()).toString();
if (content.equals(clipContent)) {
if (isContentVisible) { // clear the primary clipboard when it has the same content of the visible suggestion
ClipboardManagerCompat.clearPrimaryClip(clipboardManager);
} else { // remove hidden content from internal history since it matches the primary clipboard
mListener.onClipboardSuggestionRemoved(clipContent);
}
}
}
if (isContentVisible)
mListener.onClipboardSuggestionRemoved(content); // remove the visible suggestion from history
else { // content is redacted, clear the primary clipboard
ClipboardManagerCompat.clearPrimaryClip(clipboardManager);
}
SuggestedWords.clearSuggestedWordInfoList(mSuggestedWords);
}

private void removeSuggestion(TextView wordView) {
final String word = wordView.getText().toString();
// if it's a clipboard suggestion, clear the clipboard
if (mSuggestedWords.isClipboardSuggestion()) {
removeClipboardSuggestion(mSuggestedWords.getInfo(0).mWord);
mListener.onClipboardSuggestionPicked();
SuggestedWords.clearSuggestedWordInfoList(mSuggestedWords);
setToolbarVisibility(true);
}
mListener.removeSuggestion(word);
mMoreSuggestionsView.dismissPopupKeysPanel();
Expand Down

0 comments on commit 779cab4

Please sign in to comment.