clipboard: Pasting

The paste button send the content of the pinned clip to the editor the
same way as a string key.
This commit is contained in:
Jules Aguillon 2024-06-16 23:07:58 +02:00
parent 59d2d05c50
commit 700b7e9d19
5 changed files with 38 additions and 3 deletions

View File

@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/clipboard_pin_text" style="@style/clipboardEntry" android:maxLines="3"/> <TextView android:id="@+id/clipboard_pin_text" style="@style/clipboardEntry" android:maxLines="3"/>
<LinearLayout style="@style/clipboardEntryButtons"> <LinearLayout style="@style/clipboardEntryButtons">
<View android:id="@+id/clipboard_pin_paste" style="@style/clipboardEntryButton" android:background="@drawable/ic_clipboard_paste"/>
<View android:id="@+id/clipboard_pin_remove" style="@style/clipboardEntryButton" android:background="@drawable/ic_clipboard_unsafe"/> <View android:id="@+id/clipboard_pin_remove" style="@style/clipboardEntryButton" android:background="@drawable/ic_clipboard_unsafe"/>
</LinearLayout> </LinearLayout>
</LinearLayout> </LinearLayout>

View File

@ -11,9 +11,10 @@ import java.util.List;
public final class ClipboardHistoryService public final class ClipboardHistoryService
{ {
/** Start the service on startup and start listening to clipboard changes. */ /** Start the service on startup and start listening to clipboard changes. */
public static void on_startup(Context ctx) public static void on_startup(Context ctx, ClipboardPasteCallback cb)
{ {
get_service(ctx); get_service(ctx);
_paste_callback = cb;
} }
/** Start the service if it hasn't been started before. Returns [null] if the /** Start the service if it hasn't been started before. Returns [null] if the
@ -40,6 +41,13 @@ public final class ClipboardHistoryService
_service.clear_history(); _service.clear_history();
} }
/** Send the given string to the editor. */
public static void paste(String clip)
{
if (_paste_callback != null)
_paste_callback.paste_from_clipboard_pane(clip);
}
/** The maximum size limits the amount of user data stored in memory but also /** 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 gives a sense to the user that the history is not persisted and can be
forgotten as soon as the app stops. */ forgotten as soon as the app stops. */
@ -48,6 +56,7 @@ public final class ClipboardHistoryService
public static final long HISTORY_TTL_MS = 5 * 60 * 1000; public static final long HISTORY_TTL_MS = 5 * 60 * 1000;
static ClipboardHistoryService _service = null; static ClipboardHistoryService _service = null;
static ClipboardPasteCallback _paste_callback = null;
ClipboardManager _cm; ClipboardManager _cm;
List<HistoryEntry> _history; List<HistoryEntry> _history;
@ -163,4 +172,9 @@ public final class ClipboardHistoryService
expiry_timestamp = System.currentTimeMillis() + HISTORY_TTL_MS; expiry_timestamp = System.currentTimeMillis() + HISTORY_TTL_MS;
} }
} }
public interface ClipboardPasteCallback
{
public void paste_from_clipboard_pane(String content);
}
} }

View File

@ -55,6 +55,12 @@ public final class ClipboardPinView extends NonScrollListView
invalidate(); invalidate();
} }
/** Send the specified entry to the editor. */
public void paste_entry(int pos)
{
ClipboardHistoryService.paste(_entries.get(pos));
}
void persist() { save_to_prefs(_persist_store, _entries); } void persist() { save_to_prefs(_persist_store, _entries); }
static void load_from_prefs(SharedPreferences store, List<String> dst) static void load_from_prefs(SharedPreferences store, List<String> dst)
@ -99,6 +105,12 @@ public final class ClipboardPinView extends NonScrollListView
v = View.inflate(getContext(), R.layout.clipboard_pin_entry, null); v = View.inflate(getContext(), R.layout.clipboard_pin_entry, null);
((TextView)v.findViewById(R.id.clipboard_pin_text)) ((TextView)v.findViewById(R.id.clipboard_pin_text))
.setText(_entries.get(pos)); .setText(_entries.get(pos));
v.findViewById(R.id.clipboard_pin_paste).setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v) { paste_entry(pos); }
});
v.findViewById(R.id.clipboard_pin_remove).setOnClickListener( v.findViewById(R.id.clipboard_pin_remove).setOnClickListener(
new View.OnClickListener() new View.OnClickListener()
{ {

View File

@ -10,7 +10,9 @@ import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection; import android.view.inputmethod.InputConnection;
import java.util.Iterator; import java.util.Iterator;
public final class KeyEventHandler implements Config.IKeyEventHandler public final class KeyEventHandler
implements Config.IKeyEventHandler,
ClipboardHistoryService.ClipboardPasteCallback
{ {
IReceiver _recv; IReceiver _recv;
Autocapitalisation _autocap; Autocapitalisation _autocap;
@ -105,6 +107,12 @@ public final class KeyEventHandler implements Config.IKeyEventHandler
update_meta_state(mods); update_meta_state(mods);
} }
@Override
public void paste_from_clipboard_pane(String content)
{
send_text(content);
}
/** Update [_mods] to be consistent with the [mods], sending key events if /** Update [_mods] to be consistent with the [mods], sending key events if
needed. */ needed. */
void update_meta_state(Pointers.Modifiers mods) void update_meta_state(Pointers.Modifiers mods)

View File

@ -114,7 +114,7 @@ public class Keyboard2 extends InputMethodService
_keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard); _keyboardView = (Keyboard2View)inflate_view(R.layout.keyboard);
_keyboardView.reset(); _keyboardView.reset();
Logs.set_debug_logs(getResources().getBoolean(R.bool.debug_logs)); Logs.set_debug_logs(getResources().getBoolean(R.bool.debug_logs));
ClipboardHistoryService.on_startup(this); ClipboardHistoryService.on_startup(this, _keyeventhandler);
} }
private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm) private List<InputMethodSubtype> getEnabledSubtypes(InputMethodManager imm)