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
|
/*
* Copyright (C) 2018 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.
*/
#include "DumpManifest.h"
#include <algorithm>
#include "LoadedApk.h"
#include "SdkConstants.h"
#include "ValueVisitor.h"
#include "io/File.h"
#include "io/FileStream.h"
#include "process/IResourceTableConsumer.h"
#include "xml/XmlDom.h"
#include "androidfw/ConfigDescription.h"
using ::android::base::StringPrintf;
using ::android::ConfigDescription;
namespace aapt {
/**
* These are attribute resource constants for the platform, as found in android.R.attr.
*/
enum {
LABEL_ATTR = 0x01010001,
ICON_ATTR = 0x01010002,
NAME_ATTR = 0x01010003,
PERMISSION_ATTR = 0x01010006,
EXPORTED_ATTR = 0x01010010,
GRANT_URI_PERMISSIONS_ATTR = 0x0101001b,
PRIORITY_ATTR = 0x0101001c,
RESOURCE_ATTR = 0x01010025,
DEBUGGABLE_ATTR = 0x0101000f,
TARGET_PACKAGE_ATTR = 0x01010021,
VALUE_ATTR = 0x01010024,
VERSION_CODE_ATTR = 0x0101021b,
VERSION_NAME_ATTR = 0x0101021c,
SCREEN_ORIENTATION_ATTR = 0x0101001e,
MIN_SDK_VERSION_ATTR = 0x0101020c,
MAX_SDK_VERSION_ATTR = 0x01010271,
REQ_TOUCH_SCREEN_ATTR = 0x01010227,
REQ_KEYBOARD_TYPE_ATTR = 0x01010228,
REQ_HARD_KEYBOARD_ATTR = 0x01010229,
REQ_NAVIGATION_ATTR = 0x0101022a,
REQ_FIVE_WAY_NAV_ATTR = 0x01010232,
TARGET_SDK_VERSION_ATTR = 0x01010270,
TEST_ONLY_ATTR = 0x01010272,
ANY_DENSITY_ATTR = 0x0101026c,
GL_ES_VERSION_ATTR = 0x01010281,
SMALL_SCREEN_ATTR = 0x01010284,
NORMAL_SCREEN_ATTR = 0x01010285,
LARGE_SCREEN_ATTR = 0x01010286,
XLARGE_SCREEN_ATTR = 0x010102bf,
REQUIRED_ATTR = 0x0101028e,
INSTALL_LOCATION_ATTR = 0x010102b7,
SCREEN_SIZE_ATTR = 0x010102ca,
SCREEN_DENSITY_ATTR = 0x010102cb,
REQUIRES_SMALLEST_WIDTH_DP_ATTR = 0x01010364,
COMPATIBLE_WIDTH_LIMIT_DP_ATTR = 0x01010365,
LARGEST_WIDTH_LIMIT_DP_ATTR = 0x01010366,
PUBLIC_KEY_ATTR = 0x010103a6,
CATEGORY_ATTR = 0x010103e8,
BANNER_ATTR = 0x10103f2,
ISGAME_ATTR = 0x10103f4,
VERSION_ATTR = 0x01010519,
CERT_DIGEST_ATTR = 0x01010548,
REQUIRED_FEATURE_ATTR = 0x01010557,
REQUIRED_NOT_FEATURE_ATTR = 0x01010558,
IS_STATIC_ATTR = 0x0101055a,
REQUIRED_SYSTEM_PROPERTY_NAME_ATTR = 0x01010565,
REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR = 0x01010566,
COMPILE_SDK_VERSION_ATTR = 0x01010572,
COMPILE_SDK_VERSION_CODENAME_ATTR = 0x01010573,
VERSION_MAJOR_ATTR = 0x01010577,
PACKAGE_TYPE_ATTR = 0x01010587,
};
const std::string& kAndroidNamespace = "http://schemas.android.com/apk/res/android";
/** Retrieves the attribute of the element with the specified attribute resource id. */
static xml::Attribute* FindAttribute(xml::Element *el, uint32_t resd_id) {
for (auto& a : el->attributes) {
if (a.compiled_attribute && a.compiled_attribute.value().id) {
if (a.compiled_attribute.value().id.value() == resd_id) {
return std::move(&a);
}
}
}
return nullptr;
}
/** Retrieves the attribute of the element that has the specified namespace and attribute name. */
static xml::Attribute* FindAttribute(xml::Element *el, const std::string &package,
const std::string &name) {
return el->FindAttribute(package, name);
}
class CommonFeatureGroup;
class ManifestExtractor {
public:
explicit ManifestExtractor(LoadedApk* apk, DumpManifestOptions& options)
: apk_(apk), options_(options) { }
class Element {
public:
Element() = default;
virtual ~Element() = default;
static std::unique_ptr<Element> Inflate(ManifestExtractor* extractor, xml::Element* el);
/** Writes out the extracted contents of the element. */
virtual void Print(text::Printer* printer) { }
/** Adds an element to the list of children of the element. */
void AddChild(std::unique_ptr<Element>& child) { children_.push_back(std::move(child)); }
/** Retrieves the list of children of the element. */
const std::vector<std::unique_ptr<Element>>& children() const {
return children_;
}
/** Retrieves the extracted xml element tag. */
const std::string tag() const {
return tag_;
}
protected:
ManifestExtractor* extractor() const {
return extractor_;
}
/** Retrieves and stores the information extracted from the xml element. */
virtual void Extract(xml::Element* el) { }
/*
* Retrieves a configuration value of the resource entry that best matches the specified
* configuration.
*/
static Value* BestConfigValue(ResourceEntry* entry,
const ConfigDescription& match) {
if (!entry) {
return nullptr;
}
// Determine the config that best matches the desired config
ResourceConfigValue* best_value = nullptr;
for (auto& value : entry->values) {
if (!value->config.match(match)) {
continue;
}
if (best_value != nullptr) {
if (!value->config.isBetterThan(best_value->config, &match)) {
if (value->config.compare(best_value->config) != 0) {
continue;
}
}
}
best_value = value.get();
}
// The entry has no values
if (!best_value) {
return nullptr;
}
return best_value->value.get();
}
/** Retrieves the resource assigned to the specified resource id if one exists. */
Value* FindValueById(const ResourceTable* table, const ResourceId& res_id,
const ConfigDescription& config = DummyConfig()) {
if (table) {
for (auto& package : table->packages) {
if (package->id && package->id.value() == res_id.package_id()) {
for (auto& type : package->types) {
if (type->id && type->id.value() == res_id.type_id()) {
for (auto& entry : type->entries) {
if (entry->id && entry->id.value() == res_id.entry_id()) {
if (auto value = BestConfigValue(entry.get(), config)) {
return value;
}
}
}
}
}
}
}
}
return nullptr;
}
/** Attempts to resolve the reference to a non-reference value. */
Value* ResolveReference(Reference* ref, const ConfigDescription& config = DummyConfig()) {
const int kMaxIterations = 40;
int i = 0;
while (ref && ref->id && i++ < kMaxIterations) {
auto table = extractor_->apk_->GetResourceTable();
if (auto value = FindValueById(table, ref->id.value(), config)) {
if (ValueCast<Reference>(value)) {
ref = ValueCast<Reference>(value);
} else {
return value;
}
}
}
return nullptr;
}
/**
* Retrieves the integer value of the attribute . If the value of the attribute is a reference,
* this will attempt to resolve the reference to an integer value.
**/
int32_t* GetAttributeInteger(xml::Attribute* attr,
const ConfigDescription& config = DummyConfig()) {
if (attr != nullptr) {
if (attr->compiled_value) {
// Resolve references using the dummy configuration
Value* value = attr->compiled_value.get();
if (ValueCast<Reference>(value)) {
value = ResolveReference(ValueCast<Reference>(value), config);
} else {
value = attr->compiled_value.get();
}
// Retrieve the integer data if possible
if (value != nullptr) {
if (BinaryPrimitive* intValue = ValueCast<BinaryPrimitive>(value)) {
return (int32_t*) &intValue->value.data;
}
}
}
}
return nullptr;
}
/**
* A version of GetAttributeInteger that returns a default integer if the attribute does not
* exist or cannot be resolved to an integer value.
**/
int32_t GetAttributeIntegerDefault(xml::Attribute* attr, int32_t def,
const ConfigDescription& config = DummyConfig()) {
auto value = GetAttributeInteger(attr, config);
if (value) {
return *value;
}
return def;
}
/**
* Retrieves the string value of the attribute. If the value of the attribute is a reference,
* this will attempt to resolve the reference to a string value.
**/
const std::string* GetAttributeString(xml::Attribute* attr,
const ConfigDescription& config = DummyConfig()) {
if (attr != nullptr) {
if (attr->compiled_value) {
// Resolve references using the dummy configuration
Value* value = attr->compiled_value.get();
if (ValueCast<Reference>(value)) {
value = ResolveReference(ValueCast<Reference>(value), config);
} else {
value = attr->compiled_value.get();
}
// Retrieve the string data of the value if possible
if (value != nullptr) {
if (String* intValue = ValueCast<String>(value)) {
return &(*intValue->value);
} else if (RawString* rawValue = ValueCast<RawString>(value)) {
return &(*rawValue->value);
} else if (FileReference* strValue = ValueCast<FileReference>(value)) {
return &(*strValue->path);
}
}
}
return &attr->value;
}
return nullptr;
}
/**
* A version of GetAttributeString that returns a default string if the attribute does not
* exist or cannot be resolved to an string value.
**/
std::string GetAttributeStringDefault(xml::Attribute* attr, std::string def,
const ConfigDescription& config = DummyConfig()) {
auto value = GetAttributeString(attr, config);
if (value) {
return *value;
}
return def;
}
private:
ManifestExtractor* extractor_;
std::vector<std::unique_ptr<Element>> children_;
std::string tag_;
};
friend Element;
/** Creates a default configuration used to retrieve resources. */
static ConfigDescription DummyConfig() {
ConfigDescription config;
config.orientation = android::ResTable_config::ORIENTATION_PORT;
config.density = android::ResTable_config::DENSITY_MEDIUM;
config.sdkVersion = 10000; // Very high.
config.screenWidthDp = 320;
config.screenHeightDp = 480;
config.smallestScreenWidthDp = 320;
config.screenLayout |= android::ResTable_config::SCREENSIZE_NORMAL;
return config;
}
bool Dump(text::Printer* printer, IDiagnostics* diag);
/** Recursively visit the xml element tree and return a processed badging element tree. */
std::unique_ptr<Element> Visit(xml::Element* element);
/** Raises the target sdk value if the min target is greater than the current target. */
void RaiseTargetSdk(int32_t min_target) {
if (min_target > target_sdk_) {
target_sdk_ = min_target;
}
}
/**
* Retrieves the default feature group that features are added into when <uses-feature>
* are not in a <feature-group> element.
**/
CommonFeatureGroup* GetCommonFeatureGroup() {
return commonFeatureGroup_.get();
}
/**
* Retrieves a mapping of density values to Configurations for retrieving resources that would be
* used for that density setting.
**/
const std::map<uint16_t, ConfigDescription> densities() const {
return densities_;
}
/**
* Retrieves a mapping of locale BCP 47 strings to Configurations for retrieving resources that
* would be used for that locale setting.
**/
const std::map<std::string, ConfigDescription> locales() const {
return locales_;
}
/** Retrieves the current stack of parent during data extraction. */
const std::vector<Element*> parent_stack() const {
return parent_stack_;
}
int32_t target_sdk() const {
return target_sdk_;
}
LoadedApk* const apk_;
DumpManifestOptions& options_;
private:
std::unique_ptr<CommonFeatureGroup> commonFeatureGroup_ = util::make_unique<CommonFeatureGroup>();
std::map<std::string, ConfigDescription> locales_;
std::map<uint16_t, ConfigDescription> densities_;
std::vector<Element*> parent_stack_;
int32_t target_sdk_ = 0;
};
template<typename T> T* ElementCast(ManifestExtractor::Element* element);
/** Recurs through the children of the specified root in depth-first order. */
static void ForEachChild(ManifestExtractor::Element* root,
std::function<void(ManifestExtractor::Element*)> f) {
for (auto& child : root->children()) {
f(child.get());
ForEachChild(child.get(), f);
}
}
/**
* Checks the element and its recursive children for an element that makes the specified
* conditional function return true. Returns the first element that makes the conditional function
* return true.
**/
static ManifestExtractor::Element* FindElement(ManifestExtractor::Element* root,
std::function<bool(ManifestExtractor::Element*)> f) {
if (f(root)) {
return root;
}
for (auto& child : root->children()) {
if (auto b2 = FindElement(child.get(), f)) {
return b2;
}
}
return nullptr;
}
/** Represents the <manifest> elements **/
class Manifest : public ManifestExtractor::Element {
public:
Manifest() = default;
std::string package;
int32_t versionCode;
std::string versionName;
const std::string* split = nullptr;
const std::string* platformVersionName = nullptr;
const std::string* platformVersionCode = nullptr;
const int32_t* compilesdkVersion = nullptr;
const std::string* compilesdkVersionCodename = nullptr;
const int32_t* installLocation = nullptr;
void Extract(xml::Element* manifest) override {
package = GetAttributeStringDefault(FindAttribute(manifest, {}, "package"), "");
versionCode = GetAttributeIntegerDefault(FindAttribute(manifest, VERSION_CODE_ATTR), 0);
versionName = GetAttributeStringDefault(FindAttribute(manifest, VERSION_NAME_ATTR), "");
split = GetAttributeString(FindAttribute(manifest, {}, "split"));
// Extract the platform build info
platformVersionName = GetAttributeString(FindAttribute(manifest, {},
"platformBuildVersionName"));
platformVersionCode = GetAttributeString(FindAttribute(manifest, {},
"platformBuildVersionCode"));
// Extract the compile sdk info
compilesdkVersion = GetAttributeInteger(FindAttribute(manifest, COMPILE_SDK_VERSION_ATTR));
compilesdkVersionCodename = GetAttributeString(
FindAttribute(manifest, COMPILE_SDK_VERSION_CODENAME_ATTR));
installLocation = GetAttributeInteger(FindAttribute(manifest, INSTALL_LOCATION_ATTR));
}
void Print(text::Printer* printer) override {
printer->Print(StringPrintf("package: name='%s' ", package.data()));
printer->Print(StringPrintf("versionCode='%s' ",
(versionCode > 0) ? std::to_string(versionCode).data() : ""));
printer->Print(StringPrintf("versionName='%s'", versionName.data()));
if (split) {
printer->Print(StringPrintf(" split='%s'", split->data()));
}
if (platformVersionName) {
printer->Print(StringPrintf(" platformBuildVersionName='%s'", platformVersionName->data()));
}
if (platformVersionCode) {
printer->Print(StringPrintf(" platformBuildVersionCode='%s'", platformVersionCode->data()));
}
if (compilesdkVersion) {
printer->Print(StringPrintf(" compileSdkVersion='%d'", *compilesdkVersion));
}
if (compilesdkVersionCodename) {
printer->Print(StringPrintf(" compileSdkVersionCodename='%s'",
compilesdkVersionCodename->data()));
}
printer->Print("\n");
if (installLocation) {
switch (*installLocation) {
case 0:
printer->Print("install-location:'auto'\n");
break;
case 1:
printer->Print("install-location:'internalOnly'\n");
break;
case 2:
printer->Print("install-location:'preferExternal'\n");
break;
default:
break;
}
}
}
};
/** Represents <application> elements. **/
class Application : public ManifestExtractor::Element {
public:
Application() = default;
std::string label;
std::string icon;
std::string banner;
int32_t is_game;
int32_t debuggable;
int32_t test_only;
bool has_multi_arch;
/** Mapping from locales to app names. */
std::map<std::string, std::string> locale_labels;
/** Mapping from densities to app icons. */
std::map<uint16_t, std::string> density_icons;
void Extract(xml::Element* element) override {
label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
test_only = GetAttributeIntegerDefault(FindAttribute(element, TEST_ONLY_ATTR), 0);
banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
is_game = GetAttributeIntegerDefault(FindAttribute(element, ISGAME_ATTR), 0);
debuggable = GetAttributeIntegerDefault(FindAttribute(element, DEBUGGABLE_ATTR), 0);
// We must search by name because the multiArch flag hasn't been API
// frozen yet.
has_multi_arch = (GetAttributeIntegerDefault(
FindAttribute(element, kAndroidNamespace, "multiArch"), 0) != 0);
// Retrieve the app names for every locale the app supports
auto attr = FindAttribute(element, LABEL_ATTR);
for (auto& config : extractor()->locales()) {
if (auto label = GetAttributeString(attr, config.second)) {
if (label) {
locale_labels.insert(std::make_pair(config.first, *label));
}
}
}
// Retrieve the icons for the densities the app supports
attr = FindAttribute(element, ICON_ATTR);
for (auto& config : extractor()->densities()) {
if (auto resource = GetAttributeString(attr, config.second)) {
if (resource) {
density_icons.insert(std::make_pair(config.first, *resource));
}
}
}
}
void Print(text::Printer* printer) override {
// Print the labels for every locale
for (auto p : locale_labels) {
if (p.first.empty()) {
printer->Print(StringPrintf("application-label:'%s'\n",
android::ResTable::normalizeForOutput(p.second.data())
.c_str()));
} else {
printer->Print(StringPrintf("application-label-%s:'%s'\n", p.first.data(),
android::ResTable::normalizeForOutput(p.second.data())
.c_str()));
}
}
// Print the icon paths for every density
for (auto p : density_icons) {
printer->Print(StringPrintf("application-icon-%d:'%s'\n", p.first, p.second.data()));
}
// Print the application info
printer->Print(StringPrintf("application: label='%s' ",
android::ResTable::normalizeForOutput(label.data()).c_str()));
printer->Print(StringPrintf("icon='%s'", icon.data()));
if (!banner.empty()) {
printer->Print(StringPrintf(" banner='%s'", banner.data()));
}
printer->Print("\n");
if (test_only != 0) {
printer->Print(StringPrintf("testOnly='%d'\n", test_only));
}
if (is_game != 0) {
printer->Print("application-isGame\n");
}
if (debuggable != 0) {
printer->Print("application-debuggable\n");
}
}
};
/** Represents <uses-sdk> elements. **/
class UsesSdkBadging : public ManifestExtractor::Element {
public:
UsesSdkBadging() = default;
const int32_t* min_sdk = nullptr;
const std::string* min_sdk_name = nullptr;
const int32_t* max_sdk = nullptr;
const int32_t* target_sdk = nullptr;
const std::string* target_sdk_name = nullptr;
void Extract(xml::Element* element) override {
min_sdk = GetAttributeInteger(FindAttribute(element, MIN_SDK_VERSION_ATTR));
min_sdk_name = GetAttributeString(FindAttribute(element, MIN_SDK_VERSION_ATTR));
max_sdk = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
target_sdk = GetAttributeInteger(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
target_sdk_name = GetAttributeString(FindAttribute(element, TARGET_SDK_VERSION_ATTR));
// Detect the target sdk of the element
if ((min_sdk_name && *min_sdk_name == "Donut")
|| (target_sdk_name && *target_sdk_name == "Donut")) {
extractor()->RaiseTargetSdk(4);
}
if (min_sdk) {
extractor()->RaiseTargetSdk(*min_sdk);
}
if (target_sdk) {
extractor()->RaiseTargetSdk(*target_sdk);
}
}
void Print(text::Printer* printer) override {
if (min_sdk) {
printer->Print(StringPrintf("sdkVersion:'%d'\n", *min_sdk));
} else if (min_sdk_name) {
printer->Print(StringPrintf("sdkVersion:'%s'\n", min_sdk_name->data()));
}
if (max_sdk) {
printer->Print(StringPrintf("maxSdkVersion:'%d'\n", *max_sdk));
}
if (target_sdk) {
printer->Print(StringPrintf("targetSdkVersion:'%d'\n", *target_sdk));
} else if (target_sdk_name) {
printer->Print(StringPrintf("targetSdkVersion:'%s'\n", target_sdk_name->data()));
}
}
};
/** Represents <uses-configuration> elements. **/
class UsesConfiguarion : public ManifestExtractor::Element {
public:
UsesConfiguarion() = default;
int32_t req_touch_screen = 0;
int32_t req_keyboard_type = 0;
int32_t req_hard_keyboard = 0;
int32_t req_navigation = 0;
int32_t req_five_way_nav = 0;
void Extract(xml::Element* element) override {
req_touch_screen = GetAttributeIntegerDefault(
FindAttribute(element, REQ_TOUCH_SCREEN_ATTR), 0);
req_keyboard_type = GetAttributeIntegerDefault(
FindAttribute(element, REQ_KEYBOARD_TYPE_ATTR), 0);
req_hard_keyboard = GetAttributeIntegerDefault(
FindAttribute(element, REQ_HARD_KEYBOARD_ATTR), 0);
req_navigation = GetAttributeIntegerDefault(
FindAttribute(element, REQ_NAVIGATION_ATTR), 0);
req_five_way_nav = GetAttributeIntegerDefault(
FindAttribute(element, REQ_FIVE_WAY_NAV_ATTR), 0);
}
void Print(text::Printer* printer) override {
printer->Print("uses-configuration:");
if (req_touch_screen != 0) {
printer->Print(StringPrintf(" reqTouchScreen='%d'", req_touch_screen));
}
if (req_keyboard_type != 0) {
printer->Print(StringPrintf(" reqKeyboardType='%d'", req_keyboard_type));
}
if (req_hard_keyboard != 0) {
printer->Print(StringPrintf(" reqHardKeyboard='%d'", req_hard_keyboard));
}
if (req_navigation != 0) {
printer->Print(StringPrintf(" reqNavigation='%d'", req_navigation));
}
if (req_five_way_nav != 0) {
printer->Print(StringPrintf(" reqFiveWayNav='%d'", req_five_way_nav));
}
printer->Print("\n");
}
};
/** Represents <supports-screen> elements. **/
class SupportsScreen : public ManifestExtractor::Element {
public:
SupportsScreen() = default;
int32_t small_screen = 1;
int32_t normal_screen = 1;
int32_t large_screen = 1;
int32_t xlarge_screen = 1;
int32_t any_density = 1;
int32_t requires_smallest_width_dp = 0;
int32_t compatible_width_limit_dp = 0;
int32_t largest_width_limit_dp = 0;
void Extract(xml::Element* element) override {
small_screen = GetAttributeIntegerDefault(FindAttribute(element, SMALL_SCREEN_ATTR), 1);
normal_screen = GetAttributeIntegerDefault(FindAttribute(element, NORMAL_SCREEN_ATTR), 1);
large_screen = GetAttributeIntegerDefault(FindAttribute(element, LARGE_SCREEN_ATTR), 1);
xlarge_screen = GetAttributeIntegerDefault(FindAttribute(element, XLARGE_SCREEN_ATTR), 1);
any_density = GetAttributeIntegerDefault(FindAttribute(element, ANY_DENSITY_ATTR), 1);
requires_smallest_width_dp = GetAttributeIntegerDefault(
FindAttribute(element, REQUIRES_SMALLEST_WIDTH_DP_ATTR), 0);
compatible_width_limit_dp = GetAttributeIntegerDefault(
FindAttribute(element, COMPATIBLE_WIDTH_LIMIT_DP_ATTR), 0);
largest_width_limit_dp = GetAttributeIntegerDefault(
FindAttribute(element, LARGEST_WIDTH_LIMIT_DP_ATTR), 0);
// For modern apps, if screen size buckets haven't been specified
// but the new width ranges have, then infer the buckets from them.
if (small_screen > 0 && normal_screen > 0 && large_screen > 0 && xlarge_screen > 0
&& requires_smallest_width_dp > 0) {
int32_t compat_width = (compatible_width_limit_dp > 0) ? compatible_width_limit_dp
: requires_smallest_width_dp;
small_screen = (requires_smallest_width_dp <= 240 && compat_width >= 240) ? -1 : 0;
normal_screen = (requires_smallest_width_dp <= 320 && compat_width >= 320) ? -1 : 0;
large_screen = (requires_smallest_width_dp <= 480 && compat_width >= 480) ? -1 : 0;
xlarge_screen = (requires_smallest_width_dp <= 720 && compat_width >= 720) ? -1 : 0;
}
}
void PrintScreens(text::Printer* printer, int32_t target_sdk) {
int32_t small_screen_temp = small_screen;
int32_t normal_screen_temp = normal_screen;
int32_t large_screen_temp = large_screen;
int32_t xlarge_screen_temp = xlarge_screen;
int32_t any_density_temp = any_density;
// Determine default values for any unspecified screen sizes,
// based on the target SDK of the package. As of 4 (donut)
// the screen size support was introduced, so all default to
// enabled.
if (small_screen_temp > 0) {
small_screen_temp = target_sdk >= 4 ? -1 : 0;
}
if (normal_screen_temp > 0) {
normal_screen_temp = -1;
}
if (large_screen_temp > 0) {
large_screen_temp = target_sdk >= 4 ? -1 : 0;
}
if (xlarge_screen_temp > 0) {
// Introduced in Gingerbread.
xlarge_screen_temp = target_sdk >= 9 ? -1 : 0;
}
if (any_density_temp > 0) {
any_density_temp = (target_sdk >= 4 || requires_smallest_width_dp > 0
|| compatible_width_limit_dp > 0) ? -1 : 0;
}
// Print the formatted screen info
printer->Print("supports-screens:");
if (small_screen_temp != 0) {
printer->Print(" 'small'");
}
if (normal_screen_temp != 0) {
printer->Print(" 'normal'");
}
if (large_screen_temp != 0) {
printer->Print(" 'large'");
}
if (xlarge_screen_temp != 0) {
printer->Print(" 'xlarge'");
}
printer->Print("\n");
printer->Print(StringPrintf("supports-any-density: '%s'\n",
(any_density_temp ) ? "true" : "false"));
if (requires_smallest_width_dp > 0) {
printer->Print(StringPrintf("requires-smallest-width:'%d'\n", requires_smallest_width_dp));
}
if (compatible_width_limit_dp > 0) {
printer->Print(StringPrintf("compatible-width-limit:'%d'\n", compatible_width_limit_dp));
}
if (largest_width_limit_dp > 0) {
printer->Print(StringPrintf("largest-width-limit:'%d'\n", largest_width_limit_dp));
}
}
};
/** Represents <feature-group> elements. **/
class FeatureGroup : public ManifestExtractor::Element {
public:
FeatureGroup() = default;
std::string label;
int32_t open_gles_version = 0;
void Extract(xml::Element* element) override {
label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
}
virtual void PrintGroup(text::Printer* printer) {
printer->Print(StringPrintf("feature-group: label='%s'\n", label.data()));
if (open_gles_version > 0) {
printer->Print(StringPrintf(" uses-gl-es: '0x%x'\n", open_gles_version));
}
for (auto feature : features_) {
printer->Print(StringPrintf(" uses-feature%s: name='%s'",
(feature.second.required ? "" : "-not-required"),
feature.first.data()));
if (feature.second.version > 0) {
printer->Print(StringPrintf(" version='%d'", feature.second.version));
}
printer->Print("\n");
}
}
/** Adds a feature to the feature group. */
void AddFeature(const std::string& name, bool required = true, int32_t version = -1) {
features_.insert(std::make_pair(name, Feature{ required, version }));
if (required) {
if (name == "android.hardware.camera.autofocus" ||
name == "android.hardware.camera.flash") {
AddFeature("android.hardware.camera", true);
} else if (name == "android.hardware.location.gps" ||
name == "android.hardware.location.network") {
AddFeature("android.hardware.location", true);
} else if (name == "android.hardware.faketouch.multitouch") {
AddFeature("android.hardware.faketouch", true);
} else if (name == "android.hardware.faketouch.multitouch.distinct" ||
name == "android.hardware.faketouch.multitouch.jazzhands") {
AddFeature("android.hardware.faketouch.multitouch", true);
AddFeature("android.hardware.faketouch", true);
} else if (name == "android.hardware.touchscreen.multitouch") {
AddFeature("android.hardware.touchscreen", true);
} else if (name == "android.hardware.touchscreen.multitouch.distinct" ||
name == "android.hardware.touchscreen.multitouch.jazzhands") {
AddFeature("android.hardware.touchscreen.multitouch", true);
AddFeature("android.hardware.touchscreen", true);
} else if (name == "android.hardware.opengles.aep") {
const int kOpenGLESVersion31 = 0x00030001;
if (kOpenGLESVersion31 > open_gles_version) {
open_gles_version = kOpenGLESVersion31;
}
}
}
}
/** Returns true if the feature group has the given feature. */
virtual bool HasFeature(const std::string& name) {
return features_.find(name) != features_.end();
}
/** Merges the features of another feature group into this group. */
void Merge(FeatureGroup* group) {
open_gles_version = std::max(open_gles_version, group->open_gles_version);
for (auto& feature : group->features_) {
features_.insert(feature);
}
}
protected:
struct Feature {
public:
bool required = false;
int32_t version = -1;
};
/* Mapping of feature names to their properties. */
std::map<std::string, Feature> features_;
};
/**
* Represents the default feature group for the application if no <feature-group> elements are
* present in the manifest.
**/
class CommonFeatureGroup : public FeatureGroup {
public:
CommonFeatureGroup() = default;
void PrintGroup(text::Printer* printer) override {
FeatureGroup::PrintGroup(printer);
// Also print the implied features
for (auto feature : implied_features_) {
if (features_.find(feature.first) == features_.end()) {
const char* sdk23 = feature.second.implied_from_sdk_k23 ? "-sdk-23" : "";
printer->Print(StringPrintf(" uses-feature%s: name='%s'\n", sdk23, feature.first.data()));
printer->Print(StringPrintf(" uses-implied-feature%s: name='%s' reason='", sdk23,
feature.first.data()));
// Print the reasons as a sentence
size_t count = 0;
for (auto reason : feature.second.reasons) {
printer->Print(reason);
if (count + 2 < feature.second.reasons.size()) {
printer->Print(", ");
} else if (count + 1 < feature.second.reasons.size()) {
printer->Print(", and ");
}
count++;
}
printer->Print("'\n");
}
}
}
/** Returns true if the feature group has the given feature. */
bool HasFeature(const std::string& name) override {
return FeatureGroup::HasFeature(name)
|| implied_features_.find(name) != implied_features_.end();
}
/** Adds a feature to a set of implied features not explicitly requested in the manifest. */
void addImpliedFeature(const std::string& name, const std::string& reason, bool sdk23 = false) {
auto entry = implied_features_.find(name);
if (entry == implied_features_.end()) {
implied_features_.insert(std::make_pair(name, ImpliedFeature(sdk23)));
entry = implied_features_.find(name);
}
// A non-sdk 23 implied feature takes precedence.
if (entry->second.implied_from_sdk_k23 && !sdk23) {
entry->second.implied_from_sdk_k23 = false;
}
entry->second.reasons.insert(reason);
}
/**
* Adds a feature to a set of implied features for all features that are implied by the presence
* of the permission.
**/
void addImpliedFeaturesForPermission(int32_t targetSdk, const std::string& name, bool sdk23) {
if (name == "android.permission.CAMERA") {
addImpliedFeature("android.hardware.camera",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.ACCESS_FINE_LOCATION") {
if (targetSdk < SDK_LOLLIPOP) {
addImpliedFeature("android.hardware.location.gps",
StringPrintf("requested %s permission", name.data()),
sdk23);
addImpliedFeature("android.hardware.location.gps",
StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
sdk23);
}
addImpliedFeature("android.hardware.location",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.ACCESS_COARSE_LOCATION") {
if (targetSdk < SDK_LOLLIPOP) {
addImpliedFeature("android.hardware.location.network",
StringPrintf("requested %s permission", name.data()),
sdk23);
addImpliedFeature("android.hardware.location.network",
StringPrintf("targetSdkVersion < %d", SDK_LOLLIPOP),
sdk23);
}
addImpliedFeature("android.hardware.location",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.ACCESS_MOCK_LOCATION" ||
name == "android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" ||
name == "android.permission.INSTALL_LOCATION_PROVIDER") {
addImpliedFeature("android.hardware.location",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.BLUETOOTH" ||
name == "android.permission.BLUETOOTH_ADMIN") {
if (targetSdk > SDK_DONUT) {
addImpliedFeature("android.hardware.bluetooth",
StringPrintf("requested %s permission", name.data()),
sdk23);
addImpliedFeature("android.hardware.bluetooth",
StringPrintf("targetSdkVersion > %d", SDK_DONUT),
sdk23);
}
} else if (name == "android.permission.RECORD_AUDIO") {
addImpliedFeature("android.hardware.microphone",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.ACCESS_WIFI_STATE" ||
name == "android.permission.CHANGE_WIFI_STATE" ||
name == "android.permission.CHANGE_WIFI_MULTICAST_STATE") {
addImpliedFeature("android.hardware.wifi",
StringPrintf("requested %s permission", name.data()),
sdk23);
} else if (name == "android.permission.CALL_PHONE" ||
name == "android.permission.CALL_PRIVILEGED" ||
name == "android.permission.MODIFY_PHONE_STATE" ||
name == "android.permission.PROCESS_OUTGOING_CALLS" ||
name == "android.permission.READ_SMS" ||
name == "android.permission.RECEIVE_SMS" ||
name == "android.permission.RECEIVE_MMS" ||
name == "android.permission.RECEIVE_WAP_PUSH" ||
name == "android.permission.SEND_SMS" ||
name == "android.permission.WRITE_APN_SETTINGS" ||
name == "android.permission.WRITE_SMS") {
addImpliedFeature("android.hardware.telephony",
"requested a telephony permission",
sdk23);
}
}
private:
/**
* Represents a feature that has been automatically added due to a pre-requisite or for some
* other reason.
*/
struct ImpliedFeature {
explicit ImpliedFeature(bool sdk23 = false) : implied_from_sdk_k23(sdk23) {}
/** List of human-readable reasons for why this feature was implied. */
std::set<std::string> reasons;
// Was this implied by a permission from SDK 23 (<uses-permission-sdk-23 />)
bool implied_from_sdk_k23;
};
/* Mapping of implied feature names to their properties. */
std::map<std::string, ImpliedFeature> implied_features_;
};
/** Represents <uses-feature> elements. **/
class UsesFeature : public ManifestExtractor::Element {
public:
UsesFeature() = default;
void Extract(xml::Element* element) override {
const std::string* name = GetAttributeString(FindAttribute(element, NAME_ATTR));
int32_t* gl = GetAttributeInteger(FindAttribute(element, GL_ES_VERSION_ATTR));
bool required = GetAttributeIntegerDefault(
FindAttribute(element, REQUIRED_ATTR), true) != 0;
int32_t version = GetAttributeIntegerDefault(
FindAttribute(element, kAndroidNamespace, "version"), 0);
// Add the feature to the parent feature group element if one exists; otherwise, add it to the
// common feature group
FeatureGroup* feature_group = ElementCast<FeatureGroup>(extractor()->parent_stack()[0]);
if (!feature_group) {
feature_group = extractor()->GetCommonFeatureGroup();
} else {
// All features in side of <feature-group> elements are required.
required = true;
}
if (name) {
feature_group->AddFeature(*name, required, version);
} else if (gl) {
feature_group->open_gles_version = std::max(feature_group->open_gles_version, *gl);
}
}
};
/** Represents <uses-permission> elements. **/
class UsesPermission : public ManifestExtractor::Element {
public:
UsesPermission() = default;
std::string name;
std::string requiredFeature;
std::string requiredNotFeature;
int32_t required = true;
int32_t maxSdkVersion = -1;
void Extract(xml::Element* element) override {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
requiredFeature = GetAttributeStringDefault(
FindAttribute(element, REQUIRED_FEATURE_ATTR), "");
requiredNotFeature = GetAttributeStringDefault(
FindAttribute(element, REQUIRED_NOT_FEATURE_ATTR), "");
required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
maxSdkVersion = GetAttributeIntegerDefault(
FindAttribute(element, MAX_SDK_VERSION_ATTR), -1);
if (!name.empty()) {
CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
common->addImpliedFeaturesForPermission(extractor()->target_sdk(), name, false);
}
}
void Print(text::Printer* printer) override {
if (!name.empty()) {
printer->Print(StringPrintf("uses-permission: name='%s'", name.data()));
if (maxSdkVersion >= 0) {
printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
}
if (!requiredFeature.empty()) {
printer->Print(StringPrintf(" requiredFeature='%s'", requiredFeature.data()));
}
if (!requiredNotFeature.empty()) {
printer->Print(StringPrintf(" requiredNotFeature='%s'", requiredNotFeature.data()));
}
printer->Print("\n");
if (required == 0) {
printer->Print(StringPrintf("optional-permission: name='%s'", name.data()));
if (maxSdkVersion >= 0) {
printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
}
printer->Print("\n");
}
}
}
void PrintImplied(text::Printer* printer, const std::string& reason) {
printer->Print(StringPrintf("uses-implied-permission: name='%s'", name.data()));
if (maxSdkVersion >= 0) {
printer->Print(StringPrintf(" maxSdkVersion='%d'", maxSdkVersion));
}
printer->Print(StringPrintf(" reason='%s'\n", reason.data()));
}
};
/** Represents <uses-permission-sdk-23> elements. **/
class UsesPermissionSdk23 : public ManifestExtractor::Element {
public:
UsesPermissionSdk23() = default;
const std::string* name = nullptr;
const int32_t* maxSdkVersion = nullptr;
void Extract(xml::Element* element) override {
name = GetAttributeString(FindAttribute(element, NAME_ATTR));
maxSdkVersion = GetAttributeInteger(FindAttribute(element, MAX_SDK_VERSION_ATTR));
if (name) {
CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
common->addImpliedFeaturesForPermission(extractor()->target_sdk(), *name, true);
}
}
void Print(text::Printer* printer) override {
if (name) {
printer->Print(StringPrintf("uses-permission-sdk-23: name='%s'", name->data()));
if (maxSdkVersion) {
printer->Print(StringPrintf(" maxSdkVersion='%d'", *maxSdkVersion));
}
printer->Print("\n");
}
}
};
/** Represents <permission> elements. These elements are only printing when dumping permissions. **/
class Permission : public ManifestExtractor::Element {
public:
Permission() = default;
std::string name;
void Extract(xml::Element* element) override {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
}
void Print(text::Printer* printer) override {
if (extractor()->options_.only_permissions && !name.empty()) {
printer->Print(StringPrintf("permission: %s\n", name.data()));
}
}
};
/** Represents <activity> elements. **/
class Activity : public ManifestExtractor::Element {
public:
Activity() = default;
std::string name;
std::string icon;
std::string label;
std::string banner;
bool has_component_ = false;
bool has_launcher_category = false;
bool has_leanback_launcher_category = false;
bool has_main_action = false;
void Extract(xml::Element* element) override {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
label = GetAttributeStringDefault(FindAttribute(element, LABEL_ATTR), "");
icon = GetAttributeStringDefault(FindAttribute(element, ICON_ATTR), "");
banner = GetAttributeStringDefault(FindAttribute(element, BANNER_ATTR), "");
// Retrieve the package name from the manifest
std::string package;
for (auto& parent : extractor()->parent_stack()) {
if (auto manifest = ElementCast<Manifest>(parent)) {
package = manifest->package;
break;
}
}
// Fully qualify the activity name
ssize_t idx = name.find(".");
if (idx == 0) {
name = package + name;
} else if (idx < 0) {
name = package + "." + name;
}
auto orientation = GetAttributeInteger(FindAttribute(element, SCREEN_ORIENTATION_ATTR));
if (orientation) {
CommonFeatureGroup* common = extractor()->GetCommonFeatureGroup();
int orien = *orientation;
if (orien == 0 || orien == 6 || orien == 8) {
// Requests landscape, sensorLandscape, or reverseLandscape.
common->addImpliedFeature("android.hardware.screen.landscape",
"one or more activities have specified a landscape orientation",
false);
} else if (orien == 1 || orien == 7 || orien == 9) {
// Requests portrait, sensorPortrait, or reversePortrait.
common->addImpliedFeature("android.hardware.screen.portrait",
"one or more activities have specified a portrait orientation",
false);
}
}
}
void Print(text::Printer* printer) override {
// Print whether the activity has the HOME category and a the MAIN action
if (has_main_action && has_launcher_category) {
printer->Print("launchable-activity:");
if (!name.empty()) {
printer->Print(StringPrintf(" name='%s' ", name.data()));
}
printer->Print(StringPrintf(" label='%s' icon='%s'\n",
android::ResTable::normalizeForOutput(label.data()).c_str(),
icon.data()));
}
// Print wether the activity has the HOME category and a the MAIN action
if (has_leanback_launcher_category) {
printer->Print("leanback-launchable-activity:");
if (!name.empty()) {
printer->Print(StringPrintf(" name='%s' ", name.data()));
}
printer->Print(StringPrintf(" label='%s' icon='%s' banner='%s'\n",
android::ResTable::normalizeForOutput(label.data()).c_str(),
icon.data(), banner.data()));
}
}
};
/** Represents <intent-filter> elements. */
class IntentFilter : public ManifestExtractor::Element {
public:
IntentFilter() = default;
};
/** Represents <category> elements. */
class Category : public ManifestExtractor::Element {
public:
Category() = default;
std::string component = "";
void Extract(xml::Element* element) override {
const std::string* category = GetAttributeString(FindAttribute(element, NAME_ATTR));
auto parent_stack = extractor()->parent_stack();
if (category && ElementCast<IntentFilter>(parent_stack[0])
&& ElementCast<Activity>(parent_stack[1])) {
Activity* activity = ElementCast<Activity>(parent_stack[1]);
if (*category == "android.intent.category.LAUNCHER") {
activity->has_launcher_category = true;
} else if (*category == "android.intent.category.LEANBACK_LAUNCHER") {
activity->has_leanback_launcher_category = true;
} else if (*category == "android.intent.category.HOME") {
component = "launcher";
}
}
}
};
/**
* Represents <provider> elements. The elements may have an <intent-filter> which may have <action>
* elements nested within.
**/
class Provider : public ManifestExtractor::Element {
public:
Provider() = default;
bool has_required_saf_attributes = false;
void Extract(xml::Element* element) override {
const int32_t* exported = GetAttributeInteger(FindAttribute(element, EXPORTED_ATTR));
const int32_t* grant_uri_permissions = GetAttributeInteger(
FindAttribute(element, GRANT_URI_PERMISSIONS_ATTR));
const std::string* permission = GetAttributeString(
FindAttribute(element, PERMISSION_ATTR));
has_required_saf_attributes = ((exported && *exported != 0)
&& (grant_uri_permissions && *grant_uri_permissions != 0)
&& (permission && *permission == "android.permission.MANAGE_DOCUMENTS"));
}
};
/** Represents <receiver> elements. **/
class Receiver : public ManifestExtractor::Element {
public:
Receiver() = default;
const std::string* permission = nullptr;
bool has_component = false;
void Extract(xml::Element* element) override {
permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
}
};
/**Represents <service> elements. **/
class Service : public ManifestExtractor::Element {
public:
Service() = default;
const std::string* permission = nullptr;
bool has_component = false;
void Extract(xml::Element* element) override {
permission = GetAttributeString(FindAttribute(element, PERMISSION_ATTR));
}
};
/** Represents <uses-library> elements. **/
class UsesLibrary : public ManifestExtractor::Element {
public:
UsesLibrary() = default;
std::string name;
int required;
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
required = GetAttributeIntegerDefault(FindAttribute(element, REQUIRED_ATTR), 1);
}
}
void Print(text::Printer* printer) override {
if (!name.empty()) {
printer->Print(StringPrintf("uses-library%s:'%s'\n",
(required == 0) ? "-not-required" : "", name.data()));
}
}
};
/** Represents <static-library> elements. **/
class StaticLibrary : public ManifestExtractor::Element {
public:
StaticLibrary() = default;
std::string name;
int version;
int versionMajor;
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
}
}
void Print(text::Printer* printer) override {
printer->Print(StringPrintf(
"static-library: name='%s' version='%d' versionMajor='%d'\n",
name.data(), version, versionMajor));
}
};
/** Represents <uses-static-library> elements. **/
class UsesStaticLibrary : public ManifestExtractor::Element {
public:
UsesStaticLibrary() = default;
std::string name;
int version;
int versionMajor;
std::vector<std::string> certDigests;
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
AddCertDigest(element);
}
}
void AddCertDigest(xml::Element* element) {
std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
// We allow ":" delimiters in the SHA declaration as this is the format
// emitted by the certtool making it easy for developers to copy/paste.
digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
if (!digest.empty()) {
certDigests.push_back(digest);
}
}
void Print(text::Printer* printer) override {
printer->Print(StringPrintf(
"uses-static-library: name='%s' version='%d' versionMajor='%d'",
name.data(), version, versionMajor));
for (size_t i = 0; i < certDigests.size(); i++) {
printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
}
printer->Print("\n");
}
};
/**
* Represents <meta-data> elements. These tags are only printed when a flag is passed in to
* explicitly enable meta data printing.
**/
class MetaData : public ManifestExtractor::Element {
public:
MetaData() = default;
std::string name;
std::string value;
const int* value_int;
std::string resource;
const int* resource_int;
void Extract(xml::Element* element) override {
name = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
value = GetAttributeStringDefault(FindAttribute(element, VALUE_ATTR), "");
value_int = GetAttributeInteger(FindAttribute(element, VALUE_ATTR));
resource = GetAttributeStringDefault(FindAttribute(element, RESOURCE_ATTR), "");
resource_int = GetAttributeInteger(FindAttribute(element, RESOURCE_ATTR));
}
void Print(text::Printer* printer) override {
if (extractor()->options_.include_meta_data && !name.empty()) {
printer->Print(StringPrintf("meta-data: name='%s' ", name.data()));
if (!value.empty()) {
printer->Print(StringPrintf("value='%s' ", value.data()));
} else if (value_int) {
printer->Print(StringPrintf("value='%d' ", *value_int));
} else {
if (!resource.empty()) {
printer->Print(StringPrintf("resource='%s' ", resource.data()));
} else if (resource_int) {
printer->Print(StringPrintf("resource='%d' ", *resource_int));
}
}
printer->Print("\n");
}
}
};
/**
* Represents <action> elements. Detects the presence of certain activity, provider, receiver, and
* service components.
**/
class Action : public ManifestExtractor::Element {
public:
Action() = default;
std::string component = "";
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
std::string action = GetAttributeStringDefault(FindAttribute(element, NAME_ATTR), "");
if (ElementCast<IntentFilter>(parent_stack[0])) {
if (ElementCast<Activity>(parent_stack[1])) {
// Detects the presence of a particular type of activity.
Activity* activity = ElementCast<Activity>(parent_stack[1]);
auto map = std::map<std::string, std::string>({
{ "android.intent.action.MAIN" , "main" },
{ "android.intent.action.VIDEO_CAMERA" , "camera" },
{ "android.intent.action.STILL_IMAGE_CAMERA_SECURE" , "camera-secure" },
});
auto entry = map.find(action);
if (entry != map.end()) {
component = entry->second;
activity->has_component_ = true;
}
if (action == "android.intent.action.MAIN") {
activity->has_main_action = true;
}
} else if (ElementCast<Receiver>(parent_stack[1])) {
// Detects the presence of a particular type of receiver. If the action requires a
// permission, then the receiver element is checked for the permission.
Receiver* receiver = ElementCast<Receiver>(parent_stack[1]);
auto map = std::map<std::string, std::string>({
{ "android.appwidget.action.APPWIDGET_UPDATE" , "app-widget" },
{ "android.app.action.DEVICE_ADMIN_ENABLED" , "device-admin" },
});
auto permissions = std::map<std::string, std::string>({
{ "android.app.action.DEVICE_ADMIN_ENABLED" , "android.permission.BIND_DEVICE_ADMIN" },
});
auto entry = map.find(action);
auto permission = permissions.find(action);
if (entry != map.end() && (permission == permissions.end()
|| (receiver->permission && permission->second == *receiver->permission))) {
receiver->has_component = true;
component = entry->second;
}
} else if (ElementCast<Service>(parent_stack[1])) {
// Detects the presence of a particular type of service. If the action requires a
// permission, then the service element is checked for the permission.
Service* service = ElementCast<Service>(parent_stack[1]);
auto map = std::map<std::string, std::string>({
{ "android.view.InputMethod" , "ime" },
{ "android.service.wallpaper.WallpaperService" , "wallpaper" },
{ "android.accessibilityservice.AccessibilityService" , "accessibility" },
{ "android.printservice.PrintService" , "print-service" },
{ "android.nfc.cardemulation.action.HOST_APDU_SERVICE" , "host-apdu" },
{ "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" , "offhost-apdu" },
{ "android.service.notification.NotificationListenerService" ,"notification-listener" },
{ "android.service.dreams.DreamService" , "dream" },
});
auto permissions = std::map<std::string, std::string>({
{ "android.accessibilityservice.AccessibilityService" ,
"android.permission.BIND_ACCESSIBILITY_SERVICE" },
{ "android.printservice.PrintService" , "android.permission.BIND_PRINT_SERVICE" },
{ "android.nfc.cardemulation.action.HOST_APDU_SERVICE" ,
"android.permission.BIND_NFC_SERVICE" },
{ "android.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE" ,
"android.permission.BIND_NFC_SERVICE" },
{ "android.service.notification.NotificationListenerService" ,
"android.permission.BIND_NOTIFICATION_LISTENER_SERVICE" },
{ "android.service.dreams.DreamService" , "android.permission.BIND_DREAM_SERVICE" },
});
auto entry = map.find(action);
auto permission = permissions.find(action);
if (entry != map.end() && (permission == permissions.end()
|| (service->permission && permission->second == *service->permission))) {
service->has_component= true;
component = entry->second;
}
} else if (ElementCast<Provider>(parent_stack[1])) {
// Detects the presence of a particular type of receiver. If the provider requires a
// permission, then the provider element is checked for the permission.
// Detect whether this action
Provider* provider = ElementCast<Provider>(parent_stack[1]);
if (action == "android.content.action.DOCUMENTS_PROVIDER"
&& provider->has_required_saf_attributes) {
component = "document-provider";
}
}
}
// Represents a searchable interface
if (action == "android.intent.action.SEARCH") {
component = "search";
}
}
};
/**
* Represents <supports-input> elements. The element may have <input-type> elements nested within.
**/
class SupportsInput : public ManifestExtractor::Element {
public:
SupportsInput() = default;
std::vector<std::string> inputs;
void Print(text::Printer* printer) override {
const size_t size = inputs.size();
if (size > 0) {
printer->Print("supports-input: '");
for (size_t i = 0; i < size; i++) {
printer->Print(StringPrintf("value='%s' ", inputs[i].data()));
}
printer->Print("\n");
}
}
};
/** Represents <input-type> elements. **/
class InputType : public ManifestExtractor::Element {
public:
InputType() = default;
void Extract(xml::Element* element) override {
auto name = GetAttributeString(FindAttribute(element, NAME_ATTR));
auto parent_stack = extractor()->parent_stack();
// Add the input to the set of supported inputs
if (name && ElementCast<SupportsInput>(parent_stack[0])) {
SupportsInput* supports = ElementCast<SupportsInput>(parent_stack[0]);
supports->inputs.push_back(*name);
}
}
};
/** Represents <original-package> elements. **/
class OriginalPackage : public ManifestExtractor::Element {
public:
OriginalPackage() = default;
const std::string* name = nullptr;
void Extract(xml::Element* element) override {
name = GetAttributeString(FindAttribute(element, NAME_ATTR));
}
void Print(text::Printer* printer) override {
if (name) {
printer->Print(StringPrintf("original-package:'%s'\n", name->data()));
}
}
};
/** Represents <overlay> elements. **/
class Overlay : public ManifestExtractor::Element {
public:
Overlay() = default;
const std::string* target_package = nullptr;
int priority;
bool is_static;
const std::string* required_property_name = nullptr;
const std::string* required_property_value = nullptr;
void Extract(xml::Element* element) override {
target_package = GetAttributeString(FindAttribute(element, TARGET_PACKAGE_ATTR));
priority = GetAttributeIntegerDefault(FindAttribute(element, PRIORITY_ATTR), 0);
is_static = GetAttributeIntegerDefault(FindAttribute(element, IS_STATIC_ATTR), false) != 0;
required_property_name = GetAttributeString(
FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_NAME_ATTR));
required_property_value = GetAttributeString(
FindAttribute(element, REQUIRED_SYSTEM_PROPERTY_VALUE_ATTR));
}
void Print(text::Printer* printer) override {
printer->Print(StringPrintf("overlay:"));
if (target_package) {
printer->Print(StringPrintf(" targetPackage='%s'", target_package->c_str()));
}
printer->Print(StringPrintf(" priority='%d'", priority));
printer->Print(StringPrintf(" isStatic='%s'", is_static ? "true" : "false"));
if (required_property_name) {
printer->Print(StringPrintf(" requiredPropertyName='%s'", required_property_name->c_str()));
}
if (required_property_value) {
printer->Print(StringPrintf(" requiredPropertyValue='%s'", required_property_value->c_str()));
}
printer->Print("\n");
}
};
/** * Represents <package-verifier> elements. **/
class PackageVerifier : public ManifestExtractor::Element {
public:
PackageVerifier() = default;
const std::string* name = nullptr;
const std::string* public_key = nullptr;
void Extract(xml::Element* element) override {
name = GetAttributeString(FindAttribute(element, NAME_ATTR));
public_key = GetAttributeString(FindAttribute(element, PUBLIC_KEY_ATTR));
}
void Print(text::Printer* printer) override {
if (name && public_key) {
printer->Print(StringPrintf("package-verifier: name='%s' publicKey='%s'\n",
name->data(), public_key->data()));
}
}
};
/** Represents <uses-package> elements. **/
class UsesPackage : public ManifestExtractor::Element {
public:
UsesPackage() = default;
const std::string* packageType = nullptr;
const std::string* name = nullptr;
int version;
int versionMajor;
std::vector<std::string> certDigests;
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
if (parent_stack.size() > 0 && ElementCast<Application>(parent_stack[0])) {
packageType = GetAttributeString(FindAttribute(element, PACKAGE_TYPE_ATTR));
name = GetAttributeString(FindAttribute(element, NAME_ATTR));
version = GetAttributeIntegerDefault(FindAttribute(element, VERSION_ATTR), 0);
versionMajor = GetAttributeIntegerDefault(FindAttribute(element, VERSION_MAJOR_ATTR), 0);
AddCertDigest(element);
}
}
void AddCertDigest(xml::Element* element) {
std::string digest = GetAttributeStringDefault(FindAttribute(element, CERT_DIGEST_ATTR), "");
// We allow ":" delimiters in the SHA declaration as this is the format
// emitted by the certtool making it easy for developers to copy/paste.
digest.erase(std::remove(digest.begin(), digest.end(), ':'), digest.end());
if (!digest.empty()) {
certDigests.push_back(digest);
}
}
void Print(text::Printer* printer) override {
if (name) {
if (packageType) {
printer->Print(StringPrintf(
"uses-typed-package: type='%s' name='%s' version='%d' versionMajor='%d'",
packageType->data(), name->data(), version, versionMajor));
for (size_t i = 0; i < certDigests.size(); i++) {
printer->Print(StringPrintf(" certDigest='%s'", certDigests[i].data()));
}
printer->Print("\n");
} else {
printer->Print(StringPrintf("uses-package:'%s'\n", name->data()));
}
}
}
};
/** Represents <additional-certificate> elements. **/
class AdditionalCertificate : public ManifestExtractor::Element {
public:
AdditionalCertificate() = default;
void Extract(xml::Element* element) override {
auto parent_stack = extractor()->parent_stack();
if (parent_stack.size() > 0) {
if (ElementCast<UsesPackage>(parent_stack[0])) {
UsesPackage* uses = ElementCast<UsesPackage>(parent_stack[0]);
uses->AddCertDigest(element);
} else if (ElementCast<UsesStaticLibrary>(parent_stack[0])) {
UsesStaticLibrary* uses = ElementCast<UsesStaticLibrary>(parent_stack[0]);
uses->AddCertDigest(element);
}
}
}
};
/** Represents <screen> elements found in <compatible-screens> elements. */
class Screen : public ManifestExtractor::Element {
public:
Screen() = default;
const int32_t* size = nullptr;
const int32_t* density = nullptr;
void Extract(xml::Element* element) override {
size = GetAttributeInteger(FindAttribute(element, SCREEN_SIZE_ATTR));
density = GetAttributeInteger(FindAttribute(element, SCREEN_DENSITY_ATTR));
}
};
/**
* Represents <compatible-screens> elements. These elements have <screen> elements nested within
* that each denote a supported screen size and screen density.
**/
class CompatibleScreens : public ManifestExtractor::Element {
public:
CompatibleScreens() = default;
void Print(text::Printer* printer) override {
printer->Print("compatible-screens:");
bool first = true;
ForEachChild(this, [&printer, &first](ManifestExtractor::Element* el){
if (auto screen = ElementCast<Screen>(el)) {
if (first) {
first = false;
} else {
printer->Print(",");
}
if (screen->size && screen->density) {
printer->Print(StringPrintf("'%d/%d'", *screen->size, *screen->density));
}
}
});
printer->Print("\n");
}
};
/** Represents <supports-gl-texture> elements. **/
class SupportsGlTexture : public ManifestExtractor::Element {
public:
SupportsGlTexture() = default;
const std::string* name = nullptr;
void Extract(xml::Element* element) override {
name = GetAttributeString(FindAttribute(element, NAME_ATTR));
}
void Print(text::Printer* printer) override {
if (name) {
printer->Print(StringPrintf("supports-gl-texture:'%s'\n", name->data()));
}
}
};
/** Recursively prints the extracted badging element. */
static void Print(ManifestExtractor::Element* el, text::Printer* printer) {
el->Print(printer);
for (auto &child : el->children()) {
Print(child.get(), printer);
}
}
bool ManifestExtractor::Dump(text::Printer* printer, IDiagnostics* diag) {
// Load the manifest
std::unique_ptr<xml::XmlResource> doc = apk_->LoadXml("AndroidManifest.xml", diag);
if (doc == nullptr) {
diag->Error(DiagMessage() << "failed to find AndroidManifest.xml");
return false;
}
xml::Element* element = doc->root.get();
if (element->name != "manifest") {
diag->Error(DiagMessage() << "manifest does not start with <manifest> tag");
return false;
}
// Print only the <uses-permission>, <uses-permission-sdk23>, and <permission> elements if
// printing only permission elements is requested
if (options_.only_permissions) {
std::unique_ptr<ManifestExtractor::Element> manifest_element =
ManifestExtractor::Element::Inflate(this, element);
if (auto manifest = ElementCast<Manifest>(manifest_element.get())) {
for (xml::Element* child : element->GetChildElements()) {
if (child->name == "uses-permission" || child->name == "uses-permission-sdk-23"
|| child->name == "permission") {
auto permission_element = ManifestExtractor::Element::Inflate(this, child);
manifest->AddChild(permission_element);
}
}
printer->Print(StringPrintf("package: %s\n", manifest->package.data()));
ForEachChild(manifest, [&printer](ManifestExtractor::Element* el) -> void {
el->Print(printer);
});
return true;
}
return false;
}
// Collect information about the resource configurations
if (apk_->GetResourceTable()) {
for (auto &package : apk_->GetResourceTable()->packages) {
for (auto &type : package->types) {
for (auto &entry : type->entries) {
for (auto &value : entry->values) {
std::string locale_str = value->config.GetBcp47LanguageTag();
// Collect all the unique locales of the apk
if (locales_.find(locale_str) == locales_.end()) {
ConfigDescription config = ManifestExtractor::DummyConfig();
config.setBcp47Locale(locale_str.data());
locales_.insert(std::make_pair(locale_str, config));
}
// Collect all the unique density of the apk
uint16_t density = (value->config.density == 0) ? (uint16_t) 160
: value->config.density;
if (densities_.find(density) == densities_.end()) {
ConfigDescription config = ManifestExtractor::DummyConfig();
config.density = density;
densities_.insert(std::make_pair(density, config));
}
}
}
}
}
}
// Extract badging information
auto root = Visit(element);
// Print the elements in order seen
Print(root.get(), printer);
/** Recursively checks the extracted elements for the specified permission. **/
auto FindPermission = [&](ManifestExtractor::Element* root,
const std::string& name) -> ManifestExtractor::Element* {
return FindElement(root, [&](ManifestExtractor::Element* el) -> bool {
if (UsesPermission* permission = ElementCast<UsesPermission>(el)) {
return permission->name == name;
}
return false;
});
};
auto PrintPermission = [&printer](const std::string& name, const std::string& reason,
int32_t max_sdk_version) -> void {
auto permission = util::make_unique<UsesPermission>();
permission->name = name;
permission->maxSdkVersion = max_sdk_version;
permission->Print(printer);
permission->PrintImplied(printer, reason);
};
// Implied permissions
// Pre-1.6 implicitly granted permission compatibility logic
CommonFeatureGroup* common_feature_group = GetCommonFeatureGroup();
bool insert_write_external = false;
auto write_external_permission = ElementCast<UsesPermission>(
FindPermission(root.get(), "android.permission.WRITE_EXTERNAL_STORAGE"));
if (target_sdk() < 4) {
if (!write_external_permission) {
PrintPermission("android.permission.WRITE_EXTERNAL_STORAGE", "targetSdkVersion < 4", -1);
insert_write_external = true;
}
if (!FindPermission(root.get(), "android.permission.READ_PHONE_STATE")) {
PrintPermission("android.permission.READ_PHONE_STATE", "targetSdkVersion < 4", -1);
}
}
// If the application has requested WRITE_EXTERNAL_STORAGE, we will
// force them to always take READ_EXTERNAL_STORAGE as well. We always
// do this (regardless of target API version) because we can't have
// an app with write permission but not read permission.
auto read_external = FindPermission(root.get(), "android.permission.READ_EXTERNAL_STORAGE");
if (!read_external && (insert_write_external || write_external_permission)) {
PrintPermission("android.permission.READ_EXTERNAL_STORAGE",
"requested WRITE_EXTERNAL_STORAGE",
(write_external_permission) ? write_external_permission->maxSdkVersion : -1);
}
// Pre-JellyBean call log permission compatibility.
if (target_sdk() < 16) {
if (!FindPermission(root.get(), "android.permission.READ_CALL_LOG")
&& FindPermission(root.get(), "android.permission.READ_CONTACTS")) {
PrintPermission("android.permission.READ_CALL_LOG",
"targetSdkVersion < 16 and requested READ_CONTACTS", -1);
}
if (!FindPermission(root.get(), "android.permission.WRITE_CALL_LOG")
&& FindPermission(root.get(), "android.permission.WRITE_CONTACTS")) {
PrintPermission("android.permission.WRITE_CALL_LOG",
"targetSdkVersion < 16 and requested WRITE_CONTACTS", -1);
}
}
// If the app hasn't declared the touchscreen as a feature requirement (either
// directly or implied, required or not), then the faketouch feature is implied.
if (!common_feature_group->HasFeature("android.hardware.touchscreen")) {
common_feature_group->addImpliedFeature("android.hardware.faketouch",
"default feature for all apps", false);
}
// Only print the common feature group if no feature group is defined
std::vector<FeatureGroup*> feature_groups;
ForEachChild(root.get(), [&feature_groups](ManifestExtractor::Element* el) -> void {
if (auto feature_group = ElementCast<FeatureGroup>(el)) {
feature_groups.push_back(feature_group);
}
});
if (feature_groups.empty()) {
common_feature_group->PrintGroup(printer);
} else {
// Merge the common feature group into the feature group
for (auto& feature_group : feature_groups) {
feature_group->open_gles_version = std::max(feature_group->open_gles_version,
common_feature_group->open_gles_version);
feature_group->Merge(common_feature_group);
feature_group->PrintGroup(printer);
}
};
// Collect the component types of the application
std::set<std::string> components;
ForEachChild(root.get(), [&components](ManifestExtractor::Element* el) -> void {
if (ElementCast<Action>(el)) {
auto action = ElementCast<Action>(el);
if (!action->component.empty()) {
components.insert(action->component);
return;
}
}
if (ElementCast<Category>(el)) {
auto category = ElementCast<Category>(el);
if (!category->component.empty()) {
components.insert(category->component);
return;
}
}
});
// Check for the payment component
auto apk = apk_;
ForEachChild(root.get(), [&apk, &components, &diag](ManifestExtractor::Element* el) -> void {
if (auto service = ElementCast<Service>(el)) {
auto host_apdu_action = ElementCast<Action>(FindElement(service,
[&](ManifestExtractor::Element* el) -> bool {
if (auto action = ElementCast<Action>(el)) {
return (action->component == "host-apdu");
}
return false;
}));
auto offhost_apdu_action = ElementCast<Action>(FindElement(service,
[&](ManifestExtractor::Element* el) -> bool {
if (auto action = ElementCast<Action>(el)) {
return (action->component == "offhost-apdu");
}
return false;
}));
ForEachChild(service, [&apk, &components, &diag, &host_apdu_action,
&offhost_apdu_action](ManifestExtractor::Element* el) -> void {
if (auto meta_data = ElementCast<MetaData>(el)) {
if ((meta_data->name == "android.nfc.cardemulation.host_apdu_service" && host_apdu_action)
|| (meta_data->name == "android.nfc.cardemulation.off_host_apdu_service"
&& offhost_apdu_action)) {
// Attempt to load the resource file
if (!meta_data->resource.empty()) {
return;
}
auto resource = apk->LoadXml(meta_data->resource, diag);
if (!resource) {
return;
}
// Look for the payment category on an <aid-group> element
auto& root = resource.get()->root;
if ((host_apdu_action && root->name == "host-apdu-service")
|| (offhost_apdu_action && root->name == "offhost-apdu-service")) {
for (auto& child : root->GetChildElements()) {
if (child->name == "aid-group") {
auto category = FindAttribute(child, CATEGORY_ATTR);
if (category && category->value == "payment") {
components.insert("payment");
return;
}
}
}
}
}
}
});
}
});
// Print the components types if they are present
auto PrintComponent = [&components, &printer](const std::string& component) -> void {
if (components.find(component) != components.end()) {
printer->Print(StringPrintf("provides-component:'%s'\n", component.data()));
}
};
PrintComponent("app-widget");
PrintComponent("device-admin");
PrintComponent("ime");
PrintComponent("wallpaper");
PrintComponent("accessibility");
PrintComponent("print-service");
PrintComponent("payment");
PrintComponent("search");
PrintComponent("document-provider");
PrintComponent("launcher");
PrintComponent("notification-listener");
PrintComponent("dream");
PrintComponent("camera");
PrintComponent("camera-secure");
// Print presence of main activity
if (components.find("main") != components.end()) {
printer->Print("main\n");
}
// Print presence of activities, recivers, and services with no special components
FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
if (auto activity = ElementCast<Activity>(el)) {
if (!activity->has_component_) {
printer->Print("other-activities\n");
return true;
}
}
return false;
});
FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
if (auto receiver = ElementCast<Receiver>(el)) {
if (!receiver->has_component) {
printer->Print("other-receivers\n");
return true;
}
}
return false;
});
FindElement(root.get(), [&printer](ManifestExtractor::Element* el) -> bool {
if (auto service = ElementCast<Service>(el)) {
if (!service->has_component) {
printer->Print("other-services\n");
return true;
}
}
return false;
});
// Print the supported screens
SupportsScreen* screen = ElementCast<SupportsScreen>(FindElement(root.get(),
[&](ManifestExtractor::Element* el) -> bool {
return ElementCast<SupportsScreen>(el) != nullptr;
}));
if (screen) {
screen->PrintScreens(printer, target_sdk_);
} else {
// Print the default supported screens
SupportsScreen default_screens;
default_screens.PrintScreens(printer, target_sdk_);
}
// Print all the unique locales of the apk
printer->Print("locales:");
for (auto& config : locales_) {
if (config.first.empty()) {
printer->Print(" '--_--'");
} else {
printer->Print(StringPrintf(" '%s'", config.first.data()));
}
}
printer->Print("\n");
// Print all the densities locales of the apk
printer->Print("densities:");
for (auto& config : densities_) {
printer->Print(StringPrintf(" '%d'", config.first));
}
printer->Print("\n");
// Print the supported architectures of the app
std::set<std::string> architectures;
auto it = apk_->GetFileCollection()->Iterator();
while (it->HasNext()) {
auto file_path = it->Next()->GetSource().path;
size_t pos = file_path.find("lib/");
if (pos != std::string::npos) {
file_path = file_path.substr(pos + 4);
pos = file_path.find("/");
if (pos != std::string::npos) {
file_path = file_path.substr(0, pos);
}
architectures.insert(file_path);
}
}
// Determine if the application has multiArch supports
auto has_multi_arch = FindElement(root.get(), [&](ManifestExtractor::Element* el) -> bool {
if (auto application = ElementCast<Application>(el)) {
return application->has_multi_arch;
}
return false;
});
bool output_alt_native_code = false;
// A multiArch package is one that contains 64-bit and
// 32-bit versions of native code and expects 3rd-party
// apps to load these native code libraries. Since most
// 64-bit systems also support 32-bit apps, the apps
// loading this multiArch package's code may be either
if (has_multi_arch) {
// If this is a multiArch package, report the 64-bit
// version only. Then as a separate entry, report the
// rest.
//
// If we report the 32-bit architecture, this APK will
// be installed on a 32-bit device, causing a large waste
// of bandwidth and disk space. This assumes that
// the developer of the multiArch package has also
// made a version that is 32-bit only.
const std::string kIntel64 = "x86_64";
const std::string kArm64 = "arm64-v8a";
auto arch = architectures.find(kIntel64);
if (arch == architectures.end()) {
arch = architectures.find(kArm64);
}
if (arch != architectures.end()) {
printer->Print(StringPrintf("native-code: '%s'\n", arch->data()));
architectures.erase(arch);
output_alt_native_code = true;
}
}
if (architectures.size() > 0) {
if (output_alt_native_code) {
printer->Print("alt-");
}
printer->Print("native-code:");
for (auto& arch : architectures) {
printer->Print(StringPrintf(" '%s'", arch.data()));
}
printer->Print("\n");
}
return true;
}
/**
* Returns the element casted to the type if the element is of that type. Otherwise, returns a null
* pointer.
**/
template<typename T>
T* ElementCast(ManifestExtractor::Element* element) {
if (element == nullptr) {
return nullptr;
}
const std::unordered_map<std::string, bool> kTagCheck = {
{"action", std::is_base_of<Action, T>::value},
{"activity", std::is_base_of<Activity, T>::value},
{"application", std::is_base_of<Application, T>::value},
{"category", std::is_base_of<Category, T>::value},
{"compatible-screens", std::is_base_of<CompatibleScreens, T>::value},
{"feature-group", std::is_base_of<FeatureGroup, T>::value},
{"input-type", std::is_base_of<InputType, T>::value},
{"intent-filter", std::is_base_of<IntentFilter, T>::value},
{"meta-data", std::is_base_of<MetaData, T>::value},
{"manifest", std::is_base_of<Manifest, T>::value},
{"original-package", std::is_base_of<OriginalPackage, T>::value},
{"overlay", std::is_base_of<Overlay, T>::value},
{"package-verifier", std::is_base_of<PackageVerifier, T>::value},
{"permission", std::is_base_of<Permission, T>::value},
{"provider", std::is_base_of<Provider, T>::value},
{"receiver", std::is_base_of<Receiver, T>::value},
{"screen", std::is_base_of<Screen, T>::value},
{"service", std::is_base_of<Service, T>::value},
{"supports-gl-texture", std::is_base_of<SupportsGlTexture, T>::value},
{"supports-input", std::is_base_of<SupportsInput, T>::value},
{"supports-screens", std::is_base_of<SupportsScreen, T>::value},
{"uses-configuration", std::is_base_of<UsesConfiguarion, T>::value},
{"uses-feature", std::is_base_of<UsesFeature, T>::value},
{"uses-permission", std::is_base_of<UsesPermission, T>::value},
{"uses-permission-sdk-23", std::is_base_of<UsesPermissionSdk23, T>::value},
{"uses-library", std::is_base_of<UsesLibrary, T>::value},
{"uses-package", std::is_base_of<UsesPackage, T>::value},
{"static-library", std::is_base_of<StaticLibrary, T>::value},
{"uses-static-library", std::is_base_of<UsesStaticLibrary, T>::value},
{"additional-certificate", std::is_base_of<AdditionalCertificate, T>::value},
{"uses-sdk", std::is_base_of<UsesSdkBadging, T>::value},
};
auto check = kTagCheck.find(element->tag());
if (check != kTagCheck.end() && check->second) {
return static_cast<T*>(element);
}
return nullptr;
}
template<typename T>
std::unique_ptr<T> CreateType() {
return std::move(util::make_unique<T>());
}
std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Element::Inflate(
ManifestExtractor* extractor, xml::Element* el) {
const std::unordered_map<std::string,
std::function<std::unique_ptr<ManifestExtractor::Element>()>>
kTagCheck = {
{"action", &CreateType<Action>},
{"activity", &CreateType<Activity>},
{"application", &CreateType<Application>},
{"category", &CreateType<Category>},
{"compatible-screens", &CreateType<CompatibleScreens>},
{"feature-group", &CreateType<FeatureGroup>},
{"input-type", &CreateType<InputType>},
{"intent-filter",&CreateType<IntentFilter>},
{"manifest", &CreateType<Manifest>},
{"meta-data", &CreateType<MetaData>},
{"original-package", &CreateType<OriginalPackage>},
{"overlay", &CreateType<Overlay>},
{"package-verifier", &CreateType<PackageVerifier>},
{"permission", &CreateType<Permission>},
{"provider", &CreateType<Provider>},
{"receiver", &CreateType<Receiver>},
{"screen", &CreateType<Screen>},
{"service", &CreateType<Service>},
{"supports-gl-texture", &CreateType<SupportsGlTexture>},
{"supports-input", &CreateType<SupportsInput>},
{"supports-screens", &CreateType<SupportsScreen>},
{"uses-configuration", &CreateType<UsesConfiguarion>},
{"uses-feature", &CreateType<UsesFeature>},
{"uses-permission", &CreateType<UsesPermission>},
{"uses-permission-sdk-23", &CreateType<UsesPermissionSdk23>},
{"uses-library", &CreateType<UsesLibrary>},
{"static-library", &CreateType<StaticLibrary>},
{"uses-static-library", &CreateType<UsesStaticLibrary>},
{"uses-package", &CreateType<UsesPackage>},
{"additional-certificate", &CreateType<AdditionalCertificate>},
{"uses-sdk", &CreateType<UsesSdkBadging>},
};
// Attempt to map the xml tag to a element inflater
std::unique_ptr<ManifestExtractor::Element> element;
auto check = kTagCheck.find(el->name);
if (check != kTagCheck.end()) {
element = check->second();
} else {
element = util::make_unique<ManifestExtractor::Element>();
}
element->extractor_ = extractor;
element->tag_ = el->name;
element->Extract(el);
return element;
}
std::unique_ptr<ManifestExtractor::Element> ManifestExtractor::Visit(xml::Element* el) {
auto element = ManifestExtractor::Element::Inflate(this, el);
parent_stack_.insert(parent_stack_.begin(), element.get());
// Process the element and recursively visit the children
for (xml::Element* child : el->GetChildElements()) {
auto v = Visit(child);
element->AddChild(v);
}
parent_stack_.erase(parent_stack_.begin());
return element;
}
int DumpManifest(LoadedApk* apk, DumpManifestOptions& options, text::Printer* printer,
IDiagnostics* diag) {
ManifestExtractor extractor(apk, options);
return extractor.Dump(printer, diag) ? 0 : 1;
}
} // namespace aapt
|