Differential D12229 Diff 32204 src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java
Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java
Show First 20 Lines • Show All 63 Lines • ▼ Show 20 Line(s) | 34 | public class ClipboardListener { | |||
---|---|---|---|---|---|
64 | 64 | | |||
65 | ClipboardListener(final Context ctx) { | 65 | ClipboardListener(final Context ctx) { | ||
66 | context = ctx; | 66 | context = ctx; | ||
67 | 67 | | |||
68 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | 68 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | ||
69 | return; | 69 | return; | ||
70 | } | 70 | } | ||
71 | 71 | | |||
72 | new Handler(Looper.getMainLooper()).post(new Runnable() { | 72 | new Handler(Looper.getMainLooper()).post(() -> { | ||
73 | @Override | | |||
74 | public void run() { | | |||
75 | cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | 73 | cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | ||
76 | listener = new ClipboardManager.OnPrimaryClipChangedListener() { | 74 | listener = () -> { | ||
77 | @Override | | |||
78 | public void onPrimaryClipChanged() { | | |||
79 | try { | 75 | try { | ||
80 | 76 | | |||
81 | ClipData.Item item = cm.getPrimaryClip().getItemAt(0); | 77 | ClipData.Item item = cm.getPrimaryClip().getItemAt(0); | ||
82 | String content = item.coerceToText(context).toString(); | 78 | String content = item.coerceToText(context).toString(); | ||
83 | 79 | | |||
84 | if (content.equals(currentContent)) { | 80 | if (content.equals(currentContent)) { | ||
85 | return; | 81 | return; | ||
86 | } | 82 | } | ||
87 | 83 | | |||
88 | currentContent = content; | 84 | currentContent = content; | ||
89 | 85 | | |||
90 | for (ClipboardObserver observer : observers) { | 86 | for (ClipboardObserver observer : observers) { | ||
91 | observer.clipboardChanged(content); | 87 | observer.clipboardChanged(content); | ||
92 | } | 88 | } | ||
93 | 89 | | |||
94 | } catch (Exception e) { | 90 | } catch (Exception e) { | ||
95 | //Probably clipboard was not text | 91 | //Probably clipboard was not text | ||
96 | } | 92 | } | ||
97 | } | | |||
98 | }; | 93 | }; | ||
99 | cm.addPrimaryClipChangedListener(listener); | 94 | cm.addPrimaryClipChangedListener(listener); | ||
100 | } | | |||
101 | }); | 95 | }); | ||
102 | } | 96 | } | ||
103 | 97 | | |||
104 | @SuppressWarnings("deprecation") | 98 | @SuppressWarnings("deprecation") | ||
105 | public void setText(String text) { | 99 | public void setText(String text) { | ||
106 | currentContent = text; | 100 | currentContent = text; | ||
107 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | 101 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | ||
108 | android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | 102 | android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | ||
109 | clipboard.setText(text); | 103 | clipboard.setText(text); | ||
110 | } else { | 104 | } else { | ||
111 | cm.setText(text); | 105 | cm.setText(text); | ||
112 | } | 106 | } | ||
113 | } | 107 | } | ||
114 | 108 | | |||
115 | } | 109 | } |