Differential D10905 Diff 28488 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 All 30 Lines | |||||
31 | import java.util.HashSet; | 31 | import java.util.HashSet; | ||
32 | 32 | | |||
33 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) | 33 | @TargetApi(Build.VERSION_CODES.HONEYCOMB) | ||
34 | public class ClipboardListener { | 34 | public class ClipboardListener { | ||
35 | 35 | | |||
36 | public interface ClipboardObserver { | 36 | public interface ClipboardObserver { | ||
37 | void clipboardChanged(String content); | 37 | void clipboardChanged(String content); | ||
38 | } | 38 | } | ||
39 | | ||||
39 | private HashSet<ClipboardObserver> observers = new HashSet<>(); | 40 | private HashSet<ClipboardObserver> observers = new HashSet<>(); | ||
40 | 41 | | |||
41 | private final Context context; | 42 | private final Context context; | ||
42 | private String currentContent; | 43 | private String currentContent; | ||
43 | 44 | | |||
44 | private ClipboardManager cm = null; | 45 | private ClipboardManager cm = null; | ||
45 | private ClipboardManager.OnPrimaryClipChangedListener listener; | 46 | private ClipboardManager.OnPrimaryClipChangedListener listener; | ||
46 | 47 | | |||
47 | private static ClipboardListener _instance = null; | 48 | private static ClipboardListener _instance = null; | ||
49 | | ||||
48 | public static ClipboardListener instance(Context context) { | 50 | public static ClipboardListener instance(Context context) { | ||
49 | if (_instance == null) { | 51 | if (_instance == null) { | ||
50 | _instance = new ClipboardListener(context); | 52 | _instance = new ClipboardListener(context); | ||
51 | } | 53 | } | ||
52 | return _instance; | 54 | return _instance; | ||
53 | } | 55 | } | ||
54 | 56 | | |||
55 | public void registerObserver(ClipboardObserver observer) { | 57 | public void registerObserver(ClipboardObserver observer) { | ||
56 | observers.add(observer); | 58 | observers.add(observer); | ||
57 | } | 59 | } | ||
58 | 60 | | |||
59 | public void removeObserver(ClipboardObserver observer) { | 61 | public void removeObserver(ClipboardObserver observer) { | ||
60 | observers.remove(observer); | 62 | observers.remove(observer); | ||
61 | } | 63 | } | ||
62 | 64 | | |||
63 | ClipboardListener(final Context ctx) { | 65 | ClipboardListener(final Context ctx) { | ||
64 | context = ctx; | 66 | context = ctx; | ||
65 | 67 | | |||
66 | if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | 68 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | ||
67 | return; | 69 | return; | ||
68 | } | 70 | } | ||
69 | 71 | | |||
70 | new Handler(Looper.getMainLooper()).post(new Runnable() { | 72 | new Handler(Looper.getMainLooper()).post(new Runnable() { | ||
71 | @Override | 73 | @Override | ||
72 | public void run() { | 74 | public void run() { | ||
73 | cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | 75 | cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | ||
74 | listener = new ClipboardManager.OnPrimaryClipChangedListener() { | 76 | listener = new ClipboardManager.OnPrimaryClipChangedListener() { | ||
Show All 22 Lines | |||||
97 | cm.addPrimaryClipChangedListener(listener); | 99 | cm.addPrimaryClipChangedListener(listener); | ||
98 | } | 100 | } | ||
99 | }); | 101 | }); | ||
100 | } | 102 | } | ||
101 | 103 | | |||
102 | @SuppressWarnings("deprecation") | 104 | @SuppressWarnings("deprecation") | ||
103 | public void setText(String text) { | 105 | public void setText(String text) { | ||
104 | currentContent = text; | 106 | currentContent = text; | ||
105 | if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | 107 | if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { | ||
106 | android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | 108 | android.text.ClipboardManager clipboard = (android.text.ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); | ||
107 | clipboard.setText(text); | 109 | clipboard.setText(text); | ||
108 | } | 110 | } else { | ||
109 | else | | |||
110 | { | | |||
111 | cm.setText(text); | 111 | cm.setText(text); | ||
112 | } | 112 | } | ||
113 | } | 113 | } | ||
114 | 114 | | |||
115 | } | 115 | } |