summaryrefslogtreecommitdiff
path: root/sdl_android_lib/src/com/smartdevicelink/transport/SdlRouterService.java
blob: 5070eecf4e251ddc511df5acfd6f3028bf6cadf1 (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
package com.smartdevicelink.transport;

import static com.smartdevicelink.transport.TransportConstants.CONNECTED_DEVICE_STRING_EXTRA_NAME;
import static com.smartdevicelink.transport.TransportConstants.FORMED_PACKET_EXTRA_NAME;
import static com.smartdevicelink.transport.TransportConstants.HARDWARE_DISCONNECTED;
import static com.smartdevicelink.transport.TransportConstants.SEND_PACKET_TO_APP_LOCATION_EXTRA_NAME;

import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.Vector;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.json.JSONException;
import org.json.JSONObject;

import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Notification;
import android.app.Service;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.ResolveInfo;
import android.content.pm.ServiceInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Message;
import android.os.Messenger;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.util.Log;
import android.util.SparseArray;

import com.smartdevicelink.R;
import com.smartdevicelink.marshal.JsonRPCMarshaller;
import com.smartdevicelink.protocol.BinaryFrameHeader;
import com.smartdevicelink.protocol.ProtocolMessage;
import com.smartdevicelink.protocol.SdlPacket;
import com.smartdevicelink.protocol.SdlPacketFactory;
import com.smartdevicelink.protocol.enums.FrameType;
import com.smartdevicelink.protocol.enums.FunctionID;
import com.smartdevicelink.protocol.enums.MessageType;
import com.smartdevicelink.protocol.enums.SessionType;
import com.smartdevicelink.proxy.rpc.UnregisterAppInterface;
import com.smartdevicelink.transport.enums.TransportType;
import com.smartdevicelink.transport.utl.ByteAraryMessageAssembler;
import com.smartdevicelink.transport.utl.ByteArrayMessageSpliter;
import com.smartdevicelink.util.AndroidTools;
import com.smartdevicelink.util.BitConverter;
/**
 * <b>This class should not be modified by anyone outside of the approved contributors of the SmartDeviceLink project.</b>
 * This service is a central point of communication between hardware and the registered clients. It will multiplex a single transport
 * to provide a connection for a theoretical infinite amount of SDL sessions. 
 * @author Joey Grover
 *
 */
public class SdlRouterService extends Service{
	
	private static final String TAG = "Sdl Router Service";
	/**
	 * <b> NOTE: DO NOT MODIFY THIS UNLESS YOU KNOW WHAT YOU'RE DOING.</b>
	 */
	protected static final int ROUTER_SERVICE_VERSION_NUMBER = 3;	
	
	private static final String ROUTER_SERVICE_PROCESS = "com.smartdevicelink.router";
	
	private static final int FOREGROUND_SERVICE_ID = 849;
	
	private static final long CLIENT_PING_DELAY = 1000;
	
	public static final String REGISTER_NEWER_SERVER_INSTANCE_ACTION		= "com.sdl.android.newservice";
	public static final String START_SERVICE_ACTION							= "sdl.router.startservice";
	public static final String REGISTER_WITH_ROUTER_ACTION 					= "com.sdl.android.register"; 
	
	/** Message types sent from the BluetoothReadService Handler */
    public static final int MESSAGE_STATE_CHANGE = 1;
    public static final int MESSAGE_READ = 2;
    public static final int MESSAGE_WRITE = 3;
    public static final int MESSAGE_DEVICE_NAME = 4;
    public static final int MESSAGE_TOAST = 5;
	
    private final int UNREGISTER_APP_INTERFACE_CORRELATION_ID = 65530;
    
	private static MultiplexBluetoothTransport mSerialService = null;

	private static boolean connectAsClient = false;
	private static boolean closing = false;
	private boolean isTransportConnected = false;
	private TransportType connectedTransportType = null;
	private static Context currentContext = null;
    
    private Handler versionCheckTimeOutHandler, altTransportTimerHandler;
    private Runnable versionCheckRunable, altTransportTimerRunnable;
    private LocalRouterService localCompareTo = null;
    private final static int VERSION_TIMEOUT_RUNNABLE = 1500;
    private final static int ALT_TRANSPORT_TIMEOUT_RUNNABLE = 30000; 
	
    private boolean wrongProcess = false;
	private boolean initPassed = false;

    private Intent lastReceivedStartIntent = null;
	public static HashMap<Long,RegisteredApp> registeredApps;
	private SparseArray<Long> sessionMap;
	private SparseArray<Integer> sessionHashIdMap;
	private final Object SESSION_LOCK = new Object(), REGISTERED_APPS_LOCK = new Object(), PING_COUNT_LOCK = new Object();
	
	private static Messenger altTransportService = null;
	
	private String  connectedDeviceName = "";			//The name of the connected Device
	private boolean startSequenceComplete = false;	
	
	private ExecutorService packetExecuter = null; 
	PacketWriteTaskMaster packetWriteTaskMaster = null;

	private static LocalRouterService selfRouterService;
	
	/**
	 * This flag is to keep track of if we are currently acting as a foreground service
	 */
	private boolean isForeground = false;

	private int cachedModuleVersion = -1;
	
	/**
	 * Executor for making sure clients are still running during trying times
	 */
	private ScheduledExecutorService clientPingExecutor = null;
	Intent pingIntent = null;
	private boolean isPingingClients = false;
	int pingCount = 0;

	
	/* **************************************************************************************************************************************
	****************************************************************************************************************************************
	***********************************************  Broadcast Receivers START  **************************************************************
	****************************************************************************************************************************************
	****************************************************************************************************************************************/	
	
	/** create our receiver from the router service */
    BroadcastReceiver mainServiceReceiver = new BroadcastReceiver() 
	{
		@Override
		public void onReceive(Context context, Intent intent) 
		{				
			//Let's grab where to reply to this intent at. We will keep it temp right now because we may have to deny registration
			String action =intent.getStringExtra(SEND_PACKET_TO_APP_LOCATION_EXTRA_NAME);
			sendBroadcast(prepareRegistrationIntent(action));	
		}
	};
	
	private Intent prepareRegistrationIntent(String action){
		Intent registrationIntent = new Intent();
		registrationIntent.setAction(action);
		registrationIntent.putExtra(TransportConstants.BIND_LOCATION_PACKAGE_NAME_EXTRA, this.getPackageName());
		registrationIntent.putExtra(TransportConstants.BIND_LOCATION_CLASS_NAME_EXTRA, this.getClass().getName());
		return registrationIntent;
	}
	
	private void onAppRegistered(RegisteredApp app){
		//Log.enableDebug(receivedIntent.getBooleanExtra(LOG_BASIC_DEBUG_BOOLEAN_EXTRA, false));
		//Log.enableBluetoothTraceLogging(receivedIntent.getBooleanExtra(LOG_TRACE_BT_DEBUG_BOOLEAN_EXTRA, false));
		//Ok this is where we should do some authenticating...maybe. 
		//Should we ask for all relevant data in this packet?
		if(BluetoothAdapter.getDefaultAdapter()!=null && BluetoothAdapter.getDefaultAdapter().isEnabled()){
			
			if(startSequenceComplete &&
					!connectAsClient && (mSerialService ==null 
					|| mSerialService.getState() == MultiplexBluetoothTransport.STATE_NONE)){
				Log.e(TAG, "Serial service not initliazed while registering app");
				//Maybe we should try to do a connect here instead
				Log.d(TAG, "Serial service being restarted");
				if(mSerialService ==null){
					Log.e(TAG, "Local copy of BT Server is null");
					mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance();
					if(mSerialService==null){
						Log.e(TAG, "Local copy of BT Server is still null and so is global");
						mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(mHandlerBT);

					}
				}
				mSerialService.start();

			}
		}

		Log.i(TAG, app.appId + " has just been registered with SDL Router Service");
	}
	
	
	 /**
	  * this is to make sure the AceeptThread is still running
	  */
		BroadcastReceiver registerAnInstanceOfSerialServer = new BroadcastReceiver() {
			final Object COMPARE_LOCK = new Object();
					@Override
					public void onReceive(Context context, Intent intent) 
					{
						LocalRouterService tempService = intent.getParcelableExtra(SdlBroadcastReceiver.LOCAL_ROUTER_SERVICE_EXTRA);
						synchronized(COMPARE_LOCK){
							//Let's make sure we are on the same version.
							if(tempService!=null){
								if(tempService.name!=null){
									sdlMultiList.remove(tempService.name.getPackageName());
								}
								if((localCompareTo == null || localCompareTo.isNewer(tempService)) && AndroidTools.isServiceExported(context, tempService.name)){
									LocalRouterService self = getLocalRouterService();
									if(!self.isEqual(tempService)){ //We want to ignore self
										Log.i(TAG, "Newer service received than previously stored service - " + tempService.launchIntent.getAction());
										localCompareTo = tempService;
									}else{
										Log.i(TAG, "Ignoring self local router service");
									}
								}
								if(sdlMultiList.isEmpty()){
									Log.d(TAG, "All router services have been accounted more. We can start the version check now");
									if(versionCheckTimeOutHandler!=null){
										versionCheckTimeOutHandler.removeCallbacks(versionCheckRunable);
										
										versionCheckRunable.run();
										
										
									}
								}
							}
						}
						/*if(intent!=null && intent.getBooleanExtra(SdlBroadcastReceiver.LOCAL_ROUTER_SERVICE_DID_START_OWN, false)){
							Log.w(TAG, "Another serivce has been started, let's resend our version info to make sure they know about us too");
						}*/

					}
					@SuppressWarnings("unused")
					private void notifyStartedService(Context context){
						Intent restart = new Intent(SdlRouterService.REGISTER_NEWER_SERVER_INSTANCE_ACTION);
				    	restart.putExtra(SdlBroadcastReceiver.LOCAL_ROUTER_SERVICE_EXTRA, getLocalRouterService());
				    	context.sendBroadcast(restart);
					}
			};
			
			
	
	/**
	 * If the user disconnects the bluetooth device we will want to stop SDL and our current
	 * connection through RFCOMM
	 */
	BroadcastReceiver mListenForDisconnect = new BroadcastReceiver() 
			{
				@Override
				public void onReceive(Context context, Intent intent) 
				{
					String action = intent.getAction();
			    	if(action!=null){Log.d(TAG, "Disconnect received. Action: " + intent.getAction());}
			    	else{Log.d(TAG, "Disconnect received.");}
					if(intent.getAction()!=null && intent.getAction().equalsIgnoreCase("android.bluetooth.adapter.action.STATE_CHANGED") 
							&&(	(BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_TURNING_ON) 
							|| (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_ON))){
						return;
					}

					connectAsClient=false;
					
					if(action!=null && intent.getAction().equalsIgnoreCase("android.bluetooth.adapter.action.STATE_CHANGED") 
							&&(	(BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_TURNING_OFF) 
							|| (BluetoothAdapter.getDefaultAdapter().getState() == BluetoothAdapter.STATE_OFF))){
							Log.d(TAG, "Bluetooth is shutting off, SDL Router Service is closing.");
							//Since BT is shutting off...there's no reason for us to be on now. 
							//Let's take a break...I'm sleepy
							shouldServiceRemainOpen(intent);
						}
					else{//So we just got d/c'ed from the bluetooth...alright...Let the client know
						if(legacyModeEnabled){
							Log.d(TAG, "Legacy mode enabled and bluetooth d/c'ed, restarting router service bluetooth.");
							enableLegacyMode(false);
							onTransportDisconnected(TransportType.BLUETOOTH);
							initBluetoothSerialService();
						}
					}
			}
		};
		
/* **************************************************************************************************************************************
***********************************************  Broadcast Receivers End  **************************************************************
****************************************************************************************************************************************/

		/* **************************************************************************************************************************************
		*********************************************** Handlers for bound clients **************************************************************
		****************************************************************************************************************************************/

		
	    /**
	     * Target we publish for clients to send messages to RouterHandler.
	     */
	    final Messenger routerMessenger = new Messenger(new RouterHandler(this));
	    
		 /**
	     * Handler of incoming messages from clients.
	     */
	    static class RouterHandler extends Handler {
	    	final WeakReference<SdlRouterService> provider;

	    	public RouterHandler(SdlRouterService provider){
	    		this.provider = new WeakReference<SdlRouterService>(provider);
	    	}
	    	
	        @Override
	        public void handleMessage(Message msg) {
	        	if(this.provider.get() == null){
	        		return;
	        	}
	        	final Bundle receivedBundle = msg.getData();
	        	Bundle returnBundle;
	        	final SdlRouterService service = this.provider.get();
	        	
	            switch (msg.what) {
	            case TransportConstants.ROUTER_REQUEST_BT_CLIENT_CONNECT:              	
	            	if(receivedBundle.getBoolean(TransportConstants.CONNECT_AS_CLIENT_BOOLEAN_EXTRA, false)
	        				&& !connectAsClient){		//We check this flag to make sure we don't try to connect over and over again. On D/C we should set to false
	        				//Log.d(TAG,"Attempting to connect as bt client");
	        				BluetoothDevice device = receivedBundle.getParcelable(BluetoothDevice.EXTRA_DEVICE);
	        				connectAsClient = true;
	        				if(device==null || !service.bluetoothConnect(device)){
	        					Log.e(TAG, "Unable to connect to bluetooth device");
	        					connectAsClient = false;
	        				}
	        			}
	            	//**************** We don't break here so we can let the app register as well
	                case TransportConstants.ROUTER_REGISTER_CLIENT: //msg.arg1 is appId
	                	//pingClients();
	                	Message message = Message.obtain();
	                	message.what = TransportConstants.ROUTER_REGISTER_CLIENT_RESPONSE;
            			message.arg1 = TransportConstants.REGISTRATION_RESPONSE_SUCESS;
	                	long appId = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1);
	                	if(appId<0 || msg.replyTo == null){
	                		Log.w(TAG, "Unable to register app as no id or messenger was included");
	                		if(msg.replyTo!=null){
	                			message.arg1 = TransportConstants.REGISTRATION_RESPONSE_DENIED_APP_ID_NOT_INCLUDED;
	                			try {
									msg.replyTo.send(message);
								} catch (RemoteException e) {
									e.printStackTrace();
								}
	                		}
	                		break;
	                	}
	                	if(service.legacyModeEnabled){
	                		Log.w(TAG, "Unable to register app as legacy mode is enabled");
	                		if(msg.replyTo!=null){
	                			message.arg1 = TransportConstants.REGISTRATION_RESPONSE_DENIED_LEGACY_MODE_ENABLED;
	                			try {
									msg.replyTo.send(message);
								} catch (RemoteException e) {
									e.printStackTrace();
								}
	                		}
	                		break;
	                	}
	                	
	                	RegisteredApp app = service.new RegisteredApp(appId,msg.replyTo);
	                	synchronized(service.REGISTERED_APPS_LOCK){
	                		RegisteredApp old = registeredApps.put(app.getAppId(), app); 
	                		if(old!=null){
	                			Log.w(TAG, "Replacing already existing app with this app id");
	                			service.removeAllSessionsForApp(old, true);
	                			old.close();
	                		}
	                	}
	                	service.onAppRegistered(app);

	            		returnBundle = new Bundle();
	            		//Add params if connected
	            		if(service.isTransportConnected){
	            			returnBundle.putString(TransportConstants.HARDWARE_CONNECTED, service.connectedTransportType.name());
	                		if(MultiplexBluetoothTransport.currentlyConnectedDevice!=null){
	                			returnBundle.putString(CONNECTED_DEVICE_STRING_EXTRA_NAME, MultiplexBluetoothTransport.currentlyConnectedDevice);
	                		}
	            		}
	            		//Add the version of this router service
	            		returnBundle.putInt(TransportConstants.ROUTER_SERVICE_VERSION, SdlRouterService.ROUTER_SERVICE_VERSION_NUMBER);
	            		
	            		message.setData(returnBundle);
	            		
	            		int result = app.sendMessage(message);
	            		if(result == RegisteredApp.SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT){
	            			synchronized(service.REGISTERED_APPS_LOCK){
	            				registeredApps.remove(appId);
	            			}
	            		}
	                    break;
	                case TransportConstants.ROUTER_UNREGISTER_CLIENT:
	                	long appIdToUnregister = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1);
	                	Log.i(TAG, "Unregistering client: " + appIdToUnregister);
	                	RegisteredApp unregisteredApp = null;
	                	synchronized(service.REGISTERED_APPS_LOCK){
	                		unregisteredApp = registeredApps.remove(appIdToUnregister);
	                	}
	                	Message response = Message.obtain();
	                	response.what = TransportConstants.ROUTER_UNREGISTER_CLIENT_RESPONSE;
	                	if(unregisteredApp == null){
	                		response.arg1 = TransportConstants.UNREGISTRATION_RESPONSE_FAILED_APP_ID_NOT_FOUND;
	                		service.removeAllSessionsWithAppId(appIdToUnregister);
	                	}else{
	                		response.arg1 = TransportConstants.UNREGISTRATION_RESPONSE_SUCESS;
	                		service.removeAllSessionsForApp(unregisteredApp,false);
	                	}
	                	Log.i(TAG, "Unregistering client response: " + response.arg1 );
	                	try {
	                		msg.replyTo.send(response); //We do this because we aren't guaranteed to find the correct registeredApp to send the message through
	                	} catch (RemoteException e) {
	                		e.printStackTrace();
	                		
	                	}catch(NullPointerException e2){
	                		Log.e(TAG, "No reply address included, can't send a reply");
	                	}
	                	
	                    break;
	                case TransportConstants.ROUTER_SEND_PACKET:
	                	Log.d(TAG, "Received packet to send");
	                	if(receivedBundle!=null){
	                		Runnable packetRun = new Runnable(){
	                			@Override
	                			public void run() {
	                				if(receivedBundle!=null){

	                					Long buffAppId = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA);
	                					RegisteredApp buffApp = null;
	                					if(buffAppId!=null){
	                						synchronized(service.REGISTERED_APPS_LOCK){
	                							buffApp = registeredApps.get(buffAppId);
	                						}
	                					}
	                					
	                					if(buffApp !=null){
	                						buffApp.handleIncommingClientMessage(receivedBundle);
	                					}else{
	                						service.writeBytesToTransport(receivedBundle);
	                					}
	                				}
	                			}
	                		};
	                		if(service.packetExecuter!=null){
	                			service.packetExecuter.execute(packetRun); 
	                		}
	                	}
	                    break;
	                case TransportConstants.ROUTER_REQUEST_NEW_SESSION:
	                	long appIdRequesting = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1); Log.i(TAG, "App requesting new session: " + appIdRequesting);
	                	Message extraSessionResponse = Message.obtain();
	                	extraSessionResponse.what = TransportConstants.ROUTER_REQUEST_NEW_SESSION_RESPONSE;
	                	if(appIdRequesting>0){
							synchronized(service.REGISTERED_APPS_LOCK){
								if(registeredApps!=null){
									RegisteredApp appRequesting = registeredApps.get(appIdRequesting);
									if(appRequesting!=null){
										appRequesting.getSessionIds().add((long)-1); //Adding an extra session
										extraSessionResponse.arg1 = TransportConstants.ROUTER_REQUEST_NEW_SESSION_RESPONSE_SUCESS;
									}else{
										extraSessionResponse.arg1 = TransportConstants.ROUTER_REQUEST_NEW_SESSION_RESPONSE_FAILED_APP_NOT_FOUND;
									}
								}
							}		
						}else{
							extraSessionResponse.arg1 = TransportConstants.ROUTER_REQUEST_NEW_SESSION_RESPONSE_FAILED_APP_ID_NOT_INCL;
						}
	                	try {
	                		msg.replyTo.send(extraSessionResponse); //We do this because we aren't guaranteed to find the correct registeredApp to send the message through
	                	} catch (RemoteException e) {
	                		e.printStackTrace();
	                	}catch(NullPointerException e2){
	                		Log.e(TAG, "No reply address included, can't send a reply");
	                	}
	                	break;
	                case  TransportConstants.ROUTER_REMOVE_SESSION:
	                	long appIdWithSession = receivedBundle.getLong(TransportConstants.APP_ID_EXTRA, -1);
	                	long sessionId = receivedBundle.getLong(TransportConstants.SESSION_ID_EXTRA, -1);
	                	service.removeSessionFromMap((int)sessionId);
	                	Message removeSessionResponse = Message.obtain();
	                	removeSessionResponse.what = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE;
	                	if(appIdWithSession>0){
	                		if(sessionId>=0){
	                			synchronized(service.REGISTERED_APPS_LOCK){
	                				if(registeredApps!=null){
	                					RegisteredApp appRequesting = registeredApps.get(appIdWithSession);
	                					if(appRequesting!=null){
	                						if(appRequesting.removeSession(sessionId)){
	                							removeSessionResponse.arg1 = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE_SUCESS;
	                						}else{
	                							removeSessionResponse.arg1 = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE_FAILED_SESSION_NOT_FOUND;
	                						}							
	                					}else{
	                						removeSessionResponse.arg1 = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE_FAILED_APP_NOT_FOUND;
	                					}
	                				}
	                			}		
	                		}else{
							removeSessionResponse.arg1 = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE_FAILED_SESSION_ID_NOT_INCL;
							}
	                	}else{
							removeSessionResponse.arg1 = TransportConstants.ROUTER_REMOVE_SESSION_RESPONSE_FAILED_APP_ID_NOT_INCL;
						}
	                	try {
	                		msg.replyTo.send(removeSessionResponse); //We do this because we aren't guaranteed to find the correct registeredApp to send the message through
	                	} catch (RemoteException e) {
	                		e.printStackTrace();
	                	}catch(NullPointerException e2){
	                		Log.e(TAG, "No reply address included, can't send a reply");
	                	}
	                	break;
	                default:
	                    super.handleMessage(msg);
	            }
	        }
	    }

	    
	    /**
	     * Target we publish for alternative transport (USB) clients to send messages to RouterHandler.
	     */
	    final Messenger altTransportMessenger = new Messenger(new AltTransportHandler(this));
	    
		 /**
	     * Handler of incoming messages from an alternative transport (USB).
	     */
	    static class AltTransportHandler extends Handler {
	    	ClassLoader loader; 
	    	final WeakReference<SdlRouterService> provider;

	    	public AltTransportHandler(SdlRouterService provider){
	    		this.provider = new WeakReference<SdlRouterService>(provider);
	    		loader = getClass().getClassLoader();
	    	}

	        @Override
	        public void handleMessage(Message msg) {
	        	if(this.provider.get() == null){
	        		return;
	        	}
	        	SdlRouterService service = this.provider.get();
	        	Bundle receivedBundle = msg.getData();
	        	switch(msg.what){
	        	case TransportConstants.HARDWARE_CONNECTION_EVENT:
        			if(receivedBundle.containsKey(TransportConstants.HARDWARE_DISCONNECTED)){
        				//We should shut down, so call 
        				if(altTransportService != null 
        						&& altTransportService.equals(msg.replyTo)){
        					//The same transport that was connected to the router service is now telling us it's disconnected. Let's inform clients and clear our saved messenger
        					altTransportService = null;
        					service.onTransportDisconnected(TransportType.valueOf(receivedBundle.getString(TransportConstants.HARDWARE_DISCONNECTED)));
        					service.shouldServiceRemainOpen(null); //this will close the service if bluetooth is not available
        				}
        			}else if(receivedBundle.containsKey(TransportConstants.HARDWARE_CONNECTED)){
    					Message retMsg =  Message.obtain();
    					retMsg.what = TransportConstants.ROUTER_REGISTER_ALT_TRANSPORT_RESPONSE;
        				if(altTransportService == null){ //Ok no other transport is connected, this is good
        					Log.d(TAG, "Alt transport connected.");
        					if(msg.replyTo == null){
        						break;
        					}
        					altTransportService = msg.replyTo;
        					//Clear out the timer to make sure the service knows we're good to go
        					if(service.altTransportTimerHandler!=null && service.altTransportTimerRunnable!=null){
        						service.altTransportTimerHandler.removeCallbacks(service.altTransportTimerRunnable);
        					}
        					service.altTransportTimerHandler = null;
        					service.altTransportTimerRunnable = null;
        					
        					//Let the alt transport know they are good to go
        					retMsg.arg1 = TransportConstants.ROUTER_REGISTER_ALT_TRANSPORT_RESPONSE_SUCESS;
        					service.onTransportConnected(TransportType.valueOf(receivedBundle.getString(TransportConstants.HARDWARE_CONNECTED)));
        				}else{ //There seems to be some other transport connected
        					//Error
        					retMsg.arg1 = TransportConstants.ROUTER_REGISTER_ALT_TRANSPORT_ALREADY_CONNECTED;
        				}
        				if(msg.replyTo!=null){
        					try {msg.replyTo.send(retMsg);} catch (RemoteException e) {e.printStackTrace();}
        				}
        			}
            		break;
	        	case TransportConstants.ROUTER_RECEIVED_PACKET:
	        		if(receivedBundle!=null){
	        			receivedBundle.setClassLoader(loader);//We do this because loading a custom parceable object isn't possible without it
	            	}else{
	            		Log.e(TAG, "Bundle was null while sending packet to router service from alt transport");
	            	}
            		if(receivedBundle.containsKey(TransportConstants.FORMED_PACKET_EXTRA_NAME)){
            			SdlPacket packet = receivedBundle.getParcelable(TransportConstants.FORMED_PACKET_EXTRA_NAME);
    					if(packet!=null){
    						service.onPacketRead(packet);
    					}else{
    						Log.w(TAG, "Received null packet from alt transport service");
    					}
            		}else{
            			Log.w(TAG, "Flase positive packet reception");
            		}
            		break; 
	        	default:
	        		super.handleMessage(msg);
	        	}
	        	
	        }
	    };
	    
	    /**
	     * Target we publish for alternative transport (USB) clients to send messages to RouterHandler.
	     */
	    final Messenger routerStatusMessenger = new Messenger(new RouterStatusHandler(this));
	    
		 /**
	     * Handler of incoming messages from an alternative transport (USB).
	     */
	    static class RouterStatusHandler extends Handler {
	    	 final WeakReference<SdlRouterService> provider;

	    	 public RouterStatusHandler(SdlRouterService provider){
				 this.provider = new WeakReference<SdlRouterService>(provider);
	    	 }

	        @Override
	        public void handleMessage(Message msg) {
	        	if(this.provider.get() == null){
	        		return;
	        	}
	        	SdlRouterService service = this.provider.get();
	        	switch(msg.what){
	        	case TransportConstants.ROUTER_STATUS_CONNECTED_STATE_REQUEST:
        			int flags = msg.arg1;
	        		if(msg.replyTo!=null){
	        			Message message = Message.obtain();
	        			message.what = TransportConstants.ROUTER_STATUS_CONNECTED_STATE_RESPONSE;
	        			message.arg1 = (service.isTransportConnected == true) ? 1 : 0;
	        			try {
	        				msg.replyTo.send(message);
	        			} catch (RemoteException e) {
	        				e.printStackTrace();
	        			}
	        		}
	        		if(service.isTransportConnected && ((TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING  & flags) == TransportConstants.ROUTER_STATUS_FLAG_TRIGGER_PING)){
	        			if(service.pingIntent == null){
	        				service.initPingIntent();
	        			}
	        			service.getBaseContext().sendBroadcast(service.pingIntent); 
	        		}
	        		break;
	        	default:
	        		Log.w(TAG, "Unsopported request: " + msg.what);
	        		break;
	        	}
	        }
	    };
		
/* **************************************************************************************************************************************
***********************************************  Life Cycle **************************************************************
****************************************************************************************************************************************/

	@Override
	public IBinder onBind(Intent intent) {
		//Check intent to send back the correct binder (client binding vs alt transport)
		if(intent!=null){
			if(closing){
				Log.w(TAG, "Denying bind request due to service shutting down.");
				return null;
			}
			String requestType = intent.getAction();//intent.getIntExtra(TransportConstants.ROUTER_BIND_REQUEST_TYPE_EXTRA, TransportConstants.BIND_REQUEST_TYPE_CLIENT);
			if(TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT.equals(requestType)){
				if(0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)){ //Only allow alt transport in debug mode
					return this.altTransportMessenger.getBinder();
				}
			}else if(TransportConstants.BIND_REQUEST_TYPE_CLIENT.equals(requestType)){
				return this.routerMessenger.getBinder();
			}else if(TransportConstants.BIND_REQUEST_TYPE_STATUS.equals(requestType)){
				return this.routerStatusMessenger.getBinder();
			}else{
				Log.w(TAG, "Uknown bind request type");
			}
			
		}
		return null;
	}

	
	
	@Override
	public boolean onUnbind(Intent intent) {
		Log.d(TAG, "Unbind being called.");
		return super.onUnbind(intent);
	}

	
	private void notifyClients(Message message){
		if(message==null){
			Log.w(TAG, "Can't notify clients, message was null");
			return;
		}
		Log.d(TAG, "Notifying "+ registeredApps.size()+ " clients");
		int result;
		synchronized(REGISTERED_APPS_LOCK){
			Collection<RegisteredApp> apps = registeredApps.values();
			Iterator<RegisteredApp> it = apps.iterator();
			while(it.hasNext()){
				RegisteredApp app = it.next();
				result = app.sendMessage(message);
				if(result == RegisteredApp.SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT){
					app.close();
					it.remove();
				}
			}
		}
	}
	
	private void pingClients(){
		Message message = Message.obtain();
		Log.d(TAG, "Pinging "+ registeredApps.size()+ " clients");
		int result;
		synchronized(REGISTERED_APPS_LOCK){
			Collection<RegisteredApp> apps = registeredApps.values();
			Iterator<RegisteredApp> it = apps.iterator();
			while(it.hasNext()){
				RegisteredApp app = it.next();
				result = app.sendMessage(message);
				if(result == RegisteredApp.SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT){
					app.close();
					Vector<Long> sessions = app.getSessionIds();
					for(Long session:sessions){
						if(session !=null && session != -1){
							attemptToCleanUpModule(session.intValue(), cachedModuleVersion);
						}
					}
					it.remove();
				}
			}
		}
	}
	
	/**
	 * We want to make sure we are in the right process here. If there is somesort of developer error 
	 * we want to just close out right away.
	 * @return
	 */
	private boolean processCheck(){
		int myPid = android.os.Process.myPid();
		ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
		for (RunningAppProcessInfo processInfo : am.getRunningAppProcesses())
		{
			if (processInfo.pid == myPid)
			{
				return ROUTER_SERVICE_PROCESS.equals(processInfo.processName);
			}
		}
		return false;

	}
	
	private boolean permissionCheck(String permissionToCheck){
		if(permissionToCheck == null){
			throw new IllegalArgumentException("permission is null");
		}
		return PackageManager.PERMISSION_GRANTED == getBaseContext().checkPermission(permissionToCheck, android.os.Process.myPid(), android.os.Process.myUid());
	}

	/**
	 * Runs several checks to ensure this router service has the correct conditions to run properly 
	 * @return true if this service is set up correctly
	 */
	private boolean initCheck(){
		if(!processCheck()){
			Log.e(TAG, "Not using correct process. Shutting down");
			wrongProcess = true;
			return false;
		}
		if(!permissionCheck(Manifest.permission.BLUETOOTH)){
			Log.e(TAG, "Bluetooth Permission is not granted. Shutting down");
			return false;
		}
		if(!AndroidTools.isServiceExported(this, new ComponentName(this, this.getClass()))){ //We want to check to see if our service is actually exported
			Log.e(TAG, "Service isn't exported. Shutting down");
			return false;
		}
		return true;
	}


	@Override
	public void onCreate() {
		super.onCreate();

		if(!initCheck()){ // Run checks on process and permissions
			stopSelf();
			return;
		}
		initPassed = true;

		synchronized(REGISTERED_APPS_LOCK){
			registeredApps = new HashMap<Long,RegisteredApp>();
		}
		closing = false;
		currentContext = getBaseContext();
		
		startVersionCheck();
		Log.i(TAG, "SDL Router Service has been created");
		
		
		synchronized(SESSION_LOCK){
			this.sessionMap = new SparseArray<Long>();
			this.sessionHashIdMap = new SparseArray<Integer>();
		}
		packetExecuter =  Executors.newSingleThreadExecutor();
	}
	HashMap<String,ResolveInfo> sdlMultiList ;
	public void startVersionCheck(){
		Intent intent = new Intent(START_SERVICE_ACTION);
		List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
		sdlMultiList = new HashMap<String,ResolveInfo>();
		for(ResolveInfo info: infos){
			//Log.d(TAG, "Sdl enabled app: " + info.activityInfo.packageName);
			if(getPackageName().equals(info.activityInfo.applicationInfo.packageName)){
				//Log.d(TAG, "Ignoring my own package");
				continue;
			}
			sdlMultiList.put(info.activityInfo.packageName, info);
		}
		registerReceiver(registerAnInstanceOfSerialServer, new IntentFilter(REGISTER_NEWER_SERVER_INSTANCE_ACTION));
		newestServiceCheck(currentContext);
	}
	
	public void startUpSequence(){
		IntentFilter disconnectFilter = new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED");
		disconnectFilter.addAction("android.bluetooth.device.action.CLASS_CHANGED");
		disconnectFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECTED");
		disconnectFilter.addAction("android.bluetooth.device.action.ACL_DISCONNECT_REQUESTED");
		registerReceiver(mListenForDisconnect,disconnectFilter );
		
		IntentFilter filter = new IntentFilter();
		filter.addAction(REGISTER_WITH_ROUTER_ACTION);
		registerReceiver(mainServiceReceiver,filter);
		
		if(!connectAsClient){
			if(bluetoothAvailable()){
				initBluetoothSerialService();
			}
		}
		
		if(altTransportTimerHandler!=null){
			//There's an alt transport waiting for this service to be started
			Intent intent =  new Intent(TransportConstants.ALT_TRANSPORT_RECEIVER);
			sendBroadcast(intent);
		}
		
		startSequenceComplete= true;
	}


	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		if(!initPassed) {
			return super.onStartCommand(intent, flags, startId);
		}
		if(registeredApps == null){
			synchronized(REGISTERED_APPS_LOCK){
				registeredApps = new HashMap<Long,RegisteredApp>();
			}
		}
		if(intent != null ){
			if(intent.hasExtra(TransportConstants.PING_ROUTER_SERVICE_EXTRA)){
				//Make sure we are listening on RFCOMM
				if(startSequenceComplete){ //We only check if we are sure we are already through the start up process
					Log.i(TAG, "Received ping, making sure we are listening to bluetooth rfcomm");
					initBluetoothSerialService();
				}
			}
		}
		shouldServiceRemainOpen(intent);
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy(){
		stopClientPings();
		if(versionCheckTimeOutHandler!=null){versionCheckTimeOutHandler.removeCallbacks(versionCheckRunable);}
		if(altTransportTimerHandler!=null){
			altTransportTimerHandler.removeCallbacks(versionCheckRunable);
			altTransportTimerHandler = null;
			versionCheckRunable = null;
		}
		Log.w(TAG, "Sdl Router Service Destroyed");
	    closing = true;
		currentContext = null;
		//No need for this Broadcast Receiver anymore
		unregisterAllReceivers();
		closeBluetoothSerialServer();
		if(registeredApps!=null){
			synchronized(REGISTERED_APPS_LOCK){
				registeredApps.clear();
				registeredApps = null;
			}
		}
		synchronized(SESSION_LOCK){
			if(this.sessionMap!=null){ 
				this.sessionMap.clear();
				this.sessionMap = null;
			}
			if(this.sessionHashIdMap!=null){
				this.sessionHashIdMap.clear();
				this.sessionHashIdMap = null;
			}
		}
		
		//SESSION_LOCK = null;
		
		startSequenceComplete=false;
		if(packetExecuter!=null){
			packetExecuter.shutdownNow();
			packetExecuter = null;
		}
		
		exitForeground();
		
		if(packetWriteTaskMaster!=null){
			packetWriteTaskMaster.close();
			packetWriteTaskMaster = null;
		}
		
		super.onDestroy();
		System.gc(); //Lower end phones need this hint
		if(!wrongProcess){
			try{
				android.os.Process.killProcess(android.os.Process.myPid());
			}catch(Exception e){}
		}
	}
	
	private void unregisterAllReceivers(){
		try{
			unregisterReceiver(registerAnInstanceOfSerialServer);		///This should be first. It will always be registered, these others may not be and cause an exception.
			unregisterReceiver(mListenForDisconnect);
			unregisterReceiver(mainServiceReceiver);
		}catch(Exception e){}
	}
	
	private void notifyAltTransportOfClose(int reason){
		if(altTransportService!=null){
			Message msg = Message.obtain();
			msg.what = TransportConstants.ROUTER_SHUTTING_DOWN_NOTIFICATION;
			msg.arg1 = reason;
			try {
				altTransportService.send(msg);
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
	}
	
	@SuppressLint("NewApi")
	@SuppressWarnings("deprecation")
	private void enterForeground() {
		if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB){
			Log.w(TAG, "Unable to start service as foreground due to OS SDK version being lower than 11");
			isForeground = false;
			return;
		}

 
		Bitmap icon;
		int resourcesIncluded = getResources().getIdentifier("ic_sdl", "drawable", getPackageName());

		if ( resourcesIncluded != 0 ) {  //No additional pylons required
			icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_sdl);
		}
		else {  
			icon = BitmapFactory.decodeResource(getResources(), android.R.drawable.stat_sys_data_bluetooth);
		}
       // Bitmap icon = BitmapFactory.decodeByteArray(SdlLogo.SDL_LOGO_STRING, 0, SdlLogo.SDL_LOGO_STRING.length);

        Notification.Builder builder = new Notification.Builder(this);
        if(0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)){ //If we are in debug mode, include what app has the router service open
        	ComponentName name = new ComponentName(this, this.getClass());
        	builder.setContentTitle("SDL: " + name.getPackageName());
        }else{
        	builder.setContentTitle("SmartDeviceLink");
        }
        builder.setTicker("SmartDeviceLink Connected");
        builder.setContentText("Connected to " + this.getConnectedDeviceName());
       
       //We should use icon from library resources if available
        int trayId = getResources().getIdentifier("sdl_tray_icon", "drawable", getPackageName());

		if ( resourcesIncluded != 0 ) {  //No additional pylons required
			 builder.setSmallIcon(trayId);
		}
		else {  
			 builder.setSmallIcon(android.R.drawable.stat_sys_data_bluetooth);
		}
        builder.setLargeIcon(icon);
        builder.setOngoing(true);
        
        Notification notification;
        if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN){
        	notification = builder.getNotification();
        	
        }else{
        	notification = builder.build();
        }
        if(notification == null){
        	Log.e(TAG, "Notification was null");
        }
        startForeground(FOREGROUND_SERVICE_ID, notification);
        isForeground = true;
 
    }
	
	private void exitForeground(){
		if(isForeground){
			this.stopForeground(true);
		}
	}
	
	
	/* **************************************************************************************************************************************
	***********************************************  Helper Methods **************************************************************
	****************************************************************************************************************************************/
	
	public  String getConnectedDeviceName(){
		return connectedDeviceName;
	}
	
	/**
	 * Checks to make sure bluetooth adapter is available and on
	 * @return
	 */
	private boolean bluetoothAvailable(){
		boolean retVal = (!(BluetoothAdapter.getDefaultAdapter()==null) && BluetoothAdapter.getDefaultAdapter().isEnabled());
		//Log.d(TAG, "Bluetooth Available? - " + retVal);
		return retVal;
	}

	/**
	 * 
	 * 1. If the app has SDL shut off, 												shut down
	 * 2. if The app has an Alt Transport address or was started by one, 			stay open
	 * 3. If Bluetooth is off/NA	 												shut down
	 * 4. Anything else					
	 */
	public boolean shouldServiceRemainOpen(Intent intent){
		//Log.d(TAG, "Determining if this service should remain open");
		
		if(altTransportService!=null || altTransportTimerHandler !=null){
			//We have been started by an alt transport, we must remain open. "My life for Auir...."
			Log.d(TAG, "Alt Transport connected, remaining open");
			return true;
			
		}else if(intent!=null && TransportConstants.BIND_REQUEST_TYPE_ALT_TRANSPORT.equals(intent.getAction())){
			Log.i(TAG, "Received start intent with alt transprt request.");
			startAltTransportTimer();
			return true;
		}else if(!bluetoothAvailable()){//If bluetooth isn't on...there's nothing to see here
			//Bluetooth is off, we should shut down
			Log.d(TAG, "Bluetooth not available, shutting down service");
			closeSelf();
			return false;
		}else{
			Log.d(TAG, "Service to remain open");
			return true;
		}
	}
	/**
	 * This method is needed so that apps that choose not to implement this as a service as defined by Android, but rather
	 * just a simple class we have to know how to shut down.
	 */
	public void closeSelf(){
		closing = true;
		if(getBaseContext()!=null){
			stopSelf();
		}else{
			onDestroy();
		}
	}
	private synchronized void initBluetoothSerialService(){
		if(legacyModeEnabled){
			Log.d(TAG, "Not starting own bluetooth during legacy mode");
			return;
		}
		Log.i(TAG, "Iniitializing bluetooth transport");
		//init serial service
		if(mSerialService ==null){
			mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance();
			if(mSerialService==null){
				mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(mHandlerBT);
			}
		}
		if (mSerialService != null) {
            // Only if the state is STATE_NONE, do we know that we haven't started already
            if (mSerialService.getState() == MultiplexBluetoothTransport.STATE_NONE || mSerialService.getState() == MultiplexBluetoothTransport.STATE_ERROR) {
              // Start the Bluetooth services
            	mSerialService.start();
            }

		}
	}
	
	public void onTransportConnected(final TransportType type){
		isTransportConnected = true;
		enterForeground();
		if(packetWriteTaskMaster!=null){
			packetWriteTaskMaster.close();
			packetWriteTaskMaster = null;
		}
		packetWriteTaskMaster = new PacketWriteTaskMaster();
		packetWriteTaskMaster.start();
		
		connectedTransportType = type;
		
		Intent startService = new Intent();  
		startService.setAction(START_SERVICE_ACTION);
		startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_EXTRA, true);
		startService.putExtra(TransportConstants.FORCE_TRANSPORT_CONNECTED, true);
		startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE, getBaseContext().getPackageName());
		startService.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME, new ComponentName(this, this.getClass()));
    	sendBroadcast(startService); 
		//HARDWARE_CONNECTED
    	if(!(registeredApps== null || registeredApps.isEmpty())){
    		//If we have clients
			notifyClients(createHardwareConnectedMessage(type));
    	}
	}
	
	private Message createHardwareConnectedMessage(final TransportType type){
			Message message = Message.obtain();
			message.what = TransportConstants.HARDWARE_CONNECTION_EVENT;
			Bundle bundle = new Bundle();
			bundle.putString(TransportConstants.HARDWARE_CONNECTED, type.name());
    		if(MultiplexBluetoothTransport.currentlyConnectedDevice!=null){
    			bundle.putString(CONNECTED_DEVICE_STRING_EXTRA_NAME, MultiplexBluetoothTransport.currentlyConnectedDevice);
    		}
			message.setData(bundle);
			return message;
		
	}
	
	public void onTransportDisconnected(TransportType type){
		if(altTransportService!=null){  //If we still have an alt transport open, then we don't need to tell the clients to close
			return;
		}
		Log.e(TAG, "Notifying client service of hardware disconnect.");
		connectedTransportType = null;
		isTransportConnected = false;
		stopClientPings();
		
		exitForeground();//Leave our foreground state as we don't have a connection anymore
		
		if(packetWriteTaskMaster!=null){
			packetWriteTaskMaster.close();
			packetWriteTaskMaster = null;
		}
		
		cachedModuleVersion = -1; //Reset our cached version
		if(registeredApps== null || registeredApps.isEmpty()){
			Intent unregisterIntent = new Intent();
			unregisterIntent.putExtra(HARDWARE_DISCONNECTED, type.name());
			unregisterIntent.putExtra(TransportConstants.ENABLE_LEGACY_MODE_EXTRA, legacyModeEnabled);
			unregisterIntent.setAction(TransportConstants.START_ROUTER_SERVICE_ACTION);
			sendBroadcast(unregisterIntent);
			//return;
		}else{
			Message message = Message.obtain();
			message.what = TransportConstants.HARDWARE_CONNECTION_EVENT;
			Bundle bundle = new Bundle();
			bundle.putString(HARDWARE_DISCONNECTED, type.name());
			bundle.putBoolean(TransportConstants.ENABLE_LEGACY_MODE_EXTRA, legacyModeEnabled);
			message.setData(bundle);
			notifyClients(message);
		}
		//We've notified our clients, less clean up the mess now.
		synchronized(SESSION_LOCK){
			this.sessionMap.clear();
			this.sessionHashIdMap.clear();
		}
		synchronized(REGISTERED_APPS_LOCK){
			if(registeredApps==null){
				return;
			}
			registeredApps.clear();
		}
	}
	
	public void onPacketRead(SdlPacket packet){
        try {
    		//Log.i(TAG, "******** Read packet with header: " +(packet).toString());
    		if(packet.getVersion() == 1){
    				if( packet.getFrameType() == FrameType.Control && packet.getFrameInfo() == SdlPacket.FRAME_INFO_START_SERVICE_ACK){
    					//We received a v1 packet from the head unit, this means we can't use the router service.
    					//Enable legacy mode
    					enableLegacyMode(true);
    				return;
    				}
    		}else if(cachedModuleVersion == -1){
    			cachedModuleVersion = packet.getVersion();
    		}
        	//Send the received packet to the registered app
        	sendPacketToRegisteredApp(packet);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	 private final Handler mHandlerBT = new Handler() {
	        @Override
	        public void handleMessage(Message msg) {
	            switch (msg.what) {
	            	case MESSAGE_DEVICE_NAME:
	            		connectedDeviceName = msg.getData().getString(MultiplexBluetoothTransport.DEVICE_NAME);
	            		break;
	            	case MESSAGE_STATE_CHANGE:
	            		switch (msg.arg1) {
	            		case MultiplexBluetoothTransport.STATE_CONNECTED:
	            			onTransportConnected(TransportType.BLUETOOTH);
	            			break;
	            		case MultiplexBluetoothTransport.STATE_CONNECTING:
	            			// Currently attempting to connect - update UI?
	            			break;
	            		case MultiplexBluetoothTransport.STATE_LISTEN:
	            			break;
	            		case MultiplexBluetoothTransport.STATE_NONE:
	            			// We've just lost the connection
	            			if(!connectAsClient ){
	            				if(!legacyModeEnabled && !closing){
	            					initBluetoothSerialService();
	            				}
	            				onTransportDisconnected(TransportType.BLUETOOTH);
	            			}
	            			break;
	            		case MultiplexBluetoothTransport.STATE_ERROR:
	            			if(mSerialService!=null){
	            				Log.d(TAG, "Bluetooth serial server error received, setting state to none, and clearing local copy");
	            				mSerialService.setStateManually(MultiplexBluetoothTransport.STATE_NONE);
	            				mSerialService = null;
	            			}
	            			break;
	            		}
	                break;
	                
	            	case MESSAGE_READ:
	                	onPacketRead((SdlPacket) msg.obj);
	        			break;
	            }
	        }
	    };
		
		@SuppressWarnings("unused") //The return false after the packet null check is not dead code. Read the getByteArray method from bundle
		public boolean writeBytesToTransport(Bundle bundle){
			if(bundle == null){
				return false;
			}
			if(mSerialService !=null && mSerialService.getState()==MultiplexBluetoothTransport.STATE_CONNECTED){
				byte[] packet = bundle.getByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME); 
				int offset = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_OFFSET, 0); //If nothing, start at the begining of the array
				int count = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_COUNT, packet.length);  //In case there isn't anything just send the whole packet.
				if(packet!=null){
					mSerialService.write(packet,offset,count);
					return true;
				}
				return false;
			}else if(sendThroughAltTransport(bundle)){
				return true;
			}
			else{
				Log.e(TAG, "Can't send data, no transport connected");
				return false;
			}	
		}
		
		private boolean manuallyWriteBytes(byte[] bytes, int offset, int count){
			if(mSerialService !=null && mSerialService.getState()==MultiplexBluetoothTransport.STATE_CONNECTED){
				if(bytes!=null){
					mSerialService.write(bytes,offset,count);
					return true;
				}
				return false;
			}else if(sendThroughAltTransport(bytes,offset,count)){
				return true;
			}else{
				return false;
			}
		}
		
		
		/**
		 * This Method will send the packets through the alt transport that is connected
		 * @param array The byte array of data to be wrote out
		 * @return If it was possible to send the packet off.
		 * <p><b>NOTE: This is not guaranteed. It is a best attempt at sending the packet, it may fail.</b>
		 */
		private boolean sendThroughAltTransport(Bundle bundle){
			if(altTransportService!=null){
				Message msg = Message.obtain();
				msg.what = TransportConstants.ROUTER_SEND_PACKET;
				msg.setData(bundle);
				try {
					altTransportService.send(msg);
				} catch (RemoteException e) {
					Log.e(TAG, "Unable to send through alt transport!");
					e.printStackTrace();
				}
				return true;
			}else{
				Log.w(TAG, "Unable to send packet through alt transport, it was null");
			}
			return false;		
		}
		
		 /** This Method will send the packets through the alt transport that is connected
		 * @param array The byte array of data to be wrote out
		 * @return If it was possible to send the packet off.
		 * <p><b>NOTE: This is not guaranteed. It is a best attempt at sending the packet, it may fail.</b>
		 */
		private boolean sendThroughAltTransport(byte[] bytes, int offset, int count){
			if(altTransportService!=null){
				Message msg = Message.obtain();
				msg.what = TransportConstants.ROUTER_SEND_PACKET;
				Bundle bundle = new Bundle();
				bundle.putByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME,bytes); 
				bundle.putInt(TransportConstants.BYTES_TO_SEND_EXTRA_OFFSET, offset); 
				bundle.putInt(TransportConstants.BYTES_TO_SEND_EXTRA_COUNT, count);  
				msg.setData(bundle);
				try {
					altTransportService.send(msg);
				} catch (RemoteException e) {
					Log.e(TAG, "Unable to send through alt transport!");
					e.printStackTrace();
				}
				return true;
			}else{
				Log.w(TAG, "Unable to send packet through alt transport, it was null");
			}
			return false;		
		}
		/**
		 * This will send the received packet to the registered service. It will default to the single registered "foreground" app.
		 * This can be overridden to provide more specific functionality. 
		 * @param packet the packet that is received
		 * @return whether or not the sending was successful 
		 */
		public boolean sendPacketToRegisteredApp(SdlPacket packet) {
			if(registeredApps!=null && (registeredApps.size()>0)){
				int session = packet.getSessionId();
				boolean shouldAssertNewSession = packet.getFrameType() == FrameType.Control && (packet.getFrameInfo() == SdlPacket.FRAME_INFO_START_SERVICE_ACK || packet.getFrameInfo() == SdlPacket.FRAME_INFO_START_SERVICE_NAK);
	    		Long appid = getAppIDForSession(session, shouldAssertNewSession); //Find where this packet should go
	    		if(appid!=null){
	    			RegisteredApp app = null;
	    			synchronized(REGISTERED_APPS_LOCK){
	    				 app = registeredApps.get(appid);
	    			}
	    			if(app==null){Log.e(TAG, "No app found for app id " + appid + " Removing session maping and sending unregisterAI to head unit.");
	    				//We have no app to match the app id tied to this session
	    				removeSessionFromMap(session);
	    				byte[] uai = createForceUnregisterApp((byte)session, (byte)packet.getVersion());
	    				manuallyWriteBytes(uai,0,uai.length);
	    				int hashId = 0;
	    				synchronized(this.SESSION_LOCK){
	    					if(this.sessionHashIdMap.indexOfKey(session)>=0){
	    						hashId = this.sessionHashIdMap.get(session); 
	    						this.sessionHashIdMap.remove(session);
	    					}
	    				}
	    				byte[] stopService = (SdlPacketFactory.createEndSession(SessionType.RPC, (byte)session, 0, (byte)packet.getVersion(),BitConverter.intToByteArray(hashId))).constructPacket();
						manuallyWriteBytes(stopService,0,stopService.length);
	    				return false;
	    			}
	    			byte version = (byte)packet.getVersion();
	    			
	    			if(shouldAssertNewSession && version>1 && packet.getFrameInfo() == SdlPacket.FRAME_INFO_START_SERVICE_ACK){ //we know this was a start session response
	    				if (packet.getPayload() != null && packet.getDataSize() == 4){ //hashid will be 4 bytes in length
	    					synchronized(SESSION_LOCK){
	    						this.sessionHashIdMap.put(session, (BitConverter.intFromByteArray(packet.getPayload(), 0)));
	    					}
	    				}
	    			}

	    			int packetSize = (int) (packet.getDataSize() + SdlPacket.HEADER_SIZE);
	    			//Log.i(TAG, "Checking packet size: " + packetSize);
	    			Message message = Message.obtain();
	    			Bundle bundle = new Bundle();
	    			
	    			if(packetSize < ByteArrayMessageSpliter.MAX_BINDER_SIZE){ //This is a small enough packet just send on through
	    				//Log.w(TAG, " Packet size is just right " + packetSize  + " is smaller than " + ByteArrayMessageSpliter.MAX_BINDER_SIZE + " = " + (packetSize<ByteArrayMessageSpliter.MAX_BINDER_SIZE));
		    			message.what = TransportConstants.ROUTER_RECEIVED_PACKET;
		    			bundle.putParcelable(FORMED_PACKET_EXTRA_NAME, packet);
	    				bundle.putInt(TransportConstants.BYTES_TO_SEND_FLAGS, TransportConstants.BYTES_TO_SEND_FLAG_NONE);
		    			message.setData(bundle);
		    			return sendPacketMessageToClient(app,message, version);
	    			}else{
	    				//Log.w(TAG, "Packet too big for IPC buffer. Breaking apart and then sending to client.");
	    				//We need to churn through the packet payload and send it in chunks
	    				byte[] bytes = packet.getPayload();
	    				SdlPacket copyPacket = new SdlPacket(packet.getVersion(),packet.isEncrypted(),
	    										(int)packet.getFrameType().getValue(),
	    													packet.getServiceType(),packet.getFrameInfo(), session,
	    													(int)packet.getDataSize(),packet.getMessageId(),null);
	    				message.what = TransportConstants.ROUTER_RECEIVED_PACKET;
		    			bundle.putParcelable(FORMED_PACKET_EXTRA_NAME, copyPacket);
		    			bundle.putInt(TransportConstants.BYTES_TO_SEND_FLAGS, TransportConstants.BYTES_TO_SEND_FLAG_SDL_PACKET_INCLUDED);
		    			message.setData(bundle);
		    			//Log.d(TAG, "First packet before sending: " + message.getData().toString());
		    			if(!sendPacketMessageToClient(app, message, version)){
		    				Log.w(TAG, "Error sending first message of split packet to client " + app.appId);
		    				return false;
		    			}
	    				//Log.w(TAG, "Message too big for single IPC transaction. Breaking apart. Size - " +  packet.getDataSize());
	    				ByteArrayMessageSpliter splitter = new ByteArrayMessageSpliter(appid,TransportConstants.ROUTER_RECEIVED_PACKET,bytes,0);				
	    				while(splitter.isActive()){
	    					if(!sendPacketMessageToClient(app,splitter.nextMessage(),version)){
	    						Log.w(TAG, "Error sending first message of split packet to client " + app.appId);
	    						splitter.close();
	    						return false;
	    					}
	    				}
	    				//Log.i(TAG, "Large packet finished being sent");
	    			} 
	    			
    		}else{	//If we can't find a session for this packet we just drop the packet
	    			Log.e(TAG, "App Id was NULL for session!");
	    			if(removeSessionFromMap(session)){ //If we found the session id still tied to an app in our map we need to remove it and send the proper shutdown sequence.
	    				Log.i(TAG, "Removed session from map.  Sending unregister request to module.");
	    				attemptToCleanUpModule(session, packet.getVersion());
	    			}else{ //There was no mapping so let's try to resolve this
	    				
	    				if(packet.getFrameType() == FrameType.Single && packet.getServiceType() == SdlPacket.SERVICE_TYPE_RPC){
	    					BinaryFrameHeader binFrameHeader = BinaryFrameHeader.parseBinaryHeader(packet.getPayload());
    		    			if(binFrameHeader!=null && FunctionID.UNREGISTER_APP_INTERFACE.getId() == binFrameHeader.getFunctionID()){
    		    				Log.d(TAG, "Received an unregister app interface with no where to send it, dropping the packet.");
    		    			}else{
    		    				attemptToCleanUpModule(session, packet.getVersion());
    		    			}
    					}else if((packet.getFrameType() == FrameType.Control 
	    						&& (packet.getFrameInfo() == SdlPacket.FRAME_INFO_END_SERVICE_ACK || packet.getFrameInfo() == SdlPacket.FRAME_INFO_END_SERVICE_NAK))){
    						//We want to ignore this
    						Log.d(TAG, "Received a stop service ack/nak with no where to send it, dropping the packet.");
    					}else{
    						attemptToCleanUpModule(session, packet.getVersion());
    					}
	    			}
	    		}
	    	}
	    	return false;
		}
	    
		/**
		 * This method is an all else fails situation. If the head unit is out of synch with the apps on the phone
		 * this method will clear out an unwanted or out of date session.
		 * @param session
		 * @param version
		 */
		private void attemptToCleanUpModule(int session, int version){
			Log.i(TAG, "Attempting to stop session " + session);
			byte[] uai = createForceUnregisterApp((byte)session, (byte)version);
			manuallyWriteBytes(uai,0,uai.length);
			int hashId = 0;
			synchronized(this.SESSION_LOCK){
				if(this.sessionHashIdMap.indexOfKey(session)>=0){
					hashId = this.sessionHashIdMap.get(session); 
					this.sessionHashIdMap.remove(session);
				}
			}
			byte[] stopService = (SdlPacketFactory.createEndSession(SessionType.RPC, (byte)session, 0, (byte)version,BitConverter.intToByteArray(hashId))).constructPacket();
			manuallyWriteBytes(stopService,0,stopService.length);
		}
		
	    private boolean sendPacketMessageToClient(RegisteredApp app, Message message, byte version){
			int result = app.sendMessage(message);
			if(result == RegisteredApp.SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT){
				Log.d(TAG, "Dead object, removing app and sessions");
				//Get all their sessions and send out unregister info
				//Use the version in this packet as a best guess
				app.close();
				Vector<Long> sessions = app.getSessionIds();
				byte[]  unregister,stopService;
				int size = sessions.size(), sessionId;
				for(int i=0; i<size;i++){
					sessionId = sessions.get(i).intValue();
					unregister = createForceUnregisterApp((byte)sessionId,version);
					manuallyWriteBytes(unregister,0,unregister.length);
					int hashId = 0;
					synchronized(this.SESSION_LOCK){
						if(this.sessionHashIdMap.indexOfKey(sessionId)>=0){
							hashId = this.sessionHashIdMap.get(sessionId); 
						}
					}
					stopService = (SdlPacketFactory.createEndSession(SessionType.RPC, (byte)sessionId, 0, version,BitConverter.intToByteArray(hashId))).constructPacket();
					
					manuallyWriteBytes(stopService,0,stopService.length);
					synchronized(SESSION_LOCK){
						this.sessionMap.remove(sessionId);
						this.sessionHashIdMap.remove(sessionId);
					}
				}
				synchronized(REGISTERED_APPS_LOCK){
					registeredApps.remove(app.appId);
				}
				return false;//We did our best to correct errors
			}
			return true;//We should have sent our packet, so we can return true now
	    }

		private synchronized void closeBluetoothSerialServer(){
			if(mSerialService != null){
				mSerialService.stop();
				mSerialService = null;
			}
		}
		
		 /**
	     * bluetoothQuerryAndConnect()
	     * This function looks through the phones currently paired bluetooth devices
	     * If one of the devices' names contain "sync", or livio it will attempt to connect the RFCOMM
	     * And start SDL
	     * @return a boolean if a connection was attempted
	     */
		public synchronized boolean bluetoothQuerryAndConnect(){
			if( BluetoothAdapter.getDefaultAdapter().isEnabled()){
				Set<BluetoothDevice> pairedBT= BluetoothAdapter.getDefaultAdapter().getBondedDevices();
	        	Log.d(TAG, "Querry Bluetooth paired devices");
	        	if (pairedBT.size() > 0) {
	            	for (BluetoothDevice device : pairedBT) {
	            		if(device.getName().toLowerCase(Locale.US).contains("sync") 
	            				|| device.getName().toLowerCase(Locale.US).contains("livio")){
	            			bluetoothConnect(device);
	            				  return true;
	                	}
	            	}
	       		}
			}
			else{
				Log.e(TAG, "There was an issue with connecting as client");
			}
			return false;
			}
		
		private synchronized boolean bluetoothConnect(BluetoothDevice device){
    		Log.d(TAG,"Connecting to device: " + device.getName().toString());
			if(mSerialService == null || !mSerialService.isConnected())
			{	// Set up the Bluetooth serial object				
				mSerialService = MultiplexBluetoothTransport.getBluetoothSerialServerInstance(mHandlerBT);
			}
			// We've been given a device - let's connect to it
			if(mSerialService.getState()!=MultiplexBluetoothTransport.STATE_CONNECTING){//mSerialService.stop();
				mSerialService.connect(device);
				if(mSerialService.getState() == MultiplexBluetoothTransport.STATE_CONNECTING){
					return true;
				}
			}

			Log.d(TAG, "Bluetooth SPP Connect Attempt Completed");
			return false;
		}

		
		//**************************************************************************************************************************************
		//********************************************************* PREFERENCES ****************************************************************
		//**************************************************************************************************************************************
		/**
		 * This method will set the last known bluetooth connection method that worked with this phone.
		 * This helps speed up the process of connecting
		 * @param level The level of bluetooth connecting method that last worked
		 * @param prefLocation Where the preference should be stored
		 */
		public final static void setBluetoothPrefs (int level, String prefLocation) {
			if(currentContext==null){
				return;
			}
			SharedPreferences mBluetoothPrefs = currentContext.getSharedPreferences(prefLocation, Context.MODE_PRIVATE);
	    	// Write the new prefs
	    	SharedPreferences.Editor prefAdd = mBluetoothPrefs.edit();
	    	prefAdd.putInt("level", level);
	    	prefAdd.commit();
		}
		
		public final static int getBluetoothPrefs(String prefLocation)
		{		
			if(currentContext==null){
				return 0;
			}
			SharedPreferences mBluetoothPrefs = currentContext.getSharedPreferences(prefLocation, Context.MODE_PRIVATE);
			return mBluetoothPrefs.getInt("level", 0);
		}
	
	/* ***********************************************************************************************************************************************************************
	 * *****************************************************************  CUSTOM ADDITIONS  ************************************************************************************
	 *************************************************************************************************************************************************************************/

	private LocalRouterService getLocalBluetoothServiceComapre(){
		return this.localCompareTo;
	}
	
	protected static LocalRouterService getLocalRouterService(Intent launchIntent, ComponentName name){
		if(SdlRouterService.selfRouterService == null){
			if(launchIntent == null){
				Log.w(TAG, "Supplied intent was null, local router service will not contain intent");
				//Log.e(TAG, "Unable to create local router service instance. Supplied intent was null");
				//return null;
			}
			if(name == null){
				Log.e(TAG, "Unable to create local router service object because component name was null");
				return null;
			}
			selfRouterService = new LocalRouterService(launchIntent,ROUTER_SERVICE_VERSION_NUMBER, System.currentTimeMillis(), name);
		}
		if(launchIntent!=null){
			//Assume we want to set this in our service
			//Log.d(TAG, "Setting new intent on our local router service object");
			selfRouterService.launchIntent = launchIntent;
		}
		return selfRouterService;
	}
	
	private  LocalRouterService getLocalRouterService(){
		//return getLocalRouterService(new Intent(getBaseContext(),SdlRouterService.class));
		return getLocalRouterService(null, new ComponentName(this, this.getClass()));
	}
	/**
	 * This method is used to check for the newest version of this class to make sure the latest and greatest is up and running.
	 * @param context
	 */
	private void newestServiceCheck(final Context context){
		getLocalRouterService(); //Make sure our timestamp is set
		versionCheckTimeOutHandler = new Handler(); 
		versionCheckRunable = new Runnable() {           
            public void run() {
            	Log.i(TAG, "Starting up Version Checking ");
            	
            	LocalRouterService newestServiceReceived = getLocalBluetoothServiceComapre();
            	LocalRouterService self = getLocalRouterService(); //We can send in null here, because it should have already been created
            	//Log.v(TAG, "Self service info " + self);
            	//Log.v(TAG, "Newest compare to service info " + newestServiceReceived);
            	if(newestServiceReceived!=null && self.isNewer(newestServiceReceived)){
            		Log.d(TAG, "There is a newer version "+newestServiceReceived.version+" of the Router Service, starting it up");
                	closing = true;
					closeBluetoothSerialServer();
					Intent serviceIntent = newestServiceReceived.launchIntent;
					if(getLastReceivedStartIntent()!=null){
						serviceIntent.putExtras(getLastReceivedStartIntent());
					}
					if(newestServiceReceived.launchIntent == null){
						Log.e(TAG, "Service didn't include launch intent");
						startUpSequence();
						return;
					}
					context.startService(newestServiceReceived.launchIntent);
					notifyAltTransportOfClose(TransportConstants.ROUTER_SHUTTING_DOWN_REASON_NEWER_SERVICE);
					if(getBaseContext()!=null){
						stopSelf();
					}else{
						onDestroy();
					}
            	}
            	else{			//Let's start up like normal
            		Log.d(TAG, "No newer services than " + ROUTER_SERVICE_VERSION_NUMBER +" found. Starting up bluetooth transport");
                	startUpSequence();
            	}
            }
        };
        versionCheckTimeOutHandler.postDelayed(versionCheckRunable, VERSION_TIMEOUT_RUNNABLE); 
	}
	
	/**
	 * This method is used to check for the newest version of this class to make sure the latest and greatest is up and running.
	 * @param context
	 */
	private void startAltTransportTimer(){
		altTransportTimerHandler = new Handler(); 
		altTransportTimerRunnable = new Runnable() {           
            public void run() {
            	altTransportTimerHandler = null;
            	altTransportTimerRunnable = null;
            	shouldServiceRemainOpen(null);
            }
        };
        altTransportTimerHandler.postDelayed(altTransportTimerRunnable, ALT_TRANSPORT_TIMEOUT_RUNNABLE); 
	}
	
	private Intent getLastReceivedStartIntent(){	
		return lastReceivedStartIntent;
	}

	/**
	 * Removes session from map if the key is found.
	 * @param sessionId
	 * @return if the key was found
	 */
	private boolean removeSessionFromMap(int sessionId){
		synchronized(SESSION_LOCK){
			if(sessionMap!=null){
				if(sessionMap.indexOfKey(sessionId)>=0){
					sessionMap.remove(sessionId);
					return true;
				}
			}
			return false;
		}
	}
	
	@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
	private boolean removeAllSessionsWithAppId(long appId){
		synchronized(SESSION_LOCK){
			if(sessionMap!=null){
				SparseArray<Long> iter = sessionMap.clone();
				int size = iter.size();
				for(int i = 0; i<size; i++){
					//Log.d(TAG, "Investigating session " +iter.keyAt(i));
					//Log.d(TAG, "App id is: " + iter.valueAt(i));
					if(((Long)iter.valueAt(i)).compareTo(appId) == 0){
						sessionHashIdMap.remove(iter.keyAt(i));
						sessionMap.removeAt(i);	
					}
				}
			}
		}
		return false;
	}
	
	/**
	 * Removes all sessions from the sessions map for this given app id
	 * @param app
	 */
    private void removeAllSessionsForApp(RegisteredApp app, boolean cleanModule){
    	Vector<Long> sessions = app.getSessionIds();
		int size = sessions.size(), sessionId;
		for(int i=0; i<size;i++){
			//Log.d(TAG, "Investigating session " +sessions.get(i).intValue());
			//Log.d(TAG, "App id is: " + sessionMap.get(sessions.get(i).intValue()));
			sessionId = sessions.get(i).intValue();
			removeSessionFromMap(sessionId);
			if(cleanModule){
				attemptToCleanUpModule(sessionId, cachedModuleVersion);
			}
		}
    }
    
    private boolean removeAppFromMap(RegisteredApp app){
    	synchronized(REGISTERED_APPS_LOCK){
    		RegisteredApp old = registeredApps.remove(app); 
    		if(old!=null){
    			old.close();
    			return true;
    		}
    	}
    	return false;
    }
	
	private Long getAppIDForSession(int sessionId, boolean shouldAssertNewSession){
		synchronized(SESSION_LOCK){
			//Log.d(TAG, "Looking for session: " + sessionId);
			if(sessionMap == null){ 
				Log.w(TAG, "Session map was null during look up. Creating one on the fly");
				sessionMap = new SparseArray<Long>(); //THIS SHOULD NEVER BE NULL! WHY IS THIS HAPPENING?!?!?!
			}
			Long appId = sessionMap.get(sessionId);// SdlRouterService.this.sessionMap.get(sessionId);
			if(appId==null && shouldAssertNewSession){
				int pos;
				synchronized(REGISTERED_APPS_LOCK){
					for (RegisteredApp app : registeredApps.values()) {
						pos = app.containsSessionId(-1); 
						if(pos != -1){
							app.setSessionId(pos,sessionId);
							appId = app.getAppId();
							sessionMap.put(sessionId, appId);
							break;
						}
					}
				}
			}
			//Log.d(TAG, sessionId + " session returning App Id: " + appId);
			return appId;
		}
	}
	
	/* ****************************************************************************************************************************************
	// ***********************************************************   LEGACY   ****************************************************************
	//*****************************************************************************************************************************************/
	private boolean legacyModeEnabled = false;
	
	private void enableLegacyMode(boolean enable){
		Log.d(TAG, "Enable legacy mode: " + enable);
		legacyModeEnabled = enable; //We put this at the end to avoid a race condition between the bluetooth d/c and notify of legacy mode enabled

		if(legacyModeEnabled){
			//So we need to let the clients know they need to host their own bluetooth sessions because the currently connected head unit only supports a very old version of SDL/Applink
			//Start by closing our own bluetooth connection. The following calls will handle actually notifying the clients of legacy mode
			closeBluetoothSerialServer();			
			//Now wait until we get a d/c, then the apps should shut their bluetooth down and go back to normal
			
		}//else{}

	}
	
	/* ****************************************************************************************************************************************
	// ***********************************************************   UTILITY   ****************************************************************
	//*****************************************************************************************************************************************/
	
	@SuppressWarnings("unused")
	private void debugPacket(byte[] bytes){
		//DEBUG
		
		if(bytes[0] != 0x00){
			Log.d(TAG, "Writing packet with header: " + BitConverter.bytesToHex(bytes, 12)); //just want the header
		}else{
			
			//Log.d(TAG, "Writing packet with binary header: " + BitConverter.bytesToHex(bytes, 12)); //just want the header
		//int length = bytes.length-12;
		if(bytes.length<=8){
			Log.w(TAG, "No payload to debug or too small");
			return;
		}
		//Check first byte if 0, make to json
		char[] buffer = new char[bytes.length];
		for(int i = 12;i<bytes.length;i++){
			 buffer[i-12] = (char)(bytes[i] & 0xFF);
		}
		try {
			
			JSONObject object = new JSONObject(new String(buffer));
			Log.d(TAG, "JSON: " + object.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
		
	}
	
	/**
	 * If an app crashes the only way we can handle it being on the head unit is to send an unregister app interface rpc.
	 * This method should only be called when the router service recognizes the client is no longer valid
	 * @param sessionId
	 * @param version
	 * @return
	 */
	private byte[] createForceUnregisterApp(byte sessionId,byte version){
		UnregisterAppInterface request = new UnregisterAppInterface();
		request.setCorrelationID(UNREGISTER_APP_INTERFACE_CORRELATION_ID);
		byte[] msgBytes = JsonRPCMarshaller.marshall(request, version);
		ProtocolMessage pm = new ProtocolMessage();
		pm.setData(msgBytes);
		pm.setSessionID(sessionId);
		pm.setMessageType(MessageType.RPC);
		pm.setSessionType(SessionType.RPC);
		pm.setFunctionID(FunctionID.getFunctionId(request.getFunctionName()));
		pm.setCorrID(request.getCorrelationID());
		if (request.getBulkData() != null) 
			pm.setBulkData(request.getBulkData());
		byte[] data = null;
		if(version > 1) { //If greater than v1 we need to include a binary frame header in the data before all the JSON starts
			data = new byte[12 + pm.getJsonSize()];
			BinaryFrameHeader binFrameHeader = new BinaryFrameHeader();
			binFrameHeader = SdlPacketFactory.createBinaryFrameHeader(pm.getRPCType(), pm.getFunctionID(), pm.getCorrID(), pm.getJsonSize());
			System.arraycopy(binFrameHeader.assembleHeaderBytes(), 0, data, 0, 12);
			System.arraycopy(pm.getData(), 0, data, 12, pm.getJsonSize());
		}else {
			data = pm.getData();
		}

		SdlPacket packet = new SdlPacket(version,false,SdlPacket.FRAME_TYPE_SINGLE,SdlPacket.SERVICE_TYPE_RPC,0,sessionId,data.length,data.length+100,data);
		return packet.constructPacket();
	}
	
	
    /**
     * Method for finding the next, highest priority write task from all connected apps.
     * @return
     */
	protected PacketWriteTask getNextTask(){
		final long currentTime = System.currentTimeMillis();
		RegisteredApp priorityApp = null;
		long currentPriority = -Long.MAX_VALUE, peekWeight;
		synchronized(REGISTERED_APPS_LOCK){
			PacketWriteTask peekTask = null;
			for (RegisteredApp app : registeredApps.values()) {
				peekTask = app.peekNextTask();
				if(peekTask!=null){
					peekWeight = peekTask.getWeight(currentTime);
					//Log.v(TAG, "App " + app.appId +" has a task with weight "+ peekWeight);
					if(peekWeight>currentPriority){
						if(app.queuePaused){
							app.notIt();//Reset the timer
							continue;
						}
						if(priorityApp!=null){
							priorityApp.notIt();
						}
						currentPriority = peekWeight;
						priorityApp = app;
					}
				}
			}
			if(priorityApp!=null){
				return priorityApp.getNextTask();
			}
		}
		return null;
	}
	
	private void initPingIntent(){
		pingIntent = new Intent();  
		pingIntent.setAction(START_SERVICE_ACTION);
		pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_EXTRA, true);
		pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_APP_PACKAGE, getBaseContext().getPackageName());
		pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_CMP_NAME, new ComponentName(SdlRouterService.this, SdlRouterService.this.getClass()));
		pingIntent.putExtra(TransportConstants.START_ROUTER_SERVICE_SDL_ENABLED_PING, true);
	}
	
	private void startClientPings(){
		synchronized(this){
			if(!isTransportConnected){ //If we aren't connected, bail
				return;
			}
		if(isPingingClients){
			Log.w(TAG, "Already pinging clients. Resting count");
			synchronized(PING_COUNT_LOCK){
				pingCount = 0;
			}
			return;
		}
		if(clientPingExecutor == null){
			clientPingExecutor = Executors.newSingleThreadScheduledExecutor();
		}
		isPingingClients = true;
		synchronized(PING_COUNT_LOCK){
			pingCount = 0;
		}
		clientPingExecutor.scheduleAtFixedRate(new Runnable(){
			
			@Override
			public void run() {
				if(getPingCount()>=10){
					Log.d(TAG, "Hit ping limit");
					stopClientPings();
					return;
				}
				if(pingIntent == null){
					initPingIntent();
				}
				getBaseContext().sendBroadcast(pingIntent); 
				synchronized(PING_COUNT_LOCK){
					pingCount++;
				}
				
			}
		}, CLIENT_PING_DELAY, CLIENT_PING_DELAY, TimeUnit.MILLISECONDS); //Give a little delay for first call
		}
	}
	
	private int getPingCount(){
		synchronized(PING_COUNT_LOCK){
			return pingCount;
		}
	}
	
	private void stopClientPings(){
		if(clientPingExecutor!=null && !clientPingExecutor.isShutdown()){
			clientPingExecutor.shutdownNow();
			clientPingExecutor = null;
			isPingingClients = false;
		}
		pingIntent = null;
	}
	
	/* ****************************************************************************************************************************************
	// **********************************************************   TINY CLASSES   ************************************************************
	//*****************************************************************************************************************************************/

	/**
	 *This class enables us to compare two router services
	 * from different apps and determine which is the newest
	 * and therefore which one should be the one spun up.
	 * @author Joey Grover
	 *
	 */
	static class LocalRouterService implements Parcelable{
		Intent launchIntent = null;
		int version = 0;
		long timestamp;
		ComponentName name;
		
		private LocalRouterService(Intent intent, int version, long timeStamp,ComponentName name ){
			this.launchIntent = intent;
			this.version = version;
			this.timestamp = timeStamp;
			this.name = name;
		}
		/**
		 * Check if input is newer than this version
		 * @param service
		 * @return
		 */
		public boolean isNewer(LocalRouterService service){
			if(service.version>this.version){
				return true;
			}else if(service.version == this.version){ //If we have the same version, we will use a timestamp
				return service.timestamp<this.timestamp;
			}
			return false;
		}
		
		
		public boolean isEqual(LocalRouterService service) {
			if(service != null && service.name!= null 
					&& this.name !=null){
				return (this.name.equals(service.name));
			}
			return false;
		}
		@Override
		public String toString() {
			StringBuilder build = new StringBuilder();
			build.append("Intent action: ");
			if(launchIntent!=null){
				build.append(launchIntent.getComponent().getClassName());
			}else if(name!=null){
				build.append(name.getClassName());
			}
			
			build.append(" Version: ");
			build.append(version);
			build.append(" Timestamp: ");
			build.append(timestamp);
			
			return build.toString();
		}
		public LocalRouterService(Parcel p) {
			this.version = p.readInt();
			this.timestamp = p.readLong();
			this.launchIntent = p.readParcelable(Intent.class.getClassLoader());
			this.name = p.readParcelable(ComponentName.class.getClassLoader());
		}
		
		@Override
		public int describeContents() {
			return 0;
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeInt(version);
			dest.writeLong(timestamp);
			dest.writeParcelable(launchIntent, 0);
			dest.writeParcelable(name, 0);

		}
		
		public static final Parcelable.Creator<LocalRouterService> CREATOR = new Parcelable.Creator<LocalRouterService>() {
	        public LocalRouterService createFromParcel(Parcel in) {
	     	   return new LocalRouterService(in); 
	        }

			@Override
			public LocalRouterService[] newArray(int size) {
				return new LocalRouterService[size];
			}

	    };
		
	}
	
	
	/**
	 * This class helps keep track of all the different sessions established with the head unit
	 * and to which app they belong to.
	 * @author Joey Grover
	 *
	 */
	class RegisteredApp {
		protected static final int SEND_MESSAGE_SUCCESS 							= 0x00;
		protected static final int SEND_MESSAGE_ERROR_MESSAGE_NULL 					= 0x01;
		protected static final int SEND_MESSAGE_ERROR_MESSENGER_NULL 				= 0x02;
		protected static final int SEND_MESSAGE_ERROR_MESSENGER_GENERIC_EXCEPTION 	= 0x03;
		protected static final int SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT 		= 0x04;
		
		protected static final int PAUSE_TIME_FOR_QUEUE 							= 1500;
		
		long appId;
		Messenger messenger;
		Vector<Long> sessionIds;
		ByteAraryMessageAssembler buffer;
		int prioirtyForBuffingMessage;
		DeathRecipient deathNote = null;
		//Packey queue vars
		PacketWriteTaskBlockingQueue queue; 
		Handler queueWaitHandler= null;
		Runnable queueWaitRunnable = null;
		boolean queuePaused = false;
		
		/**
		 * This is a simple class to hold onto a reference of a registered app.
		 * @param appId
		 * @param messenger
		 */
		public RegisteredApp(long appId, Messenger messenger){			
			this.appId = appId;
			this.messenger = messenger;
			this.sessionIds = new Vector<Long>();
			this.queue = new PacketWriteTaskBlockingQueue();
			queueWaitHandler = new Handler();
			setDeathNote();
		}
		
		/**
		 * Closes this app properly. 
		 */
		public void close(){
			clearDeathNote();
			clearBuffer();
			if(this.queue!=null){
				this.queue.clear();
				queue = null;
			}
			if(queueWaitHandler!=null){
				if(queueWaitRunnable!=null){
					queueWaitHandler.removeCallbacks(queueWaitRunnable);
				}
				queueWaitHandler = null;
			}
		}
		
		public long getAppId() {
			return appId;
		}

		/**
		 * This is a convenience variable and may not be used or useful in different protocols
		 * @return
		 */
		public Vector<Long> getSessionIds() {
			return sessionIds;
		}
		
		/**
		 * Returns the position of the desired object if it is contained in the vector. If not it will return -1.
		 * @param id
		 * @return
		 */
		public int containsSessionId(long id){
			return sessionIds.indexOf(id);
		}
		/**
		 * This will remove a session from the session id list
		 * @param sessionId
		 * @return
		 */
		public boolean removeSession(Long sessionId){
			int location = sessionIds.indexOf(sessionId);
			if(location>=0){
				Long removedSessionId = sessionIds.remove(location);
				if(removedSessionId!=null){
					return true;
				}else{
					return false;
				}
			}else{
				return false;
			}
		}
		/**
		 * @param sessionId
		 */
		public void setSessionId(int position,long sessionId) throws ArrayIndexOutOfBoundsException {
			this.sessionIds.set(position, (long)sessionId); 
		}
		
		public void clearSessionIds(){
			this.sessionIds.clear();
		}
		
		public boolean handleIncommingClientMessage(final Bundle receivedBundle){
			int flags = receivedBundle.getInt(TransportConstants.BYTES_TO_SEND_FLAGS, TransportConstants.BYTES_TO_SEND_FLAG_NONE);
			//Log.d(TAG, "Flags received: " + flags);
			if(flags!=TransportConstants.BYTES_TO_SEND_FLAG_NONE){
				byte[] packet = receivedBundle.getByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME); 
				if(flags == TransportConstants.BYTES_TO_SEND_FLAG_LARGE_PACKET_START){
					this.prioirtyForBuffingMessage = receivedBundle.getInt(TransportConstants.PACKET_PRIORITY_COEFFICIENT,0);
				}
				handleMessage(flags, packet);
			}else{
				//Add the write task on the stack
				if(queue!=null){
					queue.add(new PacketWriteTask(receivedBundle));
					if(packetWriteTaskMaster!=null){
						packetWriteTaskMaster.alert();
					}
				}
			}
			return true;
		}

		public int sendMessage(Message message){
			if(this.messenger == null){return SEND_MESSAGE_ERROR_MESSENGER_NULL;}
			if(message == null){return SEND_MESSAGE_ERROR_MESSAGE_NULL;}
			try {
				this.messenger.send(message);
				return SEND_MESSAGE_SUCCESS;
			} catch (RemoteException e) {
				e.printStackTrace();
				if(e instanceof DeadObjectException){
					return SEND_MESSAGE_ERROR_MESSENGER_DEAD_OBJECT;
				}else{
					return SEND_MESSAGE_ERROR_MESSENGER_GENERIC_EXCEPTION;
				}
			}
		}
		
		public void handleMessage(int flags, byte[] packet){
			if(flags == TransportConstants.BYTES_TO_SEND_FLAG_LARGE_PACKET_START){
				clearBuffer();
				buffer = new ByteAraryMessageAssembler();
				buffer.init();
			}
			if(buffer == null){
				Log.e(TAG, "Unable to assemble message as buffer was null/not started");
			}
			if(!buffer.handleMessage(flags, packet)){ //If this returns false
				Log.e(TAG, "Error handling bytes");
			}
			if(buffer.isFinished()){ //We are finished building the buffer so we should write the bytes out
				byte[] bytes = buffer.getBytes();
				if(queue!=null){
					queue.add(new PacketWriteTask(bytes, 0, bytes.length,this.prioirtyForBuffingMessage));
					if(packetWriteTaskMaster!=null){
						packetWriteTaskMaster.alert();
					}
				}
				buffer.close();
			}
		}
		
		protected PacketWriteTask peekNextTask(){
			if(queue !=null){
				return queue.peek();
			}
			return null;
		}
		
		protected PacketWriteTask getNextTask(){
			if(queue !=null){
				return queue.poll();
			}
			return null;
		}

		/**
		 * This will inform the local app object that it was not picked to have the highest priority. This will allow the user to continue to perform interactions
		 * with the module and not be bogged down by large packet requests. 
		 */
		protected void notIt(){
			if(queue!=null && queue.peek().priorityCoefficient>0){ //If this has any sort of priority coefficient we want to make it wait. 
				//Flag to wait
				if(queueWaitHandler == null){
					Log.e(TAG, "Unable to pause queue, handler was null");
				}
				if(queueWaitRunnable == null){
					queueWaitRunnable = new Runnable(){

						@Override
						public void run() {
							pauseQueue(false);
							if(packetWriteTaskMaster!=null){
								packetWriteTaskMaster.alert();
							}
							
						}
						
					};
				}
				if(queuePaused){
					queueWaitHandler.removeCallbacks(queueWaitRunnable);
				}
				pauseQueue(queueWaitHandler.postDelayed(queueWaitRunnable, PAUSE_TIME_FOR_QUEUE));
			}
		}
		private void pauseQueue(boolean paused){
			this.queuePaused = paused;
		}
		protected void clearBuffer(){
			if(buffer!=null){
				buffer.close();
				buffer = null;
			}
		}
		
		protected boolean setDeathNote(){
			if(messenger!=null){
				if(deathNote == null){
					deathNote = new DeathRecipient(){
						final Object deathLock = new Object();
						@Override
						public void binderDied() {
							synchronized(deathLock){
								Log.w(TAG, "Binder died for app " + RegisteredApp.this.appId);
								if(messenger!=null && messenger.getBinder()!=null){
									messenger.getBinder().unlinkToDeath(this, 0);
								}
								removeAllSessionsForApp(RegisteredApp.this,true);
								removeAppFromMap(RegisteredApp.this);
								startClientPings();
							}
						}
					};
				}
				try {
					messenger.getBinder().linkToDeath(deathNote, 0);
					return true;
				} catch (RemoteException e) {
					e.printStackTrace();
					return false;
				}
			}
			return false;
		}
		
		protected boolean clearDeathNote(){
			if(messenger!=null && messenger.getBinder()!=null && deathNote!=null){
				return messenger.getBinder().unlinkToDeath(deathNote, 0);
			}
			return false;
		}
	}
	
	/**
	 * A runnable task for writing out packets.
	 * @author Joey Grover
	 *
	 */
	public class PacketWriteTask implements Runnable{
		private static final long DELAY_CONSTANT = 500; //250ms
		private static final long SIZE_CONSTANT = 1000; //1kb
		private static final long PRIORITY_COEF_CONSTATNT = 500;
		private static final int DELAY_COEF = 1;
		private static final int SIZE_COEF = 1;
		
		private byte[] bytesToWrite = null;
		private int offset, size, priorityCoefficient;
		private final long timestamp;
		final Bundle receivedBundle;
		
		public PacketWriteTask(byte[] bytes, int offset, int size, int priorityCoefficient){
			timestamp = System.currentTimeMillis();
			bytesToWrite = bytes;
			this.offset = offset;
			this.size = size;
			this.priorityCoefficient = priorityCoefficient;
			receivedBundle = null;
		}
		
		public PacketWriteTask(Bundle bundle){
			this.receivedBundle = bundle;
			timestamp = System.currentTimeMillis();
			bytesToWrite = bundle.getByteArray(TransportConstants.BYTES_TO_SEND_EXTRA_NAME); 
			offset = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_OFFSET, 0); //If nothing, start at the begining of the array
			size = bundle.getInt(TransportConstants.BYTES_TO_SEND_EXTRA_COUNT, bytesToWrite.length);  //In case there isn't anything just send the whole packet.
			this.priorityCoefficient = bundle.getInt(TransportConstants.PACKET_PRIORITY_COEFFICIENT,0); Log.d(TAG, "packet priority coef: "+ this.priorityCoefficient);
		}
		
		@Override
		public void run() {
			if(receivedBundle!=null){
				writeBytesToTransport(receivedBundle);
			}else if(bytesToWrite !=null){
				manuallyWriteBytes(bytesToWrite, offset, size);
			}
		}
		
		private long getWeight(long currentTime){ //Time waiting - size - prioirty_coef
			return ((((currentTime-timestamp) + DELAY_CONSTANT) * DELAY_COEF ) - ((size -SIZE_CONSTANT) * SIZE_COEF) - (priorityCoefficient * PRIORITY_COEF_CONSTATNT));
		}
	}
	
	/**
	 * Extends thread to consume PacketWriteTasks in a priority queue fashion. It will attempt to look
	 * at all apps serial queue of tasks and compare them
	 * @author Joey Grover
	 *
	 */
	private class PacketWriteTaskMaster extends Thread{
		protected final  Object QUEUE_LOCK = new Object();
		private boolean isHalted = false, isWaiting = false;
		
		public PacketWriteTaskMaster(){
			this.setName("PacketWriteTaskMaster");
		}
		
		@Override
		public void run() {
			while(!isHalted){
				try{
					PacketWriteTask task = null;
					synchronized(QUEUE_LOCK){
						task = getNextTask();
						if(task != null){
							task.run();
						}else{
							isWaiting = true;
							QUEUE_LOCK.wait();
							isWaiting = false;
						}
					}
				}catch(InterruptedException e){
					break;
				}
			}
		}

		private void alert(){
			if(isWaiting){
				synchronized(QUEUE_LOCK){
					QUEUE_LOCK.notify();
				}
			}
		}

		private void close(){
			this.isHalted = true;
		}
	}
	
	/**
	 * Custom queue to prioritize packet write tasks based on their priority coefficient.<br> The queue is a doubly linked list.<br><br>
	 * When a tasks is added to the queue, it will be evaluated using it's priority coefficient. If the coefficient is greater than 0, it will simply
	 * be placed at the end of the queue. If the coefficient is equal to 0, the queue will begin to iterate at the head and work it's way back. Once it is found that the current 
	 * tasks has a priority coefficient greater than 0, it will be placed right before that task. The idea is to keep a semi-serial queue but creates a priority that allows urgent
	 * tasks such as UI related to skip near the front. However, it is assumed those tasks of higher priority should also be handled in a serial fashion.
	 * 
	 * @author Joey Grover
	 *
	 */
	private class PacketWriteTaskBlockingQueue{
		final class Node<E> {
			E item;
			Node<E> prev;
			Node<E> next;
			Node(E item, Node<E> previous, Node<E> next) {
				this.item = item;
				this.prev = previous;
				this.next = next;
			}
		}

		private Node<PacketWriteTask> head;
		private Node<PacketWriteTask> tail;

		/**
		 * This will take the given task and insert it at the tail of the queue
		 * @param task the task to be inserted at the tail of the queue
		 */
		private void insertAtTail(PacketWriteTask task){
			if (task == null){
				throw new NullPointerException();
			}
			Node<PacketWriteTask> oldTail = tail;
			Node<PacketWriteTask> newTail = new Node<PacketWriteTask>(task, oldTail, null);
			tail = newTail;
			if (head == null){
				head = newTail;
			}else{
				oldTail.next = newTail;
			}

		}

		/**
		 * This will take the given task and insert it at the head of the queue
		 * @param task the task to be inserted at the head of the queue
		 */
		private void insertAtHead(PacketWriteTask task){
			if (task == null){
				throw new NullPointerException();
			}
			Node<PacketWriteTask> oldHead = head;
			Node<PacketWriteTask> newHead = new Node<PacketWriteTask>(task, null, oldHead);
			head = newHead;
			if (tail == null){
				tail = newHead;
			}else{
				if(oldHead!=null){
					oldHead.prev = newHead;
				}
			}
		}

		/**
		 * Insert the task in the queue where it belongs
		 * @param task
		 */
		 public void add(PacketWriteTask task){
			synchronized(this){
				if (task == null){
					throw new NullPointerException();
				}

				//If we currently don't have anything in our queue
				if(head == null || tail == null){
					Node<PacketWriteTask> taskNode = new Node<PacketWriteTask>(task, head, tail);
					head = taskNode;
					tail = taskNode;
					return;
				}else if(task.priorityCoefficient>0){ //If the  task is already a not high priority task, we just need to insert it at the tail
					insertAtTail(task);
					return;
				}else if(head.item.priorityCoefficient>0){ //If the head task is already a not high priority task, we just need to insert at head
					insertAtHead(task);
					return;
				}else{
					if(tail!=null && tail.item.priorityCoefficient==0){ //Saves us from going through the entire list if all of these tasks are priority coef == 0
						insertAtTail(task);
						return;
					}
					Node<PacketWriteTask> currentPlace = head;
					while(true){
						if(currentPlace.item.priorityCoefficient==0){
							if(currentPlace.next==null){
								//We've reached the end of the list
								insertAtTail(task);
								return;
							}else{
								currentPlace = currentPlace.next;
								continue;
							}
						}else{
							//We've found where this task should be inserted
							Node<PacketWriteTask> previous = currentPlace.prev;
							Node<PacketWriteTask> taskNode = new Node<PacketWriteTask>(task, previous, currentPlace);
							previous.next = taskNode;
							currentPlace.prev = taskNode;
							return;

						}
					}
				}
			}
		 }

		 /**
		  * Peek at the current head of the queue
		  * @return the task at the head of the queue but does not remove it from the queue
		  */
		 public PacketWriteTask peek(){
			 synchronized(this){
				 if(head == null){
					 return null;
				 }else{
					 return head.item;
				 }
			 }
		 }

		 /**
		  * Remove the head of the queue
		  * @return the old head of the queue
		  */
		 public PacketWriteTask poll(){
			 synchronized(this){
				 if(head == null){
					 return null;
				 }else{
					 Node<PacketWriteTask> retValNode = head;
					 Node<PacketWriteTask> newHead = head.next;
					 if(newHead == null){
						 tail = null;
					 }
					 head = newHead;

					 return retValNode.item;
				 }
			 }
		 }

		 /**
		  * Currently only clears the head and the tail of the queue.
		  */
		 public void clear(){
			 //Should probably go through the linked list and clear elements, but gc should clear them out automatically. 
			 head = null;
			 tail = null;
		 }
	}


}