# -*- coding:utf-8 -*- from V1 import Exec_Cmd_Utils import os import xml.etree.ElementTree as ET from V1.PrintLog import printlog def ResizeIcon(icon, toPath, width, height, saveName, uname): if not os.path.exists(icon): return False, "no such icon:%s" % icon if width == 0 or height == 0: return False, "width or height is 0" if saveName == "": return False, "icon save name is empty" if not os.path.exists(toPath): printlog("package icon path is empty:%s" % toPath) return False, "package icon path is empty" copyToPathShell = "/bin/cp %s %s/%s" % (icon, toPath, saveName) ret, output = Exec_Cmd_Utils.exeCommonCmd(copyToPathShell) if ret != 0: return False, output resizeShell = '' if uname.strip() == "Darwin": resizeShell = "/usr/bin/sips %s -z %s %s --out %s/%s" % (icon, width, height, toPath, saveName) elif uname.strip() == "Linux": resizeShell = "/usr/bin/convert -resize %sx%s %s/%s %s/%s" % ( width, height, toPath, saveName, toPath, saveName) ret, output = Exec_Cmd_Utils.exeCommonCmd(resizeShell) if ret != 0: return False, output return True, None pass def replaceIcon(icon_path, apk_decompile_tmp_dir): temp_xml_path = os.path.join(apk_decompile_tmp_dir,'AndroidManifest.xml') tem_res_dir ="%s/res/"%apk_decompile_tmp_dir ret,icon_name = GetIconName(temp_xml_path) if ret: if os.path.exists(icon_path): printlog("begin copy and resize icon:%s" % icon_name) status, uname = Exec_Cmd_Utils.exeCommonCmd("uname") allIconSize = {'drawable': '512', 'drawable-ldpi': '72', 'drawable-mdpi': '96', 'drawable-hdpi': '192', 'drawable-xhdpi': '256' , 'drawable-xxhdpi': '384', 'drawable-xxxhdpi': '512', 'drawable-xhdpi-v4': '256', 'drawable-xxhdpi-v4': '384', 'drawable-xxxhdpi-v4': '512', 'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256', 'mipmap-xxhdpi': '384', 'mipmap-xxxhdpi': '512' } roundIconSize = {'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256', 'mipmap-xxhdpi-v4': '384', 'mipmap-xxxhdpi': '512'} for iconDir, iconSize in allIconSize.items(): drawablePath = tem_res_dir + iconDir ResizeIcon(icon_path, drawablePath, iconSize, iconSize, "%s.png" % icon_name, uname) for iconDir, iconSize in roundIconSize.items(): drawablePath = tem_res_dir + iconDir ResizeIcon(icon_path, drawablePath, iconSize, iconSize, "%s_round.png" % icon_name, uname) return True else: return False def GetIconName(am_path): if not os.path.exists(am_path): return False, "AndroidManifest.xml is not exists!" namespace = "http://schemas.android.com/apk/res/android" amTree = ET.parse(am_path) amRoot = amTree.getroot() applicationNode = amRoot.find("application") iconName = applicationNode.attrib.get("{%s}icon" % namespace) if iconName is None: return False, "cant find icon name" iconName = iconName.split("/")[1] return True, iconName pass