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

com.mobgen.halo.android.plugin.sdk.tasks.ProcessPushManifestTask.groovy Maven / Gradle / Ivy

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

import com.android.build.gradle.api.BaseVariant
import com.mobgen.halo.android.plugin.sdk.modules.user.PushModule
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 ProcessPushManifestTask extends DefaultTask {

    BaseVariant androidVariant
    PushModule pushModule

    @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
    }

    def appendPermissions(Node xml) {
        String internetPermission = ""
        String wakeLockPermission = ""
        String receiveGCMPermission = ""
        String declareGCMPermission = ""
        String gcmPermission = ""
        xml.append(new XmlParser().parseText(internetPermission))
        xml.append(new XmlParser().parseText(wakeLockPermission))
        xml.append(new XmlParser().parseText(receiveGCMPermission))
        xml.append(new XmlParser().parseText(declareGCMPermission))
        xml.append(new XmlParser().parseText(gcmPermission))

        //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)
            }
        }
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy