# coding:utf8

__author__ = '极无双'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob

ANDROID_NS = 'http://schemas.android.com/apk/res/android'
from xml.etree import ElementTree as ET


def script(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    modify_manifest(decompileDir, "com.excelliance.lbsdk.debug.LBSdkCrashReportService", "foregroundServiceType")
    modify_manifest(decompileDir, "com.excelliance.lbsdk.base.AssistService", "foregroundServiceType")
    modify_manifest(decompileDir, "com.excelliance.lbsdk.base.BaseService", "foregroundServiceType")
    modify_manifest(decompileDir, "com.excelliance.lbsdk.main.BGService", "foregroundServiceType")
    modify_manifest_activity(decompileDir, "org.cocos2dx.lua.AppActivity", "requestLegacyExternalStorage")
    # modify_manifest_common(decompileDir, "dataExtractionRules")


def modify_manifest(decompileDir, serviceName, key):
    ET.register_namespace('android', ANDROID_NS)
    xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
    root_node = ET.parse(xmlparse)
    root = root_node.getroot()
    name = '{' + ANDROID_NS + '}name'
    authorities = '{' + ANDROID_NS + '}' + key

    package_name = root.attrib.get('package')
    if package_name == None:
        return
    providers = root.findall('./application/service')
    if providers != None:
        for provider in providers:
            providerName = provider.attrib.get(name)
            if serviceName == providerName:
                try:
                    del provider.attrib[authorities]
                    # del provider.attrib[authoritiesTwo]
                except:
                    ""
    root_node.write(xmlparse, 'utf-8')


def modify_manifest_activity(decompileDir, serviceName, key):
    ET.register_namespace('android', ANDROID_NS)
    xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
    root_node = ET.parse(xmlparse)
    root = root_node.getroot()
    name = '{' + ANDROID_NS + '}name'
    authorities = '{' + ANDROID_NS + '}' + key

    package_name = root.attrib.get('package')
    if package_name == None:
        return
    providers = root.findall('./application/activity')
    if providers != None:
        for provider in providers:
            providerName = provider.attrib.get(name)
            if serviceName == providerName:
                try:
                    del provider.attrib[authorities]
                    # del provider.attrib[authoritiesTwo]
                except:
                    ""
    root_node.write(xmlparse, 'utf-8')


def modify_manifest_common(decompileDir, removeKey):
    ET.register_namespace('android', ANDROID_NS)
    xmlparse = os.path.join(decompileDir, 'AndroidManifest.xml')
    logging.info(xmlparse)
    root_node = ET.parse(xmlparse)
    root = root_node.getroot()
    name = '{' + ANDROID_NS + '}name'
    authorities = '{' + ANDROID_NS + '}' + removeKey
    package_name = root.attrib.get('package')
    if package_name == None:
        return
    providers = root.findall('./application')
    if providers != None:
        for provider in providers:
            try:
                del provider.attrib[authorities]
            except:
                ""
    root_node.write(xmlparse, 'utf-8')


if __name__ == '__main__':
    ""
