-
Notifications
You must be signed in to change notification settings - Fork 0
/
installed_package.go
247 lines (229 loc) · 6.69 KB
/
installed_package.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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 adb
import (
"context"
"fmt"
"regexp"
"sort"
"strconv"
"strings"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/android"
"github.com/google/gapid/core/os/device"
)
// InstalledPackages returns the sorted list of installed packages on the
// device.
func (b *binding) InstalledPackages(ctx context.Context) (android.InstalledPackages, error) {
str, err := b.Shell("dumpsys", "package").Call(ctx)
if err != nil {
return nil, log.Errf(ctx, err, "Failed to get installed packages")
}
return b.parsePackages(str)
}
// InstalledPackage returns information about a single installed package on the
// device.
func (b *binding) InstalledPackage(ctx context.Context, name string) (*android.InstalledPackage, error) {
str, err := b.Shell("dumpsys", "package", name).Call(ctx)
if err != nil {
return nil, log.Errf(ctx, err, "Failed to get installed packages")
}
packages, err := b.parsePackages(str)
if err != nil {
return nil, err
}
switch len(packages) {
case 0:
return nil, fmt.Errorf("Package '%v' not found", name)
case 1:
return packages[0], nil
default:
return nil, fmt.Errorf("%v packages found with name '%v'", len(packages), name)
}
}
// The minSdk field was added more recently [see https://goo.gl/UN7oFv]
var reVersionCodeMinSDKTargetSDK = regexp.MustCompile("^(?:versionCode=([0-9]+))(?: minSdk=([0-9]+))? (?:targetSdk=([0-9]+))?.*$")
func (b *binding) parsePackages(str string) (android.InstalledPackages, error) {
tree := parseTabbedTree(str)
packageMap := map[string]*android.InstalledPackage{}
parseActions := func(group *treeNode, cb func(pkg *android.InstalledPackage, name, owner string)) error {
actions := group.find("Non-Data Actions:")
if actions == nil {
return fmt.Errorf("Could not find Non-Data Actions in dumpsys")
}
for _, action := range actions.children {
for _, entry := range action.children {
// 43178558 com.google.foo/.FooActivity filter 431d7db8
// 43178558 com.google.foo/.FooActivity
fields := strings.Fields(entry.text)
if len(fields) < 2 {
return fmt.Errorf("Could not parse package: '%v'", entry.text)
}
component := fields[1]
parts := strings.SplitN(component, "/", 2)
pkgName := parts[0]
p, ok := packageMap[pkgName]
if !ok {
p = &android.InstalledPackage{
Name: pkgName,
Device: b,
ActivityActions: android.ActivityActions{},
ServiceActions: android.ServiceActions{},
ABI: device.UnknownABI,
}
packageMap[pkgName] = p
}
actionName := strings.TrimRight(action.text, ":")
actionOwner := parts[1]
if strings.HasPrefix(actionOwner, ".") {
actionOwner = pkgName + actionOwner
}
cb(p, actionName, actionOwner)
}
}
return nil
}
if activities := tree.find("Activity Resolver Table:"); activities != nil {
err := parseActions(activities, func(pkg *android.InstalledPackage, name, owner string) {
pkg.ActivityActions = append(pkg.ActivityActions, &android.ActivityAction{
Package: pkg,
Name: name,
Activity: owner,
})
})
if err != nil {
return nil, err
}
}
if services := tree.find("Service Resolver Table:"); services != nil {
err := parseActions(services, func(pkg *android.InstalledPackage, name, owner string) {
pkg.ServiceActions = append(pkg.ServiceActions, &android.ServiceAction{
Package: pkg,
Name: name,
Service: owner,
})
})
if err != nil {
return nil, err
}
}
// Read the "Packages:" section if it is present and use it to set ABI
packSection := tree.find("Packages:")
if packSection != nil {
for _, pack := range packSection.children {
// Package [com.google.foo] (ffffffc):
fields := strings.Fields(pack.text)
if len(fields) != 3 {
continue
}
name := strings.Trim(fields[1], "[]")
ip, ok := packageMap[name]
if !ok {
// We didn't find an action for this package
ip = &android.InstalledPackage{
Name: name,
Device: b,
ActivityActions: android.ActivityActions{},
ServiceActions: android.ServiceActions{},
ABI: device.UnknownABI,
}
packageMap[name] = ip
}
for _, attr := range pack.children {
av := strings.TrimSpace(attr.text)
splits := strings.SplitN(av, "=", 2)
if len(splits) < 2 {
continue
}
switch {
case strings.HasPrefix(av, "flags="):
ip.Debuggable = strings.Contains(av, " DEBUGGABLE ")
case strings.HasPrefix(av, "versionCode="):
match := reVersionCodeMinSDKTargetSDK.FindStringSubmatch(av)
if len(match) == 4 {
ip.VersionCode, _ = strconv.Atoi(match[1])
ip.MinSDK, _ = strconv.Atoi(match[2])
ip.TargetSdk, _ = strconv.Atoi(match[3])
}
case strings.HasPrefix(av, "versionName="):
ip.VersionName = splits[1]
case strings.HasPrefix(av, "primaryCpuAbi="):
// primaryCpuAbi=arm64-v8a
// primaryCpuAbi=null
if splits[1] == "null" {
break // This means the package manager will select the platform ABI
}
ip.ABI = device.ABIByName(splits[1])
}
}
}
}
packages := make(android.InstalledPackages, 0, len(packageMap))
for _, p := range packageMap {
packages = append(packages, p)
}
sort.Sort(packages)
return packages, nil
}
type treeNode struct {
text string
children []*treeNode
parent *treeNode
depth int
}
func (t *treeNode) find(name string) *treeNode {
if t == nil {
return nil
}
for _, c := range t.children {
if c.text == name {
return c
}
}
return nil
}
func parseTabbedTree(str string) *treeNode {
head := &treeNode{depth: -1}
for _, line := range strings.Split(str, "\n") {
line = strings.TrimRight(line, "\r")
if line == "" {
continue
}
// Calculate the line's depth
depth := 0
for i, r := range line {
if r == ' ' {
depth++
} else {
line = line[i:]
break
}
}
// Find the insertion point
for {
if head.depth >= depth {
head = head.parent
} else {
node := &treeNode{text: line, depth: depth, parent: head}
head.children = append(head.children, node)
head = node
break
}
}
}
for head.parent != nil {
head = head.parent
}
return head
}