summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/sms/MmsUtils.java
blob: b819fc4b1082dddc4ceb98f68fd87735f5c5cf4b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
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
/*
 * Copyright (C) 2015 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 com.android.messaging.sms;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.Resources;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.DatabaseUtils;
import android.media.MediaMetadataRetriever;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.Settings;
import android.provider.Telephony;
import android.provider.Telephony.Mms;
import android.provider.Telephony.Sms;
import android.provider.Telephony.Threads;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.telephony.SubscriptionManager;
import android.text.TextUtils;
import android.text.util.Rfc822Token;
import android.text.util.Rfc822Tokenizer;

import com.android.messaging.Factory;
import com.android.messaging.R;
import com.android.messaging.datamodel.BugleDatabaseOperations;
import com.android.messaging.datamodel.DataModel;
import com.android.messaging.datamodel.MediaScratchFileProvider;
import com.android.messaging.datamodel.action.DownloadMmsAction;
import com.android.messaging.datamodel.action.SendMessageAction;
import com.android.messaging.datamodel.data.MessageData;
import com.android.messaging.datamodel.data.MessagePartData;
import com.android.messaging.datamodel.data.ParticipantData;
import com.android.messaging.datamodel.DatabaseHelper;
import com.android.messaging.datamodel.DatabaseWrapper;
import com.android.messaging.datamodel.MessagingContentProvider;
import com.android.messaging.mmslib.InvalidHeaderValueException;
import com.android.messaging.mmslib.MmsException;
import com.android.messaging.mmslib.SqliteWrapper;
import com.android.messaging.mmslib.pdu.CharacterSets;
import com.android.messaging.mmslib.pdu.EncodedStringValue;
import com.android.messaging.mmslib.pdu.GenericPdu;
import com.android.messaging.mmslib.pdu.NotificationInd;
import com.android.messaging.mmslib.pdu.ReadOrigInd;
import com.android.messaging.mmslib.pdu.DeliveryInd;
import com.android.messaging.mmslib.pdu.PduBody;
import com.android.messaging.mmslib.pdu.PduComposer;
import com.android.messaging.mmslib.pdu.PduHeaders;
import com.android.messaging.mmslib.pdu.PduParser;
import com.android.messaging.mmslib.pdu.PduPart;
import com.android.messaging.mmslib.pdu.PduPersister;
import com.android.messaging.mmslib.pdu.RetrieveConf;
import com.android.messaging.mmslib.pdu.SendConf;
import com.android.messaging.mmslib.pdu.SendReq;
import com.android.messaging.sms.SmsSender.SendResult;
import com.android.messaging.util.Assert;
import com.android.messaging.util.BugleGservices;
import com.android.messaging.util.BugleGservicesKeys;
import com.android.messaging.util.BuglePrefs;
import com.android.messaging.util.ContentType;
import com.android.messaging.util.DebugUtils;
import com.android.messaging.util.EmailAddress;
import com.android.messaging.util.ImageUtils;
import com.android.messaging.util.ImageUtils.ImageResizer;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.MediaMetadataRetrieverWrapper;
import com.android.messaging.util.OsUtil;
import com.android.messaging.util.PhoneUtils;
import com.google.common.base.Joiner;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.UUID;

import android.util.Log;

/**
 * Utils for sending sms/mms messages.
 */
public class MmsUtils {
    private static final String TAG = LogUtil.BUGLE_TAG;

    public static final boolean DEFAULT_DELIVERY_REPORT_MODE  = false;
    public static final boolean DEFAULT_READ_REPORT_MODE = false;
    public static final long DEFAULT_EXPIRY_TIME_IN_SECONDS = 7 * 24 * 60 * 60;
    public static final int DEFAULT_PRIORITY = PduHeaders.PRIORITY_NORMAL;

    public static final int MAX_SMS_RETRY = 3;

    /**
     * MMS request succeeded
     */
    public static final int MMS_REQUEST_SUCCEEDED = 0;
    /**
     * MMS request failed with a transient error and can be retried automatically
     */
    public static final int MMS_REQUEST_AUTO_RETRY = 1;
    /**
     * MMS request failed with an error and can be retried manually
     */
    public static final int MMS_REQUEST_MANUAL_RETRY = 2;
    /**
     * MMS request failed with a specific error and should not be retried
     */
    public static final int MMS_REQUEST_NO_RETRY = 3;

    public static final String getRequestStatusDescription(final int status) {
        switch (status) {
            case MMS_REQUEST_SUCCEEDED:
                return "SUCCEEDED";
            case MMS_REQUEST_AUTO_RETRY:
                return "AUTO_RETRY";
            case MMS_REQUEST_MANUAL_RETRY:
                return "MANUAL_RETRY";
            case MMS_REQUEST_NO_RETRY:
                return "NO_RETRY";
            default:
                return String.valueOf(status) + " (check MmsUtils)";
        }
    }

    public static final int PDU_HEADER_VALUE_UNDEFINED = 0;

    private static final int DEFAULT_DURATION = 5000; //ms

    // amount of space to leave in a MMS for text and overhead.
    private static final int MMS_MAX_SIZE_SLOP = 1024;
    public static final long INVALID_TIMESTAMP = 0L;
    private static String[] sNoSubjectStrings;

    public static class MmsInfo {
        public Uri mUri;
        public int mMessageSize;
        public PduBody mPduBody;
    }

    // Sync all remote messages apart from drafts
    private static final String REMOTE_SMS_SELECTION = String.format(
            Locale.US,
            "(%s IN (%d, %d, %d, %d, %d))",
            Sms.TYPE,
            Sms.MESSAGE_TYPE_INBOX,
            Sms.MESSAGE_TYPE_OUTBOX,
            Sms.MESSAGE_TYPE_QUEUED,
            Sms.MESSAGE_TYPE_FAILED,
            Sms.MESSAGE_TYPE_SENT);

    private static final String REMOTE_MMS_SELECTION = String.format(
            Locale.US,
            "((%s IN (%d, %d, %d, %d)) AND (%s IN (%d, %d, %d)))",
            Mms.MESSAGE_BOX,
            Mms.MESSAGE_BOX_INBOX,
            Mms.MESSAGE_BOX_OUTBOX,
            Mms.MESSAGE_BOX_SENT,
            Mms.MESSAGE_BOX_FAILED,
            Mms.MESSAGE_TYPE,
            PduHeaders.MESSAGE_TYPE_SEND_REQ,
            PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND,
            PduHeaders.MESSAGE_TYPE_RETRIEVE_CONF);

    /**
     * Type selection for importing sms messages.
     *
     * @return The SQL selection for importing sms messages
     */
    public static String getSmsTypeSelectionSql() {
        return REMOTE_SMS_SELECTION;
    }

    /**
     * Type selection for importing mms messages.
     *
     * @return The SQL selection for importing mms messages. This selects the message type,
     * not including the selection on timestamp.
     */
    public static String getMmsTypeSelectionSql() {
        return REMOTE_MMS_SELECTION;
    }

    // SMIL spec: http://www.w3.org/TR/SMIL3

    private static final String sSmilImagePart =
            "<par dur=\"" + DEFAULT_DURATION + "ms\">" +
                "<img src=\"%s\" region=\"Image\" />" +
            "</par>";

    private static final String sSmilVideoPart =
            "<par dur=\"%2$dms\">" +
                "<video src=\"%1$s\" dur=\"%2$dms\" region=\"Image\" />" +
            "</par>";

    private static final String sSmilAudioPart =
            "<par dur=\"%2$dms\">" +
                    "<audio src=\"%1$s\" dur=\"%2$dms\" />" +
            "</par>";

    private static final String sSmilTextPart =
            "<par dur=\"" + DEFAULT_DURATION + "ms\">" +
                "<text src=\"%s\" region=\"Text\" />" +
            "</par>";

    private static final String sSmilPart =
            "<par dur=\"" + DEFAULT_DURATION + "ms\">" +
                "<ref src=\"%s\" />" +
            "</par>";

    private static final String sSmilTextOnly =
            "<smil>" +
                "<head>" +
                    "<layout>" +
                        "<root-layout/>" +
                        "<region id=\"Text\" top=\"0\" left=\"0\" "
                          + "height=\"100%%\" width=\"100%%\"/>" +
                    "</layout>" +
                "</head>" +
                "<body>" +
                       "%s" +  // constructed body goes here
                "</body>" +
            "</smil>";

    private static final String sSmilVisualAttachmentsOnly =
            "<smil>" +
                "<head>" +
                    "<layout>" +
                        "<root-layout/>" +
                        "<region id=\"Image\" fit=\"meet\" top=\"0\" left=\"0\" "
                          + "height=\"100%%\" width=\"100%%\"/>" +
                    "</layout>" +
                "</head>" +
                "<body>" +
                       "%s" +  // constructed body goes here
                "</body>" +
            "</smil>";

    private static final String sSmilVisualAttachmentsWithText =
            "<smil>" +
                "<head>" +
                    "<layout>" +
                        "<root-layout/>" +
                        "<region id=\"Image\" fit=\"meet\" top=\"0\" left=\"0\" "
                          + "height=\"80%%\" width=\"100%%\"/>" +
                        "<region id=\"Text\" top=\"80%%\" left=\"0\" height=\"20%%\" "
                          + "width=\"100%%\"/>" +
                    "</layout>" +
                "</head>" +
                "<body>" +
                       "%s" +  // constructed body goes here
                "</body>" +
            "</smil>";

    private static final String sSmilNonVisualAttachmentsOnly =
            "<smil>" +
                "<head>" +
                    "<layout>" +
                        "<root-layout/>" +
                    "</layout>" +
                "</head>" +
                "<body>" +
                       "%s" +  // constructed body goes here
                "</body>" +
            "</smil>";

    private static final String sSmilNonVisualAttachmentsWithText = sSmilTextOnly;

    public static final String MMS_DUMP_PREFIX = "mmsdump-";
    public static final String SMS_DUMP_PREFIX = "smsdump-";

    public static final int MIN_VIDEO_BYTES_PER_SECOND = 4 * 1024;
    public static final int MIN_IMAGE_BYTE_SIZE = 16 * 1024;
    public static final int MAX_VIDEO_ATTACHMENT_COUNT = 1;

    public static MmsInfo makePduBody(final Context context, final MessageData message,
            final int subId) {
        final PduBody pb = new PduBody();

        // Compute data size requirements for this message: count up images and total size of
        // non-image attachments.
        int totalLength = 0;
        int countImage = 0;
        for (final MessagePartData part : message.getParts()) {
            if (part.isAttachment()) {
                final String contentType = part.getContentType();
                if (ContentType.isImageType(contentType)) {
                    countImage++;
                } else if (ContentType.isVCardType(contentType)) {
                    totalLength += getDataLength(context, part.getContentUri());
                } else {
                    totalLength += getMediaFileSize(part.getContentUri());
                }
            }
        }
        final long minSize = countImage * MIN_IMAGE_BYTE_SIZE;
        final int byteBudget = MmsConfig.get(subId).getMaxMessageSize() - totalLength
                - MMS_MAX_SIZE_SLOP;
        final double budgetFactor =
                minSize > 0 ? Math.max(1.0, byteBudget / ((double) minSize)) : 1;
        final int bytesPerImage = (int) (budgetFactor * MIN_IMAGE_BYTE_SIZE);
        final int widthLimit = MmsConfig.get(subId).getMaxImageWidth();
        final int heightLimit = MmsConfig.get(subId).getMaxImageHeight();

        // Actually add the attachments, shrinking images appropriately.
        int index = 0;
        totalLength = 0;
        boolean hasVisualAttachment = false;
        boolean hasNonVisualAttachment = false;
        boolean hasText = false;
        final StringBuilder smilBody = new StringBuilder();
        for (final MessagePartData part : message.getParts()) {
            String srcName;
            if (part.isAttachment()) {
                String contentType = part.getContentType();
                if (ContentType.isImageType(contentType)) {
                    // There's a good chance that if we selected the image from our media picker the
                    // content type is image/*. Fix the content type here for gifs so that we only
                    // need to open the input stream once. All other gif vs static image checks will
                    // only have to do a string comparison which is much cheaper.
                    final boolean isGif = ImageUtils.isGif(contentType, part.getContentUri());
                    contentType = isGif ? ContentType.IMAGE_GIF : contentType;
                    srcName = String.format(isGif ? "image%06d.gif" : "image%06d.jpg", index);
                    smilBody.append(String.format(sSmilImagePart, srcName));
                    totalLength += addPicturePart(context, pb, index, part,
                            widthLimit, heightLimit, bytesPerImage, srcName, contentType);
                    hasVisualAttachment = true;
                } else if (ContentType.isVideoType(contentType)) {
                    srcName = String.format("video%06d.mp4", index);
                    final int length = addVideoPart(context, pb, part, srcName);
                    totalLength += length;
                    smilBody.append(String.format(sSmilVideoPart, srcName,
                            getMediaDurationMs(context, part, DEFAULT_DURATION)));
                    hasVisualAttachment = true;
                } else if (ContentType.isVCardType(contentType)) {
                    srcName = String.format("contact%06d.vcf", index);
                    totalLength += addVCardPart(context, pb, part, srcName);
                    smilBody.append(String.format(sSmilPart, srcName));
                    hasNonVisualAttachment = true;
                } else if (ContentType.isAudioType(contentType)) {
                    srcName = String.format("recording%06d.amr", index);
                    totalLength += addOtherPart(context, pb, part, srcName);
                    final int duration = getMediaDurationMs(context, part, -1);
                    Assert.isTrue(duration != -1);
                    smilBody.append(String.format(sSmilAudioPart, srcName, duration));
                    hasNonVisualAttachment = true;
                } else {
                    srcName = String.format("other%06d.dat", index);
                    totalLength += addOtherPart(context, pb, part, srcName);
                    smilBody.append(String.format(sSmilPart, srcName));
                }
                index++;
            }
            if (!TextUtils.isEmpty(part.getText())) {
                hasText = true;
            }
        }

        if (hasText) {
            final String srcName = String.format("text.%06d.txt", index);
            final String text = message.getMessageText();
            totalLength += addTextPart(context, pb, text, srcName);

            // Append appropriate SMIL to the body.
            smilBody.append(String.format(sSmilTextPart, srcName));
        }

        final String smilTemplate = getSmilTemplate(hasVisualAttachment,
                hasNonVisualAttachment, hasText);
        addSmilPart(pb, smilTemplate, smilBody.toString());

        final MmsInfo mmsInfo = new MmsInfo();
        mmsInfo.mPduBody = pb;
        mmsInfo.mMessageSize = totalLength;

        return mmsInfo;
    }

    private static int getMediaDurationMs(final Context context, final MessagePartData part,
            final int defaultDurationMs) {
        Assert.notNull(context);
        Assert.notNull(part);
        Assert.isTrue(ContentType.isAudioType(part.getContentType()) ||
                ContentType.isVideoType(part.getContentType()));

        final MediaMetadataRetrieverWrapper retriever = new MediaMetadataRetrieverWrapper();
        try {
            retriever.setDataSource(part.getContentUri());
            return retriever.extractInteger(
                    MediaMetadataRetriever.METADATA_KEY_DURATION, defaultDurationMs);
        } catch (final IOException e) {
            LogUtil.i(LogUtil.BUGLE_TAG, "Error extracting duration from " + part.getContentUri(), e);
            return defaultDurationMs;
        } finally {
            retriever.release();
        }
    }

    private static void setPartContentLocationAndId(final PduPart part, final String srcName) {
        // Set Content-Location.
        part.setContentLocation(srcName.getBytes());

        // Set Content-Id.
        final int index = srcName.lastIndexOf(".");
        final String contentId = (index == -1) ? srcName : srcName.substring(0, index);
        part.setContentId(contentId.getBytes());
    }

    private static int addTextPart(final Context context, final PduBody pb,
            final String text, final String srcName) {
        final PduPart part = new PduPart();

        // Set Charset if it's a text media.
        part.setCharset(CharacterSets.UTF_8);

        // Set Content-Type.
        part.setContentType(ContentType.TEXT_PLAIN.getBytes());

        // Set Content-Location.
        setPartContentLocationAndId(part, srcName);

        part.setData(text.getBytes());

        pb.addPart(part);

        return part.getData().length;
    }

    private static int addPicturePart(final Context context, final PduBody pb, final int index,
            final MessagePartData messagePart, int widthLimit, int heightLimit,
            final int maxPartSize, final String srcName, final String contentType) {
        final Uri imageUri = messagePart.getContentUri();
        final int width = messagePart.getWidth();
        final int height = messagePart.getHeight();

        // Swap the width and height limits to match the orientation of the image so we scale the
        // picture as little as possible.
        if ((height > width) != (heightLimit > widthLimit)) {
            final int temp = widthLimit;
            widthLimit = heightLimit;
            heightLimit = temp;
        }

        final int orientation = ImageUtils.getOrientation(context, imageUri);
        int imageSize = getDataLength(context, imageUri);
        if (imageSize <= 0) {
            LogUtil.e(TAG, "Can't get image", new Exception());
            return 0;
        }

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "addPicturePart size: " + imageSize + " width: "
                    + width + " widthLimit: " + widthLimit
                    + " height: " + height
                    + " heightLimit: " + heightLimit);
        }

        PduPart part;
        // Check if we're already within the limits - in which case we don't need to resize.
        // The size can be zero here, even when the media has content. See the comment in
        // MediaModel.initMediaSize. Sometimes it'll compute zero and it's costly to read the
        // whole stream to compute the size. When we call getResizedImageAsPart(), we'll correctly
        // set the size.
        if (imageSize <= maxPartSize &&
                width <= widthLimit &&
                height <= heightLimit &&
                (orientation == android.media.ExifInterface.ORIENTATION_UNDEFINED ||
                orientation == android.media.ExifInterface.ORIENTATION_NORMAL)) {
            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "addPicturePart - already sized");
            }
            part = new PduPart();
            part.setDataUri(imageUri);
            part.setContentType(contentType.getBytes());
        } else {
            part = getResizedImageAsPart(widthLimit, heightLimit, maxPartSize,
                    width, height, orientation, imageUri, context, contentType);
            if (part == null) {
                final OutOfMemoryError e = new OutOfMemoryError();
                LogUtil.e(TAG, "Can't resize image: not enough memory?", e);
                throw e;
            }
            imageSize = part.getData().length;
        }

        setPartContentLocationAndId(part, srcName);

        pb.addPart(index, part);

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "addPicturePart size: " + imageSize);
        }

        return imageSize;
    }

    private static void addPartForUri(final Context context, final PduBody pb,
            final String srcName, final Uri uri, final String contentType) {
        final PduPart part = new PduPart();
        part.setDataUri(uri);
        part.setContentType(contentType.getBytes());

        setPartContentLocationAndId(part, srcName);

        pb.addPart(part);
    }

    private static int addVCardPart(final Context context, final PduBody pb,
            final MessagePartData messagePart, final String srcName) {
        final Uri vcardUri = messagePart.getContentUri();
        final String contentType = messagePart.getContentType();
        final int vcardSize = getDataLength(context, vcardUri);
        if (vcardSize <= 0) {
            LogUtil.e(TAG, "Can't get vcard", new Exception());
            return 0;
        }

        addPartForUri(context, pb, srcName, vcardUri, contentType);

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "addVCardPart size: " + vcardSize);
        }

        return vcardSize;
    }

    /**
     * Add video part recompressing video if necessary.  If recompression fails, part is not
     * added.
     */
    private static int addVideoPart(final Context context, final PduBody pb,
            final MessagePartData messagePart, final String srcName) {
        final Uri attachmentUri = messagePart.getContentUri();
        String contentType = messagePart.getContentType();

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "addPart attachmentUrl: " + attachmentUri.toString());
        }

        if (TextUtils.isEmpty(contentType)) {
            contentType = ContentType.VIDEO_3G2;
        }

        addPartForUri(context, pb, srcName, attachmentUri, contentType);
        return (int) getMediaFileSize(attachmentUri);
    }

    private static int addOtherPart(final Context context, final PduBody pb,
            final MessagePartData messagePart, final String srcName) {
        final Uri attachmentUri = messagePart.getContentUri();
        final String contentType = messagePart.getContentType();

        if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
            LogUtil.v(TAG, "addPart attachmentUrl: " + attachmentUri.toString());
        }

        final int dataSize = (int) getMediaFileSize(attachmentUri);

        addPartForUri(context, pb, srcName, attachmentUri, contentType);

        return dataSize;
    }

    private static void addSmilPart(final PduBody pb, final String smilTemplate,
            final String smilBody) {
        final PduPart smilPart = new PduPart();
        smilPart.setContentId("smil".getBytes());
        smilPart.setContentLocation("smil.xml".getBytes());
        smilPart.setContentType(ContentType.APP_SMIL.getBytes());
        final String smil = String.format(smilTemplate, smilBody);
        smilPart.setData(smil.getBytes());
        pb.addPart(0, smilPart);
    }

    private static String getSmilTemplate(final boolean hasVisualAttachments,
            final boolean hasNonVisualAttachments, final boolean hasText) {
        if (hasVisualAttachments) {
            return hasText ? sSmilVisualAttachmentsWithText : sSmilVisualAttachmentsOnly;
        }
        if (hasNonVisualAttachments) {
            return hasText ? sSmilNonVisualAttachmentsWithText : sSmilNonVisualAttachmentsOnly;
        }
        return sSmilTextOnly;
    }

    private static int getDataLength(final Context context, final Uri uri) {
        InputStream is = null;
        try {
            is = context.getContentResolver().openInputStream(uri);
            try {
                return is == null ? 0 : is.available();
            } catch (final IOException e) {
                LogUtil.e(TAG, "getDataLength couldn't stream: " + uri, e);
            }
        } catch (final FileNotFoundException e) {
            LogUtil.e(TAG, "getDataLength couldn't open: " + uri, e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (final IOException e) {
                    LogUtil.e(TAG, "getDataLength couldn't close: " + uri, e);
                }
            }
        }
        return 0;
    }

    /**
     * Returns {@code true} if group mms is turned on,
     * {@code false} otherwise.
     *
     * For the group mms feature to be enabled, the following must be true:
     *  1. the feature is enabled in mms_config.xml (currently on by default)
     *  2. the feature is enabled in the SMS settings page
     *
     * @return true if group mms is supported
     */
    public static boolean groupMmsEnabled(final int subId) {
        final Context context = Factory.get().getApplicationContext();
        final Resources resources = context.getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String groupMmsKey = resources.getString(R.string.group_mms_pref_key);
        final boolean groupMmsEnabledDefault = resources.getBoolean(R.bool.group_mms_pref_default);
        final boolean groupMmsPrefOn = prefs.getBoolean(groupMmsKey, groupMmsEnabledDefault);
        return MmsConfig.get(subId).getGroupMmsEnabled() && groupMmsPrefOn;
    }

    /**
     * Get a version of this image resized to fit the given dimension and byte-size limits. Note
     * that the content type of the resulting PduPart may not be the same as the content type of
     * this UriImage; always call {@link PduPart#getContentType()} to get the new content type.
     *
     * @param widthLimit The width limit, in pixels
     * @param heightLimit The height limit, in pixels
     * @param byteLimit The binary size limit, in bytes
     * @param width The image width, in pixels
     * @param height The image height, in pixels
     * @param orientation Orientation constant from ExifInterface for rotating or flipping the
     *                    image
     * @param imageUri Uri to the image data
     * @param context Needed to open the image
     * @return A new PduPart containing the resized image data
     */
    private static PduPart getResizedImageAsPart(final int widthLimit,
            final int heightLimit, final int byteLimit, final int width, final int height,
            final int orientation, final Uri imageUri, final Context context, final String contentType) {
        final PduPart part = new PduPart();

        final byte[] data = ImageResizer.getResizedImageData(width, height, orientation,
                widthLimit, heightLimit, byteLimit, imageUri, context, contentType);
        if (data == null) {
            if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                LogUtil.v(TAG, "Resize image failed.");
            }
            return null;
        }

        part.setData(data);
        // Any static images will be compressed into a jpeg
        final String contentTypeOfResizedImage = ImageUtils.isGif(contentType, imageUri)
                ? ContentType.IMAGE_GIF : ContentType.IMAGE_JPEG;
        part.setContentType(contentTypeOfResizedImage.getBytes());

        return part;
    }

    /**
     * Get media file size
     */
    public static long getMediaFileSize(final Uri uri) {
        final Context context = Factory.get().getApplicationContext();
        AssetFileDescriptor fd = null;
        try {
            fd = context.getContentResolver().openAssetFileDescriptor(uri, "r");
            if (fd != null) {
                return fd.getParcelFileDescriptor().getStatSize();
            }
        } catch (final FileNotFoundException e) {
            LogUtil.e(TAG, "MmsUtils.getMediaFileSize: cound not find media file: " + e, e);
        } finally {
            if (fd != null) {
                try {
                    fd.close();
                } catch (final IOException e) {
                    LogUtil.e(TAG, "MmsUtils.getMediaFileSize: failed to close " + e, e);
                }
            }
        }
        return 0L;
    }

    // Code for extracting the actual phone numbers for the participants in a conversation,
    // given a thread id.

    private static final Uri ALL_THREADS_URI =
            Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();

    private static final String[] RECIPIENTS_PROJECTION = {
        Threads._ID,
        Threads.RECIPIENT_IDS
    };

    private static final int RECIPIENT_IDS  = 1;

    public static List<String> getRecipientsByThread(final long threadId) {
        final String spaceSepIds = getRawRecipientIdsForThread(threadId);
        if (!TextUtils.isEmpty(spaceSepIds)) {
            final Context context = Factory.get().getApplicationContext();
            return getAddresses(context, spaceSepIds);
        }
        return null;
    }

    // NOTE: There are phones on which you can't get the recipients from the thread id for SMS
    // until you have a message in the conversation!
    public static String getRawRecipientIdsForThread(final long threadId) {
        if (threadId <= 0) {
            return null;
        }
        final Context context = Factory.get().getApplicationContext();
        final ContentResolver cr = context.getContentResolver();
        final Cursor thread = cr.query(
                ALL_THREADS_URI,
                RECIPIENTS_PROJECTION, "_id=?", new String[] { String.valueOf(threadId) }, null);
        if (thread != null) {
            try {
                if (thread.moveToFirst()) {
                    // recipientIds will be a space-separated list of ids into the
                    // canonical addresses table.
                    return thread.getString(RECIPIENT_IDS);
                }
            } finally {
                thread.close();
            }
        }
        return null;
    }

    private static final Uri SINGLE_CANONICAL_ADDRESS_URI =
            Uri.parse("content://mms-sms/canonical-address");

    private static List<String> getAddresses(final Context context, final String spaceSepIds) {
        final List<String> numbers = new ArrayList<String>();
        final String[] ids = spaceSepIds.split(" ");
        for (final String id : ids) {
            long longId;

            try {
                longId = Long.parseLong(id);
                if (longId < 0) {
                    LogUtil.e(TAG, "MmsUtils.getAddresses: invalid id " + longId);
                    continue;
                }
            } catch (final NumberFormatException ex) {
                LogUtil.e(TAG, "MmsUtils.getAddresses: invalid id. " + ex, ex);
                // skip this id
                continue;
            }

            // TODO: build a single query where we get all the addresses at once.
            Cursor c = null;
            try {
                c = context.getContentResolver().query(
                        ContentUris.withAppendedId(SINGLE_CANONICAL_ADDRESS_URI, longId),
                        null, null, null, null);
            } catch (final Exception e) {
                LogUtil.e(TAG, "MmsUtils.getAddresses: query failed for id " + longId, e);
            }
            if (c != null) {
                try {
                    if (c.moveToFirst()) {
                        final String number = c.getString(0);
                        if (!TextUtils.isEmpty(number)) {
                            numbers.add(number);
                        } else {
                            LogUtil.w(TAG, "Canonical MMS/SMS address is empty for id: " + longId);
                        }
                    }
                } finally {
                    c.close();
                }
            }
        }
        if (numbers.isEmpty()) {
            LogUtil.w(TAG, "No MMS addresses found from ids string [" + spaceSepIds + "]");
        }
        return numbers;
    }

    // Get telephony SMS thread ID
    public static long getOrCreateSmsThreadId(final Context context, final String dest) {
        // use destinations to determine threadId
        final Set<String> recipients = new HashSet<String>();
        recipients.add(dest);
        try {
            return MmsSmsUtils.Threads.getOrCreateThreadId(context, recipients);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: getting thread id failed: " + e);
            return -1;
        }
    }

    // Get telephony SMS thread ID
    public static long getOrCreateThreadId(final Context context, final List<String> dests) {
        if (dests == null || dests.size() == 0) {
            return -1;
        }
        // use destinations to determine threadId
        final Set<String> recipients = new HashSet<String>(dests);
        try {
            return MmsSmsUtils.Threads.getOrCreateThreadId(context, recipients);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: getting thread id failed: " + e);
            return -1;
        }
    }

    /**
     * Add an SMS to the given URI with thread_id specified.
     *
     * @param resolver the content resolver to use
     * @param uri the URI to add the message to
     * @param subId subId for the receiving sim
     * @param address the address of the sender
     * @param body the body of the message
     * @param subject the psuedo-subject of the message
     * @param date the timestamp for the message
     * @param read true if the message has been read, false if not
     * @param threadId the thread_id of the message
     * @return the URI for the new message
     */
    private static Uri addMessageToUri(final ContentResolver resolver,
            final Uri uri, final int subId, final String address, final String body,
            final String subject, final Long date, final boolean read, final boolean seen,
            final int status, final int type, final long threadId) {
        final ContentValues values = new ContentValues(7);

        values.put(Telephony.Sms.ADDRESS, address);
        if (date != null) {
            values.put(Telephony.Sms.DATE, date);
        }
        values.put(Telephony.Sms.READ, read ? 1 : 0);
        values.put(Telephony.Sms.SEEN, seen ? 1 : 0);
        values.put(Telephony.Sms.SUBJECT, subject);
        values.put(Telephony.Sms.BODY, body);
        if (OsUtil.isAtLeastL_MR1()) {
            values.put(Telephony.Sms.SUBSCRIPTION_ID, subId);
        }
        if (status != Telephony.Sms.STATUS_NONE) {
            values.put(Telephony.Sms.STATUS, status);
        }
        if (type != Telephony.Sms.MESSAGE_TYPE_ALL) {
            values.put(Telephony.Sms.TYPE, type);
        }
        if (threadId != -1L) {
            values.put(Telephony.Sms.THREAD_ID, threadId);
        }
        return resolver.insert(uri, values);
    }

    // Insert an SMS message to telephony
    public static Uri insertSmsMessage(final Context context, final Uri uri, final int subId,
            final String dest, final String text, final long timestamp, final int status,
            final int type, final long threadId) {
        Uri response = null;
        try {
            response = addMessageToUri(context.getContentResolver(), uri, subId, dest,
                    text, null /* subject */, timestamp, true /* read */,
                    true /* seen */, status, type, threadId);
            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, "Mmsutils: Inserted SMS message into telephony (type = " + type + ")"
                        + ", uri: " + response);
            }
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsUtils: persist sms message failure " + e, e);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: persist sms message failure " + e, e);
        }
        return response;
    }

    // Update SMS message type in telephony; returns true if it succeeded.
    public static boolean updateSmsMessageSendingStatus(final Context context, final Uri uri,
            final int type, final long date) {
        try {
            final ContentResolver resolver = context.getContentResolver();
            final ContentValues values = new ContentValues(2);

            values.put(Telephony.Sms.TYPE, type);
            values.put(Telephony.Sms.DATE, date);
            final int cnt = resolver.update(uri, values, null, null);
            if (cnt == 1) {
                if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "Mmsutils: Updated sending SMS " + uri + "; type = " + type
                            + ", date = " + date + " (millis since epoch)");
                }
                return true;
            }
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsUtils: update sms message failure " + e, e);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: update sms message failure " + e, e);
        }
        return false;
    }

    // Persist a sent MMS message in telephony
    private static Uri insertSendReq(final Context context, final GenericPdu pdu, final int subId,
            final String subPhoneNumber) {
        final PduPersister persister = PduPersister.getPduPersister(context);
        Uri uri = null;
        try {
            // Persist the PDU
            uri = persister.persist(
                    pdu,
                    Mms.Sent.CONTENT_URI,
                    subId,
                    subPhoneNumber,
                    null/*preOpenedFiles*/);
            // Update mms table to reflect sent messages are always seen and read
            final ContentValues values = new ContentValues(1);
            values.put(Mms.READ, 1);
            values.put(Mms.SEEN, 1);
            SqliteWrapper.update(context, context.getContentResolver(), uri, values, null, null);
        } catch (final MmsException e) {
            LogUtil.e(TAG, "MmsUtils: persist mms sent message failure " + e, e);
        }
        return uri;
    }

    // Persist a received MMS message in telephony
    public static Uri insertReceivedMmsMessage(final Context context,
            final RetrieveConf retrieveConf, final int subId, final String subPhoneNumber,
            final long receivedTimestampInSeconds, final String contentLocation) {
        final PduPersister persister = PduPersister.getPduPersister(context);
        Uri uri = null;
        try {
            uri = persister.persist(
                    retrieveConf,
                    Mms.Inbox.CONTENT_URI,
                    subId,
                    subPhoneNumber,
                    null/*preOpenedFiles*/);

            final ContentValues values = new ContentValues(2);
            // Update mms table with local time instead of PDU time
            values.put(Mms.DATE, receivedTimestampInSeconds);
            // Also update the content location field from NotificationInd so that
            // wap push dedup would work even after the wap push is deleted
            values.put(Mms.CONTENT_LOCATION, contentLocation);
            SqliteWrapper.update(context, context.getContentResolver(), uri, values, null, null);
            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, "MmsUtils: Inserted MMS message into telephony, uri: " + uri);
            }
        } catch (final MmsException e) {
            LogUtil.e(TAG, "MmsUtils: persist mms received message failure " + e, e);
            // Just returns empty uri to RetrieveMmsRequest, which triggers a permanent failure
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsUtils: update mms received message failure " + e, e);
            // Time update failure is ignored.
        }
        return uri;
    }

    // Update MMS message type in telephony; returns true if it succeeded.
    public static boolean updateMmsMessageSendingStatus(final Context context, final Uri uri,
            final int box, final long timestampInMillis) {
        try {
            final ContentResolver resolver = context.getContentResolver();
            final ContentValues values = new ContentValues();

            final long timestampInSeconds = timestampInMillis / 1000L;
            values.put(Telephony.Mms.MESSAGE_BOX, box);
            values.put(Telephony.Mms.DATE, timestampInSeconds);
            final int cnt = resolver.update(uri, values, null, null);
            if (cnt == 1) {
                if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "Mmsutils: Updated sending MMS " + uri + "; box = " + box
                            + ", date = " + timestampInSeconds + " (secs since epoch)");
                }
                return true;
            }
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsUtils: update mms message failure " + e, e);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: update mms message failure " + e, e);
        }
        return false;
    }

    /**
     * Parse values from a received sms message
     *
     * @param context
     * @param msgs The received sms message content
     * @param error The received sms error
     * @return Parsed values from the message
     */
    public static ContentValues parseReceivedSmsMessage(
            final Context context, final SmsMessage[] msgs, final int error) {
        final SmsMessage sms = msgs[0];
        final ContentValues values = new ContentValues();

        values.put(Sms.ADDRESS, sms.getDisplayOriginatingAddress());
        values.put(Sms.BODY, buildMessageBodyFromPdus(msgs));
        if (MmsUtils.hasSmsDateSentColumn()) {
            // TODO:: The boxing here seems unnecessary.
            values.put(Sms.DATE_SENT, Long.valueOf(sms.getTimestampMillis()));
        }
        values.put(Sms.PROTOCOL, sms.getProtocolIdentifier());
        if (sms.getPseudoSubject().length() > 0) {
            values.put(Sms.SUBJECT, sms.getPseudoSubject());
        }
        values.put(Sms.REPLY_PATH_PRESENT, sms.isReplyPathPresent() ? 1 : 0);
        values.put(Sms.SERVICE_CENTER, sms.getServiceCenterAddress());
        // Error code
        values.put(Sms.ERROR_CODE, error);

        return values;
    }

    // Some providers send formfeeds in their messages. Convert those formfeeds to newlines.
    private static String replaceFormFeeds(final String s) {
        return s == null ? "" : s.replace('\f', '\n');
    }

    // Parse the message body from message PDUs
    private static String buildMessageBodyFromPdus(final SmsMessage[] msgs) {
        if (msgs.length == 1) {
            // There is only one part, so grab the body directly.
            return replaceFormFeeds(msgs[0].getDisplayMessageBody());
        } else {
            // Build up the body from the parts.
            final StringBuilder body = new StringBuilder();
            for (final SmsMessage msg : msgs) {
                try {
                    // getDisplayMessageBody() can NPE if mWrappedMessage inside is null.
                    body.append(msg.getDisplayMessageBody());
                } catch (final NullPointerException e) {
                    // Nothing to do
                }
            }
            return replaceFormFeeds(body.toString());
        }
    }

    // Parse the message date
    public static Long getMessageDate(final SmsMessage sms, long now) {
        // Use now for the timestamp to avoid confusion with clock
        // drift between the handset and the SMSC.
        // Check to make sure the system is giving us a non-bogus time.
        final Calendar buildDate = new GregorianCalendar(2011, 8, 18);    // 18 Sep 2011
        final Calendar nowDate = new GregorianCalendar();
        nowDate.setTimeInMillis(now);
        if (nowDate.before(buildDate)) {
            // It looks like our system clock isn't set yet because the current time right now
            // is before an arbitrary time we made this build. Instead of inserting a bogus
            // receive time in this case, use the timestamp of when the message was sent.
            now = sms.getTimestampMillis();
        }
        return now;
    }

    /**
     * cleanseMmsSubject will take a subject that's says, "<Subject: no subject>", and return
     * a null string. Otherwise it will return the original subject string.
     * @param resources So the function can grab string resources
     * @param subject the raw subject
     * @return
     */
    public static String cleanseMmsSubject(final Resources resources, final String subject) {
        if (TextUtils.isEmpty(subject)) {
            return null;
        }
        if (sNoSubjectStrings == null) {
            sNoSubjectStrings =
                    resources.getStringArray(R.array.empty_subject_strings);
        }
        for (final String noSubjectString : sNoSubjectStrings) {
            if (subject.equalsIgnoreCase(noSubjectString)) {
                return null;
            }
        }
        return subject;
    }

    // return a semicolon separated list of phone numbers from a smsto: uri.
    public static String getSmsRecipients(final Uri uri) {
        String recipients = uri.getSchemeSpecificPart();
        final int pos = recipients.indexOf('?');
        if (pos != -1) {
            recipients = recipients.substring(0, pos);
        }
        recipients = replaceUnicodeDigits(recipients).replace(',', ';');
        return recipients;
    }

    // This function was lifted from Telephony.PhoneNumberUtils because it was @hide
    /**
     * Replace arabic/unicode digits with decimal digits.
     * @param number
     *            the number to be normalized.
     * @return the replaced number.
     */
    private static String replaceUnicodeDigits(final String number) {
        final StringBuilder normalizedDigits = new StringBuilder(number.length());
        for (final char c : number.toCharArray()) {
            final int digit = Character.digit(c, 10);
            if (digit != -1) {
                normalizedDigits.append(digit);
            } else {
                normalizedDigits.append(c);
            }
        }
        return normalizedDigits.toString();
    }

    /**
     * @return Whether the data roaming is enabled
     */
    private static boolean isDataRoamingEnabled() {
        boolean dataRoamingEnabled = false;
        final ContentResolver cr = Factory.get().getApplicationContext().getContentResolver();
        if (OsUtil.isAtLeastJB_MR1()) {
            dataRoamingEnabled = (Settings.Global.getInt(cr, Settings.Global.DATA_ROAMING, 0) != 0);
        } else {
            dataRoamingEnabled = (Settings.System.getInt(cr, Settings.System.DATA_ROAMING, 0) != 0);
        }
        return dataRoamingEnabled;
    }

    /**
     * @return Whether to auto retrieve MMS
     */
    public static boolean allowMmsAutoRetrieve(final int subId) {
        final Context context = Factory.get().getApplicationContext();
        final Resources resources = context.getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final boolean autoRetrieve = prefs.getBoolean(
                resources.getString(R.string.auto_retrieve_mms_pref_key),
                resources.getBoolean(R.bool.auto_retrieve_mms_pref_default));
        if (autoRetrieve) {
            final boolean autoRetrieveInRoaming = prefs.getBoolean(
                    resources.getString(R.string.auto_retrieve_mms_when_roaming_pref_key),
                    resources.getBoolean(R.bool.auto_retrieve_mms_when_roaming_pref_default));
            final PhoneUtils phoneUtils = PhoneUtils.get(subId);
            if ((autoRetrieveInRoaming && phoneUtils.isDataRoamingEnabled())
                    || !phoneUtils.isRoaming()) {
                return true;
            }
        }
        return false;
    }

    /**
     * Parse the message row id from a message Uri.
     *
     * @param messageUri The input Uri
     * @return The message row id if valid, otherwise -1
     */
    public static long parseRowIdFromMessageUri(final Uri messageUri) {
        try {
            if (messageUri != null) {
                return ContentUris.parseId(messageUri);
            }
        } catch (final UnsupportedOperationException e) {
            // Nothing to do
        } catch (final NumberFormatException e) {
            // Nothing to do
        }
        return -1;
    }

    public static SmsMessage getSmsMessageFromDeliveryReport(final Intent intent) {
        final byte[] pdu = intent.getByteArrayExtra("pdu");
        return SmsMessage.createFromPdu(pdu);
    }

    /**
     * Update the status and date_sent column of sms message in telephony provider
     *
     * @param smsMessageUri
     * @param status
     * @param timeSentInMillis
     */
    public static void updateSmsStatusAndDateSent(final Uri smsMessageUri, final int status,
            final long timeSentInMillis) {
        if (smsMessageUri == null) {
            return;
        }
        final ContentValues values = new ContentValues();
        values.put(Sms.STATUS, status);
        if (MmsUtils.hasSmsDateSentColumn()) {
            values.put(Sms.DATE_SENT, timeSentInMillis);
        }
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        resolver.update(smsMessageUri, values, null/*where*/, null/*selectionArgs*/);
    }

    /**
     * Get the SQL selection statement for matching messages with media.
     *
     * Example for MMS part table:
     * "((ct LIKE 'image/%')
     *   OR (ct LIKE 'video/%')
     *   OR (ct LIKE 'audio/%')
     *   OR (ct='application/ogg'))
     *
     * @param contentTypeColumn The content-type column name
     * @return The SQL selection statement for matching media types: image, video, audio
     */
    public static String getMediaTypeSelectionSql(final String contentTypeColumn) {
        return String.format(
                Locale.US,
                "((%s LIKE '%s') OR (%s LIKE '%s') OR (%s LIKE '%s') OR (%s='%s'))",
                contentTypeColumn,
                "image/%",
                contentTypeColumn,
                "video/%",
                contentTypeColumn,
                "audio/%",
                contentTypeColumn,
                ContentType.AUDIO_OGG);
    }

    // Max number of operands per SQL query for deleting SMS messages
    public static final int MAX_IDS_PER_QUERY = 128;

    /**
     * Delete MMS messages with media parts.
     *
     * Because the telephony provider constraints, we can't use JOIN and delete messages in one
     * shot. We have to do a query first and then batch delete the messages based on IDs.
     *
     * @return The count of messages deleted.
     */
    public static int deleteMediaMessages() {
        // Do a query first
        //
        // The WHERE clause has two parts:
        // The first part is to select the exact same types of MMS messages as when we import them
        // (so that we don't delete messages that are not in local database)
        // The second part is to select MMS with media parts, including image, video and audio
        final String selection = String.format(
                Locale.US,
                "%s AND (%s IN (SELECT %s FROM part WHERE %s))",
                getMmsTypeSelectionSql(),
                Mms._ID,
                Mms.Part.MSG_ID,
                getMediaTypeSelectionSql(Mms.Part.CONTENT_TYPE));
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        final Cursor cursor = resolver.query(Mms.CONTENT_URI,
                new String[]{ Mms._ID },
                selection,
                null/*selectionArgs*/,
                null/*sortOrder*/);
        int deleted = 0;
        if (cursor != null) {
            final long[] messageIds = new long[cursor.getCount()];
            try {
                int i = 0;
                while (cursor.moveToNext()) {
                    messageIds[i++] = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
            final int totalIds = messageIds.length;
            if (totalIds > 0) {
                // Batch delete the messages using IDs
                // We don't want to send all IDs at once since there is a limit on SQL statement
                for (int start = 0; start < totalIds; start += MAX_IDS_PER_QUERY) {
                    final int end = Math.min(start + MAX_IDS_PER_QUERY, totalIds); // excluding
                    final int count = end - start;
                    final String batchSelection = String.format(
                            Locale.US,
                            "%s IN %s",
                            Mms._ID,
                            getSqlInOperand(count));
                    final String[] batchSelectionArgs =
                            getSqlInOperandArgs(messageIds, start, count);
                    final int deletedForBatch = resolver.delete(
                            Mms.CONTENT_URI,
                            batchSelection,
                            batchSelectionArgs);
                    if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                        LogUtil.d(TAG, "deleteMediaMessages: deleting IDs = "
                                + Joiner.on(',').skipNulls().join(batchSelectionArgs)
                                + ", deleted = " + deletedForBatch);
                    }
                    deleted += deletedForBatch;
                }
            }
        }
        return deleted;
    }

    /**
     * Get the (?,?,...) thing for the SQL IN operator by a count
     *
     * @param count
     * @return
     */
    public static String getSqlInOperand(final int count) {
        if (count <= 0) {
            return null;
        }
        final StringBuilder sb = new StringBuilder();
        sb.append("(?");
        for (int i = 0; i < count - 1; i++) {
            sb.append(",?");
        }
        sb.append(")");
        return sb.toString();
    }

    /**
     * Get the args for SQL IN operator from a long ID array
     *
     * @param ids The original long id array
     * @param start Start of the ids to fill the args
     * @param count Number of ids to pack
     * @return The long array with the id args
     */
    private static String[] getSqlInOperandArgs(
            final long[] ids, final int start, final int count) {
        if (count <= 0) {
            return null;
        }
        final String[] args = new String[count];
        for (int i = 0; i < count; i++) {
            args[i] = Long.toString(ids[start + i]);
        }
        return args;
    }

    /**
     * Delete SMS and MMS messages that are earlier than a specific timestamp
     *
     * @param cutOffTimestampInMillis The cut-off timestamp
     * @return Total number of messages deleted.
     */
    public static int deleteMessagesOlderThan(final long cutOffTimestampInMillis) {
        int deleted = 0;
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        // Delete old SMS
        final String smsSelection = String.format(
                Locale.US,
                "%s AND (%s<=%d)",
                getSmsTypeSelectionSql(),
                Sms.DATE,
                cutOffTimestampInMillis);
        deleted += resolver.delete(Sms.CONTENT_URI, smsSelection, null/*selectionArgs*/);
        // Delete old MMS
        final String mmsSelection = String.format(
                Locale.US,
                "%s AND (%s<=%d)",
                getMmsTypeSelectionSql(),
                Mms.DATE,
                cutOffTimestampInMillis / 1000L);
        deleted += resolver.delete(Mms.CONTENT_URI, mmsSelection, null/*selectionArgs*/);
        return deleted;
    }

    public static int deleteMessagesOlderThanByProtocol(final long cutOffTimestampInMillis,
            final int protocol) {
        Uri uri;
        String selectionSql;
        String dateField;
        long cutOffTimeStamp = Long.MIN_VALUE;

        switch (protocol) {
            case MessageData.PROTOCOL_SMS:
                uri = Sms.CONTENT_URI;
                selectionSql = getSmsTypeSelectionSql();
                cutOffTimeStamp = cutOffTimestampInMillis;
                dateField = Sms.DATE;
                break;
            case MessageData.PROTOCOL_MMS:
                uri = Mms.CONTENT_URI;
                selectionSql = getMmsTypeSelectionSql();
                cutOffTimeStamp = cutOffTimestampInMillis / 1000L;
                dateField = Mms.DATE;
                break;
            default:
                return 0;
        }
        final ContentResolver resolver =
                Factory.get().getApplicationContext().getContentResolver();
        final String selection = String.format(
                Locale.US,
                "%s AND (%s<=%d)",
                selectionSql,
                dateField,
                cutOffTimeStamp);
        int deleted = resolver.delete(uri, selection, null);

        return deleted;
    }

    /**
     * Update the read status of SMS/MMS messages by thread and timestamp
     *
     * @param threadId The thread of sms/mms to change
     * @param timestampInMillis Change the status before this timestamp
     */
    public static void updateSmsReadStatus(final long threadId, final long timestampInMillis) {
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        final ContentValues values = new ContentValues();
        values.put("read", 1);
        values.put("seen", 1); /* If you read it you saw it */
        final String smsSelection = String.format(
                Locale.US,
                "%s=%d AND %s<=%d AND %s=0",
                Sms.THREAD_ID,
                threadId,
                Sms.DATE,
                timestampInMillis,
                Sms.READ);
        resolver.update(
                Sms.CONTENT_URI,
                values,
                smsSelection,
                null/*selectionArgs*/);
        final String mmsSelection = String.format(
                Locale.US,
                "%s=%d AND %s<=%d AND %s=0",
                Mms.THREAD_ID,
                threadId,
                Mms.DATE,
                timestampInMillis / 1000L,
                Mms.READ);
        resolver.update(
                Mms.CONTENT_URI,
                values,
                mmsSelection,
                null/*selectionArgs*/);
    }

    /**
     * Update the read status of a single MMS message by its URI
     *
     * @param mmsUri
     * @param read
     */
    public static void updateReadStatusForMmsMessage(final Uri mmsUri, final boolean read) {
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        final ContentValues values = new ContentValues();
        values.put(Mms.READ, read ? 1 : 0);
        resolver.update(mmsUri, values, null/*where*/, null/*selectionArgs*/);
    }

    public static class AttachmentInfo {
        public String mUrl;
        public String mContentType;
        public int mWidth;
        public int mHeight;
    }

    /**
     * Convert byte array to Java String using a charset name
     *
     * @param bytes
     * @param charsetName
     * @return
     */
    public static String bytesToString(final byte[] bytes, final String charsetName) {
        if (bytes == null) {
            return null;
        }
        try {
            return new String(bytes, charsetName);
        } catch (final UnsupportedEncodingException e) {
            LogUtil.e(TAG, "MmsUtils.bytesToString: " + e, e);
            return new String(bytes);
        }
    }

    /**
     * Convert a Java String to byte array using a charset name
     *
     * @param string
     * @param charsetName
     * @return
     */
    public static byte[] stringToBytes(final String string, final String charsetName) {
        if (string == null) {
            return null;
        }
        try {
            return string.getBytes(charsetName);
        } catch (final UnsupportedEncodingException e) {
            LogUtil.e(TAG, "MmsUtils.stringToBytes: " + e, e);
            return string.getBytes();
        }
    }

    private static final String[] TEST_DATE_SENT_PROJECTION = new String[] { Sms.DATE_SENT };
    private static Boolean sHasSmsDateSentColumn = null;
    /**
     * Check if date_sent column exists on ICS and above devices. We need to do a test
     * query to figure that out since on some ICS+ devices, somehow the date_sent column does
     * not exist. http://b/17629135 tracks the associated compliance test.
     *
     * @return Whether "date_sent" column exists in sms table
     */
    public static boolean hasSmsDateSentColumn() {
        if (sHasSmsDateSentColumn == null) {
            Cursor cursor = null;
            try {
                final Context context = Factory.get().getApplicationContext();
                final ContentResolver resolver = context.getContentResolver();
                cursor = SqliteWrapper.query(
                        context,
                        resolver,
                        Sms.CONTENT_URI,
                        TEST_DATE_SENT_PROJECTION,
                        null/*selection*/,
                        null/*selectionArgs*/,
                        Sms.DATE_SENT + " ASC LIMIT 1");
                sHasSmsDateSentColumn = true;
            } catch (final SQLiteException e) {
                LogUtil.w(TAG, "date_sent in sms table does not exist", e);
                sHasSmsDateSentColumn = false;
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
        return sHasSmsDateSentColumn;
    }

    private static final String[] TEST_CARRIERS_PROJECTION =
            new String[] { Telephony.Carriers.MMSC };
    private static Boolean sUseSystemApn = null;
    /**
     * Check if we can access the APN data in the Telephony provider. Access was restricted in
     * JB MR1 (and some JB MR2) devices. If we can't access the APN, we have to fall back and use
     * a private table in our own app.
     *
     * @return Whether we can access the system APN table
     */
    public static boolean useSystemApnTable() {
        if (sUseSystemApn == null) {
            Cursor cursor = null;
            try {
                final Context context = Factory.get().getApplicationContext();
                final ContentResolver resolver = context.getContentResolver();
                cursor = SqliteWrapper.query(
                        context,
                        resolver,
                        Telephony.Carriers.CONTENT_URI,
                        TEST_CARRIERS_PROJECTION,
                        null/*selection*/,
                        null/*selectionArgs*/,
                        null);
                sUseSystemApn = true;
            } catch (final SecurityException e) {
                LogUtil.w(TAG, "Can't access system APN, using internal table", e);
                sUseSystemApn = false;
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
        return sUseSystemApn;
    }

    // For the internal debugger only
    public static void setUseSystemApnTable(final boolean turnOn) {
        if (!turnOn) {
            // We're not turning on to the system table. Instead, we're using our internal table.
            final int osVersion = OsUtil.getApiVersion();
            if (osVersion != android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
                // We're turning on local APNs on a device where we wouldn't normally have the
                // local APN table. Build it here.

                final SQLiteDatabase database = ApnDatabase.getApnDatabase().getWritableDatabase();

                // Do we already have the table?
                Cursor cursor = null;
                try {
                    cursor = database.query(ApnDatabase.APN_TABLE,
                            ApnDatabase.APN_PROJECTION,
                            null, null, null, null, null, null);
                } catch (final Exception e) {
                    // Apparently there's no table, create it now.
                    ApnDatabase.forceBuildAndLoadApnTables();
                } finally {
                    if (cursor != null) {
                        cursor.close();
                    }
                }
            }
        }
        sUseSystemApn = turnOn;
    }

    /**
     * Checks if we should dump sms, based on both the setting and the global debug
     * flag
     *
     * @return if dump sms is enabled
     */
    public static boolean isDumpSmsEnabled() {
        if (!DebugUtils.isDebugEnabled()) {
            return false;
        }
        return getDumpSmsOrMmsPref(R.string.dump_sms_pref_key, R.bool.dump_sms_pref_default);
    }

    /**
     * Checks if we should dump mms, based on both the setting and the global debug
     * flag
     *
     * @return if dump mms is enabled
     */
    public static boolean isDumpMmsEnabled() {
        if (!DebugUtils.isDebugEnabled()) {
            return false;
        }
        return getDumpSmsOrMmsPref(R.string.dump_mms_pref_key, R.bool.dump_mms_pref_default);
    }

    /**
     * Load the value of dump sms or mms setting preference
     */
    private static boolean getDumpSmsOrMmsPref(final int prefKeyRes, final int defaultKeyRes) {
        final Context context = Factory.get().getApplicationContext();
        final Resources resources = context.getResources();
        final BuglePrefs prefs = BuglePrefs.getApplicationPrefs();
        final String key = resources.getString(prefKeyRes);
        final boolean defaultValue = resources.getBoolean(defaultKeyRes);
        return prefs.getBoolean(key, defaultValue);
    }

    public static final Uri MMS_PART_CONTENT_URI = Uri.parse("content://mms/part");

    /**
     * Load MMS from telephony
     *
     * @param mmsUri The MMS pdu Uri
     * @return A memory copy of the MMS pdu including parts (but not addresses)
     */
    public static DatabaseMessages.MmsMessage loadMms(final Uri mmsUri) {
        final Context context = Factory.get().getApplicationContext();
        final ContentResolver resolver = context.getContentResolver();
        DatabaseMessages.MmsMessage mms = null;
        Cursor cursor = null;
        // Load pdu first
        try {
            cursor = SqliteWrapper.query(context, resolver,
                    mmsUri,
                    DatabaseMessages.MmsMessage.getProjection(),
                    null/*selection*/, null/*selectionArgs*/, null/*sortOrder*/);
            if (cursor != null && cursor.moveToFirst()) {
                mms = DatabaseMessages.MmsMessage.get(cursor);
            }
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsLoader: query pdu failure: " + e, e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        if (mms == null) {
            return null;
        }
        // Load parts except SMIL
        // TODO: we may need to load SMIL part in the future.
        final long rowId = MmsUtils.parseRowIdFromMessageUri(mmsUri);
        final String selection = String.format(
                Locale.US,
                "%s != '%s' AND %s = ?",
                Mms.Part.CONTENT_TYPE,
                ContentType.APP_SMIL,
                Mms.Part.MSG_ID);
        cursor = null;
        try {
            cursor = SqliteWrapper.query(context, resolver,
                    MMS_PART_CONTENT_URI,
                    DatabaseMessages.MmsPart.PROJECTION,
                    selection,
                    new String[] { Long.toString(rowId) },
                    null/*sortOrder*/);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    mms.addPart(DatabaseMessages.MmsPart.get(cursor, true/*loadMedia*/));
                }
            }
        } catch (final SQLiteException e) {
            LogUtil.e(TAG, "MmsLoader: query parts failure: " + e, e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return mms;
    }

    /**
     * Get the sender of an MMS message
     *
     * @param recipients The recipient list of the message
     * @param mmsUri The pdu uri of the MMS
     * @return The sender phone number of the MMS
     */
    public static String getMmsSender(final List<String> recipients, final String mmsUri) {
        final Context context = Factory.get().getApplicationContext();
        // We try to avoid the database query.
        // If this is a 1v1 conv., then the other party is the sender
        if (recipients != null && recipients.size() == 1) {
            return recipients.get(0);
        }
        // Otherwise, we have to query the MMS addr table for sender address
        // This should only be done for a received group mms message
        final Cursor cursor = SqliteWrapper.query(
                context,
                context.getContentResolver(),
                Uri.withAppendedPath(Uri.parse(mmsUri), "addr"),
                new String[] { Mms.Addr.ADDRESS, Mms.Addr.CHARSET },
                Mms.Addr.TYPE + "=" + PduHeaders.FROM,
                null/*selectionArgs*/,
                null/*sortOrder*/);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return DatabaseMessages.MmsAddr.get(cursor);
                }
            } finally {
                cursor.close();
            }
        }
        return null;
    }

    public static int bugleStatusForMms(final boolean isOutgoing, final boolean isNotification,
            final int messageBox) {
        int bugleStatus = MessageData.BUGLE_STATUS_UNKNOWN;
        // For a message we sync either
        if (isOutgoing) {
            if (messageBox == Mms.MESSAGE_BOX_OUTBOX || messageBox == Mms.MESSAGE_BOX_FAILED) {
                // Not sent counts as failed and available for manual resend
                bugleStatus = MessageData.BUGLE_STATUS_OUTGOING_FAILED;
            } else {
                // Otherwise outgoing message is complete
                bugleStatus = MessageData.BUGLE_STATUS_OUTGOING_COMPLETE;
            }
        } else if (isNotification) {
            // Incoming MMS notifications we sync count as failed and available for manual download
            bugleStatus = MessageData.BUGLE_STATUS_INCOMING_YET_TO_MANUAL_DOWNLOAD;
        } else {
            // Other incoming MMS messages are complete
            bugleStatus = MessageData.BUGLE_STATUS_INCOMING_COMPLETE;
        }
        return bugleStatus;
    }

    public static MessageData createMmsMessage(final DatabaseMessages.MmsMessage mms,
            final String conversationId, final String participantId, final String selfId,
            final int bugleStatus) {
        Assert.notNull(mms);
        final boolean isNotification = (mms.mMmsMessageType ==
                PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND);
        final int rawMmsStatus = (bugleStatus < MessageData.BUGLE_STATUS_FIRST_INCOMING
                ? mms.mRetrieveStatus : mms.mResponseStatus);

        final MessageData message = MessageData.createMmsMessage(mms.getUri(),
                participantId, selfId, conversationId, isNotification, bugleStatus,
                mms.mContentLocation, mms.mTransactionId, mms.mPriority, mms.mSubject,
                mms.mSeen, mms.mRead, mms.getSize(), rawMmsStatus,
                mms.mExpiryInMillis, mms.mSentTimestampInMillis, mms.mTimestampInMillis);

        for (final DatabaseMessages.MmsPart part : mms.mParts) {
            final MessagePartData messagePart = MmsUtils.createMmsMessagePart(part);
            // Import media and text parts (skip SMIL and others)
            if (messagePart != null) {
                message.addPart(messagePart);
            }
        }

        if (!message.getParts().iterator().hasNext()) {
            message.addPart(MessagePartData.createEmptyMessagePart());
        }

        return message;
    }

    public static MessagePartData createMmsMessagePart(final DatabaseMessages.MmsPart part) {
        MessagePartData messagePart = null;
        if (part.isText()) {
            final int mmsTextLengthLimit =
                    BugleGservices.get().getInt(BugleGservicesKeys.MMS_TEXT_LIMIT,
                            BugleGservicesKeys.MMS_TEXT_LIMIT_DEFAULT);
            String text = part.mText;
            if (text != null && text.length() > mmsTextLengthLimit) {
                // Limit the text to a reasonable value. We ran into a situation where a vcard
                // with a photo was sent as plain text. The massive amount of text caused the
                // app to hang, ANR, and eventually crash in native text code.
                text = text.substring(0, mmsTextLengthLimit);
            }
            messagePart = MessagePartData.createTextMessagePart(text);
        } else if (part.isMedia()) {
            messagePart = MessagePartData.createMediaMessagePart(part.mContentType,
                    part.getDataUri(), MessagePartData.UNSPECIFIED_SIZE,
                    MessagePartData.UNSPECIFIED_SIZE);
        }
        return messagePart;
    }

    public static class StatusPlusUri {
        // The request status to be as the result of the operation
        // e.g. MMS_REQUEST_MANUAL_RETRY
        public final int status;
        // The raw telephony status
        public final int rawStatus;
        // The raw telephony URI
        public final Uri uri;
        // The operation result code from system api invocation (sent by system)
        // or mapped from internal exception (sent by app)
        public final int resultCode;

        public StatusPlusUri(final int status, final int rawStatus, final Uri uri) {
            this.status = status;
            this.rawStatus = rawStatus;
            this.uri = uri;
            resultCode = MessageData.UNKNOWN_RESULT_CODE;
        }

        public StatusPlusUri(final int status, final int rawStatus, final Uri uri,
                final int resultCode) {
            this.status = status;
            this.rawStatus = rawStatus;
            this.uri = uri;
            this.resultCode = resultCode;
        }
    }

    public static class SendReqResp {
        public SendReq mSendReq;
        public SendConf mSendConf;

        public SendReqResp(final SendReq sendReq, final SendConf sendConf) {
            mSendReq = sendReq;
            mSendConf = sendConf;
        }
    }

    /**
     * Returned when sending/downloading MMS via platform APIs. In that case, we have to wait to
     * receive the pending intent to determine status.
     */
    public static final StatusPlusUri STATUS_PENDING = new StatusPlusUri(-1, -1, null);

    public static StatusPlusUri downloadMmsMessage(final Context context, final Uri notificationUri,
            final int subId, final String subPhoneNumber, final String transactionId,
            final String contentLocation, final boolean autoDownload,
            final long receivedTimestampInSeconds, Bundle extras) {
        if (TextUtils.isEmpty(contentLocation)) {
            LogUtil.e(TAG, "MmsUtils: Download from empty content location URL");
            return new StatusPlusUri(
                    MMS_REQUEST_NO_RETRY, MessageData.RAW_TELEPHONY_STATUS_UNDEFINED, null);
        }
        if (!isMmsDataAvailable(subId)) {
            LogUtil.e(TAG,
                    "MmsUtils: failed to download message, no data available");
            return new StatusPlusUri(MMS_REQUEST_MANUAL_RETRY,
                    MessageData.RAW_TELEPHONY_STATUS_UNDEFINED,
                    null,
                    SmsManager.MMS_ERROR_NO_DATA_NETWORK);
        }
        int status = MMS_REQUEST_MANUAL_RETRY;
        try {
            RetrieveConf retrieveConf = null;
            if (DebugUtils.isDebugEnabled() &&
                    MediaScratchFileProvider
                            .isMediaScratchSpaceUri(Uri.parse(contentLocation))) {
                if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "MmsUtils: Reading MMS from dump file: " + contentLocation);
                }
                final String fileName = Uri.parse(contentLocation).getPathSegments().get(1);
                final byte[] data = DebugUtils.receiveFromDumpFile(fileName);
                retrieveConf = receiveFromDumpFile(data);
            } else {
                if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "MmsUtils: Downloading MMS via MMS lib API; notification "
                            + "message: " + notificationUri);
                }
                if (OsUtil.isAtLeastL_MR1()) {
                    if (subId < 0) {
                        LogUtil.e(TAG, "MmsUtils: Incoming MMS came from unknown SIM");
                        throw new MmsFailureException(MMS_REQUEST_NO_RETRY,
                                "Message from unknown SIM");
                    }
                } else {
                    Assert.isTrue(subId == ParticipantData.DEFAULT_SELF_SUB_ID);
                }
                if (extras == null) {
                    extras = new Bundle();
                }
                extras.putParcelable(DownloadMmsAction.EXTRA_NOTIFICATION_URI, notificationUri);
                extras.putInt(DownloadMmsAction.EXTRA_SUB_ID, subId);
                extras.putString(DownloadMmsAction.EXTRA_SUB_PHONE_NUMBER, subPhoneNumber);
                extras.putString(DownloadMmsAction.EXTRA_TRANSACTION_ID, transactionId);
                extras.putString(DownloadMmsAction.EXTRA_CONTENT_LOCATION, contentLocation);
                extras.putBoolean(DownloadMmsAction.EXTRA_AUTO_DOWNLOAD, autoDownload);
                extras.putLong(DownloadMmsAction.EXTRA_RECEIVED_TIMESTAMP,
                        receivedTimestampInSeconds);

                MmsSender.downloadMms(context, subId, contentLocation, extras);
                return STATUS_PENDING; // Download happens asynchronously; no status to return
            }
            return insertDownloadedMessageAndSendResponse(context, notificationUri, subId,
                    subPhoneNumber, transactionId, contentLocation, autoDownload,
                    receivedTimestampInSeconds, retrieveConf);

        } catch (final MmsFailureException e) {
            LogUtil.e(TAG, "MmsUtils: failed to download message " + notificationUri, e);
            status = e.retryHint;
        } catch (final InvalidHeaderValueException e) {
            LogUtil.e(TAG, "MmsUtils: failed to download message " + notificationUri, e);
        }
        return new StatusPlusUri(status, PDU_HEADER_VALUE_UNDEFINED, null);
    }

    public static StatusPlusUri insertDownloadedMessageAndSendResponse(final Context context,
            final Uri notificationUri, final int subId, final String subPhoneNumber,
            final String transactionId, final String contentLocation,
            final boolean autoDownload, final long receivedTimestampInSeconds,
            final RetrieveConf retrieveConf) {
        final byte[] transactionIdBytes = stringToBytes(transactionId, "UTF-8");
        Uri messageUri = null;
        int status = MMS_REQUEST_MANUAL_RETRY;
        int retrieveStatus = PDU_HEADER_VALUE_UNDEFINED;

        retrieveStatus = retrieveConf.getRetrieveStatus();
        if (retrieveStatus == PduHeaders.RETRIEVE_STATUS_OK) {
            status = MMS_REQUEST_SUCCEEDED;
        } else if (retrieveStatus >= PduHeaders.RETRIEVE_STATUS_ERROR_TRANSIENT_FAILURE &&
                retrieveStatus < PduHeaders.RETRIEVE_STATUS_ERROR_PERMANENT_FAILURE) {
            status = MMS_REQUEST_AUTO_RETRY;
        } else {
            // else not meant to retry download
            status = MMS_REQUEST_NO_RETRY;
            LogUtil.e(TAG, "MmsUtils: failed to retrieve message; retrieveStatus: "
                    + retrieveStatus);
        }
        final ContentValues values = new ContentValues(1);
        values.put(Mms.RETRIEVE_STATUS, retrieveConf.getRetrieveStatus());
        SqliteWrapper.update(context, context.getContentResolver(),
                notificationUri, values, null, null);

        if (status == MMS_REQUEST_SUCCEEDED) {
            // Send response of the notification
            if (autoDownload) {
                sendNotifyResponseForMmsDownload(context, subId, transactionIdBytes,
                        contentLocation, PduHeaders.STATUS_RETRIEVED);
            } else {
                sendAcknowledgeForMmsDownload(context, subId, transactionIdBytes, contentLocation);
            }

            // Insert downloaded message into telephony
            final Uri inboxUri = MmsUtils.insertReceivedMmsMessage(context, retrieveConf, subId,
                    subPhoneNumber, receivedTimestampInSeconds, contentLocation);
            messageUri = ContentUris.withAppendedId(Mms.CONTENT_URI, ContentUris.parseId(inboxUri));
        } else if (status == MMS_REQUEST_AUTO_RETRY) {
            // For a retry do nothing
        } else if (status == MMS_REQUEST_MANUAL_RETRY && autoDownload) {
            // Failure from autodownload - just treat like manual download
            sendNotifyResponseForMmsDownload(context, subId, transactionIdBytes,
                    contentLocation, PduHeaders.STATUS_DEFERRED);
        }
        return new StatusPlusUri(status, retrieveStatus, messageUri);
    }

    /**
     * Send response for MMS download - catches and ignores errors
     */
    public static void sendNotifyResponseForMmsDownload(final Context context, final int subId,
            final byte[] transactionId, final String contentLocation, final int status) {
        try {
            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, "MmsUtils: Sending M-NotifyResp.ind for received MMS, status: "
                        + String.format("0x%X", status));
            }
            if (contentLocation == null) {
                LogUtil.w(TAG, "MmsUtils: Can't send NotifyResp; contentLocation is null");
                return;
            }
            if (transactionId == null) {
                LogUtil.w(TAG, "MmsUtils: Can't send NotifyResp; transaction id is null");
                return;
            }
            if (!isMmsDataAvailable(subId)) {
                LogUtil.w(TAG, "MmsUtils: Can't send NotifyResp; no data available");
                return;
            }
            MmsSender.sendNotifyResponseForMmsDownload(
                    context, subId, transactionId, contentLocation, status);
        } catch (final MmsFailureException e) {
            LogUtil.e(TAG, "sendNotifyResponseForMmsDownload: failed to retrieve message " + e, e);
        } catch (final InvalidHeaderValueException e) {
            LogUtil.e(TAG, "sendNotifyResponseForMmsDownload: failed to retrieve message " + e, e);
        }
    }

    /**
     * Send acknowledge for mms download - catched and ignores errors
     */
    public static void sendAcknowledgeForMmsDownload(final Context context, final int subId,
            final byte[] transactionId, final String contentLocation) {
        try {
            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, "MmsUtils: Sending M-Acknowledge.ind for received MMS");
            }
            if (contentLocation == null) {
                LogUtil.w(TAG, "MmsUtils: Can't send AckInd; contentLocation is null");
                return;
            }
            if (transactionId == null) {
                LogUtil.w(TAG, "MmsUtils: Can't send AckInd; transaction id is null");
                return;
            }
            if (!isMmsDataAvailable(subId)) {
                LogUtil.w(TAG, "MmsUtils: Can't send AckInd; no data available");
                return;
            }
            MmsSender.sendAcknowledgeForMmsDownload(context, subId, transactionId, contentLocation);
        } catch (final MmsFailureException e) {
            LogUtil.e(TAG, "sendAcknowledgeForMmsDownload: failed to retrieve message " + e, e);
        } catch (final InvalidHeaderValueException e) {
            LogUtil.e(TAG, "sendAcknowledgeForMmsDownload: failed to retrieve message " + e, e);
        }
    }

    /**
     * Try parsing a PDU without knowing the carrier. This is useful for importing
     * MMS or storing draft when carrier info is not available
     *
     * @param data The PDU data
     * @return Parsed PDU, null if failed to parse
     */
    private static GenericPdu parsePduForAnyCarrier(final byte[] data) {
        GenericPdu pdu = null;
        try {
            pdu = (new PduParser(data, true/*parseContentDisposition*/)).parse();
        } catch (final RuntimeException e) {
            LogUtil.d(TAG, "parsePduForAnyCarrier: Failed to parse PDU with content disposition",
                    e);
        }
        if (pdu == null) {
            try {
                pdu = (new PduParser(data, false/*parseContentDisposition*/)).parse();
            } catch (final RuntimeException e) {
                LogUtil.d(TAG,
                        "parsePduForAnyCarrier: Failed to parse PDU without content disposition",
                        e);
            }
        }
        return pdu;
    }

    private static RetrieveConf receiveFromDumpFile(final byte[] data) throws MmsFailureException {
        final GenericPdu pdu = parsePduForAnyCarrier(data);
        if (pdu == null || !(pdu instanceof RetrieveConf)) {
            LogUtil.e(TAG, "receiveFromDumpFile: Parsing retrieved PDU failure");
            throw new MmsFailureException(MMS_REQUEST_MANUAL_RETRY, "Failed reading dump file");
        }
        return (RetrieveConf) pdu;
    }

    private static boolean isMmsDataAvailable(final int subId) {
        if (OsUtil.isAtLeastL_MR1()) {
            // L_MR1 above may support sending mms via wifi
            return true;
        }
        final PhoneUtils phoneUtils = PhoneUtils.get(subId);
        return !phoneUtils.isAirplaneModeOn() && phoneUtils.isMobileDataEnabled();
    }

    private static boolean isSmsDataAvailable(final int subId) {
        if (OsUtil.isAtLeastL_MR1()) {
            // L_MR1 above may support sending sms via wifi
            return true;
        }
        final PhoneUtils phoneUtils = PhoneUtils.get(subId);
        return !phoneUtils.isAirplaneModeOn();
    }

    public static boolean isMobileDataEnabled(final int subId) {
        final PhoneUtils phoneUtils = PhoneUtils.get(subId);
        return phoneUtils.isMobileDataEnabled();
    }

    public static boolean isAirplaneModeOn(final int subId) {
        final PhoneUtils phoneUtils = PhoneUtils.get(subId);
        return phoneUtils.isAirplaneModeOn();
    }

    public static StatusPlusUri sendMmsMessage(final Context context, final int subId,
            final Uri messageUri, final Bundle extras) {
        int status = MMS_REQUEST_MANUAL_RETRY;
        int rawStatus = MessageData.RAW_TELEPHONY_STATUS_UNDEFINED;
        if (!isMmsDataAvailable(subId)) {
            LogUtil.w(TAG, "MmsUtils: failed to send message, no data available");
            return new StatusPlusUri(MMS_REQUEST_MANUAL_RETRY,
                    MessageData.RAW_TELEPHONY_STATUS_UNDEFINED,
                    messageUri,
                    SmsManager.MMS_ERROR_NO_DATA_NETWORK);
        }
        final PduPersister persister = PduPersister.getPduPersister(context);
        try {
            final SendReq sendReq = (SendReq) persister.load(messageUri);
            if (sendReq == null) {
                LogUtil.w(TAG, "MmsUtils: Sending MMS was deleted; uri = " + messageUri);
                return new StatusPlusUri(MMS_REQUEST_NO_RETRY,
                        MessageData.RAW_TELEPHONY_STATUS_UNDEFINED, messageUri);
            }
            if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                LogUtil.d(TAG, String.format("MmsUtils: Sending MMS, message uri: %s", messageUri));
            }
            extras.putInt(SendMessageAction.KEY_SUB_ID, subId);
            MmsSender.sendMms(context, subId, messageUri, sendReq, extras);
            return STATUS_PENDING;
        } catch (final MmsFailureException e) {
            status = e.retryHint;
            rawStatus = e.rawStatus;
            LogUtil.e(TAG, "MmsUtils: failed to send message " + e, e);
        } catch (final InvalidHeaderValueException e) {
            LogUtil.e(TAG, "MmsUtils: failed to send message " + e, e);
        } catch (final IllegalArgumentException e) {
            LogUtil.e(TAG, "MmsUtils: invalid message to send " + e, e);
        } catch (final MmsException e) {
            LogUtil.e(TAG, "MmsUtils: failed to send message " + e, e);
        }
        // If we get here, some exception occurred
        return new StatusPlusUri(status, rawStatus, messageUri);
    }

    public static StatusPlusUri updateSentMmsMessageStatus(final Context context,
            final Uri messageUri, final SendConf sendConf) {
        int status = MMS_REQUEST_MANUAL_RETRY;
        final int respStatus = sendConf.getResponseStatus();

        final ContentValues values = new ContentValues(2);
        values.put(Mms.RESPONSE_STATUS, respStatus);
        final byte[] messageId = sendConf.getMessageId();
        if (messageId != null && messageId.length > 0) {
            values.put(Mms.MESSAGE_ID, PduPersister.toIsoString(messageId));
        }
        SqliteWrapper.update(context, context.getContentResolver(),
                messageUri, values, null, null);
        if (respStatus == PduHeaders.RESPONSE_STATUS_OK) {
            status = MMS_REQUEST_SUCCEEDED;
        } else if (respStatus == PduHeaders.RESPONSE_STATUS_ERROR_TRANSIENT_FAILURE ||
                respStatus == PduHeaders.RESPONSE_STATUS_ERROR_TRANSIENT_NETWORK_PROBLEM ||
                respStatus == PduHeaders.RESPONSE_STATUS_ERROR_TRANSIENT_PARTIAL_SUCCESS) {
            status = MMS_REQUEST_AUTO_RETRY;
        } else {
            // else permanent failure
            LogUtil.e(TAG, "MmsUtils: failed to send message; respStatus = "
                    + String.format("0x%X", respStatus));
        }
        return new StatusPlusUri(status, respStatus, messageUri);
    }

    public static void clearMmsStatus(final Context context, final Uri uri) {
        // Messaging application can leave invalid values in STATUS field of M-Notification.ind
        // messages.  Take this opportunity to clear it.
        // Downloading status just kept in local db and not reflected into telephony.
        final ContentValues values = new ContentValues(1);
        values.putNull(Mms.STATUS);
        SqliteWrapper.update(context, context.getContentResolver(),
                    uri, values, null, null);
    }

    // Selection for new dedup algorithm:
    // ((m_type<>130) OR (exp>NOW)) AND (date>NOW-7d) AND (date<NOW+7d) AND (ct_l=xxxxxx)
    // i.e. If it is NotificationInd and not expired or not NotificationInd
    //      AND message is received with +/- 7 days from now
    //      AND content location is the input URL
    private static final String DUP_NOTIFICATION_QUERY_SELECTION =
            "((" + Mms.MESSAGE_TYPE + "<>?) OR (" + Mms.EXPIRY + ">?)) AND ("
                    + Mms.DATE + ">?) AND (" + Mms.DATE + "<?) AND (" + Mms.CONTENT_LOCATION +
                    "=?)";
    // Selection for old behavior: only checks NotificationInd and its content location
    private static final String DUP_NOTIFICATION_QUERY_SELECTION_OLD =
            "(" + Mms.MESSAGE_TYPE + "=?) AND (" + Mms.CONTENT_LOCATION + "=?)";

    private static final int MAX_RETURN = 32;
    private static String[] getDupNotifications(final Context context, final NotificationInd nInd) {
        final byte[] rawLocation = nInd.getContentLocation();
        if (rawLocation != null) {
            final String location = new String(rawLocation);
            // We can not be sure if the content location of an MMS is globally and historically
            // unique. So we limit the dedup time within the last 7 days
            // (or configured by gservices remotely). If the same content location shows up after
            // that, we will download regardless. Duplicated message is better than no message.
            String selection;
            String[] selectionArgs;
            final long timeLimit = BugleGservices.get().getLong(
                    BugleGservicesKeys.MMS_WAP_PUSH_DEDUP_TIME_LIMIT_SECS,
                    BugleGservicesKeys.MMS_WAP_PUSH_DEDUP_TIME_LIMIT_SECS_DEFAULT);
            if (timeLimit > 0) {
                // New dedup algorithm
                selection = DUP_NOTIFICATION_QUERY_SELECTION;
                final long nowSecs = System.currentTimeMillis() / 1000;
                final long timeLowerBoundSecs = nowSecs - timeLimit;
                // Need upper bound to protect against clock change so that a message has a time
                // stamp in the future
                final long timeUpperBoundSecs = nowSecs + timeLimit;
                selectionArgs = new String[] {
                        Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND),
                        Long.toString(nowSecs),
                        Long.toString(timeLowerBoundSecs),
                        Long.toString(timeUpperBoundSecs),
                        location
                };
            } else {
                // If time limit is 0, we revert back to old behavior in case the new
                // dedup algorithm behaves badly
                selection = DUP_NOTIFICATION_QUERY_SELECTION_OLD;
                selectionArgs = new String[] {
                        Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND),
                        location
                };
            }
            Cursor cursor = null;
            try {
                cursor = SqliteWrapper.query(
                        context, context.getContentResolver(),
                        Mms.CONTENT_URI, new String[] { Mms._ID },
                        selection, selectionArgs, null);
                final int dupCount = cursor.getCount();
                if (dupCount > 0) {
                    // We already received the same notification before.
                    // Don't want to return too many dups. It is only for debugging.
                    final int returnCount = dupCount < MAX_RETURN ? dupCount : MAX_RETURN;
                    final String[] dups = new String[returnCount];
                    for (int i = 0; cursor.moveToNext() && i < returnCount; i++) {
                        dups[i] = cursor.getString(0);
                    }
                    return dups;
                }
            } catch (final SQLiteException e) {
                LogUtil.e(TAG, "query failure: " + e, e);
            } finally {
                cursor.close();
            }
        }
        return null;
    }

    /**
     * Try parse the address using RFC822 format. If it fails to parse, then return the
     * original address
     *
     * @param address The MMS ind sender address to parse
     * @return The real address. If in RFC822 format, returns the correct email.
     */
    private static String parsePotentialRfc822EmailAddress(final String address) {
        if (address == null || !address.contains("@") || !address.contains("<")) {
            return address;
        }
        final Rfc822Token[] tokens = Rfc822Tokenizer.tokenize(address);
        if (tokens != null && tokens.length > 0) {
            for (final Rfc822Token token : tokens) {
                if (token != null && !TextUtils.isEmpty(token.getAddress())) {
                    return token.getAddress();
                }
            }
        }
        return address;
    }

    public static DatabaseMessages.MmsMessage processReceivedPdu(final Context context,
            final byte[] pushData, final int subId, final String subPhoneNumber) {
        // Parse data

        // Insert placeholder row to telephony and local db
        // Get raw PDU push-data from the message and parse it
        final PduParser parser = new PduParser(pushData,
                MmsConfig.get(subId).getSupportMmsContentDisposition());
        final GenericPdu pdu = parser.parse();

        if (null == pdu) {
            LogUtil.e(TAG, "Invalid PUSH data");
            return null;
        }

        final PduPersister p = PduPersister.getPduPersister(context);
        final int type = pdu.getMessageType();

        Uri messageUri = null;
        long threadId = -1;
        switch (type) {
            case PduHeaders.MESSAGE_TYPE_DELIVERY_IND: {
                long localId = -1;
                String messageId = new String(((DeliveryInd) pdu).getMessageId());
                //Update Telephony DB
                Cursor cursor = getCursorFromMessageId(context, messageId);
                if (cursor != null) {
                    try {
                        if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
                            threadId = cursor.getLong(cursor.getColumnIndex(Mms.THREAD_ID));
                            localId = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID));
                        }
                    } finally {
                        cursor.close();
                    }
                }
                if (threadId == -1 || localId == -1) {
                    // The associated SendReq isn't found, therefore skip
                    // processing this PDU.
                    break;
                }
                Uri inboxUri = null;
                try {
                    inboxUri = p.persist(pdu, Mms.Inbox.CONTENT_URI, subId, subPhoneNumber,
                            null);
                    // Update thread ID for ReadOrigInd & DeliveryInd.
                    ContentValues values = new ContentValues(1);
                    values.put(Mms.THREAD_ID, threadId);
                    SqliteWrapper.update(context, context.getContentResolver(), inboxUri, values,
                            null, null);
                } catch (final MmsException e) {
                    LogUtil.e(TAG, "Failed to save the data from PUSH: type=" + type, e);
                }
                // Update local message
                final DatabaseWrapper db = DataModel.get().getDatabase();
                db.beginTransaction();
                try {
                    int status = ((DeliveryInd) pdu).getStatus();
                    if (status == PduHeaders.STATUS_RETRIEVED ||
                            status == PduHeaders.STATUS_FORWARDED) {
                        final ContentValues values = new ContentValues();
                        status = MessageData.BUGLE_STATUS_OUTGOING_DELIVERED;
                        values.put(DatabaseHelper.MessageColumns.STATUS, status);
                        Uri mUri = Uri.withAppendedPath(Mms.CONTENT_URI, String.valueOf(localId));
                        final MessageData messageData =
                                BugleDatabaseOperations.readMessageData(db, mUri);
                        // Check the message was not removed before the delivery report comes in
                        if (messageData != null) {
                            // Row must exist as was just loaded above (on ActionService thread)
                            BugleDatabaseOperations.updateMessageRow(db, messageData.getMessageId(),
                                    values);
                            MessagingContentProvider.notifyMessagesChanged(
                                    messageData.getConversationId());
                        }
                        db.setTransactionSuccessful();
                    }
                } finally {
                    db.endTransaction();
                }
                break;
            }
            case PduHeaders.MESSAGE_TYPE_READ_ORIG_IND: {
                // TODO: Should this be commented out?
//                threadId = findThreadId(context, pdu, type);
//                if (threadId == -1) {
//                    // The associated SendReq isn't found, therefore skip
//                    // processing this PDU.
//                    break;
//                }

//                Uri uri = p.persist(pdu, Inbox.CONTENT_URI, true,
//                        MessagingPreferenceActivity.getIsGroupMmsEnabled(mContext), null);
//                // Update thread ID for ReadOrigInd & DeliveryInd.
//                ContentValues values = new ContentValues(1);
//                values.put(Mms.THREAD_ID, threadId);
//                SqliteWrapper.update(mContext, cr, uri, values, null, null);
                LogUtil.w(TAG, "Received unsupported WAP Push, type=" + type);
                break;
            }
            case PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND: {
                final NotificationInd nInd = (NotificationInd) pdu;

                if (MmsConfig.get(subId).getTransIdEnabled()) {
                    final byte [] contentLocationTemp = nInd.getContentLocation();
                    if ('=' == contentLocationTemp[contentLocationTemp.length - 1]) {
                        final byte [] transactionIdTemp = nInd.getTransactionId();
                        final byte [] contentLocationWithId =
                                new byte [contentLocationTemp.length
                                                                  + transactionIdTemp.length];
                        System.arraycopy(contentLocationTemp, 0, contentLocationWithId,
                                0, contentLocationTemp.length);
                        System.arraycopy(transactionIdTemp, 0, contentLocationWithId,
                                contentLocationTemp.length, transactionIdTemp.length);
                        nInd.setContentLocation(contentLocationWithId);
                    }
                }
                final String[] dups = getDupNotifications(context, nInd);
                if (dups == null) {
                    // TODO: Do we handle Rfc822 Email Addresses?
                    //final String contentLocation =
                    //        MmsUtils.bytesToString(nInd.getContentLocation(), "UTF-8");
                    //final byte[] transactionId = nInd.getTransactionId();
                    //final long messageSize = nInd.getMessageSize();
                    //final long expiry = nInd.getExpiry();
                    //final String transactionIdString =
                    //        MmsUtils.bytesToString(transactionId, "UTF-8");

                    //final EncodedStringValue fromEncoded = nInd.getFrom();
                    // An mms ind received from email address will have from address shown as
                    // "John Doe <johndoe@foobar.com>" but the actual received message will only
                    // have the email address. So let's try to parse the RFC822 format to get the
                    // real email. Otherwise we will create two conversations for the MMS
                    // notification and the actual MMS message if auto retrieve is disabled.
                    //final String from = parsePotentialRfc822EmailAddress(
                    //        fromEncoded != null ? fromEncoded.getString() : null);

                    Uri inboxUri = null;
                    try {
                        inboxUri = p.persist(pdu, Mms.Inbox.CONTENT_URI, subId, subPhoneNumber,
                                null);
                        messageUri = ContentUris.withAppendedId(Mms.CONTENT_URI,
                                ContentUris.parseId(inboxUri));
                    } catch (final MmsException e) {
                        LogUtil.e(TAG, "Failed to save the data from PUSH: type=" + type, e);
                    }
                } else {
                    LogUtil.w(TAG, "Received WAP Push is a dup: " + Joiner.on(',').join(dups));
                    if (LogUtil.isLoggable(TAG, LogUtil.VERBOSE)) {
                        LogUtil.w(TAG, "Dup WAP Push url=" + new String(nInd.getContentLocation()));
                    }
                }
                break;
            }
            default:
                LogUtil.e(TAG, "Received unrecognized WAP Push, type=" + type);
        }

        DatabaseMessages.MmsMessage mms = null;
        if (messageUri != null) {
            mms = MmsUtils.loadMms(messageUri);
        }
        return mms;
    }

    private static Cursor getCursorFromMessageId(Context context, String messageId) {
        StringBuilder sb = new StringBuilder('(');
        sb.append(Mms.MESSAGE_ID);
        sb.append('=');
        sb.append(DatabaseUtils.sqlEscapeString(messageId));
        sb.append(" AND ");
        sb.append(Mms.MESSAGE_TYPE);
        sb.append('=');
        sb.append(PduHeaders.MESSAGE_TYPE_SEND_REQ);
        Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
                Mms.CONTENT_URI, new String[]{BaseColumns._ID, Mms.THREAD_ID},
                sb.toString(), null, null);
        return cursor;
    }

    public static Uri insertSendingMmsMessage(final Context context, final List<String> recipients,
            final MessageData content, final int subId, final String subPhoneNumber,
            final long timestamp) {
        final boolean requireDeliveryReport = isMmsDeliveryReportRequired(subId);
        final boolean requireReadReport = isMmsReadReportRequired(subId);
        final long expiryTime = getExpiryTime(subId);
        final SendReq sendReq = createMmsSendReq(
                context, subId, recipients.toArray(new String[recipients.size()]), content,
                requireDeliveryReport,
                requireReadReport,
                expiryTime,
                getSendPriority(subId),
                timestamp);
        Uri messageUri = null;
        if (sendReq != null) {
            final Uri outboxUri = MmsUtils.insertSendReq(context, sendReq, subId, subPhoneNumber);
            if (outboxUri != null) {
                messageUri = ContentUris.withAppendedId(Telephony.Mms.CONTENT_URI,
                        ContentUris.parseId(outboxUri));
                if (LogUtil.isLoggable(TAG, LogUtil.DEBUG)) {
                    LogUtil.d(TAG, "Mmsutils: Inserted sending MMS message into telephony, uri: "
                            + outboxUri);
                }
            } else {
                LogUtil.e(TAG, "insertSendingMmsMessage: failed to persist message into telephony");
            }
        }
        return messageUri;
    }

    public static MessageData readSendingMmsMessage(final Uri messageUri,
            final String conversationId, final String participantId, final String selfId) {
        MessageData message = null;
        if (messageUri != null) {
            final DatabaseMessages.MmsMessage mms = MmsUtils.loadMms(messageUri);

            // Make sure that the message has not been deleted from the Telephony DB
            if (mms != null) {
                // Transform the message
                message = MmsUtils.createMmsMessage(mms, conversationId, participantId, selfId,
                        MessageData.BUGLE_STATUS_OUTGOING_RESENDING);
            }
        }
        return message;
    }

    /**
     * Create an MMS message with subject, text and image
     *
     * @return Both the M-Send.req and the M-Send.conf for processing in the caller
     * @throws MmsException
     */
    private static SendReq createMmsSendReq(final Context context, final int subId,
            final String[] recipients, final MessageData message,
            final boolean requireDeliveryReport, final boolean requireReadReport,
            final long expiryTime, final int priority, final long timestampMillis) {
        Assert.notNull(context);
        if (recipients == null || recipients.length < 1) {
            throw new IllegalArgumentException("MMS sendReq no recipient");
        }

        // Make a copy so we don't propagate changes to recipients to outside of this method
        final String[] recipientsCopy = new String[recipients.length];
        // Don't send phone number as is since some received phone number is malformed
        // for sending. We need to strip the separators.
        for (int i = 0; i < recipients.length; i++) {
            final String recipient = recipients[i];
            if (EmailAddress.isValidEmail(recipients[i])) {
                // Don't do stripping for emails
                recipientsCopy[i] = recipient;
            } else {
                recipientsCopy[i] = stripPhoneNumberSeparators(recipient);
            }
        }

        SendReq sendReq = null;
        try {
            sendReq = createSendReq(context, subId, recipientsCopy,
                    message, requireDeliveryReport,
                    requireReadReport, expiryTime, priority, timestampMillis);
        } catch (final InvalidHeaderValueException e) {
            LogUtil.e(TAG, "InvalidHeaderValue creating sendReq PDU");
        } catch (final OutOfMemoryError e) {
            LogUtil.e(TAG, "Out of memory error creating sendReq PDU");
        }
        return sendReq;
    }

    /**
     * Stripping out the invalid characters in a phone number before sending
     * MMS. We only keep alphanumeric and '*', '#', '+'.
     */
    private static String stripPhoneNumberSeparators(final String phoneNumber) {
        if (phoneNumber == null) {
            return null;
        }
        final int len = phoneNumber.length();
        final StringBuilder ret = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            final char c = phoneNumber.charAt(i);
            if (Character.isLetterOrDigit(c) || c == '+' || c == '*' || c == '#') {
                ret.append(c);
            }
        }
        return ret.toString();
    }

    /**
     * Create M-Send.req for the MMS message to be sent.
     *
     * @return the M-Send.req
     * @throws InvalidHeaderValueException if there is any error in parsing the input
     */
    static SendReq createSendReq(final Context context, final int subId,
            final String[] recipients, final MessageData message,
            final boolean requireDeliveryReport,
            final boolean requireReadReport, final long expiryTime, final int priority,
            final long timestampMillis)
            throws InvalidHeaderValueException {
        final SendReq req = new SendReq();
        // From, per spec
        final String lineNumber = PhoneUtils.get(subId).getCanonicalForSelf(true/*allowOverride*/);
        if (!TextUtils.isEmpty(lineNumber)) {
            req.setFrom(new EncodedStringValue(lineNumber));
        }
        // To
        final EncodedStringValue[] encodedNumbers = EncodedStringValue.encodeStrings(recipients);
        if (encodedNumbers != null) {
            req.setTo(encodedNumbers);
        }
        // Subject
        if (!TextUtils.isEmpty(message.getMmsSubject())) {
            req.setSubject(new EncodedStringValue(message.getMmsSubject()));
        }
        // Date
        req.setDate(timestampMillis / 1000L);
        // Body
        final MmsInfo bodyInfo = MmsUtils.makePduBody(context, message, subId);
        req.setBody(bodyInfo.mPduBody);
        // Message size
        req.setMessageSize(bodyInfo.mMessageSize);
        // Message class
        req.setMessageClass(PduHeaders.MESSAGE_CLASS_PERSONAL_STR.getBytes());
        // Expiry
        if(expiryTime != -1) { // Set only if != -1 (MAX). For -1,  not calling setExpiry leads to use MAX as default.
            req.setExpiry(expiryTime);
        }
        // Priority
        req.setPriority(priority);
        // Delivery report
        req.setDeliveryReport(requireDeliveryReport ? PduHeaders.VALUE_YES : PduHeaders.VALUE_NO);
        // Read report
        req.setReadReport(requireReadReport ? PduHeaders.VALUE_YES : PduHeaders.VALUE_NO);
        return req;
    }

    public static boolean isDeliveryReportRequired(final int subId) {
        if (!MmsConfig.get(subId).getSMSDeliveryReportsEnabled()) {
            return false;
        }
        final Resources res = Factory.get().getApplicationContext().getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String deliveryReportKey = res.getString(R.string.delivery_reports_pref_key);
        final boolean defaultValue = res.getBoolean(R.bool.delivery_reports_pref_default);
        return prefs.getBoolean(deliveryReportKey, defaultValue);
    }

    private static boolean isMmsDeliveryReportRequired(final int subId) {
        final Resources res = Factory.get().getApplicationContext().getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String mmsDeliveryReportKey = res.getString(R.string.delivery_reports_mms_pref_key);
        final boolean defaultValue = res.getBoolean(R.bool.def_mms_delivery_reports);
        return prefs.getBoolean(mmsDeliveryReportKey, defaultValue);
    }

    private static boolean isMmsReadReportRequired(final int subId) {
        final Resources res = Factory.get().getApplicationContext().getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String readReportKey = res.getString(R.string.read_reports_mms_pref_key);
        final boolean defaultValue = DEFAULT_READ_REPORT_MODE;
        return prefs.getBoolean(readReportKey, defaultValue);
    }

    private static long getExpiryTime(final int subId) {
        final Resources res = Factory.get().getApplicationContext().getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String expiryStr = res.getString(R.string.expiry_mms_pref_key);
        final long expiryTime = Long.parseLong(prefs.getString(expiryStr, "0"));
        return expiryTime != 0 ? expiryTime : DEFAULT_EXPIRY_TIME_IN_SECONDS;
    }

    private static int getSendPriority(final int subId){
        final Resources res = Factory.get().getApplicationContext().getResources();
        final BuglePrefs prefs = BuglePrefs.getSubscriptionPrefs(subId);
        final String sendPriorityMmsKey = res.getString(R.string.priority_mms_pref_key);
        String priority = prefs.getString(sendPriorityMmsKey, Integer.toString(DEFAULT_PRIORITY));
        return Integer.parseInt(priority);
    }

    public static int sendSmsMessage(final String recipient, final String messageText,
            final Uri requestUri, final int subId,
            final String smsServiceCenter, final boolean requireDeliveryReport) {
        if (!isSmsDataAvailable(subId)) {
            LogUtil.w(TAG, "MmsUtils: can't send SMS without radio");
            return MMS_REQUEST_MANUAL_RETRY;
        }
        final Context context = Factory.get().getApplicationContext();
        int status = MMS_REQUEST_MANUAL_RETRY;
        try {
            // Send a single message
            final SendResult result = SmsSender.sendMessage(
                    context,
                    subId,
                    recipient,
                    messageText,
                    smsServiceCenter,
                    requireDeliveryReport,
                    requestUri);
            if (!result.hasPending()) {
                // not timed out, check failures
                final int failureLevel = result.getHighestFailureLevel();
                switch (failureLevel) {
                    case SendResult.FAILURE_LEVEL_NONE:
                        status = MMS_REQUEST_SUCCEEDED;
                        break;
                    case SendResult.FAILURE_LEVEL_TEMPORARY:
                        status = MMS_REQUEST_AUTO_RETRY;
                        LogUtil.e(TAG, "MmsUtils: SMS temporary failure");
                        break;
                    case SendResult.FAILURE_LEVEL_PERMANENT:
                        LogUtil.e(TAG, "MmsUtils: SMS permanent failure");
                        break;
                }
            } else {
                // Timed out
                LogUtil.e(TAG, "MmsUtils: sending SMS timed out");
            }
        } catch (final Exception e) {
            LogUtil.e(TAG, "MmsUtils: failed to send SMS " + e, e);
        }
        return status;
    }

    /**
     * Delete SMS and MMS messages in a particular thread
     *
     * @return the number of messages deleted
     */
    public static int deleteThread(final long threadId, final long cutOffTimestampInMillis) {
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        final Uri threadUri = ContentUris.withAppendedId(Telephony.Threads.CONTENT_URI, threadId);
        if (cutOffTimestampInMillis < Long.MAX_VALUE) {
            return resolver.delete(threadUri, Sms.DATE + "<=?",
                    new String[] { Long.toString(cutOffTimestampInMillis) });
        } else {
            return resolver.delete(threadUri, null /* smsSelection */, null /* selectionArgs */);
        }
    }

    /**
     * Delete single SMS and MMS message
     *
     * @return number of rows deleted (should be 1 or 0)
     */
    public static int deleteMessage(final Uri messageUri) {
        final ContentResolver resolver = Factory.get().getApplicationContext().getContentResolver();
        return resolver.delete(messageUri, null /* selection */, null /* selectionArgs */);
    }

    public static byte[] createDebugNotificationInd(final String fileName) {
        byte[] pduData = null;
        try {
            final Context context = Factory.get().getApplicationContext();
            // Load the message file
            final byte[] data = DebugUtils.receiveFromDumpFile(fileName);
            final RetrieveConf retrieveConf = receiveFromDumpFile(data);
            // Create the notification
            final NotificationInd notification = new NotificationInd();
            final long expiry = System.currentTimeMillis() / 1000 + 600;
            notification.setTransactionId(fileName.getBytes());
            notification.setMmsVersion(retrieveConf.getMmsVersion());
            notification.setFrom(retrieveConf.getFrom());
            notification.setSubject(retrieveConf.getSubject());
            notification.setExpiry(expiry);
            notification.setMessageSize(data.length);
            notification.setMessageClass(retrieveConf.getMessageClass());

            final Uri.Builder builder = MediaScratchFileProvider.getUriBuilder();
            builder.appendPath(fileName);
            final Uri contentLocation = builder.build();
            notification.setContentLocation(contentLocation.toString().getBytes());

            // Serialize
            pduData = new PduComposer(context, notification).make();
            if (pduData == null || pduData.length < 1) {
                throw new IllegalArgumentException("Empty or zero length PDU data");
            }
        } catch (final MmsFailureException e) {
            // Nothing to do
        } catch (final InvalidHeaderValueException e) {
            // Nothing to do
        }
        return pduData;
    }

    public static int mapRawStatusToErrorResourceId(final int bugleStatus, final int rawStatus) {
        int stringResId = R.string.message_status_send_failed;
        switch (rawStatus) {
            case PduHeaders.RESPONSE_STATUS_ERROR_SERVICE_DENIED:
            case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_SERVICE_DENIED:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_REPLY_CHARGING_LIMITATIONS_NOT_MET:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_REPLY_CHARGING_REQUEST_NOT_ACCEPTED:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_REPLY_CHARGING_FORWARDING_DENIED:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_REPLY_CHARGING_NOT_SUPPORTED:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_ADDRESS_HIDING_NOT_SUPPORTED:
            //case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_LACK_OF_PREPAID:
                stringResId = R.string.mms_failure_outgoing_service;
                break;
            case PduHeaders.RESPONSE_STATUS_ERROR_SENDING_ADDRESS_UNRESOLVED:
            case PduHeaders.RESPONSE_STATUS_ERROR_TRANSIENT_SENDNG_ADDRESS_UNRESOLVED:
            case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_SENDING_ADDRESS_UNRESOLVED:
                stringResId = R.string.mms_failure_outgoing_address;
                break;
            case PduHeaders.RESPONSE_STATUS_ERROR_MESSAGE_FORMAT_CORRUPT:
            case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_MESSAGE_FORMAT_CORRUPT:
                stringResId = R.string.mms_failure_outgoing_corrupt;
                break;
            case PduHeaders.RESPONSE_STATUS_ERROR_CONTENT_NOT_ACCEPTED:
            case PduHeaders.RESPONSE_STATUS_ERROR_PERMANENT_CONTENT_NOT_ACCEPTED:
                stringResId = R.string.mms_failure_outgoing_content;
                break;
            case PduHeaders.RESPONSE_STATUS_ERROR_UNSUPPORTED_MESSAGE:
            //case PduHeaders.RESPONSE_STATUS_ERROR_MESSAGE_NOT_FOUND:
            //case PduHeaders.RESPONSE_STATUS_ERROR_TRANSIENT_MESSAGE_NOT_FOUND:
                stringResId = R.string.mms_failure_outgoing_unsupported;
                break;
            case MessageData.RAW_TELEPHONY_STATUS_MESSAGE_TOO_BIG:
                stringResId = R.string.mms_failure_outgoing_too_large;
                break;
        }
        return stringResId;
    }

    /**
     * The absence of a connection type.
     */
    public static final int TYPE_NONE = -1;

    public static int getConnectivityEventNetworkType(final Context context, final Intent intent) {
        final ConnectivityManager connMgr = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (OsUtil.isAtLeastJB_MR1()) {
            return intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, TYPE_NONE);
        } else {
            final NetworkInfo info = (NetworkInfo) intent.getParcelableExtra(
                    ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                return info.getType();
            }
        }
        return TYPE_NONE;
    }

    /**
     * Dump the raw MMS data into a file
     *
     * @param rawPdu The raw pdu data
     * @param pdu The parsed pdu, used to construct a dump file name
     */
    public static void dumpPdu(final byte[] rawPdu, final GenericPdu pdu) {
        if (rawPdu == null || rawPdu.length < 1) {
            return;
        }
        final String dumpFileName = MmsUtils.MMS_DUMP_PREFIX + getDumpFileId(pdu);
        final File dumpFile = DebugUtils.getDebugFile(dumpFileName, true);
        if (dumpFile != null) {
            try {
                final FileOutputStream fos = new FileOutputStream(dumpFile);
                final BufferedOutputStream bos = new BufferedOutputStream(fos);
                try {
                    bos.write(rawPdu);
                    bos.flush();
                } finally {
                    bos.close();
                }
                DebugUtils.ensureReadable(dumpFile);
            } catch (final IOException e) {
                LogUtil.e(TAG, "dumpPdu: " + e, e);
            }
        }
    }

    /**
     * Get the dump file id based on the parsed PDU
     * 1. Use message id if not empty
     * 2. Use transaction id if message id is empty
     * 3. If all above is empty, use random UUID
     *
     * @param pdu the parsed PDU
     * @return the id of the dump file
     */
    private static String getDumpFileId(final GenericPdu pdu) {
        String fileId = null;
        if (pdu != null && pdu instanceof RetrieveConf) {
            final RetrieveConf retrieveConf = (RetrieveConf) pdu;
            if (retrieveConf.getMessageId() != null) {
                fileId = new String(retrieveConf.getMessageId());
            } else if (retrieveConf.getTransactionId() != null) {
                fileId = new String(retrieveConf.getTransactionId());
            }
        }
        if (TextUtils.isEmpty(fileId)) {
            fileId = UUID.randomUUID().toString();
        }
        return fileId;
    }
}