Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#8964 Fix app crash on incoming call when running Android 14+ #8989

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/8964.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix incoming call crash on Android 14+. ([#8964](https://github.com/element-hq/element-android/issues/8964))
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,16 @@ class VectorCallActivity :
== PackageManager.PERMISSION_GRANTED) {
// Only start the service if the app is in the foreground
if (isAppInForeground()) {
Timber.tag(loggerTag.value).v("Starting microphone foreground service")
val intent = Intent(this, MicrophoneAccessService::class.java)
ContextCompat.startForegroundService(this, intent)
// Starting in Android 14, you can't create a microphone foreground service while your app is in
// the background. If we call startForegroundService the app will crash.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but in this block the app is in the foreground (since isAppInForeground() returned true), or am I misreading something?

Copy link
Contributor Author

@christianrowlands christianrowlands Jan 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assumed that too, but when I added logging it indicated the app is in the foreground. I am guessing this is the case because by the time this code is reached the app has been brought to the foreground with an incoming call UI, but because this was not triggered by a user action, it does not matter if the app is in the foreground. Maybe my comment is not the clearest. The app not only needs to be in the foreground, but it must have gotten there via a user interaction (or another exemption path).

As for my comment about creating the microphone foreground service while your app is in the background, it depends on how the activity was triggered. A user interaction with the foreground such as clicking on a UI button or on a notification will allow the microphone to be accessed. However, if the chain is triggered entirely from the background then the exception will occur.

I believe this approach works because now the app does not use the microphone permission until the "answer" button is clicked, which is a user interaction with the UI.

See the documentation here: https://developer.android.com/develop/background-work/services/fgs/restrictions-bg-start#background-start-restriction-exemptions

And the relevant section:

The user performs an action on a UI element related to your app. For example, they might interact with a bubble, notification, widget, or activity.

// https://github.com/element-hq/element-android/issues/8964
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
Timber.tag(loggerTag.value).v("Starting microphone foreground service")
val intent = Intent(this, MicrophoneAccessService::class.java)
ContextCompat.startForegroundService(this, intent)
} else {
Timber.tag(loggerTag.value).v("App is in running Android 14+; cannot start microphone service")
}
} else {
Timber.tag(loggerTag.value).v("App is not in foreground; cannot start microphone service")
}
Expand Down
Loading