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
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
|
/*
* Copyright (C) 2012 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.widget;
import android.R;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.ClipData;
import android.content.ClipData.Item;
import android.content.Context;
import android.content.Intent;
import android.content.UndoManager;
import android.content.UndoOperation;
import android.content.UndoOwner;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.inputmethodservice.ExtractEditText;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ParcelableParcel;
import android.os.SystemClock;
import android.provider.Settings;
import android.text.DynamicLayout;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Layout;
import android.text.ParcelableSpan;
import android.text.Selection;
import android.text.SpanWatcher;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.StaticLayout;
import android.text.TextUtils;
import android.text.method.KeyListener;
import android.text.method.MetaKeyKeyListener;
import android.text.method.MovementMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.WordIterator;
import android.text.style.EasyEditSpan;
import android.text.style.SuggestionRangeSpan;
import android.text.style.SuggestionSpan;
import android.text.style.TextAppearanceSpan;
import android.text.style.URLSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.ActionMode;
import android.view.ActionMode.Callback;
import android.view.DisplayListCanvas;
import android.view.DragEvent;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.RenderNode;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.view.inputmethod.CorrectionInfo;
import android.view.inputmethod.CursorAnchorInfo;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.ExtractedText;
import android.view.inputmethod.ExtractedTextRequest;
import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView.Drawables;
import android.widget.TextView.OnEditorActionListener;
import com.android.internal.util.ArrayUtils;
import com.android.internal.util.GrowingArrayUtils;
import com.android.internal.view.menu.MenuBuilder;
import com.android.internal.widget.EditableInputConnection;
import java.text.BreakIterator;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
/**
* Helper class used by TextView to handle editable text views.
*
* @hide
*/
public class Editor {
private static final String TAG = "Editor";
private static final boolean DEBUG_UNDO = false;
static final int BLINK = 500;
private static final float[] TEMP_POSITION = new float[2];
private static int DRAG_SHADOW_MAX_TEXT_LENGTH = 20;
// Tag used when the Editor maintains its own separate UndoManager.
private static final String UNDO_OWNER_TAG = "Editor";
// Each Editor manages its own undo stack.
private final UndoManager mUndoManager = new UndoManager();
private UndoOwner mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this);
final UndoInputFilter mUndoInputFilter = new UndoInputFilter(this);
boolean mAllowUndo = true;
// Cursor Controllers.
InsertionPointCursorController mInsertionPointCursorController;
SelectionModifierCursorController mSelectionModifierCursorController;
ActionMode mSelectionActionMode;
boolean mInsertionControllerEnabled;
boolean mSelectionControllerEnabled;
// Used to highlight a word when it is corrected by the IME
CorrectionHighlighter mCorrectionHighlighter;
InputContentType mInputContentType;
InputMethodState mInputMethodState;
private static class TextDisplayList {
RenderNode displayList;
boolean isDirty;
public TextDisplayList(String name) {
isDirty = true;
displayList = RenderNode.create(name, null);
}
boolean needsRecord() { return isDirty || !displayList.isValid(); }
}
TextDisplayList[] mTextDisplayLists;
boolean mFrozenWithFocus;
boolean mSelectionMoved;
boolean mTouchFocusSelected;
KeyListener mKeyListener;
int mInputType = EditorInfo.TYPE_NULL;
boolean mDiscardNextActionUp;
boolean mIgnoreActionUpEvent;
long mShowCursor;
Blink mBlink;
boolean mCursorVisible = true;
boolean mSelectAllOnFocus;
boolean mTextIsSelectable;
CharSequence mError;
boolean mErrorWasChanged;
ErrorPopup mErrorPopup;
/**
* This flag is set if the TextView tries to display an error before it
* is attached to the window (so its position is still unknown).
* It causes the error to be shown later, when onAttachedToWindow()
* is called.
*/
boolean mShowErrorAfterAttach;
boolean mInBatchEditControllers;
boolean mShowSoftInputOnFocus = true;
boolean mPreserveDetachedSelection;
boolean mTemporaryDetach;
SuggestionsPopupWindow mSuggestionsPopupWindow;
SuggestionRangeSpan mSuggestionRangeSpan;
Runnable mShowSuggestionRunnable;
final Drawable[] mCursorDrawable = new Drawable[2];
int mCursorCount; // Current number of used mCursorDrawable: 0 (resource=0), 1 or 2 (split)
private Drawable mSelectHandleLeft;
private Drawable mSelectHandleRight;
private Drawable mSelectHandleCenter;
// Global listener that detects changes in the global position of the TextView
private PositionListener mPositionListener;
float mLastDownPositionX, mLastDownPositionY;
Callback mCustomSelectionActionModeCallback;
// Set when this TextView gained focus with some text selected. Will start selection mode.
boolean mCreatedWithASelection;
// The span controller helps monitoring the changes to which the Editor needs to react:
// - EasyEditSpans, for which we have some UI to display on attach and on hide
// - SelectionSpans, for which we need to call updateSelection if an IME is attached
private SpanController mSpanController;
WordIterator mWordIterator;
SpellChecker mSpellChecker;
// This word iterator is set with text and used to determine word boundaries
// when a user is selecting text.
private WordIterator mWordIteratorWithText;
// Indicate that the text in the word iterator needs to be updated.
private boolean mUpdateWordIteratorText;
private Rect mTempRect;
private TextView mTextView;
final CursorAnchorInfoNotifier mCursorAnchorInfoNotifier = new CursorAnchorInfoNotifier();
Editor(TextView textView) {
mTextView = textView;
// Synchronize the filter list, which places the undo input filter at the end.
mTextView.setFilters(mTextView.getFilters());
}
ParcelableParcel saveInstanceState() {
ParcelableParcel state = new ParcelableParcel(getClass().getClassLoader());
Parcel parcel = state.getParcel();
mUndoManager.saveInstanceState(parcel);
mUndoInputFilter.saveInstanceState(parcel);
return state;
}
void restoreInstanceState(ParcelableParcel state) {
Parcel parcel = state.getParcel();
mUndoManager.restoreInstanceState(parcel, state.getClassLoader());
mUndoInputFilter.restoreInstanceState(parcel);
// Re-associate this object as the owner of undo state.
mUndoOwner = mUndoManager.getOwner(UNDO_OWNER_TAG, this);
}
/**
* Forgets all undo and redo operations for this Editor.
*/
void forgetUndoRedo() {
UndoOwner[] owners = { mUndoOwner };
mUndoManager.forgetUndos(owners, -1 /* all */);
mUndoManager.forgetRedos(owners, -1 /* all */);
}
boolean canUndo() {
UndoOwner[] owners = { mUndoOwner };
return mAllowUndo && mUndoManager.countUndos(owners) > 0;
}
boolean canRedo() {
UndoOwner[] owners = { mUndoOwner };
return mAllowUndo && mUndoManager.countRedos(owners) > 0;
}
void undo() {
if (!mAllowUndo) {
return;
}
UndoOwner[] owners = { mUndoOwner };
mUndoManager.undo(owners, 1); // Undo 1 action.
}
void redo() {
if (!mAllowUndo) {
return;
}
UndoOwner[] owners = { mUndoOwner };
mUndoManager.redo(owners, 1); // Redo 1 action.
}
void onAttachedToWindow() {
if (mShowErrorAfterAttach) {
showError();
mShowErrorAfterAttach = false;
}
mTemporaryDetach = false;
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
// No need to create the controller.
// The get method will add the listener on controller creation.
if (mInsertionPointCursorController != null) {
observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
}
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
}
updateSpellCheckSpans(0, mTextView.getText().length(),
true /* create the spell checker if needed */);
if (mTextView.hasTransientState() &&
mTextView.getSelectionStart() != mTextView.getSelectionEnd()) {
// Since transient state is reference counted make sure it stays matched
// with our own calls to it for managing selection.
// The action mode callback will set this back again when/if the action mode starts.
mTextView.setHasTransientState(false);
// We had an active selection from before, start the selection mode.
startSelectionActionModeWithSelection();
}
getPositionListener().addSubscriber(mCursorAnchorInfoNotifier, true);
}
void onDetachedFromWindow() {
getPositionListener().removeSubscriber(mCursorAnchorInfoNotifier);
if (mError != null) {
hideError();
}
if (mBlink != null) {
mBlink.removeCallbacks(mBlink);
}
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.onDetached();
}
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.onDetached();
}
if (mShowSuggestionRunnable != null) {
mTextView.removeCallbacks(mShowSuggestionRunnable);
}
destroyDisplayListsData();
if (mSpellChecker != null) {
mSpellChecker.closeSession();
// Forces the creation of a new SpellChecker next time this window is created.
// Will handle the cases where the settings has been changed in the meantime.
mSpellChecker = null;
}
mPreserveDetachedSelection = true;
hideControllers();
mPreserveDetachedSelection = false;
mTemporaryDetach = false;
}
private void destroyDisplayListsData() {
if (mTextDisplayLists != null) {
for (int i = 0; i < mTextDisplayLists.length; i++) {
RenderNode displayList = mTextDisplayLists[i] != null
? mTextDisplayLists[i].displayList : null;
if (displayList != null && displayList.isValid()) {
displayList.destroyDisplayListData();
}
}
}
}
private void showError() {
if (mTextView.getWindowToken() == null) {
mShowErrorAfterAttach = true;
return;
}
if (mErrorPopup == null) {
LayoutInflater inflater = LayoutInflater.from(mTextView.getContext());
final TextView err = (TextView) inflater.inflate(
com.android.internal.R.layout.textview_hint, null);
final float scale = mTextView.getResources().getDisplayMetrics().density;
mErrorPopup = new ErrorPopup(err, (int)(200 * scale + 0.5f), (int)(50 * scale + 0.5f));
mErrorPopup.setFocusable(false);
// The user is entering text, so the input method is needed. We
// don't want the popup to be displayed on top of it.
mErrorPopup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
}
TextView tv = (TextView) mErrorPopup.getContentView();
chooseSize(mErrorPopup, mError, tv);
tv.setText(mError);
mErrorPopup.showAsDropDown(mTextView, getErrorX(), getErrorY());
mErrorPopup.fixDirection(mErrorPopup.isAboveAnchor());
}
public void setError(CharSequence error, Drawable icon) {
mError = TextUtils.stringOrSpannedString(error);
mErrorWasChanged = true;
if (mError == null) {
setErrorIcon(null);
if (mErrorPopup != null) {
if (mErrorPopup.isShowing()) {
mErrorPopup.dismiss();
}
mErrorPopup = null;
}
mShowErrorAfterAttach = false;
} else {
setErrorIcon(icon);
if (mTextView.isFocused()) {
showError();
}
}
}
private void setErrorIcon(Drawable icon) {
Drawables dr = mTextView.mDrawables;
if (dr == null) {
mTextView.mDrawables = dr = new Drawables(mTextView.getContext());
}
dr.setErrorDrawable(icon, mTextView);
mTextView.resetResolvedDrawables();
mTextView.invalidate();
mTextView.requestLayout();
}
private void hideError() {
if (mErrorPopup != null) {
if (mErrorPopup.isShowing()) {
mErrorPopup.dismiss();
}
}
mShowErrorAfterAttach = false;
}
/**
* Returns the X offset to make the pointy top of the error point
* at the middle of the error icon.
*/
private int getErrorX() {
/*
* The "25" is the distance between the point and the right edge
* of the background
*/
final float scale = mTextView.getResources().getDisplayMetrics().density;
final Drawables dr = mTextView.mDrawables;
final int layoutDirection = mTextView.getLayoutDirection();
int errorX;
int offset;
switch (layoutDirection) {
default:
case View.LAYOUT_DIRECTION_LTR:
offset = - (dr != null ? dr.mDrawableSizeRight : 0) / 2 + (int) (25 * scale + 0.5f);
errorX = mTextView.getWidth() - mErrorPopup.getWidth() -
mTextView.getPaddingRight() + offset;
break;
case View.LAYOUT_DIRECTION_RTL:
offset = (dr != null ? dr.mDrawableSizeLeft : 0) / 2 - (int) (25 * scale + 0.5f);
errorX = mTextView.getPaddingLeft() + offset;
break;
}
return errorX;
}
/**
* Returns the Y offset to make the pointy top of the error point
* at the bottom of the error icon.
*/
private int getErrorY() {
/*
* Compound, not extended, because the icon is not clipped
* if the text height is smaller.
*/
final int compoundPaddingTop = mTextView.getCompoundPaddingTop();
int vspace = mTextView.getBottom() - mTextView.getTop() -
mTextView.getCompoundPaddingBottom() - compoundPaddingTop;
final Drawables dr = mTextView.mDrawables;
final int layoutDirection = mTextView.getLayoutDirection();
int height;
switch (layoutDirection) {
default:
case View.LAYOUT_DIRECTION_LTR:
height = (dr != null ? dr.mDrawableHeightRight : 0);
break;
case View.LAYOUT_DIRECTION_RTL:
height = (dr != null ? dr.mDrawableHeightLeft : 0);
break;
}
int icontop = compoundPaddingTop + (vspace - height) / 2;
/*
* The "2" is the distance between the point and the top edge
* of the background.
*/
final float scale = mTextView.getResources().getDisplayMetrics().density;
return icontop + height - mTextView.getHeight() - (int) (2 * scale + 0.5f);
}
void createInputContentTypeIfNeeded() {
if (mInputContentType == null) {
mInputContentType = new InputContentType();
}
}
void createInputMethodStateIfNeeded() {
if (mInputMethodState == null) {
mInputMethodState = new InputMethodState();
}
}
boolean isCursorVisible() {
// The default value is true, even when there is no associated Editor
return mCursorVisible && mTextView.isTextEditable();
}
void prepareCursorControllers() {
boolean windowSupportsHandles = false;
ViewGroup.LayoutParams params = mTextView.getRootView().getLayoutParams();
if (params instanceof WindowManager.LayoutParams) {
WindowManager.LayoutParams windowParams = (WindowManager.LayoutParams) params;
windowSupportsHandles = windowParams.type < WindowManager.LayoutParams.FIRST_SUB_WINDOW
|| windowParams.type > WindowManager.LayoutParams.LAST_SUB_WINDOW;
}
boolean enabled = windowSupportsHandles && mTextView.getLayout() != null;
mInsertionControllerEnabled = enabled && isCursorVisible();
mSelectionControllerEnabled = enabled && mTextView.textCanBeSelected();
if (!mInsertionControllerEnabled) {
hideInsertionPointCursorController();
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.onDetached();
mInsertionPointCursorController = null;
}
}
if (!mSelectionControllerEnabled) {
stopSelectionActionMode();
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.onDetached();
mSelectionModifierCursorController = null;
}
}
}
void hideInsertionPointCursorController() {
if (mInsertionPointCursorController != null) {
mInsertionPointCursorController.hide();
}
}
/**
* Hides the insertion controller and stops text selection mode, hiding the selection controller
*/
void hideControllers() {
hideCursorControllers();
hideSpanControllers();
}
private void hideSpanControllers() {
if (mSpanController != null) {
mSpanController.hide();
}
}
private void hideCursorControllers() {
if (mSuggestionsPopupWindow != null && !mSuggestionsPopupWindow.isShowingUp()) {
// Should be done before hide insertion point controller since it triggers a show of it
mSuggestionsPopupWindow.hide();
}
hideInsertionPointCursorController();
stopSelectionActionMode();
}
/**
* Create new SpellCheckSpans on the modified region.
*/
private void updateSpellCheckSpans(int start, int end, boolean createSpellChecker) {
// Remove spans whose adjacent characters are text not punctuation
mTextView.removeAdjacentSuggestionSpans(start);
mTextView.removeAdjacentSuggestionSpans(end);
if (mTextView.isTextEditable() && mTextView.isSuggestionsEnabled() &&
!(mTextView instanceof ExtractEditText)) {
if (mSpellChecker == null && createSpellChecker) {
mSpellChecker = new SpellChecker(mTextView);
}
if (mSpellChecker != null) {
mSpellChecker.spellCheck(start, end);
}
}
}
void onScreenStateChanged(int screenState) {
switch (screenState) {
case View.SCREEN_STATE_ON:
resumeBlink();
break;
case View.SCREEN_STATE_OFF:
suspendBlink();
break;
}
}
private void suspendBlink() {
if (mBlink != null) {
mBlink.cancel();
}
}
private void resumeBlink() {
if (mBlink != null) {
mBlink.uncancel();
makeBlink();
}
}
void adjustInputType(boolean password, boolean passwordInputType,
boolean webPasswordInputType, boolean numberPasswordInputType) {
// mInputType has been set from inputType, possibly modified by mInputMethod.
// Specialize mInputType to [web]password if we have a text class and the original input
// type was a password.
if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_TEXT) {
if (password || passwordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_TEXT_VARIATION_PASSWORD;
}
if (webPasswordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_TEXT_VARIATION_WEB_PASSWORD;
}
} else if ((mInputType & EditorInfo.TYPE_MASK_CLASS) == EditorInfo.TYPE_CLASS_NUMBER) {
if (numberPasswordInputType) {
mInputType = (mInputType & ~(EditorInfo.TYPE_MASK_VARIATION))
| EditorInfo.TYPE_NUMBER_VARIATION_PASSWORD;
}
}
}
private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
int wid = tv.getPaddingLeft() + tv.getPaddingRight();
int ht = tv.getPaddingTop() + tv.getPaddingBottom();
int defaultWidthInPixels = mTextView.getResources().getDimensionPixelSize(
com.android.internal.R.dimen.textview_error_popup_default_width);
Layout l = new StaticLayout(text, tv.getPaint(), defaultWidthInPixels,
Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
float max = 0;
for (int i = 0; i < l.getLineCount(); i++) {
max = Math.max(max, l.getLineWidth(i));
}
/*
* Now set the popup size to be big enough for the text plus the border capped
* to DEFAULT_MAX_POPUP_WIDTH
*/
pop.setWidth(wid + (int) Math.ceil(max));
pop.setHeight(ht + l.getHeight());
}
void setFrame() {
if (mErrorPopup != null) {
TextView tv = (TextView) mErrorPopup.getContentView();
chooseSize(mErrorPopup, mError, tv);
mErrorPopup.update(mTextView, getErrorX(), getErrorY(),
mErrorPopup.getWidth(), mErrorPopup.getHeight());
}
}
/**
* Unlike {@link TextView#textCanBeSelected()}, this method is based on the <i>current</i> state
* of the TextView. textCanBeSelected() has to be true (this is one of the conditions to have
* a selection controller (see {@link #prepareCursorControllers()}), but this is not sufficient.
*/
private boolean canSelectText() {
return hasSelectionController() && mTextView.getText().length() != 0;
}
/**
* It would be better to rely on the input type for everything. A password inputType should have
* a password transformation. We should hence use isPasswordInputType instead of this method.
*
* We should:
* - Call setInputType in setKeyListener instead of changing the input type directly (which
* would install the correct transformation).
* - Refuse the installation of a non-password transformation in setTransformation if the input
* type is password.
*
* However, this is like this for legacy reasons and we cannot break existing apps. This method
* is useful since it matches what the user can see (obfuscated text or not).
*
* @return true if the current transformation method is of the password type.
*/
private boolean hasPasswordTransformationMethod() {
return mTextView.getTransformationMethod() instanceof PasswordTransformationMethod;
}
private int getWordStart(int offset) {
// FIXME - For this and similar methods we're not doing anything to check if there's
// a LocaleSpan in the text, this may be something we should try handling or checking for.
int retOffset = getWordIteratorWithText().getBeginning(offset);
if (retOffset == BreakIterator.DONE) retOffset = offset;
return retOffset;
}
private int getWordEnd(int offset, boolean includePunctuation) {
int retOffset = getWordIteratorWithText().getEnd(offset);
if (retOffset == BreakIterator.DONE) {
retOffset = offset;
} else if (includePunctuation) {
retOffset = handlePunctuation(retOffset);
}
return retOffset;
}
private boolean isEndBoundary(int offset) {
int thisEnd = getWordEnd(offset, false);
return offset == thisEnd;
}
private boolean isStartBoundary(int offset) {
int thisStart = getWordStart(offset);
return thisStart == offset;
}
private int handlePunctuation(int offset) {
// FIXME - Check with UX how repeated ending punctuation should be handled.
// FIXME - Check with UX if / how we would handle non sentence ending characters.
// FIXME - Consider punctuation in different languages.
CharSequence text = mTextView.getText();
if (offset < text.length()) {
int c = Character.codePointAt(text, offset);
if (c == 0x002e /* period */|| c == 0x003f /* question mark */
|| c == 0x0021 /* exclamation mark */) {
offset = Character.offsetByCodePoints(text, offset, 1);
}
}
return offset;
}
/**
* Adjusts selection to the word under last touch offset. Return true if the operation was
* successfully performed.
*/
private boolean selectCurrentWord() {
if (!canSelectText()) {
return false;
}
if (hasPasswordTransformationMethod()) {
// Always select all on a password field.
// Cut/copy menu entries are not available for passwords, but being able to select all
// is however useful to delete or paste to replace the entire content.
return mTextView.selectAllText();
}
int inputType = mTextView.getInputType();
int klass = inputType & InputType.TYPE_MASK_CLASS;
int variation = inputType & InputType.TYPE_MASK_VARIATION;
// Specific text field types: select the entire text for these
if (klass == InputType.TYPE_CLASS_NUMBER ||
klass == InputType.TYPE_CLASS_PHONE ||
klass == InputType.TYPE_CLASS_DATETIME ||
variation == InputType.TYPE_TEXT_VARIATION_URI ||
variation == InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS ||
variation == InputType.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS ||
variation == InputType.TYPE_TEXT_VARIATION_FILTER) {
return mTextView.selectAllText();
}
long lastTouchOffsets = getLastTouchOffsets();
final int minOffset = TextUtils.unpackRangeStartFromLong(lastTouchOffsets);
final int maxOffset = TextUtils.unpackRangeEndFromLong(lastTouchOffsets);
// Safety check in case standard touch event handling has been bypassed
if (minOffset < 0 || minOffset >= mTextView.getText().length()) return false;
if (maxOffset < 0 || maxOffset >= mTextView.getText().length()) return false;
int selectionStart, selectionEnd;
// If a URLSpan (web address, email, phone...) is found at that position, select it.
URLSpan[] urlSpans = ((Spanned) mTextView.getText()).
getSpans(minOffset, maxOffset, URLSpan.class);
if (urlSpans.length >= 1) {
URLSpan urlSpan = urlSpans[0];
selectionStart = ((Spanned) mTextView.getText()).getSpanStart(urlSpan);
selectionEnd = ((Spanned) mTextView.getText()).getSpanEnd(urlSpan);
} else {
// FIXME - We should check if there's a LocaleSpan in the text, this may be
// something we should try handling or checking for.
final WordIterator wordIterator = getWordIterator();
wordIterator.setCharSequence(mTextView.getText(), minOffset, maxOffset);
selectionStart = wordIterator.getBeginning(minOffset);
selectionEnd = wordIterator.getEnd(maxOffset);
if (selectionStart == BreakIterator.DONE || selectionEnd == BreakIterator.DONE ||
selectionStart == selectionEnd) {
// Possible when the word iterator does not properly handle the text's language
long range = getCharRange(minOffset);
selectionStart = TextUtils.unpackRangeStartFromLong(range);
selectionEnd = TextUtils.unpackRangeEndFromLong(range);
}
}
Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
return selectionEnd > selectionStart;
}
void onLocaleChanged() {
// Will be re-created on demand in getWordIterator with the proper new locale
mWordIterator = null;
mWordIteratorWithText = null;
}
/**
* @hide
*/
public WordIterator getWordIterator() {
if (mWordIterator == null) {
mWordIterator = new WordIterator(mTextView.getTextServicesLocale());
}
return mWordIterator;
}
private WordIterator getWordIteratorWithText() {
if (mWordIteratorWithText == null) {
mWordIteratorWithText = new WordIterator(mTextView.getTextServicesLocale());
mUpdateWordIteratorText = true;
}
if (mUpdateWordIteratorText) {
// FIXME - Shouldn't copy all of the text as only the area of the text relevant
// to the user's selection is needed. A possible solution would be to
// copy some number N of characters near the selection and then when the
// user approaches N then we'd do another copy of the next N characters.
CharSequence text = mTextView.getText();
mWordIteratorWithText.setCharSequence(text, 0, text.length());
mUpdateWordIteratorText = false;
}
return mWordIteratorWithText;
}
private long getCharRange(int offset) {
final int textLength = mTextView.getText().length();
if (offset + 1 < textLength) {
final char currentChar = mTextView.getText().charAt(offset);
final char nextChar = mTextView.getText().charAt(offset + 1);
if (Character.isSurrogatePair(currentChar, nextChar)) {
return TextUtils.packRangeInLong(offset, offset + 2);
}
}
if (offset < textLength) {
return TextUtils.packRangeInLong(offset, offset + 1);
}
if (offset - 2 >= 0) {
final char previousChar = mTextView.getText().charAt(offset - 1);
final char previousPreviousChar = mTextView.getText().charAt(offset - 2);
if (Character.isSurrogatePair(previousPreviousChar, previousChar)) {
return TextUtils.packRangeInLong(offset - 2, offset);
}
}
if (offset - 1 >= 0) {
return TextUtils.packRangeInLong(offset - 1, offset);
}
return TextUtils.packRangeInLong(offset, offset);
}
private boolean touchPositionIsInSelection() {
int selectionStart = mTextView.getSelectionStart();
int selectionEnd = mTextView.getSelectionEnd();
if (selectionStart == selectionEnd) {
return false;
}
if (selectionStart > selectionEnd) {
int tmp = selectionStart;
selectionStart = selectionEnd;
selectionEnd = tmp;
Selection.setSelection((Spannable) mTextView.getText(), selectionStart, selectionEnd);
}
SelectionModifierCursorController selectionController = getSelectionController();
int minOffset = selectionController.getMinTouchOffset();
int maxOffset = selectionController.getMaxTouchOffset();
return ((minOffset >= selectionStart) && (maxOffset < selectionEnd));
}
private PositionListener getPositionListener() {
if (mPositionListener == null) {
mPositionListener = new PositionListener();
}
return mPositionListener;
}
private interface TextViewPositionListener {
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled);
}
private boolean isPositionVisible(final float positionX, final float positionY) {
synchronized (TEMP_POSITION) {
final float[] position = TEMP_POSITION;
position[0] = positionX;
position[1] = positionY;
View view = mTextView;
while (view != null) {
if (view != mTextView) {
// Local scroll is already taken into account in positionX/Y
position[0] -= view.getScrollX();
position[1] -= view.getScrollY();
}
if (position[0] < 0 || position[1] < 0 ||
position[0] > view.getWidth() || position[1] > view.getHeight()) {
return false;
}
if (!view.getMatrix().isIdentity()) {
view.getMatrix().mapPoints(position);
}
position[0] += view.getLeft();
position[1] += view.getTop();
final ViewParent parent = view.getParent();
if (parent instanceof View) {
view = (View) parent;
} else {
// We've reached the ViewRoot, stop iterating
view = null;
}
}
}
// We've been able to walk up the view hierarchy and the position was never clipped
return true;
}
private boolean isOffsetVisible(int offset) {
Layout layout = mTextView.getLayout();
if (layout == null) return false;
final int line = layout.getLineForOffset(offset);
final int lineBottom = layout.getLineBottom(line);
final int primaryHorizontal = (int) layout.getPrimaryHorizontal(offset);
return isPositionVisible(primaryHorizontal + mTextView.viewportToContentHorizontalOffset(),
lineBottom + mTextView.viewportToContentVerticalOffset());
}
/** Returns true if the screen coordinates position (x,y) corresponds to a character displayed
* in the view. Returns false when the position is in the empty space of left/right of text.
*/
private boolean isPositionOnText(float x, float y) {
Layout layout = mTextView.getLayout();
if (layout == null) return false;
final int line = mTextView.getLineAtCoordinate(y);
x = mTextView.convertToLocalHorizontalCoordinate(x);
if (x < layout.getLineLeft(line)) return false;
if (x > layout.getLineRight(line)) return false;
return true;
}
public boolean performLongClick(boolean handled) {
// Long press in empty space moves cursor and starts the selection action mode.
if (!handled && !isPositionOnText(mLastDownPositionX, mLastDownPositionY) &&
mInsertionControllerEnabled) {
final int offset = mTextView.getOffsetForPosition(mLastDownPositionX,
mLastDownPositionY);
stopSelectionActionMode();
Selection.setSelection((Spannable) mTextView.getText(), offset);
getInsertionController().show();
startSelectionActionModeWithoutSelection();
handled = true;
}
if (!handled && mSelectionActionMode != null) {
if (touchPositionIsInSelection()) {
// Start a drag
final int start = mTextView.getSelectionStart();
final int end = mTextView.getSelectionEnd();
CharSequence selectedText = mTextView.getTransformedText(start, end);
ClipData data = ClipData.newPlainText(null, selectedText);
DragLocalState localState = new DragLocalState(mTextView, start, end);
mTextView.startDrag(data, getTextThumbnailBuilder(selectedText), localState,
View.DRAG_FLAG_GLOBAL);
stopSelectionActionMode();
} else {
stopSelectionActionMode();
startSelectionActionModeWithSelection();
}
handled = true;
}
// Start a new selection
if (!handled) {
handled = startSelectionActionModeWithSelection();
}
return handled;
}
private long getLastTouchOffsets() {
SelectionModifierCursorController selectionController = getSelectionController();
final int minOffset = selectionController.getMinTouchOffset();
final int maxOffset = selectionController.getMaxTouchOffset();
return TextUtils.packRangeInLong(minOffset, maxOffset);
}
void onFocusChanged(boolean focused, int direction) {
mShowCursor = SystemClock.uptimeMillis();
ensureEndedBatchEdit();
if (focused) {
int selStart = mTextView.getSelectionStart();
int selEnd = mTextView.getSelectionEnd();
// SelectAllOnFocus fields are highlighted and not selected. Do not start text selection
// mode for these, unless there was a specific selection already started.
final boolean isFocusHighlighted = mSelectAllOnFocus && selStart == 0 &&
selEnd == mTextView.getText().length();
mCreatedWithASelection = mFrozenWithFocus && mTextView.hasSelection() &&
!isFocusHighlighted;
if (!mFrozenWithFocus || (selStart < 0 || selEnd < 0)) {
// If a tap was used to give focus to that view, move cursor at tap position.
// Has to be done before onTakeFocus, which can be overloaded.
final int lastTapPosition = getLastTapPosition();
if (lastTapPosition >= 0) {
Selection.setSelection((Spannable) mTextView.getText(), lastTapPosition);
}
// Note this may have to be moved out of the Editor class
MovementMethod mMovement = mTextView.getMovementMethod();
if (mMovement != null) {
mMovement.onTakeFocus(mTextView, (Spannable) mTextView.getText(), direction);
}
// The DecorView does not have focus when the 'Done' ExtractEditText button is
// pressed. Since it is the ViewAncestor's mView, it requests focus before
// ExtractEditText clears focus, which gives focus to the ExtractEditText.
// This special case ensure that we keep current selection in that case.
// It would be better to know why the DecorView does not have focus at that time.
if (((mTextView instanceof ExtractEditText) || mSelectionMoved) &&
selStart >= 0 && selEnd >= 0) {
/*
* Someone intentionally set the selection, so let them
* do whatever it is that they wanted to do instead of
* the default on-focus behavior. We reset the selection
* here instead of just skipping the onTakeFocus() call
* because some movement methods do something other than
* just setting the selection in theirs and we still
* need to go through that path.
*/
Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd);
}
if (mSelectAllOnFocus) {
mTextView.selectAllText();
}
mTouchFocusSelected = true;
}
mFrozenWithFocus = false;
mSelectionMoved = false;
if (mError != null) {
showError();
}
makeBlink();
} else {
if (mError != null) {
hideError();
}
// Don't leave us in the middle of a batch edit.
mTextView.onEndBatchEdit();
if (mTextView instanceof ExtractEditText) {
// terminateTextSelectionMode removes selection, which we want to keep when
// ExtractEditText goes out of focus.
final int selStart = mTextView.getSelectionStart();
final int selEnd = mTextView.getSelectionEnd();
hideControllers();
Selection.setSelection((Spannable) mTextView.getText(), selStart, selEnd);
} else {
if (mTemporaryDetach) mPreserveDetachedSelection = true;
hideControllers();
if (mTemporaryDetach) mPreserveDetachedSelection = false;
downgradeEasyCorrectionSpans();
}
// No need to create the controller
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.resetTouchOffsets();
}
}
}
/**
* Downgrades to simple suggestions all the easy correction spans that are not a spell check
* span.
*/
private void downgradeEasyCorrectionSpans() {
CharSequence text = mTextView.getText();
if (text instanceof Spannable) {
Spannable spannable = (Spannable) text;
SuggestionSpan[] suggestionSpans = spannable.getSpans(0,
spannable.length(), SuggestionSpan.class);
for (int i = 0; i < suggestionSpans.length; i++) {
int flags = suggestionSpans[i].getFlags();
if ((flags & SuggestionSpan.FLAG_EASY_CORRECT) != 0
&& (flags & SuggestionSpan.FLAG_MISSPELLED) == 0) {
flags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
suggestionSpans[i].setFlags(flags);
}
}
}
}
void sendOnTextChanged(int start, int after) {
updateSpellCheckSpans(start, start + after, false);
// Flip flag to indicate the word iterator needs to have the text reset.
mUpdateWordIteratorText = true;
// Hide the controllers as soon as text is modified (typing, procedural...)
// We do not hide the span controllers, since they can be added when a new text is
// inserted into the text view (voice IME).
hideCursorControllers();
}
private int getLastTapPosition() {
// No need to create the controller at that point, no last tap position saved
if (mSelectionModifierCursorController != null) {
int lastTapPosition = mSelectionModifierCursorController.getMinTouchOffset();
if (lastTapPosition >= 0) {
// Safety check, should not be possible.
if (lastTapPosition > mTextView.getText().length()) {
lastTapPosition = mTextView.getText().length();
}
return lastTapPosition;
}
}
return -1;
}
void onWindowFocusChanged(boolean hasWindowFocus) {
if (hasWindowFocus) {
if (mBlink != null) {
mBlink.uncancel();
makeBlink();
}
} else {
if (mBlink != null) {
mBlink.cancel();
}
if (mInputContentType != null) {
mInputContentType.enterDown = false;
}
// Order matters! Must be done before onParentLostFocus to rely on isShowingUp
hideControllers();
if (mSuggestionsPopupWindow != null) {
mSuggestionsPopupWindow.onParentLostFocus();
}
// Don't leave us in the middle of a batch edit. Same as in onFocusChanged
ensureEndedBatchEdit();
}
}
void onTouchEvent(MotionEvent event) {
if (hasSelectionController()) {
getSelectionController().onTouchEvent(event);
}
if (mShowSuggestionRunnable != null) {
mTextView.removeCallbacks(mShowSuggestionRunnable);
mShowSuggestionRunnable = null;
}
if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
mLastDownPositionX = event.getX();
mLastDownPositionY = event.getY();
// Reset this state; it will be re-set if super.onTouchEvent
// causes focus to move to the view.
mTouchFocusSelected = false;
mIgnoreActionUpEvent = false;
}
}
public void beginBatchEdit() {
mInBatchEditControllers = true;
final InputMethodState ims = mInputMethodState;
if (ims != null) {
int nesting = ++ims.mBatchEditNesting;
if (nesting == 1) {
ims.mCursorChanged = false;
ims.mChangedDelta = 0;
if (ims.mContentChanged) {
// We already have a pending change from somewhere else,
// so turn this into a full update.
ims.mChangedStart = 0;
ims.mChangedEnd = mTextView.getText().length();
} else {
ims.mChangedStart = EXTRACT_UNKNOWN;
ims.mChangedEnd = EXTRACT_UNKNOWN;
ims.mContentChanged = false;
}
mUndoInputFilter.beginBatchEdit();
mTextView.onBeginBatchEdit();
}
}
}
public void endBatchEdit() {
mInBatchEditControllers = false;
final InputMethodState ims = mInputMethodState;
if (ims != null) {
int nesting = --ims.mBatchEditNesting;
if (nesting == 0) {
finishBatchEdit(ims);
}
}
}
void ensureEndedBatchEdit() {
final InputMethodState ims = mInputMethodState;
if (ims != null && ims.mBatchEditNesting != 0) {
ims.mBatchEditNesting = 0;
finishBatchEdit(ims);
}
}
void finishBatchEdit(final InputMethodState ims) {
mTextView.onEndBatchEdit();
mUndoInputFilter.endBatchEdit();
if (ims.mContentChanged || ims.mSelectionModeChanged) {
mTextView.updateAfterEdit();
reportExtractedText();
} else if (ims.mCursorChanged) {
// Cheesy way to get us to report the current cursor location.
mTextView.invalidateCursor();
}
// sendUpdateSelection knows to avoid sending if the selection did
// not actually change.
sendUpdateSelection();
}
static final int EXTRACT_NOTHING = -2;
static final int EXTRACT_UNKNOWN = -1;
boolean extractText(ExtractedTextRequest request, ExtractedText outText) {
return extractTextInternal(request, EXTRACT_UNKNOWN, EXTRACT_UNKNOWN,
EXTRACT_UNKNOWN, outText);
}
private boolean extractTextInternal(ExtractedTextRequest request,
int partialStartOffset, int partialEndOffset, int delta,
ExtractedText outText) {
final CharSequence content = mTextView.getText();
if (content != null) {
if (partialStartOffset != EXTRACT_NOTHING) {
final int N = content.length();
if (partialStartOffset < 0) {
outText.partialStartOffset = outText.partialEndOffset = -1;
partialStartOffset = 0;
partialEndOffset = N;
} else {
// Now use the delta to determine the actual amount of text
// we need.
partialEndOffset += delta;
// Adjust offsets to ensure we contain full spans.
if (content instanceof Spanned) {
Spanned spanned = (Spanned)content;
Object[] spans = spanned.getSpans(partialStartOffset,
partialEndOffset, ParcelableSpan.class);
int i = spans.length;
while (i > 0) {
i--;
int j = spanned.getSpanStart(spans[i]);
if (j < partialStartOffset) partialStartOffset = j;
j = spanned.getSpanEnd(spans[i]);
if (j > partialEndOffset) partialEndOffset = j;
}
}
outText.partialStartOffset = partialStartOffset;
outText.partialEndOffset = partialEndOffset - delta;
if (partialStartOffset > N) {
partialStartOffset = N;
} else if (partialStartOffset < 0) {
partialStartOffset = 0;
}
if (partialEndOffset > N) {
partialEndOffset = N;
} else if (partialEndOffset < 0) {
partialEndOffset = 0;
}
}
if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) {
outText.text = content.subSequence(partialStartOffset,
partialEndOffset);
} else {
outText.text = TextUtils.substring(content, partialStartOffset,
partialEndOffset);
}
} else {
outText.partialStartOffset = 0;
outText.partialEndOffset = 0;
outText.text = "";
}
outText.flags = 0;
if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) {
outText.flags |= ExtractedText.FLAG_SELECTING;
}
if (mTextView.isSingleLine()) {
outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
}
outText.startOffset = 0;
outText.selectionStart = mTextView.getSelectionStart();
outText.selectionEnd = mTextView.getSelectionEnd();
return true;
}
return false;
}
boolean reportExtractedText() {
final Editor.InputMethodState ims = mInputMethodState;
if (ims != null) {
final boolean contentChanged = ims.mContentChanged;
if (contentChanged || ims.mSelectionModeChanged) {
ims.mContentChanged = false;
ims.mSelectionModeChanged = false;
final ExtractedTextRequest req = ims.mExtractedTextRequest;
if (req != null) {
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
if (TextView.DEBUG_EXTRACT) Log.v(TextView.LOG_TAG,
"Retrieving extracted start=" + ims.mChangedStart +
" end=" + ims.mChangedEnd +
" delta=" + ims.mChangedDelta);
if (ims.mChangedStart < 0 && !contentChanged) {
ims.mChangedStart = EXTRACT_NOTHING;
}
if (extractTextInternal(req, ims.mChangedStart, ims.mChangedEnd,
ims.mChangedDelta, ims.mExtractedText)) {
if (TextView.DEBUG_EXTRACT) Log.v(TextView.LOG_TAG,
"Reporting extracted start=" +
ims.mExtractedText.partialStartOffset +
" end=" + ims.mExtractedText.partialEndOffset +
": " + ims.mExtractedText.text);
imm.updateExtractedText(mTextView, req.token, ims.mExtractedText);
ims.mChangedStart = EXTRACT_UNKNOWN;
ims.mChangedEnd = EXTRACT_UNKNOWN;
ims.mChangedDelta = 0;
ims.mContentChanged = false;
return true;
}
}
}
}
}
return false;
}
private void sendUpdateSelection() {
if (null != mInputMethodState && mInputMethodState.mBatchEditNesting <= 0) {
final InputMethodManager imm = InputMethodManager.peekInstance();
if (null != imm) {
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
int candStart = -1;
int candEnd = -1;
if (mTextView.getText() instanceof Spannable) {
final Spannable sp = (Spannable) mTextView.getText();
candStart = EditableInputConnection.getComposingSpanStart(sp);
candEnd = EditableInputConnection.getComposingSpanEnd(sp);
}
// InputMethodManager#updateSelection skips sending the message if
// none of the parameters have changed since the last time we called it.
imm.updateSelection(mTextView,
selectionStart, selectionEnd, candStart, candEnd);
}
}
}
void onDraw(Canvas canvas, Layout layout, Path highlight, Paint highlightPaint,
int cursorOffsetVertical) {
final int selectionStart = mTextView.getSelectionStart();
final int selectionEnd = mTextView.getSelectionEnd();
final InputMethodState ims = mInputMethodState;
if (ims != null && ims.mBatchEditNesting == 0) {
InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
if (imm.isActive(mTextView)) {
boolean reported = false;
if (ims.mContentChanged || ims.mSelectionModeChanged) {
// We are in extract mode and the content has changed
// in some way... just report complete new text to the
// input method.
reported = reportExtractedText();
}
}
}
}
if (mCorrectionHighlighter != null) {
mCorrectionHighlighter.draw(canvas, cursorOffsetVertical);
}
if (highlight != null && selectionStart == selectionEnd && mCursorCount > 0) {
drawCursor(canvas, cursorOffsetVertical);
// Rely on the drawable entirely, do not draw the cursor line.
// Has to be done after the IMM related code above which relies on the highlight.
highlight = null;
}
if (mTextView.canHaveDisplayList() && canvas.isHardwareAccelerated()) {
drawHardwareAccelerated(canvas, layout, highlight, highlightPaint,
cursorOffsetVertical);
} else {
layout.draw(canvas, highlight, highlightPaint, cursorOffsetVertical);
}
}
private void drawHardwareAccelerated(Canvas canvas, Layout layout, Path highlight,
Paint highlightPaint, int cursorOffsetVertical) {
final long lineRange = layout.getLineRangeForDraw(canvas);
int firstLine = TextUtils.unpackRangeStartFromLong(lineRange);
int lastLine = TextUtils.unpackRangeEndFromLong(lineRange);
if (lastLine < 0) return;
layout.drawBackground(canvas, highlight, highlightPaint, cursorOffsetVertical,
firstLine, lastLine);
if (layout instanceof DynamicLayout) {
if (mTextDisplayLists == null) {
mTextDisplayLists = ArrayUtils.emptyArray(TextDisplayList.class);
}
DynamicLayout dynamicLayout = (DynamicLayout) layout;
int[] blockEndLines = dynamicLayout.getBlockEndLines();
int[] blockIndices = dynamicLayout.getBlockIndices();
final int numberOfBlocks = dynamicLayout.getNumberOfBlocks();
final int indexFirstChangedBlock = dynamicLayout.getIndexFirstChangedBlock();
int endOfPreviousBlock = -1;
int searchStartIndex = 0;
for (int i = 0; i < numberOfBlocks; i++) {
int blockEndLine = blockEndLines[i];
int blockIndex = blockIndices[i];
final boolean blockIsInvalid = blockIndex == DynamicLayout.INVALID_BLOCK_INDEX;
if (blockIsInvalid) {
blockIndex = getAvailableDisplayListIndex(blockIndices, numberOfBlocks,
searchStartIndex);
// Note how dynamic layout's internal block indices get updated from Editor
blockIndices[i] = blockIndex;
if (mTextDisplayLists[blockIndex] != null) {
mTextDisplayLists[blockIndex].isDirty = true;
}
searchStartIndex = blockIndex + 1;
}
if (mTextDisplayLists[blockIndex] == null) {
mTextDisplayLists[blockIndex] =
new TextDisplayList("Text " + blockIndex);
}
final boolean blockDisplayListIsInvalid = mTextDisplayLists[blockIndex].needsRecord();
RenderNode blockDisplayList = mTextDisplayLists[blockIndex].displayList;
if (i >= indexFirstChangedBlock || blockDisplayListIsInvalid) {
final int blockBeginLine = endOfPreviousBlock + 1;
final int top = layout.getLineTop(blockBeginLine);
final int bottom = layout.getLineBottom(blockEndLine);
int left = 0;
int right = mTextView.getWidth();
if (mTextView.getHorizontallyScrolling()) {
float min = Float.MAX_VALUE;
float max = Float.MIN_VALUE;
for (int line = blockBeginLine; line <= blockEndLine; line++) {
min = Math.min(min, layout.getLineLeft(line));
max = Math.max(max, layout.getLineRight(line));
}
left = (int) min;
right = (int) (max + 0.5f);
}
// Rebuild display list if it is invalid
if (blockDisplayListIsInvalid) {
final DisplayListCanvas displayListCanvas = blockDisplayList.start(
right - left, bottom - top);
try {
// drawText is always relative to TextView's origin, this translation
// brings this range of text back to the top left corner of the viewport
displayListCanvas.translate(-left, -top);
layout.drawText(displayListCanvas, blockBeginLine, blockEndLine);
mTextDisplayLists[blockIndex].isDirty = false;
// No need to untranslate, previous context is popped after
// drawDisplayList
} finally {
blockDisplayList.end(displayListCanvas);
// Same as drawDisplayList below, handled by our TextView's parent
blockDisplayList.setClipToBounds(false);
}
}
// Valid disply list whose index is >= indexFirstChangedBlock
// only needs to update its drawing location.
blockDisplayList.setLeftTopRightBottom(left, top, right, bottom);
}
((DisplayListCanvas) canvas).drawRenderNode(blockDisplayList,
0 /* no child clipping, our TextView parent enforces it */);
endOfPreviousBlock = blockEndLine;
}
dynamicLayout.setIndexFirstChangedBlock(numberOfBlocks);
} else {
// Boring layout is used for empty and hint text
layout.drawText(canvas, firstLine, lastLine);
}
}
private int getAvailableDisplayListIndex(int[] blockIndices, int numberOfBlocks,
int searchStartIndex) {
int length = mTextDisplayLists.length;
for (int i = searchStartIndex; i < length; i++) {
boolean blockIndexFound = false;
for (int j = 0; j < numberOfBlocks; j++) {
if (blockIndices[j] == i) {
blockIndexFound = true;
break;
}
}
if (blockIndexFound) continue;
return i;
}
// No available index found, the pool has to grow
mTextDisplayLists = GrowingArrayUtils.append(mTextDisplayLists, length, null);
return length;
}
private void drawCursor(Canvas canvas, int cursorOffsetVertical) {
final boolean translate = cursorOffsetVertical != 0;
if (translate) canvas.translate(0, cursorOffsetVertical);
for (int i = 0; i < mCursorCount; i++) {
mCursorDrawable[i].draw(canvas);
}
if (translate) canvas.translate(0, -cursorOffsetVertical);
}
/**
* Invalidates all the sub-display lists that overlap the specified character range
*/
void invalidateTextDisplayList(Layout layout, int start, int end) {
if (mTextDisplayLists != null && layout instanceof DynamicLayout) {
final int firstLine = layout.getLineForOffset(start);
final int lastLine = layout.getLineForOffset(end);
DynamicLayout dynamicLayout = (DynamicLayout) layout;
int[] blockEndLines = dynamicLayout.getBlockEndLines();
int[] blockIndices = dynamicLayout.getBlockIndices();
final int numberOfBlocks = dynamicLayout.getNumberOfBlocks();
int i = 0;
// Skip the blocks before firstLine
while (i < numberOfBlocks) {
if (blockEndLines[i] >= firstLine) break;
i++;
}
// Invalidate all subsequent blocks until lastLine is passed
while (i < numberOfBlocks) {
final int blockIndex = blockIndices[i];
if (blockIndex != DynamicLayout.INVALID_BLOCK_INDEX) {
mTextDisplayLists[blockIndex].isDirty = true;
}
if (blockEndLines[i] >= lastLine) break;
i++;
}
}
}
void invalidateTextDisplayList() {
if (mTextDisplayLists != null) {
for (int i = 0; i < mTextDisplayLists.length; i++) {
if (mTextDisplayLists[i] != null) mTextDisplayLists[i].isDirty = true;
}
}
}
void updateCursorsPositions() {
if (mTextView.mCursorDrawableRes == 0) {
mCursorCount = 0;
return;
}
Layout layout = mTextView.getLayout();
Layout hintLayout = mTextView.getHintLayout();
final int offset = mTextView.getSelectionStart();
final int line = layout.getLineForOffset(offset);
final int top = layout.getLineTop(line);
final int bottom = layout.getLineTop(line + 1);
mCursorCount = layout.isLevelBoundary(offset) ? 2 : 1;
int middle = bottom;
if (mCursorCount == 2) {
// Similar to what is done in {@link Layout.#getCursorPath(int, Path, CharSequence)}
middle = (top + bottom) >> 1;
}
boolean clamped = layout.shouldClampCursor(line);
updateCursorPosition(0, top, middle,
getPrimaryHorizontal(layout, hintLayout, offset, clamped));
if (mCursorCount == 2) {
updateCursorPosition(1, middle, bottom,
layout.getSecondaryHorizontal(offset, clamped));
}
}
private float getPrimaryHorizontal(Layout layout, Layout hintLayout, int offset,
boolean clamped) {
if (TextUtils.isEmpty(layout.getText()) &&
hintLayout != null &&
!TextUtils.isEmpty(hintLayout.getText())) {
return hintLayout.getPrimaryHorizontal(offset, clamped);
} else {
return layout.getPrimaryHorizontal(offset, clamped);
}
}
/**
* @return true if the selection mode was actually started.
*/
private boolean startSelectionActionModeWithoutSelection() {
if (mSelectionActionMode != null) {
// Selection action mode is already started
// TODO: revisit invocations to minimize this case.
mSelectionActionMode.invalidate();
return false;
}
ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
mSelectionActionMode = mTextView.startActionMode(
actionModeCallback, ActionMode.TYPE_FLOATING);
return mSelectionActionMode != null;
}
/**
* @return true if the selection mode was actually started.
*/
boolean startSelectionActionModeWithSelection() {
if (mSelectionActionMode != null) {
// Selection action mode is already started
mSelectionActionMode.invalidate();
return false;
}
if (!canSelectText() || !mTextView.requestFocus()) {
Log.w(TextView.LOG_TAG,
"TextView does not support text selection. Action mode cancelled.");
return false;
}
if (!mTextView.hasSelection()) {
// There may already be a selection on device rotation
if (!selectCurrentWord()) {
// No word found under cursor or text selection not permitted.
return false;
}
}
boolean willExtract = extractedTextModeWillBeStarted();
// Do not start the action mode when extracted text will show up full screen, which would
// immediately hide the newly created action bar and would be visually distracting.
if (!willExtract) {
ActionMode.Callback actionModeCallback = new SelectionActionModeCallback();
mSelectionActionMode = mTextView.startActionMode(
actionModeCallback, ActionMode.TYPE_FLOATING);
}
final boolean selectionStarted = mSelectionActionMode != null || willExtract;
if (selectionStarted && !mTextView.isTextSelectable() && mShowSoftInputOnFocus) {
// Show the IME to be able to replace text, except when selecting non editable text.
final InputMethodManager imm = InputMethodManager.peekInstance();
if (imm != null) {
imm.showSoftInput(mTextView, 0, null);
}
}
if (selectionStarted) {
getSelectionController().enterDrag();
}
return selectionStarted;
}
private boolean extractedTextModeWillBeStarted() {
if (!(mTextView instanceof ExtractEditText)) {
final InputMethodManager imm = InputMethodManager.peekInstance();
return imm != null && imm.isFullscreenMode();
}
return false;
}
/**
* @return <code>true</code> if the cursor/current selection overlaps a {@link SuggestionSpan}.
*/
private boolean isCursorInsideSuggestionSpan() {
CharSequence text = mTextView.getText();
if (!(text instanceof Spannable)) return false;
SuggestionSpan[] suggestionSpans = ((Spannable) text).getSpans(
mTextView.getSelectionStart(), mTextView.getSelectionEnd(), SuggestionSpan.class);
return (suggestionSpans.length > 0);
}
/**
* @return <code>true</code> if the cursor is inside an {@link SuggestionSpan} with
* {@link SuggestionSpan#FLAG_EASY_CORRECT} set.
*/
private boolean isCursorInsideEasyCorrectionSpan() {
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = spannable.getSpans(mTextView.getSelectionStart(),
mTextView.getSelectionEnd(), SuggestionSpan.class);
for (int i = 0; i < suggestionSpans.length; i++) {
if ((suggestionSpans[i].getFlags() & SuggestionSpan.FLAG_EASY_CORRECT) != 0) {
return true;
}
}
return false;
}
void onTouchUpEvent(MotionEvent event) {
boolean selectAllGotFocus = mSelectAllOnFocus && mTextView.didTouchFocusSelect();
hideControllers();
CharSequence text = mTextView.getText();
if (!selectAllGotFocus && text.length() > 0) {
// Move cursor
final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
Selection.setSelection((Spannable) text, offset);
if (mSpellChecker != null) {
// When the cursor moves, the word that was typed may need spell check
mSpellChecker.onSelectionChanged();
}
if (!extractedTextModeWillBeStarted()) {
if (isCursorInsideEasyCorrectionSpan()) {
mShowSuggestionRunnable = new Runnable() {
public void run() {
showSuggestions();
}
};
// removeCallbacks is performed on every touch
mTextView.postDelayed(mShowSuggestionRunnable,
ViewConfiguration.getDoubleTapTimeout());
} else if (hasInsertionController()) {
getInsertionController().show();
}
}
}
}
protected void stopSelectionActionMode() {
if (mSelectionActionMode != null) {
// This will hide the mSelectionModifierCursorController
mSelectionActionMode.finish();
}
}
/**
* @return True if this view supports insertion handles.
*/
boolean hasInsertionController() {
return mInsertionControllerEnabled;
}
/**
* @return True if this view supports selection handles.
*/
boolean hasSelectionController() {
return mSelectionControllerEnabled;
}
InsertionPointCursorController getInsertionController() {
if (!mInsertionControllerEnabled) {
return null;
}
if (mInsertionPointCursorController == null) {
mInsertionPointCursorController = new InsertionPointCursorController();
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.addOnTouchModeChangeListener(mInsertionPointCursorController);
}
return mInsertionPointCursorController;
}
SelectionModifierCursorController getSelectionController() {
if (!mSelectionControllerEnabled) {
return null;
}
if (mSelectionModifierCursorController == null) {
mSelectionModifierCursorController = new SelectionModifierCursorController();
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.addOnTouchModeChangeListener(mSelectionModifierCursorController);
}
return mSelectionModifierCursorController;
}
private void updateCursorPosition(int cursorIndex, int top, int bottom, float horizontal) {
if (mCursorDrawable[cursorIndex] == null)
mCursorDrawable[cursorIndex] = mTextView.getContext().getDrawable(
mTextView.mCursorDrawableRes);
if (mTempRect == null) mTempRect = new Rect();
mCursorDrawable[cursorIndex].getPadding(mTempRect);
final int width = mCursorDrawable[cursorIndex].getIntrinsicWidth();
horizontal = Math.max(0.5f, horizontal - 0.5f);
final int left = (int) (horizontal) - mTempRect.left;
mCursorDrawable[cursorIndex].setBounds(left, top - mTempRect.top, left + width,
bottom + mTempRect.bottom);
}
/**
* Called by the framework in response to a text auto-correction (such as fixing a typo using a
* a dictionary) from the current input method, provided by it calling
* {@link InputConnection#commitCorrection} InputConnection.commitCorrection()}. The default
* implementation flashes the background of the corrected word to provide feedback to the user.
*
* @param info The auto correct info about the text that was corrected.
*/
public void onCommitCorrection(CorrectionInfo info) {
if (mCorrectionHighlighter == null) {
mCorrectionHighlighter = new CorrectionHighlighter();
} else {
mCorrectionHighlighter.invalidate(false);
}
mCorrectionHighlighter.highlight(info);
}
void showSuggestions() {
if (mSuggestionsPopupWindow == null) {
mSuggestionsPopupWindow = new SuggestionsPopupWindow();
}
hideControllers();
mSuggestionsPopupWindow.show();
}
boolean areSuggestionsShown() {
return mSuggestionsPopupWindow != null && mSuggestionsPopupWindow.isShowing();
}
void onScrollChanged() {
if (mPositionListener != null) {
mPositionListener.onScrollChanged();
}
}
/**
* @return True when the TextView isFocused and has a valid zero-length selection (cursor).
*/
private boolean shouldBlink() {
if (!isCursorVisible() || !mTextView.isFocused()) return false;
final int start = mTextView.getSelectionStart();
if (start < 0) return false;
final int end = mTextView.getSelectionEnd();
if (end < 0) return false;
return start == end;
}
void makeBlink() {
if (shouldBlink()) {
mShowCursor = SystemClock.uptimeMillis();
if (mBlink == null) mBlink = new Blink();
mBlink.removeCallbacks(mBlink);
mBlink.postAtTime(mBlink, mShowCursor + BLINK);
} else {
if (mBlink != null) mBlink.removeCallbacks(mBlink);
}
}
private class Blink extends Handler implements Runnable {
private boolean mCancelled;
public void run() {
if (mCancelled) {
return;
}
removeCallbacks(Blink.this);
if (shouldBlink()) {
if (mTextView.getLayout() != null) {
mTextView.invalidateCursorPath();
}
postAtTime(this, SystemClock.uptimeMillis() + BLINK);
}
}
void cancel() {
if (!mCancelled) {
removeCallbacks(Blink.this);
mCancelled = true;
}
}
void uncancel() {
mCancelled = false;
}
}
private DragShadowBuilder getTextThumbnailBuilder(CharSequence text) {
TextView shadowView = (TextView) View.inflate(mTextView.getContext(),
com.android.internal.R.layout.text_drag_thumbnail, null);
if (shadowView == null) {
throw new IllegalArgumentException("Unable to inflate text drag thumbnail");
}
if (text.length() > DRAG_SHADOW_MAX_TEXT_LENGTH) {
text = text.subSequence(0, DRAG_SHADOW_MAX_TEXT_LENGTH);
}
shadowView.setText(text);
shadowView.setTextColor(mTextView.getTextColors());
shadowView.setTextAppearance(mTextView.getContext(), R.styleable.Theme_textAppearanceLarge);
shadowView.setGravity(Gravity.CENTER);
shadowView.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
final int size = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
shadowView.measure(size, size);
shadowView.layout(0, 0, shadowView.getMeasuredWidth(), shadowView.getMeasuredHeight());
shadowView.invalidate();
return new DragShadowBuilder(shadowView);
}
private static class DragLocalState {
public TextView sourceTextView;
public int start, end;
public DragLocalState(TextView sourceTextView, int start, int end) {
this.sourceTextView = sourceTextView;
this.start = start;
this.end = end;
}
}
void onDrop(DragEvent event) {
StringBuilder content = new StringBuilder("");
ClipData clipData = event.getClipData();
final int itemCount = clipData.getItemCount();
for (int i=0; i < itemCount; i++) {
Item item = clipData.getItemAt(i);
content.append(item.coerceToStyledText(mTextView.getContext()));
}
final int offset = mTextView.getOffsetForPosition(event.getX(), event.getY());
Object localState = event.getLocalState();
DragLocalState dragLocalState = null;
if (localState instanceof DragLocalState) {
dragLocalState = (DragLocalState) localState;
}
boolean dragDropIntoItself = dragLocalState != null &&
dragLocalState.sourceTextView == mTextView;
if (dragDropIntoItself) {
if (offset >= dragLocalState.start && offset < dragLocalState.end) {
// A drop inside the original selection discards the drop.
return;
}
}
final int originalLength = mTextView.getText().length();
int min = offset;
int max = offset;
Selection.setSelection((Spannable) mTextView.getText(), max);
mTextView.replaceText_internal(min, max, content);
if (dragDropIntoItself) {
int dragSourceStart = dragLocalState.start;
int dragSourceEnd = dragLocalState.end;
if (max <= dragSourceStart) {
// Inserting text before selection has shifted positions
final int shift = mTextView.getText().length() - originalLength;
dragSourceStart += shift;
dragSourceEnd += shift;
}
// Delete original selection
mTextView.deleteText_internal(dragSourceStart, dragSourceEnd);
// Make sure we do not leave two adjacent spaces.
final int prevCharIdx = Math.max(0, dragSourceStart - 1);
final int nextCharIdx = Math.min(mTextView.getText().length(), dragSourceStart + 1);
if (nextCharIdx > prevCharIdx + 1) {
CharSequence t = mTextView.getTransformedText(prevCharIdx, nextCharIdx);
if (Character.isSpaceChar(t.charAt(0)) && Character.isSpaceChar(t.charAt(1))) {
mTextView.deleteText_internal(prevCharIdx, prevCharIdx + 1);
}
}
}
}
public void addSpanWatchers(Spannable text) {
final int textLength = text.length();
if (mKeyListener != null) {
text.setSpan(mKeyListener, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
if (mSpanController == null) {
mSpanController = new SpanController();
}
text.setSpan(mSpanController, 0, textLength, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
}
/**
* Controls the {@link EasyEditSpan} monitoring when it is added, and when the related
* pop-up should be displayed.
* Also monitors {@link Selection} to call back to the attached input method.
*/
class SpanController implements SpanWatcher {
private static final int DISPLAY_TIMEOUT_MS = 3000; // 3 secs
private EasyEditPopupWindow mPopupWindow;
private Runnable mHidePopup;
// This function is pure but inner classes can't have static functions
private boolean isNonIntermediateSelectionSpan(final Spannable text,
final Object span) {
return (Selection.SELECTION_START == span || Selection.SELECTION_END == span)
&& (text.getSpanFlags(span) & Spanned.SPAN_INTERMEDIATE) == 0;
}
@Override
public void onSpanAdded(Spannable text, Object span, int start, int end) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (span instanceof EasyEditSpan) {
if (mPopupWindow == null) {
mPopupWindow = new EasyEditPopupWindow();
mHidePopup = new Runnable() {
@Override
public void run() {
hide();
}
};
}
// Make sure there is only at most one EasyEditSpan in the text
if (mPopupWindow.mEasyEditSpan != null) {
mPopupWindow.mEasyEditSpan.setDeleteEnabled(false);
}
mPopupWindow.setEasyEditSpan((EasyEditSpan) span);
mPopupWindow.setOnDeleteListener(new EasyEditDeleteListener() {
@Override
public void onDeleteClick(EasyEditSpan span) {
Editable editable = (Editable) mTextView.getText();
int start = editable.getSpanStart(span);
int end = editable.getSpanEnd(span);
if (start >= 0 && end >= 0) {
sendEasySpanNotification(EasyEditSpan.TEXT_DELETED, span);
mTextView.deleteText_internal(start, end);
}
editable.removeSpan(span);
}
});
if (mTextView.getWindowVisibility() != View.VISIBLE) {
// The window is not visible yet, ignore the text change.
return;
}
if (mTextView.getLayout() == null) {
// The view has not been laid out yet, ignore the text change
return;
}
if (extractedTextModeWillBeStarted()) {
// The input is in extract mode. Do not handle the easy edit in
// the original TextView, as the ExtractEditText will do
return;
}
mPopupWindow.show();
mTextView.removeCallbacks(mHidePopup);
mTextView.postDelayed(mHidePopup, DISPLAY_TIMEOUT_MS);
}
}
@Override
public void onSpanRemoved(Spannable text, Object span, int start, int end) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (mPopupWindow != null && span == mPopupWindow.mEasyEditSpan) {
hide();
}
}
@Override
public void onSpanChanged(Spannable text, Object span, int previousStart, int previousEnd,
int newStart, int newEnd) {
if (isNonIntermediateSelectionSpan(text, span)) {
sendUpdateSelection();
} else if (mPopupWindow != null && span instanceof EasyEditSpan) {
EasyEditSpan easyEditSpan = (EasyEditSpan) span;
sendEasySpanNotification(EasyEditSpan.TEXT_MODIFIED, easyEditSpan);
text.removeSpan(easyEditSpan);
}
}
public void hide() {
if (mPopupWindow != null) {
mPopupWindow.hide();
mTextView.removeCallbacks(mHidePopup);
}
}
private void sendEasySpanNotification(int textChangedType, EasyEditSpan span) {
try {
PendingIntent pendingIntent = span.getPendingIntent();
if (pendingIntent != null) {
Intent intent = new Intent();
intent.putExtra(EasyEditSpan.EXTRA_TEXT_CHANGED_TYPE, textChangedType);
pendingIntent.send(mTextView.getContext(), 0, intent);
}
} catch (CanceledException e) {
// This should not happen, as we should try to send the intent only once.
Log.w(TAG, "PendingIntent for notification cannot be sent", e);
}
}
}
/**
* Listens for the delete event triggered by {@link EasyEditPopupWindow}.
*/
private interface EasyEditDeleteListener {
/**
* Clicks the delete pop-up.
*/
void onDeleteClick(EasyEditSpan span);
}
/**
* Displays the actions associated to an {@link EasyEditSpan}. The pop-up is controlled
* by {@link SpanController}.
*/
private class EasyEditPopupWindow extends PinnedPopupWindow
implements OnClickListener {
private static final int POPUP_TEXT_LAYOUT =
com.android.internal.R.layout.text_edit_action_popup_text;
private TextView mDeleteTextView;
private EasyEditSpan mEasyEditSpan;
private EasyEditDeleteListener mOnDeleteListener;
@Override
protected void createPopupWindow() {
mPopupWindow = new PopupWindow(mTextView.getContext(), null,
com.android.internal.R.attr.textSelectHandleWindowStyle);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopupWindow.setClippingEnabled(true);
}
@Override
protected void initContentView() {
LinearLayout linearLayout = new LinearLayout(mTextView.getContext());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
mContentView = linearLayout;
mContentView.setBackgroundResource(
com.android.internal.R.drawable.text_edit_side_paste_window);
LayoutInflater inflater = (LayoutInflater)mTextView.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutParams wrapContent = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
mDeleteTextView = (TextView) inflater.inflate(POPUP_TEXT_LAYOUT, null);
mDeleteTextView.setLayoutParams(wrapContent);
mDeleteTextView.setText(com.android.internal.R.string.delete);
mDeleteTextView.setOnClickListener(this);
mContentView.addView(mDeleteTextView);
}
public void setEasyEditSpan(EasyEditSpan easyEditSpan) {
mEasyEditSpan = easyEditSpan;
}
private void setOnDeleteListener(EasyEditDeleteListener listener) {
mOnDeleteListener = listener;
}
@Override
public void onClick(View view) {
if (view == mDeleteTextView
&& mEasyEditSpan != null && mEasyEditSpan.isDeleteEnabled()
&& mOnDeleteListener != null) {
mOnDeleteListener.onDeleteClick(mEasyEditSpan);
}
}
@Override
public void hide() {
if (mEasyEditSpan != null) {
mEasyEditSpan.setDeleteEnabled(false);
}
mOnDeleteListener = null;
super.hide();
}
@Override
protected int getTextOffset() {
// Place the pop-up at the end of the span
Editable editable = (Editable) mTextView.getText();
return editable.getSpanEnd(mEasyEditSpan);
}
@Override
protected int getVerticalLocalPosition(int line) {
return mTextView.getLayout().getLineBottom(line);
}
@Override
protected int clipVertically(int positionY) {
// As we display the pop-up below the span, no vertical clipping is required.
return positionY;
}
}
private class PositionListener implements ViewTreeObserver.OnPreDrawListener {
// 3 handles
// 3 ActionPopup [replace, suggestion, easyedit] (suggestionsPopup first hides the others)
// 1 CursorAnchorInfoNotifier
private final int MAXIMUM_NUMBER_OF_LISTENERS = 7;
private TextViewPositionListener[] mPositionListeners =
new TextViewPositionListener[MAXIMUM_NUMBER_OF_LISTENERS];
private boolean mCanMove[] = new boolean[MAXIMUM_NUMBER_OF_LISTENERS];
private boolean mPositionHasChanged = true;
// Absolute position of the TextView with respect to its parent window
private int mPositionX, mPositionY;
private int mNumberOfListeners;
private boolean mScrollHasChanged;
final int[] mTempCoords = new int[2];
public void addSubscriber(TextViewPositionListener positionListener, boolean canMove) {
if (mNumberOfListeners == 0) {
updatePosition();
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.addOnPreDrawListener(this);
}
int emptySlotIndex = -1;
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
TextViewPositionListener listener = mPositionListeners[i];
if (listener == positionListener) {
return;
} else if (emptySlotIndex < 0 && listener == null) {
emptySlotIndex = i;
}
}
mPositionListeners[emptySlotIndex] = positionListener;
mCanMove[emptySlotIndex] = canMove;
mNumberOfListeners++;
}
public void removeSubscriber(TextViewPositionListener positionListener) {
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
if (mPositionListeners[i] == positionListener) {
mPositionListeners[i] = null;
mNumberOfListeners--;
break;
}
}
if (mNumberOfListeners == 0) {
ViewTreeObserver vto = mTextView.getViewTreeObserver();
vto.removeOnPreDrawListener(this);
}
}
public int getPositionX() {
return mPositionX;
}
public int getPositionY() {
return mPositionY;
}
@Override
public boolean onPreDraw() {
updatePosition();
for (int i = 0; i < MAXIMUM_NUMBER_OF_LISTENERS; i++) {
if (mPositionHasChanged || mScrollHasChanged || mCanMove[i]) {
TextViewPositionListener positionListener = mPositionListeners[i];
if (positionListener != null) {
positionListener.updatePosition(mPositionX, mPositionY,
mPositionHasChanged, mScrollHasChanged);
}
}
}
mScrollHasChanged = false;
return true;
}
private void updatePosition() {
mTextView.getLocationInWindow(mTempCoords);
mPositionHasChanged = mTempCoords[0] != mPositionX || mTempCoords[1] != mPositionY;
mPositionX = mTempCoords[0];
mPositionY = mTempCoords[1];
}
public void onScrollChanged() {
mScrollHasChanged = true;
}
}
private abstract class PinnedPopupWindow implements TextViewPositionListener {
protected PopupWindow mPopupWindow;
protected ViewGroup mContentView;
int mPositionX, mPositionY;
protected abstract void createPopupWindow();
protected abstract void initContentView();
protected abstract int getTextOffset();
protected abstract int getVerticalLocalPosition(int line);
protected abstract int clipVertically(int positionY);
public PinnedPopupWindow() {
createPopupWindow();
mPopupWindow.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
mPopupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
initContentView();
LayoutParams wrapContent = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mContentView.setLayoutParams(wrapContent);
mPopupWindow.setContentView(mContentView);
}
public void show() {
getPositionListener().addSubscriber(this, false /* offset is fixed */);
computeLocalPosition();
final PositionListener positionListener = getPositionListener();
updatePosition(positionListener.getPositionX(), positionListener.getPositionY());
}
protected void measureContent() {
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
mContentView.measure(
View.MeasureSpec.makeMeasureSpec(displayMetrics.widthPixels,
View.MeasureSpec.AT_MOST),
View.MeasureSpec.makeMeasureSpec(displayMetrics.heightPixels,
View.MeasureSpec.AT_MOST));
}
/* The popup window will be horizontally centered on the getTextOffset() and vertically
* positioned according to viewportToContentHorizontalOffset.
*
* This method assumes that mContentView has properly been measured from its content. */
private void computeLocalPosition() {
measureContent();
final int width = mContentView.getMeasuredWidth();
final int offset = getTextOffset();
mPositionX = (int) (mTextView.getLayout().getPrimaryHorizontal(offset) - width / 2.0f);
mPositionX += mTextView.viewportToContentHorizontalOffset();
final int line = mTextView.getLayout().getLineForOffset(offset);
mPositionY = getVerticalLocalPosition(line);
mPositionY += mTextView.viewportToContentVerticalOffset();
}
private void updatePosition(int parentPositionX, int parentPositionY) {
int positionX = parentPositionX + mPositionX;
int positionY = parentPositionY + mPositionY;
positionY = clipVertically(positionY);
// Horizontal clipping
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
final int width = mContentView.getMeasuredWidth();
positionX = Math.min(displayMetrics.widthPixels - width, positionX);
positionX = Math.max(0, positionX);
if (isShowing()) {
mPopupWindow.update(positionX, positionY, -1, -1);
} else {
mPopupWindow.showAtLocation(mTextView, Gravity.NO_GRAVITY,
positionX, positionY);
}
}
public void hide() {
mPopupWindow.dismiss();
getPositionListener().removeSubscriber(this);
}
@Override
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled) {
// Either parentPositionChanged or parentScrolled is true, check if still visible
if (isShowing() && isOffsetVisible(getTextOffset())) {
if (parentScrolled) computeLocalPosition();
updatePosition(parentPositionX, parentPositionY);
} else {
hide();
}
}
public boolean isShowing() {
return mPopupWindow.isShowing();
}
}
private class SuggestionsPopupWindow extends PinnedPopupWindow implements OnItemClickListener {
private static final int MAX_NUMBER_SUGGESTIONS = SuggestionSpan.SUGGESTIONS_MAX_SIZE;
private static final int ADD_TO_DICTIONARY = -1;
private static final int DELETE_TEXT = -2;
private SuggestionInfo[] mSuggestionInfos;
private int mNumberOfSuggestions;
private boolean mCursorWasVisibleBeforeSuggestions;
private boolean mIsShowingUp = false;
private SuggestionAdapter mSuggestionsAdapter;
private final Comparator<SuggestionSpan> mSuggestionSpanComparator;
private final HashMap<SuggestionSpan, Integer> mSpansLengths;
private class CustomPopupWindow extends PopupWindow {
public CustomPopupWindow(Context context, int defStyleAttr) {
super(context, null, defStyleAttr);
}
@Override
public void dismiss() {
super.dismiss();
getPositionListener().removeSubscriber(SuggestionsPopupWindow.this);
// Safe cast since show() checks that mTextView.getText() is an Editable
((Spannable) mTextView.getText()).removeSpan(mSuggestionRangeSpan);
mTextView.setCursorVisible(mCursorWasVisibleBeforeSuggestions);
if (hasInsertionController()) {
getInsertionController().show();
}
}
}
public SuggestionsPopupWindow() {
mCursorWasVisibleBeforeSuggestions = mCursorVisible;
mSuggestionSpanComparator = new SuggestionSpanComparator();
mSpansLengths = new HashMap<SuggestionSpan, Integer>();
}
@Override
protected void createPopupWindow() {
mPopupWindow = new CustomPopupWindow(mTextView.getContext(),
com.android.internal.R.attr.textSuggestionsWindowStyle);
mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NOT_NEEDED);
mPopupWindow.setFocusable(true);
mPopupWindow.setClippingEnabled(false);
}
@Override
protected void initContentView() {
ListView listView = new ListView(mTextView.getContext());
mSuggestionsAdapter = new SuggestionAdapter();
listView.setAdapter(mSuggestionsAdapter);
listView.setOnItemClickListener(this);
mContentView = listView;
// Inflate the suggestion items once and for all. + 2 for add to dictionary and delete
mSuggestionInfos = new SuggestionInfo[MAX_NUMBER_SUGGESTIONS + 2];
for (int i = 0; i < mSuggestionInfos.length; i++) {
mSuggestionInfos[i] = new SuggestionInfo();
}
}
public boolean isShowingUp() {
return mIsShowingUp;
}
public void onParentLostFocus() {
mIsShowingUp = false;
}
private class SuggestionInfo {
int suggestionStart, suggestionEnd; // range of actual suggestion within text
SuggestionSpan suggestionSpan; // the SuggestionSpan that this TextView represents
int suggestionIndex; // the index of this suggestion inside suggestionSpan
SpannableStringBuilder text = new SpannableStringBuilder();
TextAppearanceSpan highlightSpan = new TextAppearanceSpan(mTextView.getContext(),
android.R.style.TextAppearance_SuggestionHighlight);
}
private class SuggestionAdapter extends BaseAdapter {
private LayoutInflater mInflater = (LayoutInflater) mTextView.getContext().
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@Override
public int getCount() {
return mNumberOfSuggestions;
}
@Override
public Object getItem(int position) {
return mSuggestionInfos[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) convertView;
if (textView == null) {
textView = (TextView) mInflater.inflate(mTextView.mTextEditSuggestionItemLayout,
parent, false);
}
final SuggestionInfo suggestionInfo = mSuggestionInfos[position];
textView.setText(suggestionInfo.text);
if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY ||
suggestionInfo.suggestionIndex == DELETE_TEXT) {
textView.setBackgroundColor(Color.TRANSPARENT);
} else {
textView.setBackgroundColor(Color.WHITE);
}
return textView;
}
}
private class SuggestionSpanComparator implements Comparator<SuggestionSpan> {
public int compare(SuggestionSpan span1, SuggestionSpan span2) {
final int flag1 = span1.getFlags();
final int flag2 = span2.getFlags();
if (flag1 != flag2) {
// The order here should match what is used in updateDrawState
final boolean easy1 = (flag1 & SuggestionSpan.FLAG_EASY_CORRECT) != 0;
final boolean easy2 = (flag2 & SuggestionSpan.FLAG_EASY_CORRECT) != 0;
final boolean misspelled1 = (flag1 & SuggestionSpan.FLAG_MISSPELLED) != 0;
final boolean misspelled2 = (flag2 & SuggestionSpan.FLAG_MISSPELLED) != 0;
if (easy1 && !misspelled1) return -1;
if (easy2 && !misspelled2) return 1;
if (misspelled1) return -1;
if (misspelled2) return 1;
}
return mSpansLengths.get(span1).intValue() - mSpansLengths.get(span2).intValue();
}
}
/**
* Returns the suggestion spans that cover the current cursor position. The suggestion
* spans are sorted according to the length of text that they are attached to.
*/
private SuggestionSpan[] getSuggestionSpans() {
int pos = mTextView.getSelectionStart();
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = spannable.getSpans(pos, pos, SuggestionSpan.class);
mSpansLengths.clear();
for (SuggestionSpan suggestionSpan : suggestionSpans) {
int start = spannable.getSpanStart(suggestionSpan);
int end = spannable.getSpanEnd(suggestionSpan);
mSpansLengths.put(suggestionSpan, Integer.valueOf(end - start));
}
// The suggestions are sorted according to their types (easy correction first, then
// misspelled) and to the length of the text that they cover (shorter first).
Arrays.sort(suggestionSpans, mSuggestionSpanComparator);
return suggestionSpans;
}
@Override
public void show() {
if (!(mTextView.getText() instanceof Editable)) return;
if (updateSuggestions()) {
mCursorWasVisibleBeforeSuggestions = mCursorVisible;
mTextView.setCursorVisible(false);
mIsShowingUp = true;
super.show();
}
}
@Override
protected void measureContent() {
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
final int horizontalMeasure = View.MeasureSpec.makeMeasureSpec(
displayMetrics.widthPixels, View.MeasureSpec.AT_MOST);
final int verticalMeasure = View.MeasureSpec.makeMeasureSpec(
displayMetrics.heightPixels, View.MeasureSpec.AT_MOST);
int width = 0;
View view = null;
for (int i = 0; i < mNumberOfSuggestions; i++) {
view = mSuggestionsAdapter.getView(i, view, mContentView);
view.getLayoutParams().width = LayoutParams.WRAP_CONTENT;
view.measure(horizontalMeasure, verticalMeasure);
width = Math.max(width, view.getMeasuredWidth());
}
// Enforce the width based on actual text widths
mContentView.measure(
View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY),
verticalMeasure);
Drawable popupBackground = mPopupWindow.getBackground();
if (popupBackground != null) {
if (mTempRect == null) mTempRect = new Rect();
popupBackground.getPadding(mTempRect);
width += mTempRect.left + mTempRect.right;
}
mPopupWindow.setWidth(width);
}
@Override
protected int getTextOffset() {
return mTextView.getSelectionStart();
}
@Override
protected int getVerticalLocalPosition(int line) {
return mTextView.getLayout().getLineBottom(line);
}
@Override
protected int clipVertically(int positionY) {
final int height = mContentView.getMeasuredHeight();
final DisplayMetrics displayMetrics = mTextView.getResources().getDisplayMetrics();
return Math.min(positionY, displayMetrics.heightPixels - height);
}
@Override
public void hide() {
super.hide();
}
private boolean updateSuggestions() {
Spannable spannable = (Spannable) mTextView.getText();
SuggestionSpan[] suggestionSpans = getSuggestionSpans();
final int nbSpans = suggestionSpans.length;
// Suggestions are shown after a delay: the underlying spans may have been removed
if (nbSpans == 0) return false;
mNumberOfSuggestions = 0;
int spanUnionStart = mTextView.getText().length();
int spanUnionEnd = 0;
SuggestionSpan misspelledSpan = null;
int underlineColor = 0;
for (int spanIndex = 0; spanIndex < nbSpans; spanIndex++) {
SuggestionSpan suggestionSpan = suggestionSpans[spanIndex];
final int spanStart = spannable.getSpanStart(suggestionSpan);
final int spanEnd = spannable.getSpanEnd(suggestionSpan);
spanUnionStart = Math.min(spanStart, spanUnionStart);
spanUnionEnd = Math.max(spanEnd, spanUnionEnd);
if ((suggestionSpan.getFlags() & SuggestionSpan.FLAG_MISSPELLED) != 0) {
misspelledSpan = suggestionSpan;
}
// The first span dictates the background color of the highlighted text
if (spanIndex == 0) underlineColor = suggestionSpan.getUnderlineColor();
String[] suggestions = suggestionSpan.getSuggestions();
int nbSuggestions = suggestions.length;
for (int suggestionIndex = 0; suggestionIndex < nbSuggestions; suggestionIndex++) {
String suggestion = suggestions[suggestionIndex];
boolean suggestionIsDuplicate = false;
for (int i = 0; i < mNumberOfSuggestions; i++) {
if (mSuggestionInfos[i].text.toString().equals(suggestion)) {
SuggestionSpan otherSuggestionSpan = mSuggestionInfos[i].suggestionSpan;
final int otherSpanStart = spannable.getSpanStart(otherSuggestionSpan);
final int otherSpanEnd = spannable.getSpanEnd(otherSuggestionSpan);
if (spanStart == otherSpanStart && spanEnd == otherSpanEnd) {
suggestionIsDuplicate = true;
break;
}
}
}
if (!suggestionIsDuplicate) {
SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions];
suggestionInfo.suggestionSpan = suggestionSpan;
suggestionInfo.suggestionIndex = suggestionIndex;
suggestionInfo.text.replace(0, suggestionInfo.text.length(), suggestion);
mNumberOfSuggestions++;
if (mNumberOfSuggestions == MAX_NUMBER_SUGGESTIONS) {
// Also end outer for loop
spanIndex = nbSpans;
break;
}
}
}
}
for (int i = 0; i < mNumberOfSuggestions; i++) {
highlightTextDifferences(mSuggestionInfos[i], spanUnionStart, spanUnionEnd);
}
// Add "Add to dictionary" item if there is a span with the misspelled flag
if (misspelledSpan != null) {
final int misspelledStart = spannable.getSpanStart(misspelledSpan);
final int misspelledEnd = spannable.getSpanEnd(misspelledSpan);
if (misspelledStart >= 0 && misspelledEnd > misspelledStart) {
SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions];
suggestionInfo.suggestionSpan = misspelledSpan;
suggestionInfo.suggestionIndex = ADD_TO_DICTIONARY;
suggestionInfo.text.replace(0, suggestionInfo.text.length(), mTextView.
getContext().getString(com.android.internal.R.string.addToDictionary));
suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 0,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mNumberOfSuggestions++;
}
}
// Delete item
SuggestionInfo suggestionInfo = mSuggestionInfos[mNumberOfSuggestions];
suggestionInfo.suggestionSpan = null;
suggestionInfo.suggestionIndex = DELETE_TEXT;
suggestionInfo.text.replace(0, suggestionInfo.text.length(),
mTextView.getContext().getString(com.android.internal.R.string.deleteText));
suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0, 0,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mNumberOfSuggestions++;
if (mSuggestionRangeSpan == null) mSuggestionRangeSpan = new SuggestionRangeSpan();
if (underlineColor == 0) {
// Fallback on the default highlight color when the first span does not provide one
mSuggestionRangeSpan.setBackgroundColor(mTextView.mHighlightColor);
} else {
final float BACKGROUND_TRANSPARENCY = 0.4f;
final int newAlpha = (int) (Color.alpha(underlineColor) * BACKGROUND_TRANSPARENCY);
mSuggestionRangeSpan.setBackgroundColor(
(underlineColor & 0x00FFFFFF) + (newAlpha << 24));
}
spannable.setSpan(mSuggestionRangeSpan, spanUnionStart, spanUnionEnd,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
mSuggestionsAdapter.notifyDataSetChanged();
return true;
}
private void highlightTextDifferences(SuggestionInfo suggestionInfo, int unionStart,
int unionEnd) {
final Spannable text = (Spannable) mTextView.getText();
final int spanStart = text.getSpanStart(suggestionInfo.suggestionSpan);
final int spanEnd = text.getSpanEnd(suggestionInfo.suggestionSpan);
// Adjust the start/end of the suggestion span
suggestionInfo.suggestionStart = spanStart - unionStart;
suggestionInfo.suggestionEnd = suggestionInfo.suggestionStart
+ suggestionInfo.text.length();
suggestionInfo.text.setSpan(suggestionInfo.highlightSpan, 0,
suggestionInfo.text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Add the text before and after the span.
final String textAsString = text.toString();
suggestionInfo.text.insert(0, textAsString.substring(unionStart, spanStart));
suggestionInfo.text.append(textAsString.substring(spanEnd, unionEnd));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Editable editable = (Editable) mTextView.getText();
SuggestionInfo suggestionInfo = mSuggestionInfos[position];
if (suggestionInfo.suggestionIndex == DELETE_TEXT) {
final int spanUnionStart = editable.getSpanStart(mSuggestionRangeSpan);
int spanUnionEnd = editable.getSpanEnd(mSuggestionRangeSpan);
if (spanUnionStart >= 0 && spanUnionEnd > spanUnionStart) {
// Do not leave two adjacent spaces after deletion, or one at beginning of text
if (spanUnionEnd < editable.length() &&
Character.isSpaceChar(editable.charAt(spanUnionEnd)) &&
(spanUnionStart == 0 ||
Character.isSpaceChar(editable.charAt(spanUnionStart - 1)))) {
spanUnionEnd = spanUnionEnd + 1;
}
mTextView.deleteText_internal(spanUnionStart, spanUnionEnd);
}
hide();
return;
}
final int spanStart = editable.getSpanStart(suggestionInfo.suggestionSpan);
final int spanEnd = editable.getSpanEnd(suggestionInfo.suggestionSpan);
if (spanStart < 0 || spanEnd <= spanStart) {
// Span has been removed
hide();
return;
}
final String originalText = editable.toString().substring(spanStart, spanEnd);
if (suggestionInfo.suggestionIndex == ADD_TO_DICTIONARY) {
Intent intent = new Intent(Settings.ACTION_USER_DICTIONARY_INSERT);
intent.putExtra("word", originalText);
intent.putExtra("locale", mTextView.getTextServicesLocale().toString());
// Put a listener to replace the original text with a word which the user
// modified in a user dictionary dialog.
intent.setFlags(intent.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
mTextView.getContext().startActivity(intent);
// There is no way to know if the word was indeed added. Re-check.
// TODO The ExtractEditText should remove the span in the original text instead
editable.removeSpan(suggestionInfo.suggestionSpan);
Selection.setSelection(editable, spanEnd);
updateSpellCheckSpans(spanStart, spanEnd, false);
} else {
// SuggestionSpans are removed by replace: save them before
SuggestionSpan[] suggestionSpans = editable.getSpans(spanStart, spanEnd,
SuggestionSpan.class);
final int length = suggestionSpans.length;
int[] suggestionSpansStarts = new int[length];
int[] suggestionSpansEnds = new int[length];
int[] suggestionSpansFlags = new int[length];
for (int i = 0; i < length; i++) {
final SuggestionSpan suggestionSpan = suggestionSpans[i];
suggestionSpansStarts[i] = editable.getSpanStart(suggestionSpan);
suggestionSpansEnds[i] = editable.getSpanEnd(suggestionSpan);
suggestionSpansFlags[i] = editable.getSpanFlags(suggestionSpan);
// Remove potential misspelled flags
int suggestionSpanFlags = suggestionSpan.getFlags();
if ((suggestionSpanFlags & SuggestionSpan.FLAG_MISSPELLED) > 0) {
suggestionSpanFlags &= ~SuggestionSpan.FLAG_MISSPELLED;
suggestionSpanFlags &= ~SuggestionSpan.FLAG_EASY_CORRECT;
suggestionSpan.setFlags(suggestionSpanFlags);
}
}
final int suggestionStart = suggestionInfo.suggestionStart;
final int suggestionEnd = suggestionInfo.suggestionEnd;
final String suggestion = suggestionInfo.text.subSequence(
suggestionStart, suggestionEnd).toString();
mTextView.replaceText_internal(spanStart, spanEnd, suggestion);
// Notify source IME of the suggestion pick. Do this before
// swaping texts.
suggestionInfo.suggestionSpan.notifySelection(
mTextView.getContext(), originalText, suggestionInfo.suggestionIndex);
// Swap text content between actual text and Suggestion span
String[] suggestions = suggestionInfo.suggestionSpan.getSuggestions();
suggestions[suggestionInfo.suggestionIndex] = originalText;
// Restore previous SuggestionSpans
final int lengthDifference = suggestion.length() - (spanEnd - spanStart);
for (int i = 0; i < length; i++) {
// Only spans that include the modified region make sense after replacement
// Spans partially included in the replaced region are removed, there is no
// way to assign them a valid range after replacement
if (suggestionSpansStarts[i] <= spanStart &&
suggestionSpansEnds[i] >= spanEnd) {
mTextView.setSpan_internal(suggestionSpans[i], suggestionSpansStarts[i],
suggestionSpansEnds[i] + lengthDifference, suggestionSpansFlags[i]);
}
}
// Move cursor at the end of the replaced word
final int newCursorPosition = spanEnd + lengthDifference;
mTextView.setCursorPosition_internal(newCursorPosition, newCursorPosition);
}
hide();
}
}
/**
* An ActionMode Callback class that is used to provide actions while in text selection mode.
*
* The default callback provides a subset of Select All, Cut, Copy and Paste actions, depending
* on which of these this TextView supports.
*/
private class SelectionActionModeCallback extends ActionMode.Callback2 {
private final Path mSelectionPath = new Path();
private final RectF mSelectionBounds = new RectF();
private int mSelectionHandleHeight;
private int mInsertionHandleHeight;
public SelectionActionModeCallback() {
SelectionModifierCursorController selectionController = getSelectionController();
if (selectionController.mStartHandle == null) {
selectionController.initDrawables();
selectionController.initHandles();
}
mSelectionHandleHeight = Math.max(
mSelectHandleLeft.getMinimumHeight(), mSelectHandleRight.getMinimumHeight());
InsertionPointCursorController insertionController = getInsertionController();
if (insertionController != null) {
insertionController.getHandle();
mInsertionHandleHeight = mSelectHandleCenter.getMinimumHeight();
}
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.setTitle(mTextView.getContext().getString(
com.android.internal.R.string.textSelectionCABTitle));
mode.setSubtitle(null);
mode.setTitleOptionalHint(true);
populateMenuWithItems(menu);
if (mCustomSelectionActionModeCallback != null) {
if (!mCustomSelectionActionModeCallback.onCreateActionMode(mode, menu)) {
// The custom mode can choose to cancel the action mode
return false;
}
}
addIntentMenuItemsForTextProcessing(menu);
if (menu.hasVisibleItems() || mode.getCustomView() != null) {
mTextView.setHasTransientState(true);
return true;
} else {
return false;
}
}
private void populateMenuWithItems(Menu menu) {
final boolean legacy = mTextView.getContext().getApplicationInfo().targetSdkVersion <
Build.VERSION_CODES.LOLLIPOP;
final Context context = !legacy && menu instanceof MenuBuilder ?
((MenuBuilder) menu).getContext() :
mTextView.getContext();
final TypedArray styledAttributes = context.obtainStyledAttributes(
com.android.internal.R.styleable.SelectionModeDrawables);
if (mTextView.canCut()) {
menu.add(0, TextView.ID_CUT, 0, com.android.internal.R.string.cut).
setIcon(styledAttributes.getResourceId(
R.styleable.SelectionModeDrawables_actionModeCutDrawable, 0)).
setAlphabeticShortcut('x').
setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
if (mTextView.canCopy()) {
menu.add(0, TextView.ID_COPY, 0, com.android.internal.R.string.copy).
setIcon(styledAttributes.getResourceId(
R.styleable.SelectionModeDrawables_actionModeCopyDrawable, 0)).
setAlphabeticShortcut('c').
setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
if (mTextView.canPaste()) {
menu.add(0, TextView.ID_PASTE, 0, com.android.internal.R.string.paste).
setIcon(styledAttributes.getResourceId(
R.styleable.SelectionModeDrawables_actionModePasteDrawable, 0)).
setAlphabeticShortcut('v').
setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
menu.add(0, TextView.ID_SELECT_ALL, 0, com.android.internal.R.string.selectAll).
setIcon(styledAttributes.getResourceId(
R.styleable.SelectionModeDrawables_actionModeSelectAllDrawable, 0)).
setAlphabeticShortcut('a').
setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
updateReplaceItem(menu);
styledAttributes.recycle();
}
private void addIntentMenuItemsForTextProcessing(Menu menu) {
if (mTextView.canProcessText()) {
PackageManager packageManager = mTextView.getContext().getPackageManager();
List<ResolveInfo> supportedActivities =
packageManager.queryIntentActivities(createProcessTextIntent(), 0);
for (ResolveInfo info : supportedActivities) {
menu.add(info.loadLabel(packageManager))
.setIntent(createProcessTextIntentForResolveInfo(info))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM
| MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
}
}
private Intent createProcessTextIntent() {
return new Intent()
.setAction(Intent.ACTION_PROCESS_TEXT)
.setType("text/plain");
}
private Intent createProcessTextIntentForResolveInfo(ResolveInfo info) {
return createProcessTextIntent()
.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, !mTextView.isTextEditable())
.setClassName(info.activityInfo.packageName, info.activityInfo.name);
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
updateReplaceItem(menu);
if (mCustomSelectionActionModeCallback != null) {
return mCustomSelectionActionModeCallback.onPrepareActionMode(mode, menu);
}
return true;
}
private void updateReplaceItem(Menu menu) {
boolean canReplace = mTextView.isSuggestionsEnabled() && isCursorInsideSuggestionSpan();
boolean replaceItemExists = menu.findItem(TextView.ID_REPLACE) != null;
if (canReplace && !replaceItemExists) {
menu.add(0, TextView.ID_REPLACE, 0, com.android.internal.R.string.replace).
setShowAsAction(
MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
} else if (!canReplace && replaceItemExists) {
menu.removeItem(TextView.ID_REPLACE);
}
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
if (item.getIntent() != null
&& item.getIntent().getAction().equals(Intent.ACTION_PROCESS_TEXT)) {
item.getIntent().putExtra(Intent.EXTRA_PROCESS_TEXT, mTextView.getSelectedText());
mTextView.startActivityForResult(
item.getIntent(), TextView.PROCESS_TEXT_REQUEST_CODE);
return true;
}
if (mCustomSelectionActionModeCallback != null &&
mCustomSelectionActionModeCallback.onActionItemClicked(mode, item)) {
return true;
}
if (item.getItemId() == TextView.ID_REPLACE) {
onReplace();
return true;
}
return mTextView.onTextContextMenuItem(item.getItemId());
}
@Override
public void onDestroyActionMode(ActionMode mode) {
if (mCustomSelectionActionModeCallback != null) {
mCustomSelectionActionModeCallback.onDestroyActionMode(mode);
}
/*
* If we're ending this mode because we're detaching from a window,
* we still have selection state to preserve. Don't clear it, we'll
* bring back the selection mode when (if) we get reattached.
*/
if (!mPreserveDetachedSelection) {
Selection.setSelection((Spannable) mTextView.getText(),
mTextView.getSelectionEnd());
mTextView.setHasTransientState(false);
}
if (mSelectionModifierCursorController != null) {
mSelectionModifierCursorController.hide();
}
mSelectionActionMode = null;
}
@Override
public void onGetContentRect(ActionMode mode, View view, Rect outRect) {
if (!view.equals(mTextView) || mTextView.getLayout() == null) {
super.onGetContentRect(mode, view, outRect);
return;
}
if (mTextView.getSelectionStart() != mTextView.getSelectionEnd()) {
// We have a selection.
mSelectionPath.reset();
mTextView.getLayout().getSelectionPath(
mTextView.getSelectionStart(), mTextView.getSelectionEnd(), mSelectionPath);
mSelectionPath.computeBounds(mSelectionBounds, true);
mSelectionBounds.bottom += mSelectionHandleHeight;
} else if (mCursorCount == 2) {
// We have a split cursor. In this case, we take the rectangle that includes both
// parts of the cursor to ensure we don't obscure either of them.
Rect firstCursorBounds = mCursorDrawable[0].getBounds();
Rect secondCursorBounds = mCursorDrawable[1].getBounds();
mSelectionBounds.set(
Math.min(firstCursorBounds.left, secondCursorBounds.left),
Math.min(firstCursorBounds.top, secondCursorBounds.top),
Math.max(firstCursorBounds.right, secondCursorBounds.right),
Math.max(firstCursorBounds.bottom, secondCursorBounds.bottom)
+ mInsertionHandleHeight);
} else {
// We have a single cursor.
int line = mTextView.getLayout().getLineForOffset(mTextView.getSelectionStart());
float primaryHorizontal =
mTextView.getLayout().getPrimaryHorizontal(mTextView.getSelectionStart());
mSelectionBounds.set(
primaryHorizontal,
mTextView.getLayout().getLineTop(line),
primaryHorizontal + 1,
mTextView.getLayout().getLineTop(line + 1) + mInsertionHandleHeight);
}
// Take TextView's padding and scroll into account.
int textHorizontalOffset = mTextView.viewportToContentHorizontalOffset();
int textVerticalOffset = mTextView.viewportToContentVerticalOffset();
outRect.set(
(int) Math.floor(mSelectionBounds.left + textHorizontalOffset),
(int) Math.floor(mSelectionBounds.top + textVerticalOffset),
(int) Math.ceil(mSelectionBounds.right + textHorizontalOffset),
(int) Math.ceil(mSelectionBounds.bottom + textVerticalOffset));
}
}
private void onReplace() {
int middle = (mTextView.getSelectionStart() + mTextView.getSelectionEnd()) / 2;
stopSelectionActionMode();
Selection.setSelection((Spannable) mTextView.getText(), middle);
showSuggestions();
}
/**
* A listener to call {@link InputMethodManager#updateCursorAnchorInfo(View, CursorAnchorInfo)}
* while the input method is requesting the cursor/anchor position. Does nothing as long as
* {@link InputMethodManager#isWatchingCursor(View)} returns false.
*/
private final class CursorAnchorInfoNotifier implements TextViewPositionListener {
final CursorAnchorInfo.Builder mSelectionInfoBuilder = new CursorAnchorInfo.Builder();
final int[] mTmpIntOffset = new int[2];
final Matrix mViewToScreenMatrix = new Matrix();
@Override
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled) {
final InputMethodState ims = mInputMethodState;
if (ims == null || ims.mBatchEditNesting > 0) {
return;
}
final InputMethodManager imm = InputMethodManager.peekInstance();
if (null == imm) {
return;
}
if (!imm.isActive(mTextView)) {
return;
}
// Skip if the IME has not requested the cursor/anchor position.
if (!imm.isCursorAnchorInfoEnabled()) {
return;
}
Layout layout = mTextView.getLayout();
if (layout == null) {
return;
}
final CursorAnchorInfo.Builder builder = mSelectionInfoBuilder;
builder.reset();
final int selectionStart = mTextView.getSelectionStart();
builder.setSelectionRange(selectionStart, mTextView.getSelectionEnd());
// Construct transformation matrix from view local coordinates to screen coordinates.
mViewToScreenMatrix.set(mTextView.getMatrix());
mTextView.getLocationOnScreen(mTmpIntOffset);
mViewToScreenMatrix.postTranslate(mTmpIntOffset[0], mTmpIntOffset[1]);
builder.setMatrix(mViewToScreenMatrix);
final float viewportToContentHorizontalOffset =
mTextView.viewportToContentHorizontalOffset();
final float viewportToContentVerticalOffset =
mTextView.viewportToContentVerticalOffset();
final CharSequence text = mTextView.getText();
if (text instanceof Spannable) {
final Spannable sp = (Spannable) text;
int composingTextStart = EditableInputConnection.getComposingSpanStart(sp);
int composingTextEnd = EditableInputConnection.getComposingSpanEnd(sp);
if (composingTextEnd < composingTextStart) {
final int temp = composingTextEnd;
composingTextEnd = composingTextStart;
composingTextStart = temp;
}
final boolean hasComposingText =
(0 <= composingTextStart) && (composingTextStart < composingTextEnd);
if (hasComposingText) {
final CharSequence composingText = text.subSequence(composingTextStart,
composingTextEnd);
builder.setComposingText(composingTextStart, composingText);
final int minLine = layout.getLineForOffset(composingTextStart);
final int maxLine = layout.getLineForOffset(composingTextEnd - 1);
for (int line = minLine; line <= maxLine; ++line) {
final int lineStart = layout.getLineStart(line);
final int lineEnd = layout.getLineEnd(line);
final int offsetStart = Math.max(lineStart, composingTextStart);
final int offsetEnd = Math.min(lineEnd, composingTextEnd);
final boolean ltrLine =
layout.getParagraphDirection(line) == Layout.DIR_LEFT_TO_RIGHT;
final float[] widths = new float[offsetEnd - offsetStart];
layout.getPaint().getTextWidths(text, offsetStart, offsetEnd, widths);
final float top = layout.getLineTop(line);
final float bottom = layout.getLineBottom(line);
for (int offset = offsetStart; offset < offsetEnd; ++offset) {
final float charWidth = widths[offset - offsetStart];
final boolean isRtl = layout.isRtlCharAt(offset);
final float primary = layout.getPrimaryHorizontal(offset);
final float secondary = layout.getSecondaryHorizontal(offset);
// TODO: This doesn't work perfectly for text with custom styles and
// TAB chars.
final float left;
final float right;
if (ltrLine) {
if (isRtl) {
left = secondary - charWidth;
right = secondary;
} else {
left = primary;
right = primary + charWidth;
}
} else {
if (!isRtl) {
left = secondary;
right = secondary + charWidth;
} else {
left = primary - charWidth;
right = primary;
}
}
// TODO: Check top-right and bottom-left as well.
final float localLeft = left + viewportToContentHorizontalOffset;
final float localRight = right + viewportToContentHorizontalOffset;
final float localTop = top + viewportToContentVerticalOffset;
final float localBottom = bottom + viewportToContentVerticalOffset;
final boolean isTopLeftVisible = isPositionVisible(localLeft, localTop);
final boolean isBottomRightVisible =
isPositionVisible(localRight, localBottom);
int characterBoundsFlags = 0;
if (isTopLeftVisible || isBottomRightVisible) {
characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
}
if (!isTopLeftVisible || !isBottomRightVisible) {
characterBoundsFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
}
if (isRtl) {
characterBoundsFlags |= CursorAnchorInfo.FLAG_IS_RTL;
}
// Here offset is the index in Java chars.
builder.addCharacterBounds(offset, localLeft, localTop, localRight,
localBottom, characterBoundsFlags);
}
}
}
}
// Treat selectionStart as the insertion point.
if (0 <= selectionStart) {
final int offset = selectionStart;
final int line = layout.getLineForOffset(offset);
final float insertionMarkerX = layout.getPrimaryHorizontal(offset)
+ viewportToContentHorizontalOffset;
final float insertionMarkerTop = layout.getLineTop(line)
+ viewportToContentVerticalOffset;
final float insertionMarkerBaseline = layout.getLineBaseline(line)
+ viewportToContentVerticalOffset;
final float insertionMarkerBottom = layout.getLineBottom(line)
+ viewportToContentVerticalOffset;
final boolean isTopVisible =
isPositionVisible(insertionMarkerX, insertionMarkerTop);
final boolean isBottomVisible =
isPositionVisible(insertionMarkerX, insertionMarkerBottom);
int insertionMarkerFlags = 0;
if (isTopVisible || isBottomVisible) {
insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_VISIBLE_REGION;
}
if (!isTopVisible || !isBottomVisible) {
insertionMarkerFlags |= CursorAnchorInfo.FLAG_HAS_INVISIBLE_REGION;
}
if (layout.isRtlCharAt(offset)) {
insertionMarkerFlags |= CursorAnchorInfo.FLAG_IS_RTL;
}
builder.setInsertionMarkerLocation(insertionMarkerX, insertionMarkerTop,
insertionMarkerBaseline, insertionMarkerBottom, insertionMarkerFlags);
}
imm.updateCursorAnchorInfo(mTextView, builder.build());
}
}
private abstract class HandleView extends View implements TextViewPositionListener {
protected Drawable mDrawable;
protected Drawable mDrawableLtr;
protected Drawable mDrawableRtl;
private final PopupWindow mContainer;
// Position with respect to the parent TextView
private int mPositionX, mPositionY;
private boolean mIsDragging;
// Offset from touch position to mPosition
private float mTouchToWindowOffsetX, mTouchToWindowOffsetY;
protected int mHotspotX;
protected int mHorizontalGravity;
// Offsets the hotspot point up, so that cursor is not hidden by the finger when moving up
private float mTouchOffsetY;
// Where the touch position should be on the handle to ensure a maximum cursor visibility
private float mIdealVerticalOffset;
// Parent's (TextView) previous position in window
private int mLastParentX, mLastParentY;
// Previous text character offset
protected int mPreviousOffset = -1;
// Previous text character offset
private boolean mPositionHasChanged = true;
// Minimum touch target size for handles
private int mMinSize;
// Indicates the line of text that the handle is on.
protected int mPrevLine = -1;
public HandleView(Drawable drawableLtr, Drawable drawableRtl) {
super(mTextView.getContext());
mContainer = new PopupWindow(mTextView.getContext(), null,
com.android.internal.R.attr.textSelectHandleWindowStyle);
mContainer.setSplitTouchEnabled(true);
mContainer.setClippingEnabled(false);
mContainer.setWindowLayoutType(WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL);
mContainer.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
mContainer.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mContainer.setContentView(this);
mDrawableLtr = drawableLtr;
mDrawableRtl = drawableRtl;
mMinSize = mTextView.getContext().getResources().getDimensionPixelSize(
com.android.internal.R.dimen.text_handle_min_size);
updateDrawable();
final int handleHeight = getPreferredHeight();
mTouchOffsetY = -0.3f * handleHeight;
mIdealVerticalOffset = 0.7f * handleHeight;
}
protected void updateDrawable() {
final int offset = getCurrentCursorOffset();
final boolean isRtlCharAtOffset = mTextView.getLayout().isRtlCharAt(offset);
mDrawable = isRtlCharAtOffset ? mDrawableRtl : mDrawableLtr;
mHotspotX = getHotspotX(mDrawable, isRtlCharAtOffset);
mHorizontalGravity = getHorizontalGravity(isRtlCharAtOffset);
}
protected abstract int getHotspotX(Drawable drawable, boolean isRtlRun);
protected abstract int getHorizontalGravity(boolean isRtlRun);
// Touch-up filter: number of previous positions remembered
private static final int HISTORY_SIZE = 5;
private static final int TOUCH_UP_FILTER_DELAY_AFTER = 150;
private static final int TOUCH_UP_FILTER_DELAY_BEFORE = 350;
private final long[] mPreviousOffsetsTimes = new long[HISTORY_SIZE];
private final int[] mPreviousOffsets = new int[HISTORY_SIZE];
private int mPreviousOffsetIndex = 0;
private int mNumberPreviousOffsets = 0;
private void startTouchUpFilter(int offset) {
mNumberPreviousOffsets = 0;
addPositionToTouchUpFilter(offset);
}
private void addPositionToTouchUpFilter(int offset) {
mPreviousOffsetIndex = (mPreviousOffsetIndex + 1) % HISTORY_SIZE;
mPreviousOffsets[mPreviousOffsetIndex] = offset;
mPreviousOffsetsTimes[mPreviousOffsetIndex] = SystemClock.uptimeMillis();
mNumberPreviousOffsets++;
}
private void filterOnTouchUp() {
final long now = SystemClock.uptimeMillis();
int i = 0;
int index = mPreviousOffsetIndex;
final int iMax = Math.min(mNumberPreviousOffsets, HISTORY_SIZE);
while (i < iMax && (now - mPreviousOffsetsTimes[index]) < TOUCH_UP_FILTER_DELAY_AFTER) {
i++;
index = (mPreviousOffsetIndex - i + HISTORY_SIZE) % HISTORY_SIZE;
}
if (i > 0 && i < iMax &&
(now - mPreviousOffsetsTimes[index]) > TOUCH_UP_FILTER_DELAY_BEFORE) {
positionAtCursorOffset(mPreviousOffsets[index], false);
}
}
public boolean offsetHasBeenChanged() {
return mNumberPreviousOffsets > 1;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getPreferredWidth(), getPreferredHeight());
}
private int getPreferredWidth() {
return Math.max(mDrawable.getIntrinsicWidth(), mMinSize);
}
private int getPreferredHeight() {
return Math.max(mDrawable.getIntrinsicHeight(), mMinSize);
}
public void show() {
if (isShowing()) return;
getPositionListener().addSubscriber(this, true /* local position may change */);
// Make sure the offset is always considered new, even when focusing at same position
mPreviousOffset = -1;
positionAtCursorOffset(getCurrentCursorOffset(), false);
}
protected void dismiss() {
mIsDragging = false;
mContainer.dismiss();
onDetached();
}
public void hide() {
dismiss();
getPositionListener().removeSubscriber(this);
}
public boolean isShowing() {
return mContainer.isShowing();
}
private boolean isVisible() {
// Always show a dragging handle.
if (mIsDragging) {
return true;
}
if (mTextView.isInBatchEditMode()) {
return false;
}
return isPositionVisible(mPositionX + mHotspotX, mPositionY);
}
public abstract int getCurrentCursorOffset();
protected abstract void updateSelection(int offset);
public abstract void updatePosition(float x, float y);
protected void positionAtCursorOffset(int offset, boolean parentScrolled) {
// A HandleView relies on the layout, which may be nulled by external methods
Layout layout = mTextView.getLayout();
if (layout == null) {
// Will update controllers' state, hiding them and stopping selection mode if needed
prepareCursorControllers();
return;
}
boolean offsetChanged = offset != mPreviousOffset;
if (offsetChanged || parentScrolled) {
if (offsetChanged) {
updateSelection(offset);
addPositionToTouchUpFilter(offset);
}
final int line = layout.getLineForOffset(offset);
mPrevLine = line;
mPositionX = (int) (layout.getPrimaryHorizontal(offset) - 0.5f - mHotspotX -
getHorizontalOffset() + getCursorOffset());
mPositionY = layout.getLineBottom(line);
// Take TextView's padding and scroll into account.
mPositionX += mTextView.viewportToContentHorizontalOffset();
mPositionY += mTextView.viewportToContentVerticalOffset();
mPreviousOffset = offset;
mPositionHasChanged = true;
}
}
public void updatePosition(int parentPositionX, int parentPositionY,
boolean parentPositionChanged, boolean parentScrolled) {
positionAtCursorOffset(getCurrentCursorOffset(), parentScrolled);
if (parentPositionChanged || mPositionHasChanged) {
if (mIsDragging) {
// Update touchToWindow offset in case of parent scrolling while dragging
if (parentPositionX != mLastParentX || parentPositionY != mLastParentY) {
mTouchToWindowOffsetX += parentPositionX - mLastParentX;
mTouchToWindowOffsetY += parentPositionY - mLastParentY;
mLastParentX = parentPositionX;
mLastParentY = parentPositionY;
}
onHandleMoved();
}
if (isVisible()) {
final int positionX = parentPositionX + mPositionX;
final int positionY = parentPositionY + mPositionY;
if (isShowing()) {
mContainer.update(positionX, positionY, -1, -1);
} else {
mContainer.showAtLocation(mTextView, Gravity.NO_GRAVITY,
positionX, positionY);
}
} else {
if (isShowing()) {
dismiss();
}
}
mPositionHasChanged = false;
}
}
public void showAtLocation(int offset) {
// TODO - investigate if there's a better way to show the handles
// after the drag accelerator has occured.
int[] tmpCords = new int[2];
mTextView.getLocationInWindow(tmpCords);
Layout layout = mTextView.getLayout();
int posX = tmpCords[0];
int posY = tmpCords[1];
final int line = layout.getLineForOffset(offset);
int startX = (int) (layout.getPrimaryHorizontal(offset) - 0.5f
- mHotspotX - getHorizontalOffset() + getCursorOffset());
int startY = layout.getLineBottom(line);
// Take TextView's padding and scroll into account.
startX += mTextView.viewportToContentHorizontalOffset();
startY += mTextView.viewportToContentVerticalOffset();
mContainer.showAtLocation(mTextView, Gravity.NO_GRAVITY,
startX + posX, startY + posY);
}
@Override
protected void onDraw(Canvas c) {
final int drawWidth = mDrawable.getIntrinsicWidth();
final int left = getHorizontalOffset();
mDrawable.setBounds(left, 0, left + drawWidth, mDrawable.getIntrinsicHeight());
mDrawable.draw(c);
}
private int getHorizontalOffset() {
final int width = getPreferredWidth();
final int drawWidth = mDrawable.getIntrinsicWidth();
final int left;
switch (mHorizontalGravity) {
case Gravity.LEFT:
left = 0;
break;
default:
case Gravity.CENTER:
left = (width - drawWidth) / 2;
break;
case Gravity.RIGHT:
left = width - drawWidth;
break;
}
return left;
}
protected int getCursorOffset() {
return 0;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN: {
startTouchUpFilter(getCurrentCursorOffset());
mTouchToWindowOffsetX = ev.getRawX() - mPositionX;
mTouchToWindowOffsetY = ev.getRawY() - mPositionY;
final PositionListener positionListener = getPositionListener();
mLastParentX = positionListener.getPositionX();
mLastParentY = positionListener.getPositionY();
mIsDragging = true;
break;
}
case MotionEvent.ACTION_MOVE: {
final float rawX = ev.getRawX();
final float rawY = ev.getRawY();
// Vertical hysteresis: vertical down movement tends to snap to ideal offset
final float previousVerticalOffset = mTouchToWindowOffsetY - mLastParentY;
final float currentVerticalOffset = rawY - mPositionY - mLastParentY;
float newVerticalOffset;
if (previousVerticalOffset < mIdealVerticalOffset) {
newVerticalOffset = Math.min(currentVerticalOffset, mIdealVerticalOffset);
newVerticalOffset = Math.max(newVerticalOffset, previousVerticalOffset);
} else {
newVerticalOffset = Math.max(currentVerticalOffset, mIdealVerticalOffset);
newVerticalOffset = Math.min(newVerticalOffset, previousVerticalOffset);
}
mTouchToWindowOffsetY = newVerticalOffset + mLastParentY;
final float newPosX = rawX - mTouchToWindowOffsetX + mHotspotX;
final float newPosY = rawY - mTouchToWindowOffsetY + mTouchOffsetY;
updatePosition(newPosX, newPosY);
break;
}
case MotionEvent.ACTION_UP:
filterOnTouchUp();
mIsDragging = false;
break;
case MotionEvent.ACTION_CANCEL:
mIsDragging = false;
break;
}
return true;
}
public boolean isDragging() {
return mIsDragging;
}
void onHandleMoved() {}
public void onDetached() {}
}
private class InsertionHandleView extends HandleView {
private static final int DELAY_BEFORE_HANDLE_FADES_OUT = 4000;
private static final int RECENT_CUT_COPY_DURATION = 15 * 1000; // seconds
// Used to detect taps on the insertion handle, which will affect the selection action mode
private float mDownPositionX, mDownPositionY;
private Runnable mHider;
public InsertionHandleView(Drawable drawable) {
super(drawable, drawable);
}
@Override
public void show() {
super.show();
final long durationSinceCutOrCopy =
SystemClock.uptimeMillis() - TextView.LAST_CUT_OR_COPY_TIME;
if (durationSinceCutOrCopy < RECENT_CUT_COPY_DURATION) {
startSelectionActionModeWithoutSelection();
}
hideAfterDelay();
}
private void hideAfterDelay() {
if (mHider == null) {
mHider = new Runnable() {
public void run() {
hide();
}
};
} else {
removeHiderCallback();
}
mTextView.postDelayed(mHider, DELAY_BEFORE_HANDLE_FADES_OUT);
}
private void removeHiderCallback() {
if (mHider != null) {
mTextView.removeCallbacks(mHider);
}
}
@Override
protected int getHotspotX(Drawable drawable, boolean isRtlRun) {
return drawable.getIntrinsicWidth() / 2;
}
@Override
protected int getHorizontalGravity(boolean isRtlRun) {
return Gravity.CENTER_HORIZONTAL;
}
@Override
protected int getCursorOffset() {
int offset = super.getCursorOffset();
final Drawable cursor = mCursorCount > 0 ? mCursorDrawable[0] : null;
if (cursor != null) {
cursor.getPadding(mTempRect);
offset += (cursor.getIntrinsicWidth() - mTempRect.left - mTempRect.right) / 2;
}
return offset;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final boolean result = super.onTouchEvent(ev);
switch (ev.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
mDownPositionX = ev.getRawX();
mDownPositionY = ev.getRawY();
break;
case MotionEvent.ACTION_UP:
if (!offsetHasBeenChanged()) {
final float deltaX = mDownPositionX - ev.getRawX();
final float deltaY = mDownPositionY - ev.getRawY();
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
final ViewConfiguration viewConfiguration = ViewConfiguration.get(
mTextView.getContext());
final int touchSlop = viewConfiguration.getScaledTouchSlop();
if (distanceSquared < touchSlop * touchSlop) {
// Tapping on the handle toggles the selection action mode.
if (mSelectionActionMode != null) {
mSelectionActionMode.finish();
} else {
startSelectionActionModeWithoutSelection();
}
}
}
hideAfterDelay();
break;
case MotionEvent.ACTION_CANCEL:
hideAfterDelay();
break;
default:
break;
}
return result;
}
@Override
public int getCurrentCursorOffset() {
return mTextView.getSelectionStart();
}
@Override
public void updateSelection(int offset) {
Selection.setSelection((Spannable) mTextView.getText(), offset);
}
@Override
public void updatePosition(float x, float y) {
positionAtCursorOffset(mTextView.getOffsetForPosition(x, y), false);
}
@Override
void onHandleMoved() {
super.onHandleMoved();
removeHiderCallback();
}
@Override
public void onDetached() {
super.onDetached();
removeHiderCallback();
}
}
private class SelectionStartHandleView extends HandleView {
// Indicates whether the cursor is making adjustments within a word.
private boolean mInWord = false;
// Offset to track difference between touch and word boundary.
protected int mTouchWordOffset;
public SelectionStartHandleView(Drawable drawableLtr, Drawable drawableRtl) {
super(drawableLtr, drawableRtl);
}
@Override
protected int getHotspotX(Drawable drawable, boolean isRtlRun) {
return isRtlRun ? 0 : drawable.getIntrinsicWidth();
}
@Override
protected int getHorizontalGravity(boolean isRtlRun) {
return isRtlRun ? Gravity.RIGHT : Gravity.LEFT;
}
@Override
public int getCurrentCursorOffset() {
return mTextView.getSelectionStart();
}
@Override
public void updateSelection(int offset) {
Selection.setSelection((Spannable) mTextView.getText(), offset,
mTextView.getSelectionEnd());
updateDrawable();
if (mSelectionActionMode != null) {
mSelectionActionMode.invalidate();
}
}
@Override
public void updatePosition(float x, float y) {
final int trueOffset = mTextView.getOffsetForPosition(x, y);
final int currLine = mTextView.getLineAtCoordinate(y);
// Don't select white space on different lines.
if (isWhitespaceLine(mPrevLine, currLine, trueOffset)) return;
boolean positionCursor = false;
int offset = trueOffset;
int end = getWordEnd(offset, true);
int start = getWordStart(offset);
if (offset < mPreviousOffset) {
// User is increasing the selection.
if (!mInWord || currLine < mPrevLine) {
// We're not in a word, or we're on a different line so we'll expand by
// word. First ensure the user has at least entered the next word.
int offsetToWord = Math.min((end - start) / 2, 2);
if (offset <= end - offsetToWord || currLine < mPrevLine) {
offset = start;
} else {
offset = mPreviousOffset;
}
}
mTouchWordOffset = Math.max(trueOffset - offset, 0);
mInWord = !isStartBoundary(offset);
positionCursor = true;
} else if (offset - mTouchWordOffset > mPreviousOffset) {
// User is shrinking the selection.
if (currLine > mPrevLine) {
// We're on a different line, so we'll snap to word boundaries.
offset = end;
}
offset -= mTouchWordOffset;
mInWord = !isEndBoundary(offset);
positionCursor = true;
}
// Handles can not cross and selection is at least one character.
if (positionCursor) {
final int selectionEnd = mTextView.getSelectionEnd();
if (offset >= selectionEnd) {
// We can't cross the handles so let's just constrain the Y value.
int alteredOffset = mTextView.getOffsetAtCoordinate(mPrevLine, x);
if (alteredOffset >= selectionEnd) {
// Can't pass the other drag handle.
offset = Math.max(0, selectionEnd - 1);
} else {
offset = alteredOffset;
}
}
positionAtCursorOffset(offset, false);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean superResult = super.onTouchEvent(event);
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
// Reset the touch word offset when the user has lifted their finger.
mTouchWordOffset = 0;
}
return superResult;
}
}
private class SelectionEndHandleView extends HandleView {
// Indicates whether the cursor is making adjustments within a word.
private boolean mInWord = false;
// Offset to track difference between touch and word boundary.
protected int mTouchWordOffset;
public SelectionEndHandleView(Drawable drawableLtr, Drawable drawableRtl) {
super(drawableLtr, drawableRtl);
}
@Override
protected int getHotspotX(Drawable drawable, boolean isRtlRun) {
return isRtlRun ? drawable.getIntrinsicWidth() : 0;
}
@Override
protected int getHorizontalGravity(boolean isRtlRun) {
return isRtlRun ? Gravity.LEFT : Gravity.RIGHT;
}
@Override
public int getCurrentCursorOffset() {
return mTextView.getSelectionEnd();
}
@Override
public void updateSelection(int offset) {
Selection.setSelection((Spannable) mTextView.getText(),
mTextView.getSelectionStart(), offset);
if (mSelectionActionMode != null) {
mSelectionActionMode.invalidate();
}
updateDrawable();
}
@Override
public void updatePosition(float x, float y) {
final int trueOffset = mTextView.getOffsetForPosition(x, y);
final int currLine = mTextView.getLineAtCoordinate(y);
// Don't select white space on different lines.
if (isWhitespaceLine(mPrevLine, currLine, trueOffset)) return;
int offset = trueOffset;
boolean positionCursor = false;
int end = getWordEnd(offset, true);
int start = getWordStart(offset);
if (offset > mPreviousOffset) {
// User is increasing the selection.
if (!mInWord || currLine > mPrevLine) {
// We're not in a word, or we're on a different line so we'll expand by
// word. First ensure the user has at least entered the next word.
int midPoint = Math.min((end - start) / 2, 2);
if (offset >= start + midPoint || currLine > mPrevLine) {
offset = end;
} else {
offset = mPreviousOffset;
}
}
mTouchWordOffset = Math.max(offset - trueOffset, 0);
mInWord = !isEndBoundary(offset);
positionCursor = true;
} else if (offset + mTouchWordOffset < mPreviousOffset) {
// User is shrinking the selection.
if (currLine > mPrevLine) {
// We're on a different line, so we'll snap to word boundaries.
offset = getWordStart(offset);
}
offset += mTouchWordOffset;
positionCursor = true;
mInWord = !isStartBoundary(offset);
}
if (positionCursor) {
final int selectionStart = mTextView.getSelectionStart();
if (offset <= selectionStart) {
// We can't cross the handles so let's just constrain the Y value.
int alteredOffset = mTextView.getOffsetAtCoordinate(mPrevLine, x);
int length = mTextView.getText().length();
if (alteredOffset <= selectionStart) {
// Can't pass the other drag handle.
offset = Math.min(selectionStart + 1, length);
} else {
offset = Math.min(alteredOffset, length);
}
}
positionAtCursorOffset(offset, false);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean superResult = super.onTouchEvent(event);
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
// Reset the touch word offset when the user has lifted their finger.
mTouchWordOffset = 0;
}
return superResult;
}
}
/**
* Checks whether selection is happening on a different line than previous and
* if that line only contains whitespace up to the touch location.
*
* @param prevLine The previous line the selection was on.
* @param currLine The current line being selected.
* @param offset The offset in the text where the touch occurred.
* @return Whether or not it was just a white space line being selected.
*/
private boolean isWhitespaceLine(int prevLine, int currLine, int offset) {
if (prevLine == currLine) {
// Same line; don't care.
return false;
}
CharSequence text = mTextView.getText();
if (offset == text.length()) {
// No character at the last position.
return false;
}
int lineEndOffset = mTextView.getLayout().getLineEnd(currLine);
for (int cp, i = offset; i < lineEndOffset; i += Character.charCount(cp)) {
cp = Character.codePointAt(text, i);
if (!Character.isSpaceChar(cp) && !Character.isWhitespace(cp)) {
// There are non white space chars on the line.
return false;
}
}
return true;
}
/**
* A CursorController instance can be used to control a cursor in the text.
*/
private interface CursorController extends ViewTreeObserver.OnTouchModeChangeListener {
/**
* Makes the cursor controller visible on screen.
* See also {@link #hide()}.
*/
public void show();
/**
* Hide the cursor controller from screen.
* See also {@link #show()}.
*/
public void hide();
/**
* Called when the view is detached from window. Perform house keeping task, such as
* stopping Runnable thread that would otherwise keep a reference on the context, thus
* preventing the activity from being recycled.
*/
public void onDetached();
}
private class InsertionPointCursorController implements CursorController {
private InsertionHandleView mHandle;
public void show() {
getHandle().show();
}
public void hide() {
if (mHandle != null) {
mHandle.hide();
}
}
public void onTouchModeChanged(boolean isInTouchMode) {
if (!isInTouchMode) {
hide();
}
}
private InsertionHandleView getHandle() {
if (mSelectHandleCenter == null) {
mSelectHandleCenter = mTextView.getContext().getDrawable(
mTextView.mTextSelectHandleRes);
}
if (mHandle == null) {
mHandle = new InsertionHandleView(mSelectHandleCenter);
}
return mHandle;
}
@Override
public void onDetached() {
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.removeOnTouchModeChangeListener(this);
if (mHandle != null) mHandle.onDetached();
}
}
class SelectionModifierCursorController implements CursorController {
// The cursor controller handles, lazily created when shown.
private SelectionStartHandleView mStartHandle;
private SelectionEndHandleView mEndHandle;
// The offsets of that last touch down event. Remembered to start selection there.
private int mMinTouchOffset, mMaxTouchOffset;
// Double tap detection
private long mPreviousTapUpTime = 0;
private float mDownPositionX, mDownPositionY;
private boolean mGestureStayedInTapRegion;
// Where the user first starts the drag motion.
private int mStartOffset = -1;
// Indicates whether the user is selecting text and using the drag accelerator.
private boolean mDragAcceleratorActive;
// Indicates the line of text the drag accelerator is on.
private int mPrevLine = -1;
SelectionModifierCursorController() {
resetTouchOffsets();
}
public void show() {
if (mTextView.isInBatchEditMode()) {
return;
}
initDrawables();
initHandles();
hideInsertionPointCursorController();
}
private void initDrawables() {
if (mSelectHandleLeft == null) {
mSelectHandleLeft = mTextView.getContext().getDrawable(
mTextView.mTextSelectHandleLeftRes);
}
if (mSelectHandleRight == null) {
mSelectHandleRight = mTextView.getContext().getDrawable(
mTextView.mTextSelectHandleRightRes);
}
}
private void initHandles() {
// Lazy object creation has to be done before updatePosition() is called.
if (mStartHandle == null) {
mStartHandle = new SelectionStartHandleView(mSelectHandleLeft, mSelectHandleRight);
}
if (mEndHandle == null) {
mEndHandle = new SelectionEndHandleView(mSelectHandleRight, mSelectHandleLeft);
}
mStartHandle.show();
mEndHandle.show();
hideInsertionPointCursorController();
}
public void hide() {
if (mStartHandle != null) mStartHandle.hide();
if (mEndHandle != null) mEndHandle.hide();
}
public void enterDrag() {
// Just need to init the handles / hide insertion cursor.
show();
mDragAcceleratorActive = true;
// Start location of selection.
mStartOffset = mTextView.getOffsetForPosition(mLastDownPositionX,
mLastDownPositionY);
// Don't show the handles until user has lifted finger.
hide();
// This stops scrolling parents from intercepting the touch event, allowing
// the user to continue dragging across the screen to select text; TextView will
// scroll as necessary.
mTextView.getParent().requestDisallowInterceptTouchEvent(true);
}
public void onTouchEvent(MotionEvent event) {
// This is done even when the View does not have focus, so that long presses can start
// selection and tap can move cursor from this tap position.
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
final float x = event.getX();
final float y = event.getY();
// Remember finger down position, to be able to start selection from there.
mMinTouchOffset = mMaxTouchOffset = mTextView.getOffsetForPosition(x, y);
// Double tap detection
if (mGestureStayedInTapRegion) {
long duration = SystemClock.uptimeMillis() - mPreviousTapUpTime;
if (duration <= ViewConfiguration.getDoubleTapTimeout()) {
final float deltaX = x - mDownPositionX;
final float deltaY = y - mDownPositionY;
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
ViewConfiguration viewConfiguration = ViewConfiguration.get(
mTextView.getContext());
int doubleTapSlop = viewConfiguration.getScaledDoubleTapSlop();
boolean stayedInArea = distanceSquared < doubleTapSlop * doubleTapSlop;
if (stayedInArea && isPositionOnText(x, y)) {
startSelectionActionModeWithSelection();
mDiscardNextActionUp = true;
}
}
}
// New selection, reset line.
mPrevLine = mTextView.getLineAtCoordinate(y);
mDownPositionX = x;
mDownPositionY = y;
mGestureStayedInTapRegion = true;
break;
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_POINTER_UP:
// Handle multi-point gestures. Keep min and max offset positions.
// Only activated for devices that correctly handle multi-touch.
if (mTextView.getContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH_DISTINCT)) {
updateMinAndMaxOffsets(event);
}
break;
case MotionEvent.ACTION_MOVE:
final ViewConfiguration viewConfiguration = ViewConfiguration.get(
mTextView.getContext());
if (mGestureStayedInTapRegion) {
final float deltaX = event.getX() - mDownPositionX;
final float deltaY = event.getY() - mDownPositionY;
final float distanceSquared = deltaX * deltaX + deltaY * deltaY;
int doubleTapTouchSlop = viewConfiguration.getScaledDoubleTapTouchSlop();
if (distanceSquared > doubleTapTouchSlop * doubleTapTouchSlop) {
mGestureStayedInTapRegion = false;
}
}
if (mStartHandle != null && mStartHandle.isShowing()) {
// Don't do the drag if the handles are showing already.
break;
}
if (mStartOffset != -1) {
final int rawOffset = mTextView.getOffsetForPosition(event.getX(),
event.getY());
int offset = rawOffset;
// We don't start "dragging" until the user is past the initial word that
// gets selected on long press.
int firstWordStart = getWordStart(mStartOffset);
int firstWordEnd = getWordEnd(mStartOffset, false);
if (offset > firstWordEnd || offset < firstWordStart) {
// Basically the goal in the below code is to have the highlight be
// offset so that your finger isn't covering the end point.
int fingerOffset = viewConfiguration.getScaledTouchSlop();
float mx = event.getX();
float my = event.getY();
if (mx > fingerOffset) mx -= fingerOffset;
if (my > fingerOffset) my -= fingerOffset;
offset = mTextView.getOffsetForPosition(mx, my);
int currLine = mTextView.getLineAtCoordinate(my);
// Don't select white space on different lines.
if (isWhitespaceLine(mPrevLine, currLine, offset)) return;
mPrevLine = currLine;
// Perform the check for closeness at edge of view, if we're very close
// don't adjust the offset to be in front of the finger - otherwise the
// user can't select words at the edge.
if (mTextView.getWidth() - fingerOffset > mx) {
// We're going by word, so we need to make sure that the offset
// that we get is within this, so we'll get the previous boundary.
final WordIterator wordIterator = getWordIteratorWithText();
final int precedingOffset = wordIterator.preceding(offset);
if (mStartOffset < offset) {
// Expanding with bottom handle, in this case the selection end
// is before the finger.
offset = Math.max(precedingOffset - 1, 0);
} else {
// Expand with the start handle, in this case the selection
// start is before the finger.
if (precedingOffset == WordIterator.DONE) {
offset = 0;
} else {
offset = wordIterator.preceding(precedingOffset);
}
}
}
if (offset == WordIterator.DONE)
offset = rawOffset;
// Need to adjust start offset based on direction of movement.
int newStart = mStartOffset < offset ? getWordStart(mStartOffset)
: getWordEnd(mStartOffset, true);
Selection.setSelection((Spannable) mTextView.getText(), newStart,
offset);
}
}
break;
case MotionEvent.ACTION_UP:
mPreviousTapUpTime = SystemClock.uptimeMillis();
if (mDragAcceleratorActive) {
// No longer dragging to select text, let the parent intercept events.
mTextView.getParent().requestDisallowInterceptTouchEvent(false);
show();
int startOffset = mTextView.getSelectionStart();
int endOffset = mTextView.getSelectionEnd();
// Since we don't let drag handles pass once they're visible, we need to
// make sure the start / end locations are correct because the user *can*
// switch directions during the initial drag.
if (endOffset < startOffset) {
int tmp = endOffset;
endOffset = startOffset;
startOffset = tmp;
// Also update the selection with the right offsets in this case.
Selection.setSelection((Spannable) mTextView.getText(),
startOffset, endOffset);
}
// Need to do this to display the handles.
mStartHandle.showAtLocation(startOffset);
mEndHandle.showAtLocation(endOffset);
// No longer the first dragging motion, reset.
mDragAcceleratorActive = false;
mStartOffset = -1;
}
break;
}
}
/**
* @param event
*/
private void updateMinAndMaxOffsets(MotionEvent event) {
int pointerCount = event.getPointerCount();
for (int index = 0; index < pointerCount; index++) {
int offset = mTextView.getOffsetForPosition(event.getX(index), event.getY(index));
if (offset < mMinTouchOffset) mMinTouchOffset = offset;
if (offset > mMaxTouchOffset) mMaxTouchOffset = offset;
}
}
public int getMinTouchOffset() {
return mMinTouchOffset;
}
public int getMaxTouchOffset() {
return mMaxTouchOffset;
}
public void resetTouchOffsets() {
mMinTouchOffset = mMaxTouchOffset = -1;
mStartOffset = -1;
mDragAcceleratorActive = false;
}
/**
* @return true iff this controller is currently used to move the selection start.
*/
public boolean isSelectionStartDragged() {
return mStartHandle != null && mStartHandle.isDragging();
}
/**
* @return true if the user is selecting text using the drag accelerator.
*/
public boolean isDragAcceleratorActive() {
return mDragAcceleratorActive;
}
public void onTouchModeChanged(boolean isInTouchMode) {
if (!isInTouchMode) {
hide();
}
}
@Override
public void onDetached() {
final ViewTreeObserver observer = mTextView.getViewTreeObserver();
observer.removeOnTouchModeChangeListener(this);
if (mStartHandle != null) mStartHandle.onDetached();
if (mEndHandle != null) mEndHandle.onDetached();
}
}
private class CorrectionHighlighter {
private final Path mPath = new Path();
private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mStart, mEnd;
private long mFadingStartTime;
private RectF mTempRectF;
private final static int FADE_OUT_DURATION = 400;
public CorrectionHighlighter() {
mPaint.setCompatibilityScaling(mTextView.getResources().getCompatibilityInfo().
applicationScale);
mPaint.setStyle(Paint.Style.FILL);
}
public void highlight(CorrectionInfo info) {
mStart = info.getOffset();
mEnd = mStart + info.getNewText().length();
mFadingStartTime = SystemClock.uptimeMillis();
if (mStart < 0 || mEnd < 0) {
stopAnimation();
}
}
public void draw(Canvas canvas, int cursorOffsetVertical) {
if (updatePath() && updatePaint()) {
if (cursorOffsetVertical != 0) {
canvas.translate(0, cursorOffsetVertical);
}
canvas.drawPath(mPath, mPaint);
if (cursorOffsetVertical != 0) {
canvas.translate(0, -cursorOffsetVertical);
}
invalidate(true); // TODO invalidate cursor region only
} else {
stopAnimation();
invalidate(false); // TODO invalidate cursor region only
}
}
private boolean updatePaint() {
final long duration = SystemClock.uptimeMillis() - mFadingStartTime;
if (duration > FADE_OUT_DURATION) return false;
final float coef = 1.0f - (float) duration / FADE_OUT_DURATION;
final int highlightColorAlpha = Color.alpha(mTextView.mHighlightColor);
final int color = (mTextView.mHighlightColor & 0x00FFFFFF) +
((int) (highlightColorAlpha * coef) << 24);
mPaint.setColor(color);
return true;
}
private boolean updatePath() {
final Layout layout = mTextView.getLayout();
if (layout == null) return false;
// Update in case text is edited while the animation is run
final int length = mTextView.getText().length();
int start = Math.min(length, mStart);
int end = Math.min(length, mEnd);
mPath.reset();
layout.getSelectionPath(start, end, mPath);
return true;
}
private void invalidate(boolean delayed) {
if (mTextView.getLayout() == null) return;
if (mTempRectF == null) mTempRectF = new RectF();
mPath.computeBounds(mTempRectF, false);
int left = mTextView.getCompoundPaddingLeft();
int top = mTextView.getExtendedPaddingTop() + mTextView.getVerticalOffset(true);
if (delayed) {
mTextView.postInvalidateOnAnimation(
left + (int) mTempRectF.left, top + (int) mTempRectF.top,
left + (int) mTempRectF.right, top + (int) mTempRectF.bottom);
} else {
mTextView.postInvalidate((int) mTempRectF.left, (int) mTempRectF.top,
(int) mTempRectF.right, (int) mTempRectF.bottom);
}
}
private void stopAnimation() {
Editor.this.mCorrectionHighlighter = null;
}
}
private static class ErrorPopup extends PopupWindow {
private boolean mAbove = false;
private final TextView mView;
private int mPopupInlineErrorBackgroundId = 0;
private int mPopupInlineErrorAboveBackgroundId = 0;
ErrorPopup(TextView v, int width, int height) {
super(v, width, height);
mView = v;
// Make sure the TextView has a background set as it will be used the first time it is
// shown and positioned. Initialized with below background, which should have
// dimensions identical to the above version for this to work (and is more likely).
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageBackground);
mView.setBackgroundResource(mPopupInlineErrorBackgroundId);
}
void fixDirection(boolean above) {
mAbove = above;
if (above) {
mPopupInlineErrorAboveBackgroundId =
getResourceId(mPopupInlineErrorAboveBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageAboveBackground);
} else {
mPopupInlineErrorBackgroundId = getResourceId(mPopupInlineErrorBackgroundId,
com.android.internal.R.styleable.Theme_errorMessageBackground);
}
mView.setBackgroundResource(above ? mPopupInlineErrorAboveBackgroundId :
mPopupInlineErrorBackgroundId);
}
private int getResourceId(int currentId, int index) {
if (currentId == 0) {
TypedArray styledAttributes = mView.getContext().obtainStyledAttributes(
R.styleable.Theme);
currentId = styledAttributes.getResourceId(index, 0);
styledAttributes.recycle();
}
return currentId;
}
@Override
public void update(int x, int y, int w, int h, boolean force) {
super.update(x, y, w, h, force);
boolean above = isAboveAnchor();
if (above != mAbove) {
fixDirection(above);
}
}
}
static class InputContentType {
int imeOptions = EditorInfo.IME_NULL;
String privateImeOptions;
CharSequence imeActionLabel;
int imeActionId;
Bundle extras;
OnEditorActionListener onEditorActionListener;
boolean enterDown;
}
static class InputMethodState {
Rect mCursorRectInWindow = new Rect();
float[] mTmpOffset = new float[2];
ExtractedTextRequest mExtractedTextRequest;
final ExtractedText mExtractedText = new ExtractedText();
int mBatchEditNesting;
boolean mCursorChanged;
boolean mSelectionModeChanged;
boolean mContentChanged;
int mChangedStart, mChangedEnd, mChangedDelta;
}
/**
* @return True iff (start, end) is a valid range within the text.
*/
private static boolean isValidRange(CharSequence text, int start, int end) {
return 0 <= start && start <= end && end <= text.length();
}
/**
* An InputFilter that monitors text input to maintain undo history. It does not modify the
* text being typed (and hence always returns null from the filter() method).
*/
public static class UndoInputFilter implements InputFilter {
private final Editor mEditor;
// Whether the current filter pass is directly caused by an end-user text edit.
private boolean mIsUserEdit;
// Whether the text field is handling an IME composition. Must be parceled in case the user
// rotates the screen during composition.
private boolean mHasComposition;
public UndoInputFilter(Editor editor) {
mEditor = editor;
}
public void saveInstanceState(Parcel parcel) {
parcel.writeInt(mIsUserEdit ? 1 : 0);
parcel.writeInt(mHasComposition ? 1 : 0);
}
public void restoreInstanceState(Parcel parcel) {
mIsUserEdit = parcel.readInt() != 0;
mHasComposition = parcel.readInt() != 0;
}
/**
* Signals that a user-triggered edit is starting.
*/
public void beginBatchEdit() {
if (DEBUG_UNDO) Log.d(TAG, "beginBatchEdit");
mIsUserEdit = true;
}
public void endBatchEdit() {
if (DEBUG_UNDO) Log.d(TAG, "endBatchEdit");
mIsUserEdit = false;
}
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (DEBUG_UNDO) {
Log.d(TAG, "filter: source=" + source + " (" + start + "-" + end + ") " +
"dest=" + dest + " (" + dstart + "-" + dend + ")");
}
// Check to see if this edit should be tracked for undo.
if (!canUndoEdit(source, start, end, dest, dstart, dend)) {
return null;
}
// Check for and handle IME composition edits.
if (handleCompositionEdit(source, start, end, dstart)) {
return null;
}
// Handle keyboard edits.
handleKeyboardEdit(source, start, end, dest, dstart, dend);
return null;
}
/**
* Returns true iff the edit was handled, either because it should be ignored or because
* this function created an undo operation for it.
*/
private boolean handleCompositionEdit(CharSequence source, int start, int end, int dstart) {
// Ignore edits while the user is composing.
if (isComposition(source)) {
mHasComposition = true;
return true;
}
final boolean hadComposition = mHasComposition;
mHasComposition = false;
// Check for the transition out of the composing state.
if (hadComposition) {
// If there was no text the user canceled composition. Ignore the edit.
if (start == end) {
return true;
}
// Otherwise the user inserted the composition.
String newText = TextUtils.substring(source, start, end);
EditOperation edit = new EditOperation(mEditor, "", dstart, newText);
recordEdit(edit, false /* forceMerge */);
return true;
}
// This was neither a composition event nor a transition out of composing.
return false;
}
private void handleKeyboardEdit(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
// An application may install a TextWatcher to provide additional modifications after
// the initial input filters run (e.g. a credit card formatter that adds spaces to a
// string). This results in multiple filter() calls for what the user considers to be
// a single operation. Always undo the whole set of changes in one step.
final boolean forceMerge = isInTextWatcher();
// Build a new operation with all the information from this edit.
String newText = TextUtils.substring(source, start, end);
String oldText = TextUtils.substring(dest, dstart, dend);
EditOperation edit = new EditOperation(mEditor, oldText, dstart, newText);
recordEdit(edit, forceMerge);
}
/**
* Fetches the last undo operation and checks to see if a new edit should be merged into it.
* If forceMerge is true then the new edit is always merged.
*/
private void recordEdit(EditOperation edit, boolean forceMerge) {
// Fetch the last edit operation and attempt to merge in the new edit.
final UndoManager um = mEditor.mUndoManager;
um.beginUpdate("Edit text");
EditOperation lastEdit = um.getLastOperation(
EditOperation.class, mEditor.mUndoOwner, UndoManager.MERGE_MODE_UNIQUE);
if (lastEdit == null) {
// Add this as the first edit.
if (DEBUG_UNDO) Log.d(TAG, "filter: adding first op " + edit);
um.addOperation(edit, UndoManager.MERGE_MODE_NONE);
} else if (forceMerge) {
// Forced merges take priority because they could be the result of a non-user-edit
// change and this case should not create a new undo operation.
if (DEBUG_UNDO) Log.d(TAG, "filter: force merge " + edit);
lastEdit.forceMergeWith(edit);
} else if (!mIsUserEdit) {
// An application directly modified the Editable outside of a text edit. Treat this
// as a new change and don't attempt to merge.
if (DEBUG_UNDO) Log.d(TAG, "non-user edit, new op " + edit);
um.commitState(mEditor.mUndoOwner);
um.addOperation(edit, UndoManager.MERGE_MODE_NONE);
} else if (lastEdit.mergeWith(edit)) {
// Merge succeeded, nothing else to do.
if (DEBUG_UNDO) Log.d(TAG, "filter: merge succeeded, created " + lastEdit);
} else {
// Could not merge with the last edit, so commit the last edit and add this edit.
if (DEBUG_UNDO) Log.d(TAG, "filter: merge failed, adding " + edit);
um.commitState(mEditor.mUndoOwner);
um.addOperation(edit, UndoManager.MERGE_MODE_NONE);
}
um.endUpdate();
}
private boolean canUndoEdit(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
if (!mEditor.mAllowUndo) {
if (DEBUG_UNDO) Log.d(TAG, "filter: undo is disabled");
return false;
}
if (mEditor.mUndoManager.isInUndo()) {
if (DEBUG_UNDO) Log.d(TAG, "filter: skipping, currently performing undo/redo");
return false;
}
// Text filters run before input operations are applied. However, some input operations
// are invalid and will throw exceptions when applied. This is common in tests. Don't
// attempt to undo invalid operations.
if (!isValidRange(source, start, end) || !isValidRange(dest, dstart, dend)) {
if (DEBUG_UNDO) Log.d(TAG, "filter: invalid op");
return false;
}
// Earlier filters can rewrite input to be a no-op, for example due to a length limit
// on an input field. Skip no-op changes.
if (start == end && dstart == dend) {
if (DEBUG_UNDO) Log.d(TAG, "filter: skipping no-op");
return false;
}
return true;
}
private boolean isComposition(CharSequence source) {
if (!(source instanceof Spannable)) {
return false;
}
// This is a composition edit if the source has a non-zero-length composing span.
Spannable text = (Spannable) source;
int composeBegin = EditableInputConnection.getComposingSpanStart(text);
int composeEnd = EditableInputConnection.getComposingSpanEnd(text);
return composeBegin < composeEnd;
}
private boolean isInTextWatcher() {
CharSequence text = mEditor.mTextView.getText();
return (text instanceof SpannableStringBuilder)
&& ((SpannableStringBuilder) text).getTextWatcherDepth() > 0;
}
}
/**
* An operation to undo a single "edit" to a text view.
*/
public static class EditOperation extends UndoOperation<Editor> {
private static final int TYPE_INSERT = 0;
private static final int TYPE_DELETE = 1;
private static final int TYPE_REPLACE = 2;
private int mType;
private String mOldText;
private int mOldTextStart;
private String mNewText;
private int mNewTextStart;
private int mOldCursorPos;
private int mNewCursorPos;
/**
* Constructs an edit operation from a text input operation on editor that replaces the
* oldText starting at dstart with newText.
*/
public EditOperation(Editor editor, String oldText, int dstart, String newText) {
super(editor.mUndoOwner);
mOldText = oldText;
mNewText = newText;
// Determine the type of the edit and store where it occurred. Avoid storing
// irrevelant data (e.g. mNewTextStart for a delete) because that makes the
// merging logic more complex (e.g. merging deletes could lead to mNewTextStart being
// outside the bounds of the final text).
if (mNewText.length() > 0 && mOldText.length() == 0) {
mType = TYPE_INSERT;
mNewTextStart = dstart;
} else if (mNewText.length() == 0 && mOldText.length() > 0) {
mType = TYPE_DELETE;
mOldTextStart = dstart;
} else {
mType = TYPE_REPLACE;
mOldTextStart = mNewTextStart = dstart;
}
// Store cursor data.
mOldCursorPos = editor.mTextView.getSelectionStart();
mNewCursorPos = dstart + mNewText.length();
}
public EditOperation(Parcel src, ClassLoader loader) {
super(src, loader);
mType = src.readInt();
mOldText = src.readString();
mOldTextStart = src.readInt();
mNewText = src.readString();
mNewTextStart = src.readInt();
mOldCursorPos = src.readInt();
mNewCursorPos = src.readInt();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(mType);
dest.writeString(mOldText);
dest.writeInt(mOldTextStart);
dest.writeString(mNewText);
dest.writeInt(mNewTextStart);
dest.writeInt(mOldCursorPos);
dest.writeInt(mNewCursorPos);
}
private int getNewTextEnd() {
return mNewTextStart + mNewText.length();
}
private int getOldTextEnd() {
return mOldTextStart + mOldText.length();
}
@Override
public void commit() {
}
@Override
public void undo() {
if (DEBUG_UNDO) Log.d(TAG, "undo");
// Remove the new text and insert the old.
Editor editor = getOwnerData();
Editable text = (Editable) editor.mTextView.getText();
modifyText(text, mNewTextStart, getNewTextEnd(), mOldText, mOldTextStart,
mOldCursorPos);
}
@Override
public void redo() {
if (DEBUG_UNDO) Log.d(TAG, "redo");
// Remove the old text and insert the new.
Editor editor = getOwnerData();
Editable text = (Editable) editor.mTextView.getText();
modifyText(text, mOldTextStart, getOldTextEnd(), mNewText, mNewTextStart,
mNewCursorPos);
}
/**
* Attempts to merge this existing operation with a new edit.
* @param edit The new edit operation.
* @return If the merge succeeded, returns true. Otherwise returns false and leaves this
* object unchanged.
*/
private boolean mergeWith(EditOperation edit) {
if (DEBUG_UNDO) {
Log.d(TAG, "mergeWith old " + this);
Log.d(TAG, "mergeWith new " + edit);
}
switch (mType) {
case TYPE_INSERT:
return mergeInsertWith(edit);
case TYPE_DELETE:
return mergeDeleteWith(edit);
case TYPE_REPLACE:
return mergeReplaceWith(edit);
default:
return false;
}
}
private boolean mergeInsertWith(EditOperation edit) {
// Only merge continuous insertions.
if (edit.mType != TYPE_INSERT) {
return false;
}
// Only merge insertions that are contiguous.
if (getNewTextEnd() != edit.mNewTextStart) {
return false;
}
mNewText += edit.mNewText;
mNewCursorPos = edit.mNewCursorPos;
return true;
}
// TODO: Support forward delete.
private boolean mergeDeleteWith(EditOperation edit) {
// Only merge continuous deletes.
if (edit.mType != TYPE_DELETE) {
return false;
}
// Only merge deletions that are contiguous.
if (mOldTextStart != edit.getOldTextEnd()) {
return false;
}
mOldTextStart = edit.mOldTextStart;
mOldText = edit.mOldText + mOldText;
mNewCursorPos = edit.mNewCursorPos;
return true;
}
private boolean mergeReplaceWith(EditOperation edit) {
// Replacements can merge only with adjacent inserts.
if (edit.mType != TYPE_INSERT || getNewTextEnd() != edit.mNewTextStart) {
return false;
}
mOldText += edit.mOldText;
mNewText += edit.mNewText;
mNewCursorPos = edit.mNewCursorPos;
return true;
}
/**
* Forcibly creates a single merged edit operation by simulating the entire text
* contents being replaced.
*/
public void forceMergeWith(EditOperation edit) {
if (DEBUG_UNDO) Log.d(TAG, "forceMerge");
Editor editor = getOwnerData();
// Copy the text of the current field.
// NOTE: Using StringBuilder instead of SpannableStringBuilder would be somewhat faster,
// but would require two parallel implementations of modifyText() because Editable and
// StringBuilder do not share an interface for replace/delete/insert.
Editable editable = (Editable) editor.mTextView.getText();
Editable originalText = new SpannableStringBuilder(editable.toString());
// Roll back the last operation.
modifyText(originalText, mNewTextStart, getNewTextEnd(), mOldText, mOldTextStart,
mOldCursorPos);
// Clone the text again and apply the new operation.
Editable finalText = new SpannableStringBuilder(editable.toString());
modifyText(finalText, edit.mOldTextStart, edit.getOldTextEnd(), edit.mNewText,
edit.mNewTextStart, edit.mNewCursorPos);
// Convert this operation into a non-mergeable replacement of the entire string.
mType = TYPE_REPLACE;
mNewText = finalText.toString();
mNewTextStart = 0;
mOldText = originalText.toString();
mOldTextStart = 0;
mNewCursorPos = edit.mNewCursorPos;
// mOldCursorPos is unchanged.
}
private static void modifyText(Editable text, int deleteFrom, int deleteTo,
CharSequence newText, int newTextInsertAt, int newCursorPos) {
// Apply the edit if it is still valid.
if (isValidRange(text, deleteFrom, deleteTo) &&
newTextInsertAt <= text.length() - (deleteTo - deleteFrom)) {
if (deleteFrom != deleteTo) {
text.delete(deleteFrom, deleteTo);
}
if (newText.length() != 0) {
text.insert(newTextInsertAt, newText);
}
}
// Restore the cursor position. If there wasn't an old cursor (newCursorPos == -1) then
// don't explicitly set it and rely on SpannableStringBuilder to position it.
// TODO: Select all the text that was undone.
if (0 <= newCursorPos && newCursorPos <= text.length()) {
Selection.setSelection(text, newCursorPos);
}
}
private String getTypeString() {
switch (mType) {
case TYPE_INSERT:
return "insert";
case TYPE_DELETE:
return "delete";
case TYPE_REPLACE:
return "replace";
default:
return "";
}
}
@Override
public String toString() {
return "[mType=" + getTypeString() + ", " +
"mOldText=" + mOldText + ", " +
"mOldTextStart=" + mOldTextStart + ", " +
"mNewText=" + mNewText + ", " +
"mNewTextStart=" + mNewTextStart + ", " +
"mOldCursorPos=" + mOldCursorPos + ", " +
"mNewCursorPos=" + mNewCursorPos + "]";
}
public static final Parcelable.ClassLoaderCreator<EditOperation> CREATOR
= new Parcelable.ClassLoaderCreator<EditOperation>() {
@Override
public EditOperation createFromParcel(Parcel in) {
return new EditOperation(in, null);
}
@Override
public EditOperation createFromParcel(Parcel in, ClassLoader loader) {
return new EditOperation(in, loader);
}
@Override
public EditOperation[] newArray(int size) {
return new EditOperation[size];
}
};
}
}
|