SecurityException Crash Fix (v3.2.1)

Issue Summary

When the app process is killed during sleep tracking due to an OS-level WebView update or similar event, AsleepService is restarted via START_STICKY. At this point, if the RECORD_AUDIO permission has been revoked by the OS, calling startForeground(FOREGROUND_SERVICE_TYPE_MICROPHONE) throws a SecurityException, and a NetworkOnMainThreadException is subsequently thrown in the catch block, causing an app crash.


Reproduction Conditions

targetSdk 34+ implementation 'ai.asleep:asleepsdk:3.2.0'


Reproduction Steps

  1. Start beginSleepTracking in the app.
  2. Run the following adb command:
    adb shell pm revoke [app package name] android.permission.RECORD_AUDIO

▎ This reproduces the same behavior as an OS-level WebView update: runtime permission revoked → entire app process killed → service restarted via START_STICKY.

AsleepSDK 3.2.0 Behavior (crash occurs)

SecurityException thrown → NetworkOnMainThreadException thrown in catch block → app crashes after forced termination.

Operation not started: uid=11116 pkg=ai.asleep.asleep_sdk_android_test(null) op=RECORD_AUDIO
Operation not started: uid=11116 pkg=ai.asleep.asleep_sdk_android_test(null) op=START_FOREGROUND
...
Caused by: android.os.NetworkOnMainThreadException
...

AsleepSDK 3.2.1 Behavior (no crash)

SecurityException thrown → handled gracefully in catch block → service terminates without a crash.

Operation not started: uid=11116 pkg=ai.asleep.asleep_sdk_android_test(null) op=RECORD_AUDIO
Operation not started: uid=11116 pkg=ai.asleep.asleep_sdk_android_test(null) op=START_FOREGROUND