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

com.mobgen.halo.android.gradle.tasks.HaloNotificationsManifestTask.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 HaloNotificationsManifestTask extends DefaultTask {

    BaseVariant androidVariant

    @TaskAction
    public onProcessManifests() {
        String manifestLocation = getManifestLocation()
        def xml = new XmlParser().parse(manifestLocation)
        appendPermissions(xml)
        appendServices(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)
            }
        }
    }

    static def appendServices(Node xml) {
        if (xml.application && xml.application[0]) {
            String gcmService = "\n" +
                    "            \n" +
                    "                \n" +
                    "            \n" +
                    "        "
            String instanceIdService = "\n" +
                    "            \n" +
                    "                \n" +
                    "            \n" +
                    "        "
            xml.application[0].append(new XmlParser().parseText(gcmService))
            xml.application[0].append(new XmlParser().parseText(instanceIdService))
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy