@@ -73,10 +73,6 @@ export function createAndroidObservationAdapter(
7373 } ) ;
7474}
7575
76- export function resetAndroidObservationMemoForTests ( ) : void {
77- everAnswered . clear ( ) ;
78- }
79-
8076function createDumpReader ( host : AndroidObservationHost , device : DeviceInfo ) : DumpReader {
8177 const dumps = new Map < string , Promise < string > > ( ) ;
8278 return ( args ) => {
@@ -134,11 +130,19 @@ function orderedDumps(device: DeviceInfo, question: Question): readonly (readonl
134130function recordSections ( device : DeviceInfo , key : string , text : string ) : void {
135131 if ( ! text ) return ;
136132 const memo = everAnswered . get ( device . id ) ?? new Map < string , boolean > ( ) ;
137- memo . set ( `foreground ${ key } ` , markerPattern ( FOCUS_MARKERS ) . test ( text ) ) ;
138- memo . set ( `blockingDialog ${ key } ` , markerPattern ( [ FOCUSED_WINDOW_MARKER ] ) . test ( text ) ) ;
133+ preservePositiveMemo ( memo , `foreground ${ key } ` , markerPattern ( FOCUS_MARKERS ) . test ( text ) ) ;
134+ preservePositiveMemo (
135+ memo ,
136+ `blockingDialog ${ key } ` ,
137+ markerPattern ( [ FOCUSED_WINDOW_MARKER ] ) . test ( text ) ,
138+ ) ;
139139 everAnswered . set ( device . id , memo ) ;
140140}
141141
142+ function preservePositiveMemo ( memo : Map < string , boolean > , key : string , answered : boolean ) : void {
143+ if ( memo . get ( key ) !== true ) memo . set ( key , answered ) ;
144+ }
145+
142146function parseForeground ( text : string ) : AppStateRuntimeResult | null {
143147 for ( const match of text . matchAll ( FOCUS_LINE ) ) {
144148 const component = match [ 1 ] ?. match (
@@ -154,8 +158,9 @@ function parseBlockingDialog(text: string): {
154158 focus : AndroidBlockingDialogFocus | null ;
155159} {
156160 let focusObserved = false ;
161+ const lines = text . split ( '\n' ) ;
157162 for ( const marker of FOCUS_MARKERS ) {
158- for ( const line of text . split ( '\n' ) ) {
163+ for ( const line of lines ) {
159164 const index = line . indexOf ( marker ) ;
160165 if ( index === - 1 ) continue ;
161166 if ( marker === FOCUSED_WINDOW_MARKER ) focusObserved = true ;
@@ -165,30 +170,32 @@ function parseBlockingDialog(text: string): {
165170 . slice ( index + marker . length )
166171 . split ( '}' ) [ 0 ]
167172 ?. trim ( ) ?? '' ;
168- const anr = ANR_TITLE . exec ( segment ) ;
169- if ( anr ?. [ 1 ] ) {
170- return {
171- focusObserved,
172- focus : { package : anr [ 1 ] , focusedWindow : `Application Not Responding: ${ anr [ 1 ] } ` , raw } ,
173- } ;
174- }
175- const responding = RESPONDING_TITLE . exec ( segment ) ?. [ 1 ] ?. trim ( ) . replace ( / \s + / g, ' ' ) ;
176- if ( responding ) {
177- const packageName = PACKAGE_NAME . exec ( responding ) ?. [ 1 ] ;
178- return {
179- focusObserved,
180- focus : {
181- ...( packageName ? { package : packageName } : { } ) ,
182- focusedWindow : responding ,
183- raw,
184- } ,
185- } ;
186- }
173+ const focus = parseDialogFocus ( segment , raw ) ;
174+ if ( focus ) return { focusObserved, focus } ;
187175 }
188176 }
189177 return { focusObserved, focus : null } ;
190178}
191179
180+ function parseDialogFocus ( segment : string , raw : string ) : AndroidBlockingDialogFocus | null {
181+ const anrPackage = ANR_TITLE . exec ( segment ) ?. [ 1 ] ;
182+ if ( anrPackage ) {
183+ return {
184+ package : anrPackage ,
185+ focusedWindow : `Application Not Responding: ${ anrPackage } ` ,
186+ raw,
187+ } ;
188+ }
189+ const responding = RESPONDING_TITLE . exec ( segment ) ?. [ 1 ] ?. trim ( ) . replace ( / \s + / g, ' ' ) ;
190+ if ( ! responding ) return null ;
191+ const packageName = PACKAGE_NAME . exec ( responding ) ?. [ 1 ] ;
192+ return {
193+ ...( packageName ? { package : packageName } : { } ) ,
194+ focusedWindow : responding ,
195+ raw,
196+ } ;
197+ }
198+
192199function markerPattern ( markers : readonly string [ ] ) : RegExp {
193200 return new RegExp ( markers . map ( escapeRegExp ) . join ( '|' ) ) ;
194201}
0 commit comments