aboutsummaryrefslogtreecommitdiffstats
path: root/java/org/gnu/emacs/EmacsInputConnection.java
blob: 54c98d950aa498bbb5ba719809db637734d6f05e (plain) (blame)
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
/* Communication module for Android terminals.  -*- c-file-style: "GNU" -*-

Copyright (C) 2023 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.view.inputmethod.BaseInputConnection;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.TextSnapshot;
import android.view.KeyEvent;

import android.os.Build;

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 extends BaseInputConnection
{
  private static final String TAG = "EmacsInputConnection";
  private EmacsView view;
  private short windowHandle;

  /* 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 commited 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 keyboard takes `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"))
      extractAbsoluteOffsets = true;
  };

  public
  EmacsInputConnection (EmacsView view)
  {
    super (view, true);

    this.view = view;
    this.windowHandle = view.window.handle;
  }

  @Override
  public boolean
  beginBatchEdit ()
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "beginBatchEdit");

    EmacsNative.beginBatchEdit (windowHandle);
    return true;
  }

  @Override
  public boolean
  endBatchEdit ()
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "endBatchEdit");

    EmacsNative.endBatchEdit (windowHandle);
    return true;
  }

  @Override
  public boolean
  commitCompletion (CompletionInfo info)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "commitCompletion: " + info);

    EmacsNative.commitCompletion (windowHandle,
				  info.getText ().toString (),
				  info.getPosition ());
    return true;
  }

  @Override
  public boolean
  commitText (CharSequence text, int newCursorPosition)
  {
    int[] selection;

    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
  deleteSurroundingText (int leftLength, int rightLength)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, ("deleteSurroundingText: "
		   + leftLength + " " + rightLength));

    EmacsNative.deleteSurroundingText (windowHandle, leftLength,
				       rightLength);
    return true;
  }

  @Override
  public boolean
  finishComposingText ()
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "finishComposingText");

    EmacsNative.finishComposingText (windowHandle);
    return true;
  }

  @Override
  public String
  getSelectedText (int flags)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "getSelectedText: " + flags);

    return EmacsNative.getSelectedText (windowHandle, flags);
  }

  @Override
  public String
  getTextAfterCursor (int length, int flags)
  {
    String string;

    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;

    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)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, ("setComposingText: "
		   + text + " ## " + newCursorPosition));

    EmacsNative.setComposingText (windowHandle, text.toString (),
				  newCursorPosition);
    return true;
  }

  @Override
  public boolean
  setComposingRegion (int start, int end)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "setComposingRegion: " + start + " " + end);

    EmacsNative.setComposingRegion (windowHandle, start, end);
    return true;
  }

  @Override
  public boolean
  performEditorAction (int editorAction)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "performEditorAction: " + editorAction);

    EmacsNative.performEditorAction (windowHandle, editorAction);
    return true;
  }

  @Override
  public ExtractedText
  getExtractedText (ExtractedTextRequest request, int flags)
  {
    ExtractedText text;
    int[] selection;

    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)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "setSelection: " + start + " " + end);

    EmacsNative.setSelection (windowHandle, start, end);
    return true;
  }

  @Override
  public boolean
  sendKeyEvent (KeyEvent key)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "sendKeyEvent: " + key);

    return super.sendKeyEvent (key);
  }

  @Override
  public boolean
  deleteSurroundingTextInCodePoints (int beforeLength, int afterLength)
  {
    /* This can be implemented the same way as
       deleteSurroundingText.  */
    return this.deleteSurroundingText (beforeLength, afterLength);
  }

  @Override
  public boolean
  requestCursorUpdates (int cursorUpdateMode)
  {
    if (EmacsService.DEBUG_IC)
      Log.d (TAG, "requestCursorUpdates: " + cursorUpdateMode);

    EmacsNative.requestCursorUpdates (windowHandle, cursorUpdateMode);
    return true;
  }


  /* Override functions which are not implemented.  */

  @Override
  public TextSnapshot
  takeSnapshot ()
  {
    Log.d (TAG, "takeSnapshot");
    return null;
  }
}