All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mobgen.halo.android.gradle.tasks.HaloSMSManifestTask.groovy Maven / Gradle / Ivy

There is a newer version: 2.9.3
Show newest version
package com.mobgen.halo.android.gradle.tasks

import com.android.build.gradle.api.BaseVariant
import groovy.xml.Namespace
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction

/**
 * Task to write the halo configuration file to the system.
 */
public class HaloSMSManifestTask extends DefaultTask {

    BaseVariant androidVariant

    @TaskAction
    public onProcessManifests() {
        String manifestLocation = getManifestLocation()
        def xml = new XmlParser().parse(manifestLocation)
        appendPermissions(xml)

        //Add the permissions
        XmlNodePrinter printer = new XmlNodePrinter(new PrintWriter(new FileWriter(manifestLocation)))
        printer.setPreserveWhitespace(true)
        printer.print(xml)
    }

    private String getManifestLocation() {
        def manifestLocation = "${getProject().getBuildDir()}/intermediates/manifests/full"
        if (androidVariant.getFlavorName() != null) {
            manifestLocation += "/${androidVariant.getFlavorName()}"
        }
        manifestLocation += "/${androidVariant.getBuildType().getName()}/AndroidManifest.xml"
        return manifestLocation
    }

    static def appendPermissions(Node xml) {
        String internetPermission = ""
        xml.append(new XmlParser().parseText(internetPermission))

        //Remove repeated node items
        def androidNS = new Namespace("http://schemas.android.com/apk/res/android", "android")
        List nodePermissions = new ArrayList<>()
        ((NodeList) xml.get("uses-permission")).each { node ->
            String permission = node.attribute(androidNS.name)
            if (nodePermissions.contains(permission)) {
                xml.remove(node)
            } else {
                nodePermissions.add(permission)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy