summaryrefslogtreecommitdiff
path: root/java/org/gnu/emacs/EmacsWindow.java
blob: 2baede1d2d0edbec5d81f427e3784a101b54247e (plain)
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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
/* 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 java.lang.IllegalStateException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.LinkedHashMap;
import java.util.Map;

import android.app.Activity;

import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ContentResolver;
import android.content.Context;

import android.graphics.Rect;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;

import android.net.Uri;

import android.view.DragEvent;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewManager;
import android.view.WindowManager;

import android.util.SparseArray;
import android.util.Log;

import android.os.Build;

/* This defines a window, which is a handle.  Windows represent a
   rectangular subset of the screen with their own contents.

   Windows either have a parent window, in which case their views are
   attached to the parent's view, or are "floating", in which case
   their views are attached to the parent activity (if any), else
   nothing.

   Views are also drawables, meaning they can accept drawing
   requests.  */

public final class EmacsWindow extends EmacsHandleObject
  implements EmacsDrawable
{
  private static final String TAG = "EmacsWindow";

  private static class Coordinate
  {
    /* Integral coordinate.  */
    int x, y;

    /* Button associated with the coordinate, or 0 if it is a touch
       event.  */
    int button;

    /* Pointer ID associated with the coordinate.  */
    int id;

    public
    Coordinate (int x, int y, int button, int id)
    {
      this.x = x;
      this.y = y;
      this.button = button;
      this.id = id;
    }
  };

  /* The view associated with the window.  */
  public EmacsView view;

  /* The geometry of the window.  */
  private Rect rect;

  /* The parent window, or null if it is the root window.  */
  public EmacsWindow parent;

  /* List of all children in stacking order.  This must be kept
     consistent with their Z order!

     Synchronize access to this list with itself.  */
  public ArrayList<EmacsWindow> children;

  /* Map between pointer identifiers and last known position.  Used to
     compute which pointer changed upon a touch event.  */
  private SparseArray<Coordinate> pointerMap;

  /* The window consumer currently attached, if it exists.  */
  private EmacsWindowAttachmentManager.WindowConsumer attached;

  /* The window background scratch GC.  foreground is always the
     window background.  */
  private EmacsGC scratchGC;

  /* The button state and keyboard modifier mask at the time of the
     last button press or release event.  */
  public int lastButtonState;

  /* Whether or not the window is mapped.  */
  private volatile boolean isMapped;

  /* Whether or not to ask for focus upon being mapped.  */
  private boolean dontFocusOnMap;

  /* Whether or not the window is override-redirect.  An
     override-redirect window always has its own system window.  */
  private boolean overrideRedirect;

  /* The window manager that is the parent of this window.  NULL if
     there is no such window manager.  */
  private WindowManager windowManager;

  /* The time of the last KEYCODE_VOLUME_DOWN release.  This is used
     to quit Emacs upon two rapid clicks of the volume down
     button.  */
  private long lastVolumeButtonRelease;

  /* Linked list of character strings which were recently sent as
     events.  */
  public LinkedHashMap<Integer, String> eventStrings;

  /* Whether or not this window is fullscreen.  */
  public boolean fullscreen;

  /* The window background pixel.  This is used by EmacsView when
     creating new bitmaps.  */
  public volatile int background;

  /* The position of this window relative to the root window.  */
  public int xPosition, yPosition;

  /* The position of the last drag and drop event received; both
     values are -1 if no drag and drop operation is under way.  */
  private int dndXPosition, dndYPosition;

  public
  EmacsWindow (short handle, 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> ();

    /* Create the view from the context's UI thread.  The window is
       unmapped, so the view is GONE.  */
    view = EmacsService.SERVICE.getEmacsView (this, View.GONE,
					      parent == null);
    this.parent = parent;
    this.overrideRedirect = overrideRedirect;

    /* Create the list of children.  */
    children = new ArrayList<EmacsWindow> ();

    if (parent != null)
      {
	synchronized (parent.children)
	  {
	    parent.children.add (this);
	  }

        EmacsService.SERVICE.runOnUiThread (new Runnable () {
	    @Override
	    public void
	    run ()
	    {
	      parent.view.addView (view);
	    }
	  });
      }

    scratchGC = new EmacsGC ((short) 0);

    /* Create the map of input method-committed strings.  Keep at most
       ten strings in the map.  */

    eventStrings
      = new LinkedHashMap<Integer, String> () {
	  @Override
	  protected boolean
	  removeEldestEntry (Map.Entry<Integer, String> entry)
	  {
	    return size () > 10;
	  }
	};

    dndXPosition = -1;
    dndYPosition = -1;
  }

  public void
  changeWindowBackground (int pixel)
  {
    /* scratchGC is used as the argument to a FillRectangles req.  */
    scratchGC.foreground = pixel;
    scratchGC.markDirty (false);

    /* Make the background known to the view as well.  */
    background = pixel;
  }

  public synchronized Rect
  getGeometry ()
  {
    return new Rect (rect);
  }

  @Override
  public synchronized void
  destroyHandle () throws IllegalStateException
  {
    if (parent != null)
      {
	synchronized (parent.children)
	  {
	    parent.children.remove (this);
	  }
      }

    EmacsActivity.invalidateFocus (4);

    if (!children.isEmpty ())
      throw new IllegalStateException ("Trying to destroy window with "
				       + "children!");

    /* Remove the view from its parent and make it invisible.  */
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	public void
	run ()
	{
	  ViewManager parent;
	  EmacsWindowAttachmentManager manager;

	  if (EmacsActivity.focusedWindow == EmacsWindow.this)
	    EmacsActivity.focusedWindow = null;

	  manager = EmacsWindowAttachmentManager.MANAGER;
	  view.setVisibility (View.GONE);

	  /* If the window manager is set, use that instead.  */
	  if (windowManager != null)
	    parent = windowManager;
	  else
	    parent = (ViewManager) view.getParent ();
	  windowManager = null;

	  if (parent != null)
	    parent.removeView (view);

	  manager.detachWindow (EmacsWindow.this);
	}
      });

    super.destroyHandle ();
  }

  public void
  setConsumer (EmacsWindowAttachmentManager.WindowConsumer consumer)
  {
    attached = consumer;
  }

  public EmacsWindowAttachmentManager.WindowConsumer
  getAttachedConsumer ()
  {
    return attached;
  }

  public synchronized long
  viewLayout (int left, int top, int right, int bottom)
  {
    int rectWidth, rectHeight;

    rect.left = left;
    rect.top = top;
    rect.right = right;
    rect.bottom = bottom;

    rectWidth = right - left;
    rectHeight = bottom - top;

    /* If parent is null, use xPosition and yPosition instead of the
       geometry rectangle positions.  */

    if (parent == null)
      {
	left = xPosition;
	top = yPosition;
      }

    return EmacsNative.sendConfigureNotify (this.handle,
					    System.currentTimeMillis (),
					    left, top, rectWidth,
					    rectHeight);
  }

  public void
  requestViewLayout ()
  {
    view.explicitlyDirtyBitmap ();

    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  if (overrideRedirect)
	    /* Set the layout parameters again.  */
	    view.setLayoutParams (getWindowLayoutParams ());

	  view.mustReportLayout = true;
	  view.requestLayout ();
	}
      });
  }

  public synchronized void
  resizeWindow (int width, int height)
  {
    rect.right = rect.left + width;
    rect.bottom = rect.top + height;

    requestViewLayout ();
  }

  public synchronized void
  moveWindow (int x, int y)
  {
    int width, height;

    width = rect.width ();
    height = rect.height ();

    rect.left = x;
    rect.top = y;
    rect.right = x + width;
    rect.bottom = y + height;

    requestViewLayout ();
  }

  /* Return WM layout parameters for an override redirect window with
     the geometry provided here.  */

  private WindowManager.LayoutParams
  getWindowLayoutParams ()
  {
    WindowManager.LayoutParams params;
    int flags, type;
    Rect rect;

    flags = 0;
    rect = getGeometry ();
    flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;

    params
      = new WindowManager.LayoutParams (rect.width (), rect.height (),
					rect.left, rect.top,
					type, flags,
					PixelFormat.RGBA_8888);
    params.gravity = Gravity.TOP | Gravity.LEFT;
    return params;
  }

  private Activity
  findSuitableActivityContext ()
  {
    /* Find a recently focused activity.  */
    if (!EmacsActivity.focusedActivities.isEmpty ())
      return EmacsActivity.focusedActivities.get (0);

    /* Resort to the last activity to be focused.  */
    return EmacsActivity.lastFocusedActivity;
  }

  public synchronized void
  mapWindow ()
  {
    final int width, height;

    if (isMapped)
      return;

    isMapped = true;
    width = rect.width ();
    height = rect.height ();

    if (parent == null)
      {
        EmacsService.SERVICE.runOnUiThread (new Runnable () {
	    @Override
	    public void
	    run ()
	    {
	      EmacsWindowAttachmentManager manager;
	      WindowManager windowManager;
	      Activity ctx;
	      Object tem;
	      WindowManager.LayoutParams params;

	      /* Make the view visible, first of all.  */
	      view.setVisibility (View.VISIBLE);

	      if (!overrideRedirect)
		{
		  manager = EmacsWindowAttachmentManager.MANAGER;

		  /* If parent is the root window, notice that there are new
		     children available for interested activities to pick
		     up.  */
		  manager.registerWindow (EmacsWindow.this);

		  if (!getDontFocusOnMap ())
		    /* Eventually this should check no-focus-on-map.  */
		    view.requestFocus ();
		}
	      else
		{
		  /* But if the window is an override-redirect window,
		     then:

		     - Find an activity that is currently active.

		     - Map the window as a panel on top of that
                       activity using the system window manager.  */

		  ctx = findSuitableActivityContext ();

		  if (ctx == null)
		    {
		      Log.w (TAG, "failed to attach override-redirect window"
			     + " for want of activity");
		      return;
		    }

		  tem = ctx.getSystemService (Context.WINDOW_SERVICE);
		  windowManager = (WindowManager) tem;

		  /* Calculate layout parameters and propagate the
		     activity's token into it.  */

		  params = getWindowLayoutParams ();
		  params.token = (ctx.findViewById (android.R.id.content)
				  .getWindowToken ());
		  view.setLayoutParams (params);

		  /* Attach the view.  */
		  try
		    {
		      view.prepareForLayout (width, height);
		      windowManager.addView (view, params);

		      /* Record the window manager being used in the
			 EmacsWindow object.  */
		      EmacsWindow.this.windowManager = windowManager;
		    }
		  catch (Exception e)
		    {
		      Log.w (TAG,
			     "failed to attach override-redirect window, " + e);
		    }
		}
	    }
	  });
      }
    else
      {
	/* Do the same thing as above, but don't register this
	   window.  */
        EmacsService.SERVICE.runOnUiThread (new Runnable () {
	    @Override
	    public void
	    run ()
	    {
	      /* Prior to mapping the view, set its measuredWidth and
		 measuredHeight to some reasonable value, in order to
		 avoid excessive bitmap dirtying.  */

	      view.prepareForLayout (width, height);
	      view.setVisibility (View.VISIBLE);

	      if (!getDontFocusOnMap ())
		view.requestFocus ();
	    }
	  });
      }
  }

  public synchronized void
  unmapWindow ()
  {
    if (!isMapped)
      return;

    isMapped = false;

    view.post (new Runnable () {
	@Override
	public void
	run ()
	{
	  EmacsWindowAttachmentManager manager;

	  manager = EmacsWindowAttachmentManager.MANAGER;

	  view.setVisibility (View.GONE);

	  /* Detach the view from the window manager if possible.  */
	  if (windowManager != null)
	    windowManager.removeView (view);
	  windowManager = null;

	  /* Now that the window is unmapped, unregister it as
	     well.  */
	  manager.detachWindow (EmacsWindow.this);
	}
      });
  }

  @Override
  public Canvas
  lockCanvas (EmacsGC gc)
  {
    return view.getCanvas (gc);
  }

  @Override
  public void
  damageRect (Rect damageRect)
  {
    view.damageRect (damageRect.left,
		     damageRect.top,
		     damageRect.right,
		     damageRect.bottom);
  }

  @Override
  public void
  damageRect (int left, int top, int right, int bottom)
  {
    view.damageRect (left, top, right, bottom);
  }

  public void
  swapBuffers ()
  {
    view.swapBuffers ();
  }

  public void
  clearWindow ()
  {
    EmacsService.SERVICE.fillRectangle (this, scratchGC,
					0, 0, rect.width (),
					rect.height ());
  }

  public void
  clearArea (int x, int y, int width, int height)
  {
    EmacsService.SERVICE.fillRectangle (this, scratchGC,
					x, y, width, height);
  }

  @Override
  public Bitmap
  getBitmap ()
  {
    return view.getBitmap ();
  }

  /* event.getCharacters is used because older input methods still
     require it.  */
  @SuppressWarnings ("deprecation")
  public int
  getEventUnicodeChar (KeyEvent event, int state)
  {
    String characters;

    if (event.getUnicodeChar (state) != 0)
      return event.getUnicodeChar (state);

    characters = event.getCharacters ();

    if (characters != null && characters.length () == 1)
      return characters.charAt (0);

    return characters == null ? 0 : -1;
  }

  public void
  saveUnicodeString (int serial, String string)
  {
    eventStrings.put (serial, string);
  }



  /* Return the modifier mask associated with the specified keyboard
     input EVENT.  Replace bits corresponding to Left or Right keys
     with their corresponding general modifier bits.  */

  public static int
  eventModifiers (KeyEvent event)
  {
    int state;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
      state = event.getModifiers ();
    else
      {
	/* Replace this with getMetaState and manual
	   normalization.  */
	state = event.getMetaState ();

	/* Normalize the state by setting the generic modifier bit if
	   either a left or right modifier is pressed.  */

	if ((state & KeyEvent.META_ALT_LEFT_ON) != 0
	    || (state & KeyEvent.META_ALT_RIGHT_ON) != 0)
	  state |= KeyEvent.META_ALT_MASK;

	if ((state & KeyEvent.META_CTRL_LEFT_ON) != 0
	    || (state & KeyEvent.META_CTRL_RIGHT_ON) != 0)
	  state |= KeyEvent.META_CTRL_MASK;
      }

    return state;
  }

  /* event.getCharacters is used because older input methods still
     require it.  */
  @SuppressWarnings ("deprecation")
  public void
  onKeyDown (int keyCode, KeyEvent event)
  {
    int state, state_1, extra_ignored;
    long serial;
    String characters;

    if (keyCode == KeyEvent.KEYCODE_BACK)
      {
	/* New Android systems display Back navigation buttons on a
	   row of virtual buttons at the bottom of the screen.  These
	   buttons function much as physical buttons do, in that key
	   down events are produced when a finger taps them, even if
	   the finger is not ultimately released after the OS's
	   gesture navigation is activated.

	   Deliver onKeyDown events in onKeyUp instead, so as not to
	   navigate backwards during gesture navigation.  */

	return;
      }

    state = eventModifiers (event);

    /* Num Lock, Scroll Lock and Meta aren't supported by systems older
       than Android 3.0. */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
      extra_ignored = (KeyEvent.META_NUM_LOCK_ON
		       | KeyEvent.META_SCROLL_LOCK_ON
		       | KeyEvent.META_META_MASK);
    else
      extra_ignored = 0;

    /* Ignore meta-state understood by Emacs for now, or key presses
       such as Ctrl+C and Meta+C will not be recognized as ASCII key
       press events.  */

    state_1
      = state & ~(KeyEvent.META_ALT_MASK | KeyEvent.META_CTRL_MASK
		  | KeyEvent.META_SYM_ON | extra_ignored);

    /* There's no distinction between Right Alt and Alt Gr on Android,
       so restore META_ALT_RIGHT_ON if set in state to enable composing
       characters.  (bug#69321) */

    if ((state & KeyEvent.META_ALT_RIGHT_ON) != 0)
      {
	state_1 |= KeyEvent.META_ALT_ON | KeyEvent.META_ALT_RIGHT_ON;

	/* If Alt is also not depressed, remove its bit from the mask
	   reported to Emacs.  */
	if ((state & KeyEvent.META_ALT_LEFT_ON) == 0)
	  state &= ~KeyEvent.META_ALT_MASK;
      }

    synchronized (eventStrings)
      {
	serial
	  = EmacsNative.sendKeyPress (this.handle,
				      event.getEventTime (),
				      state, keyCode,
				      getEventUnicodeChar (event,
							   state_1));

	characters = event.getCharacters ();

	if (characters != null && characters.length () > 1)
	  saveUnicodeString ((int) serial, characters);
      }
  }

  public void
  onKeyUp (int keyCode, KeyEvent event)
  {
    int state, state_1, unicode_char, extra_ignored;
    long time;

    /* Compute the event's modifier mask.  */
    state = eventModifiers (event);

    /* Num Lock, Scroll Lock and Meta aren't supported by systems older
       than Android 3.0. */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
      extra_ignored = (KeyEvent.META_NUM_LOCK_ON
		       | KeyEvent.META_SCROLL_LOCK_ON
		       | KeyEvent.META_META_MASK);
    else
      extra_ignored = 0;

    /* Ignore meta-state understood by Emacs for now, or key presses
       such as Ctrl+C and Meta+C will not be recognized as ASCII key
       press events.  */

    state_1
      = state & ~(KeyEvent.META_ALT_MASK | KeyEvent.META_CTRL_MASK
		  | KeyEvent.META_SYM_ON | extra_ignored);

    /* There's no distinction between Right Alt and Alt Gr on Android,
       so restore META_ALT_RIGHT_ON if set in state to enable composing
       characters.  */

    if ((state & KeyEvent.META_ALT_RIGHT_ON) != 0)
      {
	state_1 |= KeyEvent.META_ALT_ON | KeyEvent.META_ALT_RIGHT_ON;

	/* If Alt is also not depressed, remove its bit from the mask
	   reported to Emacs.  */
	if ((state & KeyEvent.META_ALT_LEFT_ON) == 0)
	  state &= ~KeyEvent.META_ALT_MASK;
      }

    unicode_char = getEventUnicodeChar (event, state_1);

    if (keyCode == KeyEvent.KEYCODE_BACK)
      {
	/* If the key press's been canceled, return immediately.  */

	if ((event.getFlags () & KeyEvent.FLAG_CANCELED) != 0)
	  return;

	EmacsNative.sendKeyPress (this.handle, event.getEventTime (),
				  state, keyCode, unicode_char);
      }

    EmacsNative.sendKeyRelease (this.handle, event.getEventTime (),
				state, keyCode, unicode_char);

    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
      {
	/* Check if this volume down press should quit Emacs.
	   Most Android devices have no physical keyboard, so it
	   is unreasonably hard to press C-g.  */

	time = event.getEventTime ();

	if (time - lastVolumeButtonRelease < 350)
	  EmacsNative.quit ();

	lastVolumeButtonRelease = time;
      }
  }

  public void
  onFocusChanged (boolean gainFocus)
  {
    EmacsActivity.invalidateFocus (gainFocus ? 6 : 5);
  }

  /* Notice that the activity has been detached or destroyed.

     ISFINISHING is set if the activity is not the main activity, or
     if the activity was not destroyed in response to explicit user
     action.  */

  public void
  onActivityDetached (boolean isFinishing)
  {
    /* Destroy the associated frame when the activity is detached in
       response to explicit user action.  */

    if (isFinishing)
      EmacsNative.sendWindowAction (this.handle, 0);
  }



  /* Mouse and touch event handling.

     Android does not conceptually distinguish between mouse events
     (those coming from a device whose movement affects the on-screen
     pointer image) and touch screen events.  Each click or touch
     starts a single pointer gesture sequence, and subsequent motion
     of the device will result in updates being reported relative to
     that sequence until the mouse button or touch is released.

     When a touch, click, or pointer motion takes place, several kinds
     of event can be sent:

     ACTION_DOWN or ACTION_POINTER_DOWN is sent with a new coordinate
     and an associated ``pointer ID'' identifying the event and its
     gesture sequence when a click or touch takes place.  Emacs is
     responsible for recording both the position and pointer ID of
     this click for the purpose of determining future changes to its
     position.

     ACTION_UP or ACTION_POINTER_UP is sent with a pointer ID when the
     click associated with a previous ACTION_DOWN event is released.

     ACTION_CANCEL (or ACTION_POINTER_UP with FLAG_CANCELED) is sent
     if a similar situation transpires: the window system has chosen
     to grab the click, and future changes to its position will no
     longer be reported to Emacs.

     ACTION_MOVE is sent if a coordinate tied to a click that has not
     been released changes.  Emacs processes this event by comparing
     each of the coordinates within the event with its recollection of
     those contained within prior ACTION_DOWN and ACTION_MOVE events;
     the pointer ID of the differing coordinate is then reported
     within a touch or pointer motion event along with its new
     position.

     The events described above are all sent for both touch and mouse
     click events.  Determining whether an ACTION_DOWN event is
     associated with a button event is performed by inspecting the
     mouse button state associated with that event.  If it contains
     any mouse buttons that were not contained in the button state at
     the time of the last ACTION_DOWN or ACTION_UP event, the
     coordinate contained within is assumed to be a mouse click,
     leading to it and associated motion or ACTION_UP events being
     reported as mouse button or motion events.  Otherwise, those
     events are reported as touch screen events, with the touch ID set
     to the pointer ID.

     In addition to the events illustrated above, Android also sends
     several other types of event upon select types of activity from a
     mouse device:

     ACTION_HOVER_MOVE is sent with the coordinate of the mouse
     pointer if it moves above a frame prior to any click taking
     place.  Emacs sends a mouse motion event containing the
     coordinate.

     ACTION_HOVER_ENTER and ACTION_HOVER_LEAVE are respectively sent
     when the mouse pointer enters and leaves a frame.  Moreover,
     ACTION_HOVER_LEAVE events are sent immediately before an
     ACTION_DOWN event associated with a mouse click.  These
     extraneous events are distinct in that their button states always
     contain an additional button compared to the button state
     recorded at the time of the last ACTION_UP event.

     On Android 6.0 and later, ACTION_BUTTON_PRESS is sent with the
     coordinate of the mouse pointer if a mouse click occurs,
     alongside a ACTION_DOWN event.  ACTION_BUTTON_RELEASE is sent
     with the same information upon a mouse click being released, also
     accompanying an ACTION_UP event.

     However, both types of button events are implemented in a buggy
     fashion and cannot be used to report button events.  */

  /* Look through the button state to determine what button EVENT was
     generated from.  DOWN is true if EVENT is a button press event,
     false otherwise.  Value is the X number of the button.  */

  private int
  whatButtonWasIt (MotionEvent event, boolean down)
  {
    int eventState, notIn;

    /* Obtain the new button state.  */
    eventState = event.getButtonState ();

    /* Compute which button is now set or no longer set.  */

    notIn = (down ? eventState & ~lastButtonState
	     : lastButtonState & ~eventState);

    if ((notIn & (MotionEvent.BUTTON_PRIMARY
		  | MotionEvent.BUTTON_SECONDARY
		  | MotionEvent.BUTTON_TERTIARY)) == 0)
      /* No buttons have been pressed, so this is a touch event.  */
      return 0;

    if ((notIn & MotionEvent.BUTTON_PRIMARY) != 0)
      return 1;

    if ((notIn & MotionEvent.BUTTON_SECONDARY) != 0)
      return 3;

    if ((notIn & MotionEvent.BUTTON_TERTIARY) != 0)
      return 2;

    /* Buttons 4, 5, 6 and 7 are actually scroll wheels under X.
       Thus, report additional buttons starting at 8.  */

    if ((notIn & MotionEvent.BUTTON_BACK) != 0)
      return 8;

    if ((notIn & MotionEvent.BUTTON_FORWARD) != 0)
      return 9;

    /* Report stylus events as touch screen events.  */

    if ((notIn & MotionEvent.BUTTON_STYLUS_PRIMARY) != 0)
      return 0;

    if ((notIn & MotionEvent.BUTTON_STYLUS_SECONDARY) != 0)
      return 0;

    /* Not a real value.  */
    return 11;
  }

  /* Return the mouse button associated with the specified ACTION_DOWN
     or ACTION_POINTER_DOWN EVENT.

     Value is 0 if no mouse button was pressed, or the X number of
     that mouse button.  */

  private int
  buttonForEvent (MotionEvent event)
  {
    /* ICS and earlier don't support true mouse button events, so
       treat all down events as touch screen events.  */

    if (Build.VERSION.SDK_INT
	< Build.VERSION_CODES.ICE_CREAM_SANDWICH)
      return 0;

    return whatButtonWasIt (event, true);
  }

  /* Return the coordinate object associated with the specified
     EVENT, or null if it is not known.  */

  private Coordinate
  figureChange (MotionEvent event)
  {
    int i, truncatedX, truncatedY, pointerIndex, pointerID, count;
    Coordinate coordinate;

    /* Initialize this variable now.  */
    coordinate = null;

    switch (event.getActionMasked ())
      {
      case MotionEvent.ACTION_DOWN:
	/* Primary pointer pressed with index 0.  */

	pointerID = event.getPointerId (0);
	coordinate = new Coordinate ((int) event.getX (0),
				     (int) event.getY (0),
				     buttonForEvent (event),
				     pointerID);
	pointerMap.put (pointerID, coordinate);
	break;

      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
	/* Primary pointer released with index 0.  */
	pointerID = event.getPointerId (0);
	coordinate = pointerMap.get (pointerID);
	pointerMap.delete (pointerID);
	break;

      case MotionEvent.ACTION_POINTER_DOWN:
	/* New pointer.  Find the pointer ID from the index and place
	   it in the map.  */
	pointerIndex = event.getActionIndex ();
	pointerID = event.getPointerId (pointerIndex);
	coordinate = new Coordinate ((int) event.getX (pointerIndex),
				     (int) event.getY (pointerIndex),
				     buttonForEvent (event),
				     pointerID);
	pointerMap.put (pointerID, coordinate);
	break;

      case MotionEvent.ACTION_POINTER_UP:
	/* Pointer removed.  Remove it from the map.  */
	pointerIndex = event.getActionIndex ();
	pointerID = event.getPointerId (pointerIndex);
	coordinate = pointerMap.get (pointerID);
	pointerMap.delete (pointerID);
	break;

      default:

	/* Loop through each pointer in the event.  */

	count = event.getPointerCount ();
	for (i = 0; i < count; ++i)
	  {
	    pointerID = event.getPointerId (i);

	    /* Look up that pointer in the map.  */
	    coordinate = pointerMap.get (pointerID);

	    if (coordinate != null)
	      {
		/* See if coordinates have changed.  */
		truncatedX = (int) event.getX (i);
		truncatedY = (int) event.getY (i);

		if (truncatedX != coordinate.x
		    || truncatedY != coordinate.y)
		  {
		    /* The pointer changed.  Update the coordinate and
		       break out of the loop.  */
		    coordinate.x = truncatedX;
		    coordinate.y = truncatedY;

		    break;
		  }
	      }
	  }

	/* Set coordinate to NULL if the loop failed to find any
	   matching pointer.  */

	if (i == count)
	  coordinate = null;
      }

    /* Return the pointer ID.  */
    return coordinate;
  }

  /* Return the modifier mask associated with the specified motion
     EVENT.  Replace bits corresponding to Left or Right keys with
     their corresponding general modifier bits.  */

  private int
  motionEventModifiers (MotionEvent event)
  {
    int state;

    state = event.getMetaState ();

    /* Normalize the state by setting the generic modifier bit if
       either a left or right modifier is pressed.  */

    if ((state & KeyEvent.META_ALT_LEFT_ON) != 0
	|| (state & KeyEvent.META_ALT_RIGHT_ON) != 0)
      state |= KeyEvent.META_ALT_MASK;

    if ((state & KeyEvent.META_CTRL_LEFT_ON) != 0
	|| (state & KeyEvent.META_CTRL_RIGHT_ON) != 0)
      state |= KeyEvent.META_CTRL_MASK;

    return state;
  }

  /* Process a single ACTION_DOWN, ACTION_POINTER_DOWN, ACTION_UP,
     ACTION_POINTER_UP, ACTION_CANCEL, or ACTION_MOVE event.

     Ascertain which coordinate changed and send an appropriate mouse
     or touch screen event.  */

  private void
  motionEvent (MotionEvent event)
  {
    Coordinate coordinate;
    int modifiers;
    long time;

    /* Find data associated with this event's pointer.  Namely, its
       current location, whether or not a change has taken place, and
       whether or not it is a button event.  */

    coordinate = figureChange (event);

    if (coordinate == null)
      return;

    time = event.getEventTime ();

    if (coordinate.button != 0)
      {
	/* This event is tied to a mouse click, so report mouse motion
	   and button events.  */

	modifiers = motionEventModifiers (event);

	switch (event.getAction ())
	  {
	  case MotionEvent.ACTION_POINTER_DOWN:
	  case MotionEvent.ACTION_DOWN:
	    EmacsNative.sendButtonPress (this.handle, coordinate.x,
					 coordinate.y, time, modifiers,
					 coordinate.button);
	    break;

	  case MotionEvent.ACTION_POINTER_UP:
	  case MotionEvent.ACTION_UP:
	  case MotionEvent.ACTION_CANCEL:
	    EmacsNative.sendButtonRelease (this.handle, coordinate.x,
					   coordinate.y, time, modifiers,
					   coordinate.button);
	    break;

	  case MotionEvent.ACTION_MOVE:
	    EmacsNative.sendMotionNotify (this.handle, coordinate.x,
					  coordinate.y, time);
	    break;
	  }
      }
    else
      {
	/* This event is a touch event, and the touch ID is the
	   pointer ID.  */

	switch (event.getActionMasked ())
	  {
	  case MotionEvent.ACTION_DOWN:
	  case MotionEvent.ACTION_POINTER_DOWN:
	    /* Touch down event.  */
	    EmacsNative.sendTouchDown (this.handle, coordinate.x,
				       coordinate.y, time,
				       coordinate.id, 0);
	    break;

	  case MotionEvent.ACTION_UP:
	  case MotionEvent.ACTION_POINTER_UP:
	    /* Touch up event.  */
	    EmacsNative.sendTouchUp (this.handle, coordinate.x,
				     coordinate.y, time,
				     coordinate.id, 0);
	    break;

	  case MotionEvent.ACTION_CANCEL:
	    /* Touch sequence cancellation event.  */
	    EmacsNative.sendTouchUp (this.handle, coordinate.x,
				     coordinate.y, time,
				     coordinate.id,
				     1 /* ANDROID_TOUCH_SEQUENCE_CANCELED */);
	    break;

	  case MotionEvent.ACTION_MOVE:
	    /* Pointer motion event.  */
	    EmacsNative.sendTouchMove (this.handle, coordinate.x,
				       coordinate.y, time,
				       coordinate.id, 0);
	    break;
	  }
      }

    if (Build.VERSION.SDK_INT
	< Build.VERSION_CODES.ICE_CREAM_SANDWICH)
      return;

    /* Now update the button state.  */
    lastButtonState = event.getButtonState ();
    return;
  }

  public boolean
  onTouchEvent (MotionEvent event)
  {
    switch (event.getActionMasked ())
      {
      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_POINTER_DOWN:
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_MOVE:
	motionEvent (event);
	return true;
      }

    return false;
  }

  public boolean
  onGenericMotionEvent (MotionEvent event)
  {
    switch (event.getAction ())
      {
      case MotionEvent.ACTION_HOVER_ENTER:
	EmacsNative.sendEnterNotify (this.handle, (int) event.getX (),
				     (int) event.getY (),
				     event.getEventTime ());
	return true;

      case MotionEvent.ACTION_HOVER_MOVE:
	EmacsNative.sendMotionNotify (this.handle, (int) event.getX (),
				      (int) event.getY (),
				      event.getEventTime ());
	return true;

      case MotionEvent.ACTION_HOVER_EXIT:

	/* If the exit event comes from a button press, its button
	   state will have extra bits compared to the last known
	   button state.  Since the exit event will interfere with
	   tool bar button presses, ignore such splurious events.  */

	if ((event.getButtonState () & ~lastButtonState) == 0)
	  EmacsNative.sendLeaveNotify (this.handle, (int) event.getX (),
				       (int) event.getY (),
				       event.getEventTime ());

	return true;

      case MotionEvent.ACTION_DOWN:
      case MotionEvent.ACTION_POINTER_DOWN:
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_POINTER_UP:
      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_MOVE:
	/* MotionEvents may either be sent to onGenericMotionEvent or
	   onTouchEvent depending on if Android thinks it is a mouse
	   event or not, but we detect them ourselves.  */
	motionEvent (event);
	return true;

      case MotionEvent.ACTION_SCROLL:
	/* Send a scroll event with the specified deltas.  */
	EmacsNative.sendWheel (this.handle, (int) event.getX (),
			       (int) event.getY (),
			       event.getEventTime (),
			       motionEventModifiers (event),
			       event.getAxisValue (MotionEvent.AXIS_HSCROLL),
			       event.getAxisValue (MotionEvent.AXIS_VSCROLL));
	return true;
      }

    return false;
  }



  public synchronized void
  reparentTo (final EmacsWindow otherWindow, int x, int y)
  {
    int width, height;

    /* Reparent this window to the other window.  */

    if (parent != null)
      {
	synchronized (parent.children)
	  {
	    parent.children.remove (this);
	  }
      }

    if (otherWindow != null)
      {
	synchronized (otherWindow.children)
	  {
	    otherWindow.children.add (this);
	  }
      }

    parent = otherWindow;

    /* Move this window to the new location.  */
    width = rect.width ();
    height = rect.height ();
    rect.left = x;
    rect.top = y;
    rect.right = x + width;
    rect.bottom = y + height;

    /* Now do the work necessary on the UI thread to reparent the
       window.  */
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  EmacsWindowAttachmentManager manager;
	  ViewManager parent;

	  /* First, detach this window if necessary.  */
	  manager = EmacsWindowAttachmentManager.MANAGER;
	  manager.detachWindow (EmacsWindow.this);

	  /* Also unparent this view.  */

	  /* If the window manager is set, use that instead.  */
	  if (windowManager != null)
	    parent = windowManager;
	  else
	    parent = (ViewManager) view.getParent ();
	  windowManager = null;

	  if (parent != null)
	    parent.removeView (view);

	  /* Next, either add this window as a child of the new
	     parent's view, or make it available again.  */
	  if (otherWindow != null)
	    otherWindow.view.addView (view);
	  else if (EmacsWindow.this.isMapped)
	    manager.registerWindow (EmacsWindow.this);

	  /* Request relayout.  */
	  view.requestLayout ();
	}
      });
  }

  public void
  makeInputFocus (long time)
  {
    /* TIME is currently ignored.  Request the input focus now.  */

    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  view.requestFocus ();
	}
      });
  }

  public synchronized void
  raise ()
  {
    /* This does nothing here.  */
    if (parent == null)
      return;

    synchronized (parent.children)
      {
	/* Remove and add this view again.  */
	parent.children.remove (this);
	parent.children.add (this);
      }

    /* Request a relayout.  */
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  view.raise ();
	}
      });
  }

  public synchronized void
  lower ()
  {
    /* This does nothing here.  */
    if (parent == null)
      return;

    synchronized (parent.children)
      {
	/* Remove and add this view again.  */
	parent.children.remove (this);
	parent.children.add (this);
      }

    /* Request a relayout.  */
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  view.lower ();
	}
      });
  }

  public synchronized void
  reconfigure (final EmacsWindow window, final int stackMode)
  {
    ListIterator<EmacsWindow> iterator;
    EmacsWindow object;

    /* This does nothing here.  */
    if (parent == null)
      return;

    /* If window is NULL, call lower or upper subject to
       stackMode.  */

    if (window == null)
      {
	if (stackMode == 1) /* ANDROID_BELOW */
	  lower ();
	else
	  raise ();

	return;
      }

    /* Otherwise, if window.parent is distinct from this, return.  */
    if (window.parent != this.parent)
      return;

    /* Synchronize with the parent's child list.  Iterate over each
       item until WINDOW is encountered, before moving this window to
       the location prescribed by STACKMODE.  */

    synchronized (parent.children)
      {
	/* Remove this window from parent.children, for it will be
	   reinserted before or after WINDOW.  */
	parent.children.remove (this);

	/* Create an iterator.  */
	iterator = parent.children.listIterator ();

	while (iterator.hasNext ())
	  {
	    object = iterator.next ();

	    if (object == window)
	      {
		/* Now place this before or after the cursor of the
		   iterator.  */

		if (stackMode == 0) /* ANDROID_ABOVE */
		  iterator.add (this);
		else
		  {
		    iterator.previous ();
		    iterator.add (this);
		  }

		/* Effect the same adjustment upon the view
		   hierarchy.  */

		EmacsService.SERVICE.runOnUiThread (new Runnable () {
		    @Override
		    public void
		    run ()
		    {
		      if (stackMode == 0)
			view.moveAbove (window.view);
		      else
			view.moveBelow (window.view);
		    }
		  });
	      }
	  }

	/* parent.children does not list WINDOW, which should never
	   transpire.  */
	EmacsNative.emacsAbort ();
      }
  }

  public synchronized int[]
  getWindowGeometry ()
  {
    int[] array;

    array = new int[4];

    array[0] = parent != null ? rect.left : xPosition;
    array[1] = parent != null ? rect.top : yPosition;
    array[2] = rect.width ();
    array[3] = rect.height ();

    return array;
  }

  public void
  noticeIconified ()
  {
    EmacsNative.sendIconified (this.handle);
  }

  public void
  noticeDeiconified ()
  {
    EmacsNative.sendDeiconified (this.handle);
  }

  public synchronized void
  setDontAcceptFocus (final boolean dontAcceptFocus)
  {
    /* Update the view's focus state.  */
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  view.setFocusable (!dontAcceptFocus);
	  view.setFocusableInTouchMode (!dontAcceptFocus);
	}
      });
  }

  public synchronized void
  setDontFocusOnMap (final boolean dontFocusOnMap)
  {
    this.dontFocusOnMap = dontFocusOnMap;
  }

  public synchronized boolean
  getDontFocusOnMap ()
  {
    return dontFocusOnMap;
  }

  public int[]
  translateCoordinates (int x, int y)
  {
    int[] array;

    /* This is supposed to translate coordinates to the root
       window.  */
    array = new int[2];
    EmacsService.SERVICE.getLocationOnScreen (view, array);

    /* Now, the coordinates of the view should be in array.  Offset X
       and Y by them.  */
    array[0] += x;
    array[1] += y;

    /* Return the resulting coordinates.  */
    return array;
  }

  public void
  toggleOnScreenKeyboard (final boolean on)
  {
    /* Even though InputMethodManager functions are thread safe,
       `showOnScreenKeyboard' etc must be called from the UI thread in
       order to avoid deadlocks if the calls happen in tandem with a
       call to a synchronizing function within
       `onCreateInputConnection'.  */

    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  if (on)
	    view.showOnScreenKeyboard ();
	  else
	    view.hideOnScreenKeyboard ();
	}
      });
  }

  public String
  lookupString (int eventSerial)
  {
    String any;

    synchronized (eventStrings)
      {
	any = eventStrings.remove (eventSerial);
      }

    return any;
  }

  public void
  setFullscreen (final boolean isFullscreen)
  {
    EmacsService.SERVICE.runOnUiThread (new Runnable () {
	@Override
	public void
	run ()
	{
	  EmacsActivity activity;
	  Object tem;

	  fullscreen = isFullscreen;
	  tem = getAttachedConsumer ();

	  if (tem != null)
	    {
	      activity = (EmacsActivity) tem;
	      activity.syncFullscreenWith (EmacsWindow.this);
	    }
	}
      });
  }

  public void
  defineCursor (final EmacsCursor cursor)
  {
    /* Don't post this message if pointer icons aren't supported.  */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
      view.post (new Runnable () {
	  @Override
	  public void
	  run ()
	  {
	    if (cursor != null)
	      view.setPointerIcon (cursor.icon);
	    else
	      view.setPointerIcon (null);
	  }
	});
  }

  public synchronized void
  notifyContentRectPosition (int xPosition, int yPosition)
  {
    Rect geometry;

    /* Ignore these notifications if not a child of the root
       window.  */
    if (parent != null)
      return;

    /* xPosition and yPosition are the position of this window
       relative to the screen.  Set them and request a ConfigureNotify
       event.  */

    if (this.xPosition != xPosition
	|| this.yPosition != yPosition)
      {
	this.xPosition = xPosition;
	this.yPosition = yPosition;

	EmacsNative.sendConfigureNotify (this.handle,
					 System.currentTimeMillis (),
					 xPosition, yPosition,
					 rect.width (), rect.height ());
      }
  }



  /* Drag and drop.

     Android 7.0 and later permit multiple windows to be juxtaposed
     on-screen, consequently enabling items selected from one window
     to be dragged onto another.  Data is transferred across program
     boundaries using ClipData items, much the same way clipboard data
     is transferred.

     When an item is dropped, Emacs must ascertain whether the clip
     data represents plain text, a content URI incorporating a file,
     or some other data.  This is implemented by examining the clip
     data's ``description'', which enumerates each of the MIME data
     types the clip data is capable of providing data in.

     If the clip data represents plain text, then that text is copied
     into a string and conveyed to Lisp code.  Otherwise, Emacs must
     solicit rights to access the URI from the system, absent which it
     is accounted plain text and reinterpreted as such, to cue the
     user that something has gone awry.

     Moreover, events are regularly sent as the item being dragged
     travels across the frame, even if it might not be dropped.  This
     facilitates cursor motion and scrolling in response, as provided
     by the options dnd-indicate-insertion-point and
     dnd-scroll-margin.  */

  /* Register the drag and drop event EVENT.  */

  public boolean
  onDragEvent (DragEvent event)
  {
    ClipData data;
    ClipDescription description;
    int i, j, x, y, itemCount;
    String type, uriString;
    Uri uri;
    EmacsActivity activity;
    StringBuilder builder;
    ContentResolver resolver;

    x = (int) event.getX ();
    y = (int) event.getY ();

    switch (event.getAction ())
      {
      case DragEvent.ACTION_DRAG_STARTED:
	/* Return true to continue the drag and drop operation.  */
	return true;

      case DragEvent.ACTION_DRAG_LOCATION:
	/* Send this drag motion event to Emacs.  Skip this when the
	   integer position hasn't changed, for Android sends events
	   even if the movement from the previous position of the drag
	   is less than 1 pixel on either axis.  */

	if (x != dndXPosition || y != dndYPosition)
	  {
	    EmacsNative.sendDndDrag (handle, x, y);
	    dndXPosition = x;
	    dndYPosition = y;
	  }

	return true;

      case DragEvent.ACTION_DROP:
	/* Reset this view's record of the previous drag and drop
	   event's position.  */
	dndXPosition = -1;
	dndYPosition = -1;

	/* Judge whether this is plain text, or if it's a file URI for
	   which permissions must be requested.  */

	data = event.getClipData ();
	description = data.getDescription ();
	itemCount = data.getItemCount ();

	/* If there are insufficient items within the clip data,
	   return false.  */

	if (itemCount < 1)
	  return false;

	/* Search for plain text data within the clipboard.  */

	for (i = 0; i < description.getMimeTypeCount (); ++i)
	  {
	    type = description.getMimeType (i);

	    if (type.equals (ClipDescription.MIMETYPE_TEXT_PLAIN)
		|| type.equals (ClipDescription.MIMETYPE_TEXT_HTML))
	      {
		/* The data being dropped is plain text; encode it
		   suitably and send it to the main thread.  */
		type = (data.getItemAt (0).coerceToText (EmacsService.SERVICE)
			.toString ());
		EmacsNative.sendDndText (handle, x, y, type);
		return true;
	      }
	    else if (type.equals (ClipDescription.MIMETYPE_TEXT_URILIST))
	      {
		/* The data being dropped is a list of URIs; encode it
		   suitably and send it to the main thread.  */
		type = (data.getItemAt (0).coerceToText (EmacsService.SERVICE)
			.toString ());
		EmacsNative.sendDndUri (handle, x, y, type);
		return true;
	      }
	  }

	/* There's no plain text data within this clipboard item, so
	   each item within should be treated as a content URI
	   designating a file.  */

	/* Collect the URIs into a string with each suffixed
	   by newlines, much as in a text/uri-list.  */
	builder = new StringBuilder ();

	for (i = 0; i < itemCount; ++i)
	  {
	    /* If the item dropped is a URI, send it to the
	       main thread.  */

	    uri = data.getItemAt (i).getUri ();

	    /* Attempt to acquire permissions for this URI;
	       failing which, insert it as text instead.  */

	    if (uri != null
		&& uri.getScheme () != null
		&& uri.getScheme ().equals ("content")
		&& (activity = EmacsActivity.lastFocusedActivity) != null)
	      {
		if ((activity.requestDragAndDropPermissions (event) == null))
		  uri = null;
		else
		  {
		    resolver = activity.getContentResolver ();

		    /* Substitute a content file name for the URI, if
		       possible.  */
		    uriString = EmacsService.buildContentName (uri, resolver);

		    if (uriString != null)
		      {
			builder.append (uriString).append ("\n");
			continue;
		      }
		  }
	      }

	    if (uri != null)
	      builder.append (uri.toString ()).append ("\n");
	    else
	      {
		/* Treat each URI that Emacs cannot secure
		   permissions for as plain text.  */
		type = (data.getItemAt (i)
			.coerceToText (EmacsService.SERVICE)
			.toString ());
		EmacsNative.sendDndText (handle, x, y, type);
	      }
	  }

	/* Now send each URI to Emacs.  */

	if (builder.length () > 0)
	  EmacsNative.sendDndUri (handle, x, y, builder.toString ());
	return true;

      default:
	/* Reset this view's record of the previous drag and drop
	   event's position.  */
	dndXPosition = -1;
	dndYPosition = -1;
      }

    return true;
  }



  /* Miscellaneous functions for debugging graphics code.  */

  /* Recreate the activity to which this window is attached, if any.
     This is nonfunctional on Android 2.3.7 and earlier.  */

  public void
  recreateActivity ()
  {
    final EmacsWindowAttachmentManager.WindowConsumer attached;

    attached = this.attached;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB)
      return;

    view.post (new Runnable () {
	@Override
	public void
	run ()
	{
	  if (attached instanceof EmacsActivity)
	    ((EmacsActivity) attached).recreate ();
	}
      });
  }
};