Skip to content

Commit 7f6d1c8

Browse files
Add more permission checks for logging
Signed-off-by: Keith Conger <keith.conger@blackboxembedded.com>
1 parent 85c90c6 commit 7f6d1c8

3 files changed

Lines changed: 32 additions & 7 deletions

File tree

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ android {
66
applicationId "com.blackboxembedded.WunderLINQ"
77
minSdkVersion 26
88
targetSdkVersion 36
9-
versionCode 215
10-
versionName "2.99.6"
9+
versionCode 216
10+
versionName "2.99.7"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1212
vectorDrawables.useSupportLibrary = true
1313
Properties properties = new Properties()

app/src/main/java/com/blackboxembedded/WunderLINQ/LoggingService.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,30 @@ public void onCreate() {
146146
.setCategory(NotificationCompat.CATEGORY_SERVICE)
147147
.build();
148148

149-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
150-
startForeground(1234, notification, FOREGROUND_SERVICE_TYPE_LOCATION);
151-
} else {
152-
startForeground(1234, notification);
149+
try {
150+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
151+
// Starting with Android 14 (API 34), using FOREGROUND_SERVICE_TYPE_LOCATION
152+
// requires the FOREGROUND_SERVICE_LOCATION permission in the manifest
153+
// AND runtime location permission (fine or coarse).
154+
boolean hasLocationPermission = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
155+
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
156+
157+
if (hasLocationPermission) {
158+
startForeground(1234, notification, FOREGROUND_SERVICE_TYPE_LOCATION);
159+
} else {
160+
Log.e(TAG, "Location permission not granted; cannot start as location foreground service.");
161+
// On API 34+, calling startForeground without a type when one is declared in manifest
162+
// throws MissingForegroundServiceTypeException.
163+
// If we don't have permission, we can't use the type. Best to stop the service.
164+
stopSelf();
165+
return;
166+
}
167+
} else {
168+
startForeground(1234, notification);
169+
}
170+
} catch (Exception e) {
171+
Log.e(TAG, "Failed to start foreground service", e);
172+
stopSelf();
153173
}
154174

155175
((MyApplication) this.getApplication()).setTripRecording(true);

app/src/main/java/com/blackboxembedded/WunderLINQ/comms/BLE/BluetoothLeService.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,12 @@ public void onConnectionStateChange(BluetoothGatt gatt, int status,
625625

626626
// Check for auto-trip logging
627627
if (sharedPrefs.getBoolean("prefAutoTripLogging", false)) {
628-
LoggingService.startLoggingService(MyApplication.getContext());
628+
if (ActivityCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
629+
|| ActivityCompat.checkSelfPermission(MyApplication.getContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
630+
LoggingService.startLoggingService(MyApplication.getContext());
631+
} else {
632+
Log.w(TAG, "Auto-trip logging enabled but location permission not granted.");
633+
}
629634
}
630635

631636
String dataLog = "GATT Connected: [" + mBluetoothDeviceName + "|" + mBluetoothDeviceAddress + "] " +

0 commit comments

Comments
 (0)