Skip to content

Commit

Permalink
make screenshot works with android 4.4
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Dec 9, 2018
1 parent ca23637 commit 98232d1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 25 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ya = your adb
[![Build Status](https://travis-ci.org/codeskyblue/ya.svg?branch=master)](https://travis-ci.org/codeskyblue/ya)
# fa = fast adb
[![Build Status](https://travis-ci.org/codeskyblue/fa.svg?branch=master)](https://travis-ci.org/codeskyblue/fa)

`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.
`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.

## Features
- [x] show device selection when multi device connected
Expand All @@ -14,24 +14,24 @@
**For mac**

```bash
brew install codeskyblue/tap/ya
brew install codeskyblue/tap/fa
```

**For windows and linux**

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

## Usage
Screenshot

```bash
ya screenshot -o screenshot.png
fa screenshot -o screenshot.png
```

~~Install APK~~

```
ya install https://example.org/demo.apk
fa install https://example.org/demo.apk
```

## Reference
Expand Down
24 changes: 6 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"syscall"

"github.com/manifoldco/promptui"
"gopkg.in/urfave/cli.v1"
cli "gopkg.in/urfave/cli.v1"
)

var (
Expand Down Expand Up @@ -163,24 +163,12 @@ func main() {
Value: "screenshot.png",
Usage: "output screenshot name",
},
cli.BoolFlag{
Name: "open",
Usage: "open file after screenshot",
},
},
Action: func(ctx *cli.Context) error {
serial, err := chooseOne()
if err != nil {
return err
}
log.Println(ctx.String("output"))
c := exec.Command(adbPath(), "exec-out", "screencap", "-p")
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial)
imgfile, err := os.Create(ctx.String("output"))
if err != nil {
return err
}
defer imgfile.Close()
c.Stdout = imgfile
c.Stderr = os.Stderr
return c.Run()
},
Action: actScreenshot,
},
}
err := app.Run(os.Args)
Expand Down
68 changes: 68 additions & 0 deletions screenshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package main

import (
"log"
"os"
"os/exec"

cli "gopkg.in/urfave/cli.v1"
)

func adbCommand(serial string, args ...string) *exec.Cmd {
c := exec.Command(adbPath(), args...)
c.Env = append(os.Environ(), "ANDROID_SERIAL="+serial)
return c
}

func screenshotExecOut(serial, output string) error {
serial, err := chooseOne()
if err != nil {
return err
}
c := adbCommand(serial, "exec-out", "screencap", "-p")
imgfile, err := os.Create(output)
if err != nil {
return err
}
defer func() {
imgfile.Close()
if err != nil {
os.Remove(output)
}
}()
c.Stdout = imgfile
// c.Stderr = os.Stderr
return c.Run()
}

func screenshotScreencap(serial, output string) error {
tmpPath := "/sdcard/fa-screenshot.png"
c := adbCommand(serial, "shell", "screencap", "-p", tmpPath)
if err := c.Run(); err != nil {
return err
}
defer adbCommand(serial, "shell", "rm", tmpPath).Run()
return adbCommand(serial, "pull", tmpPath, output).Run()
}

func actScreenshot(ctx *cli.Context) (err error) {
serial, err := chooseOne()
if err != nil {
return err
}
output := ctx.String("output")
err = screenshotExecOut(serial, output)
if err != nil {
// log.Println("FAIL:", "exec-out", "screencap")
err = screenshotScreencap(serial, output)
}
if err == nil {
// log.Println("OKAY:", "shell", "screencap")
log.Println("save to", output)
if ctx.Bool("open") {
// TODO(ssx): only works on mac
exec.Command("open", output).Run()
}
}
return
}

0 comments on commit 98232d1

Please sign in to comment.