def void updateManifestXml(def path){
File file = new File(path)
def doc = DOMBuilder.parse(new StringReader(file.text))
def root = doc.documentElement
use(groovy.xml.dom.DOMCategory) {
//移除掉默认启动intent-filter 以及 基地标志的intent-filter
removeIntentFilterByAction_Categroy(root,"android.intent.action.MAIN","android.intent.category.LAUNCHER")
removeIntentFilterByAction_Categroy(root,"android.intent.action.CHINAMOBILE_OMS_GAME","android.intent.category.CHINAMOBILE_GAMES")
// 根据不同启动Activity添加不同的操作
if(hasLoginActivity){
// LoginAcitivity 闪屏页启动
// 1. 启动页横竖屏设置
// 2. 启动页主题设置
//def androidMap = ["android:screenOrientation":"landscape",
// "android:theme":"@style/GameTheme"]
if(androidMap.size()>0){
replaceActivityAttributes(root,androidMap)
}
// 3. LoginActivity 添加启动项 和 基地标志
addMainIntentFilter(root,"com.zengame.basic.LoginActivity")
addAndGameIntentFilter(root,"com.zengame.basic.LoginActivity")
}else{
// GameActivity 启动
// 1. 移除掉LoginActivity
removeActivityByName(root,"com.zengame.basic.LoginActivity")
// 2. GameActivity 添加启动项 和 基地标志
addMainIntentFilter(root,"com.zengame.basic.GameActivity")
addAndGameIntentFilter(root,"com.zengame.basic.GameActivity")
}
}
def result = XmlUtil.serialize(root)
file.write(result,"UTF-8")
// println result
}
def void addMainIntentFilter(def root,String name){
root.getElementsByTagName("activity").each {node ->
if(node.attributes.getNamedItem("android:name").nodeValue==name){
org.w3c.dom.Element element = node.appendNode("intent-filter")
element.appendNode("action",["android:name":"android.intent.action.MAIN"])
element.appendNode("category",["android:name":"android.intent.category.LAUNCHER"])
}
}
}
def void addAndGameIntentFilter(def root,String name){
root.getElementsByTagName("activity").each {node ->
if(node.attributes.getNamedItem("android:name").nodeValue==name){
org.w3c.dom.Element element = node.appendNode("intent-filter")
element.appendNode("action",["android:name":"android.intent.action.CHINAMOBILE_OMS_GAME"])
element.appendNode("category",["android:name":"android.intent.category.CHINAMOBILE_GAMES"])
}
}
}
def void removeActivityByName(def root,String name){
root.getElementsByTagName("activity").each {node ->
if(node.attributes.getNamedItem("android:name").nodeValue==name){
node.parentNode.removeChild(node)
}
}
}
def void replaceActivityAttributes(def root,def map){
root.getElementsByTagName("activity").each {node ->
if(node.attributes.getNamedItem("android:name").nodeValue=="com.zengame.basic.LoginActivity"){
map.each {key,value ->
node.attributes.getNamedItem(key).setNodeValue(value)
}
}
}
}
def void removeIntentFilterByAction_Categroy(def root,String actionValue,String categroyValue){
root.getElementsByTagName("intent-filter").each {node ->
boolean hasMain
boolean hasLuancher
node.childNodes.each { childNode ->
if (childNode.nodeName == "action" && actionValue == childNode.attributes.getNamedItem("android:name").nodeValue) {
hasMain = true
}
if (childNode.nodeName == "category" && categroyValue == childNode.attributes.getNamedItem("android:name").nodeValue) {
hasLuancher = true
}
}
if (hasMain && hasLuancher) {
node.parentNode.removeChild(node)
}
}
}