summaryrefslogtreecommitdiff
path: root/services/core/java/com/android/server/tv/tunerresourcemanager/TunerResourceManagerService.java
blob: 0afb049d31c7b1a298e2a1d324f0937601329a76 (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
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.tv.tunerresourcemanager;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.tv.tuner.DemuxFilterMainType;
import android.media.IResourceManagerService;
import android.media.tv.TvInputManager;
import android.media.tv.tunerresourcemanager.CasSessionRequest;
import android.media.tv.tunerresourcemanager.IResourcesReclaimListener;
import android.media.tv.tunerresourcemanager.ITunerResourceManager;
import android.media.tv.tunerresourcemanager.ResourceClientProfile;
import android.media.tv.tunerresourcemanager.TunerCiCamRequest;
import android.media.tv.tunerresourcemanager.TunerDemuxInfo;
import android.media.tv.tunerresourcemanager.TunerDemuxRequest;
import android.media.tv.tunerresourcemanager.TunerDescramblerRequest;
import android.media.tv.tunerresourcemanager.TunerFrontendInfo;
import android.media.tv.tunerresourcemanager.TunerFrontendRequest;
import android.media.tv.tunerresourcemanager.TunerLnbRequest;
import android.media.tv.tunerresourcemanager.TunerResourceManager;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.util.IndentingPrintWriter;
import android.util.Log;
import android.util.Slog;
import android.util.SparseIntArray;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.server.SystemService;

import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * This class provides a system service that manages the TV tuner resources.
 *
 * @hide
 */
public class TunerResourceManagerService extends SystemService implements IBinder.DeathRecipient {
    private static final String TAG = "TunerResourceManagerService";
    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);

    public static final int INVALID_CLIENT_ID = -1;
    private static final int MAX_CLIENT_PRIORITY = 1000;
    private static final long INVALID_THREAD_ID = -1;
    private static final long TRMS_LOCK_TIMEOUT = 500;

    private static final int INVALID_FE_COUNT = -1;

    // Map of the registered client profiles
    private Map<Integer, ClientProfile> mClientProfiles = new HashMap<>();
    private int mNextUnusedClientId = 0;

    // Map of the current available frontend resources
    private Map<Integer, FrontendResource> mFrontendResources = new HashMap<>();
    // SparseIntArray of the max usable number for each frontend resource type
    private SparseIntArray mFrontendMaxUsableNums = new SparseIntArray();
    // SparseIntArray of the currently used number for each frontend resource type
    private SparseIntArray mFrontendUsedNums = new SparseIntArray();
    // SparseIntArray of the existing number for each frontend resource type
    private SparseIntArray mFrontendExistingNums = new SparseIntArray();

    // Backups for the frontend resource maps for enabling testing with custom resource maps
    // such as TunerTest.testHasUnusedFrontend1()
    private Map<Integer, FrontendResource> mFrontendResourcesBackup = new HashMap<>();
    private SparseIntArray mFrontendMaxUsableNumsBackup = new SparseIntArray();
    private SparseIntArray mFrontendUsedNumsBackup = new SparseIntArray();
    private SparseIntArray mFrontendExistingNumsBackup = new SparseIntArray();

    // Map of the current available demux resources
    private Map<Integer, DemuxResource> mDemuxResources = new HashMap<>();
    // Map of the current available lnb resources
    private Map<Integer, LnbResource> mLnbResources = new HashMap<>();
    // Map of the current available Cas resources
    private Map<Integer, CasResource> mCasResources = new HashMap<>();
    // Map of the current available CiCam resources
    private Map<Integer, CiCamResource> mCiCamResources = new HashMap<>();

    @GuardedBy("mLock")
    private Map<Integer, ResourcesReclaimListenerRecord> mListeners = new HashMap<>();

    private TvInputManager mTvInputManager;
    private ActivityManager mActivityManager;
    private IResourceManagerService mMediaResourceManager;
    private UseCasePriorityHints mPriorityCongfig = new UseCasePriorityHints();

    // An internal resource request count to help generate resource handle.
    private int mResourceRequestCount = 0;

    // Used to synchronize the access to the service.
    private final Object mLock = new Object();

    private final ReentrantLock mLockForTRMSLock = new ReentrantLock();
    private final Condition mTunerApiLockReleasedCV = mLockForTRMSLock.newCondition();
    private int mTunerApiLockHolder = INVALID_CLIENT_ID;
    private long mTunerApiLockHolderThreadId = INVALID_THREAD_ID;
    private int mTunerApiLockNestedCount = 0;

    public TunerResourceManagerService(@Nullable Context context) {
        super(context);
    }

    @Override
    public void onStart() {
        onStart(false /*isForTesting*/);
    }

    @VisibleForTesting
    protected void onStart(boolean isForTesting) {
        if (!isForTesting) {
            publishBinderService(Context.TV_TUNER_RESOURCE_MGR_SERVICE, new BinderService());
        }
        mTvInputManager = (TvInputManager) getContext().getSystemService(Context.TV_INPUT_SERVICE);
        mActivityManager =
                (ActivityManager) getContext().getSystemService(Context.ACTIVITY_SERVICE);
        mPriorityCongfig.parse();

        // Call SystemProperties.set() in mock app will throw exception because of permission.
        if (!isForTesting) {
            final boolean lazyHal = SystemProperties.getBoolean("ro.tuner.lazyhal", false);
            if (!lazyHal) {
                // The HAL is not a lazy HAL, enable the tuner server.
                SystemProperties.set("tuner.server.enable", "true");
            }
        }

        if (mMediaResourceManager == null) {
            IBinder mediaResourceManagerBinder = getBinderService("media.resource_manager");
            if (mediaResourceManagerBinder == null) {
                Slog.w(TAG, "Resource Manager Service not available.");
                return;
            }
            try {
                mediaResourceManagerBinder.linkToDeath(this, /*flags*/ 0);
            } catch (RemoteException e) {
                Slog.w(TAG, "Could not link to death of native resource manager service.");
                return;
            }
            mMediaResourceManager = IResourceManagerService.Stub.asInterface(
                    mediaResourceManagerBinder);
        }
    }

    private final class BinderService extends ITunerResourceManager.Stub {
        @Override
        public void registerClientProfile(@NonNull ResourceClientProfile profile,
                @NonNull IResourcesReclaimListener listener, @NonNull int[] clientId)
                throws RemoteException {
            enforceTrmAccessPermission("registerClientProfile");
            enforceTunerAccessPermission("registerClientProfile");
            if (profile == null) {
                throw new RemoteException("ResourceClientProfile can't be null");
            }

            if (clientId == null) {
                throw new RemoteException("clientId can't be null!");
            }

            if (listener == null) {
                throw new RemoteException("IResourcesReclaimListener can't be null!");
            }

            if (!mPriorityCongfig.isDefinedUseCase(profile.useCase)) {
                throw new RemoteException("Use undefined client use case:" + profile.useCase);
            }

            synchronized (mLock) {
                registerClientProfileInternal(profile, listener, clientId);
            }
        }

        @Override
        public void unregisterClientProfile(int clientId) throws RemoteException {
            enforceTrmAccessPermission("unregisterClientProfile");
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    Slog.e(TAG, "Unregistering non exists client:" + clientId);
                    return;
                }
                unregisterClientProfileInternal(clientId);
            }
        }

        @Override
        public boolean updateClientPriority(int clientId, int priority, int niceValue) {
            enforceTrmAccessPermission("updateClientPriority");
            synchronized (mLock) {
                return updateClientPriorityInternal(clientId, priority, niceValue);
            }
        }

        @Override
        public boolean hasUnusedFrontend(int frontendType) {
            enforceTrmAccessPermission("hasUnusedFrontend");
            synchronized (mLock) {
                return hasUnusedFrontendInternal(frontendType);
            }
        }

        @Override
        public boolean isLowestPriority(int clientId, int frontendType)
                throws RemoteException {
            enforceTrmAccessPermission("isLowestPriority");
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    throw new RemoteException("isLowestPriority called from unregistered client: "
                            + clientId);
                }
                return isLowestPriorityInternal(clientId, frontendType);
            }
        }

        @Override
        public void setFrontendInfoList(@NonNull TunerFrontendInfo[] infos) throws RemoteException {
            enforceTrmAccessPermission("setFrontendInfoList");
            if (infos == null) {
                throw new RemoteException("TunerFrontendInfo can't be null");
            }
            synchronized (mLock) {
                setFrontendInfoListInternal(infos);
            }
        }

        @Override
        public void setDemuxInfoList(@NonNull TunerDemuxInfo[] infos) throws RemoteException {
            enforceTrmAccessPermission("setDemuxInfoList");
            if (infos == null) {
                throw new RemoteException("TunerDemuxInfo can't be null");
            }
            synchronized (mLock) {
                setDemuxInfoListInternal(infos);
            }
        }

        @Override
        public void updateCasInfo(int casSystemId, int maxSessionNum) {
            enforceTrmAccessPermission("updateCasInfo");
            synchronized (mLock) {
                updateCasInfoInternal(casSystemId, maxSessionNum);
            }
        }

        @Override
        public void setLnbInfoList(int[] lnbHandles) throws RemoteException {
            enforceTrmAccessPermission("setLnbInfoList");
            if (lnbHandles == null) {
                throw new RemoteException("Lnb handle list can't be null");
            }
            synchronized (mLock) {
                setLnbInfoListInternal(lnbHandles);
            }
        }

        @Override
        public boolean requestFrontend(@NonNull TunerFrontendRequest request,
                @NonNull int[] frontendHandle) {
            enforceTunerAccessPermission("requestFrontend");
            enforceTrmAccessPermission("requestFrontend");
            if (frontendHandle == null) {
                Slog.e(TAG, "frontendHandle can't be null");
                return false;
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    Slog.e(TAG, "Request frontend from unregistered client: "
                            + request.clientId);
                    return false;
                }
                // If the request client is holding or sharing a frontend, throw an exception.
                if (!getClientProfile(request.clientId).getInUseFrontendHandles().isEmpty()) {
                    Slog.e(TAG, "Release frontend before requesting another one. Client id: "
                            + request.clientId);
                    return false;
                }
                return requestFrontendInternal(request, frontendHandle);
            }
        }

        @Override
        public boolean setMaxNumberOfFrontends(int frontendType, int maxUsableNum) {
            enforceTunerAccessPermission("setMaxNumberOfFrontends");
            enforceTrmAccessPermission("setMaxNumberOfFrontends");
            if (maxUsableNum < 0) {
                Slog.w(TAG, "setMaxNumberOfFrontends failed with maxUsableNum:" + maxUsableNum
                        + " frontendType:" + frontendType);
                return false;
            }
            synchronized (mLock) {
                return setMaxNumberOfFrontendsInternal(frontendType, maxUsableNum);
            }
        }

        @Override
        public int getMaxNumberOfFrontends(int frontendType) {
            enforceTunerAccessPermission("getMaxNumberOfFrontends");
            enforceTrmAccessPermission("getMaxNumberOfFrontends");
            synchronized (mLock) {
                return getMaxNumberOfFrontendsInternal(frontendType);
            }
        }

        @Override
        public void shareFrontend(int selfClientId, int targetClientId) throws RemoteException {
            enforceTunerAccessPermission("shareFrontend");
            enforceTrmAccessPermission("shareFrontend");
            synchronized (mLock) {
                if (!checkClientExists(selfClientId)) {
                    throw new RemoteException("Share frontend request from an unregistered client:"
                            + selfClientId);
                }
                if (!checkClientExists(targetClientId)) {
                    throw new RemoteException("Request to share frontend with an unregistered "
                            + "client:" + targetClientId);
                }
                if (getClientProfile(targetClientId).getInUseFrontendHandles().isEmpty()) {
                    throw new RemoteException("Request to share frontend with a client that has no "
                            + "frontend resources. Target client id:" + targetClientId);
                }
                shareFrontendInternal(selfClientId, targetClientId);
            }
        }

        @Override
        public boolean transferOwner(int resourceType, int currentOwnerId, int newOwnerId) {
            enforceTunerAccessPermission("transferOwner");
            enforceTrmAccessPermission("transferOwner");
            synchronized (mLock) {
                if (!checkClientExists(currentOwnerId)) {
                    Slog.e(TAG, "currentOwnerId:" + currentOwnerId + " does not exit");
                    return false;
                }
                if (!checkClientExists(newOwnerId)) {
                    Slog.e(TAG, "newOwnerId:" + newOwnerId + " does not exit");
                    return false;
                }
                return transferOwnerInternal(resourceType, currentOwnerId, newOwnerId);
            }
        }

        @Override
        public boolean requestDemux(@NonNull TunerDemuxRequest request,
                    @NonNull int[] demuxHandle) throws RemoteException {
            enforceTunerAccessPermission("requestDemux");
            enforceTrmAccessPermission("requestDemux");
            if (demuxHandle == null) {
                throw new RemoteException("demuxHandle can't be null");
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    throw new RemoteException("Request demux from unregistered client:"
                            + request.clientId);
                }
                return requestDemuxInternal(request, demuxHandle);
            }
        }

        @Override
        public boolean requestDescrambler(@NonNull TunerDescramblerRequest request,
                    @NonNull int[] descramblerHandle) throws RemoteException {
            enforceDescramblerAccessPermission("requestDescrambler");
            enforceTrmAccessPermission("requestDescrambler");
            if (descramblerHandle == null) {
                throw new RemoteException("descramblerHandle can't be null");
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    throw new RemoteException("Request descrambler from unregistered client:"
                            + request.clientId);
                }
                return requestDescramblerInternal(request, descramblerHandle);
            }
        }

        @Override
        public boolean requestCasSession(@NonNull CasSessionRequest request,
                @NonNull int[] casSessionHandle) throws RemoteException {
            enforceTrmAccessPermission("requestCasSession");
            if (casSessionHandle == null) {
                throw new RemoteException("casSessionHandle can't be null");
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    throw new RemoteException("Request cas from unregistered client:"
                            + request.clientId);
                }
                return requestCasSessionInternal(request, casSessionHandle);
            }
        }

        @Override
        public boolean requestCiCam(@NonNull TunerCiCamRequest request,
                @NonNull int[] ciCamHandle) throws RemoteException {
            enforceTrmAccessPermission("requestCiCam");
            if (ciCamHandle == null) {
                throw new RemoteException("ciCamHandle can't be null");
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    throw new RemoteException("Request ciCam from unregistered client:"
                            + request.clientId);
                }
                return requestCiCamInternal(request, ciCamHandle);
            }
        }

        @Override
        public boolean requestLnb(@NonNull TunerLnbRequest request, @NonNull int[] lnbHandle)
                throws RemoteException {
            enforceTunerAccessPermission("requestLnb");
            enforceTrmAccessPermission("requestLnb");
            if (lnbHandle == null) {
                throw new RemoteException("lnbHandle can't be null");
            }
            synchronized (mLock) {
                if (!checkClientExists(request.clientId)) {
                    throw new RemoteException("Request lnb from unregistered client:"
                            + request.clientId);
                }
                return requestLnbInternal(request, lnbHandle);
            }
        }

        @Override
        public void releaseFrontend(int frontendHandle, int clientId) throws RemoteException {
            enforceTunerAccessPermission("releaseFrontend");
            enforceTrmAccessPermission("releaseFrontend");
            if (!validateResourceHandle(TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND,
                    frontendHandle)) {
                throw new RemoteException("frontendHandle can't be invalid");
            }
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    throw new RemoteException("Release frontend from unregistered client:"
                            + clientId);
                }
                FrontendResource fe = getFrontendResource(frontendHandle);
                if (fe == null) {
                    throw new RemoteException("Releasing frontend does not exist.");
                }
                int ownerClientId = fe.getOwnerClientId();
                ClientProfile ownerProfile = getClientProfile(ownerClientId);
                if (ownerClientId != clientId
                        && (ownerProfile != null
                              && !ownerProfile.getShareFeClientIds().contains(clientId))) {
                    throw new RemoteException(
                            "Client is not the current owner of the releasing fe.");
                }
                releaseFrontendInternal(fe, clientId);
            }
        }

        @Override
        public void releaseDemux(int demuxHandle, int clientId) throws RemoteException {
            enforceTunerAccessPermission("releaseDemux");
            enforceTrmAccessPermission("releaseDemux");
            if (DEBUG) {
                Slog.e(TAG, "releaseDemux(demuxHandle=" + demuxHandle + ")");
            }

            synchronized (mLock) {
                // For Tuner 2.0 and below or any HW constraint devices that are unable to support
                // ITuner.openDemuxById(), demux resources are not really managed under TRM and
                // mDemuxResources.size() will be zero
                if (mDemuxResources.size() == 0) {
                    return;
                }

                if (!checkClientExists(clientId)) {
                    throw new RemoteException("Release demux for unregistered client:" + clientId);
                }
                DemuxResource demux = getDemuxResource(demuxHandle);
                if (demux == null) {
                    throw new RemoteException("Releasing demux does not exist.");
                }
                if (demux.getOwnerClientId() != clientId) {
                    throw new RemoteException("Client is not the current owner "
                            + "of the releasing demux.");
                }
                releaseDemuxInternal(demux);
            }
        }

        @Override
        public void releaseDescrambler(int descramblerHandle, int clientId) {
            enforceTunerAccessPermission("releaseDescrambler");
            enforceTrmAccessPermission("releaseDescrambler");
            if (DEBUG) {
                Slog.d(TAG, "releaseDescrambler(descramblerHandle=" + descramblerHandle + ")");
            }
        }

        @Override
        public void releaseCasSession(int casSessionHandle, int clientId) throws RemoteException {
            enforceTrmAccessPermission("releaseCasSession");
            if (!validateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_CAS_SESSION, casSessionHandle)) {
                throw new RemoteException("casSessionHandle can't be invalid");
            }
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    throw new RemoteException("Release cas from unregistered client:" + clientId);
                }
                int casSystemId = getClientProfile(clientId).getInUseCasSystemId();
                CasResource cas = getCasResource(casSystemId);
                if (cas == null) {
                    throw new RemoteException("Releasing cas does not exist.");
                }
                if (!cas.getOwnerClientIds().contains(clientId)) {
                    throw new RemoteException(
                            "Client is not the current owner of the releasing cas.");
                }
                releaseCasSessionInternal(cas, clientId);
            }
        }

        @Override
        public void releaseCiCam(int ciCamHandle, int clientId) throws RemoteException {
            enforceTrmAccessPermission("releaseCiCam");
            if (!validateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND_CICAM, ciCamHandle)) {
                throw new RemoteException("ciCamHandle can't be invalid");
            }
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    throw new RemoteException("Release ciCam from unregistered client:" + clientId);
                }
                int ciCamId = getClientProfile(clientId).getInUseCiCamId();
                if (ciCamId != getResourceIdFromHandle(ciCamHandle)) {
                    throw new RemoteException("The client " + clientId + " is not the owner of "
                            + "the releasing ciCam.");
                }
                CiCamResource ciCam = getCiCamResource(ciCamId);
                if (ciCam == null) {
                    throw new RemoteException("Releasing ciCam does not exist.");
                }
                if (!ciCam.getOwnerClientIds().contains(clientId)) {
                    throw new RemoteException(
                            "Client is not the current owner of the releasing ciCam.");
                }
                releaseCiCamInternal(ciCam, clientId);
            }
        }

        @Override
        public void releaseLnb(int lnbHandle, int clientId) throws RemoteException {
            enforceTunerAccessPermission("releaseLnb");
            enforceTrmAccessPermission("releaseLnb");
            if (!validateResourceHandle(TunerResourceManager.TUNER_RESOURCE_TYPE_LNB, lnbHandle)) {
                throw new RemoteException("lnbHandle can't be invalid");
            }
            synchronized (mLock) {
                if (!checkClientExists(clientId)) {
                    throw new RemoteException("Release lnb from unregistered client:" + clientId);
                }
                LnbResource lnb = getLnbResource(lnbHandle);
                if (lnb == null) {
                    throw new RemoteException("Releasing lnb does not exist.");
                }
                if (lnb.getOwnerClientId() != clientId) {
                    throw new RemoteException("Client is not the current owner "
                            + "of the releasing lnb.");
                }
                releaseLnbInternal(lnb);
            }
        }

        @Override
        public boolean isHigherPriority(
                ResourceClientProfile challengerProfile, ResourceClientProfile holderProfile)
                throws RemoteException {
            enforceTrmAccessPermission("isHigherPriority");
            if (challengerProfile == null || holderProfile == null) {
                throw new RemoteException("Client profiles can't be null.");
            }
            synchronized (mLock) {
                return isHigherPriorityInternal(challengerProfile, holderProfile);
            }
        }

        @Override
        public void storeResourceMap(int resourceType) {
            enforceTrmAccessPermission("storeResourceMap");
            synchronized (mLock) {
                storeResourceMapInternal(resourceType);
            }
        }

        @Override
        public void clearResourceMap(int resourceType) {
            enforceTrmAccessPermission("clearResourceMap");
            synchronized (mLock) {
                clearResourceMapInternal(resourceType);
            }
        }

        @Override
        public void restoreResourceMap(int resourceType) {
            enforceTrmAccessPermission("restoreResourceMap");
            synchronized (mLock) {
                restoreResourceMapInternal(resourceType);
            }
        }

        @Override
        public boolean acquireLock(int clientId, long clientThreadId) {
            enforceTrmAccessPermission("acquireLock");
            // this must not be locked with mLock
            return acquireLockInternal(clientId, clientThreadId, TRMS_LOCK_TIMEOUT);
        }

        @Override
        public boolean releaseLock(int clientId) {
            enforceTrmAccessPermission("releaseLock");
            // this must not be locked with mLock
            return releaseLockInternal(clientId, TRMS_LOCK_TIMEOUT, false, false);
        }

        @Override
        protected void dump(FileDescriptor fd, final PrintWriter writer, String[] args) {
            final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");

            if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
                    != PackageManager.PERMISSION_GRANTED) {
                pw.println("Permission Denial: can't dump!");
                return;
            }

            synchronized (mLock) {
                dumpMap(mClientProfiles, "ClientProfiles:", "\n", pw);
                dumpMap(mFrontendResources, "FrontendResources:", "\n", pw);
                dumpSIA(mFrontendExistingNums, "FrontendExistingNums:", ", ", pw);
                dumpSIA(mFrontendUsedNums, "FrontendUsedNums:", ", ", pw);
                dumpSIA(mFrontendMaxUsableNums, "FrontendMaxUsableNums:", ", ", pw);
                dumpMap(mFrontendResourcesBackup, "FrontendResourcesBackUp:", "\n", pw);
                dumpSIA(mFrontendExistingNumsBackup, "FrontendExistingNumsBackup:", ", ", pw);
                dumpSIA(mFrontendUsedNumsBackup, "FrontendUsedNumsBackup:", ", ", pw);
                dumpSIA(mFrontendMaxUsableNumsBackup, "FrontendUsedNumsBackup:", ", ", pw);
                dumpMap(mDemuxResources, "DemuxResource:", "\n", pw);
                dumpMap(mLnbResources, "LnbResource:", "\n", pw);
                dumpMap(mCasResources, "CasResource:", "\n", pw);
                dumpMap(mCiCamResources, "CiCamResource:", "\n", pw);
                dumpMap(mListeners, "Listners:", "\n", pw);
            }
        }

        @Override
        public int getClientPriority(int useCase, int pid) throws RemoteException {
            enforceTrmAccessPermission("getClientPriority");
            synchronized (mLock) {
                return TunerResourceManagerService.this.getClientPriority(
                        useCase, checkIsForeground(pid));
            }
        }
        @Override
        public int getConfigPriority(int useCase, boolean isForeground) throws RemoteException {
            enforceTrmAccessPermission("getConfigPriority");
            synchronized (mLock) {
                return TunerResourceManagerService.this.getClientPriority(useCase, isForeground);
            }
        }
    }

    /**
     * Handle the death of the native resource manager service
     */
    @Override
    public void binderDied() {
        if (DEBUG) {
            Slog.w(TAG, "Native media resource manager service has died");
        }
        synchronized (mLock) {
            mMediaResourceManager = null;
        }
    }

    @VisibleForTesting
    protected void registerClientProfileInternal(ResourceClientProfile profile,
            IResourcesReclaimListener listener, int[] clientId) {
        if (DEBUG) {
            Slog.d(TAG, "registerClientProfile(clientProfile=" + profile + ")");
        }

        clientId[0] = INVALID_CLIENT_ID;
        if (mTvInputManager == null) {
            Slog.e(TAG, "TvInputManager is null. Can't register client profile.");
            return;
        }
        // TODO tell if the client already exists
        clientId[0] = mNextUnusedClientId++;

        int pid = profile.tvInputSessionId == null
                ? Binder.getCallingPid() /*callingPid*/
                : mTvInputManager.getClientPid(profile.tvInputSessionId); /*tvAppId*/

        // Update Media Resource Manager with the tvAppId
        if (profile.tvInputSessionId != null && mMediaResourceManager != null) {
            try {
                mMediaResourceManager.overridePid(Binder.getCallingPid(), pid);
            } catch (RemoteException e) {
                Slog.e(TAG, "Could not overridePid in resourceManagerSercice,"
                        + " remote exception: " + e);
            }
        }

        ClientProfile clientProfile = new ClientProfile.Builder(clientId[0])
                                              .tvInputSessionId(profile.tvInputSessionId)
                                              .useCase(profile.useCase)
                                              .processId(pid)
                                              .build();
        clientProfile.setPriority(
                getClientPriority(profile.useCase, checkIsForeground(pid)));

        addClientProfile(clientId[0], clientProfile, listener);
    }

    @VisibleForTesting
    protected void unregisterClientProfileInternal(int clientId) {
        if (DEBUG) {
            Slog.d(TAG, "unregisterClientProfile(clientId=" + clientId + ")");
        }
        removeClientProfile(clientId);
        // Remove the Media Resource Manager callingPid to tvAppId mapping
        if (mMediaResourceManager != null) {
            try {
                mMediaResourceManager.overridePid(Binder.getCallingPid(), -1);
            } catch (RemoteException e) {
                Slog.e(TAG, "Could not overridePid in resourceManagerSercice when unregister,"
                        + " remote exception: " + e);
            }
        }
    }

    @VisibleForTesting
    protected boolean updateClientPriorityInternal(int clientId, int priority, int niceValue) {
        if (DEBUG) {
            Slog.d(TAG,
                    "updateClientPriority(clientId=" + clientId + ", priority=" + priority
                            + ", niceValue=" + niceValue + ")");
        }

        ClientProfile profile = getClientProfile(clientId);
        if (profile == null) {
            Slog.e(TAG,
                    "Can not find client profile with id " + clientId
                            + " when trying to update the client priority.");
            return false;
        }

        profile.overwritePriority(priority);
        profile.setNiceValue(niceValue);

        return true;
    }


    protected boolean hasUnusedFrontendInternal(int frontendType) {
        for (FrontendResource fr : getFrontendResources().values()) {
            if (fr.getType() == frontendType && !fr.isInUse()) {
                return true;
            }
        }
        return false;
    }

    protected boolean isLowestPriorityInternal(int clientId, int frontendType)
            throws RemoteException {
        // Update the client priority
        ClientProfile requestClient = getClientProfile(clientId);
        if (requestClient == null) {
            return true;
        }
        clientPriorityUpdateOnRequest(requestClient);
        int clientPriority = requestClient.getPriority();

        // Check if there is another holder with lower priority
        for (FrontendResource fr : getFrontendResources().values()) {
            if (fr.getType() == frontendType && fr.isInUse()) {
                int priority = updateAndGetOwnerClientPriority(fr.getOwnerClientId());
                // Returns false only when the clientPriority is strictly greater
                // because false means that there is another reclaimable resource
                if (clientPriority > priority) {
                    return false;
                }
            }
        }
        return true;
    }

    protected void storeResourceMapInternal(int resourceType) {
        switch (resourceType) {
            case TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND:
                replaceFeResourceMap(mFrontendResources, mFrontendResourcesBackup);
                replaceFeCounts(mFrontendExistingNums, mFrontendExistingNumsBackup);
                replaceFeCounts(mFrontendUsedNums, mFrontendUsedNumsBackup);
                replaceFeCounts(mFrontendMaxUsableNums, mFrontendMaxUsableNumsBackup);
                break;
                // TODO: implement for other resource type when needed
            default:
                break;
        }
    }

    protected void clearResourceMapInternal(int resourceType) {
        switch (resourceType) {
            case TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND:
                replaceFeResourceMap(null, mFrontendResources);
                replaceFeCounts(null, mFrontendExistingNums);
                replaceFeCounts(null, mFrontendUsedNums);
                replaceFeCounts(null, mFrontendMaxUsableNums);
                break;
                // TODO: implement for other resource type when needed
            default:
                break;
        }
    }

    protected void restoreResourceMapInternal(int resourceType) {
        switch (resourceType) {
            case TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND:
                replaceFeResourceMap(mFrontendResourcesBackup, mFrontendResources);
                replaceFeCounts(mFrontendExistingNumsBackup, mFrontendExistingNums);
                replaceFeCounts(mFrontendUsedNumsBackup, mFrontendUsedNums);
                replaceFeCounts(mFrontendMaxUsableNumsBackup, mFrontendMaxUsableNums);
                break;
                // TODO: implement for other resource type when needed
            default:
                break;
        }
    }

    @VisibleForTesting
    protected void setFrontendInfoListInternal(TunerFrontendInfo[] infos) {
        if (DEBUG) {
            Slog.d(TAG, "updateFrontendInfo:");
            for (int i = 0; i < infos.length; i++) {
                Slog.d(TAG, infos[i].toString());
            }
        }

        // A set to record the frontends pending on updating. Ids will be removed
        // from this set once its updating finished. Any frontend left in this set when all
        // the updates are done will be removed from mFrontendResources.
        Set<Integer> updatingFrontendHandles = new HashSet<>(getFrontendResources().keySet());

        // Update frontendResources map and other mappings accordingly
        for (int i = 0; i < infos.length; i++) {
            if (getFrontendResource(infos[i].handle) != null) {
                if (DEBUG) {
                    Slog.d(TAG, "Frontend handle=" + infos[i].handle + "exists.");
                }
                updatingFrontendHandles.remove(infos[i].handle);
            } else {
                // Add a new fe resource
                FrontendResource newFe = new FrontendResource.Builder(infos[i].handle)
                                                 .type(infos[i].type)
                                                 .exclusiveGroupId(infos[i].exclusiveGroupId)
                                                 .build();
                addFrontendResource(newFe);
            }
        }

        for (int removingHandle : updatingFrontendHandles) {
            // update the exclusive group id member list
            removeFrontendResource(removingHandle);
        }
    }

    @VisibleForTesting
    protected void setDemuxInfoListInternal(TunerDemuxInfo[] infos) {
        if (DEBUG) {
            Slog.d(TAG, "updateDemuxInfo:");
            for (int i = 0; i < infos.length; i++) {
                Slog.d(TAG, infos[i].toString());
            }
        }

        // A set to record the demuxes pending on updating. Ids will be removed
        // from this set once its updating finished. Any demux left in this set when all
        // the updates are done will be removed from mDemuxResources.
        Set<Integer> updatingDemuxHandles = new HashSet<>(getDemuxResources().keySet());

        // Update demuxResources map and other mappings accordingly
        for (int i = 0; i < infos.length; i++) {
            if (getDemuxResource(infos[i].handle) != null) {
                if (DEBUG) {
                    Slog.d(TAG, "Demux handle=" + infos[i].handle + "exists.");
                }
                updatingDemuxHandles.remove(infos[i].handle);
            } else {
                // Add a new demux resource
                DemuxResource newDemux = new DemuxResource.Builder(infos[i].handle)
                                                 .filterTypes(infos[i].filterTypes)
                                                 .build();
                addDemuxResource(newDemux);
            }
        }

        for (int removingHandle : updatingDemuxHandles) {
            // update the exclusive group id member list
            removeDemuxResource(removingHandle);
        }
    }
    @VisibleForTesting
    protected void setLnbInfoListInternal(int[] lnbHandles) {
        if (DEBUG) {
            for (int i = 0; i < lnbHandles.length; i++) {
                Slog.d(TAG, "updateLnbInfo(lnbHanle=" + lnbHandles[i] + ")");
            }
        }

        // A set to record the Lnbs pending on updating. Handles will be removed
        // from this set once its updating finished. Any lnb left in this set when all
        // the updates are done will be removed from mLnbResources.
        Set<Integer> updatingLnbHandles = new HashSet<>(getLnbResources().keySet());

        // Update lnbResources map and other mappings accordingly
        for (int i = 0; i < lnbHandles.length; i++) {
            if (getLnbResource(lnbHandles[i]) != null) {
                if (DEBUG) {
                    Slog.d(TAG, "Lnb handle=" + lnbHandles[i] + "exists.");
                }
                updatingLnbHandles.remove(lnbHandles[i]);
            } else {
                // Add a new lnb resource
                LnbResource newLnb = new LnbResource.Builder(lnbHandles[i]).build();
                addLnbResource(newLnb);
            }
        }

        for (int removingHandle : updatingLnbHandles) {
            removeLnbResource(removingHandle);
        }
    }

    @VisibleForTesting
    protected void updateCasInfoInternal(int casSystemId, int maxSessionNum) {
        if (DEBUG) {
            Slog.d(TAG,
                    "updateCasInfo(casSystemId=" + casSystemId
                            + ", maxSessionNum=" + maxSessionNum + ")");
        }
        // If maxSessionNum is 0, removing the Cas Resource.
        if (maxSessionNum == 0) {
            removeCasResource(casSystemId);
            removeCiCamResource(casSystemId);
            return;
        }
        // If the Cas exists, updates the Cas Resource accordingly.
        CasResource cas = getCasResource(casSystemId);
        CiCamResource ciCam = getCiCamResource(casSystemId);
        if (cas != null) {
            if (cas.getUsedSessionNum() > maxSessionNum) {
                // Sort and release the short number of Cas resources.
                int releasingCasResourceNum = cas.getUsedSessionNum() - maxSessionNum;
                // TODO: handle CiCam session update.
            }
            cas.updateMaxSessionNum(maxSessionNum);
            if (ciCam != null) {
                ciCam.updateMaxSessionNum(maxSessionNum);
            }
            return;
        }
        // Add the new Cas Resource.
        cas = new CasResource.Builder(casSystemId)
                             .maxSessionNum(maxSessionNum)
                             .build();
        ciCam = new CiCamResource.Builder(casSystemId)
                             .maxSessionNum(maxSessionNum)
                             .build();
        addCasResource(cas);
        addCiCamResource(ciCam);
    }

    @VisibleForTesting
    protected boolean requestFrontendInternal(TunerFrontendRequest request, int[] frontendHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestFrontend(request=" + request + ")");
        }

        frontendHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        // TODO: check if this is really needed
        if (requestClient == null) {
            return false;
        }
        clientPriorityUpdateOnRequest(requestClient);
        int grantingFrontendHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        int inUseLowestPriorityFrHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
        boolean isRequestFromSameProcess = false;
        // If the desired frontend id was specified, we only need to check the frontend.
        boolean hasDesiredFrontend = request.desiredId != TunerFrontendRequest.DEFAULT_DESIRED_ID;
        for (FrontendResource fr : getFrontendResources().values()) {
            int frontendId = getResourceIdFromHandle(fr.getHandle());
            if (fr.getType() == request.frontendType
                    && (!hasDesiredFrontend || frontendId == request.desiredId)) {
                if (!fr.isInUse()) {
                    // Unused resource cannot be acquired if the max is already reached, but
                    // TRM still has to look for the reclaim candidate
                    if (isFrontendMaxNumUseReached(request.frontendType)) {
                        continue;
                    }
                    // Grant unused frontend with no exclusive group members first.
                    if (fr.getExclusiveGroupMemberFeHandles().isEmpty()) {
                        grantingFrontendHandle = fr.getHandle();
                        break;
                    } else if (grantingFrontendHandle
                            == TunerResourceManager.INVALID_RESOURCE_HANDLE) {
                        // Grant the unused frontend with lower id first if all the unused
                        // frontends have exclusive group members.
                        grantingFrontendHandle = fr.getHandle();
                    }
                } else if (grantingFrontendHandle == TunerResourceManager.INVALID_RESOURCE_HANDLE) {
                    // Record the frontend id with the lowest client priority among all the
                    // in use frontends when no available frontend has been found.
                    int priority = getFrontendHighestClientPriority(fr.getOwnerClientId());
                    if (currentLowestPriority > priority) {
                        // we need to check the max used num if the target frontend type is not
                        // currently in primary use (and simply blocked due to exclusive group)
                        ClientProfile targetOwnerProfile = getClientProfile(fr.getOwnerClientId());
                        int primaryFeId = targetOwnerProfile.getPrimaryFrontend();
                        FrontendResource primaryFe = getFrontendResource(primaryFeId);
                        if (fr.getType() != primaryFe.getType()
                                && isFrontendMaxNumUseReached(fr.getType())) {
                            continue;
                        }
                        // update the target frontend
                        inUseLowestPriorityFrHandle = fr.getHandle();
                        currentLowestPriority = priority;
                        isRequestFromSameProcess = (requestClient.getProcessId()
                            == (getClientProfile(fr.getOwnerClientId())).getProcessId());
                    }
                }
            }
        }

        // Grant frontend when there is unused resource.
        if (grantingFrontendHandle != TunerResourceManager.INVALID_RESOURCE_HANDLE) {
            frontendHandle[0] = grantingFrontendHandle;
            updateFrontendClientMappingOnNewGrant(grantingFrontendHandle, request.clientId);
            return true;
        }

        // When all the resources are occupied, grant the lowest priority resource if the
        // request client has higher priority.
        if (inUseLowestPriorityFrHandle != TunerResourceManager.INVALID_RESOURCE_HANDLE
            && ((requestClient.getPriority() > currentLowestPriority) || (
            (requestClient.getPriority() == currentLowestPriority) && isRequestFromSameProcess))) {
            if (!reclaimResource(
                    getFrontendResource(inUseLowestPriorityFrHandle).getOwnerClientId(),
                    TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND)) {
                return false;
            }
            frontendHandle[0] = inUseLowestPriorityFrHandle;
            updateFrontendClientMappingOnNewGrant(
                    inUseLowestPriorityFrHandle, request.clientId);
            return true;
        }

        return false;
    }

    @VisibleForTesting
    protected void shareFrontendInternal(int selfClientId, int targetClientId) {
        if (DEBUG) {
            Slog.d(TAG, "shareFrontend from " + selfClientId + " with " + targetClientId);
        }
        Integer shareeFeClientId = getClientProfile(selfClientId).getShareeFeClientId();
        if (shareeFeClientId != ClientProfile.INVALID_RESOURCE_ID) {
            getClientProfile(shareeFeClientId).stopSharingFrontend(selfClientId);
            getClientProfile(selfClientId).releaseFrontend();
        }
        for (int feId : getClientProfile(targetClientId).getInUseFrontendHandles()) {
            getClientProfile(selfClientId).useFrontend(feId);
        }
        getClientProfile(selfClientId).setShareeFeClientId(targetClientId);
        getClientProfile(targetClientId).shareFrontend(selfClientId);
    }

    private boolean transferFeOwner(int currentOwnerId, int newOwnerId) {
        ClientProfile currentOwnerProfile = getClientProfile(currentOwnerId);
        ClientProfile newOwnerProfile = getClientProfile(newOwnerId);
        // change the owner of all the inUse frontend
        newOwnerProfile.shareFrontend(currentOwnerId);
        currentOwnerProfile.stopSharingFrontend(newOwnerId);
        newOwnerProfile.setShareeFeClientId(ClientProfile.INVALID_RESOURCE_ID);
        currentOwnerProfile.setShareeFeClientId(newOwnerId);
        for (int inUseHandle : newOwnerProfile.getInUseFrontendHandles()) {
            getFrontendResource(inUseHandle).setOwner(newOwnerId);
        }
        // change the primary frontend
        newOwnerProfile.setPrimaryFrontend(currentOwnerProfile.getPrimaryFrontend());
        currentOwnerProfile.setPrimaryFrontend(TunerResourceManager.INVALID_RESOURCE_HANDLE);
        // double check there is no other resources tied to the previous owner
        for (int inUseHandle : currentOwnerProfile.getInUseFrontendHandles()) {
            int ownerId = getFrontendResource(inUseHandle).getOwnerClientId();
            if (ownerId != newOwnerId) {
                Slog.e(TAG, "something is wrong in transferFeOwner:" + inUseHandle
                        + ", " + ownerId + ", " + newOwnerId);
                return false;
            }
        }
        return true;
    }

    private boolean transferFeCiCamOwner(int currentOwnerId, int newOwnerId) {
        ClientProfile currentOwnerProfile = getClientProfile(currentOwnerId);
        ClientProfile newOwnerProfile = getClientProfile(newOwnerId);

        // link ciCamId to the new profile
        int ciCamId = currentOwnerProfile.getInUseCiCamId();
        newOwnerProfile.useCiCam(ciCamId);

        // set the new owner Id
        CiCamResource ciCam = getCiCamResource(ciCamId);
        ciCam.setOwner(newOwnerId);

        // unlink cicam resource from the original owner profile
        currentOwnerProfile.releaseCiCam();
        return true;
    }

    private boolean transferLnbOwner(int currentOwnerId, int newOwnerId) {
        ClientProfile currentOwnerProfile = getClientProfile(currentOwnerId);
        ClientProfile newOwnerProfile = getClientProfile(newOwnerId);

        Set<Integer> inUseLnbHandles = new HashSet<>();
        for (Integer lnbHandle : currentOwnerProfile.getInUseLnbHandles()) {
            // link lnb handle to the new profile
            newOwnerProfile.useLnb(lnbHandle);

            // set new owner Id
            LnbResource lnb = getLnbResource(lnbHandle);
            lnb.setOwner(newOwnerId);

            inUseLnbHandles.add(lnbHandle);
        }

        // unlink lnb handles from the original owner
        for (Integer lnbHandle : inUseLnbHandles) {
            currentOwnerProfile.releaseLnb(lnbHandle);
        }

        return true;
    }

    @VisibleForTesting
    protected boolean transferOwnerInternal(int resourceType, int currentOwnerId, int newOwnerId) {
        switch (resourceType) {
            case TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND:
                return transferFeOwner(currentOwnerId, newOwnerId);
            case TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND_CICAM:
                return transferFeCiCamOwner(currentOwnerId, newOwnerId);
            case TunerResourceManager.TUNER_RESOURCE_TYPE_LNB:
                return transferLnbOwner(currentOwnerId, newOwnerId);
            default:
                Slog.e(TAG, "transferOwnerInternal. unsupported resourceType: " + resourceType);
                return false;
        }
    }

    @VisibleForTesting
    protected boolean requestLnbInternal(TunerLnbRequest request, int[] lnbHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestLnb(request=" + request + ")");
        }

        lnbHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        clientPriorityUpdateOnRequest(requestClient);
        int grantingLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        int inUseLowestPriorityLnbHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
        boolean isRequestFromSameProcess = false;
        for (LnbResource lnb : getLnbResources().values()) {
            if (!lnb.isInUse()) {
                // Grant the unused lnb with lower handle first
                grantingLnbHandle = lnb.getHandle();
                break;
            } else {
                // Record the lnb id with the lowest client priority among all the
                // in use lnb when no available lnb has been found.
                int priority = updateAndGetOwnerClientPriority(lnb.getOwnerClientId());
                if (currentLowestPriority > priority) {
                    inUseLowestPriorityLnbHandle = lnb.getHandle();
                    currentLowestPriority = priority;
                    isRequestFromSameProcess = (requestClient.getProcessId()
                        == (getClientProfile(lnb.getOwnerClientId())).getProcessId());
                }
            }
        }

        // Grant Lnb when there is unused resource.
        if (grantingLnbHandle > -1) {
            lnbHandle[0] = grantingLnbHandle;
            updateLnbClientMappingOnNewGrant(grantingLnbHandle, request.clientId);
            return true;
        }

        // When all the resources are occupied, grant the lowest priority resource if the
        // request client has higher priority.
        if (inUseLowestPriorityLnbHandle > TunerResourceManager.INVALID_RESOURCE_HANDLE
            && ((requestClient.getPriority() > currentLowestPriority) || (
            (requestClient.getPriority() == currentLowestPriority) && isRequestFromSameProcess))) {
            if (!reclaimResource(getLnbResource(inUseLowestPriorityLnbHandle).getOwnerClientId(),
                    TunerResourceManager.TUNER_RESOURCE_TYPE_LNB)) {
                return false;
            }
            lnbHandle[0] = inUseLowestPriorityLnbHandle;
            updateLnbClientMappingOnNewGrant(inUseLowestPriorityLnbHandle, request.clientId);
            return true;
        }

        return false;
    }

    @VisibleForTesting
    protected boolean requestCasSessionInternal(CasSessionRequest request, int[] casSessionHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestCasSession(request=" + request + ")");
        }
        CasResource cas = getCasResource(request.casSystemId);
        // Unregistered Cas System is treated as having unlimited sessions.
        if (cas == null) {
            cas = new CasResource.Builder(request.casSystemId)
                                 .maxSessionNum(Integer.MAX_VALUE)
                                 .build();
            addCasResource(cas);
        }
        casSessionHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        clientPriorityUpdateOnRequest(requestClient);
        int lowestPriorityOwnerId = -1;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
        boolean isRequestFromSameProcess = false;
        if (!cas.isFullyUsed()) {
            casSessionHandle[0] = generateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_CAS_SESSION, cas.getSystemId());
            updateCasClientMappingOnNewGrant(request.casSystemId, request.clientId);
            return true;
        }
        for (int ownerId : cas.getOwnerClientIds()) {
            // Record the client id with lowest priority that is using the current Cas system.
            int priority = updateAndGetOwnerClientPriority(ownerId);
            if (currentLowestPriority > priority) {
                lowestPriorityOwnerId = ownerId;
                currentLowestPriority = priority;
                isRequestFromSameProcess = (requestClient.getProcessId()
                    == (getClientProfile(ownerId)).getProcessId());
            }
        }

        // When all the Cas sessions are occupied, reclaim the lowest priority client if the
        // request client has higher priority.
        if (lowestPriorityOwnerId > -1 && ((requestClient.getPriority() > currentLowestPriority)
        || ((requestClient.getPriority() == currentLowestPriority) && isRequestFromSameProcess))) {
            if (!reclaimResource(lowestPriorityOwnerId,
                    TunerResourceManager.TUNER_RESOURCE_TYPE_CAS_SESSION)) {
                return false;
            }
            casSessionHandle[0] = generateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_CAS_SESSION, cas.getSystemId());
            updateCasClientMappingOnNewGrant(request.casSystemId, request.clientId);
            return true;
        }
        return false;
    }

    @VisibleForTesting
    protected boolean requestCiCamInternal(TunerCiCamRequest request, int[] ciCamHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestCiCamInternal(TunerCiCamRequest=" + request + ")");
        }
        CiCamResource ciCam = getCiCamResource(request.ciCamId);
        // Unregistered Cas System is treated as having unlimited sessions.
        if (ciCam == null) {
            ciCam = new CiCamResource.Builder(request.ciCamId)
                                     .maxSessionNum(Integer.MAX_VALUE)
                                     .build();
            addCiCamResource(ciCam);
        }
        ciCamHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);
        clientPriorityUpdateOnRequest(requestClient);
        int lowestPriorityOwnerId = -1;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
        boolean isRequestFromSameProcess = false;
        if (!ciCam.isFullyUsed()) {
            ciCamHandle[0] = generateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND_CICAM, ciCam.getCiCamId());
            updateCiCamClientMappingOnNewGrant(request.ciCamId, request.clientId);
            return true;
        }
        for (int ownerId : ciCam.getOwnerClientIds()) {
            // Record the client id with lowest priority that is using the current Cas system.
            int priority = updateAndGetOwnerClientPriority(ownerId);
            if (currentLowestPriority > priority) {
                lowestPriorityOwnerId = ownerId;
                currentLowestPriority = priority;
                isRequestFromSameProcess = (requestClient.getProcessId()
                    == (getClientProfile(ownerId)).getProcessId());
            }
        }

        // When all the CiCam sessions are occupied, reclaim the lowest priority client if the
        // request client has higher priority.
        if (lowestPriorityOwnerId > -1 && ((requestClient.getPriority() > currentLowestPriority)
            || ((requestClient.getPriority() == currentLowestPriority)
                && isRequestFromSameProcess))) {
            if (!reclaimResource(lowestPriorityOwnerId,
                    TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND_CICAM)) {
                return false;
            }
            ciCamHandle[0] = generateResourceHandle(
                    TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND_CICAM, ciCam.getCiCamId());
            updateCiCamClientMappingOnNewGrant(request.ciCamId, request.clientId);
            return true;
        }
        return false;
    }

    @VisibleForTesting
    protected boolean isHigherPriorityInternal(ResourceClientProfile challengerProfile,
            ResourceClientProfile holderProfile) {
        if (DEBUG) {
            Slog.d(TAG,
                    "isHigherPriority(challengerProfile=" + challengerProfile
                            + ", holderProfile=" + challengerProfile + ")");
        }
        if (mTvInputManager == null) {
            Slog.e(TAG, "TvInputManager is null. Can't compare the priority.");
            // Allow the client to acquire the hardware interface
            // when the TRM is not able to compare the priority.
            return true;
        }

        int challengerPid = challengerProfile.tvInputSessionId == null
                ? Binder.getCallingPid() /*callingPid*/
                : mTvInputManager.getClientPid(challengerProfile.tvInputSessionId); /*tvAppId*/
        int holderPid = holderProfile.tvInputSessionId == null
                ? Binder.getCallingPid() /*callingPid*/
                : mTvInputManager.getClientPid(holderProfile.tvInputSessionId); /*tvAppId*/

        int challengerPriority = getClientPriority(
                challengerProfile.useCase, checkIsForeground(challengerPid));
        int holderPriority = getClientPriority(holderProfile.useCase, checkIsForeground(holderPid));
        return challengerPriority > holderPriority;
    }

    @VisibleForTesting
    protected void releaseFrontendInternal(FrontendResource fe, int clientId) {
        if (DEBUG) {
            Slog.d(TAG, "releaseFrontend(id=" + fe.getHandle() + ", clientId=" + clientId + " )");
        }
        if (clientId == fe.getOwnerClientId()) {
            ClientProfile ownerClient = getClientProfile(fe.getOwnerClientId());
            if (ownerClient != null) {
                for (int shareOwnerId : ownerClient.getShareFeClientIds()) {
                    reclaimResource(shareOwnerId,
                            TunerResourceManager.TUNER_RESOURCE_TYPE_FRONTEND);
                }
            }
        }
        clearFrontendAndClientMapping(getClientProfile(clientId));
    }

    @VisibleForTesting
    protected void releaseDemuxInternal(DemuxResource demux) {
        if (DEBUG) {
            Slog.d(TAG, "releaseDemux(DemuxHandle=" + demux.getHandle() + ")");
        }
        updateDemuxClientMappingOnRelease(demux);
    }

    @VisibleForTesting
    protected void releaseLnbInternal(LnbResource lnb) {
        if (DEBUG) {
            Slog.d(TAG, "releaseLnb(lnbHandle=" + lnb.getHandle() + ")");
        }
        updateLnbClientMappingOnRelease(lnb);
    }

    @VisibleForTesting
    protected void releaseCasSessionInternal(CasResource cas, int ownerClientId) {
        if (DEBUG) {
            Slog.d(TAG, "releaseCasSession(sessionResourceId=" + cas.getSystemId() + ")");
        }
        updateCasClientMappingOnRelease(cas, ownerClientId);
    }

    @VisibleForTesting
    protected void releaseCiCamInternal(CiCamResource ciCam, int ownerClientId) {
        if (DEBUG) {
            Slog.d(TAG, "releaseCiCamInternal(ciCamId=" + ciCam.getCiCamId() + ")");
        }
        updateCiCamClientMappingOnRelease(ciCam, ownerClientId);
    }

    @VisibleForTesting
    protected boolean requestDemuxInternal(TunerDemuxRequest request, int[] demuxHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestDemux(request=" + request + ")");
        }

        // For Tuner 2.0 and below or any HW constraint devices that are unable to support
        // ITuner.openDemuxById(), demux resources are not really managed under TRM and
        // mDemuxResources.size() will be zero
        if (mDemuxResources.size() == 0) {
            // There are enough Demux resources, so we don't manage Demux in R.
            demuxHandle[0] =
                    generateResourceHandle(TunerResourceManager.TUNER_RESOURCE_TYPE_DEMUX, 0);
            return true;
        }

        demuxHandle[0] = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        ClientProfile requestClient = getClientProfile(request.clientId);

        if (requestClient == null) {
            return false;
        }

        clientPriorityUpdateOnRequest(requestClient);
        int grantingDemuxHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        int inUseLowestPriorityDrHandle = TunerResourceManager.INVALID_RESOURCE_HANDLE;
        // Priority max value is 1000
        int currentLowestPriority = MAX_CLIENT_PRIORITY + 1;
        boolean isRequestFromSameProcess = false;
        // If the desired demux id was specified, we only need to check the demux.
        boolean hasDesiredDemuxCap = request.desiredFilterTypes
                != DemuxFilterMainType.UNDEFINED;
        int smallestNumOfSupportedCaps = Integer.SIZE + 1;
        int smallestNumOfSupportedCapsInUse = Integer.SIZE + 1;
        for (DemuxResource dr : getDemuxResources().values()) {
            if (!hasDesiredDemuxCap || dr.hasSufficientCaps(request.desiredFilterTypes)) {
                if (!dr.isInUse()) {
                    int numOfSupportedCaps = dr.getNumOfCaps();

                    // look for the demux with minimum caps supporting the desired caps
                    if (smallestNumOfSupportedCaps > numOfSupportedCaps) {
                        smallestNumOfSupportedCaps = numOfSupportedCaps;
                        grantingDemuxHandle = dr.getHandle();
                    }
                } else if (grantingDemuxHandle == TunerResourceManager.INVALID_RESOURCE_HANDLE) {
                    // Record the demux id with the lowest client priority among all the
                    // in use demuxes when no availabledemux has been found.
                    int priority = updateAndGetOwnerClientPriority(dr.getOwnerClientId());
                    if (currentLowestPriority >= priority) {
                        int numOfSupportedCaps = dr.getNumOfCaps();
                        boolean shouldUpdate = false;
                        // update lowest priority
                        if (currentLowestPriority > priority) {
                            currentLowestPriority = priority;
                            isRequestFromSameProcess = (requestClient.getProcessId()
                                == (getClientProfile(dr.getOwnerClientId())).getProcessId());

                            // reset the smallest caps when lower priority resource is found
                            smallestNumOfSupportedCapsInUse = numOfSupportedCaps;

                            shouldUpdate = true;
                        } else {
                            // This is the case when the priority is the same as previously found
                            // one. Update smallest caps when priority.
                            if (smallestNumOfSupportedCapsInUse > numOfSupportedCaps) {
                                smallestNumOfSupportedCapsInUse = numOfSupportedCaps;
                                shouldUpdate = true;
                            }
                        }
                        if (shouldUpdate) {
                            inUseLowestPriorityDrHandle = dr.getHandle();
                        }
                    }
                }
            }
        }

        // Grant demux when there is unused resource.
        if (grantingDemuxHandle != TunerResourceManager.INVALID_RESOURCE_HANDLE) {
            demuxHandle[0] = grantingDemuxHandle;
            updateDemuxClientMappingOnNewGrant(grantingDemuxHandle, request.clientId);
            return true;
        }

        // When all the resources are occupied, grant the lowest priority resource if the
        // request client has higher priority.
        if (inUseLowestPriorityDrHandle != TunerResourceManager.INVALID_RESOURCE_HANDLE
            && ((requestClient.getPriority() > currentLowestPriority) || (
            (requestClient.getPriority() == currentLowestPriority) && isRequestFromSameProcess))) {
            if (!reclaimResource(
                    getDemuxResource(inUseLowestPriorityDrHandle).getOwnerClientId(),
                    TunerResourceManager.TUNER_RESOURCE_TYPE_DEMUX)) {
                return false;
            }
            demuxHandle[0] = inUseLowestPriorityDrHandle;
            updateDemuxClientMappingOnNewGrant(
                    inUseLowestPriorityDrHandle, request.clientId);
            return true;
        }

        return false;
    }

    @VisibleForTesting
    // This mothod is to sync up the request/holder client's foreground/background status and update
    // the client priority accordingly whenever a new resource request comes in.
    protected void clientPriorityUpdateOnRequest(ClientProfile profile) {
        if (profile.isPriorityOverwritten()) {
            // To avoid overriding the priority set through updateClientPriority API.
            return;
        }
        int pid = profile.getProcessId();
        boolean currentIsForeground = checkIsForeground(pid);
        profile.setPriority(
                getClientPriority(profile.getUseCase(), currentIsForeground));
    }

    @VisibleForTesting
    protected boolean requestDescramblerInternal(
            TunerDescramblerRequest request, int[] descramblerHandle) {
        if (DEBUG) {
            Slog.d(TAG, "requestDescrambler(request=" + request + ")");
        }
        // There are enough Descrambler resources, so we don't manage Descrambler in R.
        descramblerHandle[0] =
                generateResourceHandle(TunerResourceManager.TUNER_RESOURCE_TYPE_DESCRAMBLER, 0);
        return true;
    }

    // Return value is guaranteed to be positive
    private long getElapsedTime(long begin) {
        long now = SystemClock.uptimeMillis();
        long elapsed;
        if (now >= begin) {
            elapsed = now - begin;
        } else {
            elapsed = Long.MAX_VALUE - begin + now;
            if (elapsed < 0) {
                elapsed = Long.MAX_VALUE;
            }
        }
        return elapsed;
    }

    private boolean lockForTunerApiLock(int clientId, long timeoutMS, String callerFunction) {
        try {
            if (mLockForTRMSLock.tryLock(timeoutMS, TimeUnit.MILLISECONDS)) {
                return true;
            } else {
                Slog.e(TAG, "FAILED to lock mLockForTRMSLock in " + callerFunction
                        + ", clientId:" + clientId + ", timeoutMS:" + timeoutMS
                        + ", mTunerApiLockHolder:" + mTunerApiLockHolder);
                return false;
            }
        } catch (InterruptedException ie) {
            Slog.e(TAG, "exception thrown in " + callerFunction + ":" + ie);
            if (mLockForTRMSLock.isHeldByCurrentThread()) {
                mLockForTRMSLock.unlock();
            }
            return false;
        }
    }

    private boolean acquireLockInternal(int clientId, long clientThreadId, long timeoutMS) {
        long begin = SystemClock.uptimeMillis();

        // Grab lock
        if (!lockForTunerApiLock(clientId, timeoutMS, "acquireLockInternal()")) {
            return false;
        }

        try {
            boolean available = mTunerApiLockHolder == INVALID_CLIENT_ID;
            boolean nestedSelf = (clientId == mTunerApiLockHolder)
                    && (clientThreadId == mTunerApiLockHolderThreadId);
            boolean recovery = false;

            // Allow same thread to grab the lock multiple times
            while (!available && !nestedSelf) {
                // calculate how much time is left before timeout
                long leftOverMS = timeoutMS - getElapsedTime(begin);
                if (leftOverMS <= 0) {
                    Slog.e(TAG, "FAILED:acquireLockInternal(" + clientId + ", " + clientThreadId
                            + ", " + timeoutMS + ") - timed out, but will grant the lock to "
                            + "the callee by stealing it from the current holder:"
                            + mTunerApiLockHolder + "(" + mTunerApiLockHolderThreadId + "), "
                            + "who likely failed to call releaseLock(), "
                            + "to prevent this from becoming an unrecoverable error");
                    // This should not normally happen, but there sometimes are cases where
                    // in-flight tuner API execution gets scheduled even after binderDied(),
                    // which can leave the in-flight execution dissappear/stopped in between
                    // acquireLock and releaseLock
                    recovery = true;
                    break;
                }

                // Cond wait for left over time
                mTunerApiLockReleasedCV.await(leftOverMS, TimeUnit.MILLISECONDS);

                // Check the availability for "spurious wakeup"
                // The case that was confirmed is that someone else can acquire this in between
                // signal() and wakup from the above await()
                available = mTunerApiLockHolder == INVALID_CLIENT_ID;

                if (!available) {
                    Slog.w(TAG, "acquireLockInternal(" + clientId + ", " + clientThreadId + ", "
                            + timeoutMS + ") - woken up from cond wait, but " + mTunerApiLockHolder
                            + "(" + mTunerApiLockHolderThreadId + ") is already holding the lock. "
                            + "Going to wait again if timeout hasn't reached yet");
                }
            }

            // Will always grant unless exception is thrown (or lock is already held)
            if (available || recovery) {
                if (DEBUG) {
                    Slog.d(TAG, "SUCCESS:acquireLockInternal(" + clientId + ", " + clientThreadId
                            + ", " + timeoutMS + ")");
                }

                if (mTunerApiLockNestedCount != 0) {
                    Slog.w(TAG, "Something is wrong as nestedCount(" + mTunerApiLockNestedCount
                            + ") is not zero. Will overriding it to 1 anyways");
                }

                // set the caller to be the holder
                mTunerApiLockHolder = clientId;
                mTunerApiLockHolderThreadId = clientThreadId;
                mTunerApiLockNestedCount = 1;
            } else if (nestedSelf) {
                // Increment the nested count so releaseLockInternal won't signal prematuredly
                mTunerApiLockNestedCount++;
                if (DEBUG) {
                    Slog.d(TAG, "acquireLockInternal(" + clientId + ", " + clientThreadId
                            + ", " + timeoutMS + ") - nested count incremented to "
                            + mTunerApiLockNestedCount);
                }
            } else {
                Slog.e(TAG, "acquireLockInternal(" + clientId + ", " + clientThreadId
                        + ", " + timeoutMS + ") - should not reach here");
            }
            // return true in "recovery" so callee knows that the deadlock is possible
            // only when the return value is false
            return (available || nestedSelf || recovery);
        } catch (InterruptedException ie) {
            Slog.e(TAG, "exception thrown in acquireLockInternal(" + clientId + ", "
                    + clientThreadId + ", " + timeoutMS + "):" + ie);
            return false;
        } finally {
            if (mLockForTRMSLock.isHeldByCurrentThread()) {
                mLockForTRMSLock.unlock();
            }
        }
    }

    private boolean releaseLockInternal(int clientId, long timeoutMS,
            boolean ignoreNestedCount, boolean suppressError) {
        // Grab lock first
        if (!lockForTunerApiLock(clientId, timeoutMS, "releaseLockInternal()")) {
            return false;
        }

        try {
            if (mTunerApiLockHolder == clientId) {
                // Should always reach here unless called from binderDied()
                mTunerApiLockNestedCount--;
                if (ignoreNestedCount || mTunerApiLockNestedCount <= 0) {
                    if (DEBUG) {
                        Slog.d(TAG, "SUCCESS:releaseLockInternal(" + clientId + ", " + timeoutMS
                                + ", " + ignoreNestedCount + ", " + suppressError
                                + ") - signaling!");
                    }
                    // Reset the current holder and signal
                    mTunerApiLockHolder = INVALID_CLIENT_ID;
                    mTunerApiLockHolderThreadId = INVALID_THREAD_ID;
                    mTunerApiLockNestedCount = 0;
                    mTunerApiLockReleasedCV.signal();
                } else {
                    if (DEBUG) {
                        Slog.d(TAG, "releaseLockInternal(" + clientId + ", " + timeoutMS
                                + ", " + ignoreNestedCount + ", " + suppressError
                                + ") - NOT signaling because nested count is not zero ("
                                + mTunerApiLockNestedCount + ")");
                    }
                }
                return true;
            } else if (mTunerApiLockHolder == INVALID_CLIENT_ID) {
                if (!suppressError) {
                    Slog.w(TAG, "releaseLockInternal(" + clientId + ", " + timeoutMS
                            + ") - called while there is no current holder");
                }
                // No need to do anything.
                // Shouldn't reach here unless called from binderDied()
                return false;
            } else {
                if (!suppressError) {
                    Slog.e(TAG, "releaseLockInternal(" + clientId + ", " + timeoutMS
                            + ") - called while someone else:" + mTunerApiLockHolder
                            + "is the current holder");
                }
                // Cannot reset the holder Id because it reaches here when called
                // from binderDied()
                return false;
            }
        } finally {
            if (mLockForTRMSLock.isHeldByCurrentThread()) {
                mLockForTRMSLock.unlock();
            }
        }
    }

    @VisibleForTesting
    protected class ResourcesReclaimListenerRecord implements IBinder.DeathRecipient {
        private final IResourcesReclaimListener mListener;
        private final int mClientId;

        public ResourcesReclaimListenerRecord(IResourcesReclaimListener listener, int clientId) {
            mListener = listener;
            mClientId = clientId;
        }

        @Override
        public void binderDied() {
            try {
                synchronized (mLock) {
                    if (checkClientExists(mClientId)) {
                        removeClientProfile(mClientId);
                    }
                }
            } finally {
                // reset the tuner API lock
                releaseLockInternal(mClientId, TRMS_LOCK_TIMEOUT, true, true);
            }
        }

        public int getId() {
            return mClientId;
        }

        public IResourcesReclaimListener getListener() {
            return mListener;
        }
    }

    private void addResourcesReclaimListener(int clientId, IResourcesReclaimListener listener) {
        if (listener == null) {
            if (DEBUG) {
                Slog.w(TAG, "Listener is null when client " + clientId + " registered!");
            }
            return;
        }

        ResourcesReclaimListenerRecord record =
                new ResourcesReclaimListenerRecord(listener, clientId);

        try {
            listener.asBinder().linkToDeath(record, 0);
        } catch (RemoteException e) {
            Slog.w(TAG, "Listener already died.");
            return;
        }

        mListeners.put(clientId, record);
    }

    @VisibleForTesting
    protected boolean reclaimResource(int reclaimingClientId,
            @TunerResourceManager.TunerResourceType int resourceType) {

        // Allowing this because:
        // 1) serialization of resource reclaim is required in the current design
        // 2) the outgoing transaction is handled by the system app (with
        //    android.Manifest.permission.TUNER_RESOURCE_ACCESS), which goes through full
        //    Google certification
        Binder.allowBlockingForCurrentThread();

        // Reclaim all the resources of the share owners of the frontend that is used by the current
        // resource reclaimed client.
        ClientProfile profile = getClientProfile(reclaimingClientId);
        // TODO: check if this check is really needed.
        if (profile == null) {
            return true;
        }
        Set<Integer> shareFeClientIds = profile.getShareFeClientIds();
        for (int clientId : shareFeClientIds) {
            try {
                mListeners.get(clientId).getListener().onReclaimResources();
            } catch (RemoteException e) {
                Slog.e(TAG, "Failed to reclaim resources on client " + clientId, e);
                return false;
            }
            clearAllResourcesAndClientMapping(getClientProfile(clientId));
        }

        if (DEBUG) {
            Slog.d(TAG, "Reclaiming resources because higher priority client request resource type "
                    + resourceType + ", clientId:" + reclaimingClientId);
        }
        try {
            mListeners.get(reclaimingClientId).getListener().onReclaimResources();
        } catch (RemoteException e) {
            Slog.e(TAG, "Failed to reclaim resources on client " + reclaimingClientId, e);
            return false;
        }
        clearAllResourcesAndClientMapping(profile);
        return true;
    }

    @VisibleForTesting
    protected int getClientPriority(int useCase, boolean isForeground) {
        if (DEBUG) {
            Slog.d(TAG, "getClientPriority useCase=" + useCase
                    + ", isForeground=" + isForeground + ")");
        }

        if (isForeground) {
            return mPriorityCongfig.getForegroundPriority(useCase);
        }
        return mPriorityCongfig.getBackgroundPriority(useCase);
    }

    @VisibleForTesting
    protected boolean checkIsForeground(int pid) {
        if (mActivityManager == null) {
            return false;
        }
        List<RunningAppProcessInfo> appProcesses = mActivityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }
        for (RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.pid == pid
                    && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }
        return false;
    }

    private void updateFrontendClientMappingOnNewGrant(int grantingHandle, int ownerClientId) {
        FrontendResource grantingFrontend = getFrontendResource(grantingHandle);
        ClientProfile ownerProfile = getClientProfile(ownerClientId);
        grantingFrontend.setOwner(ownerClientId);
        increFrontendNum(mFrontendUsedNums, grantingFrontend.getType());
        ownerProfile.useFrontend(grantingHandle);
        for (int exclusiveGroupMember : grantingFrontend.getExclusiveGroupMemberFeHandles()) {
            getFrontendResource(exclusiveGroupMember).setOwner(ownerClientId);
            ownerProfile.useFrontend(exclusiveGroupMember);
        }
        ownerProfile.setPrimaryFrontend(grantingHandle);
    }

    private void updateDemuxClientMappingOnNewGrant(int grantingHandle, int ownerClientId) {
        DemuxResource grantingDemux = getDemuxResource(grantingHandle);
        if (grantingDemux != null) {
            ClientProfile ownerProfile = getClientProfile(ownerClientId);
            grantingDemux.setOwner(ownerClientId);
            ownerProfile.useDemux(grantingHandle);
        }
    }

    private void updateDemuxClientMappingOnRelease(@NonNull DemuxResource releasingDemux) {
        ClientProfile ownerProfile = getClientProfile(releasingDemux.getOwnerClientId());
        releasingDemux.removeOwner();
        ownerProfile.releaseDemux(releasingDemux.getHandle());
    }

    private void updateLnbClientMappingOnNewGrant(int grantingHandle, int ownerClientId) {
        LnbResource grantingLnb = getLnbResource(grantingHandle);
        ClientProfile ownerProfile = getClientProfile(ownerClientId);
        grantingLnb.setOwner(ownerClientId);
        ownerProfile.useLnb(grantingHandle);
    }

    private void updateLnbClientMappingOnRelease(@NonNull LnbResource releasingLnb) {
        ClientProfile ownerProfile = getClientProfile(releasingLnb.getOwnerClientId());
        releasingLnb.removeOwner();
        ownerProfile.releaseLnb(releasingLnb.getHandle());
    }

    private void updateCasClientMappingOnNewGrant(int grantingId, int ownerClientId) {
        CasResource grantingCas = getCasResource(grantingId);
        ClientProfile ownerProfile = getClientProfile(ownerClientId);
        grantingCas.setOwner(ownerClientId);
        ownerProfile.useCas(grantingId);
    }

    private void updateCiCamClientMappingOnNewGrant(int grantingId, int ownerClientId) {
        CiCamResource grantingCiCam = getCiCamResource(grantingId);
        ClientProfile ownerProfile = getClientProfile(ownerClientId);
        grantingCiCam.setOwner(ownerClientId);
        ownerProfile.useCiCam(grantingId);
    }

    private void updateCasClientMappingOnRelease(@NonNull CasResource cas, int ownerClientId) {
        cas.removeSession(ownerClientId);
        if (!cas.hasOpenSessions(ownerClientId)) {
            ClientProfile ownerProfile = getClientProfile(ownerClientId);
            cas.removeOwner(ownerClientId);
            ownerProfile.releaseCas();
        }
    }

    private void updateCiCamClientMappingOnRelease(
            @NonNull CiCamResource releasingCiCam, int ownerClientId) {
        ClientProfile ownerProfile = getClientProfile(ownerClientId);
        releasingCiCam.removeOwner(ownerClientId);
        ownerProfile.releaseCiCam();
    }

    /**
     * Update and get the owner client's priority.
     *
     * @param clientId the owner client id.
     * @return the priority of the owner client.
     */
    private int updateAndGetOwnerClientPriority(int clientId) {
        ClientProfile profile = getClientProfile(clientId);
        clientPriorityUpdateOnRequest(profile);
        return profile.getPriority();
    }

    /**
     * Update the owner and sharee clients' priority and get the highest priority
     * for frontend resource
     *
     * @param clientId the owner client id.
     * @return the highest priority among all the clients holding the same frontend resource.
     */
    private int getFrontendHighestClientPriority(int clientId) {
        // Check if the owner profile exists
        ClientProfile ownerClient = getClientProfile(clientId);
        if (ownerClient == null) {
            return 0;
        }

        // Update and get the priority of the owner client
        int highestPriority = updateAndGetOwnerClientPriority(clientId);

        // Update and get all the client IDs of frontend resource holders
        for (int shareeId : ownerClient.getShareFeClientIds()) {
            int priority = updateAndGetOwnerClientPriority(shareeId);
            if (priority > highestPriority) {
                highestPriority = priority;
            }
        }
        return highestPriority;
    }

    @VisibleForTesting
    @Nullable
    protected FrontendResource getFrontendResource(int frontendHandle) {
        return mFrontendResources.get(frontendHandle);
    }

    @VisibleForTesting
    protected Map<Integer, FrontendResource> getFrontendResources() {
        return mFrontendResources;
    }

    @VisibleForTesting
    @Nullable
    protected DemuxResource getDemuxResource(int demuxHandle) {
        return mDemuxResources.get(demuxHandle);
    }

    @VisibleForTesting
    protected Map<Integer, DemuxResource> getDemuxResources() {
        return mDemuxResources;
    }

    private boolean setMaxNumberOfFrontendsInternal(int frontendType, int maxUsableNum) {
        int usedNum = mFrontendUsedNums.get(frontendType, INVALID_FE_COUNT);
        if (usedNum == INVALID_FE_COUNT || usedNum <= maxUsableNum) {
            mFrontendMaxUsableNums.put(frontendType, maxUsableNum);
            return true;
        } else {
            Slog.e(TAG, "max number of frontend for frontendType: " + frontendType
                    + " cannot be set to a value lower than the current usage count."
                    + " (requested max num = " + maxUsableNum + ", current usage = " + usedNum);
            return false;
        }
    }

    private int getMaxNumberOfFrontendsInternal(int frontendType) {
        int existingNum = mFrontendExistingNums.get(frontendType, INVALID_FE_COUNT);
        if (existingNum == INVALID_FE_COUNT) {
            Log.e(TAG, "existingNum is -1 for " + frontendType);
            return -1;
        }
        int maxUsableNum = mFrontendMaxUsableNums.get(frontendType, INVALID_FE_COUNT);
        if (maxUsableNum == INVALID_FE_COUNT) {
            return existingNum;
        } else {
            return maxUsableNum;
        }
    }

    private boolean isFrontendMaxNumUseReached(int frontendType) {
        int maxUsableNum = mFrontendMaxUsableNums.get(frontendType, INVALID_FE_COUNT);
        if (maxUsableNum == INVALID_FE_COUNT) {
            return false;
        }
        int useNum = mFrontendUsedNums.get(frontendType, INVALID_FE_COUNT);
        if (useNum == INVALID_FE_COUNT) {
            useNum = 0;
        }
        return useNum >= maxUsableNum;
    }

    private void increFrontendNum(SparseIntArray targetNums, int frontendType) {
        int num = targetNums.get(frontendType, INVALID_FE_COUNT);
        if (num == INVALID_FE_COUNT) {
            targetNums.put(frontendType, 1);
        } else {
            targetNums.put(frontendType, num + 1);
        }
    }

    private void decreFrontendNum(SparseIntArray targetNums, int frontendType) {
        int num = targetNums.get(frontendType, INVALID_FE_COUNT);
        if (num != INVALID_FE_COUNT) {
            targetNums.put(frontendType, num - 1);
        }
    }

    private void replaceFeResourceMap(Map<Integer, FrontendResource> srcMap, Map<Integer,
            FrontendResource> dstMap) {
        if (dstMap != null) {
            dstMap.clear();
            if (srcMap != null && srcMap.size() > 0) {
                dstMap.putAll(srcMap);
            }
        }
    }

    private void replaceFeCounts(SparseIntArray srcCounts, SparseIntArray dstCounts) {
        if (dstCounts != null) {
            dstCounts.clear();
            if (srcCounts != null) {
                for (int i = 0; i < srcCounts.size(); i++) {
                    dstCounts.put(srcCounts.keyAt(i), srcCounts.valueAt(i));
                }
            }
        }
    }
    private void dumpMap(Map<?, ?> targetMap, String headline, String delimiter,
            IndentingPrintWriter pw) {
        if (targetMap != null) {
            pw.println(headline);
            pw.increaseIndent();
            for (Map.Entry<?, ?> entry : targetMap.entrySet()) {
                pw.print(entry.getKey() + " : " + entry.getValue());
                pw.print(delimiter);
            }
            pw.println();
            pw.decreaseIndent();
        }
    }

    private void dumpSIA(SparseIntArray array, String headline, String delimiter,
            IndentingPrintWriter pw) {
        if (array != null) {
            pw.println(headline);
            pw.increaseIndent();
            for (int i = 0; i < array.size(); i++) {
                pw.print(array.keyAt(i) + " : " + array.valueAt(i));
                pw.print(delimiter);
            }
            pw.println();
            pw.decreaseIndent();
        }
    }

    private void addFrontendResource(FrontendResource newFe) {
        // Update the exclusive group member list in all the existing Frontend resource
        for (FrontendResource fe : getFrontendResources().values()) {
            if (fe.getExclusiveGroupId() == newFe.getExclusiveGroupId()) {
                newFe.addExclusiveGroupMemberFeHandle(fe.getHandle());
                newFe.addExclusiveGroupMemberFeHandles(fe.getExclusiveGroupMemberFeHandles());
                for (int excGroupmemberFeHandle : fe.getExclusiveGroupMemberFeHandles()) {
                    getFrontendResource(excGroupmemberFeHandle)
                            .addExclusiveGroupMemberFeHandle(newFe.getHandle());
                }
                fe.addExclusiveGroupMemberFeHandle(newFe.getHandle());
                break;
            }
        }
        // Update resource list and available id list
        mFrontendResources.put(newFe.getHandle(), newFe);
        increFrontendNum(mFrontendExistingNums, newFe.getType());

    }

    private void addDemuxResource(DemuxResource newDemux) {
        mDemuxResources.put(newDemux.getHandle(), newDemux);
    }

    private void removeFrontendResource(int removingHandle) {
        FrontendResource fe = getFrontendResource(removingHandle);
        if (fe == null) {
            return;
        }
        if (fe.isInUse()) {
            ClientProfile ownerClient = getClientProfile(fe.getOwnerClientId());
            for (int shareOwnerId : ownerClient.getShareFeClientIds()) {
                clearFrontendAndClientMapping(getClientProfile(shareOwnerId));
            }
            clearFrontendAndClientMapping(ownerClient);
        }
        for (int excGroupmemberFeHandle : fe.getExclusiveGroupMemberFeHandles()) {
            getFrontendResource(excGroupmemberFeHandle)
                    .removeExclusiveGroupMemberFeId(fe.getHandle());
        }
        decreFrontendNum(mFrontendExistingNums, fe.getType());
        mFrontendResources.remove(removingHandle);
    }

    private void removeDemuxResource(int removingHandle) {
        DemuxResource demux = getDemuxResource(removingHandle);
        if (demux == null) {
            return;
        }
        if (demux.isInUse()) {
            releaseDemuxInternal(demux);
        }
        mDemuxResources.remove(removingHandle);
    }

    @VisibleForTesting
    @Nullable
    protected LnbResource getLnbResource(int lnbHandle) {
        return mLnbResources.get(lnbHandle);
    }

    @VisibleForTesting
    protected Map<Integer, LnbResource> getLnbResources() {
        return mLnbResources;
    }

    private void addLnbResource(LnbResource newLnb) {
        // Update resource list and available id list
        mLnbResources.put(newLnb.getHandle(), newLnb);
    }

    private void removeLnbResource(int removingHandle) {
        LnbResource lnb = getLnbResource(removingHandle);
        if (lnb == null) {
            return;
        }
        if (lnb.isInUse()) {
            releaseLnbInternal(lnb);
        }
        mLnbResources.remove(removingHandle);
    }

    @VisibleForTesting
    @Nullable
    protected CasResource getCasResource(int systemId) {
        return mCasResources.get(systemId);
    }

    @VisibleForTesting
    @Nullable
    protected CiCamResource getCiCamResource(int ciCamId) {
        return mCiCamResources.get(ciCamId);
    }

    @VisibleForTesting
    protected Map<Integer, CasResource> getCasResources() {
        return mCasResources;
    }

    @VisibleForTesting
    protected Map<Integer, CiCamResource> getCiCamResources() {
        return mCiCamResources;
    }

    private void addCasResource(CasResource newCas) {
        // Update resource list and available id list
        mCasResources.put(newCas.getSystemId(), newCas);
    }

    private void addCiCamResource(CiCamResource newCiCam) {
        // Update resource list and available id list
        mCiCamResources.put(newCiCam.getCiCamId(), newCiCam);
    }

    private void removeCasResource(int removingId) {
        CasResource cas = getCasResource(removingId);
        if (cas == null) {
            return;
        }
        for (int ownerId : cas.getOwnerClientIds()) {
            getClientProfile(ownerId).releaseCas();
        }
        mCasResources.remove(removingId);
    }

    private void removeCiCamResource(int removingId) {
        CiCamResource ciCam = getCiCamResource(removingId);
        if (ciCam == null) {
            return;
        }
        for (int ownerId : ciCam.getOwnerClientIds()) {
            getClientProfile(ownerId).releaseCiCam();
        }
        mCiCamResources.remove(removingId);
    }

    private void releaseLowerPriorityClientCasResources(int releasingCasResourceNum) {
        // TODO: Sort with a treemap

        // select the first num client to release
    }

    @VisibleForTesting
    @Nullable
    protected ClientProfile getClientProfile(int clientId) {
        return mClientProfiles.get(clientId);
    }

    private void addClientProfile(int clientId, ClientProfile profile,
            IResourcesReclaimListener listener) {
        mClientProfiles.put(clientId, profile);
        addResourcesReclaimListener(clientId, listener);
    }

    private void removeClientProfile(int clientId) {
        for (int shareOwnerId : getClientProfile(clientId).getShareFeClientIds()) {
            clearFrontendAndClientMapping(getClientProfile(shareOwnerId));
        }
        clearAllResourcesAndClientMapping(getClientProfile(clientId));
        mClientProfiles.remove(clientId);

        // it may be called by unregisterClientProfileInternal under test
        synchronized (mLock) {
            ResourcesReclaimListenerRecord record = mListeners.remove(clientId);
            if (record != null) {
                record.getListener().asBinder().unlinkToDeath(record, 0);
            }            
        }
    }

    private void clearFrontendAndClientMapping(ClientProfile profile) {
        // TODO: check if this check is really needed
        if (profile == null) {
            return;
        }
        for (Integer feId : profile.getInUseFrontendHandles()) {
            FrontendResource fe = getFrontendResource(feId);
            int ownerClientId = fe.getOwnerClientId();
            if (ownerClientId == profile.getId()) {
                fe.removeOwner();
                continue;
            }
            ClientProfile ownerClientProfile = getClientProfile(ownerClientId);
            if (ownerClientProfile != null) {
                ownerClientProfile.stopSharingFrontend(profile.getId());
            }

        }

        int primaryFeId = profile.getPrimaryFrontend();
        if (primaryFeId != TunerResourceManager.INVALID_RESOURCE_HANDLE) {
            FrontendResource primaryFe = getFrontendResource(primaryFeId);
            if (primaryFe != null) {
                decreFrontendNum(mFrontendUsedNums, primaryFe.getType());
            }
        }

        profile.releaseFrontend();
    }

    private void clearAllResourcesAndClientMapping(ClientProfile profile) {
        // TODO: check if this check is really needed. Maybe needed for reclaimResource path.
        if (profile == null) {
            return;
        }
        // Clear Lnb
        for (Integer lnbHandle : profile.getInUseLnbHandles()) {
            getLnbResource(lnbHandle).removeOwner();
        }
        // Clear Cas
        if (profile.getInUseCasSystemId() != ClientProfile.INVALID_RESOURCE_ID) {
            getCasResource(profile.getInUseCasSystemId()).removeOwner(profile.getId());
        }
        // Clear CiCam
        if (profile.getInUseCiCamId() != ClientProfile.INVALID_RESOURCE_ID) {
            getCiCamResource(profile.getInUseCiCamId()).removeOwner(profile.getId());
        }
        // Clear Demux
        for (Integer demuxHandle : profile.getInUseDemuxHandles()) {
            getDemuxResource(demuxHandle).removeOwner();
        }
        // Clear Frontend
        clearFrontendAndClientMapping(profile);
        profile.reclaimAllResources();
    }

    @VisibleForTesting
    protected boolean checkClientExists(int clientId) {
        return mClientProfiles.keySet().contains(clientId);
    }

    private int generateResourceHandle(
            @TunerResourceManager.TunerResourceType int resourceType, int resourceId) {
        return (resourceType & 0x000000ff) << 24
                | (resourceId << 16)
                | (mResourceRequestCount++ & 0xffff);
    }

    @VisibleForTesting
    protected int getResourceIdFromHandle(int resourceHandle) {
        if (resourceHandle == TunerResourceManager.INVALID_RESOURCE_HANDLE) {
            return resourceHandle;
        }
        return (resourceHandle & 0x00ff0000) >> 16;
    }

    private boolean validateResourceHandle(int resourceType, int resourceHandle) {
        if (resourceHandle == TunerResourceManager.INVALID_RESOURCE_HANDLE
                || ((resourceHandle & 0xff000000) >> 24) != resourceType) {
            return false;
        }
        return true;
    }

    private void enforceTrmAccessPermission(String apiName) {
        getContext().enforceCallingOrSelfPermission("android.permission.TUNER_RESOURCE_ACCESS",
                TAG + ": " + apiName);
    }

    private void enforceTunerAccessPermission(String apiName) {
        getContext().enforceCallingPermission("android.permission.ACCESS_TV_TUNER",
                TAG + ": " + apiName);
    }

    private void enforceDescramblerAccessPermission(String apiName) {
        getContext().enforceCallingPermission("android.permission.ACCESS_TV_DESCRAMBLER",
                TAG + ": " + apiName);
    }
}