From dab50e0572e98d00be1645e3c0843576eb1fb249 Mon Sep 17 00:00:00 2001 From: Jules Aguillon Date: Fri, 26 Dec 2025 16:43:34 +0100 Subject: Fix clipboard manager crashes (#1148) * Fix background crash in clipboard history service The clipboard history service might crash when the connection between the keyboard and the system is in a bad state. * Fix race condition in clipboard history service The clipboard history service callback appears to be called concurrently leading to concurrent accesses to data and to concurrent calls to 'on_clipboard_history_change'. Access are now synchronized. * Fix clipboard manager crash when device is locked--- srcs/juloo.keyboard2/ClipboardHistoryService.java | 31 +++++++++++++---------- 1 file changed, 18 insertions(+), 13 deletions(-) (limited to 'srcs/juloo.keyboard2/ClipboardHistoryService.java') diff --git a/srcs/juloo.keyboard2/ClipboardHistoryService.java b/srcs/juloo.keyboard2/ClipboardHistoryService.java index 27e2eb9..5ec5ab5 100644 --- a/srcs/juloo.keyboard2/ClipboardHistoryService.java +++ b/srcs/juloo.keyboard2/ClipboardHistoryService.java @@ -66,7 +66,7 @@ public final class ClipboardHistoryService _cm.addPrimaryClipChangedListener(this.new SystemListener()); } - public List clear_expired_and_get_history() + public synchronized List clear_expired_and_get_history() { long now_ms = System.currentTimeMillis(); List dst = new ArrayList(); @@ -83,30 +83,33 @@ public final class ClipboardHistoryService } /** This will call [on_clipboard_history_change]. */ - public void remove_history_entry(String clip) + public synchronized void remove_history_entry(String clip) { int last_pos = _history.size() - 1; + boolean last_pos_changed = false; for (int pos = last_pos; pos >= 0; pos--) { if (!_history.get(pos).content.equals(clip)) continue; // Removing the current clipboard, clear the system clipboard. if (pos == last_pos) - { - if (VERSION.SDK_INT >= 28) - _cm.clearPrimaryClip(); - else - _cm.setText(""); - } + last_pos_changed = true; _history.remove(pos); - if (_listener != null) - _listener.on_clipboard_history_change(); } + if (last_pos_changed) + { + if (VERSION.SDK_INT >= 28) + _cm.clearPrimaryClip(); + else + _cm.setText(""); + } + if (_listener != null) + _listener.on_clipboard_history_change(); } /** Add clipboard entries to the history, skipping consecutive duplicates and empty strings. */ - public void add_clip(String clip) + public synchronized void add_clip(String clip) { if (!Config.globalConfig().clipboard_history_enabled) return; @@ -120,7 +123,7 @@ public final class ClipboardHistoryService _listener.on_clipboard_history_change(); } - public void clear_history() + public synchronized void clear_history() { _history.clear(); if (_listener != null) @@ -137,7 +140,9 @@ public final class ClipboardHistoryService /** Add what is currently in the system clipboard into the history. */ void add_current_clip() { - ClipData clip = _cm.getPrimaryClip(); + ClipData clip = null; + // getPrimaryClip might throw when the keyboard is disconnected. + try { clip = _cm.getPrimaryClip(); } catch (Exception _e) {} if (clip == null) return; int count = clip.getItemCount(); -- cgit v1.2.3