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

Add foreground service types for Android 14 target #4561

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion app/src/full/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
<service
android:name=".location.HighAccuracyLocationService"
android:enabled="true"
android:exported="true"/>
android:exported="true"
android:foregroundServiceType="location"/>

<service
android:name=".matter.MatterCommissioningService"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
import android.graphics.Color
import android.location.Location
import android.os.Build
Expand Down Expand Up @@ -148,7 +149,8 @@ class HighAccuracyLocationService : Service() {
createNotificationBuilder(this)
notification = notificationBuilder.build()

LAUNCHER.onServiceCreated(this, notificationId, notification)
val type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) FOREGROUND_SERVICE_TYPE_LOCATION else 0
LAUNCHER.onServiceCreated(this, notificationId, notification, type)

Log.d(TAG, "High accuracy location service created -> onCreate")
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
Expand Down Expand Up @@ -893,6 +896,11 @@
android:enabled="true"
android:exported="true" />

<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync|remoteMessaging"
tools:node="merge" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) {
}

@Synchronized
fun onServiceCreated(service: Service, id: Int, notification: Notification) {
fun onServiceCreated(service: Service, id: Int, notification: Notification, foregroundServiceType: Int) {
// Make sure to call the startForeground method as fast as possible
service.startForeground(id, notification)
ServiceCompat.startForeground(service, id, notification, foregroundServiceType)
isStarting = false
isRunning = true
restartInProcess = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Build
import android.os.PowerManager
import android.util.Log
Expand Down Expand Up @@ -261,7 +262,12 @@ class WebsocketManager(
)
.build()
return try {
setForeground(ForegroundInfo(NOTIFICATION_ID, notification))
val type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_REMOTE_MESSAGING
} else {
0
}
setForeground(ForegroundInfo(NOTIFICATION_ID, notification, type))
true
} catch (e: IllegalStateException) {
if (e is CancellationException) return false
Expand Down
3 changes: 2 additions & 1 deletion automotive/src/full/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
<service
android:name=".location.HighAccuracyLocationService"
android:enabled="true"
android:exported="true"/>
android:exported="true"
android:foregroundServiceType="location"/>

<service
android:name=".matter.MatterCommissioningService"
Expand Down
8 changes: 8 additions & 0 deletions automotive/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_REMOTE_MESSAGING"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
Expand Down Expand Up @@ -892,6 +895,11 @@
android:enabled="true"
android:exported="true" />

<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync|remoteMessaging"
tools:node="merge" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.homeassistant.companion.android.common.sensors
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.pm.ServiceInfo
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
Expand Down Expand Up @@ -49,7 +50,15 @@ abstract class SensorWorkerBase(
.setPriority(NotificationCompat.PRIORITY_LOW)
.build()

val foregroundInfo = ForegroundInfo(NOTIFICATION_ID, notification)
val foregroundInfo = ForegroundInfo(
NOTIFICATION_ID,
notification,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
} else {
0
}
)
try {
setForeground(foregroundInfo)
Log.d(TAG, "Updating all Sensors in foreground.")
Expand Down
7 changes: 7 additions & 0 deletions wear/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>

<uses-feature android:name="android.hardware.type.watch" />
<uses-feature android:name="android.hardware.microphone" android:required="false" />
Expand Down Expand Up @@ -277,6 +279,11 @@
</intent-filter>
</service>

<service
android:name="androidx.work.impl.foreground.SystemForegroundService"
android:foregroundServiceType="dataSync"
tools:node="merge" />

</application>

</manifest>