1 # Copyright 2014 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.
5 """Manages intents and associated information.
7 This is generally intended to be used with functions that calls Android's
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):
19 action: A string containing the action.
20 activity: A string that, with |package|, can be used to specify the
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
27 flags: A string containing flags to pass.
28 package: A string that, with activity, can be used to specify the
32 self
._activity
= activity
33 if isinstance(category
, list) or category
is None:
34 self
._category
= category
36 self
._category
= [category
]
37 self
._component
= component
41 self
._package
= package
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
)
62 return self
._component
82 """Returns the intent as a list of arguments for the activity manager.
84 For details refer to the specification at:
85 - http://developer.android.com/tools/help/adb.html#IntentSpec
89 args
.extend(['-a', self
.action
])
91 args
.extend(['-d', self
.data
])
93 args
.extend(arg
for cat
in self
.category
for arg
in ('-c', cat
))
95 args
.extend(['-n', self
.component
])
97 args
.extend(['-f', self
.flags
])
99 for key
, value
in self
.extras
.iteritems():
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
)])
111 raise NotImplementedError(
112 'Intent does not know how to pass %s extras' % type(value
))