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