Add locale option to get_app_name() method.

This commit is contained in:
Forgo7ten 2024-04-19 20:25:37 +08:00
parent 72e29adf9f
commit 33c99a1695
4 changed files with 38 additions and 3 deletions

View File

@ -447,7 +447,7 @@ class APK:
"""
return self.filename
def get_app_name(self):
def get_app_name(self, **kwargs):
"""
Return the appname of the APK
@ -500,9 +500,10 @@ class APK:
return app_name
try:
config = ARSCResTableConfig(None, **kwargs) if kwargs else ARSCResTableConfig.default_config()
app_name = res_parser.get_resolved_res_configs(
res_id,
ARSCResTableConfig.default_config())[0][1]
config)[0][1]
except Exception as e:
logger.warning("Exception selecting app name: %s" % e)
return app_name

View File

@ -2510,7 +2510,12 @@ class ARSCResTableConfig:
((kwargs.pop('mcc', 0) & 0xffff) << 0) + \
((kwargs.pop('mnc', 0) & 0xffff) << 16)
self.locale = 0
temp_locale = kwargs.pop('locale', 0)
if isinstance(temp_locale, str):
self.set_language_and_region(temp_locale)
else:
self.locale = temp_locale
for char_ix, char in kwargs.pop('locale', "")[0:4]:
self.locale += (ord(char) << (char_ix * 8))
@ -2563,6 +2568,26 @@ class ARSCResTableConfig:
char_out += chr(char_in[1])
return char_out
def _pack_language_or_region(self, char_in):
char_out = [0x00, 0x00]
if len(char_in) != 2:
return char_out
char_out[0] = ord(char_in[0])
char_out[1] = ord(char_in[1])
return char_out
def set_language_and_region(self, language_region):
try:
language, region = language_region.split("-r")
except ValueError:
language, region = language_region, None
language_bytes = self._pack_language_or_region(language)
if region:
region_bytes = self._pack_language_or_region(region)
else:
region_bytes = [0x00, 0x00]
self.locale = language_bytes[0] | (language_bytes[1] << 8) | (region_bytes[0] << 16) | (region_bytes[1] << 24)
def get_language_and_region(self):
"""
Returns the combined language+region string or \x00\x00 for the default locale

Binary file not shown.

View File

@ -724,6 +724,15 @@ class APKTest(unittest.TestCase):
self.assertEqual(a._format_value('bla.bar.foo'), 'bla.bar.foo')
self.assertEqual(a._format_value(None), None)
def testMultipleLocaleAppName(self):
"""Test multiple locale appname"""
a = apk.APK(os.path.join(test_dir, 'data/APK/multiple_locale_appname_test.apk'))
self.assertEqual(a.get_app_name(), "values")
self.assertEqual(a.get_app_name(locale='en'), "values-en")
self.assertEqual(a.get_app_name(locale='zh-rCN'), "values-zh-rCN")
self.assertEqual(a.get_app_name(locale='zh-rTW'), "values-zh-rTW")
self.assertEqual(a.get_app_name(locale='ru-rRU'), "values-ru-rRU")
if __name__ == '__main__':
unittest.main(failfast=True)