/
action.go
171 lines (139 loc) · 4.99 KB
/
action.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package android
import (
"fmt"
"strings"
)
// ActivityAction represents an Android action that can be sent as an intent to
// an activity.
type ActivityAction struct {
// The action name.
// Example: android.intent.action.MAIN
Name string
// The package that owns this activity action.
Package *InstalledPackage
// The activity that performs the action.
// Example: FooBarAction
Activity string
}
// ActivityActions is a list of activity actions.
type ActivityActions []*ActivityAction
// FindByName returns the service action with the specified names, or nil if no
// action with the matching names is found.
func (l ActivityActions) FindByName(action, activity string) *ActivityAction {
for _, a := range l {
if a.Name == action && a.Activity == activity {
return a
}
}
return nil
}
// ServiceAction represents an Android action that can be sent as an intent to
// a service.
type ServiceAction struct {
// The action name.
// Example: android.intent.action.MAIN
Name string
// The package that owns this service action.
Package *InstalledPackage
// The service that performs the action.
// Example: FooBarService
Service string
}
// ServiceActions is a list of service actions.
type ServiceActions []*ServiceAction
// FindByName returns the service action with the specified names, or nil if no
// action with the matching names is found.
func (l ServiceActions) FindByName(action, service string) *ServiceAction {
for _, a := range l {
if a.Name == action && a.Service == service {
return a
}
}
return nil
}
// Component returns the component name with package name prefix. For example:
// "com.example.app/.ExampleActivity" or "com.example.app/com.foo.ExampleActivity"
func (a *ActivityAction) Component() string {
return actionComponent(a.Package, a.Activity)
}
func (a *ActivityAction) String() string {
return a.Name + ":" + a.Component()
}
// Component returns the component name with package name prefix. For example:
// "com.example.app/.ExampleService" or "com.example.app/com.foo.ExampleService"
func (a *ServiceAction) Component() string {
return actionComponent(a.Package, a.Service)
}
func (a *ServiceAction) String() string {
return a.Name + ":" + a.Component()
}
func actionComponent(pkg *InstalledPackage, owner string) string {
if strings.ContainsRune(owner, '.') {
return fmt.Sprintf("%s/%s", pkg.Name, owner)
}
return fmt.Sprintf("%s/.%s", pkg.Name, owner)
}
// ActionExtra is the interface implemented by intent extras.
type ActionExtra interface {
// Flags returns the formatted flags to pass to the Android am command.
Flags() []string
}
// StringExtra represents an extra with a string value.
type StringExtra struct {
Key string
Value string
}
// BoolExtra represents an extra with a bool value.
type BoolExtra struct {
Key string
Value bool
}
// IntExtra represents an extra with an int value.
type IntExtra struct {
Key string
Value int
}
// LongExtra represents an extra with a long value.
type LongExtra struct {
Key string
Value int
}
// FloatExtra represents an extra with a float value.
type FloatExtra struct {
Key string
Value float32
}
// URIExtra represents an extra with a URI value.
type URIExtra struct {
Key string
Value string
}
// CustomExtras is a list of custom intent extras
type CustomExtras []string
// Flags returns the formatted flags to pass to the Android am command.
func (e StringExtra) Flags() []string { return []string{"--es", e.Key, fmt.Sprintf(`"%v"`, e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e BoolExtra) Flags() []string { return []string{"--ez", e.Key, fmt.Sprintf("%v", e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e IntExtra) Flags() []string { return []string{"--ei", e.Key, fmt.Sprintf("%v", e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e LongExtra) Flags() []string { return []string{"--el", e.Key, fmt.Sprintf("%v", e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e FloatExtra) Flags() []string { return []string{"--ef", e.Key, fmt.Sprintf("%v", e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e URIExtra) Flags() []string { return []string{"--eu", e.Key, fmt.Sprintf("%v", e.Value)} }
// Flags returns the formatted flags to pass to the Android am command.
func (e CustomExtras) Flags() []string { return []string(e) }