OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Manages intents and associated information. | 5 # pylint: disable=unused-wildcard-import |
| 6 # pylint: disable=wildcard-import |
6 | 7 |
7 This is generally intended to be used with functions that calls Android's | 8 from devil.android.sdk.intent import * |
8 Am command. | |
9 """ | |
10 | |
11 class Intent(object): | |
12 | |
13 def __init__(self, action='android.intent.action.VIEW', activity=None, | |
14 category=None, component=None, data=None, extras=None, | |
15 flags=None, package=None): | |
16 """Creates an Intent. | |
17 | |
18 Args: | |
19 action: A string containing the action. | |
20 activity: A string that, with |package|, can be used to specify the | |
21 component. | |
22 category: A string or list containing any categories. | |
23 component: A string that specifies the component to send the intent to. | |
24 data: A string containing a data URI. | |
25 extras: A dict containing extra parameters to be passed along with the | |
26 intent. | |
27 flags: A string containing flags to pass. | |
28 package: A string that, with activity, can be used to specify the | |
29 component. | |
30 """ | |
31 self._action = action | |
32 self._activity = activity | |
33 if isinstance(category, list) or category is None: | |
34 self._category = category | |
35 else: | |
36 self._category = [category] | |
37 self._component = component | |
38 self._data = data | |
39 self._extras = extras | |
40 self._flags = flags | |
41 self._package = package | |
42 | |
43 if self._component and '/' in component: | |
44 self._package, self._activity = component.split('/', 1) | |
45 elif self._package and self._activity: | |
46 self._component = '%s/%s' % (package, activity) | |
47 | |
48 @property | |
49 def action(self): | |
50 return self._action | |
51 | |
52 @property | |
53 def activity(self): | |
54 return self._activity | |
55 | |
56 @property | |
57 def category(self): | |
58 return self._category | |
59 | |
60 @property | |
61 def component(self): | |
62 return self._component | |
63 | |
64 @property | |
65 def data(self): | |
66 return self._data | |
67 | |
68 @property | |
69 def extras(self): | |
70 return self._extras | |
71 | |
72 @property | |
73 def flags(self): | |
74 return self._flags | |
75 | |
76 @property | |
77 def package(self): | |
78 return self._package | |
79 | |
80 @property | |
81 def am_args(self): | |
82 """Returns the intent as a list of arguments for the activity manager. | |
83 | |
84 For details refer to the specification at: | |
85 - http://developer.android.com/tools/help/adb.html#IntentSpec | |
86 """ | |
87 args = [] | |
88 if self.action: | |
89 args.extend(['-a', self.action]) | |
90 if self.data: | |
91 args.extend(['-d', self.data]) | |
92 if self.category: | |
93 args.extend(arg for cat in self.category for arg in ('-c', cat)) | |
94 if self.component: | |
95 args.extend(['-n', self.component]) | |
96 if self.flags: | |
97 args.extend(['-f', self.flags]) | |
98 if self.extras: | |
99 for key, value in self.extras.iteritems(): | |
100 if value is None: | |
101 args.extend(['--esn', key]) | |
102 elif isinstance(value, str): | |
103 args.extend(['--es', key, value]) | |
104 elif isinstance(value, bool): | |
105 args.extend(['--ez', key, str(value)]) | |
106 elif isinstance(value, int): | |
107 args.extend(['--ei', key, str(value)]) | |
108 elif isinstance(value, float): | |
109 args.extend(['--ef', key, str(value)]) | |
110 else: | |
111 raise NotImplementedError( | |
112 'Intent does not know how to pass %s extras' % type(value)) | |
113 return args | |
OLD | NEW |