Skip to content

Commit 98232d1

Browse files
committed
make screenshot works with android 4.4
1 parent ca23637 commit 98232d1

File tree

3 files changed

+81
-25
lines changed

3 files changed

+81
-25
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# ya = your adb
2-
[![Build Status](https://travis-ci.org/codeskyblue/ya.svg?branch=master)](https://travis-ci.org/codeskyblue/ya)
1+
# fa = fast adb
2+
[![Build Status](https://travis-ci.org/codeskyblue/fa.svg?branch=master)](https://travis-ci.org/codeskyblue/fa)
33

4-
`ya` is a command line tool that wraps `adb` in order to extend it with extra features and commands that make working with Android easier.
4+
`fa` is a command line tool that wraps `adb` in order to extend it with extra features and commands that make working with Android easier.
55

66
## Features
77
- [x] show device selection when multi device connected
@@ -14,24 +14,24 @@
1414
**For mac**
1515

1616
```bash
17-
brew install codeskyblue/tap/ya
17+
brew install codeskyblue/tap/fa
1818
```
1919

2020
**For windows and linux**
2121

22-
download binary from [**releases**](https://github.com/codeskyblue/ya/releases)
22+
download binary from [**releases**](https://github.com/codeskyblue/fa/releases)
2323

2424
## Usage
2525
Screenshot
2626

2727
```bash
28-
ya screenshot -o screenshot.png
28+
fa screenshot -o screenshot.png
2929
```
3030

3131
~~Install APK~~
3232

3333
```
34-
ya install https://example.org/demo.apk
34+
fa install https://example.org/demo.apk
3535
```
3636

3737
## Reference

main.go

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"syscall"
1212

1313
"github.com/manifoldco/promptui"
14-
"gopkg.in/urfave/cli.v1"
14+
cli "gopkg.in/urfave/cli.v1"
1515
)
1616

1717
var (
@@ -163,24 +163,12 @@ func main() {
163163
Value: "screenshot.png",
164164
Usage: "output screenshot name",
165165
},
166+
cli.BoolFlag{
167+
Name: "open",
168+
Usage: "open file after screenshot",
169+
},
166170
},
167-
Action: func(ctx *cli.Context) error {
168-
serial, err := chooseOne()
169-
if err != nil {
170-
return err
171-
}
172-
log.Println(ctx.String("output"))
173-
c := exec.Command(adbPath(), "exec-out", "screencap", "-p")
174-
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial)
175-
imgfile, err := os.Create(ctx.String("output"))
176-
if err != nil {
177-
return err
178-
}
179-
defer imgfile.Close()
180-
c.Stdout = imgfile
181-
c.Stderr = os.Stderr
182-
return c.Run()
183-
},
171+
Action: actScreenshot,
184172
},
185173
}
186174
err := app.Run(os.Args)

screenshot.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"os"
6+
"os/exec"
7+
8+
cli "gopkg.in/urfave/cli.v1"
9+
)
10+
11+
func adbCommand(serial string, args ...string) *exec.Cmd {
12+
c := exec.Command(adbPath(), args...)
13+
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial)
14+
return c
15+
}
16+
17+
func screenshotExecOut(serial, output string) error {
18+
serial, err := chooseOne()
19+
if err != nil {
20+
return err
21+
}
22+
c := adbCommand(serial, "exec-out", "screencap", "-p")
23+
imgfile, err := os.Create(output)
24+
if err != nil {
25+
return err
26+
}
27+
defer func() {
28+
imgfile.Close()
29+
if err != nil {
30+
os.Remove(output)
31+
}
32+
}()
33+
c.Stdout = imgfile
34+
// c.Stderr = os.Stderr
35+
return c.Run()
36+
}
37+
38+
func screenshotScreencap(serial, output string) error {
39+
tmpPath := "/sdcard/fa-screenshot.png"
40+
c := adbCommand(serial, "shell", "screencap", "-p", tmpPath)
41+
if err := c.Run(); err != nil {
42+
return err
43+
}
44+
defer adbCommand(serial, "shell", "rm", tmpPath).Run()
45+
return adbCommand(serial, "pull", tmpPath, output).Run()
46+
}
47+
48+
func actScreenshot(ctx *cli.Context) (err error) {
49+
serial, err := chooseOne()
50+
if err != nil {
51+
return err
52+
}
53+
output := ctx.String("output")
54+
err = screenshotExecOut(serial, output)
55+
if err != nil {
56+
// log.Println("FAIL:", "exec-out", "screencap")
57+
err = screenshotScreencap(serial, output)
58+
}
59+
if err == nil {
60+
// log.Println("OKAY:", "shell", "screencap")
61+
log.Println("save to", output)
62+
if ctx.Bool("open") {
63+
// TODO(ssx): only works on mac
64+
exec.Command("open", output).Run()
65+
}
66+
}
67+
return
68+
}

0 commit comments

Comments
 (0)