Skip to content

Commit

Permalink
Add foreground service types for Android 14 target (#4561)
Browse files Browse the repository at this point in the history
* Add foreground service types for Android 14 target

 - Adds information about the foreground service type as this is required when targeting Android 14 and will otherwise throw exceptions: https://developer.android.com/about/versions/14/changes/fgs-types-required

* Update automotive full manifest
  • Loading branch information
jpelgrom authored Sep 6, 2024
1 parent 241d724 commit e0e4293
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 7 deletions.
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 @@ -21,6 +21,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 @@ -278,6 +280,11 @@
</intent-filter>
</service>

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

</application>

</manifest>

0 comments on commit e0e4293

Please sign in to comment.