Files
GoADB/app.go

62 lines
1.4 KiB
Go

package goadb
import (
"fmt"
"regexp"
"strconv"
"strings"
)
// Get the current focussed app package name
func (d *Device) GetCurrentAppPackage() (string, error) {
data, err := d.RunCommand("shell dumpsys activity activities | grep ResumedActivity")
re := regexp.MustCompile(`\b([a-zA-Z0-9._]+)/(?:[a-zA-Z0-9._]+)\b`)
matches := re.FindStringSubmatch(string(data))
if len(matches) > 1 {
return matches[1], nil
} else {
return "", nil
}
return "", err
}
// Get the pid for a app
func (d *Device) GetAppPid(appackage string) (int, error) {
data, err := d.RunCommand(fmt.Sprintf("shell pidof %s"))
if err != nil {
return -1, err
}
pid, err := strconv.Atoi(strings.TrimSpace(string(data)))
if err != nil {
return -1, err
}
return pid, nil
}
// Check if a app is running
func (d *Device) CheckAppRunning(appackage string) (bool, error) {
pid, err := d.GetAppPid(appackage)
if err != nil {
return false, err
}
if pid != -1 {
return true, nil
}
return false, nil
}
// Start a app using the package name
func (d *Device) StartApp(appackage string) error {
_, err := d.RunCommand(fmt.Sprintf("shell monkey -p %s com.yourstoryinteractive.sails.pirate.adventure -c android.intent.category.LAUNCHER 1"))
return err
}
// Force kill a running app
func (d *Device) KillApp(appackage string) error {
_, err := d.RunCommand(fmt.Sprintf("shell am force-stop %s"))
return err
}