From d5f36885c475411dfaa87f81b42e04b128effa17 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Sun, 16 Jun 2024 22:10:18 +0200 Subject: [PATCH] clipboard: Enable history collection with a checkbox The feature is off by default for privacy reasons. The checkbox state is persisted in the configuration. The history is immediately cleared when it's disabled. The content of the system clipboard is visible when the history is enabled. --- res/layout/clipboard_pane.xml | 2 +- .../ClipboardHistoryCheckBox.java | 22 ++++++++++ .../ClipboardHistoryService.java | 40 ++++++++++++++++--- srcs/juloo.keyboard2/Config.java | 2 + 4 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 srcs/juloo.keyboard2/ClipboardHistoryCheckBox.java diff --git a/res/layout/clipboard_pane.xml b/res/layout/clipboard_pane.xml index bcb96a3..84dc6c9 100644 --- a/res/layout/clipboard_pane.xml +++ b/res/layout/clipboard_pane.xml @@ -2,7 +2,7 @@ - + diff --git a/srcs/juloo.keyboard2/ClipboardHistoryCheckBox.java b/srcs/juloo.keyboard2/ClipboardHistoryCheckBox.java new file mode 100644 index 0000000..9842058 --- /dev/null +++ b/srcs/juloo.keyboard2/ClipboardHistoryCheckBox.java @@ -0,0 +1,22 @@ +package juloo.keyboard2; + +import android.content.Context; +import android.util.AttributeSet; +import android.widget.CheckBox; +import android.widget.CompoundButton; + +final class ClipboardHistoryCheckBox extends CheckBox + implements CompoundButton.OnCheckedChangeListener +{ + public ClipboardHistoryCheckBox(Context ctx, AttributeSet attrs) + { + super(ctx, attrs); + setOnCheckedChangeListener(this); + } + + @Override + public void onCheckedChanged(CompoundButton _v, boolean isChecked) + { + ClipboardHistoryService.set_history_enabled(isChecked); + } +} diff --git a/srcs/juloo.keyboard2/ClipboardHistoryService.java b/srcs/juloo.keyboard2/ClipboardHistoryService.java index 2800f45..0231d8d 100644 --- a/srcs/juloo.keyboard2/ClipboardHistoryService.java +++ b/srcs/juloo.keyboard2/ClipboardHistoryService.java @@ -27,6 +27,19 @@ public final class ClipboardHistoryService return _service; } + public static void set_history_enabled(boolean e) + { + if (_service == null) + return; + Config.globalPrefs().edit() + .putBoolean("clipboard_history_enabled", e) + .commit(); + if (e) + _service.add_current_clip(); + else + _service.clear_history(); + } + /** The maximum size limits the amount of user data stored in memory but also gives a sense to the user that the history is not persisted and can be forgotten as soon as the app stops. */ @@ -89,6 +102,8 @@ public final class ClipboardHistoryService empty strings. */ public void add_clip(String clip) { + if (!Config.globalConfig().clipboard_history_enabled) + return; int size = _history.size(); if (clip.equals("") || (size > 0 && _history.get(size - 1).content.equals(clip))) return; @@ -99,6 +114,13 @@ public final class ClipboardHistoryService _listener.on_clipboard_history_change(); } + public void clear_history() + { + _history.clear(); + if (_listener != null) + _listener.on_clipboard_history_change(); + } + public void set_on_clipboard_history_change(OnClipboardHistoryChange l) { _listener = l; } public static interface OnClipboardHistoryChange @@ -106,6 +128,17 @@ public final class ClipboardHistoryService public void on_clipboard_history_change(); } + /** Add what is currently in the system clipboard into the history. */ + void add_current_clip() + { + ClipData clip = _cm.getPrimaryClip(); + if (clip == null) + return; + int count = clip.getItemCount(); + for (int i = 0; i < count; i++) + add_clip(clip.getItemAt(i).getText().toString()); + } + final class SystemListener implements ClipboardManager.OnPrimaryClipChangedListener { public SystemListener() {} @@ -113,12 +146,7 @@ public final class ClipboardHistoryService @Override public void onPrimaryClipChanged() { - ClipData clip = _cm.getPrimaryClip(); - if (clip == null) - return; - int count = clip.getItemCount(); - for (int i = 0; i < count; i++) - add_clip(clip.getItemAt(i).getText().toString()); + add_current_clip(); } } diff --git a/srcs/juloo.keyboard2/Config.java b/srcs/juloo.keyboard2/Config.java index 8b72bc7..980bac2 100644 --- a/srcs/juloo.keyboard2/Config.java +++ b/srcs/juloo.keyboard2/Config.java @@ -66,6 +66,7 @@ public final class Config public boolean pin_entry_enabled; public boolean borderConfig; public int circle_sensitivity; + public boolean clipboard_history_enabled; // Dynamically set public boolean shouldOfferVoiceTyping; @@ -183,6 +184,7 @@ public final class Config current_layout_portrait = _prefs.getInt("current_layout_portrait", 0); current_layout_landscape = _prefs.getInt("current_layout_landscape", 0); circle_sensitivity = Integer.valueOf(_prefs.getString("circle_sensitivity", "2")); + clipboard_history_enabled = _prefs.getBoolean("clipboard_history_enabled", false); } public int get_current_layout()