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 Log.d (TAG, ("onPrimaryClipChanged: "
75 + monitoredClipboardChangedCount
76 + " " + clipboardChangedCount));
77
78
79
80
81 monitoredClipboardChangedCount++;
82
83 if (monitoredClipboardChangedCount > clipboardChangedCount)
84 {
85 ownsClipboard = false;
86
87
88 monitoredClipboardChangedCount = 0;
89 clipboardChangedCount = 0;
90 }
91 }
92
93
94
95
96 @Override
97 public synchronized void
98 setClipboard (byte[] bytes)
99 {
100 ClipData data;
101 String string;
102
103 try
104 {
105 string = new String (bytes, "UTF-8");
106 data = ClipData.newPlainText ("Emacs", string);
107 manager.setPrimaryClip (data);
108 ownsClipboard = true;
109
110
111
112
113 ++clipboardChangedCount;
114 }
115 catch (UnsupportedEncodingException exception)
116 {
117 Log.w (TAG, "setClipboard: " + exception);
118 }
119 }
120
121
122
123
124
125 @Override
126 public synchronized int
127 ownsClipboard ()
128 {
129 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
130 return -1;
131
132 return ownsClipboard ? 1 : 0;
133 }
134
135
136
137 @Override
138 public boolean
139 clipboardExists ()
140 {
141 return manager.hasPrimaryClip ();
142 }
143
144
145
146
147 @Override
148 public byte[]
149 getClipboard ()
150 {
151 ClipData clip;
152 CharSequence text;
153 Context context;
154
155 clip = manager.getPrimaryClip ();
156
157 if (clip == null || clip.getItemCount () < 1)
158 return null;
159
160 context = EmacsService.SERVICE;
161
162 try
163 {
164 text = clip.getItemAt (0).coerceToText (context);
165 return text.toString ().getBytes ("UTF-8");
166 }
167 catch (UnsupportedEncodingException exception)
168 {
169 Log.w (TAG, "getClipboard: " + exception);
170 }
171
172 return null;
173 }
174
175
176
177
178 @Override
179 public byte[][]
180 getClipboardTargets ()
181 {
182 ClipData clip;
183 ClipDescription description;
184 byte[][] typeArray;
185 int i;
186
187
188
189 clip = manager.getPrimaryClip ();
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 Log.d (TAG, "getClipboardData: "+ mimeType);
241
242
243
244
245 data = manager.getPrimaryClip ();
246
247 if (data.getItemCount () < 1)
248 return null;
249
250 try
251 {
252 uri = data.getItemAt (0).getUri ();
253
254 if (uri == null)
255 return null;
256
257 Log.d (TAG, "getClipboardData: "+ uri);
258
259
260 assetFd = resolver.openTypedAssetFileDescriptor (uri, mimeType,
261 null);
262
263
264 fd = assetFd.getParcelFileDescriptor ().getFd ();
265 fd = EmacsNative.dup (fd);
266
267
268 value = new long[] { fd, assetFd.getStartOffset (),
269 assetFd.getLength (), };
270
271
272 assetFd.close ();
273
274 Log.d (TAG, "getClipboardData: "+ value);
275 }
276 catch (FileNotFoundException e)
277 {
278 return null;
279 }
280 catch (IOException e)
281 {
282 return null;
283 }
284
285
286
287
288 return fd != -1 ? value : null;
289 }
290 };