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
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
|
/*
* Copyright (C) 2007-2008 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package android.inputmethodservice;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Region;
import android.os.Bundle;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.InputType;
import android.text.Layout;
import android.text.Spannable;
import android.text.method.MovementMethod;
import android.util.Log;
import android.util.PrintWriterPrinter;
import android.util.Printer;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputBinding;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethod;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.InputMethodSubtype;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import java.io.FileDescriptor;
import java.io.PrintWriter;
/**
* InputMethodService provides a standard implementation of an InputMethod,
* which final implementations can derive from and customize. See the
* base class {@link AbstractInputMethodService} and the {@link InputMethod}
* interface for more information on the basics of writing input methods.
*
* <p>In addition to the normal Service lifecycle methods, this class
* introduces some new specific callbacks that most subclasses will want
* to make use of:</p>
* <ul>
* <li> {@link #onInitializeInterface()} for user-interface initialization,
* in particular to deal with configuration changes while the service is
* running.
* <li> {@link #onBindInput} to find out about switching to a new client.
* <li> {@link #onStartInput} to deal with an input session starting with
* the client.
* <li> {@link #onCreateInputView()}, {@link #onCreateCandidatesView()},
* and {@link #onCreateExtractTextView()} for non-demand generation of the UI.
* <li> {@link #onStartInputView(EditorInfo, boolean)} to deal with input
* starting within the input area of the IME.
* </ul>
*
* <p>An input method has significant discretion in how it goes about its
* work: the {@link android.inputmethodservice.InputMethodService} provides
* a basic framework for standard UI elements (input view, candidates view,
* and running in fullscreen mode), but it is up to a particular implementor
* to decide how to use them. For example, one input method could implement
* an input area with a keyboard, another could allow the user to draw text,
* while a third could have no input area (and thus not be visible to the
* user) but instead listen to audio and perform text to speech conversion.</p>
*
* <p>In the implementation provided here, all of these elements are placed
* together in a single window managed by the InputMethodService. It will
* execute callbacks as it needs information about them, and provides APIs for
* programmatic control over them. They layout of these elements is explicitly
* defined:</p>
*
* <ul>
* <li>The soft input view, if available, is placed at the bottom of the
* screen.
* <li>The candidates view, if currently shown, is placed above the soft
* input view.
* <li>If not running fullscreen, the application is moved or resized to be
* above these views; if running fullscreen, the window will completely cover
* the application and its top part will contain the extract text of what is
* currently being edited by the application.
* </ul>
*
*
* <a name="SoftInputView"></a>
* <h3>Soft Input View</h3>
*
* <p>Central to most input methods is the soft input view. This is where most
* user interaction occurs: pressing on soft keys, drawing characters, or
* however else your input method wants to generate text. Most implementations
* will simply have their own view doing all of this work, and return a new
* instance of it when {@link #onCreateInputView()} is called. At that point,
* as long as the input view is visible, you will see user interaction in
* that view and can call back on the InputMethodService to interact with the
* application as appropriate.</p>
*
* <p>There are some situations where you want to decide whether or not your
* soft input view should be shown to the user. This is done by implementing
* the {@link #onEvaluateInputViewShown()} to return true or false based on
* whether it should be shown in the current environment. If any of your
* state has changed that may impact this, call
* {@link #updateInputViewShown()} to have it re-evaluated. The default
* implementation always shows the input view unless there is a hard
* keyboard available, which is the appropriate behavior for most input
* methods.</p>
*
*
* <a name="CandidatesView"></a>
* <h3>Candidates View</h3>
*
* <p>Often while the user is generating raw text, an input method wants to
* provide them with a list of possible interpretations of that text that can
* be selected for use. This is accomplished with the candidates view, and
* like the soft input view you implement {@link #onCreateCandidatesView()}
* to instantiate your own view implementing your candidates UI.</p>
*
* <p>Management of the candidates view is a little different than the input
* view, because the candidates view tends to be more transient, being shown
* only when there are possible candidates for the current text being entered
* by the user. To control whether the candidates view is shown, you use
* {@link #setCandidatesViewShown(boolean)}. Note that because the candidate
* view tends to be shown and hidden a lot, it does not impact the application
* UI in the same way as the soft input view: it will never cause application
* windows to resize, only cause them to be panned if needed for the user to
* see the current focus.</p>
*
*
* <a name="FullscreenMode"></a>
* <h3>Fullscreen Mode</h3>
*
* <p>Sometimes your input method UI is too large to integrate with the
* application UI, so you just want to take over the screen. This is
* accomplished by switching to full-screen mode, causing the input method
* window to fill the entire screen and add its own "extracted text" editor
* showing the user the text that is being typed. Unlike the other UI elements,
* there is a standard implementation for the extract editor that you should
* not need to change. The editor is placed at the top of the IME, above the
* input and candidates views.</p>
*
* <p>Similar to the input view, you control whether the IME is running in
* fullscreen mode by implementing {@link #onEvaluateFullscreenMode()}
* to return true or false based on
* whether it should be fullscreen in the current environment. If any of your
* state has changed that may impact this, call
* {@link #updateFullscreenMode()} to have it re-evaluated. The default
* implementation selects fullscreen mode when the screen is in a landscape
* orientation, which is appropriate behavior for most input methods that have
* a significant input area.</p>
*
* <p>When in fullscreen mode, you have some special requirements because the
* user can not see the application UI. In particular, you should implement
* {@link #onDisplayCompletions(CompletionInfo[])} to show completions
* generated by your application, typically in your candidates view like you
* would normally show candidates.
*
*
* <a name="GeneratingText"></a>
* <h3>Generating Text</h3>
*
* <p>The key part of an IME is of course generating text for the application.
* This is done through calls to the
* {@link android.view.inputmethod.InputConnection} interface to the
* application, which can be retrieved from {@link #getCurrentInputConnection()}.
* This interface allows you to generate raw key events or, if the target
* supports it, directly edit in strings of candidates and committed text.</p>
*
* <p>Information about what the target is expected and supports can be found
* through the {@link android.view.inputmethod.EditorInfo} class, which is
* retrieved with {@link #getCurrentInputEditorInfo()} method. The most
* important part of this is {@link android.view.inputmethod.EditorInfo#inputType
* EditorInfo.inputType}; in particular, if this is
* {@link android.view.inputmethod.EditorInfo#TYPE_NULL EditorInfo.TYPE_NULL},
* then the target does not support complex edits and you need to only deliver
* raw key events to it. An input method will also want to look at other
* values here, to for example detect password mode, auto complete text views,
* phone number entry, etc.</p>
*
* <p>When the user switches between input targets, you will receive calls to
* {@link #onFinishInput()} and {@link #onStartInput(EditorInfo, boolean)}.
* You can use these to reset and initialize your input state for the current
* target. For example, you will often want to clear any input state, and
* update a soft keyboard to be appropriate for the new inputType.</p>
*
* @attr ref android.R.styleable#InputMethodService_imeFullscreenBackground
* @attr ref android.R.styleable#InputMethodService_imeExtractEnterAnimation
* @attr ref android.R.styleable#InputMethodService_imeExtractExitAnimation
*/
public class InputMethodService extends AbstractInputMethodService {
static final String TAG = "InputMethodService";
static final boolean DEBUG = false;
/**
* The back button will close the input window.
*/
public static final int BACK_DISPOSITION_DEFAULT = 0; // based on window
/**
* This input method will not consume the back key.
*/
public static final int BACK_DISPOSITION_WILL_NOT_DISMISS = 1; // back
/**
* This input method will consume the back key.
*/
public static final int BACK_DISPOSITION_WILL_DISMISS = 2; // down
/**
* @hide
* The IME is active. It may or may not be visible.
*/
public static final int IME_ACTIVE = 0x1;
/**
* @hide
* The IME is visible.
*/
public static final int IME_VISIBLE = 0x2;
InputMethodManager mImm;
int mTheme = 0;
LayoutInflater mInflater;
TypedArray mThemeAttrs;
View mRootView;
SoftInputWindow mWindow;
boolean mInitialized;
boolean mWindowCreated;
boolean mWindowAdded;
boolean mWindowVisible;
boolean mWindowWasVisible;
boolean mInShowWindow;
ViewGroup mFullscreenArea;
FrameLayout mExtractFrame;
FrameLayout mCandidatesFrame;
FrameLayout mInputFrame;
IBinder mToken;
InputBinding mInputBinding;
InputConnection mInputConnection;
boolean mInputStarted;
boolean mInputViewStarted;
boolean mCandidatesViewStarted;
InputConnection mStartedInputConnection;
EditorInfo mInputEditorInfo;
int mShowInputFlags;
boolean mShowInputRequested;
boolean mLastShowInputRequested;
int mCandidatesVisibility;
CompletionInfo[] mCurCompletions;
boolean mShowInputForced;
boolean mFullscreenApplied;
boolean mIsFullscreen;
View mExtractView;
boolean mExtractViewHidden;
ExtractEditText mExtractEditText;
ViewGroup mExtractAccessories;
Button mExtractAction;
ExtractedText mExtractedText;
int mExtractedToken;
View mInputView;
boolean mIsInputViewShown;
int mStatusIcon;
int mBackDisposition;
final Insets mTmpInsets = new Insets();
final int[] mTmpLocation = new int[2];
final ViewTreeObserver.OnComputeInternalInsetsListener mInsetsComputer =
new ViewTreeObserver.OnComputeInternalInsetsListener() {
public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo info) {
if (isExtractViewShown()) {
// In true fullscreen mode, we just say the window isn't covering
// any content so we don't impact whatever is behind.
View decor = getWindow().getWindow().getDecorView();
info.contentInsets.top = info.visibleInsets.top
= decor.getHeight();
info.touchableRegion.setEmpty();
info.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME);
} else {
onComputeInsets(mTmpInsets);
info.contentInsets.top = mTmpInsets.contentTopInsets;
info.visibleInsets.top = mTmpInsets.visibleTopInsets;
info.touchableRegion.set(mTmpInsets.touchableRegion);
info.setTouchableInsets(mTmpInsets.touchableInsets);
}
}
};
final View.OnClickListener mActionClickListener = new View.OnClickListener() {
public void onClick(View v) {
final EditorInfo ei = getCurrentInputEditorInfo();
final InputConnection ic = getCurrentInputConnection();
if (ei != null && ic != null) {
if (ei.actionId != 0) {
ic.performEditorAction(ei.actionId);
} else if ((ei.imeOptions&EditorInfo.IME_MASK_ACTION)
!= EditorInfo.IME_ACTION_NONE) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
}
}
}
};
/**
* Concrete implementation of
* {@link AbstractInputMethodService.AbstractInputMethodImpl} that provides
* all of the standard behavior for an input method.
*/
public class InputMethodImpl extends AbstractInputMethodImpl {
/**
* Take care of attaching the given window token provided by the system.
*/
public void attachToken(IBinder token) {
if (mToken == null) {
mToken = token;
mWindow.setToken(token);
}
}
/**
* Handle a new input binding, calling
* {@link InputMethodService#onBindInput InputMethodService.onBindInput()}
* when done.
*/
public void bindInput(InputBinding binding) {
mInputBinding = binding;
mInputConnection = binding.getConnection();
if (DEBUG) Log.v(TAG, "bindInput(): binding=" + binding
+ " ic=" + mInputConnection);
InputConnection ic = getCurrentInputConnection();
if (ic != null) ic.reportFullscreenMode(mIsFullscreen);
initialize();
onBindInput();
}
/**
* Clear the current input binding.
*/
public void unbindInput() {
if (DEBUG) Log.v(TAG, "unbindInput(): binding=" + mInputBinding
+ " ic=" + mInputConnection);
onUnbindInput();
mInputStarted = false;
mInputBinding = null;
mInputConnection = null;
}
public void startInput(InputConnection ic, EditorInfo attribute) {
if (DEBUG) Log.v(TAG, "startInput(): editor=" + attribute);
doStartInput(ic, attribute, false);
}
public void restartInput(InputConnection ic, EditorInfo attribute) {
if (DEBUG) Log.v(TAG, "restartInput(): editor=" + attribute);
doStartInput(ic, attribute, true);
}
/**
* Handle a request by the system to hide the soft input area.
*/
public void hideSoftInput(int flags, ResultReceiver resultReceiver) {
if (DEBUG) Log.v(TAG, "hideSoftInput()");
boolean wasVis = isInputViewShown();
mShowInputFlags = 0;
mShowInputRequested = false;
mShowInputForced = false;
hideWindow();
if (resultReceiver != null) {
resultReceiver.send(wasVis != isInputViewShown()
? InputMethodManager.RESULT_HIDDEN
: (wasVis ? InputMethodManager.RESULT_UNCHANGED_SHOWN
: InputMethodManager.RESULT_UNCHANGED_HIDDEN), null);
}
}
/**
* Handle a request by the system to show the soft input area.
*/
public void showSoftInput(int flags, ResultReceiver resultReceiver) {
if (DEBUG) Log.v(TAG, "showSoftInput()");
boolean wasVis = isInputViewShown();
mShowInputFlags = 0;
if (onShowInputRequested(flags, false)) {
showWindow(true);
}
// If user uses hard keyboard, IME button should always be shown.
boolean showing = onEvaluateInputViewShown();
mImm.setImeWindowStatus(mToken, IME_ACTIVE | (showing ? IME_VISIBLE : 0),
mBackDisposition);
if (resultReceiver != null) {
resultReceiver.send(wasVis != isInputViewShown()
? InputMethodManager.RESULT_SHOWN
: (wasVis ? InputMethodManager.RESULT_UNCHANGED_SHOWN
: InputMethodManager.RESULT_UNCHANGED_HIDDEN), null);
}
}
public void changeInputMethodSubtype(InputMethodSubtype subtype) {
onCurrentInputMethodSubtypeChanged(subtype);
}
}
/**
* Concrete implementation of
* {@link AbstractInputMethodService.AbstractInputMethodSessionImpl} that provides
* all of the standard behavior for an input method session.
*/
public class InputMethodSessionImpl extends AbstractInputMethodSessionImpl {
public void finishInput() {
if (!isEnabled()) {
return;
}
if (DEBUG) Log.v(TAG, "finishInput() in " + this);
doFinishInput();
}
/**
* Call {@link InputMethodService#onDisplayCompletions
* InputMethodService.onDisplayCompletions()}.
*/
public void displayCompletions(CompletionInfo[] completions) {
if (!isEnabled()) {
return;
}
mCurCompletions = completions;
onDisplayCompletions(completions);
}
/**
* Call {@link InputMethodService#onUpdateExtractedText
* InputMethodService.onUpdateExtractedText()}.
*/
public void updateExtractedText(int token, ExtractedText text) {
if (!isEnabled()) {
return;
}
onUpdateExtractedText(token, text);
}
/**
* Call {@link InputMethodService#onUpdateSelection
* InputMethodService.onUpdateSelection()}.
*/
public void updateSelection(int oldSelStart, int oldSelEnd,
int newSelStart, int newSelEnd,
int candidatesStart, int candidatesEnd) {
if (!isEnabled()) {
return;
}
InputMethodService.this.onUpdateSelection(oldSelStart, oldSelEnd,
newSelStart, newSelEnd, candidatesStart, candidatesEnd);
}
/**
* Call {@link InputMethodService#onUpdateCursor
* InputMethodService.onUpdateCursor()}.
*/
public void updateCursor(Rect newCursor) {
if (!isEnabled()) {
return;
}
InputMethodService.this.onUpdateCursor(newCursor);
}
/**
* Call {@link InputMethodService#onAppPrivateCommand
* InputMethodService.onAppPrivateCommand()}.
*/
public void appPrivateCommand(String action, Bundle data) {
if (!isEnabled()) {
return;
}
InputMethodService.this.onAppPrivateCommand(action, data);
}
/**
*
*/
public void toggleSoftInput(int showFlags, int hideFlags) {
InputMethodService.this.onToggleSoftInput(showFlags, hideFlags);
}
}
/**
* Information about where interesting parts of the input method UI appear.
*/
public static final class Insets {
/**
* This is the top part of the UI that is the main content. It is
* used to determine the basic space needed, to resize/pan the
* application behind. It is assumed that this inset does not
* change very much, since any change will cause a full resize/pan
* of the application behind. This value is relative to the top edge
* of the input method window.
*/
public int contentTopInsets;
/**
* This is the top part of the UI that is visibly covering the
* application behind it. This provides finer-grained control over
* visibility, allowing you to change it relatively frequently (such
* as hiding or showing candidates) without disrupting the underlying
* UI too much. For example, this will never resize the application
* UI, will only pan if needed to make the current focus visible, and
* will not aggressively move the pan position when this changes unless
* needed to make the focus visible. This value is relative to the top edge
* of the input method window.
*/
public int visibleTopInsets;
/**
* This is the region of the UI that is touchable. It is used when
* {@link #touchableInsets} is set to {@link #TOUCHABLE_INSETS_REGION}.
* The region should be specified relative to the origin of the window frame.
*/
public final Region touchableRegion = new Region();
/**
* Option for {@link #touchableInsets}: the entire window frame
* can be touched.
*/
public static final int TOUCHABLE_INSETS_FRAME
= ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_FRAME;
/**
* Option for {@link #touchableInsets}: the area inside of
* the content insets can be touched.
*/
public static final int TOUCHABLE_INSETS_CONTENT
= ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_CONTENT;
/**
* Option for {@link #touchableInsets}: the area inside of
* the visible insets can be touched.
*/
public static final int TOUCHABLE_INSETS_VISIBLE
= ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_VISIBLE;
/**
* Option for {@link #touchableInsets}: the region specified by
* {@link #touchableRegion} can be touched.
*/
public static final int TOUCHABLE_INSETS_REGION
= ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION;
/**
* Determine which area of the window is touchable by the user. May
* be one of: {@link #TOUCHABLE_INSETS_FRAME},
* {@link #TOUCHABLE_INSETS_CONTENT}, {@link #TOUCHABLE_INSETS_VISIBLE},
* or {@link #TOUCHABLE_INSETS_REGION}.
*/
public int touchableInsets;
}
/**
* You can call this to customize the theme used by your IME's window.
* This theme should typically be one that derives from
* {@link android.R.style#Theme_InputMethod}, which is the default theme
* you will get. This must be set before {@link #onCreate}, so you
* will typically call it in your constructor with the resource ID
* of your custom theme.
*/
@Override
public void setTheme(int theme) {
if (mWindow != null) {
throw new IllegalStateException("Must be called before onCreate()");
}
mTheme = theme;
}
@Override public void onCreate() {
mTheme = Resources.selectSystemTheme(mTheme,
getApplicationInfo().targetSdkVersion,
android.R.style.Theme_InputMethod, android.R.style.Theme_Holo_InputMethod);
super.setTheme(mTheme);
super.onCreate();
mImm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
mInflater = (LayoutInflater)getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mWindow = new SoftInputWindow(this, mTheme, mDispatcherState);
initViews();
mWindow.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT);
}
/**
* This is a hook that subclasses can use to perform initialization of
* their interface. It is called for you prior to any of your UI objects
* being created, both after the service is first created and after a
* configuration change happens.
*/
public void onInitializeInterface() {
}
void initialize() {
if (!mInitialized) {
mInitialized = true;
onInitializeInterface();
}
}
void initViews() {
mInitialized = false;
mWindowCreated = false;
mShowInputRequested = false;
mShowInputForced = false;
mThemeAttrs = obtainStyledAttributes(android.R.styleable.InputMethodService);
mRootView = mInflater.inflate(
com.android.internal.R.layout.input_method, null);
mWindow.setContentView(mRootView);
mRootView.getViewTreeObserver().addOnComputeInternalInsetsListener(mInsetsComputer);
if (Settings.System.getInt(getContentResolver(),
Settings.System.FANCY_IME_ANIMATIONS, 0) != 0) {
mWindow.getWindow().setWindowAnimations(
com.android.internal.R.style.Animation_InputMethodFancy);
}
mFullscreenArea = (ViewGroup)mRootView.findViewById(com.android.internal.R.id.fullscreenArea);
mExtractViewHidden = false;
mExtractFrame = (FrameLayout)mRootView.findViewById(android.R.id.extractArea);
mExtractView = null;
mExtractEditText = null;
mExtractAccessories = null;
mExtractAction = null;
mFullscreenApplied = false;
mCandidatesFrame = (FrameLayout)mRootView.findViewById(android.R.id.candidatesArea);
mInputFrame = (FrameLayout)mRootView.findViewById(android.R.id.inputArea);
mInputView = null;
mIsInputViewShown = false;
mExtractFrame.setVisibility(View.GONE);
mCandidatesVisibility = getCandidatesHiddenVisibility();
mCandidatesFrame.setVisibility(mCandidatesVisibility);
mInputFrame.setVisibility(View.GONE);
}
@Override public void onDestroy() {
super.onDestroy();
mRootView.getViewTreeObserver().removeOnComputeInternalInsetsListener(
mInsetsComputer);
if (mWindowAdded) {
mWindow.dismiss();
}
}
/**
* Take care of handling configuration changes. Subclasses of
* InputMethodService generally don't need to deal directly with
* this on their own; the standard implementation here takes care of
* regenerating the input method UI as a result of the configuration
* change, so you can rely on your {@link #onCreateInputView} and
* other methods being called as appropriate due to a configuration change.
*
* <p>When a configuration change does happen,
* {@link #onInitializeInterface()} is guaranteed to be called the next
* time prior to any of the other input or UI creation callbacks. The
* following will be called immediately depending if appropriate for current
* state: {@link #onStartInput} if input is active, and
* {@link #onCreateInputView} and {@link #onStartInputView} and related
* appropriate functions if the UI is displayed.
*/
@Override public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
boolean visible = mWindowVisible;
int showFlags = mShowInputFlags;
boolean showingInput = mShowInputRequested;
CompletionInfo[] completions = mCurCompletions;
initViews();
mInputViewStarted = false;
mCandidatesViewStarted = false;
if (mInputStarted) {
doStartInput(getCurrentInputConnection(),
getCurrentInputEditorInfo(), true);
}
if (visible) {
if (showingInput) {
// If we were last showing the soft keyboard, try to do so again.
if (onShowInputRequested(showFlags, true)) {
showWindow(true);
if (completions != null) {
mCurCompletions = completions;
onDisplayCompletions(completions);
}
} else {
hideWindow();
}
} else if (mCandidatesVisibility == View.VISIBLE) {
// If the candidates are currently visible, make sure the
// window is shown for them.
showWindow(false);
} else {
// Otherwise hide the window.
hideWindow();
}
// If user uses hard keyboard, IME button should always be shown.
boolean showing = onEvaluateInputViewShown();
mImm.setImeWindowStatus(mToken, IME_ACTIVE | (showing ? IME_VISIBLE : 0),
mBackDisposition);
}
}
/**
* Implement to return our standard {@link InputMethodImpl}. Subclasses
* can override to provide their own customized version.
*/
@Override
public AbstractInputMethodImpl onCreateInputMethodInterface() {
return new InputMethodImpl();
}
/**
* Implement to return our standard {@link InputMethodSessionImpl}. Subclasses
* can override to provide their own customized version.
*/
@Override
public AbstractInputMethodSessionImpl onCreateInputMethodSessionInterface() {
return new InputMethodSessionImpl();
}
public LayoutInflater getLayoutInflater() {
return mInflater;
}
public Dialog getWindow() {
return mWindow;
}
public void setBackDisposition(int disposition) {
mBackDisposition = disposition;
}
public int getBackDisposition() {
return mBackDisposition;
}
/**
* Return the maximum width, in pixels, available the input method.
* Input methods are positioned at the bottom of the screen and, unless
* running in fullscreen, will generally want to be as short as possible
* so should compute their height based on their contents. However, they
* can stretch as much as needed horizontally. The function returns to
* you the maximum amount of space available horizontally, which you can
* use if needed for UI placement.
*
* <p>In many cases this is not needed, you can just rely on the normal
* view layout mechanisms to position your views within the full horizontal
* space given to the input method.
*
* <p>Note that this value can change dynamically, in particular when the
* screen orientation changes.
*/
public int getMaxWidth() {
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getWidth();
}
/**
* Return the currently active InputBinding for the input method, or
* null if there is none.
*/
public InputBinding getCurrentInputBinding() {
return mInputBinding;
}
/**
* Retrieve the currently active InputConnection that is bound to
* the input method, or null if there is none.
*/
public InputConnection getCurrentInputConnection() {
InputConnection ic = mStartedInputConnection;
if (ic != null) {
return ic;
}
return mInputConnection;
}
public boolean getCurrentInputStarted() {
return mInputStarted;
}
public EditorInfo getCurrentInputEditorInfo() {
return mInputEditorInfo;
}
/**
* Re-evaluate whether the input method should be running in fullscreen
* mode, and update its UI if this has changed since the last time it
* was evaluated. This will call {@link #onEvaluateFullscreenMode()} to
* determine whether it should currently run in fullscreen mode. You
* can use {@link #isFullscreenMode()} to determine if the input method
* is currently running in fullscreen mode.
*/
public void updateFullscreenMode() {
boolean isFullscreen = mShowInputRequested && onEvaluateFullscreenMode();
boolean changed = mLastShowInputRequested != mShowInputRequested;
if (mIsFullscreen != isFullscreen || !mFullscreenApplied) {
changed = true;
mIsFullscreen = isFullscreen;
InputConnection ic = getCurrentInputConnection();
if (ic != null) ic.reportFullscreenMode(isFullscreen);
mFullscreenApplied = true;
initialize();
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)
mFullscreenArea.getLayoutParams();
if (isFullscreen) {
mFullscreenArea.setBackgroundDrawable(mThemeAttrs.getDrawable(
com.android.internal.R.styleable.InputMethodService_imeFullscreenBackground));
lp.height = 0;
lp.weight = 1;
} else {
mFullscreenArea.setBackgroundDrawable(null);
lp.height = LinearLayout.LayoutParams.WRAP_CONTENT;
lp.weight = 0;
}
((ViewGroup)mFullscreenArea.getParent()).updateViewLayout(
mFullscreenArea, lp);
if (isFullscreen) {
if (mExtractView == null) {
View v = onCreateExtractTextView();
if (v != null) {
setExtractView(v);
}
}
startExtractingText(false);
}
updateExtractFrameVisibility();
}
if (changed) {
onConfigureWindow(mWindow.getWindow(), isFullscreen,
!mShowInputRequested);
mLastShowInputRequested = mShowInputRequested;
}
}
/**
* Update the given window's parameters for the given mode. This is called
* when the window is first displayed and each time the fullscreen or
* candidates only mode changes.
*
* <p>The default implementation makes the layout for the window
* MATCH_PARENT x MATCH_PARENT when in fullscreen mode, and
* MATCH_PARENT x WRAP_CONTENT when in non-fullscreen mode.
*
* @param win The input method's window.
* @param isFullscreen If true, the window is running in fullscreen mode
* and intended to cover the entire application display.
* @param isCandidatesOnly If true, the window is only showing the
* candidates view and none of the rest of its UI. This is mutually
* exclusive with fullscreen mode.
*/
public void onConfigureWindow(Window win, boolean isFullscreen,
boolean isCandidatesOnly) {
if (isFullscreen) {
mWindow.getWindow().setLayout(MATCH_PARENT, MATCH_PARENT);
} else {
mWindow.getWindow().setLayout(MATCH_PARENT, WRAP_CONTENT);
}
}
/**
* Return whether the input method is <em>currently</em> running in
* fullscreen mode. This is the mode that was last determined and
* applied by {@link #updateFullscreenMode()}.
*/
public boolean isFullscreenMode() {
return mIsFullscreen;
}
/**
* Override this to control when the input method should run in
* fullscreen mode. The default implementation runs in fullsceen only
* when the screen is in landscape mode. If you change what
* this returns, you will need to call {@link #updateFullscreenMode()}
* yourself whenever the returned value may have changed to have it
* re-evaluated and applied.
*/
public boolean onEvaluateFullscreenMode() {
Configuration config = getResources().getConfiguration();
if (config.orientation != Configuration.ORIENTATION_LANDSCAPE) {
return false;
}
if (mInputEditorInfo != null
&& (mInputEditorInfo.imeOptions & EditorInfo.IME_FLAG_NO_FULLSCREEN) != 0) {
return false;
}
return true;
}
/**
* Controls the visibility of the extracted text area. This only applies
* when the input method is in fullscreen mode, and thus showing extracted
* text. When false, the extracted text will not be shown, allowing some
* of the application to be seen behind. This is normally set for you
* by {@link #onUpdateExtractingVisibility}. This controls the visibility
* of both the extracted text and candidate view; the latter since it is
* not useful if there is no text to see.
*/
public void setExtractViewShown(boolean shown) {
if (mExtractViewHidden == shown) {
mExtractViewHidden = !shown;
updateExtractFrameVisibility();
}
}
/**
* Return whether the fullscreen extract view is shown. This will only
* return true if {@link #isFullscreenMode()} returns true, and in that
* case its value depends on the last call to
* {@link #setExtractViewShown(boolean)}. This effectively lets you
* determine if the application window is entirely covered (when this
* returns true) or if some part of it may be shown (if this returns
* false, though if {@link #isFullscreenMode()} returns true in that case
* then it is probably only a sliver of the application).
*/
public boolean isExtractViewShown() {
return mIsFullscreen && !mExtractViewHidden;
}
void updateExtractFrameVisibility() {
int vis;
if (isFullscreenMode()) {
vis = mExtractViewHidden ? View.INVISIBLE : View.VISIBLE;
mExtractFrame.setVisibility(View.VISIBLE);
} else {
vis = View.VISIBLE;
mExtractFrame.setVisibility(View.GONE);
}
updateCandidatesVisibility(mCandidatesVisibility == View.VISIBLE);
if (mWindowWasVisible && mFullscreenArea.getVisibility() != vis) {
int animRes = mThemeAttrs.getResourceId(vis == View.VISIBLE
? com.android.internal.R.styleable.InputMethodService_imeExtractEnterAnimation
: com.android.internal.R.styleable.InputMethodService_imeExtractExitAnimation,
0);
if (animRes != 0) {
mFullscreenArea.startAnimation(AnimationUtils.loadAnimation(
this, animRes));
}
}
mFullscreenArea.setVisibility(vis);
}
/**
* Compute the interesting insets into your UI. The default implementation
* uses the top of the candidates frame for the visible insets, and the
* top of the input frame for the content insets. The default touchable
* insets are {@link Insets#TOUCHABLE_INSETS_VISIBLE}.
*
* <p>Note that this method is not called when
* {@link #isExtractViewShown} returns true, since
* in that case the application is left as-is behind the input method and
* not impacted by anything in its UI.
*
* @param outInsets Fill in with the current UI insets.
*/
public void onComputeInsets(Insets outInsets) {
int[] loc = mTmpLocation;
if (mInputFrame.getVisibility() == View.VISIBLE) {
mInputFrame.getLocationInWindow(loc);
} else {
View decor = getWindow().getWindow().getDecorView();
loc[1] = decor.getHeight();
}
if (isFullscreenMode()) {
// In fullscreen mode, we never resize the underlying window.
View decor = getWindow().getWindow().getDecorView();
outInsets.contentTopInsets = decor.getHeight();
} else {
outInsets.contentTopInsets = loc[1];
}
if (mCandidatesFrame.getVisibility() == View.VISIBLE) {
mCandidatesFrame.getLocationInWindow(loc);
}
outInsets.visibleTopInsets = loc[1];
outInsets.touchableInsets = Insets.TOUCHABLE_INSETS_VISIBLE;
outInsets.touchableRegion.setEmpty();
}
/**
* Re-evaluate whether the soft input area should currently be shown, and
* update its UI if this has changed since the last time it
* was evaluated. This will call {@link #onEvaluateInputViewShown()} to
* determine whether the input view should currently be shown. You
* can use {@link #isInputViewShown()} to determine if the input view
* is currently shown.
*/
public void updateInputViewShown() {
boolean isShown = mShowInputRequested && onEvaluateInputViewShown();
if (mIsInputViewShown != isShown && mWindowVisible) {
mIsInputViewShown = isShown;
mInputFrame.setVisibility(isShown ? View.VISIBLE : View.GONE);
if (mInputView == null) {
initialize();
View v = onCreateInputView();
if (v != null) {
setInputView(v);
}
}
}
}
/**
* Returns true if we have been asked to show our input view.
*/
public boolean isShowInputRequested() {
return mShowInputRequested;
}
/**
* Return whether the soft input view is <em>currently</em> shown to the
* user. This is the state that was last determined and
* applied by {@link #updateInputViewShown()}.
*/
public boolean isInputViewShown() {
return mIsInputViewShown && mWindowVisible;
}
/**
* Override this to control when the soft input area should be shown to
* the user. The default implementation only shows the input view when
* there is no hard keyboard or the keyboard is hidden. If you change what
* this returns, you will need to call {@link #updateInputViewShown()}
* yourself whenever the returned value may have changed to have it
* re-evaluated and applied.
*/
public boolean onEvaluateInputViewShown() {
Configuration config = getResources().getConfiguration();
return config.keyboard == Configuration.KEYBOARD_NOKEYS
|| config.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES;
}
/**
* Controls the visibility of the candidates display area. By default
* it is hidden.
*/
public void setCandidatesViewShown(boolean shown) {
updateCandidatesVisibility(shown);
if (!mShowInputRequested && mWindowVisible != shown) {
// If we are being asked to show the candidates view while the app
// has not asked for the input view to be shown, then we need
// to update whether the window is shown.
if (shown) {
showWindow(false);
} else {
hideWindow();
}
}
}
void updateCandidatesVisibility(boolean shown) {
int vis = shown ? View.VISIBLE : getCandidatesHiddenVisibility();
if (mCandidatesVisibility != vis) {
mCandidatesFrame.setVisibility(vis);
mCandidatesVisibility = vis;
}
}
/**
* Returns the visibility mode (either {@link View#INVISIBLE View.INVISIBLE}
* or {@link View#GONE View.GONE}) of the candidates view when it is not
* shown. The default implementation returns GONE when
* {@link #isExtractViewShown} returns true,
* otherwise VISIBLE. Be careful if you change this to return GONE in
* other situations -- if showing or hiding the candidates view causes
* your window to resize, this can cause temporary drawing artifacts as
* the resize takes place.
*/
public int getCandidatesHiddenVisibility() {
return isExtractViewShown() ? View.GONE : View.INVISIBLE;
}
public void showStatusIcon(int iconResId) {
mStatusIcon = iconResId;
mImm.showStatusIcon(mToken, getPackageName(), iconResId);
}
public void hideStatusIcon() {
mStatusIcon = 0;
mImm.hideStatusIcon(mToken);
}
/**
* Force switch to a new input method, as identified by <var>id</var>. This
* input method will be destroyed, and the requested one started on the
* current input field.
*
* @param id Unique identifier of the new input method ot start.
*/
public void switchInputMethod(String id) {
mImm.setInputMethod(mToken, id);
}
public void setExtractView(View view) {
mExtractFrame.removeAllViews();
mExtractFrame.addView(view, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mExtractView = view;
if (view != null) {
mExtractEditText = (ExtractEditText)view.findViewById(
com.android.internal.R.id.inputExtractEditText);
mExtractEditText.setIME(this);
mExtractAction = (Button)view.findViewById(
com.android.internal.R.id.inputExtractAction);
if (mExtractAction != null) {
mExtractAccessories = (ViewGroup)view.findViewById(
com.android.internal.R.id.inputExtractAccessories);
}
startExtractingText(false);
} else {
mExtractEditText = null;
mExtractAccessories = null;
mExtractAction = null;
}
}
/**
* Replaces the current candidates view with a new one. You only need to
* call this when dynamically changing the view; normally, you should
* implement {@link #onCreateCandidatesView()} and create your view when
* first needed by the input method.
*/
public void setCandidatesView(View view) {
mCandidatesFrame.removeAllViews();
mCandidatesFrame.addView(view, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
}
/**
* Replaces the current input view with a new one. You only need to
* call this when dynamically changing the view; normally, you should
* implement {@link #onCreateInputView()} and create your view when
* first needed by the input method.
*/
public void setInputView(View view) {
mInputFrame.removeAllViews();
mInputFrame.addView(view, new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
mInputView = view;
}
/**
* Called by the framework to create the layout for showing extacted text.
* Only called when in fullscreen mode. The returned view hierarchy must
* have an {@link ExtractEditText} whose ID is
* {@link android.R.id#inputExtractEditText}.
*/
public View onCreateExtractTextView() {
return mInflater.inflate(
com.android.internal.R.layout.input_method_extract_view, null);
}
/**
* Create and return the view hierarchy used to show candidates. This will
* be called once, when the candidates are first displayed. You can return
* null to have no candidates view; the default implementation returns null.
*
* <p>To control when the candidates view is displayed, use
* {@link #setCandidatesViewShown(boolean)}.
* To change the candidates view after the first one is created by this
* function, use {@link #setCandidatesView(View)}.
*/
public View onCreateCandidatesView() {
return null;
}
/**
* Create and return the view hierarchy used for the input area (such as
* a soft keyboard). This will be called once, when the input area is
* first displayed. You can return null to have no input area; the default
* implementation returns null.
*
* <p>To control when the input view is displayed, implement
* {@link #onEvaluateInputViewShown()}.
* To change the input view after the first one is created by this
* function, use {@link #setInputView(View)}.
*/
public View onCreateInputView() {
return null;
}
/**
* Called when the input view is being shown and input has started on
* a new editor. This will always be called after {@link #onStartInput},
* allowing you to do your general setup there and just view-specific
* setup here. You are guaranteed that {@link #onCreateInputView()} will
* have been called some time before this function is called.
*
* @param info Description of the type of text being edited.
* @param restarting Set to true if we are restarting input on the
* same text field as before.
*/
public void onStartInputView(EditorInfo info, boolean restarting) {
}
/**
* Called when the input view is being hidden from the user. This will
* be called either prior to hiding the window, or prior to switching to
* another target for editing.
*
* <p>The default
* implementation uses the InputConnection to clear any active composing
* text; you can override this (not calling the base class implementation)
* to perform whatever behavior you would like.
*
* @param finishingInput If true, {@link #onFinishInput} will be
* called immediately after.
*/
public void onFinishInputView(boolean finishingInput) {
if (!finishingInput) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.finishComposingText();
}
}
}
/**
* Called when only the candidates view has been shown for showing
* processing as the user enters text through a hard keyboard.
* This will always be called after {@link #onStartInput},
* allowing you to do your general setup there and just view-specific
* setup here. You are guaranteed that {@link #onCreateCandidatesView()}
* will have been called some time before this function is called.
*
* <p>Note that this will <em>not</em> be called when the input method
* is running in full editing mode, and thus receiving
* {@link #onStartInputView} to initiate that operation. This is only
* for the case when candidates are being shown while the input method
* editor is hidden but wants to show its candidates UI as text is
* entered through some other mechanism.
*
* @param info Description of the type of text being edited.
* @param restarting Set to true if we are restarting input on the
* same text field as before.
*/
public void onStartCandidatesView(EditorInfo info, boolean restarting) {
}
/**
* Called when the candidates view is being hidden from the user. This will
* be called either prior to hiding the window, or prior to switching to
* another target for editing.
*
* <p>The default
* implementation uses the InputConnection to clear any active composing
* text; you can override this (not calling the base class implementation)
* to perform whatever behavior you would like.
*
* @param finishingInput If true, {@link #onFinishInput} will be
* called immediately after.
*/
public void onFinishCandidatesView(boolean finishingInput) {
if (!finishingInput) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.finishComposingText();
}
}
}
/**
* The system has decided that it may be time to show your input method.
* This is called due to a corresponding call to your
* {@link InputMethod#showSoftInput InputMethod.showSoftInput()}
* method. The default implementation uses
* {@link #onEvaluateInputViewShown()}, {@link #onEvaluateFullscreenMode()},
* and the current configuration to decide whether the input view should
* be shown at this point.
*
* @param flags Provides additional information about the show request,
* as per {@link InputMethod#showSoftInput InputMethod.showSoftInput()}.
* @param configChange This is true if we are re-showing due to a
* configuration change.
* @return Returns true to indicate that the window should be shown.
*/
public boolean onShowInputRequested(int flags, boolean configChange) {
if (!onEvaluateInputViewShown()) {
return false;
}
if ((flags&InputMethod.SHOW_EXPLICIT) == 0) {
if (!configChange && onEvaluateFullscreenMode()) {
// Don't show if this is not explicitly requested by the user and
// the input method is fullscreen. That would be too disruptive.
// However, we skip this change for a config change, since if
// the IME is already shown we do want to go into fullscreen
// mode at this point.
return false;
}
Configuration config = getResources().getConfiguration();
if (config.keyboard != Configuration.KEYBOARD_NOKEYS) {
// And if the device has a hard keyboard, even if it is
// currently hidden, don't show the input method implicitly.
// These kinds of devices don't need it that much.
return false;
}
}
if ((flags&InputMethod.SHOW_FORCED) != 0) {
mShowInputForced = true;
}
return true;
}
public void showWindow(boolean showInput) {
if (DEBUG) Log.v(TAG, "Showing window: showInput=" + showInput
+ " mShowInputRequested=" + mShowInputRequested
+ " mWindowAdded=" + mWindowAdded
+ " mWindowCreated=" + mWindowCreated
+ " mWindowVisible=" + mWindowVisible
+ " mInputStarted=" + mInputStarted);
if (mInShowWindow) {
Log.w(TAG, "Re-entrance in to showWindow");
return;
}
try {
mWindowWasVisible = mWindowVisible;
mInShowWindow = true;
showWindowInner(showInput);
} finally {
mWindowWasVisible = true;
mInShowWindow = false;
}
}
void showWindowInner(boolean showInput) {
boolean doShowInput = false;
boolean wasVisible = mWindowVisible;
mWindowVisible = true;
if (!mShowInputRequested) {
if (mInputStarted) {
if (showInput) {
doShowInput = true;
mShowInputRequested = true;
}
}
} else {
showInput = true;
}
if (DEBUG) Log.v(TAG, "showWindow: updating UI");
initialize();
updateFullscreenMode();
updateInputViewShown();
if (!mWindowAdded || !mWindowCreated) {
mWindowAdded = true;
mWindowCreated = true;
initialize();
if (DEBUG) Log.v(TAG, "CALL: onCreateCandidatesView");
View v = onCreateCandidatesView();
if (DEBUG) Log.v(TAG, "showWindow: candidates=" + v);
if (v != null) {
setCandidatesView(v);
}
}
if (mShowInputRequested) {
if (!mInputViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onStartInputView");
mInputViewStarted = true;
onStartInputView(mInputEditorInfo, false);
}
} else if (!mCandidatesViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onStartCandidatesView");
mCandidatesViewStarted = true;
onStartCandidatesView(mInputEditorInfo, false);
}
if (doShowInput) {
startExtractingText(false);
}
if (!wasVisible) {
if (DEBUG) Log.v(TAG, "showWindow: showing!");
mImm.setImeWindowStatus(mToken, IME_ACTIVE, mBackDisposition);
onWindowShown();
mWindow.show();
}
}
public void hideWindow() {
if (mInputViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onFinishInputView");
onFinishInputView(false);
} else if (mCandidatesViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onFinishCandidatesView");
onFinishCandidatesView(false);
}
mInputViewStarted = false;
mCandidatesViewStarted = false;
mImm.setImeWindowStatus(mToken, 0, mBackDisposition);
if (mWindowVisible) {
mWindow.hide();
mWindowVisible = false;
onWindowHidden();
mWindowWasVisible = false;
}
}
/**
* Called when the input method window has been shown to the user, after
* previously not being visible. This is done after all of the UI setup
* for the window has occurred (creating its views etc).
*/
public void onWindowShown() {
}
/**
* Called when the input method window has been hidden from the user,
* after previously being visible.
*/
public void onWindowHidden() {
}
/**
* Called when a new client has bound to the input method. This
* may be followed by a series of {@link #onStartInput(EditorInfo, boolean)}
* and {@link #onFinishInput()} calls as the user navigates through its
* UI. Upon this call you know that {@link #getCurrentInputBinding}
* and {@link #getCurrentInputConnection} return valid objects.
*/
public void onBindInput() {
}
/**
* Called when the previous bound client is no longer associated
* with the input method. After returning {@link #getCurrentInputBinding}
* and {@link #getCurrentInputConnection} will no longer return
* valid objects.
*/
public void onUnbindInput() {
}
/**
* Called to inform the input method that text input has started in an
* editor. You should use this callback to initialize the state of your
* input to match the state of the editor given to it.
*
* @param attribute The attributes of the editor that input is starting
* in.
* @param restarting Set to true if input is restarting in the same
* editor such as because the application has changed the text in
* the editor. Otherwise will be false, indicating this is a new
* session with the editor.
*/
public void onStartInput(EditorInfo attribute, boolean restarting) {
}
void doFinishInput() {
if (mInputViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onFinishInputView");
onFinishInputView(true);
} else if (mCandidatesViewStarted) {
if (DEBUG) Log.v(TAG, "CALL: onFinishCandidatesView");
onFinishCandidatesView(true);
}
mInputViewStarted = false;
mCandidatesViewStarted = false;
if (mInputStarted) {
if (DEBUG) Log.v(TAG, "CALL: onFinishInput");
onFinishInput();
}
mInputStarted = false;
mStartedInputConnection = null;
mCurCompletions = null;
}
void doStartInput(InputConnection ic, EditorInfo attribute, boolean restarting) {
if (!restarting) {
doFinishInput();
}
mInputStarted = true;
mStartedInputConnection = ic;
mInputEditorInfo = attribute;
initialize();
if (DEBUG) Log.v(TAG, "CALL: onStartInput");
onStartInput(attribute, restarting);
if (mWindowVisible) {
if (mShowInputRequested) {
if (DEBUG) Log.v(TAG, "CALL: onStartInputView");
mInputViewStarted = true;
onStartInputView(mInputEditorInfo, restarting);
startExtractingText(true);
} else if (mCandidatesVisibility == View.VISIBLE) {
if (DEBUG) Log.v(TAG, "CALL: onStartCandidatesView");
mCandidatesViewStarted = true;
onStartCandidatesView(mInputEditorInfo, restarting);
}
}
}
/**
* Called to inform the input method that text input has finished in
* the last editor. At this point there may be a call to
* {@link #onStartInput(EditorInfo, boolean)} to perform input in a
* new editor, or the input method may be left idle. This method is
* <em>not</em> called when input restarts in the same editor.
*
* <p>The default
* implementation uses the InputConnection to clear any active composing
* text; you can override this (not calling the base class implementation)
* to perform whatever behavior you would like.
*/
public void onFinishInput() {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.finishComposingText();
}
}
/**
* Called when the application has reported auto-completion candidates that
* it would like to have the input method displayed. Typically these are
* only used when an input method is running in full-screen mode, since
* otherwise the user can see and interact with the pop-up window of
* completions shown by the application.
*
* <p>The default implementation here does nothing.
*/
public void onDisplayCompletions(CompletionInfo[] completions) {
}
/**
* Called when the application has reported new extracted text to be shown
* due to changes in its current text state. The default implementation
* here places the new text in the extract edit text, when the input
* method is running in fullscreen mode.
*/
public void onUpdateExtractedText(int token, ExtractedText text) {
if (mExtractedToken != token) {
return;
}
if (text != null) {
if (mExtractEditText != null) {
mExtractedText = text;
mExtractEditText.setExtractedText(text);
}
}
}
/**
* Called when the application has reported a new selection region of
* the text. This is called whether or not the input method has requested
* extracted text updates, although if so it will not receive this call
* if the extracted text has changed as well.
*
* <p>The default implementation takes care of updating the cursor in
* the extract text, if it is being shown.
*/
public void onUpdateSelection(int oldSelStart, int oldSelEnd,
int newSelStart, int newSelEnd,
int candidatesStart, int candidatesEnd) {
final ExtractEditText eet = mExtractEditText;
if (eet != null && isFullscreenMode() && mExtractedText != null) {
final int off = mExtractedText.startOffset;
eet.startInternalChanges();
newSelStart -= off;
newSelEnd -= off;
final int len = eet.getText().length();
if (newSelStart < 0) newSelStart = 0;
else if (newSelStart > len) newSelStart = len;
if (newSelEnd < 0) newSelEnd = 0;
else if (newSelEnd > len) newSelEnd = len;
eet.setSelection(newSelStart, newSelEnd);
eet.finishInternalChanges();
}
}
/**
* Called when the application has reported a new location of its text
* cursor. This is only called if explicitly requested by the input method.
* The default implementation does nothing.
*/
public void onUpdateCursor(Rect newCursor) {
}
/**
* Close this input method's soft input area, removing it from the display.
* The input method will continue running, but the user can no longer use
* it to generate input by touching the screen.
* @param flags Provides additional operating flags. Currently may be
* 0 or have the {@link InputMethodManager#HIDE_IMPLICIT_ONLY
* InputMethodManager.HIDE_IMPLICIT_ONLY} bit set.
*/
public void requestHideSelf(int flags) {
mImm.hideSoftInputFromInputMethod(mToken, flags);
}
/**
* Show the input method. This is a call back to the
* IMF to handle showing the input method.
* Close this input method's soft input area, removing it from the display.
* The input method will continue running, but the user can no longer use
* it to generate input by touching the screen.
* @param flags Provides additional operating flags. Currently may be
* 0 or have the {@link InputMethodManager#SHOW_FORCED
* InputMethodManager.} bit set.
*/
private void requestShowSelf(int flags) {
mImm.showSoftInputFromInputMethod(mToken, flags);
}
private boolean handleBack(boolean doIt) {
if (mShowInputRequested) {
// If the soft input area is shown, back closes it and we
// consume the back key.
if (doIt) requestHideSelf(0);
return true;
} else if (mWindowVisible) {
if (mCandidatesVisibility == View.VISIBLE) {
// If we are showing candidates even if no input area, then
// hide them.
if (doIt) setCandidatesViewShown(false);
} else {
// If we have the window visible for some other reason --
// most likely to show candidates -- then just get rid
// of it. This really shouldn't happen, but just in case...
if (doIt) hideWindow();
}
return true;
}
return false;
}
/**
* Override this to intercept key down events before they are processed by the
* application. If you return true, the application will not itself
* process the event. If you return true, the normal application processing
* will occur as if the IME had not seen the event at all.
*
* <p>The default implementation intercepts {@link KeyEvent#KEYCODE_BACK
* KeyEvent.KEYCODE_BACK} if the IME is currently shown, to
* possibly hide it when the key goes up (if not canceled or long pressed). In
* addition, in fullscreen mode only, it will consume DPAD movement
* events to move the cursor in the extracted text view, not allowing
* them to perform navigation in the underlying application.
*/
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
if (handleBack(false)) {
event.startTracking();
return true;
}
return false;
}
return doMovementKey(keyCode, event, MOVEMENT_DOWN);
}
/**
* Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
* KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
* the event).
*/
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
return false;
}
/**
* Override this to intercept special key multiple events before they are
* processed by the
* application. If you return true, the application will not itself
* process the event. If you return true, the normal application processing
* will occur as if the IME had not seen the event at all.
*
* <p>The default implementation always returns false, except when
* in fullscreen mode, where it will consume DPAD movement
* events to move the cursor in the extracted text view, not allowing
* them to perform navigation in the underlying application.
*/
public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
return doMovementKey(keyCode, event, count);
}
/**
* Override this to intercept key up events before they are processed by the
* application. If you return true, the application will not itself
* process the event. If you return true, the normal application processing
* will occur as if the IME had not seen the event at all.
*
* <p>The default implementation intercepts {@link KeyEvent#KEYCODE_BACK
* KeyEvent.KEYCODE_BACK} to hide the current IME UI if it is shown. In
* addition, in fullscreen mode only, it will consume DPAD movement
* events to move the cursor in the extracted text view, not allowing
* them to perform navigation in the underlying application.
*/
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && event.isTracking()
&& !event.isCanceled()) {
return handleBack(true);
}
return doMovementKey(keyCode, event, MOVEMENT_UP);
}
@Override
public boolean onTrackballEvent(MotionEvent event) {
return false;
}
public void onAppPrivateCommand(String action, Bundle data) {
}
/**
* Handle a request by the system to toggle the soft input area.
*/
private void onToggleSoftInput(int showFlags, int hideFlags) {
if (DEBUG) Log.v(TAG, "toggleSoftInput()");
if (isInputViewShown()) {
requestHideSelf(hideFlags);
} else {
requestShowSelf(showFlags);
}
}
static final int MOVEMENT_DOWN = -1;
static final int MOVEMENT_UP = -2;
void reportExtractedMovement(int keyCode, int count) {
int dx = 0, dy = 0;
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
dx = -count;
break;
case KeyEvent.KEYCODE_DPAD_RIGHT:
dx = count;
break;
case KeyEvent.KEYCODE_DPAD_UP:
dy = -count;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
dy = count;
break;
}
onExtractedCursorMovement(dx, dy);
}
boolean doMovementKey(int keyCode, KeyEvent event, int count) {
final ExtractEditText eet = mExtractEditText;
if (isExtractViewShown() && isInputViewShown() && eet != null) {
// If we are in fullscreen mode, the cursor will move around
// the extract edit text, but should NOT cause focus to move
// to other fields.
MovementMethod movement = eet.getMovementMethod();
Layout layout = eet.getLayout();
if (movement != null && layout != null) {
// We want our own movement method to handle the key, so the
// cursor will properly move in our own word wrapping.
if (count == MOVEMENT_DOWN) {
if (movement.onKeyDown(eet,
(Spannable)eet.getText(), keyCode, event)) {
reportExtractedMovement(keyCode, 1);
return true;
}
} else if (count == MOVEMENT_UP) {
if (movement.onKeyUp(eet,
(Spannable)eet.getText(), keyCode, event)) {
return true;
}
} else {
if (movement.onKeyOther(eet, (Spannable)eet.getText(), event)) {
reportExtractedMovement(keyCode, count);
} else {
KeyEvent down = KeyEvent.changeAction(event, KeyEvent.ACTION_DOWN);
if (movement.onKeyDown(eet,
(Spannable)eet.getText(), keyCode, down)) {
KeyEvent up = KeyEvent.changeAction(event, KeyEvent.ACTION_UP);
movement.onKeyUp(eet,
(Spannable)eet.getText(), keyCode, up);
while (--count > 0) {
movement.onKeyDown(eet,
(Spannable)eet.getText(), keyCode, down);
movement.onKeyUp(eet,
(Spannable)eet.getText(), keyCode, up);
}
reportExtractedMovement(keyCode, count);
}
}
}
}
// Regardless of whether the movement method handled the key,
// we never allow DPAD navigation to the application.
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_DPAD_DOWN:
return true;
}
}
return false;
}
/**
* Send the given key event code (as defined by {@link KeyEvent}) to the
* current input connection is a key down + key up event pair. The sent
* events have {@link KeyEvent#FLAG_SOFT_KEYBOARD KeyEvent.FLAG_SOFT_KEYBOARD}
* set, so that the recipient can identify them as coming from a software
* input method, and
* {@link KeyEvent#FLAG_KEEP_TOUCH_MODE KeyEvent.FLAG_KEEP_TOUCH_MODE}, so
* that they don't impact the current touch mode of the UI.
*
* @param keyEventCode The raw key code to send, as defined by
* {@link KeyEvent}.
*/
public void sendDownUpKeyEvents(int keyEventCode) {
InputConnection ic = getCurrentInputConnection();
if (ic == null) return;
long eventTime = SystemClock.uptimeMillis();
ic.sendKeyEvent(new KeyEvent(eventTime, eventTime,
KeyEvent.ACTION_DOWN, keyEventCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
ic.sendKeyEvent(new KeyEvent(SystemClock.uptimeMillis(), eventTime,
KeyEvent.ACTION_UP, keyEventCode, 0, 0, KeyCharacterMap.VIRTUAL_KEYBOARD, 0,
KeyEvent.FLAG_SOFT_KEYBOARD|KeyEvent.FLAG_KEEP_TOUCH_MODE));
}
/**
* Ask the input target to execute its default action via
* {@link InputConnection#performEditorAction
* InputConnection.performEditorAction()}.
*
* @param fromEnterKey If true, this will be executed as if the user had
* pressed an enter key on the keyboard, that is it will <em>not</em>
* be done if the editor has set {@link EditorInfo#IME_FLAG_NO_ENTER_ACTION
* EditorInfo.IME_FLAG_NO_ENTER_ACTION}. If false, the action will be
* sent regardless of how the editor has set that flag.
*
* @return Returns a boolean indicating whether an action has been sent.
* If false, either the editor did not specify a default action or it
* does not want an action from the enter key. If true, the action was
* sent (or there was no input connection at all).
*/
public boolean sendDefaultEditorAction(boolean fromEnterKey) {
EditorInfo ei = getCurrentInputEditorInfo();
if (ei != null &&
(!fromEnterKey || (ei.imeOptions &
EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(ei.imeOptions & EditorInfo.IME_MASK_ACTION) !=
EditorInfo.IME_ACTION_NONE) {
// If the enter key was pressed, and the editor has a default
// action associated with pressing enter, then send it that
// explicit action instead of the key event.
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performEditorAction(ei.imeOptions&EditorInfo.IME_MASK_ACTION);
}
return true;
}
return false;
}
/**
* Send the given UTF-16 character to the current input connection. Most
* characters will be delivered simply by calling
* {@link InputConnection#commitText InputConnection.commitText()} with
* the character; some, however, may be handled different. In particular,
* the enter character ('\n') will either be delivered as an action code
* or a raw key event, as appropriate.
*
* @param charCode The UTF-16 character code to send.
*/
public void sendKeyChar(char charCode) {
switch (charCode) {
case '\n': // Apps may be listening to an enter key to perform an action
if (!sendDefaultEditorAction(true)) {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
break;
default:
// Make sure that digits go through any text watcher on the client side.
if (charCode >= '0' && charCode <= '9') {
sendDownUpKeyEvents(charCode - '0' + KeyEvent.KEYCODE_0);
} else {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.commitText(String.valueOf((char) charCode), 1);
}
}
break;
}
}
/**
* This is called when the user has moved the cursor in the extracted
* text view, when running in fullsreen mode. The default implementation
* performs the corresponding selection change on the underlying text
* editor.
*/
public void onExtractedSelectionChanged(int start, int end) {
InputConnection conn = getCurrentInputConnection();
if (conn != null) {
conn.setSelection(start, end);
}
}
/**
* This is called when the user has clicked on the extracted text view,
* when running in fullscreen mode. The default implementation hides
* the candidates view when this happens, but only if the extracted text
* editor has a vertical scroll bar because its text doesn't fit.
* Re-implement this to provide whatever behavior you want.
*/
public void onExtractedTextClicked() {
if (mExtractEditText == null) {
return;
}
if (mExtractEditText.hasVerticalScrollBar()) {
setCandidatesViewShown(false);
}
}
/**
* This is called when the user has performed a cursor movement in the
* extracted text view, when it is running in fullscreen mode. The default
* implementation hides the candidates view when a vertical movement
* happens, but only if the extracted text editor has a vertical scroll bar
* because its text doesn't fit.
* Re-implement this to provide whatever behavior you want.
* @param dx The amount of cursor movement in the x dimension.
* @param dy The amount of cursor movement in the y dimension.
*/
public void onExtractedCursorMovement(int dx, int dy) {
if (mExtractEditText == null || dy == 0) {
return;
}
if (mExtractEditText.hasVerticalScrollBar()) {
setCandidatesViewShown(false);
}
}
/**
* This is called when the user has selected a context menu item from the
* extracted text view, when running in fullscreen mode. The default
* implementation sends this action to the current InputConnection's
* {@link InputConnection#performContextMenuAction(int)}, for it
* to be processed in underlying "real" editor. Re-implement this to
* provide whatever behavior you want.
*/
public boolean onExtractTextContextMenuItem(int id) {
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.performContextMenuAction(id);
}
return true;
}
/**
* Return text that can be used as a button label for the given
* {@link EditorInfo#imeOptions EditorInfo.imeOptions}. Returns null
* if there is no action requested. Note that there is no guarantee that
* the returned text will be relatively short, so you probably do not
* want to use it as text on a soft keyboard key label.
*
* @param imeOptions The value from @link EditorInfo#imeOptions EditorInfo.imeOptions}.
*
* @return Returns a label to use, or null if there is no action.
*/
public CharSequence getTextForImeAction(int imeOptions) {
switch (imeOptions&EditorInfo.IME_MASK_ACTION) {
case EditorInfo.IME_ACTION_NONE:
return null;
case EditorInfo.IME_ACTION_GO:
return getText(com.android.internal.R.string.ime_action_go);
case EditorInfo.IME_ACTION_SEARCH:
return getText(com.android.internal.R.string.ime_action_search);
case EditorInfo.IME_ACTION_SEND:
return getText(com.android.internal.R.string.ime_action_send);
case EditorInfo.IME_ACTION_NEXT:
return getText(com.android.internal.R.string.ime_action_next);
case EditorInfo.IME_ACTION_DONE:
return getText(com.android.internal.R.string.ime_action_done);
case EditorInfo.IME_ACTION_PREVIOUS:
return getText(com.android.internal.R.string.ime_action_previous);
default:
return getText(com.android.internal.R.string.ime_action_default);
}
}
/**
* Called when the fullscreen-mode extracting editor info has changed,
* to determine whether the extracting (extract text and candidates) portion
* of the UI should be shown. The standard implementation hides or shows
* the extract area depending on whether it makes sense for the
* current editor. In particular, a {@link InputType#TYPE_NULL}
* input type or {@link EditorInfo#IME_FLAG_NO_EXTRACT_UI} flag will
* turn off the extract area since there is no text to be shown.
*/
public void onUpdateExtractingVisibility(EditorInfo ei) {
if (ei.inputType == InputType.TYPE_NULL ||
(ei.imeOptions&EditorInfo.IME_FLAG_NO_EXTRACT_UI) != 0) {
// No reason to show extract UI!
setExtractViewShown(false);
return;
}
setExtractViewShown(true);
}
/**
* Called when the fullscreen-mode extracting editor info has changed,
* to update the state of its UI such as the action buttons shown.
* You do not need to deal with this if you are using the standard
* full screen extract UI. If replacing it, you will need to re-implement
* this to put the appropriate action button in your own UI and handle it,
* and perform any other changes.
*
* <p>The standard implementation turns on or off its accessory area
* depending on whether there is an action button, and hides or shows
* the entire extract area depending on whether it makes sense for the
* current editor. In particular, a {@link InputType#TYPE_NULL} or
* {@link InputType#TYPE_TEXT_VARIATION_FILTER} input type will turn off the
* extract area since there is no text to be shown.
*/
public void onUpdateExtractingViews(EditorInfo ei) {
if (!isExtractViewShown()) {
return;
}
if (mExtractAccessories == null) {
return;
}
final boolean hasAction = ei.actionLabel != null || (
(ei.imeOptions&EditorInfo.IME_MASK_ACTION) != EditorInfo.IME_ACTION_NONE &&
(ei.imeOptions&EditorInfo.IME_FLAG_NO_ACCESSORY_ACTION) == 0 &&
ei.inputType != InputType.TYPE_NULL);
if (hasAction) {
mExtractAccessories.setVisibility(View.VISIBLE);
if (mExtractAction != null) {
if (ei.actionLabel != null) {
mExtractAction.setText(ei.actionLabel);
} else {
mExtractAction.setText(getTextForImeAction(ei.imeOptions));
}
mExtractAction.setOnClickListener(mActionClickListener);
}
} else {
mExtractAccessories.setVisibility(View.GONE);
if (mExtractAction != null) {
mExtractAction.setOnClickListener(null);
}
}
}
/**
* This is called when, while currently displayed in extract mode, the
* current input target changes. The default implementation will
* auto-hide the IME if the new target is not a full editor, since this
* can be an confusing experience for the user.
*/
public void onExtractingInputChanged(EditorInfo ei) {
if (ei.inputType == InputType.TYPE_NULL) {
requestHideSelf(InputMethodManager.HIDE_NOT_ALWAYS);
}
}
void startExtractingText(boolean inputChanged) {
final ExtractEditText eet = mExtractEditText;
if (eet != null && getCurrentInputStarted()
&& isFullscreenMode()) {
mExtractedToken++;
ExtractedTextRequest req = new ExtractedTextRequest();
req.token = mExtractedToken;
req.flags = InputConnection.GET_TEXT_WITH_STYLES;
req.hintMaxLines = 10;
req.hintMaxChars = 10000;
InputConnection ic = getCurrentInputConnection();
mExtractedText = ic == null? null
: ic.getExtractedText(req, InputConnection.GET_EXTRACTED_TEXT_MONITOR);
if (mExtractedText == null || ic == null) {
Log.e(TAG, "Unexpected null in startExtractingText : mExtractedText = "
+ mExtractedText + ", input connection = " + ic);
}
final EditorInfo ei = getCurrentInputEditorInfo();
try {
eet.startInternalChanges();
onUpdateExtractingVisibility(ei);
onUpdateExtractingViews(ei);
int inputType = ei.inputType;
if ((inputType&EditorInfo.TYPE_MASK_CLASS)
== EditorInfo.TYPE_CLASS_TEXT) {
if ((inputType&EditorInfo.TYPE_TEXT_FLAG_IME_MULTI_LINE) != 0) {
inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE;
}
}
eet.setInputType(inputType);
eet.setHint(ei.hintText);
if (mExtractedText != null) {
eet.setEnabled(true);
eet.setExtractedText(mExtractedText);
} else {
eet.setEnabled(false);
eet.setText("");
}
} finally {
eet.finishInternalChanges();
}
if (inputChanged) {
onExtractingInputChanged(ei);
}
}
}
// TODO: Handle the subtype change event
/**
* Called when the subtype was changed.
* @param newSubtype the subtype which is being changed to.
*/
protected void onCurrentInputMethodSubtypeChanged(InputMethodSubtype newSubtype) {
if (DEBUG) {
int nameResId = newSubtype.getNameResId();
String mode = newSubtype.getMode();
String output = "changeInputMethodSubtype:"
+ (nameResId == 0 ? "<none>" : getString(nameResId)) + ","
+ mode + ","
+ newSubtype.getLocale() + "," + newSubtype.getExtraValue();
Log.v(TAG, "--- " + output);
}
}
/**
* Performs a dump of the InputMethodService's internal state. Override
* to add your own information to the dump.
*/
@Override protected void dump(FileDescriptor fd, PrintWriter fout, String[] args) {
final Printer p = new PrintWriterPrinter(fout);
p.println("Input method service state for " + this + ":");
p.println(" mWindowCreated=" + mWindowCreated
+ " mWindowAdded=" + mWindowAdded);
p.println(" mWindowVisible=" + mWindowVisible
+ " mWindowWasVisible=" + mWindowWasVisible
+ " mInShowWindow=" + mInShowWindow);
p.println(" Configuration=" + getResources().getConfiguration());
p.println(" mToken=" + mToken);
p.println(" mInputBinding=" + mInputBinding);
p.println(" mInputConnection=" + mInputConnection);
p.println(" mStartedInputConnection=" + mStartedInputConnection);
p.println(" mInputStarted=" + mInputStarted
+ " mInputViewStarted=" + mInputViewStarted
+ " mCandidatesViewStarted=" + mCandidatesViewStarted);
if (mInputEditorInfo != null) {
p.println(" mInputEditorInfo:");
mInputEditorInfo.dump(p, " ");
} else {
p.println(" mInputEditorInfo: null");
}
p.println(" mShowInputRequested=" + mShowInputRequested
+ " mLastShowInputRequested=" + mLastShowInputRequested
+ " mShowInputForced=" + mShowInputForced
+ " mShowInputFlags=0x" + Integer.toHexString(mShowInputFlags));
p.println(" mCandidatesVisibility=" + mCandidatesVisibility
+ " mFullscreenApplied=" + mFullscreenApplied
+ " mIsFullscreen=" + mIsFullscreen
+ " mExtractViewHidden=" + mExtractViewHidden);
if (mExtractedText != null) {
p.println(" mExtractedText:");
p.println(" text=" + mExtractedText.text.length() + " chars"
+ " startOffset=" + mExtractedText.startOffset);
p.println(" selectionStart=" + mExtractedText.selectionStart
+ " selectionEnd=" + mExtractedText.selectionEnd
+ " flags=0x" + Integer.toHexString(mExtractedText.flags));
} else {
p.println(" mExtractedText: null");
}
p.println(" mExtractedToken=" + mExtractedToken);
p.println(" mIsInputViewShown=" + mIsInputViewShown
+ " mStatusIcon=" + mStatusIcon);
p.println("Last computed insets:");
p.println(" contentTopInsets=" + mTmpInsets.contentTopInsets
+ " visibleTopInsets=" + mTmpInsets.visibleTopInsets
+ " touchableInsets=" + mTmpInsets.touchableInsets
+ " touchableRegion=" + mTmpInsets.touchableRegion);
}
}
|