use of com.taobao.android.builder.tools.manifest.Permission.Item in project atlas by alibaba.
the class ManifestFileUtils method updatePermission.
/**
* Set the permissions for the manifest
*
* @param document
* @param manifestOptions
*/
private static void updatePermission(Document document, ManifestOptions manifestOptions) throws IOException {
if (null == manifestOptions) {
return;
}
// Get the root node
Element root = document.getRootElement();
if (null != manifestOptions.getPermissionJsonFile() && manifestOptions.getPermissionJsonFile().exists()) {
String json = FileUtils.readFileToString(manifestOptions.getPermissionJsonFile());
Permission permission = JSON.parseObject(json, Permission.class);
List<Node> nodes = root.selectNodes("//permission");
List<Node> nodes1 = root.selectNodes("//uses-permission");
List<Node> nodes2 = root.selectNodes("//uses-feature");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
Item item = Permission.query(permission.getPermissions(), name);
if (null == item) {
logger.warn("[permission] remove " + name);
element.getParent().remove(element);
}
}
for (Node node : nodes1) {
Element element = (Element) node;
String name = element.attributeValue("name");
Item item = Permission.query(permission.getUses_permissions(), name);
if (null == item) {
logger.warn("[uses-permission] remove " + name);
element.getParent().remove(element);
}
}
for (Node node : nodes2) {
Element element = (Element) node;
String name = element.attributeValue("name");
if (StringUtils.isEmpty(name)) {
continue;
}
Item item = Permission.query(permission.getUses_features(), name);
if (null == item) {
logger.warn("[uses-feature] remove " + name);
element.getParent().remove(element);
} else {
// Modify the value
if (StringUtils.isNotEmpty(item.getValue())) {
Attribute required = element.attribute("required");
if (null != required) {
required.setValue(item.getValue());
}
// element.addAttribute(new QName("required", new Namespace("android", null)), item.getValue());
}
}
}
return;
}
if (null != manifestOptions.getPermissionListFile() && manifestOptions.getPermissionListFile().exists()) {
List<String> whiteList = FileUtils.readLines(manifestOptions.getPermissionListFile());
List<Node> nodes = new ArrayList<>();
nodes.addAll(root.selectNodes("//permission"));
nodes.addAll(root.selectNodes("//uses-permission"));
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
if (!whiteList.contains(name)) {
logger.warn("[permission] remove " + name);
element.getParent().remove(element);
}
}
return;
}
// Update custom permissions
if (manifestOptions.isRemoveCustomPermission()) {
List<? extends Node> nodes = root.selectNodes("//permission");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
boolean retain = false;
for (String systemPermission : SYSTEM_PERMISSION) {
if (name.startsWith(systemPermission)) {
retain = true;
break;
}
}
// If there is a custom permission
if (null != manifestOptions.getRetainPermissions() && manifestOptions.getRetainPermissions().size() > 0) {
for (String retainPermission : manifestOptions.getRetainPermissions()) {
if (name.startsWith(retainPermission)) {
retain = true;
break;
}
}
}
if (!retain) {
element.getParent().remove(element);
}
}
}
// Update system permissions
if (null != manifestOptions.getRemoveSystemPermissions() && manifestOptions.getRemoveSystemPermissions().size() > 0) {
List<? extends Node> nodes = root.selectNodes("//uses-permission");
for (Node node : nodes) {
Element element = (Element) node;
String name = element.attributeValue("name");
Set<String> removedPermissions = manifestOptions.getRemoveSystemPermissions();
if (removedPermissions.contains(name)) {
element.getParent().remove(element);
}
}
}
}