forked from google/agi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
installed_package.go
286 lines (252 loc) · 9.33 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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// 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 (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"github.com/google/gapid/core/os/device"
)
// https://android.googlesource.com/platform/bionic/+/master/libc/include/sys/system_properties.h#38
// Actually 32, but that includes null-terminator.
const maxPropName = 31
// InstalledPackage describes a package installed on a device.
type InstalledPackage struct {
Name string // Name of the package.
Device Device // The device this package is installed on.
ActivityActions ActivityActions // The activity actions this package supports.
ServiceActions ServiceActions // The service actions this package supports.
ABI *device.ABI // The ABI of the package or empty
Debuggable bool // Whether the package is debuggable or not
VersionCode int // The version code as reported by the manifest.
VersionName string // The version name as reported by the manifest.
MinSDK int // The minimum SDK reported by the manifest.
TargetSdk int // The target SDK reported by the manifest.
}
// InstalledPackages is a list of installed packages.
type InstalledPackages []*InstalledPackage
func (l InstalledPackages) Len() int { return len(l) }
func (l InstalledPackages) Less(i, j int) bool { return l[i].Name < l[j].Name }
func (l InstalledPackages) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
// ErrProcessNotFound is returned by InstalledPackage.Pid when no running process of the package is found.
var ErrProcessNotFound = fmt.Errorf("Process not found")
// FindByPartialName returns a list of installed packages whose name contains or
// equals s (case insensitive).
func (l InstalledPackages) FindByPartialName(s string) InstalledPackages {
s = strings.ToLower(s)
found := make(InstalledPackages, 0, 1)
for _, p := range l {
if strings.Contains(strings.ToLower(p.Name), s) {
found = append(found, p)
}
}
return found
}
// FindByName returns the installed package whose name exactly matches s.
func (l InstalledPackages) FindByName(s string) *InstalledPackage {
for _, p := range l {
if p.Name == s {
return p
}
}
return nil
}
// FindSingleByPartialName returns the single installed package whose name
// contains or equals s (case insensitive). If none or more than one packages
// partially matches s then an error is returned. If a package exactly matches s
// then that is returned regardless of any partial matches.
func (l InstalledPackages) FindSingleByPartialName(s string) (*InstalledPackage, error) {
found := l.FindByPartialName(s)
if len(found) == 0 {
return nil, fmt.Errorf("No packages found containing the name '%v'", s)
}
if len(found) > 1 {
names := make([]string, len(found))
for i, p := range found {
if p.Name == s { // Exact match
return p, nil
}
names[i] = p.Name
}
return nil, fmt.Errorf("%v packages found containing the name '%v':\n%v",
len(found), s, strings.Join(names, "\n"))
}
return found[0], nil
}
// WrapProperties returns the list of wrap-properties for the given installed
// package.
func (p *InstalledPackage) WrapProperties(ctx context.Context) ([]string, error) {
list, err := p.Device.SystemProperty(ctx, p.wrapPropName())
return strings.Fields(list), err
}
// SetWrapProperties sets the list of wrap-properties for the given installed
// package.
func (p *InstalledPackage) SetWrapProperties(ctx context.Context, props ...string) error {
arg := strings.Join(props, " ")
if err := p.Device.SetSystemProperty(ctx, p.wrapPropName(), arg); err != nil {
return err
}
return nil
}
// ClearCache deletes all data associated with a package.
func (p *InstalledPackage) ClearCache(ctx context.Context) error {
return p.Device.Shell("pm", "clear", p.Name).Run(ctx)
}
// Stop stops any activities belonging to the package from running on the device.
func (p *InstalledPackage) Stop(ctx context.Context) error {
return p.Device.Shell("am", "force-stop", p.Name).Run(ctx)
}
// Path returns the absolute path of the installed package on the device.
func (p *InstalledPackage) Path(ctx context.Context) (string, error) {
out, err := p.Device.Shell("pm", "path", p.Name).Call(ctx)
if err != nil {
return "", err
}
prefix := "package:"
if !strings.HasPrefix(out, prefix) {
return "", fmt.Errorf("Unexpected output: '%s'", out)
}
path := out[len(prefix):]
return path, err
}
func (p *InstalledPackage) obbStoragePath(ctx context.Context) (string, error) {
storage, err := p.Device.Shell("echo", "$EXTERNAL_STORAGE").Call(ctx)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/Android/obb/%s/main.%d.%[2]s.obb", storage, p.Name, p.VersionCode), nil
}
// OBBExists checks whether an OBB file exists in the matching location for this APK on
// the device's external storage.
func (p *InstalledPackage) OBBExists(ctx context.Context) bool {
obbStoragePath, err := p.obbStoragePath(ctx)
if err != nil {
return false
}
if p.Device.Shell("stat", obbStoragePath).Run(ctx) != nil {
return false
}
return true
}
// PushOBB places a OBB file to the correct location for an APK to access.
func (p *InstalledPackage) PushOBB(ctx context.Context, obbPath string) error {
obbStoragePath, err := p.obbStoragePath(ctx)
if err != nil {
return err
}
return p.Device.Push(ctx, obbPath, obbStoragePath)
}
// PullOBB pulls the matching OBB file from the device's external storage to the specified
// local directory.
func (p *InstalledPackage) PullOBB(ctx context.Context, target string) error {
obbStoragePath, err := p.obbStoragePath(ctx)
if err != nil {
return err
}
return p.Device.Pull(ctx, obbStoragePath, target)
}
// RemoveOBB removes the OBB file for a specific package from external storage.
func (p *InstalledPackage) RemoveOBB(ctx context.Context) error {
obbStoragePath, err := p.obbStoragePath(ctx)
if err != nil {
return err
}
return p.Device.Shell("rm", "-f", obbStoragePath).Run(ctx)
}
// GrantExternalStorageRW gives an installed package read and write permissions for external
// storage, this is mainly used to give an apk access to a pushed OBB file.
func (p *InstalledPackage) GrantExternalStorageRW(ctx context.Context) error {
err := p.Device.Shell("pm", "grant", p.Name, "android.permission.READ_EXTERNAL_STORAGE").Run(ctx)
if err != nil {
return err
}
return p.Device.Shell("pm", "grant", p.Name, "android.permission.WRITE_EXTERNAL_STORAGE").Run(ctx)
}
// FileDir returns the absolute path of the installed packages files directory.
func (p *InstalledPackage) FileDir(ctx context.Context) (string, error) {
out, err := p.Device.Shell("run-as", p.Name, "pwd").Call(ctx)
if err != nil {
return "", err
}
path := out + "/files"
return path, err
}
// AppDir returns the absolute path of the installed packages files directory.
func (p *InstalledPackage) AppDir(ctx context.Context) (string, error) {
out, err := p.Device.Shell("run-as", p.Name, "pwd").Call(ctx)
if err != nil {
return "", err
}
return out, err
}
// Pid returns the PID of the newest (if pgrep exists) running process belonging to the given package.
func (p *InstalledPackage) Pid(ctx context.Context) (int, error) {
// First, try pgrep.
out, err := p.Device.Shell("pgrep", "-n", "-f", p.Name).Call(ctx)
if err == nil {
if out == "" {
// Empty pgrep output. Process not found.
return -1, ErrProcessNotFound
}
if regexp.MustCompile("^[0-9]+$").MatchString(out) {
pid, _ := strconv.Atoi(out)
return pid, nil
}
}
// pgrep not found or other error, fall back to trying ps.
out, err = p.Device.Shell("ps").Call(ctx)
if err != nil {
return -1, err
}
matches := regexp.MustCompile(
`(?m)^\S+\s+([0-9]+)\s+[0-9]+\s+[0-9]+\s+[^\n\r]*\s+(\S+)\s*$`).FindAllStringSubmatch(out, -1)
if matches != nil {
// If we're here, we're getting sensible output from ps.
for _, match := range matches {
if match[2] == p.Name {
pid, _ := strconv.Atoi(match[1])
return pid, nil
}
}
// Process not found.
return -1, ErrProcessNotFound
}
return -1, fmt.Errorf("failed to get pid for package (pgrep and ps both missing or misbehaving)")
}
// Pull pulls the installed package from the device to the specified local directory.
func (p *InstalledPackage) Pull(ctx context.Context, target string) error {
path, err := p.Path(ctx)
if err != nil {
return err
}
return p.Device.Pull(ctx, path, target)
}
// Uninstall uninstalls the package from the device.
func (p *InstalledPackage) Uninstall(ctx context.Context) error {
return p.Device.Shell("pm", "uninstall", p.Name).Run(ctx)
}
// String returns the package name.
func (p *InstalledPackage) String() string {
return p.Name
}
func (p *InstalledPackage) wrapPropName() string {
name := "wrap." + p.Name
if len(name) > maxPropName {
// The property name must not end in dot
name = strings.TrimRight(name[:maxPropName], ".")
}
return name
}