aboutsummaryrefslogtreecommitdiff
path: root/shadows/framework/src/main/java/org/robolectric/shadows/ShadowArscAssetManager9.java
blob: e79fa2a9f2c0f6fb8176ecb8382a73141fcc5cee (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
package org.robolectric.shadows;

import static android.os.Build.VERSION_CODES.P;
import static android.os.Build.VERSION_CODES.Q;
import static android.os.Build.VERSION_CODES.R;
import static android.os.Build.VERSION_CODES.TIRAMISU;
import static org.robolectric.res.android.ApkAssetsCookie.K_INVALID_COOKIE;
import static org.robolectric.res.android.ApkAssetsCookie.kInvalidCookie;
import static org.robolectric.res.android.Asset.SEEK_CUR;
import static org.robolectric.res.android.Asset.SEEK_END;
import static org.robolectric.res.android.Asset.SEEK_SET;
import static org.robolectric.res.android.AttributeResolution9.ApplyStyle;
import static org.robolectric.res.android.AttributeResolution9.ResolveAttrs;
import static org.robolectric.res.android.AttributeResolution9.RetrieveAttributes;
import static org.robolectric.res.android.Errors.NO_ERROR;
import static org.robolectric.res.android.Registries.NATIVE_RES_XML_PARSERS;
import static org.robolectric.res.android.Registries.NATIVE_RES_XML_TREES;
import static org.robolectric.res.android.Util.ATRACE_NAME;
import static org.robolectric.res.android.Util.CHECK;
import static org.robolectric.res.android.Util.JNI_FALSE;
import static org.robolectric.res.android.Util.JNI_TRUE;
import static org.robolectric.res.android.Util.isTruthy;
import static org.robolectric.shadow.api.Shadow.invokeConstructor;
import static org.robolectric.util.reflector.Reflector.reflector;

import android.annotation.AnyRes;
import android.annotation.ArrayRes;
import android.annotation.AttrRes;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.StyleRes;
import android.content.res.ApkAssets;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Configuration.NativeConfig;
import android.os.Build;
import android.os.ParcelFileDescriptor;
import android.util.ArraySet;
import android.util.SparseArray;
import android.util.TypedValue;
import dalvik.system.VMRuntime;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.annotation.Resetter;
import org.robolectric.res.Fs;
import org.robolectric.res.android.ApkAssetsCookie;
import org.robolectric.res.android.Asset;
import org.robolectric.res.android.AssetDir;
import org.robolectric.res.android.AssetPath;
import org.robolectric.res.android.CppApkAssets;
import org.robolectric.res.android.CppAssetManager;
import org.robolectric.res.android.CppAssetManager2;
import org.robolectric.res.android.CppAssetManager2.ResolvedBag;
import org.robolectric.res.android.CppAssetManager2.ResourceName;
import org.robolectric.res.android.CppAssetManager2.Theme;
import org.robolectric.res.android.DynamicRefTable;
import org.robolectric.res.android.Ref;
import org.robolectric.res.android.Registries;
import org.robolectric.res.android.ResStringPool;
import org.robolectric.res.android.ResTable_config;
import org.robolectric.res.android.ResXMLParser;
import org.robolectric.res.android.ResXMLTree;
import org.robolectric.res.android.ResourceTypes.Res_value;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.PerfStatsCollector;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import org.robolectric.util.reflector.Direct;
import org.robolectric.util.reflector.ForType;
import org.robolectric.util.reflector.Static;
// transliterated from
// https://android.googlesource.com/platform/frameworks/base/+/android-9.0.0_r12/core/jni/android_util_AssetManager.cpp

@Implements(
    value = AssetManager.class,
    minSdk = Build.VERSION_CODES.P,
    shadowPicker = ShadowAssetManager.Picker.class)
@SuppressWarnings("NewApi")
public class ShadowArscAssetManager9 extends ShadowAssetManager.ArscBase {

  private static final int STYLE_NUM_ENTRIES = 6;
  private static final int STYLE_TYPE = 0;
  private static final int STYLE_DATA = 1;
  private static final int STYLE_ASSET_COOKIE = 2;
  private static final int STYLE_RESOURCE_ID = 3;
  private static final int STYLE_CHANGING_CONFIGURATIONS = 4;
  private static final int STYLE_DENSITY = 5;

  private static CppAssetManager2 systemCppAssetManager2;
  private static long systemCppAssetManager2Ref;
  private static boolean inNonSystemConstructor;
  private static ApkAssets[] cachedSystemApkAssets;
  private static ArraySet<ApkAssets> cachedSystemApkAssetsSet;

  @RealObject AssetManager realAssetManager;

  //  @RealObject  //  protected AssetManager realObject;

  // #define ATRACE_TAG ATRACE_TAG_RESOURCES
  // #define LOG_TAG "asset"
  //
  // #include <inttypes.h>
  // #include <linux/capability.h>
  // #include <stdio.h>
  // #include <sys/stat.h>
  // #include <sys/system_properties.h>
  // #include <sys/types.h>
  // #include <sys/wait.h>
  //
  // #include <private/android_filesystem_config.h> // for AID_SYSTEM
  //
  // #include "android-base/logging.h"
  // #include "android-base/properties.h"
  // #include "android-base/stringprintf.h"
  // #include "android_runtime/android_util_AssetManager.h"
  // #include "android_runtime/AndroidRuntime.h"
  // #include "android_util_Binder.h"
  // #include "androidfw/Asset.h"
  // #include "androidfw/AssetManager.h"
  // #include "androidfw/AssetManager2.h"
  // #include "androidfw/AttributeResolution.h"
  // #include "androidfw/MutexGuard.h"
  // #include "androidfw/ResourceTypes.h"
  // #include "core_jni_helpers.h"
  // #include "jni.h"
  // #include "nativehelper/JNIHelp.h"
  // #include "nativehelper/ScopedPrimitiveArray.h"
  // #include "nativehelper/ScopedStringChars.h"
  // #include "nativehelper/String.h"
  // #include "utils/Log.h"
  // #include "utils/misc.h"
  // #include "utils/String.h"
  // #include "utils/Trace.h"
  //
  // extern "C" int capget(cap_user_header_t hdrp, cap_user_data_t datap);
  // extern "C" int capset(cap_user_header_t hdrp, const cap_user_data_t datap);
  //
  // using ::android::base::StringPrintf;
  //
  // namespace android {
  //
  // // ----------------------------------------------------------------------------
  //

  // static class typedvalue_offsets_t {
  //   jfieldID mType;
  //   jfieldID mData;
  //   jfieldID mString;
  //   jfieldID mAssetCookie;
  //   jfieldID mResourceId;
  //   jfieldID mChangingConfigurations;
  //   jfieldID mDensity;
  // }
  // static final typedvalue_offsets_t gTypedValueOffsets = new typedvalue_offsets_t();
  //
  // static class assetfiledescriptor_offsets_t {
  //   jfieldID mFd;
  //   jfieldID mStartOffset;
  //   jfieldID mLength;
  // }
  // static final assetfiledescriptor_offsets_t gAssetFileDescriptorOffsets = new
  // assetfiledescriptor_offsets_t();
  //
  // static class assetmanager_offsets_t
  // {
  //   jfieldID mObject;
  // };
  // // This is also used by asset_manager.cpp.
  // static final assetmanager_offsets_t gAssetManagerOffsets = new assetmanager_offsets_t();
  //
  // static class apkassetsfields {
  //   jfieldID native_ptr;
  // }
  // static final apkassetsfields gApkAssetsFields = new apkassetsfields();
  //
  // static class sparsearray_offsets_t {
  //   jclass classObject;
  //   jmethodID constructor;
  //   jmethodID put;
  // }
  // static final sparsearray_offsets_t gSparseArrayOffsets = new sparsearray_offsets_t();
  //
  // static class configuration_offsets_t {
  //   jclass classObject;
  //   jmethodID constructor;
  //   jfieldID mSmallestScreenWidthDpOffset;
  //   jfieldID mScreenWidthDpOffset;
  //   jfieldID mScreenHeightDpOffset;
  // }
  // static final configuration_offsets_t gConfigurationOffsets = new configuration_offsets_t();
  //
  // jclass g_stringClass = nullptr;
  //
  // // ----------------------------------------------------------------------------

  @Implementation(maxSdk = Q)
  protected static void createSystemAssetsInZygoteLocked() {
    _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class);
    AssetManager sSystem = _assetManagerStatic_.getSystem();
    if (sSystem != null) {
      return;
    }

    if (systemCppAssetManager2 == null) {
      // first time! let the framework create a CppAssetManager2 and an AssetManager, which we'll
      // hang on to.
      reflector(AssetManagerReflector.class).createSystemAssetsInZygoteLocked();
      cachedSystemApkAssets = _assetManagerStatic_.getSystemApkAssets();
      cachedSystemApkAssetsSet = _assetManagerStatic_.getSystemApkAssetsSet();
    } else {
      // reuse the shared system CppAssetManager2; create a new AssetManager around it.
      _assetManagerStatic_.setSystemApkAssets(cachedSystemApkAssets);
      _assetManagerStatic_.setSystemApkAssetsSet(cachedSystemApkAssetsSet);

      sSystem =
          ReflectionHelpers.callConstructor(
              AssetManager.class, ClassParameter.from(boolean.class, true /*sentinel*/));
      sSystem.setApkAssets(cachedSystemApkAssets, false /*invalidateCaches*/);
      ReflectionHelpers.setStaticField(AssetManager.class, "sSystem", sSystem);
    }
  }

  @Resetter
  public static void reset() {
    // todo: ShadowPicker doesn't discriminate properly between concrete shadow classes for
    // resetters...
    if (!useLegacy() && RuntimeEnvironment.getApiLevel() >= P) {
      _AssetManager28_ _assetManagerStatic_ = reflector(_AssetManager28_.class);
      _assetManagerStatic_.setSystemApkAssetsSet(null);
      _assetManagerStatic_.setSystemApkAssets(null);
      _assetManagerStatic_.setSystem(null);
    }
  }

  // Java asset cookies have 0 as an invalid cookie, but TypedArray expects < 0.
  static int ApkAssetsCookieToJavaCookie(ApkAssetsCookie cookie) {
    return cookie.intValue() != kInvalidCookie ? (cookie.intValue() + 1) : -1;
  }

  static ApkAssetsCookie JavaCookieToApkAssetsCookie(int cookie) {
    return ApkAssetsCookie.forInt(cookie > 0 ? (cookie - 1) : kInvalidCookie);
  }

  // This is called by zygote (running as user root) as part of preloadResources.
  // static void NativeVerifySystemIdmaps(JNIEnv* /*env*/, jclass /*clazz*/) {
  @Implementation(minSdk = P, maxSdk = Q)
  protected static void nativeVerifySystemIdmaps() {
    return;

    // todo: maybe implement?
    // switch (pid_t pid = fork()) {
    //   case -1:
    //     PLOG(ERROR) << "failed to fork for idmap";
    //     break;
    //
    //   // child
    //   case 0: {
    //     struct __user_cap_header_struct capheader;
    //     struct __user_cap_data_struct capdata;
    //
    //     memset(&capheader, 0, sizeof(capheader));
    //     memset(&capdata, 0, sizeof(capdata));
    //
    //     capheader.version = _LINUX_CAPABILITY_VERSION;
    //     capheader.pid = 0;
    //
    //     if (capget(&capheader, &capdata) != 0) {
    //       PLOG(ERROR) << "capget";
    //       exit(1);
    //     }
    //
    //     capdata.effective = capdata.permitted;
    //     if (capset(&capheader, &capdata) != 0) {
    //       PLOG(ERROR) << "capset";
    //       exit(1);
    //     }
    //
    //     if (setgid(AID_SYSTEM) != 0) {
    //       PLOG(ERROR) << "setgid";
    //       exit(1);
    //     }
    //
    //     if (setuid(AID_SYSTEM) != 0) {
    //       PLOG(ERROR) << "setuid";
    //       exit(1);
    //     }
    //
    //     // Generic idmap parameters
    //     char* argv[8];
    //     int argc = 0;
    //     struct stat st;
    //
    //     memset(argv, 0, sizeof(argv));
    //     argv[argc++] = AssetManager.IDMAP_BIN;
    //     argv[argc++] = "--scan";
    //     argv[argc++] = AssetManager.TARGET_PACKAGE_NAME;
    //     argv[argc++] = AssetManager.TARGET_APK_PATH;
    //     argv[argc++] = AssetManager.IDMAP_DIR;
    //
    //     // Directories to scan for overlays: if OVERLAY_THEME_DIR_PROPERTY is defined,
    //     // use OVERLAY_DIR/<value of OVERLAY_THEME_DIR_PROPERTY> in addition to OVERLAY_DIR.
    //     String overlay_theme_path = base.GetProperty(AssetManager.OVERLAY_THEME_DIR_PROPERTY,
    //         "");
    //     if (!overlay_theme_path.empty()) {
    //       overlay_theme_path = String(AssetManager.OVERLAY_DIR) + "/" + overlay_theme_path;
    //       if (stat(overlay_theme_path, &st) == 0) {
    //         argv[argc++] = overlay_theme_path;
    //       }
    //     }
    //
    //     if (stat(AssetManager.OVERLAY_DIR, &st) == 0) {
    //       argv[argc++] = AssetManager.OVERLAY_DIR;
    //     }
    //
    //     if (stat(AssetManager.PRODUCT_OVERLAY_DIR, &st) == 0) {
    //       argv[argc++] = AssetManager.PRODUCT_OVERLAY_DIR;
    //     }
    //
    //     // Finally, invoke idmap (if any overlay directory exists)
    //     if (argc > 5) {
    //       execv(AssetManager.IDMAP_BIN, (char* const*)argv);
    //       PLOG(ERROR) << "failed to execv for idmap";
    //       exit(1); // should never get here
    //     } else {
    //       exit(0);
    //     }
    //   } break;
    //
    //   // parent
    //   default:
    //     waitpid(pid, null, 0);
    //     break;
    // }
  }

  @Implementation(minSdk = Build.VERSION_CODES.Q, maxSdk = Build.VERSION_CODES.R)
  protected static String[] nativeCreateIdmapsForStaticOverlaysTargetingAndroid() {
    return new String[0];
  }

  static int CopyValue(
      /*JNIEnv* env,*/ ApkAssetsCookie cookie,
      Res_value value,
      int ref,
      int type_spec_flags,
      ResTable_config config,
      TypedValue out_typed_value) {
    out_typed_value.type = value.dataType;
    out_typed_value.assetCookie = ApkAssetsCookieToJavaCookie(cookie);
    out_typed_value.data = value.data;
    out_typed_value.string = null;
    out_typed_value.resourceId = ref;
    out_typed_value.changingConfigurations = type_spec_flags;
    if (config != null) {
      out_typed_value.density = config.density;
    }
    return (int) (ApkAssetsCookieToJavaCookie(cookie));
  }

  //  @Override
  //  protected int addAssetPathNative(String path) {
  //    throw new UnsupportedOperationException(); // todo
  //  }

  @Override
  Collection<Path> getAllAssetDirs() {
    ApkAssets[] apkAssetsArray = reflector(_AssetManager28_.class, realAssetManager).getApkAssets();

    ArrayList<Path> assetDirs = new ArrayList<>();
    for (ApkAssets apkAssets : apkAssetsArray) {
      long apk_assets_native_ptr =
          ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
      CppApkAssets cppApkAssets =
          Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr);

      if (new File(cppApkAssets.GetPath()).isFile()) {
        assetDirs.add(Fs.forJar(Paths.get(cppApkAssets.GetPath())).getPath("assets"));
      } else {
        assetDirs.add(Paths.get(cppApkAssets.GetPath()));
      }
    }
    return assetDirs;
  }

  @Override
  List<AssetPath> getAssetPaths() {
    return AssetManagerForJavaObject(realAssetManager).getAssetPaths();
  }

  // ----------------------------------------------------------------------------

  // interface AAssetManager {}
  //
  // // Let the opaque type AAssetManager refer to a guarded AssetManager2 instance.
  // static class GuardedAssetManager implements AAssetManager {
  //   CppAssetManager2 guarded_assetmanager = new CppAssetManager2();
  // }

  static CppAssetManager2 NdkAssetManagerForJavaObject(
      /* JNIEnv* env,*/ AssetManager jassetmanager) {
    // long assetmanager_handle = env.GetLongField(jassetmanager, gAssetManagerOffsets.mObject);
    long assetmanager_handle = ReflectionHelpers.getField(jassetmanager, "mObject");
    CppAssetManager2 am =
        Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(assetmanager_handle);
    if (am == null) {
      throw new IllegalStateException("AssetManager has been finalized!");
    }
    return am;
  }

  static CppAssetManager2 AssetManagerForJavaObject(/* JNIEnv* env,*/ AssetManager jassetmanager) {
    return NdkAssetManagerForJavaObject(jassetmanager);
  }

  static CppAssetManager2 AssetManagerFromLong(long ptr) {
    // return *AssetManagerForNdkAssetManager(reinterpret_cast<AAssetManager>(ptr));
    return Registries.NATIVE_ASSET_MANAGER_REGISTRY.getNativeObject(ptr);
  }

  static ParcelFileDescriptor ReturnParcelFileDescriptor(
      /* JNIEnv* env,*/ Asset asset, long[] out_offsets) throws FileNotFoundException {
    final Ref<Long> start_offset = new Ref<>(0L);
    final Ref<Long> length = new Ref<>(0L);
    FileDescriptor fd = asset.openFileDescriptor(start_offset, length);
    // asset.reset();

    if (fd == null) {
      throw new FileNotFoundException(
          "This file can not be opened as a file descriptor; it is probably compressed");
    }

    long[] offsets =
        out_offsets; // reinterpret_cast<long*>(env.GetPrimitiveArrayCritical(out_offsets, 0));
    if (offsets == null) {
      // close(fd);
      return null;
    }

    offsets[0] = start_offset.get();
    offsets[1] = length.get();

    // env.ReleasePrimitiveArrayCritical(out_offsets, offsets, 0);

    FileDescriptor file_desc = fd; // jniCreateFileDescriptor(env, fd);
    // if (file_desc == null) {
    //   close(fd);
    //   return null;
    // }

    // TODO: consider doing this
    // return new ParcelFileDescriptor(file_desc);
    return ParcelFileDescriptor.open(asset.getFile(), ParcelFileDescriptor.MODE_READ_ONLY);
  }

  /** Used for the creation of system assets. */
  @Implementation(minSdk = P)
  protected void __constructor__(boolean sentinel) {
    inNonSystemConstructor = true;
    try {
      // call real constructor so field initialization happens.
      invokeConstructor(
          AssetManager.class, realAssetManager, ClassParameter.from(boolean.class, sentinel));
    } finally {
      inNonSystemConstructor = false;
    }
  }

  // static jint NativeGetGlobalAssetCount(JNIEnv* /*env*/, jobject /*clazz*/) {
  @Implementation(minSdk = P)
  protected static int getGlobalAssetCount() {
    return Asset.getGlobalCount();
  }

  // static jobject NativeGetAssetAllocations(JNIEnv* env, jobject /*clazz*/) {
  @Implementation(minSdk = P)
  protected static String getAssetAllocations() {
    String alloc = Asset.getAssetAllocations();
    if (alloc.length() <= 0) {
      return null;
    }
    return alloc;
  }

  // static jint NativeGetGlobalAssetManagerCount(JNIEnv* /*env*/, jobject /*clazz*/) {
  @Implementation(minSdk = P)
  protected static int getGlobalAssetManagerCount() {
    // TODO(adamlesinski): Switch to AssetManager2.
    return CppAssetManager.getGlobalCount();
  }

  // static jlong NativeCreate(JNIEnv* /*env*/, jclass /*clazz*/) {
  @Implementation(minSdk = P)
  protected static long nativeCreate() {
    // AssetManager2 needs to be protected by a lock. To avoid cache misses, we allocate the lock
    // and
    // AssetManager2 in a contiguous block (GuardedAssetManager).
    // return reinterpret_cast<long>(new GuardedAssetManager());

    long cppAssetManagerRef;

    // we want to share a single instance of the system CppAssetManager2
    if (inNonSystemConstructor) {
      CppAssetManager2 appAssetManager = new CppAssetManager2();
      cppAssetManagerRef = Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(appAssetManager);
    } else {
      if (systemCppAssetManager2 == null) {
        systemCppAssetManager2 = new CppAssetManager2();
        systemCppAssetManager2Ref =
            Registries.NATIVE_ASSET_MANAGER_REGISTRY.register(systemCppAssetManager2);
      }

      cppAssetManagerRef = systemCppAssetManager2Ref;
    }

    return cppAssetManagerRef;
  }

  // static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
  @Implementation(minSdk = P)
  protected static void nativeDestroy(long ptr) {
    if (ptr == systemCppAssetManager2Ref) {
      // don't destroy the shared system CppAssetManager2!
      return;
    }

    // delete reinterpret_cast<GuardedAssetManager*>(ptr);
    Registries.NATIVE_ASSET_MANAGER_REGISTRY.unregister(ptr);
  }

  // static void NativeSetApkAssets(JNIEnv* env, jclass /*clazz*/, jlong ptr,
  //                                jobjectArray apk_assets_array, jboolean invalidate_caches) {
  @Implementation(minSdk = P)
  protected static void nativeSetApkAssets(
      long ptr,
      @NonNull android.content.res.ApkAssets[] apk_assets_array,
      boolean invalidate_caches) {
    ATRACE_NAME("AssetManager::SetApkAssets");

    int apk_assets_len = apk_assets_array.length;
    List<CppApkAssets> apk_assets = new ArrayList<>();
    // apk_assets.reserve(apk_assets_len);
    for (int i = 0; i < apk_assets_len; i++) {
      android.content.res.ApkAssets apkAssets =
          apk_assets_array[i]; // env.GetObjectArrayElement(apk_assets_array, i);
      if (apkAssets == null) {
        throw new NullPointerException(String.format("ApkAssets at index %d is null", i));
      }

      long apk_assets_native_ptr =
          ((ShadowArscApkAssets9) Shadow.extract(apkAssets)).getNativePtr();
      // if (env.ExceptionCheck()) {
      //   return;
      // }
      apk_assets.add(Registries.NATIVE_APK_ASSETS_REGISTRY.getNativeObject(apk_assets_native_ptr));
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    assetmanager.SetApkAssets(apk_assets, invalidate_caches);
  }

  // static void NativeSetConfiguration(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint mcc, jint
  // mnc,
  //                                    jstring locale, jint orientation, jint touchscreen, jint
  // density,
  //                                    jint keyboard, jint keyboard_hidden, jint navigation,
  //                                    jint screen_width, jint screen_height,
  //                                    jint smallest_screen_width_dp, jint screen_width_dp,
  //                                    jint screen_height_dp, jint screen_layout, jint ui_mode,
  //                                    jint color_mode, jint major_version) {
  @Implementation(minSdk = P, maxSdk = TIRAMISU)
  protected static void nativeSetConfiguration(
      long ptr,
      int mcc,
      int mnc,
      @Nullable String locale,
      int orientation,
      int touchscreen,
      int density,
      int keyboard,
      int keyboard_hidden,
      int navigation,
      int screen_width,
      int screen_height,
      int smallest_screen_width_dp,
      int screen_width_dp,
      int screen_height_dp,
      int screen_layout,
      int ui_mode,
      int color_mode,
      int major_version) {
    ATRACE_NAME("AssetManager::SetConfiguration");

    ResTable_config configuration = new ResTable_config();
    // memset(&configuration, 0, sizeof(configuration));
    configuration.mcc = (short) (mcc);
    configuration.mnc = (short) (mnc);
    configuration.orientation = (byte) (orientation);
    configuration.touchscreen = (byte) (touchscreen);
    configuration.density = (short) (density);
    configuration.keyboard = (byte) (keyboard);
    configuration.inputFlags = (byte) (keyboard_hidden);
    configuration.navigation = (byte) (navigation);
    configuration.screenWidth = (short) (screen_width);
    configuration.screenHeight = (short) (screen_height);
    configuration.smallestScreenWidthDp = (short) (smallest_screen_width_dp);
    configuration.screenWidthDp = (short) (screen_width_dp);
    configuration.screenHeightDp = (short) (screen_height_dp);
    configuration.screenLayout = (byte) (screen_layout);
    configuration.uiMode = (byte) (ui_mode);
    configuration.colorMode = (byte) (color_mode);
    configuration.sdkVersion = (short) (major_version);

    if (locale != null) {
      String locale_utf8 = locale;
      CHECK(locale_utf8 != null);
      configuration.setBcp47Locale(locale_utf8);
    }

    // Constants duplicated from Java class android.content.res.Configuration.
    int kScreenLayoutRoundMask = 0x300;
    int kScreenLayoutRoundShift = 8;

    // In Java, we use a 32bit integer for screenLayout, while we only use an 8bit integer
    // in C++. We must extract the round qualifier out of the Java screenLayout and put it
    // into screenLayout2.
    configuration.screenLayout2 =
        (byte) ((screen_layout & kScreenLayoutRoundMask) >> kScreenLayoutRoundShift);

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    assetmanager.SetConfiguration(configuration);
  }

  // static jobject NativeGetAssignedPackageIdentifiers(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
  @Implementation(minSdk = P, maxSdk = Q)
  protected static @NonNull SparseArray<String> nativeGetAssignedPackageIdentifiers(long ptr) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);

    SparseArray<String> sparse_array = new SparseArray<>();

    if (sparse_array == null) {
      // An exception is pending.
      return null;
    }

    assetmanager.ForEachPackage(
        (String package_name, byte package_id) -> {
          String jpackage_name = package_name; // env.NewStringUTF(package_name);
          if (jpackage_name == null) {
            // An exception is pending.
            return;
          }

          // env.CallVoidMethod(sparse_array, gSparseArrayOffsets.put, (int) (package_id),
          //     jpackage_name);
          sparse_array.put(package_id, jpackage_name);
        });
    return sparse_array;
  }

  // static jobjectArray NativeList(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring path) {
  @Implementation(minSdk = P)
  protected static @Nullable String[] nativeList(long ptr, @NonNull String path)
      throws IOException {
    String path_utf8 = path;
    if (path_utf8 == null) {
      // This will throw NPE.
      return null;
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    AssetDir asset_dir = assetmanager.OpenDir(path_utf8);
    if (asset_dir == null) {
      throw new FileNotFoundException(path_utf8);
    }

    int file_count = asset_dir.getFileCount();

    String[] array = new String[file_count]; // env.NewObjectArray(file_count, g_stringClass, null);
    // if (array == null) {
    //   return null;
    // }

    for (int i = 0; i < file_count; i++) {
      String java_string = asset_dir.getFileName(i).string();

      // Check for errors creating the strings (if malformed or no memory).
      // if (env.ExceptionCheck()) {
      //   return null;
      // }

      // env.SetObjectArrayElement(array, i, java_string);
      array[i] = java_string;

      // If we have a large amount of string in our array, we might overflow the
      // local reference table of the VM.
      // env.DeleteLocalRef(java_string);
    }
    return array;
  }

  // static jlong NativeOpenAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path,
  //                              jint access_mode) {
  @Implementation(minSdk = P)
  protected static long nativeOpenAsset(long ptr, @NonNull String asset_path, int access_mode)
      throws FileNotFoundException {
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
      // This will throw NPE.
      return 0;
    }

    ATRACE_NAME(String.format("AssetManager::OpenAsset(%s)", asset_path_utf8));

    if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode()
        && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode()
        && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode()
        && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) {
      throw new IllegalArgumentException("Bad access mode");
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.fromInt(access_mode));
    if (!isTruthy(asset)) {
      throw new FileNotFoundException(asset_path_utf8);
    }
    return Registries.NATIVE_ASSET_REGISTRY.register(asset);
  }

  // static jobject NativeOpenAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring asset_path,
  //                                  jlongArray out_offsets) {
  @Implementation(minSdk = P)
  protected static ParcelFileDescriptor nativeOpenAssetFd(
      long ptr, @NonNull String asset_path, long[] out_offsets) throws IOException {
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
      // This will throw NPE.
      return null;
    }

    ATRACE_NAME(String.format("AssetManager::OpenAssetFd(%s)", asset_path_utf8));

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset = assetmanager.Open(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM);
    if (!isTruthy(asset)) {
      throw new FileNotFoundException(asset_path_utf8);
    }
    return ReturnParcelFileDescriptor(asset, out_offsets);
  }

  // static jlong NativeOpenNonAsset(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
  //                                 jstring asset_path, jint access_mode) {
  @Implementation(minSdk = P)
  protected static long nativeOpenNonAsset(
      long ptr, int jcookie, @NonNull String asset_path, int access_mode)
      throws FileNotFoundException {
    ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
      // This will throw NPE.
      return 0;
    }

    ATRACE_NAME(String.format("AssetManager::OpenNonAsset(%s)", asset_path_utf8));

    if (access_mode != Asset.AccessMode.ACCESS_UNKNOWN.mode()
        && access_mode != Asset.AccessMode.ACCESS_RANDOM.mode()
        && access_mode != Asset.AccessMode.ACCESS_STREAMING.mode()
        && access_mode != Asset.AccessMode.ACCESS_BUFFER.mode()) {
      throw new IllegalArgumentException("Bad access mode");
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset;
    if (cookie.intValue() != kInvalidCookie) {
      asset =
          assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.fromInt(access_mode));
    } else {
      asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.fromInt(access_mode));
    }

    if (!isTruthy(asset)) {
      throw new FileNotFoundException(asset_path_utf8);
    }
    return Registries.NATIVE_ASSET_REGISTRY.register(asset);
  }

  // static jobject NativeOpenNonAssetFd(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint jcookie,
  //                                     jstring asset_path, jlongArray out_offsets) {
  @Implementation(minSdk = P)
  protected static @Nullable ParcelFileDescriptor nativeOpenNonAssetFd(
      long ptr, int jcookie, @NonNull String asset_path, @NonNull long[] out_offsets)
      throws IOException {
    ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
      // This will throw NPE.
      return null;
    }

    ATRACE_NAME(String.format("AssetManager::OpenNonAssetFd(%s)", asset_path_utf8));

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset;
    if (cookie.intValue() != kInvalidCookie) {
      asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM);
    } else {
      asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM);
    }

    if (!isTruthy(asset)) {
      throw new FileNotFoundException(asset_path_utf8);
    }
    return ReturnParcelFileDescriptor(asset, out_offsets);
  }

  // static jlong NativeOpenXmlAsset(JNIEnv* env, jobject /*clazz*/, jlong ptr, jint jcookie,
  //                                 jstring asset_path) {
  @Implementation(minSdk = P)
  protected static long nativeOpenXmlAsset(long ptr, int jcookie, @NonNull String asset_path)
      throws FileNotFoundException {
    ApkAssetsCookie cookie = JavaCookieToApkAssetsCookie(jcookie);
    String asset_path_utf8 = asset_path;
    if (asset_path_utf8 == null) {
      // This will throw NPE.
      return 0;
    }

    ATRACE_NAME(String.format("AssetManager::OpenXmlAsset(%s)", asset_path_utf8));

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Asset asset;
    if (cookie.intValue() != kInvalidCookie) {
      asset = assetmanager.OpenNonAsset(asset_path_utf8, cookie, Asset.AccessMode.ACCESS_RANDOM);
    } else {
      Ref<ApkAssetsCookie> cookieRef = new Ref<>(cookie);
      asset = assetmanager.OpenNonAsset(asset_path_utf8, Asset.AccessMode.ACCESS_RANDOM, cookieRef);
      cookie = cookieRef.get();
    }

    if (!isTruthy(asset)) {
      throw new FileNotFoundException(asset_path_utf8);
    }

    // May be nullptr.
    DynamicRefTable dynamic_ref_table = assetmanager.GetDynamicRefTableForCookie(cookie);

    ResXMLTree xml_tree = new ResXMLTree(dynamic_ref_table);
    int err = xml_tree.setTo(asset.getBuffer(true), (int) asset.getLength(), true);
    // asset.reset();

    if (err != NO_ERROR) {
      throw new FileNotFoundException("Corrupt XML binary file");
    }
    return NATIVE_RES_XML_TREES.register(xml_tree);
  }

  // static jint NativeGetResourceValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
  //                                    jshort density, jobject typed_value,
  //                                    jboolean resolve_references) {
  @Implementation(minSdk = P)
  protected static int nativeGetResourceValue(
      long ptr,
      @AnyRes int resid,
      short density,
      @NonNull TypedValue typed_value,
      boolean resolve_references) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    final Ref<Res_value> value = new Ref<>(null);
    final Ref<ResTable_config> selected_config = new Ref<>(null);
    final Ref<Integer> flags = new Ref<>(0);
    ApkAssetsCookie cookie =
        assetmanager.GetResource(
            resid, false /*may_be_bag*/, (short) (density), value, selected_config, flags);
    if (cookie.intValue() == kInvalidCookie) {
      return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }

    final Ref<Integer> ref = new Ref<>(resid);
    if (resolve_references) {
      cookie = assetmanager.ResolveReference(cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
      }
    }
    return CopyValue(
        cookie, value.get(), ref.get(), flags.get(), selected_config.get(), typed_value);
  }

  // static jint NativeGetResourceBagValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
  //                                       jint bag_entry_id, jobject typed_value) {
  @Implementation(minSdk = P)
  protected static int nativeGetResourceBagValue(
      long ptr, @AnyRes int resid, int bag_entry_id, @NonNull TypedValue typed_value) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }

    final Ref<Integer> type_spec_flags = new Ref<>(bag.type_spec_flags);
    ApkAssetsCookie cookie = K_INVALID_COOKIE;
    Res_value bag_value = null;
    for (ResolvedBag.Entry entry : bag.entries) {
      if (entry.key == (int) (bag_entry_id)) {
        cookie = entry.cookie;
        bag_value = entry.value;

        // Keep searching (the old implementation did that).
      }
    }

    if (cookie.intValue() == kInvalidCookie) {
      return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }

    final Ref<Res_value> value = new Ref<>(bag_value);
    final Ref<Integer> ref = new Ref<>(resid);
    final Ref<ResTable_config> selected_config = new Ref<>(null);
    cookie = assetmanager.ResolveReference(cookie, value, selected_config, type_spec_flags, ref);
    if (cookie.intValue() == kInvalidCookie) {
      return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }
    return CopyValue(cookie, value.get(), ref.get(), type_spec_flags.get(), null, typed_value);
  }

  // static jintArray NativeGetStyleAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid)
  // {
  @Implementation(minSdk = P)
  protected static @Nullable @AttrRes int[] nativeGetStyleAttributes(
      long ptr, @StyleRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return null;
    }

    int[] array = new int[bag.entry_count];
    // if (env.ExceptionCheck()) {
    //   return null;
    // }

    for (int i = 0; i < bag.entry_count; i++) {
      int attr_resid = bag.entries[i].key;
      // env.SetIntArrayRegion(array, i, 1, &attr_resid);
      array[i] = attr_resid;
    }
    return array;
  }

  // static jobjectArray NativeGetResourceStringArray(JNIEnv* env, jclass /*clazz*/, jlong ptr,
  //                                                  jint resid) {
  @Implementation(minSdk = P)
  protected static @Nullable String[] nativeGetResourceStringArray(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return null;
    }

    String[] array = new String[bag.entry_count];
    if (array == null) {
      return null;
    }

    for (int i = 0; i < bag.entry_count; i++) {
      ResolvedBag.Entry entry = bag.entries[i];

      // Resolve any references to their final value.
      final Ref<Res_value> value = new Ref<>(entry.value);
      final Ref<ResTable_config> selected_config = new Ref<>(null);
      final Ref<Integer> flags = new Ref<>(0);
      final Ref<Integer> ref = new Ref<>(0);
      ApkAssetsCookie cookie =
          assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        return null;
      }

      if (value.get().dataType == Res_value.TYPE_STRING) {
        CppApkAssets apk_assets = assetmanager.GetApkAssets().get(cookie.intValue());
        ResStringPool pool = apk_assets.GetLoadedArsc().GetStringPool();

        String java_string = null;
        int str_len;
        String str_utf8 = pool.stringAt(value.get().data);
        if (str_utf8 != null) {
          java_string = str_utf8;
        } else {
          String str_utf16 = pool.stringAt(value.get().data);
          java_string = str_utf16;
        }

        // // Check for errors creating the strings (if malformed or no memory).
        // if (env.ExceptionCheck()) {
        //   return null;
        // }

        // env.SetObjectArrayElement(array, i, java_string);
        array[i] = java_string;

        // If we have a large amount of string in our array, we might overflow the
        // local reference table of the VM.
        // env.DeleteLocalRef(java_string);
      }
    }
    return array;
  }

  // static jintArray NativeGetResourceStringArrayInfo(JNIEnv* env, jclass /*clazz*/, jlong ptr,
  //                                                   jint resid) {
  @Implementation(minSdk = P)
  protected static @Nullable int[] nativeGetResourceStringArrayInfo(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return null;
    }

    int[] array = new int[bag.entry_count * 2];
    // if (array == null) {
    //   return null;
    // }

    int[] buffer = array; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(array, null));
    // if (buffer == null) {
    //   return null;
    // }

    for (int i = 0; i < bag.entry_count; i++) {
      ResolvedBag.Entry entry = bag.entries[i];
      final Ref<Res_value> value = new Ref<>(entry.value);
      final Ref<ResTable_config> selected_config = new Ref<>(null);
      final Ref<Integer> flags = new Ref<>(0);
      final Ref<Integer> ref = new Ref<>(0);
      ApkAssetsCookie cookie =
          assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT);
        return null;
      }

      int string_index = -1;
      if (value.get().dataType == Res_value.TYPE_STRING) {
        string_index = (int) (value.get().data);
      }

      buffer[i * 2] = ApkAssetsCookieToJavaCookie(cookie);
      buffer[(i * 2) + 1] = string_index;
    }
    // env.ReleasePrimitiveArrayCritical(array, buffer, 0);
    return array;
  }

  // static jintArray NativeGetResourceIntArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint
  // resid) {
  @Implementation(minSdk = P)
  protected static @Nullable int[] nativeGetResourceIntArray(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return null;
    }

    int[] array = new int[bag.entry_count];
    // if (array == null) {
    //   return null;
    // }

    int[] buffer = array; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(array, null));
    // if (buffer == null) {
    //   return null;
    // }

    for (int i = 0; i < bag.entry_count; i++) {
      ResolvedBag.Entry entry = bag.entries[i];
      final Ref<Res_value> value = new Ref<>(entry.value);
      final Ref<ResTable_config> selected_config = new Ref<>(null);
      final Ref<Integer> flags = new Ref<>(0);
      final Ref<Integer> ref = new Ref<>(0);
      ApkAssetsCookie cookie =
          assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        // env.ReleasePrimitiveArrayCritical(array, buffer, JNI_ABORT);
        return null;
      }

      if (value.get().dataType >= Res_value.TYPE_FIRST_INT
          && value.get().dataType <= Res_value.TYPE_LAST_INT) {
        buffer[i] = (int) (value.get().data);
      }
    }
    // env.ReleasePrimitiveArrayCritical(array, buffer, 0);
    return array;
  }

  // static jint NativeGetResourceArraySize(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jint
  // resid) {
  @Implementation(minSdk = P)
  protected static int nativeGetResourceArraySize(long ptr, @ArrayRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return -1;
    }
    return (int) (bag.entry_count);
  }

  // static jint NativeGetResourceArray(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid,
  //                                    jintArray out_data) {
  @Implementation(minSdk = P)
  protected static int nativeGetResourceArray(
      long ptr, @ArrayRes int resid, @NonNull int[] out_data) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResolvedBag bag = assetmanager.GetBag(resid);
    if (bag == null) {
      return -1;
    }

    int out_data_length = out_data.length;
    // if (env.ExceptionCheck()) {
    //   return -1;
    // }

    if ((int) (bag.entry_count) > out_data_length * STYLE_NUM_ENTRIES) {
      throw new IllegalArgumentException("Input array is not large enough");
    }

    int[] buffer =
        out_data; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_data, null));
    if (buffer == null) {
      return -1;
    }

    int[] cursor = buffer;
    for (int i = 0; i < bag.entry_count; i++) {
      ResolvedBag.Entry entry = bag.entries[i];
      final Ref<Res_value> value = new Ref<>(entry.value);
      final Ref<ResTable_config> selected_config = new Ref<>(new ResTable_config());
      selected_config.get().density = 0;
      final Ref<Integer> flags = new Ref<>(bag.type_spec_flags);
      final Ref<Integer> ref = new Ref<>(0);
      ApkAssetsCookie cookie =
          assetmanager.ResolveReference(entry.cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        // env.ReleasePrimitiveArrayCritical(out_data, buffer, JNI_ABORT);
        return -1;
      }

      // Deal with the special @null value -- it turns back to TYPE_NULL.
      if (value.get().dataType == Res_value.TYPE_REFERENCE && value.get().data == 0) {
        value.set(Res_value.NULL_VALUE);
      }

      int offset = i * STYLE_NUM_ENTRIES;
      cursor[offset + STYLE_TYPE] = (int) (value.get().dataType);
      cursor[offset + STYLE_DATA] = (int) (value.get().data);
      cursor[offset + STYLE_ASSET_COOKIE] = ApkAssetsCookieToJavaCookie(cookie);
      cursor[offset + STYLE_RESOURCE_ID] = (int) (ref.get());
      cursor[offset + STYLE_CHANGING_CONFIGURATIONS] = (int) (flags.get());
      cursor[offset + STYLE_DENSITY] = (int) (selected_config.get().density);
      // cursor += STYLE_NUM_ENTRIES;
    }
    // env.ReleasePrimitiveArrayCritical(out_data, buffer, 0);
    return (int) (bag.entry_count);
  }

  // static jint NativeGetResourceIdentifier(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring name,
  //                                         jstring def_type, jstring def_package) {
  @Implementation(minSdk = P)
  protected static @AnyRes int nativeGetResourceIdentifier(
      long ptr, @NonNull String name, @Nullable String def_type, @Nullable String def_package) {
    String name_utf8 = name;
    if (name_utf8 == null) {
      // This will throw NPE.
      return 0;
    }

    String type = null;
    if (def_type != null) {
      String type_utf8 = def_type;
      CHECK(type_utf8 != null);
      type = type_utf8;
    }

    String package_ = null;
    if (def_package != null) {
      String package_utf8 = def_package;
      CHECK(package_utf8 != null);
      package_ = package_utf8;
    }
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    return (int) (assetmanager.GetResourceId(name_utf8, type, package_));
  }

  // static jstring NativeGetResourceName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid) {
  @Implementation(minSdk = P)
  protected static @Nullable String nativeGetResourceName(long ptr, @AnyRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    CppAssetManager2.ResourceName name = new ResourceName();
    if (!assetmanager.GetResourceName(resid, name)) {
      return null;
    }

    StringBuilder result = new StringBuilder();
    if (name.package_ != null) {
      result.append(name.package_ /*, name.package_len*/);
    }

    if (name.type != null /*|| name.type16 != null*/) {
      if (!(result.length() == 0)) {
        result.append(":");
      }

      // if (name.type != null) {
      result.append(name.type /*, name.type_len*/);
      // } else {
      //   result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.type16 /*, name.type_len))*/);
      // }
    }

    if (name.entry != null /*|| name.entry16 != null*/) {
      if (!(result.length() == 0)) {
        result.append("/");
      }

      // if (name.entry != null) {
      result.append(name.entry /*, name.entry_len*/);
      // } else {
      //   result.append( /*util.Utf16ToUtf8(StringPiece16(*/ name.entry16 /*, name.entry_len)*/);
      // }
    }
    return result.toString();
  }

  // static jstring NativeGetResourcePackageName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint
  // resid) {
  @Implementation(minSdk = P)
  protected static @Nullable String nativeGetResourcePackageName(long ptr, @AnyRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    final ResourceName name = new ResourceName();
    if (!assetmanager.GetResourceName(resid, name)) {
      return null;
    }

    if (name.package_ != null) {
      return name.package_;
    }
    return null;
  }

  // static jstring NativeGetResourceTypeName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid)
  // {
  @Implementation(minSdk = P)
  protected static @Nullable String nativeGetResourceTypeName(long ptr, @AnyRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    final ResourceName name = new ResourceName();
    if (!assetmanager.GetResourceName(resid, name)) {
      return null;
    }

    if (name.type != null) {
      return name.type;
      // } else if (name.get().type16 != null) {
      //   return name.get().type16; // env.NewString(reinterpret_cast<jchar*>(name.type16),
      // name.type_len);
    }
    return null;
  }

  // static jstring NativeGetResourceEntryName(JNIEnv* env, jclass /*clazz*/, jlong ptr, jint resid)
  // {
  @Implementation(minSdk = P)
  protected static @Nullable String nativeGetResourceEntryName(long ptr, @AnyRes int resid) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    final ResourceName name = new ResourceName();
    if (!assetmanager.GetResourceName(resid, name)) {
      return null;
    }

    if (name.entry != null) {
      return name.entry;
      // } else if (name.entry16 != null) {
      //   return name.entry16; // env.NewString(reinterpret_cast<jchar*>(name.entry16),
      // name.entry_len);
    }
    return null;
  }

  // static jobjectArray NativeGetLocales(JNIEnv* env, jclass /*class*/, jlong ptr,
  //                                      jboolean exclude_system) {
  @Implementation(minSdk = P)
  protected static @Nullable String[] nativeGetLocales(long ptr, boolean exclude_system) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Set<String> locales =
        assetmanager.GetResourceLocales(exclude_system, true /*merge_equivalent_languages*/);

    String[] array =
        new String[locales.size()]; // env.NewObjectArray(locales.size(), g_stringClass, null);
    // if (array == null) {
    //   return null;
    // }

    int idx = 0;
    for (String locale : locales) {
      String java_string = locale;
      if (java_string == null) {
        return null;
      }
      // env.SetObjectArrayElement(array, idx++, java_string);
      array[idx++] = java_string;
      // env.DeleteLocalRef(java_string);
    }
    return array;
  }

  static Configuration ConstructConfigurationObject(/* JNIEnv* env,*/ ResTable_config config) {
    // jobject result =
    //     env.NewObject(gConfigurationOffsets.classObject, gConfigurationOffsets.constructor);
    Configuration result = new Configuration();
    // if (result == null) {
    //   return null;
    // }

    result.smallestScreenWidthDp = config.smallestScreenWidthDp;
    result.screenWidthDp = config.screenWidthDp;
    result.screenHeightDp = config.screenHeightDp;
    return result;
  }

  // static jobjectArray NativeGetSizeConfigurations(JNIEnv* env, jclass /*clazz*/, jlong ptr) {
  @Implementation(minSdk = P)
  protected static @Nullable Configuration[] nativeGetSizeConfigurations(long ptr) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Set<ResTable_config> configurations =
        assetmanager.GetResourceConfigurations(true /*exclude_system*/, false /*exclude_mipmap*/);

    Configuration[] array = new Configuration[configurations.size()];
    // env.NewObjectArray(configurations.size(), gConfigurationOffsets.classObject, null);
    // if (array == null) {
    //   return null;
    // }

    int idx = 0;
    for (ResTable_config configuration : configurations) {
      Configuration java_configuration = ConstructConfigurationObject(configuration);
      // if (java_configuration == null) {
      //   return null;
      // }

      // env.SetObjectArrayElement(array, idx++, java_configuration);
      array[idx++] = java_configuration;
      // env.DeleteLocalRef(java_configuration);
    }
    return array;
  }

  // static void NativeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr,
  //                              jint def_style_attr, jint def_style_resid, jlong xml_parser_ptr,
  //                              jintArray java_attrs, jlong out_values_ptr, jlong out_indices_ptr)
  // {
  @Implementation(minSdk = P)
  protected static void nativeApplyStyle(
      long ptr,
      long theme_ptr,
      @AttrRes int def_style_attr,
      @StyleRes int def_style_resid,
      long xml_parser_ptr,
      @NonNull int[] java_attrs,
      long out_values_ptr,
      long out_indices_ptr) {
    PerfStatsCollector.getInstance()
        .measure(
            "applyStyle",
            () ->
                nativeApplyStyle_measured(
                    ptr,
                    theme_ptr,
                    def_style_attr,
                    def_style_resid,
                    xml_parser_ptr,
                    java_attrs,
                    out_values_ptr,
                    out_indices_ptr));
  }

  private static void nativeApplyStyle_measured(
      long ptr,
      long theme_ptr,
      @AttrRes int def_style_attr,
      @StyleRes int def_style_resid,
      long xml_parser_ptr,
      @NonNull int[] java_attrs,
      long out_values_ptr,
      long out_indices_ptr) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    CHECK(theme.GetAssetManager() == assetmanager);
    // (void) assetmanager;

    ResXMLParser xml_parser =
        xml_parser_ptr == 0 ? null : NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr);
    // int[] out_values = reinterpret_cast<int*>(out_values_ptr);
    // int[] out_indices = reinterpret_cast<int*>(out_indices_ptr);
    ShadowVMRuntime shadowVMRuntime = Shadow.extract(VMRuntime.getRuntime());
    int[] out_values = (int[]) shadowVMRuntime.getObjectForAddress(out_values_ptr);
    int[] out_indices = (int[]) shadowVMRuntime.getObjectForAddress(out_indices_ptr);

    int attrs_len = java_attrs.length;
    int[] attrs =
        java_attrs; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(java_attrs, null));
    // if (attrs == null) {
    //   return;
    // }

    ApplyStyle(
        theme,
        xml_parser,
        (int) (def_style_attr),
        (int) (def_style_resid),
        attrs,
        attrs_len,
        out_values,
        out_indices);
    // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
  }

  // static jboolean NativeResolveAttrs(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr,
  //                                    jint def_style_attr, jint def_style_resid, jintArray
  // java_values,
  //                                    jintArray java_attrs, jintArray out_java_values,
  //                                    jintArray out_java_indices) {
  @Implementation(minSdk = P)
  protected static boolean nativeResolveAttrs(
      long ptr,
      long theme_ptr,
      @AttrRes int def_style_attr,
      @StyleRes int def_style_resid,
      @Nullable int[] java_values,
      @NonNull int[] java_attrs,
      @NonNull int[] out_java_values,
      @NonNull int[] out_java_indices) {
    int attrs_len = java_attrs.length;
    int out_values_len = out_java_values.length;
    if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) {
      throw new IndexOutOfBoundsException("outValues too small");
    }

    int[] attrs =
        java_attrs; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(java_attrs, null));
    if (attrs == null) {
      return JNI_FALSE;
    }

    int[] values = null;
    int values_len = 0;
    if (java_values != null) {
      values_len = java_values.length;
      values =
          java_values; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(java_values, null));
      if (values == null) {
        // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
        return JNI_FALSE;
      }
    }

    int[] out_values = out_java_values;
    // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_java_values, null));
    if (out_values == null) {
      // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
      // if (values != null) {
      //   env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT);
      // }
      return JNI_FALSE;
    }

    int[] out_indices = null;
    if (out_java_indices != null) {
      int out_indices_len = out_java_indices.length;
      if (out_indices_len > attrs_len) {
        out_indices = out_java_indices;
        // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_java_indices, null));
        if (out_indices == null) {
          // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
          // if (values != null) {
          //   env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT);
          // }
          // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT);
          return JNI_FALSE;
        }
      }
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    CHECK(theme.GetAssetManager() == assetmanager);
    // (void) assetmanager;

    boolean result =
        ResolveAttrs(
            theme,
            (int) (def_style_attr),
            (int) (def_style_resid),
            values,
            values_len,
            attrs,
            attrs_len,
            out_values,
            out_indices);
    // if (out_indices != null) {
    //   env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0);
    // }

    // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0);
    // if (values != null) {
    //   env.ReleasePrimitiveArrayCritical(java_values, values, JNI_ABORT);
    // }
    // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
    return result ? JNI_TRUE : JNI_FALSE;
  }

  // static jboolean NativeRetrieveAttributes(JNIEnv* env, jclass /*clazz*/, jlong ptr,
  //                                          jlong xml_parser_ptr, jintArray java_attrs,
  //                                          jintArray out_java_values, jintArray out_java_indices)
  // {
  @Implementation(minSdk = P)
  protected static boolean nativeRetrieveAttributes(
      long ptr,
      long xml_parser_ptr,
      @NonNull int[] java_attrs,
      @NonNull int[] out_java_values,
      @NonNull int[] out_java_indices) {
    int attrs_len = java_attrs.length;
    int out_values_len = out_java_values.length;
    if (out_values_len < (attrs_len * STYLE_NUM_ENTRIES)) {
      throw new IndexOutOfBoundsException("outValues too small");
    }

    int[] attrs =
        java_attrs; // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(java_attrs, null));
    if (attrs == null) {
      return JNI_FALSE;
    }

    int[] out_values = out_java_values;
    // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_java_values, null));
    if (out_values == null) {
      // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
      return JNI_FALSE;
    }

    int[] out_indices = null;
    if (out_java_indices != null) {
      int out_indices_len = out_java_indices.length;
      if (out_indices_len > attrs_len) {
        out_indices = out_java_indices;
        // reinterpret_cast<int*>(env.GetPrimitiveArrayCritical(out_java_indices, null));
        if (out_indices == null) {
          // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
          // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, JNI_ABORT);
          return JNI_FALSE;
        }
      }
    }

    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    ResXMLParser xml_parser = NATIVE_RES_XML_PARSERS.getNativeObject(xml_parser_ptr);

    boolean result =
        RetrieveAttributes(assetmanager, xml_parser, attrs, attrs_len, out_values, out_indices);

    // if (out_indices != null) {
    //   env.ReleasePrimitiveArrayCritical(out_java_indices, out_indices, 0);
    // }
    // env.ReleasePrimitiveArrayCritical(out_java_values, out_values, 0);
    // env.ReleasePrimitiveArrayCritical(java_attrs, attrs, JNI_ABORT);
    return result;
  }

  // static jlong NativeThemeCreate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) {
  @Implementation(minSdk = P)
  protected static long nativeThemeCreate(long ptr) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    return Registries.NATIVE_THEME9_REGISTRY.register(assetmanager.NewTheme());
  }

  // static void NativeThemeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) {
  @Implementation(minSdk = P, maxSdk = R)
  protected static void nativeThemeDestroy(long theme_ptr) {
    Registries.NATIVE_THEME9_REGISTRY.unregister(theme_ptr);
  }

  // static void NativeThemeApplyStyle(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong theme_ptr,
  //                                   jint resid, jboolean force) {
  @Implementation(minSdk = P)
  protected static void nativeThemeApplyStyle(
      long ptr, long theme_ptr, @StyleRes int resid, boolean force) {
    // AssetManager is accessed via the theme, so grab an explicit lock here.
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    CHECK(theme.GetAssetManager() == assetmanager);
    // (void) assetmanager;
    theme.ApplyStyle(resid, force);

    // TODO(adamlesinski): Consider surfacing exception when result is failure.
    // CTS currently expects no exceptions from this method.
    // std::string error_msg = StringPrintf("Failed to apply style 0x%08x to theme", resid);
    // throw new IllegalArgumentException(error_msg.c_str());
  }

  // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_theme_ptr,
  //                             jlong src_theme_ptr) {
  @Implementation(minSdk = P, maxSdk = P)
  protected static void nativeThemeCopy(long dst_theme_ptr, long src_theme_ptr) {
    Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr);
    Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr);
    if (!dst_theme.SetTo(src_theme)) {
      throw new IllegalArgumentException("Themes are from different AssetManagers");
    }
  }

  // static void NativeThemeCopy(JNIEnv* env, jclass /*clazz*/, jlong dst_asset_manager_ptr,
  //     jlong dst_theme_ptr, jlong src_asset_manager_ptr, jlong src_theme_ptr) {
  @Implementation(minSdk = Build.VERSION_CODES.Q)
  protected static void nativeThemeCopy(
      long dst_asset_manager_ptr,
      long dst_theme_ptr,
      long src_asset_manager_ptr,
      long src_theme_ptr) {
    Theme dst_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(dst_theme_ptr);
    Theme src_theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(src_theme_ptr);

    if (dst_asset_manager_ptr != src_asset_manager_ptr) {
      CppAssetManager2 dst_assetmanager = AssetManagerFromLong(dst_asset_manager_ptr);
      CHECK(dst_theme.GetAssetManager() == dst_assetmanager);

      CppAssetManager2 src_assetmanager = AssetManagerFromLong(src_asset_manager_ptr);
      CHECK(src_theme.GetAssetManager() == src_assetmanager);

      dst_theme.SetTo(src_theme);
    } else {
      dst_theme.SetTo(src_theme);
    }
  }

  // static void NativeThemeClear(JNIEnv* /*env*/, jclass /*clazz*/, jlong theme_ptr) {
  @Implementation(minSdk = P, maxSdk = R)
  protected static void nativeThemeClear(long themePtr) {
    Registries.NATIVE_THEME9_REGISTRY.getNativeObject(themePtr).Clear();
  }

  // static jint NativeThemeGetAttributeValue(JNIEnv* env, jclass /*clazz*/, jlong ptr, jlong
  // theme_ptr,
  //                                          jint resid, jobject typed_value,
  //                                          jboolean resolve_references) {
  @Implementation(minSdk = P)
  protected static int nativeThemeGetAttributeValue(
      long ptr,
      long theme_ptr,
      @AttrRes int resid,
      @NonNull TypedValue typed_value,
      boolean resolve_references) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    CHECK(theme.GetAssetManager() == assetmanager);
    // (void) assetmanager; // huh?

    final Ref<Res_value> value = new Ref<>(null);
    final Ref<Integer> flags = new Ref<>(null);
    ApkAssetsCookie cookie = theme.GetAttribute(resid, value, flags);
    if (cookie.intValue() == kInvalidCookie) {
      return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
    }

    final Ref<Integer> ref = new Ref<>(0);
    if (resolve_references) {
      final Ref<ResTable_config> selected_config = new Ref<>(null);
      cookie = theme.GetAssetManager().ResolveReference(cookie, value, selected_config, flags, ref);
      if (cookie.intValue() == kInvalidCookie) {
        return ApkAssetsCookieToJavaCookie(K_INVALID_COOKIE);
      }
    }
    return CopyValue(cookie, value.get(), ref.get(), flags.get(), null, typed_value);
  }

  // static void NativeThemeDump(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr, jlong theme_ptr,
  //                             jint priority, jstring tag, jstring prefix) {
  @Implementation(minSdk = P)
  protected static void nativeThemeDump(
      long ptr, long theme_ptr, int priority, String tag, String prefix) {
    CppAssetManager2 assetmanager = AssetManagerFromLong(ptr);
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    CHECK(theme.GetAssetManager() == assetmanager);
    // (void) assetmanager;
    // (void) theme;
    // (void) priority;
    // (void) tag;
    // (void) prefix;
  }

  // static jint NativeThemeGetChangingConfigurations(JNIEnv* /*env*/, jclass /*clazz*/,
  //                                                  jlong theme_ptr) {
  @Implementation(minSdk = P)
  protected static @NativeConfig int nativeThemeGetChangingConfigurations(long theme_ptr) {
    Theme theme = Registries.NATIVE_THEME9_REGISTRY.getNativeObject(theme_ptr);
    return (int) (theme.GetChangingConfigurations());
  }

  // static void NativeAssetDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) {
  @Implementation(minSdk = P)
  protected static void nativeAssetDestroy(long asset_ptr) {
    Registries.NATIVE_ASSET_REGISTRY.unregister(asset_ptr);
  }

  // static jint NativeAssetReadChar(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) {
  @Implementation(minSdk = P)
  protected static int nativeAssetReadChar(long asset_ptr) {
    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    byte[] b = new byte[1];
    int res = asset.read(b, 1);
    return res == 1 ? (int) (b[0]) & 0xff : -1;
  }

  // static jint NativeAssetRead(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jbyteArray
  // java_buffer,
  //                             jint offset, jint len) {
  @Implementation(minSdk = P)
  protected static int nativeAssetRead(long asset_ptr, byte[] java_buffer, int offset, int len)
      throws IOException {
    if (len == 0) {
      return 0;
    }

    int buffer_len = java_buffer.length;
    if (offset < 0
        || offset >= buffer_len
        || len < 0
        || len > buffer_len
        || offset > buffer_len - len) {
      throw new IndexOutOfBoundsException();
    }

    // ScopedByteArrayRW byte_array(env, java_buffer);
    // if (byte_array.get() == null) {
    //   return -1;
    // }

    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    // sint res = asset.read(byte_array.get() + offset, len);
    int res = asset.read(java_buffer, offset, len);
    if (res < 0) {
      throw new IOException();
    }
    return res > 0 ? (int) (res) : -1;
  }

  // static jlong NativeAssetSeek(JNIEnv* env, jclass /*clazz*/, jlong asset_ptr, jlong offset,
  //                              jint whence) {
  @Implementation(minSdk = P)
  protected static long nativeAssetSeek(long asset_ptr, long offset, int whence) {
    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    return asset.seek((offset), (whence > 0 ? SEEK_END : (whence < 0 ? SEEK_SET : SEEK_CUR)));
  }

  // static jlong NativeAssetGetLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr) {
  @Implementation(minSdk = P)
  protected static long nativeAssetGetLength(long asset_ptr) {
    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    return asset.getLength();
  }

  // static jlong NativeAssetGetRemainingLength(JNIEnv* /*env*/, jclass /*clazz*/, jlong asset_ptr)
  // {
  @Implementation(minSdk = P)
  protected static long nativeAssetGetRemainingLength(long asset_ptr) {
    Asset asset = Registries.NATIVE_ASSET_REGISTRY.getNativeObject(asset_ptr);
    return asset.getRemainingLength();
  }

  // ----------------------------------------------------------------------------

  // JNI registration.
  // static JNINativeMethod gAssetManagerMethods[] = {
  //     // AssetManager setup methods.
  //     {"nativeCreate", "()J", (void*)NativeCreate},
  //   {"nativeDestroy", "(J)V", (void*)NativeDestroy},
  //   {"nativeSetApkAssets", "(J[Landroid/content/res/ApkAssets;Z)V", (void*)NativeSetApkAssets},
  //   {"nativeSetConfiguration", "(JIILjava/lang/String;IIIIIIIIIIIIIII)V",
  //   (void*)NativeSetConfiguration},
  //   {"nativeGetAssignedPackageIdentifiers", "(J)Landroid/util/SparseArray;",
  //   (void*)NativeGetAssignedPackageIdentifiers},
  //
  //   // AssetManager file methods.
  //   {"nativeList", "(JLjava/lang/String;)[Ljava/lang/String;", (void*)NativeList},
  //   {"nativeOpenAsset", "(JLjava/lang/String;I)J", (void*)NativeOpenAsset},
  //   {"nativeOpenAssetFd", "(JLjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;",
  //   (void*)NativeOpenAssetFd},
  //   {"nativeOpenNonAsset", "(JILjava/lang/String;I)J", (void*)NativeOpenNonAsset},
  //   {"nativeOpenNonAssetFd", "(JILjava/lang/String;[J)Landroid/os/ParcelFileDescriptor;",
  //   (void*)NativeOpenNonAssetFd},
  //   {"nativeOpenXmlAsset", "(JILjava/lang/String;)J", (void*)NativeOpenXmlAsset},
  //
  //   // AssetManager resource methods.
  //   {"nativeGetResourceValue", "(JISLandroid/util/TypedValue;Z)I",
  // (void*)NativeGetResourceValue},
  //   {"nativeGetResourceBagValue", "(JIILandroid/util/TypedValue;)I",
  //   (void*)NativeGetResourceBagValue},
  //   {"nativeGetStyleAttributes", "(JI)[I", (void*)NativeGetStyleAttributes},
  //   {"nativeGetResourceStringArray", "(JI)[Ljava/lang/String;",
  //   (void*)NativeGetResourceStringArray},
  //   {"nativeGetResourceStringArrayInfo", "(JI)[I", (void*)NativeGetResourceStringArrayInfo},
  //   {"nativeGetResourceIntArray", "(JI)[I", (void*)NativeGetResourceIntArray},
  //   {"nativeGetResourceArraySize", "(JI)I", (void*)NativeGetResourceArraySize},
  //   {"nativeGetResourceArray", "(JI[I)I", (void*)NativeGetResourceArray},
  //
  //   // AssetManager resource name/ID methods.
  //   {"nativeGetResourceIdentifier", "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)I",
  //   (void*)NativeGetResourceIdentifier},
  //   {"nativeGetResourceName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceName},
  //   {"nativeGetResourcePackageName", "(JI)Ljava/lang/String;",
  // (void*)NativeGetResourcePackageName},
  //   {"nativeGetResourceTypeName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceTypeName},
  //   {"nativeGetResourceEntryName", "(JI)Ljava/lang/String;", (void*)NativeGetResourceEntryName},
  //   {"nativeGetLocales", "(JZ)[Ljava/lang/String;", (void*)NativeGetLocales},
  //   {"nativeGetSizeConfigurations", "(J)[Landroid/content/res/Configuration;",
  //   (void*)NativeGetSizeConfigurations},
  //
  //   // Style attribute related methods.
  //   {"nativeApplyStyle", "(JJIIJ[IJJ)V", (void*)NativeApplyStyle},
  //   {"nativeResolveAttrs", "(JJII[I[I[I[I)Z", (void*)NativeResolveAttrs},
  //   {"nativeRetrieveAttributes", "(JJ[I[I[I)Z", (void*)NativeRetrieveAttributes},
  //
  //   // Theme related methods.
  //   {"nativeThemeCreate", "(J)J", (void*)NativeThemeCreate},
  //   {"nativeThemeDestroy", "(J)V", (void*)NativeThemeDestroy},
  //   {"nativeThemeApplyStyle", "(JJIZ)V", (void*)NativeThemeApplyStyle},
  //   {"nativeThemeCopy", "(JJ)V", (void*)NativeThemeCopy},
  //   {"nativeThemeClear", "(J)V", (void*)NativeThemeClear},
  //   {"nativeThemeGetAttributeValue", "(JJILandroid/util/TypedValue;Z)I",
  //   (void*)NativeThemeGetAttributeValue},
  //   {"nativeThemeDump", "(JJILjava/lang/String;Ljava/lang/String;)V", (void*)NativeThemeDump},
  //   {"nativeThemeGetChangingConfigurations", "(J)I",
  // (void*)NativeThemeGetChangingConfigurations},
  //
  //   // AssetInputStream methods.
  //   {"nativeAssetDestroy", "(J)V", (void*)NativeAssetDestroy},
  //   {"nativeAssetReadChar", "(J)I", (void*)NativeAssetReadChar},
  //   {"nativeAssetRead", "(J[BII)I", (void*)NativeAssetRead},
  //   {"nativeAssetSeek", "(JJI)J", (void*)NativeAssetSeek},
  //   {"nativeAssetGetLength", "(J)J", (void*)NativeAssetGetLength},
  //   {"nativeAssetGetRemainingLength", "(J)J", (void*)NativeAssetGetRemainingLength},
  //
  //   // System/idmap related methods.
  //   {"nativeVerifySystemIdmaps", "()V", (void*)NativeVerifySystemIdmaps},
  //
  //   // Global management/debug methods.
  //   {"getGlobalAssetCount", "()I", (void*)NativeGetGlobalAssetCount},
  //   {"getAssetAllocations", "()Ljava/lang/String;", (void*)NativeGetAssetAllocations},
  //   {"getGlobalAssetManagerCount", "()I", (void*)NativeGetGlobalAssetManagerCount},
  //   };
  //
  //   int register_android_content_AssetManager(JNIEnv* env) {
  //   jclass apk_assets_class = FindClassOrDie(env, "android/content/res/ApkAssets");
  //   gApkAssetsFields.native_ptr = GetFieldIDOrDie(env, apk_assets_class, "mNativePtr", "J");
  //
  //   jclass typedValue = FindClassOrDie(env, "android/util/TypedValue");
  //   gTypedValueOffsets.mType = GetFieldIDOrDie(env, typedValue, "type", "I");
  //   gTypedValueOffsets.mData = GetFieldIDOrDie(env, typedValue, "data", "I");
  //   gTypedValueOffsets.mString =
  //   GetFieldIDOrDie(env, typedValue, "string", "Ljava/lang/CharSequence;");
  //   gTypedValueOffsets.mAssetCookie = GetFieldIDOrDie(env, typedValue, "assetCookie", "I");
  //   gTypedValueOffsets.mResourceId = GetFieldIDOrDie(env, typedValue, "resourceId", "I");
  //   gTypedValueOffsets.mChangingConfigurations =
  //   GetFieldIDOrDie(env, typedValue, "changingConfigurations", "I");
  //   gTypedValueOffsets.mDensity = GetFieldIDOrDie(env, typedValue, "density", "I");
  //
  //   jclass assetFd = FindClassOrDie(env, "android/content/res/AssetFileDescriptor");
  //   gAssetFileDescriptorOffsets.mFd =
  //   GetFieldIDOrDie(env, assetFd, "mFd", "Landroid/os/ParcelFileDescriptor;");
  //   gAssetFileDescriptorOffsets.mStartOffset = GetFieldIDOrDie(env, assetFd, "mStartOffset",
  // "J");
  //   gAssetFileDescriptorOffsets.mLength = GetFieldIDOrDie(env, assetFd, "mLength", "J");
  //
  //   jclass assetManager = FindClassOrDie(env, "android/content/res/AssetManager");
  //   gAssetManagerOffsets.mObject = GetFieldIDOrDie(env, assetManager, "mObject", "J");
  //
  //   jclass stringClass = FindClassOrDie(env, "java/lang/String");
  //   g_stringClass = MakeGlobalRefOrDie(env, stringClass);
  //
  //   jclass sparseArrayClass = FindClassOrDie(env, "android/util/SparseArray");
  //   gSparseArrayOffsets.classObject = MakeGlobalRefOrDie(env, sparseArrayClass);
  //   gSparseArrayOffsets.constructor =
  //   GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "<init>", "()V");
  //   gSparseArrayOffsets.put =
  //   GetMethodIDOrDie(env, gSparseArrayOffsets.classObject, "put", "(ILjava/lang/Object;)V");
  //
  //   jclass configurationClass = FindClassOrDie(env, "android/content/res/Configuration");
  //   gConfigurationOffsets.classObject = MakeGlobalRefOrDie(env, configurationClass);
  //   gConfigurationOffsets.constructor = GetMethodIDOrDie(env, configurationClass, "<init>",
  // "()V");
  //   gConfigurationOffsets.mSmallestScreenWidthDpOffset =
  //   GetFieldIDOrDie(env, configurationClass, "smallestScreenWidthDp", "I");
  //   gConfigurationOffsets.mScreenWidthDpOffset =
  //   GetFieldIDOrDie(env, configurationClass, "screenWidthDp", "I");
  //   gConfigurationOffsets.mScreenHeightDpOffset =
  //   GetFieldIDOrDie(env, configurationClass, "screenHeightDp", "I");
  //
  //   return RegisterMethodsOrDie(env, "android/content/res/AssetManager", gAssetManagerMethods,
  //   NELEM(gAssetManagerMethods));
  //   }

  @ForType(AssetManager.class)
  interface AssetManagerReflector {

    @Static
    @Direct
    void createSystemAssetsInZygoteLocked();
  }
}
// namespace android