tweaselORG / appstraction

An abstraction layer for common instrumentation functions (e.g. installing and starting apps, setting preferences, etc.) on Android and iOS.
MIT License
4 stars 1 forks source link

`getForegroundAppId()` wrongly returns `undefined` on Android #99

Closed baltpeter closed 1 year ago

baltpeter commented 1 year ago

I have an app open (and in the foreground) on Android. Nonetheless, getForegroundAppId() returns undefined.

baltpeter commented 1 year ago

The function checks:

❯ adb shell dumpsys activity recents | grep 'Recent #0'
  * Recent #0: Task{daae03c #324 type=standard A=10201:de.ivu.eticketinfo}

That does show an app ID.

baltpeter commented 1 year ago

The problem is with the regex:

https://github.com/tweaselORG/appstraction/blob/d57c2461e37ccde328e5ae42c391ba0454dd90c2/src/android.ts#L578

That expects an additional U= after the app ID, which this phone doesn't output.

baltpeter commented 1 year ago

The code was imported from my master's thesis, so we have no documentation on how it came to be (the linked StackOverflow answer doesn't help).

Anyway, let's assume that the app ID can be delimited at the end by either a space (when an additional parameter follows) or } (as I observed).

baltpeter commented 1 year ago

Android 13 emulator:

❯ adb shell dumpsys activity recents | grep 'Recent #0'
  * Recent #0: Task{851de4e #34 type=standard A=10159:com.wireguard.android}

Android 11 emulator:

❯ adb shell dumpsys activity recents | grep 'Recent #0'
  * Recent #0: Task{a1cc7b9 #10 visible=true type=standard mode=fullscreen translucent=false A=10090:com.google.android.documentsui U=0 StackId=10 sz=1}

So, that's where that came from. My fix works for both.