-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrotation.go
125 lines (117 loc) · 2.47 KB
/
rotation.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
/*
* File: rotation.go
* Author : bigwavelet
* Description: android rotation watcher
* Created: 2016-09-13
*/
package minicap
import (
"bufio"
"io"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
type Rotation struct {
d AdbDevice
orientation int
proc *exec.Cmd
closed bool
brd *bufio.Reader
}
func newRotationService(option Options) (r Rotation, err error) {
r = Rotation{}
r.d, err = newAdbDevice(option.Serial, option.Adb)
r.closed = true
return
}
//download file to device
func (r *Rotation) download(path, url string) (err error) {
fout, err := r.d.Device.OpenWrite(path, 0755, time.Now())
if err != nil {
return
}
defer fout.Close()
response, err := http.Get(url)
if err != nil {
return
}
defer response.Body.Close()
_, err = io.Copy(fout, response.Body)
if err != nil {
return
}
return
}
//install rotationWatcher.apk
func (r *Rotation) install() (err error) {
url := "https://github.com/NetEaseGame/AutomatorX/raw/master/atx/vendor/RotationWatcher.apk"
//check package
pkgName := "jp.co.cyberagent.stf.rotationwatcher"
plist, err := r.d.getPackageList()
if err != nil {
return
}
for _, val := range plist {
if strings.Contains(val, pkgName) {
return
}
}
//downlaod apk
path := "/data/local/tmp/RotationWatcher.apk"
err = r.download(path, url)
if err != nil {
return
}
//install apk
_, err = r.d.shell("pm", "install", "-rt", path)
if err != nil {
return
}
return
}
//start rotation service
func (r *Rotation) start() (err error) {
pkgName := "jp.co.cyberagent.stf.rotationwatcher"
out, err := r.d.shell("pm path " + pkgName)
if err != nil {
return
}
fields := strings.Split(strip(out), ":")
path := fields[len(fields)-1]
r.proc = r.d.buildCommand("CLASSPATH="+path, "app_process", "/system/bin", "jp.co.cyberagent.stf.rotationwatcher.RotationWatcher")
r.proc.Stderr = os.Stderr
stdoutReader, err := r.proc.StdoutPipe()
if err != nil {
return
}
r.brd = bufio.NewReader(stdoutReader)
return r.proc.Start()
}
func (r *Rotation) watch() (orienC <-chan int, err error) {
rC := make(chan int, 0)
go func() {
for {
line, _, er := r.brd.ReadLine()
if er != nil {
// restart if rotation watcher crashed.
r.start()
continue
//break
}
tmp := strings.Replace(string(line), "\r", "", -1)
tmp = strings.Replace(tmp, "\n", "", -1)
orientation, er := strconv.Atoi(string(tmp))
if er != nil {
break
}
rC <- orientation
}
close(rC)
}()
orienC = rC
return
}