1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
|
/* Communication module for Android terminals. -*- c-file-style: "GNU" -*-
Copyright (C) 2023-2024 Free Software Foundation, Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
package org.gnu.emacs;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.CorrectionInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputContentInfo;
import android.view.inputmethod.SurroundingText;
import android.view.inputmethod.TextAttribute;
import android.view.inputmethod.TextSnapshot;
import android.util.Log;
/* Android input methods, take number six. See textconv.c for more
details; this is more-or-less a thin wrapper around that file. */
public final class EmacsInputConnection implements InputConnection
{
private static final String TAG = "EmacsInputConnection";
/* View associated with this input connection. */
private EmacsView view;
/* The handle ID associated with that view's window. */
private long windowHandle;
/* Number of batch edits currently underway. Used to avoid
synchronizing with the Emacs thread after each
`endBatchEdit'. */
private int batchEditCount;
/* Whether or not to synchronize and call `updateIC' with the
selection position after committing text.
This helps with on screen keyboard programs found in some vendor
versions of Android, which rely on immediate updates to the point
position after text is committed in order to place the cursor
within that text. */
private static boolean syncAfterCommit;
/* Whether or not to return empty text with the offset set to zero
if a request arrives that has no flags set and has requested no
characters at all.
This is necessary with on screen keyboard programs found in some
vendor versions of Android which don't rely on the documented
meaning of `ExtractedText.startOffset', and instead take the
selection offset inside at face value. */
private static boolean extractAbsoluteOffsets;
static
{
if (Build.MANUFACTURER.equalsIgnoreCase ("Huawei")
|| Build.MANUFACTURER.equalsIgnoreCase ("Honor"))
extractAbsoluteOffsets = syncAfterCommit = true;
/* The Samsung and Vivo keyboards take `selectionStart' at face
value if some text is returned, and also searches for words
solely within that text. However, when no text is returned, it
falls back to getTextAfterCursor and getTextBeforeCursor. */
if (Build.MANUFACTURER.equalsIgnoreCase ("Samsung")
|| Build.MANUFACTURER.equalsIgnoreCase ("Vivo"))
extractAbsoluteOffsets = true;
};
public
EmacsInputConnection (EmacsView view)
{
this.view = view;
this.windowHandle = view.window.handle;
}
/* The functions below are called by input methods whenever they
need to perform an edit. */
@Override
public boolean
beginBatchEdit ()
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "beginBatchEdit");
EmacsNative.beginBatchEdit (windowHandle);
/* Keep a record of the number of outstanding batch edits here as
well. */
batchEditCount++;
return true;
}
@Override
public boolean
endBatchEdit ()
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "endBatchEdit");
EmacsNative.endBatchEdit (windowHandle);
/* Subtract one from the UI thread record of the number of batch
edits currently under way. */
if (batchEditCount > 0)
batchEditCount -= 1;
return batchEditCount > 0;
}
public boolean
commitCompletion (CompletionInfo info)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "commitCompletion: " + info);
EmacsNative.commitCompletion (windowHandle,
info.getText ().toString (),
info.getPosition ());
return true;
}
@Override
public boolean
commitCorrection (CorrectionInfo info)
{
/* The input method calls this function not to commit text, but to
indicate that a subsequent edit will consist of a correction.
Emacs has no use for this information.
Of course this completely contradicts the provided
documentation, but this is how Android actually behaves. */
return false;
}
@Override
public boolean
commitText (CharSequence text, int newCursorPosition)
{
int[] selection;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "commitText: " + text + " " + newCursorPosition);
EmacsNative.commitText (windowHandle, text.toString (),
newCursorPosition);
if (syncAfterCommit)
{
/* Synchronize with the Emacs thread, obtain the new
selection, and report it immediately. */
selection = EmacsNative.getSelection (windowHandle);
if (EmacsService.DEBUG_IC && selection != null)
Log.d (TAG, "commitText: new selection is " + selection[0]
+ ", by " + selection[1]);
if (selection != null)
/* N.B. that the composing region is removed after text is
committed. */
view.imManager.updateSelection (view, selection[0],
selection[1], -1, -1);
}
return true;
}
@Override
public boolean
commitText (CharSequence text, int newCursorPosition,
TextAttribute textAttribute)
{
return commitText (text, newCursorPosition);
}
@Override
public boolean
deleteSurroundingText (int leftLength, int rightLength)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, ("deleteSurroundingText: "
+ leftLength + " " + rightLength));
EmacsNative.deleteSurroundingText (windowHandle, leftLength,
rightLength);
return true;
}
@Override
public boolean
deleteSurroundingTextInCodePoints (int leftLength, int rightLength)
{
/* Emacs returns characters which cannot be represented in a Java
`char' as NULL characters, so code points always reflect
characters themselves. */
return deleteSurroundingText (leftLength, rightLength);
}
@Override
public boolean
finishComposingText ()
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "finishComposingText");
EmacsNative.finishComposingText (windowHandle);
return true;
}
@Override
public String
getSelectedText (int flags)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getSelectedText: " + flags);
return EmacsNative.getSelectedText (windowHandle, flags);
}
@Override
public String
getTextAfterCursor (int length, int flags)
{
String string;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getTextAfterCursor: " + length + " " + flags);
string = EmacsNative.getTextAfterCursor (windowHandle, length,
flags);
if (EmacsService.DEBUG_IC)
Log.d (TAG, " --> " + string);
return string;
}
@Override
public String
getTextBeforeCursor (int length, int flags)
{
String string;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getTextBeforeCursor: " + length + " " + flags);
string = EmacsNative.getTextBeforeCursor (windowHandle, length,
flags);
if (EmacsService.DEBUG_IC)
Log.d (TAG, " --> " + string);
return string;
}
@Override
public boolean
setComposingText (CharSequence text, int newCursorPosition)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, ("setComposingText: "
+ text + " ## " + newCursorPosition));
EmacsNative.setComposingText (windowHandle, text.toString (),
newCursorPosition);
return true;
}
@Override
public boolean
setComposingText (CharSequence text, int newCursorPosition,
TextAttribute textAttribute)
{
return setComposingText (text, newCursorPosition);
}
@Override
public boolean
setComposingRegion (int start, int end)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "setComposingRegion: " + start + " " + end);
EmacsNative.setComposingRegion (windowHandle, start, end);
return true;
}
@Override
public boolean
setComposingRegion (int start, int end, TextAttribute textAttribute)
{
return setComposingRegion (start, end);
}
@Override
public boolean
performEditorAction (int editorAction)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "performEditorAction: " + editorAction);
EmacsNative.performEditorAction (windowHandle, editorAction);
return true;
}
@Override
public boolean
performContextMenuAction (int contextMenuAction)
{
int action;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "performContextMenuAction: " + contextMenuAction);
/* Translate the action in Java code. That way, a great deal of
JNI boilerplate can be avoided. */
switch (contextMenuAction)
{
case android.R.id.selectAll:
action = 0;
break;
case android.R.id.startSelectingText:
action = 1;
break;
case android.R.id.stopSelectingText:
action = 2;
break;
case android.R.id.cut:
action = 3;
break;
case android.R.id.copy:
action = 4;
break;
case android.R.id.paste:
action = 5;
break;
default:
return true;
}
EmacsNative.performContextMenuAction (windowHandle, action);
return true;
}
@Override
public ExtractedText
getExtractedText (ExtractedTextRequest request, int flags)
{
ExtractedText text;
int[] selection;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getExtractedText: " + request.hintMaxChars + ", "
+ request.hintMaxLines + " " + flags);
/* If a request arrives with hintMaxChars, hintMaxLines and flags
set to 0, and the system is known to be buggy, return an empty
extracted text object with the absolute selection positions. */
if (extractAbsoluteOffsets
&& request.hintMaxChars == 0
&& request.hintMaxLines == 0
&& flags == 0)
{
/* Obtain the selection. */
selection = EmacsNative.getSelection (windowHandle);
if (selection == null)
return null;
/* Create the workaround extracted text. */
text = new ExtractedText ();
text.partialStartOffset = -1;
text.partialEndOffset = -1;
text.text = "";
text.selectionStart = selection[0];
text.selectionEnd = selection[1];
}
else
text = EmacsNative.getExtractedText (windowHandle, request,
flags);
if (text == null)
{
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getExtractedText: text is NULL");
return null;
}
if (EmacsService.DEBUG_IC)
Log.d (TAG, "getExtractedText: " + text.text + " @"
+ text.startOffset + ":" + text.selectionStart
+ ", " + text.selectionEnd);
return text;
}
@Override
public boolean
setSelection (int start, int end)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "setSelection: " + start + " " + end);
EmacsNative.setSelection (windowHandle, start, end);
return true;
}
@Override
/* ACTION_MULTIPLE is apparently obsolete. */
@SuppressWarnings ("deprecation")
public boolean
sendKeyEvent (KeyEvent key)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "sendKeyEvent: " + key);
/* Use the standard API if possible. */
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
view.imManager.dispatchKeyEventFromInputMethod (view, key);
else
{
/* Fall back to dispatching the event manually if not. */
switch (key.getAction ())
{
case KeyEvent.ACTION_DOWN:
view.onKeyDown (key.getKeyCode (), key);
break;
case KeyEvent.ACTION_UP:
view.onKeyUp (key.getKeyCode (), key);
break;
case KeyEvent.ACTION_MULTIPLE:
view.onKeyMultiple (key.getKeyCode (),
key.getRepeatCount (),
key);
break;
}
}
return true;
}
@Override
public boolean
requestCursorUpdates (int cursorUpdateMode)
{
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return false;
if (EmacsService.DEBUG_IC)
Log.d (TAG, "requestCursorUpdates: " + cursorUpdateMode);
EmacsNative.requestCursorUpdates (windowHandle, cursorUpdateMode);
return true;
}
@Override
public boolean
requestCursorUpdates (int cursorUpdateMode, int filter)
{
if (filter != 0)
return false;
return requestCursorUpdates (cursorUpdateMode);
}
@Override
public SurroundingText
getSurroundingText (int beforeLength, int afterLength,
int flags)
{
SurroundingText text;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
if (EmacsService.DEBUG_IC)
Log.d (TAG, ("getSurroundingText: " + beforeLength + ", "
+ afterLength));
text = EmacsNative.getSurroundingText (windowHandle, beforeLength,
afterLength, flags);
if (EmacsService.DEBUG_IC && text != null)
Log.d (TAG, ("getSurroundingText: "
+ text.getSelectionStart ()
+ ","
+ text.getSelectionEnd ()
+ "+"
+ text.getOffset ()
+ ": "
+ text.getText ()));
return text;
}
@Override
public TextSnapshot
takeSnapshot ()
{
TextSnapshot snapshot;
/* Return if the input connection is out of date. */
if (view.icSerial < view.icGeneration)
return null;
snapshot = EmacsNative.takeSnapshot (windowHandle);
if (EmacsService.DEBUG_IC)
Log.d (TAG, ("takeSnapshot: "
+ snapshot.getSurroundingText ().getText ()
+ " @ " + snapshot.getCompositionEnd ()
+ ", " + snapshot.getCompositionStart ()));
return snapshot;
}
@Override
public void
closeConnection ()
{
batchEditCount = 0;
}
@Override
public boolean
replaceText (int start, int end, CharSequence text,
int newCursorPosition, TextAttribute attributes)
{
if (EmacsService.DEBUG_IC)
Log.d (TAG, ("replaceText: " + text + ":: " + start + ","
+ end + "," + newCursorPosition));
EmacsNative.replaceText (windowHandle, start, end,
text.toString (), newCursorPosition,
attributes);
return true;
}
public void
reset ()
{
batchEditCount = 0;
}
/* Override functions which are not implemented. */
@Override
public Handler
getHandler ()
{
return null;
}
@Override
public boolean
commitContent (InputContentInfo inputContentInfo, int flags,
Bundle opts)
{
return false;
}
@Override
public boolean
setImeConsumesInput (boolean imeConsumesInput)
{
return false;
}
@Override
public boolean
clearMetaKeyStates (int states)
{
return false;
}
@Override
public boolean
reportFullscreenMode (boolean enabled)
{
return false;
}
@Override
public boolean
performSpellCheck ()
{
return false;
}
@Override
public boolean
performPrivateCommand (String action, Bundle data)
{
return false;
}
@Override
public int
getCursorCapsMode (int reqModes)
{
return 0;
}
}
|