diff --git a/main.go b/main.go index 9b9517b..f16972f 100644 --- a/main.go +++ b/main.go @@ -177,6 +177,7 @@ var commands = []*Command{ cmdUninstall, cmdDownload, cmdDevices, + cmdScan, cmdList, cmdRepo, cmdSetup, diff --git a/scan.go b/scan.go new file mode 100644 index 0000000..824f8e2 --- /dev/null +++ b/scan.go @@ -0,0 +1,66 @@ +// Copyright (c) 2024, Thomas Dickson +// See LICENSE for licensing information + +package main + +import ( + "fmt" + "strings" + + "mvdan.cc/fdroidcl/adb" +) + +var cmdScan = &Command{ + UsageLine: "scan", + Short: "Scan for all recognised apps on a connected device", +} + +func init() { + cmdScan.Run = runScan +} + +func runScan(args []string) error { + if err := startAdbIfNeeded(); err != nil { + return err + } + devices, err := adb.Devices() + if err != nil { + return fmt.Errorf("could not get devices: %v", err) + } + + if len(devices) == 0 { + return fmt.Errorf("no devices found") + } + + for _, device := range devices { + fmt.Printf("Scanning %s - %s (%s)\n", device.ID, device.Model, device.Product) + scanForPackages(device) + fmt.Println("Scan completed without error") + } + return nil +} + +func scanForPackages(device *adb.Device) { + cmd := device.AdbShell("pm list packages") + + out, err := cmd.Output() + if err != nil { + fmt.Println("could not run command: ", err) + } + // fmt.Println(string(out)) + // otherwise, print the output from running the command + lines := strings.Split(string(out), "\n") + // fmt.Println(lines) + + for _, line := range lines { + if len(line) > 8 { + line = line[8:] + // fmt.Println(line) + + apps, err := findApps([]string{line}) + if err == nil { + fmt.Println(apps[0].PackageName) + } + } + } +}