# 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):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['SDKName'] != 'reverseforkupai':
        del_coolcloud(decompileDir)
    modify_manifest(decompileDir)
    modify_manifest_common(decompileDir, "allowNativeHeapPointerTagging")


def is_reverse_coolcloud(file):
    with open(file,'r+') as f:
        file_str = f.read()
        if 'KFSDK' in file_str:
            return True
        else:
            return False

def del_coolcloud(decompileDir):
    smali_files = glob.glob(os.path.join(decompileDir,'smali*/com/coolcloud/'))
    if len(smali_files) > 1:
        iter_smali = iter(smali_files)
        while True:
            try:
                coolcloud_file_path = iter_smali.next()
                file = os.path.join(coolcloud_file_path,'uac/android/api/Coolcloud.smali')
                if os.path.exists(file):
                    logging.info('Coolcloud exits in %s' % coolcloud_file_path)
                    if not is_reverse_coolcloud(file):
                        logging.info("delete coolcloud in %s" % coolcloud_file_path)
                        shutil.rmtree(coolcloud_file_path)
                    else:
                        pass
                else:
                    logging.info('Coolcloud not exits in %s' % coolcloud_file_path)
                    shutil.rmtree(coolcloud_file_path)

            except StopIteration:
                break
def modify_manifest(decompileDir):
    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 + '}foregroundServiceType'
    authoritiesTwo = '{' + ANDROID_NS + '}requestLegacyExternalStorage'

    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 'com.hbisoft.hbrecorder.ScreenRecordService' == 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__':
    ""
