]> git.eshelyaron.com Git - emacs.git/commitdiff
Simplify management of Android handle IDs
authorPo Lu <luangruo@yahoo.com>
Sat, 4 May 2024 03:36:09 +0000 (11:36 +0800)
committerEshel Yaron <me@eshelyaron.com>
Mon, 6 May 2024 16:38:09 +0000 (18:38 +0200)
* java/org/gnu/emacs/EmacsCursor.java (EmacsCursor):

* java/org/gnu/emacs/EmacsGC.java (EmacsGC):

* java/org/gnu/emacs/EmacsHandleObject.java (EmacsHandleObject):
Remove HANDLE argument to constructor.

* java/org/gnu/emacs/EmacsPixmap.java (EmacsPixmap):

* java/org/gnu/emacs/EmacsWindow.java (EmacsWindow):

* java/org/gnu/emacs/EmacsInputConnection.java
(EmacsInputConnection) <windowHandle>: Change type to long.

* java/org/gnu/emacs/EmacsNative.java (EmacsNative)
(sendConfigureNotify, sendKeyPress, sendKeyRelease, sendFocusIn)
(sendFocusOut, sendWindowAction, sendEnterNotify)
(sendLeaveNotify, sendMotionNotify, sendButtonPress)
(sendButtonRelease, sendTouchDown, sendTouchUp, sendTouchMove)
(sendWheel, sendIconified, sendDeiconified, sendContextMenu)
(sendExpose, sendDndDrag, sendDndUri, sendDndText)
(beginBatchEdit, commitCompletion, endBatchEdit, commitText)
(deleteSurroundingText, finishComposingText, replaceText)
(getSelectedText, getTextAfterCursor, getTextBeforeCursor)
(setComposingText, setComposingRegion, setSelection)
(performEditorAction, performContextMenuAction, getExtractedText)
(requestSelectionUpdate, requestCursorUpdates, clearInputFlags)
(getSurroundingText, takeSnapshot, getSelection): Accept handles
as longs, rather than shorts.  All callers changed.

* java/org/gnu/emacs/EmacsService.java (queryTree): Return
handles as longs rather than shorts.
(viewGetSelection): Take long WINDOW, not short.

* src/android.c (struct android_emacs_handle): New structure.
(handle_class): New variable.
(android_init_emacs_service, android_init_emacs_pixmap)
(android_init_emacs_gc_class, android_init_emacs_cursor): Adjust
to match signature changes in constructors.
(android_init_emacs_handle): New function.
(initEmacs): Initialize the handle class, its fields and metods.
(sendConfigureNotify, sendKeyPress, sendKeyRelease, sendFocusIn)
(sendFocusOut, sendWindowAction, sendEnterNotify)
(sendLeaveNotify, sendMotionNotify, sendButtonPress)
(sendButtonRelease, sendTouchDown, sendTouchUp, sendTouchMove)
(sendWheel, sendIconified, sendDeiconified, sendContextMenu)
(sendExpose, sendDndDrag, sendDndUri, sendDndText): Update for
changes to handle type.
(android_alloc_id, android_resolve_handle)
(android_resolve_handle2): Remove functions; replace the second
with a macro that accepts one fewer argument.  All callers
changed.
(android_destroy_handle): Cease indexing the handle list for the
handle object.
(android_globalize_reference): New function.
(android_create_window, android_create_gc, android_create_pixmap)
(android_create_font_cursor): Call android_globalize_reference
to convert global references into handles.
(android_free_cursor, android_destroy_window): Cease verifying
the handle type.
(android_copy_area): Check destination object type rather than
handle entry.
(android_query_tree): Adjust for changes to return types.
(likely): Define __builtin_expect variant unconditionally.

* src/android.h (android_resolve_handle): New macro.

* src/androidgui.h (android_handle): Define to intptr_t.

* src/androidterm.c (deleteSurroundingText, finishComposingText)
(performEditorAction, performContextMenuAction, getExtractedText)
(getSelectedText, requestSelectionUpdate, requestCursorUpdates)
(clearInputFlags, getSurroundingText)
(android_get_surrounding_text_internal): Accept handles as
longs, not jshorts.

(cherry picked from commit 139931fefb4cde3132ca68c3caa6ee9c4889c1b6)

18 files changed:
java/org/gnu/emacs/EmacsActivity.java
java/org/gnu/emacs/EmacsContextMenu.java
java/org/gnu/emacs/EmacsCursor.java
java/org/gnu/emacs/EmacsDialog.java
java/org/gnu/emacs/EmacsGC.java
java/org/gnu/emacs/EmacsHandleObject.java
java/org/gnu/emacs/EmacsInputConnection.java
java/org/gnu/emacs/EmacsNative.java
java/org/gnu/emacs/EmacsPixmap.java
java/org/gnu/emacs/EmacsService.java
java/org/gnu/emacs/EmacsWindow.java
java/org/gnu/emacs/EmacsWindowManager.java
src/android.c
src/android.h
src/androidfont.c
src/androidgui.h
src/androidmenu.c
src/androidterm.c

index 28bb6e4c065367381d8d21b647755e349c83ce84..90be9a385cfe2c7a4b3ce092bbca71f243ebfcdc 100644 (file)
@@ -440,8 +440,7 @@ public class EmacsActivity extends Activity
     if (!EmacsContextMenu.itemAlreadySelected)
       {
        serial = EmacsContextMenu.lastMenuEventSerial;
-       EmacsNative.sendContextMenu ((short) 0, 0,
-                                    serial);
+       EmacsNative.sendContextMenu (0, 0, serial);
       }
 
     super.onContextMenuClosed (menu);
index 0f52d45455f7a72bd8149df8ef3ea6ab73e905bf..365a7ec40afdfd360b9fee8edf0cafc0c85d391f 100644 (file)
@@ -121,8 +121,7 @@ public final class EmacsContextMenu
        }
 
       /* Send a context menu event.  */
-      EmacsNative.sendContextMenu ((short) 0, itemID,
-                                  lastMenuEventSerial);
+      EmacsNative.sendContextMenu (0, itemID, lastMenuEventSerial);
 
       /* Say that an item has already been selected.  */
       itemAlreadySelected = true;
index 1049c03d7da39d7ac9ab6d38b49082aa9be96acd..e5f22c23cfca353d75fa0a51c797c272ed33d73d 100644 (file)
@@ -31,9 +31,9 @@ public final class EmacsCursor extends EmacsHandleObject
   public final PointerIcon icon;
 
   public
-  EmacsCursor (short handle, int glyph)
+  EmacsCursor (int glyph)
   {
-    super (handle);
+    super ();
 
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
       {
index 0d5b650f7d080a85d1f77d39c8c010b11d094447..31b2969197e4846175ddd5df2580f1a2a9d56fa2 100644 (file)
@@ -93,7 +93,7 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
     onClick (View view)
     {
       wasButtonClicked = true;
-      EmacsNative.sendContextMenu ((short) 0, id, menuEventSerial);
+      EmacsNative.sendContextMenu (0, id, menuEventSerial);
       dismissDialog.dismiss ();
     }
 
@@ -102,7 +102,7 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
     onClick (DialogInterface dialog, int which)
     {
       wasButtonClicked = true;
-      EmacsNative.sendContextMenu ((short) 0, id, menuEventSerial);
+      EmacsNative.sendContextMenu (0, id, menuEventSerial);
     }
   };
 
@@ -414,6 +414,6 @@ public final class EmacsDialog implements DialogInterface.OnDismissListener
     if (wasButtonClicked)
       return;
 
-    EmacsNative.sendContextMenu ((short) 0, 0, menuEventSerial);
+    EmacsNative.sendContextMenu (0, 0, menuEventSerial);
   }
 };
index bb11f76c80001dfe68868f90ec5b25cc1891d072..f956b230f8c984dc5dbe8eb330611708cf748ee3 100644 (file)
@@ -71,13 +71,13 @@ public final class EmacsGC extends EmacsHandleObject
   /* The following fields are only set on immutable GCs.  */
 
   public
-  EmacsGC (short handle)
+  EmacsGC ()
   {
     /* For historical reasons the C code has an extra layer of
        indirection above this GC handle.  struct android_gc is the GC
        used by Emacs code, while android_gcontext is the type of the
        handle.  */
-    super (handle);
+    super ();
 
     fill_style = GC_FILL_SOLID;
     function = GC_COPY;
index 8534f08519c5f6fce03a2c46dc0ff0670980b2a9..cbd579bac5bf1912463590d7d6a02ec6f3a9fe22 100644 (file)
@@ -33,14 +33,9 @@ public abstract class EmacsHandleObject
   /* Whether or not this handle has been destroyed.  */
   volatile boolean destroyed;
 
-  /* The handle associated with this object.  */
-  public short handle;
-
-  public
-  EmacsHandleObject (short handle)
-  {
-    this.handle = handle;
-  }
+  /* The handle associated with this object, set in
+     android_globalize_reference.  */
+  public long handle;
 
   public void
   destroyHandle () throws IllegalStateException
index 054eca66cf3260e340c5a128f916638a0bcb8cb2..5b409fa1f57f94407d373d89375e484ed90a7dd5 100644 (file)
@@ -48,7 +48,7 @@ public final class EmacsInputConnection implements InputConnection
   private EmacsView view;
 
   /* The handle ID associated with that view's window.  */
-  private short windowHandle;
+  private long windowHandle;
 
   /* Number of batch edits currently underway.  Used to avoid
      synchronizing with the Emacs thread after each
index acf9e4b204b38d43c1ec7499768e0b6bc80a7100..b2764edad10cc803655496079ddb6d2c98a05066 100644 (file)
@@ -108,92 +108,92 @@ public final class EmacsNative
 
   /* Send an ANDROID_CONFIGURE_NOTIFY event.  The values of all the
      functions below are the serials of the events sent.  */
-  public static native long sendConfigureNotify (short window, long time,
+  public static native long sendConfigureNotify (long window, long time,
                                                 int x, int y, int width,
                                                 int height);
 
   /* Send an ANDROID_KEY_PRESS event.  */
-  public static native long sendKeyPress (short window, long time, int state,
+  public static native long sendKeyPress (long window, long time, int state,
                                          int keyCode, int unicodeChar);
 
   /* Send an ANDROID_KEY_RELEASE event.  */
-  public static native long sendKeyRelease (short window, long time, int state,
+  public static native long sendKeyRelease (long window, long time, int state,
                                            int keyCode, int unicodeChar);
 
   /* Send an ANDROID_FOCUS_IN event.  */
-  public static native long sendFocusIn (short window, long time);
+  public static native long sendFocusIn (long window, long time);
 
   /* Send an ANDROID_FOCUS_OUT event.  */
-  public static native long sendFocusOut (short window, long time);
+  public static native long sendFocusOut (long window, long time);
 
   /* Send an ANDROID_WINDOW_ACTION event.  */
-  public static native long sendWindowAction (short window, int action);
+  public static native long sendWindowAction (long window, int action);
 
   /* Send an ANDROID_ENTER_NOTIFY event.  */
-  public static native long sendEnterNotify (short window, int x, int y,
+  public static native long sendEnterNotify (long window, int x, int y,
                                             long time);
 
   /* Send an ANDROID_LEAVE_NOTIFY event.  */
-  public static native long sendLeaveNotify (short window, int x, int y,
+  public static native long sendLeaveNotify (long window, int x, int y,
                                             long time);
 
   /* Send an ANDROID_MOTION_NOTIFY event.  */
-  public static native long sendMotionNotify (short window, int x, int y,
+  public static native long sendMotionNotify (long window, int x, int y,
                                              long time);
 
   /* Send an ANDROID_BUTTON_PRESS event.  */
-  public static native long sendButtonPress (short window, int x, int y,
+  public static native long sendButtonPress (long window, int x, int y,
                                             long time, int state,
                                             int button);
 
   /* Send an ANDROID_BUTTON_RELEASE event.  */
-  public static native long sendButtonRelease (short window, int x, int y,
+  public static native long sendButtonRelease (long window, int x, int y,
                                               long time, int state,
                                               int button);
 
   /* Send an ANDROID_TOUCH_DOWN event.  */
-  public static native long sendTouchDown (short window, int x, int y,
+  public static native long sendTouchDown (long window, int x, int y,
                                           long time, int pointerID,
                                           int flags);
 
   /* Send an ANDROID_TOUCH_UP event.  */
-  public static native long sendTouchUp (short window, int x, int y,
+  public static native long sendTouchUp (long window, int x, int y,
                                         long time, int pointerID,
                                         int flags);
 
   /* Send an ANDROID_TOUCH_MOVE event.  */
-  public static native long sendTouchMove (short window, int x, int y,
+  public static native long sendTouchMove (long window, int x, int y,
                                           long time, int pointerID,
                                           int flags);
 
   /* Send an ANDROID_WHEEL event.  */
-  public static native long sendWheel (short window, int x, int y,
+  public static native long sendWheel (long window, int x, int y,
                                       long time, int state,
                                       float xDelta, float yDelta);
 
   /* Send an ANDROID_ICONIFIED event.  */
-  public static native long sendIconified (short window);
+  public static native long sendIconified (long window);
 
   /* Send an ANDROID_DEICONIFIED event.  */
-  public static native long sendDeiconified (short window);
+  public static native long sendDeiconified (long window);
 
   /* Send an ANDROID_CONTEXT_MENU event.  */
-  public static native long sendContextMenu (short window, int menuEventID,
+  public static native long sendContextMenu (long window, int menuEventID,
                                             int menuEventSerial);
 
   /* Send an ANDROID_EXPOSE event.  */
-  public static native long sendExpose (short window, int x, int y,
+  public static native long sendExpose (long window, int x, int y,
                                        int width, int height);
 
   /* Send an ANDROID_DND_DRAG event.  */
-  public static native long sendDndDrag (short window, int x, int y);
+  public static native long sendDndDrag (long window, int x, int y);
 
   /* Send an ANDROID_DND_URI event.  */
-  public static native long sendDndUri (short window, int x, int y,
+  public static native long sendDndUri (long window, int x, int y,
                                        String text);
 
   /* Send an ANDROID_DND_TEXT event.  */
-  public static native long sendDndText (short window, int x, int y,
+  public static native long sendDndText (long window, int x, int y,
                                         String text);
 
   /* Send an ANDROID_NOTIFICATION_CANCELED event.  */
@@ -241,48 +241,48 @@ public final class EmacsNative
   /* Input connection functions.  These mostly correspond to their
      counterparts in Android's InputConnection.  */
 
-  public static native void beginBatchEdit (short window);
-  public static native void endBatchEdit (short window);
-  public static native void commitCompletion (short window, String text,
+  public static native void beginBatchEdit (long window);
+  public static native void endBatchEdit (long window);
+  public static native void commitCompletion (long window, String text,
                                              int position);
-  public static native void commitText (short window, String text,
+  public static native void commitText (long window, String text,
                                        int position);
-  public static native void deleteSurroundingText (short window,
+  public static native void deleteSurroundingText (long window,
                                                   int leftLength,
                                                   int rightLength);
-  public static native void finishComposingText (short window);
-  public static native void replaceText (short window, int start, int end,
+  public static native void finishComposingText (long window);
+  public static native void replaceText (long window, int start, int end,
                                         String text, int newCursorPosition,
                                         TextAttribute attributes);
-  public static native String getSelectedText (short window, int flags);
-  public static native String getTextAfterCursor (short window, int length,
+  public static native String getSelectedText (long window, int flags);
+  public static native String getTextAfterCursor (long window, int length,
                                                  int flags);
-  public static native String getTextBeforeCursor (short window, int length,
+  public static native String getTextBeforeCursor (long window, int length,
                                                   int flags);
-  public static native void setComposingText (short window, String text,
+  public static native void setComposingText (long window, String text,
                                              int newCursorPosition);
-  public static native void setComposingRegion (short window, int start,
+  public static native void setComposingRegion (long window, int start,
                                                int end);
-  public static native void setSelection (short window, int start, int end);
-  public static native void performEditorAction (short window,
+  public static native void setSelection (long window, int start, int end);
+  public static native void performEditorAction (long window,
                                                 int editorAction);
-  public static native void performContextMenuAction (short window,
+  public static native void performContextMenuAction (long window,
                                                      int contextMenuAction);
-  public static native ExtractedText getExtractedText (short window,
+  public static native ExtractedText getExtractedText (long window,
                                                       ExtractedTextRequest req,
                                                       int flags);
-  public static native void requestSelectionUpdate (short window);
-  public static native void requestCursorUpdates (short window, int mode);
-  public static native void clearInputFlags (short window);
-  public static native SurroundingText getSurroundingText (short window,
+  public static native void requestSelectionUpdate (long window);
+  public static native void requestCursorUpdates (long window, int mode);
+  public static native void clearInputFlags (long window);
+  public static native SurroundingText getSurroundingText (long window,
                                                           int left, int right,
                                                           int flags);
-  public static native TextSnapshot takeSnapshot (short window);
+  public static native TextSnapshot takeSnapshot (long window);
 
 \f
   /* Return the current value of the selection, or -1 upon
      failure.  */
-  public static native int[] getSelection (short window);
+  public static native int[] getSelection (long window);
 
 \f
   /* Graphics functions used as replacements for potentially buggy
index c621e2de3c5d028138c08fb3b920b320ea236507..bd4e085994efd14054de6888610e7671e4298b09 100644 (file)
@@ -51,9 +51,9 @@ public final class EmacsPixmap extends EmacsHandleObject
   private long gcClipRectID;
 
   public
-  EmacsPixmap (short handle, int width, int height, int depth)
+  EmacsPixmap (int width, int height, int depth)
   {
-    super (handle);
+    super ();
 
     if (depth != 1 && depth != 24)
       throw new IllegalArgumentException ("Invalid depth specified"
index 8e459ce4cdc4b99eb48b0b6a86d1d9f4e0d76811..ced9f59996074f687d0f31b2f7a4fe204b0281c5 100644 (file)
@@ -514,10 +514,10 @@ public final class EmacsService extends Service
       vibrator.vibrate (duration);
   }
 
-  public short[]
+  public long[]
   queryTree (EmacsWindow window)
   {
-    short[] array;
+    long[] array;
     List<EmacsWindow> windowList;
     int i;
 
@@ -529,7 +529,7 @@ public final class EmacsService extends Service
 
     synchronized (windowList)
       {
-       array = new short[windowList.size () + 1];
+       array = new long[windowList.size () + 1];
        i = 1;
 
        array[0] = (window == null
@@ -846,7 +846,7 @@ public final class EmacsService extends Service
   }
 
   public static int[]
-  viewGetSelection (short window)
+  viewGetSelection (long window)
   {
     int[] selection;
 
index 62e7b06d53e48e621563d843ac2bb2586571c137..793ffa4f191e768956079e629ed68f82ded8d09d 100644 (file)
@@ -170,11 +170,9 @@ public final class EmacsWindow extends EmacsHandleObject
   public boolean preserve, previouslyAttached;
 
   public
-  EmacsWindow (short handle, final EmacsWindow parent, int x, int y,
+  EmacsWindow (final EmacsWindow parent, int x, int y,
               int width, int height, boolean overrideRedirect)
   {
-    super (handle);
-
     rect = new Rect (x, y, x + width, y + height);
     pointerMap = new SparseArray<Coordinate> ();
 
@@ -205,7 +203,7 @@ public final class EmacsWindow extends EmacsHandleObject
          });
       }
 
-    scratchGC = new EmacsGC ((short) 0);
+    scratchGC = new EmacsGC ();
 
     /* Create the map of input method-committed strings.  Keep at most
        ten strings in the map.  */
index 49f0ebd5841f0656353ffb2122aa8317d2a80840..23dc71dbd293aba2d3f56d86cb6bd296dc9743a7 100644 (file)
@@ -145,7 +145,7 @@ public final class EmacsWindowManager
          }
       }
 
-    EmacsNative.sendWindowAction ((short) 0, 0);
+    EmacsNative.sendWindowAction (0, 0);
   }
 
   public synchronized void
index c76afdb9bf274f21b68a6e4c09e27a6dfd297826..0dd7702b30129a7bb99be31d517c8a529bdf0f4b 100644 (file)
@@ -129,6 +129,13 @@ struct android_key_character_map
   jmethodID get_dead_char;
 };
 
+struct android_emacs_handle
+{
+  jclass class;
+  jmethodID destroy_handle;
+  jfieldID handle;
+};
+
 /* The API level of the current device.  */
 static int android_api_level;
 
@@ -214,6 +221,10 @@ static struct android_emacs_cursor cursor_class;
 /* Various methods associated with the KeyCharacterMap class.  */
 static struct android_key_character_map key_character_map_class;
 
+/* Various methods and fields associated with the EmacsHandleObject
+   class.  */
+static struct android_emacs_handle handle_class;
+
 /* The time at which Emacs was installed, which also supplies the
    mtime of asset files.  */
 struct timespec emacs_installation_time;
@@ -1620,7 +1631,7 @@ android_init_emacs_service (void)
               "Lorg/gnu/emacs/EmacsGC;II)V");
   FIND_METHOD (ring_bell, "ringBell", "(I)V");
   FIND_METHOD (query_tree, "queryTree",
-              "(Lorg/gnu/emacs/EmacsWindow;)[S");
+              "(Lorg/gnu/emacs/EmacsWindow;)[J");
   FIND_METHOD (get_screen_width, "getScreenWidth", "(Z)I");
   FIND_METHOD (get_screen_height, "getScreenHeight", "(Z)I");
   FIND_METHOD (detect_mouse, "detectMouse", "()Z");
@@ -1723,7 +1734,7 @@ android_init_emacs_pixmap (void)
                                        name, signature);       \
   eassert (pixmap_class.c_name);
 
-  FIND_METHOD (constructor_mutable, "<init>", "(SIII)V");
+  FIND_METHOD (constructor_mutable, "<init>", "(III)V");
 
 #undef FIND_METHOD
 }
@@ -1876,7 +1887,7 @@ android_init_emacs_cursor (void)
                                        name, signature);       \
   eassert (cursor_class.c_name);
 
-  FIND_METHOD (constructor, "<init>", "(SI)V");
+  FIND_METHOD (constructor, "<init>", "(I)V");
 #undef FIND_METHOD
 }
 
@@ -1906,6 +1917,42 @@ android_init_key_character_map (void)
   eassert (key_character_map_class.get_dead_char);
 }
 
+static void
+android_init_emacs_handle (void)
+{
+  jclass old;
+
+  handle_class.class
+    = (*android_java_env)->FindClass (android_java_env,
+                                     "org/gnu/emacs/EmacsHandleObject");
+  eassert (handle_class.class);
+
+  old = handle_class.class;
+  handle_class.class
+    = (jclass) (*android_java_env)->NewGlobalRef (android_java_env,
+                                                 (jobject) old);
+  ANDROID_DELETE_LOCAL_REF (old);
+
+  if (!handle_class.class)
+    emacs_abort ();
+
+#define FIND_METHOD(c_name, name, signature)                   \
+  handle_class.c_name                                          \
+    = (*android_java_env)->GetMethodID (android_java_env,      \
+                                       handle_class.class,     \
+                                       name, signature);       \
+  eassert (handle_class.c_name);
+
+  FIND_METHOD (destroy_handle, "destroyHandle", "()V");
+#undef FIND_METHOD
+
+  handle_class.handle
+    = (*android_java_env)->GetFieldID (android_java_env,
+                                      handle_class.class,
+                                      "handle", "J");
+  eassert (handle_class.handle);
+}
+
 JNIEXPORT void JNICALL
 NATIVE_NAME (initEmacs) (JNIEnv *env, jobject object, jarray argv,
                         jobject dump_file_object)
@@ -1955,6 +2002,7 @@ NATIVE_NAME (initEmacs) (JNIEnv *env, jobject object, jarray argv,
   android_init_emacs_window ();
   android_init_emacs_cursor ();
   android_init_key_character_map ();
+  android_init_emacs_handle ();
 
   /* Set HOME to the app data directory.  */
   setenv ("HOME", android_files_dir, 1);
@@ -2074,7 +2122,7 @@ NATIVE_NAME (onLowMemory) (JNIEnv *env, jobject object)
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendConfigureNotify) (JNIEnv *env, jobject object,
-                                  jshort window, jlong time,
+                                  jlong window, jlong time,
                                   jint x, jint y, jint width,
                                   jint height)
 {
@@ -2097,7 +2145,7 @@ NATIVE_NAME (sendConfigureNotify) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendKeyPress) (JNIEnv *env, jobject object,
-                           jshort window, jlong time,
+                           jlong window, jlong time,
                            jint state, jint keycode,
                            jint unicode_char)
 {
@@ -2120,7 +2168,7 @@ NATIVE_NAME (sendKeyPress) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendKeyRelease) (JNIEnv *env, jobject object,
-                             jshort window, jlong time,
+                             jlong window, jlong time,
                              jint state, jint keycode,
                              jint unicode_char)
 {
@@ -2143,7 +2191,7 @@ NATIVE_NAME (sendKeyRelease) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendFocusIn) (JNIEnv *env, jobject object,
-                          jshort window, jlong time)
+                          jlong window, jlong time)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2160,7 +2208,7 @@ NATIVE_NAME (sendFocusIn) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendFocusOut) (JNIEnv *env, jobject object,
-                           jshort window, jlong time)
+                           jlong window, jlong time)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2177,7 +2225,7 @@ NATIVE_NAME (sendFocusOut) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendWindowAction) (JNIEnv *env, jobject object,
-                               jshort window, jint action)
+                               jlong window, jint action)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2194,7 +2242,7 @@ NATIVE_NAME (sendWindowAction) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendEnterNotify) (JNIEnv *env, jobject object,
-                              jshort window, jint x, jint y,
+                              jlong window, jint x, jint y,
                               jlong time)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2214,7 +2262,7 @@ NATIVE_NAME (sendEnterNotify) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendLeaveNotify) (JNIEnv *env, jobject object,
-                              jshort window, jint x, jint y,
+                              jlong window, jint x, jint y,
                               jlong time)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2234,7 +2282,7 @@ NATIVE_NAME (sendLeaveNotify) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendMotionNotify) (JNIEnv *env, jobject object,
-                               jshort window, jint x, jint y,
+                               jlong window, jint x, jint y,
                                jlong time)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2254,7 +2302,7 @@ NATIVE_NAME (sendMotionNotify) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendButtonPress) (JNIEnv *env, jobject object,
-                              jshort window, jint x, jint y,
+                              jlong window, jint x, jint y,
                               jlong time, jint state,
                               jint button)
 {
@@ -2277,7 +2325,7 @@ NATIVE_NAME (sendButtonPress) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendButtonRelease) (JNIEnv *env, jobject object,
-                                jshort window, jint x, jint y,
+                                jlong window, jint x, jint y,
                                 jlong time, jint state,
                                 jint button)
 {
@@ -2300,7 +2348,7 @@ NATIVE_NAME (sendButtonRelease) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendTouchDown) (JNIEnv *env, jobject object,
-                            jshort window, jint x, jint y,
+                            jlong window, jint x, jint y,
                             jlong time, jint pointer_id,
                             jint flags)
 {
@@ -2323,7 +2371,7 @@ NATIVE_NAME (sendTouchDown) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendTouchUp) (JNIEnv *env, jobject object,
-                          jshort window, jint x, jint y,
+                          jlong window, jint x, jint y,
                           jlong time, jint pointer_id,
                           jint flags)
 {
@@ -2346,7 +2394,7 @@ NATIVE_NAME (sendTouchUp) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendTouchMove) (JNIEnv *env, jobject object,
-                            jshort window, jint x, jint y,
+                            jlong window, jint x, jint y,
                             jlong time, jint pointer_id,
                             jint flags)
 {
@@ -2369,7 +2417,7 @@ NATIVE_NAME (sendTouchMove) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendWheel) (JNIEnv *env, jobject object,
-                        jshort window, jint x, jint y,
+                        jlong window, jint x, jint y,
                         jlong time, jint state,
                         jfloat x_delta, jfloat y_delta)
 {
@@ -2393,7 +2441,7 @@ NATIVE_NAME (sendWheel) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendIconified) (JNIEnv *env, jobject object,
-                            jshort window)
+                            jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2409,7 +2457,7 @@ NATIVE_NAME (sendIconified) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendDeiconified) (JNIEnv *env, jobject object,
-                              jshort window)
+                              jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2425,7 +2473,7 @@ NATIVE_NAME (sendDeiconified) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendContextMenu) (JNIEnv *env, jobject object,
-                              jshort window, jint menu_event_id,
+                              jlong window, jint menu_event_id,
                               jint menu_event_serial)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2444,7 +2492,7 @@ NATIVE_NAME (sendContextMenu) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendExpose) (JNIEnv *env, jobject object,
-                         jshort window, jint x, jint y,
+                         jlong window, jint x, jint y,
                          jint width, jint height)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2465,7 +2513,7 @@ NATIVE_NAME (sendExpose) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendDndDrag) (JNIEnv *env, jobject object,
-                          jshort window, jint x, jint y)
+                          jlong window, jint x, jint y)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -2485,7 +2533,7 @@ NATIVE_NAME (sendDndDrag) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendDndUri) (JNIEnv *env, jobject object,
-                         jshort window, jint x, jint y,
+                         jlong window, jint x, jint y,
                          jstring string)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2522,7 +2570,7 @@ NATIVE_NAME (sendDndUri) (JNIEnv *env, jobject object,
 
 JNIEXPORT jlong JNICALL
 NATIVE_NAME (sendDndText) (JNIEnv *env, jobject object,
-                          jshort window, jint x, jint y,
+                          jlong window, jint x, jint y,
                           jstring string)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -2711,7 +2759,6 @@ NATIVE_NAME (blitRect) (JNIEnv *env, jobject object,
   x2 = MAX (x2, 0);
   y2 = MAX (y2, 0);
 
-
   if (x1 >= src_info.width
       || x1 >= dest_info.width)
     x1 = MIN (dest_info.width - 1, src_info.width - 1);
@@ -2854,62 +2901,6 @@ NATIVE_NAME (setupSystemThread) (void)
    This means that every local reference must be explicitly destroyed
    with DeleteLocalRef.  A helper macro is provided to do this.  */
 
-struct android_handle_entry
-{
-  /* The type.  */
-  enum android_handle_type type;
-
-  /* The handle.  */
-  jobject handle;
-};
-
-/* Table of handles MAX_HANDLE long.  */
-struct android_handle_entry android_handles[USHRT_MAX];
-
-/* The largest handle ID currently known, but subject to
-   wraparound.  */
-static android_handle max_handle;
-
-/* Allocate a new, unused, handle identifier.  If Emacs is out of
-   identifiers, return 0.  */
-
-static android_handle
-android_alloc_id (void)
-{
-  android_handle handle;
-
-  /* 0 is never a valid handle ID.  */
-
-  if (!max_handle)
-    max_handle++;
-
-  /* See if the handle is already occupied.  */
-
-  if (android_handles[max_handle].handle)
-    {
-      /* Look for a fresh unoccupied handle.  */
-
-      handle = max_handle;
-      max_handle++;
-
-      while (handle != max_handle)
-       {
-         ++max_handle;
-
-         /* Make sure the handle is valid.  */
-         if (!max_handle)
-           ++max_handle;
-
-         if (!android_handles[max_handle].handle)
-           return max_handle++;
-       }
-
-      return ANDROID_NONE;
-    }
-
-  return max_handle++;
-}
-
 /* Destroy the specified handle and mark it as free on the Java side
    as well.  */
 
@@ -2919,13 +2910,6 @@ android_destroy_handle (android_handle handle)
   static jclass old, class;
   static jmethodID method;
 
-  if (!android_handles[handle].handle)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Trying to destroy free handle!");
-      emacs_abort ();
-    }
-
   if (!class)
     {
       class
@@ -2946,8 +2930,7 @@ android_destroy_handle (android_handle handle)
       ANDROID_DELETE_LOCAL_REF (old);
     }
 
-  (*android_java_env)->CallVoidMethod (android_java_env,
-                                      android_handles[handle].handle,
+  (*android_java_env)->CallVoidMethod (android_java_env, (jobject) handle,
                                       method);
 
   /* Just clear any exception thrown.  If destroying the handle
@@ -2956,76 +2939,7 @@ android_destroy_handle (android_handle handle)
   (*android_java_env)->ExceptionClear (android_java_env);
 
   /* Delete the global reference regardless of any error.  */
-  (*android_java_env)->DeleteGlobalRef (android_java_env,
-                                       android_handles[handle].handle);
-  android_handles[handle].handle = NULL;
-}
-
-jobject
-android_resolve_handle (android_handle handle,
-                       enum android_handle_type type)
-{
-  if (!handle)
-    /* ANDROID_NONE.  */
-    return NULL;
-
-  /* CheckJNI will normally ensure that the handle exists and is
-     the right type, but with a less informative error message.
-     Don't waste cycles doing our own checking here.  */
-
-#ifdef ENABLE_CHECKING
-
-  if (!android_handles[handle].handle)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Trying to resolve free handle!");
-      emacs_abort ();
-    }
-
-  if (android_handles[handle].type != type)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Handle has wrong type!");
-      emacs_abort ();
-    }
-
-#endif /* ENABLE_CHECKING */
-
-  return android_handles[handle].handle;
-}
-
-static jobject
-android_resolve_handle2 (android_handle handle,
-                        enum android_handle_type type,
-                        enum android_handle_type type2)
-{
-  if (!handle)
-    return NULL;
-
-  /* CheckJNI will normally ensure that the handle exists and is
-     the right type, but with a less informative error message.
-     Don't waste cycles doing our own checking here.  */
-
-#ifdef ENABLE_CHECKING
-
-  if (!android_handles[handle].handle)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Trying to resolve free handle!");
-      emacs_abort ();
-    }
-
-  if (android_handles[handle].type != type
-      && android_handles[handle].type != type2)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Handle has wrong type!");
-      emacs_abort ();
-    }
-
-#endif /* ENABLE_CHECKING */
-
-  return android_handles[handle].handle;
+  (*android_java_env)->DeleteGlobalRef (android_java_env, (jobject) handle);
 }
 
 void
@@ -3037,7 +2951,7 @@ android_change_window_attributes (android_window handle,
   jobject window;
   jint pixel;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
 
   if (value_mask & ANDROID_CW_BACK_PIXEL)
     {
@@ -3051,6 +2965,35 @@ android_change_window_attributes (android_window handle,
     }
 }
 
+/* Return a reference to the local reference HANDLE suitable for
+   indefinite retention and save its value into HANDLE, deleting HANDLE,
+   or signal an error if such a reference cannot be allocated.  */
+
+static android_handle
+android_globalize_reference (jobject handle)
+{
+  jobject global;
+
+  /* Though Android 8.0 and later can support an unlimited number of
+     active local references, they remain inappropriate in threading
+     configurations for being local to the current thread.  */
+
+  global = (*android_java_env)->NewGlobalRef (android_java_env,
+                                             handle);
+  (*android_java_env)->ExceptionClear (android_java_env);
+  ANDROID_DELETE_LOCAL_REF (handle);
+
+  if (__builtin_expect (global == NULL, 0))
+    error ("JNI global reference reserves exhausted");
+
+  /* Save the value of this handle into HANDLE.  */
+  (*android_java_env)->SetLongField (android_java_env, global,
+                                    handle_class.handle,
+                                    (jlong) global);
+  verify (sizeof (jlong) >= sizeof (intptr_t));
+  return (intptr_t) global;
+}
+
 /* Create a new window with the given width, height and
    attributes.  */
 
@@ -3064,16 +3007,10 @@ android_create_window (android_window parent, int x, int y,
   static jmethodID constructor;
   jobject object, parent_object, old;
   android_window window;
-  android_handle prev_max_handle;
   bool override_redirect;
 
-  parent_object = android_resolve_handle (parent, ANDROID_HANDLE_WINDOW);
-
-  prev_max_handle = max_handle;
-  window = android_alloc_id ();
+  parent_object = android_resolve_handle (parent);
 
-  if (!window)
-    error ("Out of window handles!");
 
   if (!class)
     {
@@ -3083,7 +3020,7 @@ android_create_window (android_window parent, int x, int y,
 
       constructor
        = (*android_java_env)->GetMethodID (android_java_env, class, "<init>",
-                                           "(SLorg/gnu/emacs/EmacsWindow;"
+                                           "(Lorg/gnu/emacs/EmacsWindow;"
                                            "IIIIZ)V");
       eassert (constructor != NULL);
 
@@ -3100,28 +3037,12 @@ android_create_window (android_window parent, int x, int y,
                       && attrs->override_redirect);
 
   object = (*android_java_env)->NewObject (android_java_env, class,
-                                          constructor, (jshort) window,
-                                          parent_object, (jint) x, (jint) y,
+                                          constructor, parent_object,
+                                          (jint) x, (jint) y,
                                           (jint) width, (jint) height,
                                           (jboolean) override_redirect);
-  if (!object)
-    {
-      (*android_java_env)->ExceptionClear (android_java_env);
-
-      max_handle = prev_max_handle;
-      memory_full (0);
-    }
-
-  android_handles[window].type = ANDROID_HANDLE_WINDOW;
-  android_handles[window].handle
-    = (*android_java_env)->NewGlobalRef (android_java_env,
-                                        object);
-  (*android_java_env)->ExceptionClear (android_java_env);
-  ANDROID_DELETE_LOCAL_REF (object);
-
-  if (!android_handles[window].handle)
-    memory_full (0);
-
+  android_exception_check ();
+  window = android_globalize_reference (object);
   android_change_window_attributes (window, value_mask, attrs);
   return window;
 }
@@ -3139,13 +3060,6 @@ android_set_window_background (android_window window, unsigned long pixel)
 void
 android_destroy_window (android_window window)
 {
-  if (android_handles[window].type != ANDROID_HANDLE_WINDOW)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Trying to destroy something not a window!");
-      emacs_abort ();
-    }
-
   android_destroy_handle (window);
 }
 
@@ -3193,7 +3107,7 @@ android_init_emacs_gc_class (void)
   emacs_gc_constructor
     = (*android_java_env)->GetMethodID (android_java_env,
                                        emacs_gc_class,
-                                       "<init>", "(S)V");
+                                       "<init>", "()V");
   eassert (emacs_gc_constructor);
 
   emacs_gc_mark_dirty
@@ -3279,14 +3193,12 @@ android_create_gc (enum android_gc_value_mask mask,
                   struct android_gc_values *values)
 {
   struct android_gc *gc;
-  android_handle prev_max_handle;
   jobject object;
 
   android_init_emacs_gc_class ();
 
   gc = xmalloc (sizeof *gc);
-  prev_max_handle = max_handle;
-  gc->gcontext = android_alloc_id ();
+  gc->gcontext   = 0;
   gc->foreground = 0;
   gc->background = 0xffffff;
   gc->clip_rects = NULL;
@@ -3309,34 +3221,12 @@ android_create_gc (enum android_gc_value_mask mask,
   gc->dashes = NULL;
   gc->n_segments = 0;
 
-  if (!gc->gcontext)
-    {
-      xfree (gc);
-      error ("Out of GContext handles!");
-    }
-
   object = (*android_java_env)->NewObject (android_java_env,
                                           emacs_gc_class,
-                                          emacs_gc_constructor,
-                                          (jshort) gc->gcontext);
-
-  if (!object)
-    {
-      (*android_java_env)->ExceptionClear (android_java_env);
-
-      max_handle = prev_max_handle;
-      memory_full (0);
-    }
-
-  android_handles[gc->gcontext].type = ANDROID_HANDLE_GCONTEXT;
-  android_handles[gc->gcontext].handle
-    = (*android_java_env)->NewGlobalRef (android_java_env, object);
-  (*android_java_env)->ExceptionClear (android_java_env);
-  ANDROID_DELETE_LOCAL_REF (object);
-
-  if (!android_handles[gc->gcontext].handle)
-    memory_full (0);
+                                          emacs_gc_constructor);
+  android_exception_check ();
 
+  gc->gcontext = android_globalize_reference (object);
   android_change_gc (gc, mask, values);
   return gc;
 }
@@ -3362,8 +3252,7 @@ android_change_gc (struct android_gc *gc,
   clip_changed = false;
 
   android_init_emacs_gc_class ();
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   if (mask & ANDROID_GC_FOREGROUND)
     {
@@ -3414,8 +3303,7 @@ android_change_gc (struct android_gc *gc,
 
   if (mask & ANDROID_GC_CLIP_MASK)
     {
-      what = android_resolve_handle (values->clip_mask,
-                                    ANDROID_HANDLE_PIXMAP);
+      what = android_resolve_handle (values->clip_mask);
       (*android_java_env)->SetObjectField (android_java_env,
                                           gcontext,
                                           emacs_gc_clip_mask,
@@ -3436,8 +3324,7 @@ android_change_gc (struct android_gc *gc,
 
   if (mask & ANDROID_GC_STIPPLE)
     {
-      what = android_resolve_handle (values->stipple,
-                                    ANDROID_HANDLE_PIXMAP);
+      what = android_resolve_handle (values->stipple);
       (*android_java_env)->SetObjectField (android_java_env,
                                           gcontext,
                                           emacs_gc_stipple,
@@ -3549,8 +3436,7 @@ android_set_clip_rectangles (struct android_gc *gc, int clip_x_origin,
   android_init_android_rect_class ();
   android_init_emacs_gc_class ();
 
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   array = (*android_java_env)->NewObjectArray (android_java_env,
                                               n_clip_rects,
@@ -3623,8 +3509,7 @@ android_set_dashes (struct android_gc *gc, int dash_offset,
   int i;
   jobject array, gcontext;
 
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   if (n == gc->n_segments
       && (!gc->dashes || !memcmp (gc->dashes, dash_list,
@@ -3692,9 +3577,8 @@ android_reparent_window (android_window w, android_window parent_handle,
   jobject window, parent;
   jmethodID method;
 
-  window = android_resolve_handle (w, ANDROID_HANDLE_WINDOW);
-  parent = android_resolve_handle (parent_handle,
-                                  ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (w);
+  parent = android_resolve_handle (parent_handle);
 
   method = window_class.reparent_to;
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env, window,
@@ -3708,7 +3592,7 @@ android_clear_window (android_window handle)
 {
   jobject window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 window,
@@ -3723,7 +3607,7 @@ android_map_window (android_window handle)
   jobject window;
   jmethodID map_window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   map_window = window_class.map_window;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -3739,7 +3623,7 @@ android_unmap_window (android_window handle)
   jobject window;
   jmethodID unmap_window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   unmap_window = window_class.unmap_window;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -3756,7 +3640,7 @@ android_resize_window (android_window handle, unsigned int width,
   jobject window;
   jmethodID resize_window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   resize_window = window_class.resize_window;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -3774,7 +3658,7 @@ android_move_window (android_window handle, int x, int y)
   jobject window;
   jmethodID move_window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   move_window = window_class.move_window;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -3794,8 +3678,7 @@ android_swap_buffers (struct android_swap_info *swap_info,
 
   for (i = 0; i < num_windows; ++i)
     {
-      window = android_resolve_handle (swap_info[i].swap_window,
-                                      ANDROID_HANDLE_WINDOW);
+      window = android_resolve_handle (swap_info[i].swap_window);
       (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                     window,
                                                     window_class.class,
@@ -3856,11 +3739,8 @@ android_fill_rectangle (android_drawable handle, struct android_gc *gc,
 {
   jobject drawable, gcontext;
 
-  drawable = android_resolve_handle2 (handle,
-                                     ANDROID_HANDLE_WINDOW,
-                                     ANDROID_HANDLE_PIXMAP);
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  drawable = android_resolve_handle (handle);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -4527,7 +4407,9 @@ android_copy_area (android_drawable src, android_drawable dest,
   /* Now damage the destination drawable accordingly, should it be a
      window.  */
 
-  if (android_handles[dest].type == ANDROID_HANDLE_WINDOW)
+  if ((*android_java_env)->IsInstanceOf (android_java_env,
+                                        (jobject) dest,
+                                        window_class.class))
     android_damage_window (dest, &bounds);
 
  fail2:
@@ -4573,11 +4455,8 @@ android_fill_polygon (android_drawable drawable, struct android_gc *gc,
   jobject point, drawable_object, gcontext;
   int i;
 
-  drawable_object = android_resolve_handle2 (drawable,
-                                            ANDROID_HANDLE_WINDOW,
-                                            ANDROID_HANDLE_PIXMAP);
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  drawable_object = android_resolve_handle (drawable);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   array = (*android_java_env)->NewObjectArray (android_java_env,
                                               npoints,
@@ -4615,11 +4494,8 @@ android_draw_rectangle (android_drawable handle, struct android_gc *gc,
 {
   jobject drawable, gcontext;
 
-  drawable = android_resolve_handle2 (handle,
-                                     ANDROID_HANDLE_WINDOW,
-                                     ANDROID_HANDLE_PIXMAP);
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  drawable = android_resolve_handle (handle);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -4640,11 +4516,8 @@ android_draw_point (android_drawable handle, struct android_gc *gc,
 {
   jobject drawable, gcontext;
 
-  drawable = android_resolve_handle2 (handle,
-                                     ANDROID_HANDLE_WINDOW,
-                                     ANDROID_HANDLE_PIXMAP);
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  drawable = android_resolve_handle (handle);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -4664,11 +4537,8 @@ android_draw_line (android_drawable handle, struct android_gc *gc,
 {
   jobject drawable, gcontext;
 
-  drawable = android_resolve_handle2 (handle,
-                                     ANDROID_HANDLE_WINDOW,
-                                     ANDROID_HANDLE_PIXMAP);
-  gcontext = android_resolve_handle (gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
+  drawable = android_resolve_handle (handle);
+  gcontext = android_resolve_handle (gc->gcontext);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -4687,41 +4557,15 @@ android_pixmap
 android_create_pixmap (unsigned int width, unsigned int height,
                       int depth)
 {
-  android_handle prev_max_handle;
   jobject object;
-  android_pixmap pixmap;
-
-  /* First, allocate the pixmap handle.  */
-  prev_max_handle = max_handle;
-  pixmap = android_alloc_id ();
-
-  if (!pixmap)
-    error ("Out of pixmap handles!");
 
   object = (*android_java_env)->NewObject (android_java_env,
                                           pixmap_class.class,
                                           pixmap_class.constructor_mutable,
-                                          (jshort) pixmap,
                                           (jint) width, (jint) height,
                                           (jint) depth);
-
-  if (!object)
-    {
-      (*android_java_env)->ExceptionClear (android_java_env);
-      max_handle = prev_max_handle;
-      memory_full (0);
-    }
-
-  android_handles[pixmap].type = ANDROID_HANDLE_PIXMAP;
-  android_handles[pixmap].handle
-    = (*android_java_env)->NewGlobalRef (android_java_env, object);
-  (*android_java_env)->ExceptionClear (android_java_env);
-  ANDROID_DELETE_LOCAL_REF (object);
-
-  if (!android_handles[pixmap].handle)
-    memory_full (0);
-
-  return pixmap;
+  android_exception_check ();
+  return android_globalize_reference (object);
 }
 
 void
@@ -4742,7 +4586,7 @@ android_clear_area (android_window handle, int x, int y,
 {
   jobject window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 window,
@@ -4894,8 +4738,7 @@ android_get_image (android_drawable handle,
   unsigned char *data1, *data2;
   int i, x;
 
-  drawable = android_resolve_handle2 (handle, ANDROID_HANDLE_WINDOW,
-                                     ANDROID_HANDLE_PIXMAP);
+  drawable = android_resolve_handle (handle);
 
   /* Look up the drawable and get the bitmap corresponding to it.
      Then, lock the bitmap's bits.  */
@@ -5029,7 +4872,7 @@ android_put_image (android_pixmap handle, struct android_image *image)
   unsigned char *data_1, *data_2;
   int i, x;
 
-  drawable = android_resolve_handle (handle, ANDROID_HANDLE_PIXMAP);
+  drawable = android_resolve_handle (handle);
 
   /* Look up the drawable and get the bitmap corresponding to it.
      Then, lock the bitmap's bits.  */
@@ -5131,7 +4974,7 @@ android_set_input_focus (android_window handle, unsigned long time)
   jobject window;
   jmethodID make_input_focus;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   make_input_focus = window_class.make_input_focus;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -5148,7 +4991,7 @@ android_raise_window (android_window handle)
   jobject window;
   jmethodID raise;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   raise = window_class.raise;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -5164,7 +5007,7 @@ android_lower_window (android_window handle)
   jobject window;
   jmethodID lower;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   lower = window_class.lower;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -5181,7 +5024,7 @@ android_reconfigure_wm_window (android_window handle,
 {
   jobject sibling, window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
 
   if (!(value_mask & ANDROID_CW_STACK_MODE))
     return;
@@ -5193,8 +5036,7 @@ android_reconfigure_wm_window (android_window handle,
   sibling = NULL;
 
   if (value_mask & ANDROID_CW_SIBLING)
-    sibling = android_resolve_handle (values->sibling,
-                                     ANDROID_HANDLE_WINDOW);
+    sibling = android_resolve_handle (values->sibling);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 window,
@@ -5214,10 +5056,10 @@ android_query_tree (android_window handle, android_window *root_return,
   jobject window, array;
   jsize nelements, i;
   android_window *children;
-  jshort *shorts;
+  jlong *longs;
   jmethodID method;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
 
   /* window can be NULL, so this is a service method.  */
   method = service_class.query_tree;
@@ -5237,25 +5079,25 @@ android_query_tree (android_window handle, android_window *root_return,
   /* Now fill in the children.  */
   children = xnmalloc (nelements - 1, sizeof *children);
 
-  shorts
-    = (*android_java_env)->GetShortArrayElements (android_java_env, array,
-                                                 NULL);
-  android_exception_check_nonnull (shorts, array);
+  longs
+    = (*android_java_env)->GetLongArrayElements (android_java_env, array,
+                                                NULL);
+  android_exception_check_nonnull (longs, array);
 
   for (i = 1; i < nelements; ++i)
     /* Subtract one from the index into children, since the parent is
        not included.  */
-    children[i - 1] = shorts[i];
+    children[i - 1] = longs[i];
 
   /* Finally, return the parent and other values.  */
   *root_return = 0;
-  *parent_return = shorts[0];
+  *parent_return = longs[0];
   *children_return = children;
   *nchildren_return = nelements - 1;
 
   /* Release the array contents.  */
-  (*android_java_env)->ReleaseShortArrayElements (android_java_env, array,
-                                                 shorts, JNI_ABORT);
+  (*android_java_env)->ReleaseLongArrayElements (android_java_env, array,
+                                                longs, JNI_ABORT);
 
   ANDROID_DELETE_LOCAL_REF (array);
   return 1;
@@ -5274,7 +5116,7 @@ android_get_geometry (android_window handle,
   jmethodID get_geometry;
   jint *ints;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   get_geometry = window_class.get_window_geometry;
 
   window_geometry
@@ -5336,7 +5178,7 @@ android_translate_coordinates (android_window src, int x,
   jmethodID method;
   jint *ints;
 
-  window = android_resolve_handle (src, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (src);
   method = window_class.translate_coordinates;
   coordinates
     = (*android_java_env)->CallNonvirtualObjectMethod (android_java_env,
@@ -5504,51 +5346,44 @@ android_wc_lookup_string (android_key_pressed_event *event,
   /* Now look up the window.  */
   rc = 0;
 
-  if (!android_handles[event->window].handle
-      || (android_handles[event->window].type
-         != ANDROID_HANDLE_WINDOW))
+  window = android_resolve_handle (event->window);
+  string
+    = (*env)->CallNonvirtualObjectMethod (env, window,
+                                         window_class.class,
+                                         window_class.lookup_string,
+                                         (jint) event->serial);
+  android_exception_check ();
+
+  if (!string)
     status = ANDROID_LOOKUP_NONE;
   else
     {
-      window = android_handles[event->window].handle;
-      string
-       = (*env)->CallNonvirtualObjectMethod (env, window,
-                                             window_class.class,
-                                             window_class.lookup_string,
-                                             (jint) event->serial);
-      android_exception_check ();
-
-      if (!string)
-       status = ANDROID_LOOKUP_NONE;
-      else
-       {
-         /* Now return this input method string.  */
-         characters = (*env)->GetStringChars (env, string, NULL);
-         android_exception_check_nonnull ((void *) characters, string);
+      /* Now return this input method string.  */
+      characters = (*env)->GetStringChars (env, string, NULL);
+      android_exception_check_nonnull ((void *) characters, string);
 
-         /* Establish the size of the the string.  */
-         size = (*env)->GetStringLength (env, string);
+      /* Establish the size of the the string.  */
+      size = (*env)->GetStringLength (env, string);
 
-         /* Copy over the string data.  */
-         for (i = 0; i < MIN ((unsigned int) wchars_buffer, size); ++i)
-           buffer_return[i] = characters[i];
+      /* Copy over the string data.  */
+      for (i = 0; i < MIN ((unsigned int) wchars_buffer, size); ++i)
+       buffer_return[i] = characters[i];
 
-         if (i < size)
-           status = ANDROID_BUFFER_OVERFLOW;
-         else
-           status = ANDROID_LOOKUP_CHARS;
+      if (i < size)
+       status = ANDROID_BUFFER_OVERFLOW;
+      else
+       status = ANDROID_LOOKUP_CHARS;
 
-         /* Return the number of characters that should have been
-            written.  */
+      /* Return the number of characters that should have been
+        written.  */
 
-         if (size > INT_MAX)
-           rc = INT_MAX;
-         else
-           rc = size;
+      if (size > INT_MAX)
+       rc = INT_MAX;
+      else
+       rc = size;
 
-         (*env)->ReleaseStringChars (env, string, characters);
-         ANDROID_DELETE_LOCAL_REF (string);
-       }
+      (*env)->ReleaseStringChars (env, string, characters);
+      ANDROID_DELETE_LOCAL_REF (string);
     }
 
   *status_return = status;
@@ -5582,8 +5417,7 @@ android_lock_bitmap (android_drawable drawable,
   jobject object, bitmap;
   void *data;
 
-  object = android_resolve_handle2 (drawable, ANDROID_HANDLE_WINDOW,
-                                   ANDROID_HANDLE_PIXMAP);
+  object = android_resolve_handle (drawable);
 
   /* Look up the drawable and get the bitmap corresponding to it.
      Then, lock the bitmap's bits.  */
@@ -5637,7 +5471,7 @@ android_damage_window (android_drawable handle,
 {
   jobject drawable;
 
-  drawable = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  drawable = android_resolve_handle (handle);
 
   /* Post the damage to the drawable.  */
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -5758,7 +5592,7 @@ android_set_dont_focus_on_map (android_window handle,
   jmethodID method;
   jobject window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   method = window_class.set_dont_focus_on_map;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env, window,
@@ -5775,7 +5609,7 @@ android_set_dont_accept_focus (android_window handle,
   jmethodID method;
   jobject window;
 
-  window = android_resolve_handle (handle, ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (handle);
   method = window_class.set_dont_accept_focus;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env, window,
@@ -5854,7 +5688,7 @@ android_toggle_on_screen_keyboard (android_window window, bool show)
   jobject object;
   jmethodID method;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
   method = window_class.toggle_on_screen_keyboard;
 
   /* Now display the on screen keyboard.  */
@@ -6115,11 +5949,7 @@ android_build_jstring (const char *text)
    if global_foo cannot be allocated, and after the global reference
    is created.  */
 
-#if __GNUC__ >= 3
 #define likely(cond)   __builtin_expect (cond, 1)
-#else /* __GNUC__ < 3 */
-#define likely(cond)   (cond)
-#endif /* __GNUC__ >= 3 */
 
 /* Check for JNI exceptions and call memory_full in that
    situation.  */
@@ -6831,7 +6661,7 @@ android_recreate_activity (android_window window)
   jobject object;
   jmethodID method;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
   method = window_class.recreate_activity;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env, object,
@@ -7196,7 +7026,7 @@ android_update_ic (android_window window, ptrdiff_t selection_start,
 {
   jobject object;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -7233,7 +7063,7 @@ android_reset_ic (android_window window, enum android_ic_mode mode)
 {
   jobject object;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 emacs_service,
@@ -7255,7 +7085,7 @@ android_update_extracted_text (android_window window, void *text,
   jobject object;
   jmethodID method;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
   method = service_class.update_extracted_text;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -7289,7 +7119,7 @@ android_update_cursor_anchor_info (android_window window, float x,
   jobject object;
   jmethodID method;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
   method = service_class.update_cursor_anchor_info;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -7324,7 +7154,7 @@ android_set_fullscreen (android_window window, bool fullscreen)
   if (android_api_level < 16)
     return 1;
 
-  object = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
+  object = android_resolve_handle (window);
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
                                                 object,
@@ -7342,40 +7172,15 @@ android_set_fullscreen (android_window window, bool fullscreen)
 android_cursor
 android_create_font_cursor (enum android_cursor_shape shape)
 {
-  android_cursor id;
-  short prev_max_handle;
   jobject object;
 
-  /* First, allocate the cursor handle.  */
-  prev_max_handle = max_handle;
-  id = android_alloc_id ();
-
-  if (!id)
-    error ("Out of cursor handles!");
-
   /* Next, create the cursor.  */
   object = (*android_java_env)->NewObject (android_java_env,
                                           cursor_class.class,
                                           cursor_class.constructor,
-                                          (jshort) id,
                                           (jint) shape);
-  if (!object)
-    {
-      (*android_java_env)->ExceptionClear (android_java_env);
-      max_handle = prev_max_handle;
-      memory_full (0);
-    }
-
-  android_handles[id].type = ANDROID_HANDLE_CURSOR;
-  android_handles[id].handle
-    = (*android_java_env)->NewGlobalRef (android_java_env, object);
-  (*android_java_env)->ExceptionClear (android_java_env);
-  ANDROID_DELETE_LOCAL_REF (object);
-
-  if (!android_handles[id].handle)
-    memory_full (0);
-
-  return id;
+  android_exception_check ();
+  return android_globalize_reference (object);
 }
 
 void
@@ -7384,8 +7189,8 @@ android_define_cursor (android_window window, android_cursor cursor)
   jobject window1, cursor1;
   jmethodID method;
 
-  window1 = android_resolve_handle (window, ANDROID_HANDLE_WINDOW);
-  cursor1 = android_resolve_handle (cursor, ANDROID_HANDLE_CURSOR);
+  window1 = android_resolve_handle (window);
+  cursor1 = android_resolve_handle (cursor);
   method = window_class.define_cursor;
 
   (*android_java_env)->CallNonvirtualVoidMethod (android_java_env,
@@ -7398,13 +7203,6 @@ android_define_cursor (android_window window, android_cursor cursor)
 void
 android_free_cursor (android_cursor cursor)
 {
-  if (android_handles[cursor].type != ANDROID_HANDLE_CURSOR)
-    {
-      __android_log_print (ANDROID_LOG_ERROR, __func__,
-                          "Trying to destroy something not a CURSOR!");
-      emacs_abort ();
-    }
-
   android_destroy_handle (cursor);
 }
 
index 7074ca2630c303e0e7faa38229948e6ed1dbc885..a582a9b7dff9665566e13f4a6036ddfd4f6452e7 100644 (file)
@@ -101,16 +101,9 @@ extern ssize_t android_readlinkat (int, const char *restrict, char *restrict,
 extern double android_pixel_density_x, android_pixel_density_y;
 extern double android_scaled_pixel_density;
 
-enum android_handle_type
-  {
-    ANDROID_HANDLE_WINDOW,
-    ANDROID_HANDLE_GCONTEXT,
-    ANDROID_HANDLE_PIXMAP,
-    ANDROID_HANDLE_CURSOR,
-  };
-
-extern jobject android_resolve_handle (android_handle,
-                                      enum android_handle_type);
+verify (sizeof (android_handle) == sizeof (jobject));
+#define android_resolve_handle(handle) ((jobject) (handle))
+
 extern unsigned char *android_lock_bitmap (android_drawable,
                                           AndroidBitmapInfo *,
                                           jobject *);
index 5fd3018b6d45206bb237171173bddab85d507e57..20a18327ff885e8ae94f4ef9fc259fd2c8163a4d 100644 (file)
@@ -657,10 +657,8 @@ androidfont_draw (struct glyph_string *s, int from, int to,
   verify (sizeof (unsigned int) == sizeof (jint));
   info = (struct androidfont_info *) s->font;
 
-  gcontext = android_resolve_handle (s->gc->gcontext,
-                                    ANDROID_HANDLE_GCONTEXT);
-  drawable = android_resolve_handle (FRAME_ANDROID_DRAWABLE (s->f),
-                                    ANDROID_HANDLE_WINDOW);
+  gcontext = android_resolve_handle (s->gc->gcontext);
+  drawable = android_resolve_handle (FRAME_ANDROID_DRAWABLE (s->f));
   chars = (*android_java_env)->NewIntArray (android_java_env,
                                            to - from);
   android_exception_check ();
index 3b3e08ca024eb9e40b6c536e55ef41fbf5d15789..79e42c7947c9ccc4833d6a8a0621e3b437b35ba6 100644 (file)
@@ -19,6 +19,8 @@ along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
 #ifndef _ANDROID_GUI_H_
 #define _ANDROID_GUI_H_
 
+#include <stdint.h>
+
 struct android_char_struct
 {
   int rbearing;
@@ -30,7 +32,8 @@ struct android_char_struct
 
 typedef struct android_char_struct XCharStruct;
 
-typedef unsigned short android_handle;
+/* Handles are but JNI handles cast to intptr_t.  */
+typedef intptr_t android_handle;
 
 typedef android_handle android_pixmap, Emacs_Pixmap;
 typedef android_handle android_window, Emacs_Window;
index 362d500ac1a3c2b61777d9404f8638141bf97986..7d24087fa87e06f1b2276cd5c41c9b1244285811 100644 (file)
@@ -488,8 +488,7 @@ android_menu_show (struct frame *f, int x, int y, int menuflags,
   unbind_to (count1, Qnil);
 
   /* Now, display the context menu.  */
-  window = android_resolve_handle (FRAME_ANDROID_WINDOW (f),
-                                  ANDROID_HANDLE_WINDOW);
+  window = android_resolve_handle (FRAME_ANDROID_WINDOW (f));
   rc = (*env)->CallNonvirtualBooleanMethod (env, context_menu,
                                            menu_class.class,
                                            menu_class.display,
index 2979e5c1401a4630f287ba04666757c7cf780eea..f4c071f4519229eb98d74679cf1c80ef418865d6 100644 (file)
@@ -4925,7 +4925,7 @@ android_copy_java_string (JNIEnv *env, jstring string, size_t *length)
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (beginBatchEdit) (JNIEnv *env, jobject object, jshort window)
+NATIVE_NAME (beginBatchEdit) (JNIEnv *env, jobject object, jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -4946,7 +4946,7 @@ NATIVE_NAME (beginBatchEdit) (JNIEnv *env, jobject object, jshort window)
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (endBatchEdit) (JNIEnv *env, jobject object, jshort window)
+NATIVE_NAME (endBatchEdit) (JNIEnv *env, jobject object, jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -4967,7 +4967,7 @@ NATIVE_NAME (endBatchEdit) (JNIEnv *env, jobject object, jshort window)
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (commitCompletion) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (commitCompletion) (JNIEnv *env, jobject object, jlong window,
                                jstring completion_text, jint position)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5001,7 +5001,7 @@ NATIVE_NAME (commitCompletion) (JNIEnv *env, jobject object, jshort window,
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (commitText) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (commitText) (JNIEnv *env, jobject object, jlong window,
                          jstring commit_text, jint position)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5036,7 +5036,7 @@ NATIVE_NAME (commitText) (JNIEnv *env, jobject object, jshort window,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (deleteSurroundingText) (JNIEnv *env, jobject object,
-                                    jshort window, jint left_length,
+                                    jlong window, jint left_length,
                                     jint right_length)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5059,7 +5059,7 @@ NATIVE_NAME (deleteSurroundingText) (JNIEnv *env, jobject object,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (finishComposingText) (JNIEnv *env, jobject object,
-                                  jshort window)
+                                  jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5080,7 +5080,7 @@ NATIVE_NAME (finishComposingText) (JNIEnv *env, jobject object,
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (replaceText) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (replaceText) (JNIEnv *env, jobject object, jlong window,
                           jint start, jint end, jobject text,
                           int new_cursor_position, jobject attribute)
 {
@@ -5246,7 +5246,7 @@ android_text_to_string (JNIEnv *env, char *buffer, ptrdiff_t n,
 }
 
 JNIEXPORT jstring JNICALL
-NATIVE_NAME (getTextAfterCursor) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (getTextAfterCursor) (JNIEnv *env, jobject object, jlong window,
                                  jint length, jint flags)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5290,7 +5290,7 @@ NATIVE_NAME (getTextAfterCursor) (JNIEnv *env, jobject object, jshort window,
 }
 
 JNIEXPORT jstring JNICALL
-NATIVE_NAME (getTextBeforeCursor) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (getTextBeforeCursor) (JNIEnv *env, jobject object, jlong window,
                                   jint length, jint flags)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5334,7 +5334,7 @@ NATIVE_NAME (getTextBeforeCursor) (JNIEnv *env, jobject object, jshort window,
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (setComposingText) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (setComposingText) (JNIEnv *env, jobject object, jlong window,
                                jstring composing_text,
                                jint new_cursor_position)
 {
@@ -5369,7 +5369,7 @@ NATIVE_NAME (setComposingText) (JNIEnv *env, jobject object, jshort window,
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (setComposingRegion) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (setComposingRegion) (JNIEnv *env, jobject object, jlong window,
                                  jint start, jint end)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5391,7 +5391,7 @@ NATIVE_NAME (setComposingRegion) (JNIEnv *env, jobject object, jshort window,
 }
 
 JNIEXPORT void JNICALL
-NATIVE_NAME (setSelection) (JNIEnv *env, jobject object, jshort window,
+NATIVE_NAME (setSelection) (JNIEnv *env, jobject object, jlong window,
                            jint start, jint end)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5469,7 +5469,7 @@ android_get_selection (void *data)
 }
 
 JNIEXPORT jintArray JNICALL
-NATIVE_NAME (getSelection) (JNIEnv *env, jobject object, jshort window)
+NATIVE_NAME (getSelection) (JNIEnv *env, jobject object, jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5508,7 +5508,7 @@ NATIVE_NAME (getSelection) (JNIEnv *env, jobject object, jshort window)
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (performEditorAction) (JNIEnv *env, jobject object,
-                                  jshort window, int action)
+                                  jlong window, int action)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5560,7 +5560,7 @@ NATIVE_NAME (performEditorAction) (JNIEnv *env, jobject object,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (performContextMenuAction) (JNIEnv *env, jobject object,
-                                       jshort window, int action)
+                                       jlong window, int action)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5765,7 +5765,7 @@ android_build_extracted_text (jstring text, ptrdiff_t start,
 
 JNIEXPORT jobject JNICALL
 NATIVE_NAME (getExtractedText) (JNIEnv *env, jobject ignored_object,
-                               jshort window, jobject request,
+                               jlong window, jobject request,
                                jint flags)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -5877,7 +5877,7 @@ NATIVE_NAME (getExtractedText) (JNIEnv *env, jobject ignored_object,
 
 JNIEXPORT jstring JNICALL
 NATIVE_NAME (getSelectedText) (JNIEnv *env, jobject object,
-                              jshort window)
+                              jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5907,7 +5907,7 @@ NATIVE_NAME (getSelectedText) (JNIEnv *env, jobject object,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (requestSelectionUpdate) (JNIEnv *env, jobject object,
-                                     jshort window)
+                                     jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5929,7 +5929,7 @@ NATIVE_NAME (requestSelectionUpdate) (JNIEnv *env, jobject object,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (requestCursorUpdates) (JNIEnv *env, jobject object,
-                                   jshort window, jint mode)
+                                   jlong window, jint mode)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -5958,7 +5958,7 @@ NATIVE_NAME (requestCursorUpdates) (JNIEnv *env, jobject object,
 
 JNIEXPORT void JNICALL
 NATIVE_NAME (clearInputFlags) (JNIEnv *env, jobject object,
-                              jshort window)
+                              jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
 
@@ -6073,7 +6073,7 @@ android_get_surrounding_text (void *data)
    Value is the object upon success, else NULL.  */
 
 static jobject
-android_get_surrounding_text_internal (JNIEnv *env, jshort window,
+android_get_surrounding_text_internal (JNIEnv *env, jlong window,
                                       jint before_length,
                                       jint after_length,
                                       ptrdiff_t *conversion_start,
@@ -6166,7 +6166,7 @@ android_get_surrounding_text_internal (JNIEnv *env, jshort window,
 
 JNIEXPORT jobject JNICALL
 NATIVE_NAME (getSurroundingText) (JNIEnv *env, jobject object,
-                                 jshort window, jint before_length,
+                                 jlong window, jint before_length,
                                  jint after_length, jint flags)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;
@@ -6176,7 +6176,7 @@ NATIVE_NAME (getSurroundingText) (JNIEnv *env, jobject object,
 }
 
 JNIEXPORT jobject JNICALL
-NATIVE_NAME (takeSnapshot) (JNIEnv *env, jobject object, jshort window)
+NATIVE_NAME (takeSnapshot) (JNIEnv *env, jobject object, jlong window)
 {
   JNI_STACK_ALIGNMENT_PROLOGUE;