-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadb.php
228 lines (194 loc) · 7.53 KB
/
adb.php
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
<?php
class adb
{
public $success;
public $error_msg;
public $host;
public $port;
public $uri;
public $log;
public $isDeviceConnected;
public $isUnauthorized;
public $isError;
public $isUnable;
public $isDisplayOn;
public $logFile;
function __construct()
{
$this->logFile = '/var/log/adb.txt';
$this->isDeviceConnected = false;
$this->isUnauthorized = false;
$this->isUnable = false;
$this->isError = false;
$this->isDisplayOn = false;
$this->success = false;
$this->error_msg = "";
}
private function elog($msg)
{
if (!empty(trim($msg))) {
file_put_contents($this->logFile, $msg . PHP_EOL, FILE_APPEND | LOCK_EX);
}
}
private function myShellExec($cmd)
{
ob_start();
$lastLine = (string)system($cmd . " 2>&1", $returnVal);
$strOutput = (string)ob_get_contents();
ob_end_clean();
$value = $returnVal ? $lastLine : $strOutput;
if (!empty(trim($value))) {
file_put_contents($this->logFile, $value . PHP_EOL, FILE_APPEND | LOCK_EX);
}
return $value;
}
private function getDeviceConnectionStatus()
{
$results = $this->myShellExec("adb devices | grep $this->uri;");
return $results ? $this->checkResponse($results) : false;
}
private function getDisplayPowerStatus()
{
$results = $this->myShellExec("adb -s $this->uri shell dumpsys power | grep 'Display Power: state=ON';");
return $results ? $this->checkResponse($results) : false;
}
private function checkResponse($results)
{
if (strpos($results, 'unauthorized') !== false) {
$this->elog("Connection is unauthorized to $this->uri");
$this->isUnauthorized = true;
return false;
}
if (strpos($results, 'unable') !== false) {
$this->elog("Failed to connect to $this->uri");
$this->isUnable = true;
return false;
}
if (strpos($results, 'error') !== false) {
$this->elog("General error with device $this->uri");
$this->isError = true;
return false;
}
return true;
}
public function connectToDevice()
{
$results = $this->getDeviceConnectionStatus();
if ($results) {
$this->elog("Device is already connected to $this->uri");
return true;
} else {
if($this->isUnauthorized || $this->isUnable || $this->isError){
$this->myShellExec("adb disconnect $this->uri;");
return false;
}
$this->elog("Connecting to $this->uri");
$results = $this->myShellExec("adb connect $this->uri;");
return $this->checkResponse($results);
}
}
public function setDevice($device = [])
{
$this->host = $device['host'];
$this->port = isset($device['port']) ? $device['port'] : 5555;
$this->uri = "$this->host:$this->port";
if (!`which adb`) {
$this->success = false;
$this->error_msg = "ADB doesn't exist.";
}else{
$this->success = true;
}
return ['success' => $this->success, 'error_msg' => $this->error_msg];
}
public function turnOff()
{
$this->elog("Turning off Display on $this->uri");
$this->isDeviceConnected = $this->connectToDevice();
if ($this->isDeviceConnected) {
$this->isDisplayOn = $this->getDisplayPowerStatus();
if ($this->isDisplayOn) {
$this->sendKey("KEYCODE_POWER");
$this->isDisplayOn = $this->getDisplayPowerStatus();
if ($this->isDisplayOn) {
$this->sendKey("KEYCODE_SLEEP");
$this->isDisplayOn = $this->getDisplayPowerStatus();
}
}
}
// Response
if ($this->isDeviceConnected && !$this->isDisplayOn) {
$this->success = true;
} elseif ($this->isDeviceConnected && $this->isDisplayOn) {
$this->success = true;
$this->error_msg = "Device is connected but could not turn off Display";
} elseif (!$this->isDeviceConnected) {
$this->success = false;
$this->error_msg = "Could not connect to device";
}
return ['success' => $this->success, 'connected' => $this->isDeviceConnected, 'display' => $this->isDisplayOn, 'error_msg' => $this->error_msg];
}
public function turnOnAndConnect()
{
$this->elog("Turning on Display on $this->uri");
$this->isDeviceConnected = $this->connectToDevice();
if ($this->isDeviceConnected) {
$this->isDisplayOn = $this->getDisplayPowerStatus();
if (!$this->isDisplayOn) {
$this->sendKey("KEYCODE_WAKEUP");
$this->isDisplayOn = $this->getDisplayPowerStatus();
if(!$this->isDisplayOn){
$this->sendKey("KEYCODE_POWER");
$this->isDisplayOn = $this->getDisplayPowerStatus();
}
}
}
// Response
if ($this->isDeviceConnected && $this->isDisplayOn) {
$this->success = true;
} elseif ($this->isDeviceConnected && !$this->isDisplayOn) {
$this->success = true;
$this->error_msg = "Device is connected but could not turn on Display";
} elseif (!$this->isDeviceConnected) {
$this->success = false;
$this->error_msg = "Could not connect to device";
}
return ['success' => $this->success, 'connected' => $this->isDeviceConnected, 'display' => $this->isDisplayOn, 'error_msg' => $this->error_msg];
}
public function turnOnAndConnectAndLaunch($package)
{
$this->elog("Turning on $package on $this->uri");
$connected = $this->turnOnAndConnect();
if ($connected['success']) {
$this->sendKey("KEYCODE_WAKEUP");
$packageIntent = $this->myShellExec('adb -s ' . $this->uri . ' shell pm dump ' . $package . ' | grep -A 1 "MAIN" | grep ' . $package . ' | awk \'{print $2}\' | grep ' . $package . ';');
$results = $this->myShellExec('adb -s ' . $this->uri . ' shell am start -n ' . $packageIntent);
if (!$this->checkResponse($results)) {
$connected['success'] = false;
$connected['error_msg'] = $results;
}
}
return $connected;
}
public function turnOnAndConnectAndLaunchNetflixWithVideoId($id = null)
{
$this->elog("Turning on Netflix with Video ID: $id on $this->uri");
$connected = $this->turnOnAndConnect();
if (!empty($id)) {
if ($connected['success']) {
$this->sendKey("KEYCODE_WAKEUP");
$results = $this->myShellExec('adb -s ' . $this->uri . ' shell "am start -W -a android.intent.action.VIEW -d http://www.netflix.com/watch/' . $id . ' -e source 30 com.netflix.ninja/com.netflix.ninja.MainActivity";');
if (!$this->checkResponse($results)) {
$connected['success'] = false;
$connected['error_msg'] = $results;
}
}
}
return $connected;
}
public function sendKey($keys)
{
$this->elog("Sending keys $keys on $this->uri");
$results = $this->myShellExec("adb -s $this->uri shell input keyevent $keys;");
return $this->checkResponse($results);
}
}