diff --git a/dp3t-sdk/sdk/src/androidTest/java/org/dpppt/android/sdk/internal/nearby/LocationLessScanningTest.java b/dp3t-sdk/sdk/src/androidTest/java/org/dpppt/android/sdk/internal/nearby/LocationLessScanningTest.java new file mode 100644 index 00000000..a548c2c7 --- /dev/null +++ b/dp3t-sdk/sdk/src/androidTest/java/org/dpppt/android/sdk/internal/nearby/LocationLessScanningTest.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 Ubique Innovation AG + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + * + * SPDX-License-Identifier: MPL-2.0 + */ + +package org.dpppt.android.sdk.internal.nearby; + +import android.content.Context; +import android.os.Build; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; + +import org.dpppt.android.sdk.internal.ErrorHelper; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +@RunWith(AndroidJUnit4.class) +public class LocationLessScanningTest { + + private Context context; + + @Before + public void setup() { + context = InstrumentationRegistry.getInstrumentation().getContext(); + } + + @Test + public void checkLocationLessScanningOnAndroidR(){ + assertEquals(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R, ErrorHelper.deviceSupportsLocationlessScanning(context)); + } +} diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java index 573a04c7..f8b34db5 100644 --- a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/DP3T.java @@ -15,7 +15,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.location.LocationManager; -import android.os.Build; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.core.util.Consumer; @@ -100,7 +99,7 @@ private static void executeInit(Context context, AppConfigManager appConfigManag new BluetoothStateBroadcastReceiver(), new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED) ); - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) { + if (!ErrorHelper.deviceSupportsLocationlessScanning(context)) { context.registerReceiver( new LocationServiceBroadcastReceiver(), new IntentFilter(LocationManager.MODE_CHANGED_ACTION) diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/ErrorHelper.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/ErrorHelper.java index 40838d92..4aa2a3c8 100644 --- a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/ErrorHelper.java +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/ErrorHelper.java @@ -12,8 +12,8 @@ import android.bluetooth.BluetoothAdapter; import android.content.Context; import android.content.pm.PackageManager; -import android.os.Build; import android.os.PowerManager; +import android.provider.Settings; import android.system.ErrnoException; import android.system.OsConstants; @@ -22,7 +22,6 @@ import java.util.Collection; import java.util.HashSet; import java.util.Set; - import javax.net.ssl.SSLException; import com.google.android.gms.common.api.ApiException; @@ -63,7 +62,7 @@ public static Collection checkTracingErrorStatus(Context context, Ap } } - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R && !LocationServiceUtil.isLocationEnabled(context)) { + if (!deviceSupportsLocationlessScanning(context) && !LocationServiceUtil.isLocationEnabled(context)) { errors.add(ErrorState.LOCATION_SERVICE_DISABLED); } @@ -106,6 +105,11 @@ public static Collection checkTracingErrorStatus(Context context, Ap return errors; } + public static boolean deviceSupportsLocationlessScanning(Context context) { + return Settings.Global.getInt(context.getApplicationContext().getContentResolver(), + "bluetooth_sanitized_exposure_notification_supported", 0) == 1; + } + public static Collection getDelayableSyncErrors() { return new HashSet<>(DELAYABLE_SYNC_ERRORS); } diff --git a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/util/LocationServiceUtil.java b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/util/LocationServiceUtil.java index f488c38a..3a339ac6 100644 --- a/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/util/LocationServiceUtil.java +++ b/dp3t-sdk/sdk/src/main/java/org/dpppt/android/sdk/internal/util/LocationServiceUtil.java @@ -7,7 +7,6 @@ * * SPDX-License-Identifier: MPL-2.0 */ - package org.dpppt.android.sdk.internal.util; import android.content.Context; @@ -17,7 +16,7 @@ public class LocationServiceUtil { - public static Boolean isLocationEnabled(Context context) { + public static boolean isLocationEnabled(Context context) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); return lm != null && lm.isLocationEnabled();