# coding:utf8

__author__ = 'tianshuqitan'

import os
import logging
import xml.dom.minidom
import re
import shutil
import glob
from xml.etree import ElementTree as ET

ANDROID_NS = 'http://schemas.android.com/apk/res/android'


def script_second(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    if channelSdkInfo['id'] == '587':
        progress_png = os.path.join(decompileDir, "res/drawable/progress.png")
        if os.path.exists(progress_png):
            os.remove(progress_png)
    return


def script_last(SDK, decompileDir, channelSdkInfo, new_game_channel_info, gameInfo):
    logging.info('------------channelSdkInfo:' + str(channelSdkInfo))
    if channelSdkInfo['id'] == '549':
        del_coolcloud(decompileDir)
        # replace_Theme(decompileDir)
    if channelSdkInfo['id'] == '597':
        modify_manifest_common(decompileDir, "allowNativeHeapPointerTagging")


def del_coolcloud(decompileDir):
    coolcloud = os.path.join(decompileDir, 'smali_classes3/com/coolcloud')
    if os.path.exists(coolcloud):
        shutil.rmtree(coolcloud)


def replace_Theme(extract_dir):
    goal_file = os.path.join(extract_dir, 'res/values/styles.xml')
    print(os.path.isfile(goal_file))
    if os.path.isfile(goal_file):
        logging.info('method_proceed: ' + 'goal_file')
        print(goal_file)
        with open(goal_file, 'r+') as f:
            cont = f.read()
            change_str = '@android:style/Theme.Light'
            str = '@android:style/Theme.Light.NoTitleBar'
            cont = cont.replace(change_str, str)
            logging.info('replace_str: ' + cont)
        with open(goal_file, 'w+') as tf:
            tf.write(cont)


def handle_public_xml(public_xml_path, remove_node):
    if not os.path.exists(public_xml_path):
        logging.info('public_xml is null: ' + public_xml_path)
        return
    new_lines = []
    with open(public_xml_path, 'r+') as f:
        for line in f.readlines():
            # l = line.strip()
            if line.find(remove_node) > -1:
                continue
            new_lines.append(line)
        f.seek(0)
        f.truncate()
        f.writelines(new_lines)
    return


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:
            # providerName = provider.attrib.get(name)
            # if 'com.netease.ntunisdk.CcMomentRecordingForegroundService' == providerName:
            # 使用try 主要是为了 防止此属性不在时，导致的错误，而程序终止
            try:
                del provider.attrib[authorities]
            except:
                ""

    root_node.write(xmlparse, 'utf-8')


if __name__ == '__main__':
    del_coolcloud("C:\\Users\\Administrator\\Desktop\\Shadow")
