@@ -21,6 +21,11 @@ import (
2121 "github.com/mvt-project/androidqf/utils"
2222)
2323
24+ type deviceMenuItem struct {
25+ Serial string
26+ Status string
27+ }
28+
2429func init () {
2530 cfmt .Print (`
2631 {{ __ _ __ ____ }}::green
@@ -39,18 +44,52 @@ func systemPause() {
3944 os .Stdin .Read (make ([]byte , 1 ))
4045}
4146
42- func selectADBDevice (devices []string ) (string , error ) {
47+ func buildDeviceMenuItems (devices []string , running map [string ]runningExtraction ) []deviceMenuItem {
48+ items := make ([]deviceMenuItem , 0 , len (devices ))
49+ for _ , device := range devices {
50+ item := deviceMenuItem {Serial : device }
51+ if state , ok := running [device ]; ok {
52+ item .Status = fmt .Sprintf ("(extraction running, pid %d, started %s)" , state .PID , state .Started .Local ().Format ("2006-01-02 15:04:05" ))
53+ }
54+ items = append (items , item )
55+ }
56+ return items
57+ }
58+
59+ func selectADBDeviceFromMenu (items []deviceMenuItem ) (string , error ) {
4360 promptDevice := promptui.Select {
4461 Label : "Multiple Android devices detected. Select the device to acquire" ,
45- Items : devices ,
62+ Items : items ,
63+ Templates : & promptui.SelectTemplates {
64+ Active : "> {{ .Serial | cyan }} {{ .Status | yellow }}" ,
65+ Inactive : " {{ .Serial }} {{ .Status }}" ,
66+ Selected : "{{ .Serial }}" ,
67+ },
4668 }
4769
48- _ , device , err := promptDevice .Run ()
70+ index , _ , err := promptDevice .Run ()
4971 if err != nil {
5072 return "" , fmt .Errorf ("failed to select ADB device: %v" , err )
5173 }
5274
53- return device , nil
75+ return items [index ].Serial , nil
76+ }
77+
78+ func selectADBDevice (devices []string ) (string , error ) {
79+ return selectADBDeviceFromMenu (buildDeviceMenuItems (devices , activeRunningExtractionsBySerial ()))
80+ }
81+
82+ func resolveADBSerial (serial string , devices []string , selectDevice func ([]deviceMenuItem ) (string , error ), running map [string ]runningExtraction ) (string , bool , error ) {
83+ serial = strings .TrimSpace (serial )
84+ if serial != "" || len (devices ) == 0 {
85+ return serial , false , nil
86+ }
87+ if len (devices ) == 1 {
88+ return devices [0 ], false , nil
89+ }
90+
91+ selectedSerial , err := selectDevice (buildDeviceMenuItems (devices , running ))
92+ return selectedSerial , true , err
5493}
5594
5695func main () {
@@ -119,15 +158,16 @@ func main() {
119158 }
120159 }
121160 }
161+ specificDeviceRequested := serial != ""
122162
123163 // Initialization
124164 for {
125165 if serial == "" {
126166 devices , err := adb .Client .Devices ()
127167 if err != nil {
128168 log .Error (fmt .Sprintf ("Error listing ADB devices: %s" , err ))
129- } else if len ( devices ) > 1 {
130- serial , err = selectADBDevice ( devices )
169+ } else {
170+ serial , _ , err = resolveADBSerial ( serial , devices , selectADBDeviceFromMenu , activeRunningExtractionsBySerial () )
131171 if err != nil {
132172 log .Error (fmt .Sprintf ("Error selecting ADB device: %s" , err ))
133173 time .Sleep (5 * time .Second )
@@ -139,17 +179,35 @@ func main() {
139179 serial , err = adb .Client .SetSerial (serial )
140180 if err != nil {
141181 log .Error (fmt .Sprintf ("Error trying to connect over ADB: %s" , err ))
182+ if ! specificDeviceRequested {
183+ serial = ""
184+ }
142185 } else {
143186 _ , err = adb .Client .GetState ()
144187 if err == nil {
145188 break
146189 }
147190 log .Debug (err )
148191 log .Error ("Unable to get device state. Please make sure it is connected and authorized. Trying again in 5 seconds..." )
192+ if ! specificDeviceRequested {
193+ serial = ""
194+ }
149195 }
150196 time .Sleep (5 * time .Second )
151197 }
152198
199+ releaseRunning , err := registerRunningExtraction (adb .Client .Serial , "" )
200+ if err != nil {
201+ log .Warningf ("Unable to record running extraction state: %v" , err )
202+ releaseRunning = func () {}
203+ }
204+ runningReleased := false
205+ defer func () {
206+ if ! runningReleased {
207+ releaseRunning ()
208+ }
209+ }()
210+
153211 acq , err := acquisition .New (output_folder )
154212 if err != nil {
155213 log .Debug (err )
@@ -201,6 +259,8 @@ func main() {
201259 }
202260
203261 acq .Complete ()
262+ releaseRunning ()
263+ runningReleased = true
204264 log .Info ("Acquisition completed." )
205265
206266 systemPause ()
0 commit comments