-
Notifications
You must be signed in to change notification settings - Fork 327
/
commands.go
157 lines (143 loc) · 4.82 KB
/
commands.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
// 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 (
"bytes"
"context"
"fmt"
"strings"
"time"
"github.com/google/gapid/core/fault"
"github.com/google/gapid/core/log"
"github.com/google/gapid/core/os/android"
)
const (
// ErrDeviceNotRooted is returned by Device.Root when the device is running a
// production build as is not 'rooted'.
ErrDeviceNotRooted = fault.Const("Device is not rooted")
ErrRootFailed = fault.Const("Device failed to switch to root")
maxRootAttempts = 5
)
func isRootSuccessful(line string) bool {
for _, expected := range []string{
"adbd is already running as root",
"* daemon started successfully *",
} {
if line == expected {
return true
}
}
return false
}
// Root restarts adb as root. If the device is running a production build then
// Root will return ErrDeviceNotRooted.
func (b *binding) Root(ctx context.Context) error {
buf := bytes.Buffer{}
buf.WriteString("adb root gave output:")
retry:
for attempt := 0; attempt < maxRootAttempts; attempt++ {
output, err := b.Command("root").Call(ctx)
if err != nil {
return err
}
if len(output) == 0 {
return nil // Assume no output is success
}
output = strings.Replace(output, "\r\n", "\n", -1) // Not expected, but let's be safe.
buf.WriteString(fmt.Sprintf("\n#%d: %v", attempt, output))
lines := strings.Split(output, "\n")
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
if isRootSuccessful(line) {
return nil // Success
}
switch line {
case "adbd cannot run as root in production builds":
return ErrDeviceNotRooted
case "restarting adbd as root":
time.Sleep(time.Millisecond * 100)
continue retry
default:
// Some output we weren't expecting.
}
}
}
return log.Err(ctx, ErrRootFailed, buf.String())
}
// InstallAPK installs the specified APK to the device. If reinstall is true
// and the package is already installed on the device then it will be replaced.
func (b *binding) InstallAPK(ctx context.Context, path string, reinstall bool, grantPermissions bool) error {
args := []string{}
if reinstall {
args = append(args, "-r")
}
if grantPermissions && b.Instance().GetConfiguration().GetOS().GetMajor() >= 6 {
// Starting with Android 6.0, permissions are not granted by default
// during installation. Before Android 6.0, the flag did not exist.
args = append(args, "-g")
}
args = append(args, path)
return b.Command("install", args...).Run(ctx)
}
// SELinuxEnforcing returns true if the device is currently in a
// SELinux enforcing mode, or false if the device is currently in a SELinux
// permissive mode.
func (b *binding) SELinuxEnforcing(ctx context.Context) (bool, error) {
res, err := b.Shell("getenforce").Call(ctx)
return strings.Contains(strings.ToLower(res), "enforcing"), err
}
// SetSELinuxEnforcing changes the SELinux-enforcing mode.
func (b *binding) SetSELinuxEnforcing(ctx context.Context, enforce bool) error {
if enforce {
return b.Shell("setenforce", "1").Run(ctx)
}
return b.Shell("setenforce", "0").Run(ctx)
}
// StartActivity launches the specified activity action.
func (b *binding) StartActivity(ctx context.Context, a android.ActivityAction, extras ...android.ActionExtra) error {
args := append([]string{
"start",
"-S", // Force-stop the target app before starting the activity
"-a", a.Name,
"-n", a.Component(),
}, extrasFlags(extras)...)
return b.Shell("am", args...).Run(ctx)
}
// StartActivityForDebug launches the specified activity in debug mode.
func (b *binding) StartActivityForDebug(ctx context.Context, a android.ActivityAction, extras ...android.ActionExtra) error {
args := append([]string{
"start",
"-S", // Force-stop the target app before starting the activity
"-D", // Debug mode
"-a", a.Name,
"-n", a.Component(),
}, extrasFlags(extras)...)
return b.Shell("am", args...).Run(ctx)
}
// StartService launches the specified service action.
func (b *binding) StartService(ctx context.Context, a android.ServiceAction, extras ...android.ActionExtra) error {
args := append([]string{
"startservice",
"-a", a.Name,
"-n", a.Component(),
}, extrasFlags(extras)...)
return b.Shell("am", args...).Run(ctx)
}
func extrasFlags(extras []android.ActionExtra) []string {
flags := []string{}
for _, e := range extras {
flags = append(flags, e.Flags()...)
}
return flags
}