Icon_Util.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # -*- coding:utf-8 -*-
  2. from V1 import Exec_Cmd_Utils
  3. import os
  4. import xml.etree.ElementTree as ET
  5. from V1.PrintLog import printlog
  6. def ResizeIcon(icon, toPath, width, height, saveName, uname):
  7. if not os.path.exists(icon):
  8. return False, "no such icon:%s" % icon
  9. if width == 0 or height == 0:
  10. return False, "width or height is 0"
  11. if saveName == "":
  12. return False, "icon save name is empty"
  13. if not os.path.exists(toPath):
  14. printlog("package icon path is empty:%s" % toPath)
  15. return False, "package icon path is empty"
  16. copyToPathShell = "/bin/cp %s %s/%s" % (icon, toPath, saveName)
  17. ret, output = Exec_Cmd_Utils.exeCommonCmd(copyToPathShell)
  18. if ret != 0:
  19. return False, output
  20. resizeShell = ''
  21. if uname.strip() == "Darwin":
  22. resizeShell = "/usr/bin/sips %s -z %s %s --out %s/%s" % (icon, width, height, toPath, saveName)
  23. elif uname.strip() == "Linux":
  24. resizeShell = "/usr/bin/convert -resize %sx%s %s/%s %s/%s" % (
  25. width, height, toPath, saveName, toPath, saveName)
  26. ret, output = Exec_Cmd_Utils.exeCommonCmd(resizeShell)
  27. if ret != 0:
  28. return False, output
  29. return True, None
  30. pass
  31. def replaceIcon(icon_path, apk_decompile_tmp_dir):
  32. temp_xml_path = os.path.join(apk_decompile_tmp_dir,'AndroidManifest.xml')
  33. tem_res_dir ="%s/res/"%apk_decompile_tmp_dir
  34. ret,icon_name = GetIconName(temp_xml_path)
  35. if ret:
  36. if os.path.exists(icon_path):
  37. printlog("begin copy and resize icon:%s" % icon_name)
  38. status, uname = Exec_Cmd_Utils.exeCommonCmd("uname")
  39. allIconSize = {'drawable': '512', 'drawable-ldpi': '72', 'drawable-mdpi': '96', 'drawable-hdpi': '192',
  40. 'drawable-xhdpi': '256'
  41. , 'drawable-xxhdpi': '384', 'drawable-xxxhdpi': '512',
  42. 'drawable-xhdpi-v4': '256', 'drawable-xxhdpi-v4': '384', 'drawable-xxxhdpi-v4': '512',
  43. 'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256',
  44. 'mipmap-xxhdpi': '384', 'mipmap-xxxhdpi': '512'
  45. }
  46. roundIconSize = {'mipmap-ldpi': '36', 'mipmap-mdpi': '96', 'mipmap-hdpi': '192', 'mipmap-xhdpi': '256',
  47. 'mipmap-xxhdpi-v4': '384', 'mipmap-xxxhdpi': '512'}
  48. for iconDir, iconSize in allIconSize.items():
  49. drawablePath = tem_res_dir + iconDir
  50. ResizeIcon(icon_path, drawablePath, iconSize, iconSize,
  51. "%s.png" % icon_name, uname)
  52. for iconDir, iconSize in roundIconSize.items():
  53. drawablePath = tem_res_dir + iconDir
  54. ResizeIcon(icon_path, drawablePath, iconSize, iconSize,
  55. "%s_round.png" % icon_name, uname)
  56. return True
  57. else:
  58. return False
  59. def GetIconName(am_path):
  60. if not os.path.exists(am_path):
  61. return False, "AndroidManifest.xml is not exists!"
  62. namespace = "http://schemas.android.com/apk/res/android"
  63. amTree = ET.parse(am_path)
  64. amRoot = amTree.getroot()
  65. applicationNode = amRoot.find("application")
  66. iconName = applicationNode.attrib.get("{%s}icon" % namespace)
  67. if iconName is None:
  68. return False, "cant find icon name"
  69. iconName = iconName.split("/")[1]
  70. return True, iconName
  71. pass