This source file includes following definitions.
- onPrimaryClipChanged
- setClipboard
- ownsClipboard
- clipboardExists
- getClipboard
- getClipboardTargets
- getClipboardData
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.gnu.emacs;
21
22 import android.content.ClipboardManager;
23 import android.content.Context;
24 import android.content.ContentResolver;
25 import android.content.ClipData;
26 import android.content.ClipDescription;
27
28 import android.content.res.AssetFileDescriptor;
29
30 import android.net.Uri;
31
32 import android.util.Log;
33
34 import android.os.Build;
35
36 import java.io.FileNotFoundException;
37 import java.io.IOException;
38 import java.io.UnsupportedEncodingException;
39
40
41
42
43 public final class EmacsSdk11Clipboard extends EmacsClipboard
44 implements ClipboardManager.OnPrimaryClipChangedListener
45 {
46 private static final String TAG = "EmacsSdk11Clipboard";
47 private ClipboardManager manager;
48 private boolean ownsClipboard;
49 private int clipboardChangedCount;
50 private int monitoredClipboardChangedCount;
51 private ContentResolver resolver;
52
53 public
54 EmacsSdk11Clipboard ()
55 {
56 manager = EmacsService.SERVICE.getClipboardManager ();
57
58
59
60
61 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
62 manager.addPrimaryClipChangedListener (this);
63
64
65
66
67 resolver = EmacsService.SERVICE.getContentResolver ();
68 }
69
70 @Override
71 public synchronized void
72 onPrimaryClipChanged ()
73 {
74
75
76
77 monitoredClipboardChangedCount++;
78
79 if (monitoredClipboardChangedCount > clipboardChangedCount)
80 {
81 ownsClipboard = false;
82
83
84 monitoredClipboardChangedCount = 0;
85 clipboardChangedCount = 0;
86 }
87 }
88
89
90
91
92 @Override
93 public synchronized void
94 setClipboard (byte[] bytes)
95 {
96 ClipData data;
97 String string;
98
99 try
100 {
101 string = new String (bytes, "UTF-8");
102 data = ClipData.newPlainText ("Emacs", string);
103 manager.setPrimaryClip (data);
104 ownsClipboard = true;
105
106
107
108
109 ++clipboardChangedCount;
110 }
111 catch (UnsupportedEncodingException exception)
112 {
113 Log.w (TAG, "setClipboard: " + exception);
114 }
115 }
116
117
118
119
120
121 @Override
122 public synchronized int
123 ownsClipboard ()
124 {
125 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
126 return -1;
127
128 return ownsClipboard ? 1 : 0;
129 }
130
131
132
133 @Override
134 public boolean
135 clipboardExists ()
136 {
137 return manager.hasPrimaryClip ();
138 }
139
140
141
142
143 @Override
144 public byte[]
145 getClipboard ()
146 {
147 ClipData clip;
148 CharSequence text;
149 Context context;
150
151 clip = manager.getPrimaryClip ();
152
153 if (clip == null || clip.getItemCount () < 1)
154 return null;
155
156 context = EmacsService.SERVICE;
157
158 try
159 {
160 text = clip.getItemAt (0).coerceToText (context);
161 return text.toString ().getBytes ("UTF-8");
162 }
163 catch (UnsupportedEncodingException exception)
164 {
165 Log.w (TAG, "getClipboard: " + exception);
166 }
167
168 return null;
169 }
170
171
172
173
174 @Override
175 public byte[][]
176 getClipboardTargets ()
177 {
178 ClipData clip;
179 ClipDescription description;
180 byte[][] typeArray;
181 int i;
182
183
184
185 clip = manager.getPrimaryClip ();
186
187 if (clip == null)
188 return null;
189
190 description = clip.getDescription ();
191 i = description.getMimeTypeCount ();
192 typeArray = new byte[i][i];
193
194 try
195 {
196 for (i = 0; i < description.getMimeTypeCount (); ++i)
197 typeArray[i] = description.getMimeType (i).getBytes ("UTF-8");
198 }
199 catch (UnsupportedEncodingException exception)
200 {
201 return null;
202 }
203
204 return typeArray;
205 }
206
207
208
209
210
211
212
213
214
215
216
217
218
219 @Override
220 public long[]
221 getClipboardData (byte[] target)
222 {
223 ClipData data;
224 String mimeType;
225 int fd;
226 AssetFileDescriptor assetFd;
227 Uri uri;
228 long[] value;
229
230
231 try
232 {
233 mimeType = new String (target, "UTF-8");
234 }
235 catch (UnsupportedEncodingException exception)
236 {
237 return null;
238 }
239
240
241
242
243 data = manager.getPrimaryClip ();
244
245 if (data == null || data.getItemCount () < 1)
246 return null;
247
248 try
249 {
250 uri = data.getItemAt (0).getUri ();
251
252 if (uri == null)
253 return null;
254
255
256 assetFd = resolver.openTypedAssetFileDescriptor (uri, mimeType,
257 null);
258
259
260 fd = assetFd.getParcelFileDescriptor ().getFd ();
261 fd = EmacsNative.dup (fd);
262
263
264 value = new long[] { fd, assetFd.getStartOffset (),
265 assetFd.getLength (), };
266
267
268 assetFd.close ();
269 }
270 catch (FileNotFoundException e)
271 {
272 return null;
273 }
274 catch (IOException e)
275 {
276 return null;
277 }
278
279
280
281
282 return fd != -1 ? value : null;
283 }
284 };