channel_action.py 3.5 KB
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import os.path
from xml.etree import ElementTree as ET
from xml.etree.ElementTree import SubElement
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import ElementTree
import os
import os.path
import zipfile
import re
import subprocess
import platform
from xml.dom import minidom
import codecs
import sys

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

def execute(channel, decompileDir, packageName):
    '''
    sdkDir = decompileDir + '/../sdk/' + channel['sdk']
    if not os.path.exists(sdkDir):
        file_utils.printF("The sdk temp folder is not exists. path:"+sdkDir)
        return 1

    manifest = decompileDir + '/AndroidManifest.xml'
    ET.register_namespace('android', androidNS)
    
    key = '{' + androidNS + '}name'

    tree = ET.parse(manifest)
    root = tree.getroot()

    permissionLst = root.findall('uses-permission')

    if permissionLst != None:
        for permissionNode in permissionLst:
            value = permissionNode.get(key)
            if value == 'android.permission.RECORD_AUDIO':
                root.remove(permissionNode)
                print("AndroidManifest remove the permission of audio ,the detail is android.permission.RECORD_AUDIO")
            elif value == 'android.permission.GET_ACCOUNTS':
                root.remove(permissionNode)
                print("AndroidManifest remove the permission of GET_ACCOUNTS ,the detail is android.permission.GET_ACCOUNTS")

    tree.write(manifest, 'UTF-8')
    '''

    bw_appid = ''
    bw_appkey = ''
    bw_gameid = ''
    bw_agent = ''
    if channel['params'] != None and len(channel['params']) > 0:
        for param in channel['params']:
            if param['name'] == 'appID':
                bw_appid = param['value']
            elif param['name'] == 'gameID':
                bw_gameid = param['value']
            # elif param['name'] == 'appKey':
            #     bw_appkey = param['value']
            # elif param['name'] == 'agent':
            #     bw_agent = param['value']

    eu_developer_config_path = decompileDir + '/assets/xq_config.properties'
    euConfigFile = open(eu_developer_config_path, 'r')
    lines = euConfigFile.readlines()
    euConfigFile.close()

    newLines = []
    for line in lines:
        if 'appID' in line and bw_appid is not None:
            newLines.append("appID=" + bw_appid + "\n")
        elif 'gameID' in line and bw_gameid is not None:
            newLines.append("gameID=" + bw_gameid + "\n")
        # elif 'appKey' in line and bw_appkey is not None:
        #     newLines.append("appKey=" + bw_appkey + "\n")
        # elif 'agent' in line and bw_agent is not None:
        #     newLines.append("agent=" + bw_agent + "\n")
        else:
            newLines.append(line)

    content = ''
    for line in newLines:
        content = content + line

    eu_developer_config_file = open(eu_developer_config_path, 'w')
    eu_developer_config_file.write(content)
    eu_developer_config_file.close()

    manifestPath = decompileDir + '/AndroidManifest.xml'
    manifest_file = open(manifestPath, 'r+', encoding='Utf-8')
    manifestContent = str(manifest_file.read())
    manifest_file.close()

    # 4、replace "${applicationId}" "${JPUSH_PKGNAME}" with packname in AndroidManifest.xml
    manifestContent = manifestContent.replace('${applicationId}', packageName)

    manifest_new_file = open(manifestPath, 'w', encoding='Utf-8')
    manifest_new_file.write(manifestContent)
    manifest_new_file.close()

    return 0