Notify on foreground service start failure

This commit is contained in:
Sylvia van Os committed 2026-08-11 22:06:01 +02:00
1 parent cda842485f
commit bfa79a6f68
8 files changed
+89 -2

No files matched your search

@@ -5,5 +5,7 @@ object NotificationInfo {
const val NOTIFICATION_ID = 1001
const val CHANNEL_ID = "catima_wear_bt"
const val PAIRING_CHANNEL_ID = "catima_wear_bt_pairing"
const val CRITICAL_ERROR_NOTIFICATION_ID = 1002
const val CRITICAL_ERROR_CHANNEL_ID = "catima_wear_bt_critical_error"
}
}
@@ -1,13 +1,23 @@
package protect.card_locker.wearos
import android.Manifest
import android.app.ForegroundServiceStartNotAllowedException
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import androidx.core.content.ContextCompat
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import androidx.activity.result.ActivityResultCaller
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.core.content.edit
import androidx.preference.PreferenceManager
import protect.card_locker.NotificationInfo
import protect.card_locker.R
import protect.card_locker.preferences.Settings
import protect.card_locker.shared.BluetoothPermissionHelper
@@ -55,6 +65,8 @@ class WearSyncPermissionRequester(
}
object WearSyncServiceManager {
private const val TAG = "CatimaWearSyncServiceManager"
fun synchronize(context: Context, requestPermission: (() -> Unit)? = null) {
if (!Settings(context).wearSyncEnabled) {
stop(context)
@@ -105,7 +117,48 @@ object WearSyncServiceManager {
}
private fun start(context: Context) {
ContextCompat.startForegroundService(context, Intent(context, BluetoothServerService::class.java))
// Exception can only throw on API level 31 and up
if (Build.VERSION.SDK_INT >= 31) {
try {
ContextCompat.startForegroundService(
context,
Intent(context, BluetoothServerService::class.java)
)
} catch (e: ForegroundServiceStartNotAllowedException) {
// We naively assume we have enough permissions to show a notification as the service should never try to start
// before the user gave notification access anyway
Log.d(TAG, "Foreground service failed to launch: $e")
val channel = NotificationChannel(
NotificationInfo.WearBluetooth.CRITICAL_ERROR_CHANNEL_ID,
context.getString(R.string.wear_bt_critical_error_channel_name),
NotificationManager.IMPORTANCE_HIGH
).apply { setShowBadge(false) }
context.getSystemService(NotificationManager::class.java).createNotificationChannel(channel)
val notification = NotificationCompat.Builder(context, NotificationInfo.WearBluetooth.CRITICAL_ERROR_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_error)
.setContentTitle(context.getString(R.string.wear_bt_failed_foreground_notification_title))
.setContentText(e.message)
.build()
with(NotificationManagerCompat.from(context)) {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.POST_NOTIFICATIONS
) != PackageManager.PERMISSION_GRANTED
) {
return@with
}
notify(
NotificationInfo.WearBluetooth.CRITICAL_ERROR_NOTIFICATION_ID,
notification
)
}
}
} else {
ContextCompat.startForegroundService(
context,
Intent(context, BluetoothServerService::class.java)
)
}
}
private fun stop(context: Context) {
@@ -0,0 +1,30 @@
<!--
~ Copyright (C) 2026 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="960"
android:viewportHeight="960"
android:tint="#FFFFFF">
<group android:scaleX="1.104"
android:scaleY="1.104"
android:translateX="-49.92"
android:translateY="-49.92">
<path
android:fillColor="@android:color/white"
android:pathData="M508.5,668.5Q520,657 520,640Q520,623 508.5,611.5Q497,600 480,600Q463,600 451.5,611.5Q440,623 440,640Q440,657 451.5,668.5Q463,680 480,680Q497,680 508.5,668.5ZM440,520L520,520L520,280L440,280L440,520ZM480,880Q397,880 324,848.5Q251,817 197,763Q143,709 111.5,636Q80,563 80,480Q80,397 111.5,324Q143,251 197,197Q251,143 324,111.5Q397,80 480,80Q563,80 636,111.5Q709,143 763,197Q817,251 848.5,324Q880,397 880,480Q880,563 848.5,636Q817,709 763,763Q709,817 636,848.5Q563,880 480,880ZM480,800Q614,800 707,707Q800,614 800,480Q800,346 707,253Q614,160 480,160Q346,160 253,253Q160,346 160,480Q160,614 253,707Q346,800 480,800ZM480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Q480,480 480,480Z"/>
</group>
</vector>
Binary file not shown.

After

Width:  |  Height:  |  Size: 752 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 478 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 919 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

+2
View File
@@ -393,4 +393,6 @@
<string name="wear_bt_channel_name">Wear OS companion</string>
<string name="wear_bt_notification_title">Catima Wear OS sync active</string>
<string name="settings_category_title_smartwatch">Smartwatch support</string>
<string name="wear_bt_failed_foreground_notification_title">Failed to start Wear OS sync service</string>
<string name="wear_bt_critical_error_channel_name">Wear OS sync critical error</string>
</resources>