1# Copyright (c) 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Module containing utilities for apk packages."""
6
7import re
8
9from devil.android.sdk import aapt
10
11
12_MANIFEST_ATTRIBUTE_RE = re.compile(
13    r'\s*A: ([^\(\)= ]*)(?:\([^\(\)= ]*\))?='
14    r'(?:"(.*)" \(Raw: .*\)|\(type.*?\)(.*))$')
15_MANIFEST_ELEMENT_RE = re.compile(r'\s*(?:E|N): (\S*) .*$')
16
17
18def GetPackageName(apk_path):
19  """Returns the package name of the apk."""
20  return ApkHelper(apk_path).GetPackageName()
21
22
23# TODO(jbudorick): Deprecate and remove this function once callers have been
24# converted to ApkHelper.GetInstrumentationName
25def GetInstrumentationName(apk_path):
26  """Returns the name of the Instrumentation in the apk."""
27  return ApkHelper(apk_path).GetInstrumentationName()
28
29
30def ToHelper(path_or_helper):
31  """Creates an ApkHelper unless one is already given."""
32  if isinstance(path_or_helper, basestring):
33    return ApkHelper(path_or_helper)
34  return path_or_helper
35
36
37def _ParseManifestFromApk(apk_path):
38  aapt_output = aapt.Dump('xmltree', apk_path, 'AndroidManifest.xml')
39
40  parsed_manifest = {}
41  node_stack = [parsed_manifest]
42  indent = '  '
43
44  for line in aapt_output[1:]:
45    if len(line) == 0:
46      continue
47
48    indent_depth = 0
49    while line[(len(indent) * indent_depth):].startswith(indent):
50      indent_depth += 1
51
52    node_stack = node_stack[:indent_depth]
53    node = node_stack[-1]
54
55    m = _MANIFEST_ELEMENT_RE.match(line[len(indent) * indent_depth:])
56    if m:
57      if not m.group(1) in node:
58        node[m.group(1)] = {}
59      node_stack += [node[m.group(1)]]
60      continue
61
62    m = _MANIFEST_ATTRIBUTE_RE.match(line[len(indent) * indent_depth:])
63    if m:
64      if not m.group(1) in node:
65        node[m.group(1)] = []
66      node[m.group(1)].append(m.group(2) or m.group(3))
67      continue
68
69  return parsed_manifest
70
71
72class ApkHelper(object):
73
74  def __init__(self, path):
75    self._apk_path = path
76    self._manifest = None
77
78  @property
79  def path(self):
80    return self._apk_path
81
82  def GetActivityName(self):
83    """Returns the name of the Activity in the apk."""
84    manifest_info = self._GetManifest()
85    try:
86      activity = (
87          manifest_info['manifest']['application']['activity']
88              ['android:name'][0])
89    except KeyError:
90      return None
91    if '.' not in activity:
92      activity = '%s.%s' % (self.GetPackageName(), activity)
93    elif activity.startswith('.'):
94      activity = '%s%s' % (self.GetPackageName(), activity)
95    return activity
96
97  def GetInstrumentationName(
98      self, default='android.test.InstrumentationTestRunner'):
99    """Returns the name of the Instrumentation in the apk."""
100    manifest_info = self._GetManifest()
101    try:
102      return manifest_info['manifest']['instrumentation']['android:name'][0]
103    except KeyError:
104      return default
105
106  def GetPackageName(self):
107    """Returns the package name of the apk."""
108    manifest_info = self._GetManifest()
109    try:
110      return manifest_info['manifest']['package'][0]
111    except KeyError:
112      raise Exception('Failed to determine package name of %s' % self._apk_path)
113
114  def GetPermissions(self):
115    manifest_info = self._GetManifest()
116    try:
117      return manifest_info['manifest']['uses-permission']['android:name']
118    except KeyError:
119      return []
120
121  def GetSplitName(self):
122    """Returns the name of the split of the apk."""
123    manifest_info = self._GetManifest()
124    try:
125      return manifest_info['manifest']['split'][0]
126    except KeyError:
127      return None
128
129  def HasIsolatedProcesses(self):
130    """Returns whether any services exist that use isolatedProcess=true."""
131    manifest_info = self._GetManifest()
132    try:
133      services = manifest_info['manifest']['application']['service']
134      return any(int(v, 0) for v in services['android:isolatedProcess'])
135    except KeyError:
136      return False
137
138  def _GetManifest(self):
139    if not self._manifest:
140      self._manifest = _ParseManifestFromApk(self._apk_path)
141    return self._manifest
142
143