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

Fix: Update startGetLocation to only run if location is shared #1942

Merged
merged 7 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ object PreferenceOneSignalKeys {
*/
const val PREFS_OS_LAST_LOCATION_TIME = "OS_LAST_LOCATION_TIME"

/**
* (Boolean) Whether location should be shared with OneSignal.
*/
const val PREFS_OS_LOCATION_SHARED = "OS_LOCATION_SHARED"

// Permissions

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Build
import com.onesignal.common.AndroidUtils
import com.onesignal.common.threading.suspendifyOnThread
import com.onesignal.core.internal.application.IApplicationService
import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.core.internal.startup.IStartableService
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
Expand All @@ -22,13 +23,17 @@ internal class LocationManager(
private val _capturer: ILocationCapturer,
private val _locationController: ILocationController,
private val _locationPermissionController: LocationPermissionController,
private val _prefs: IPreferencesService,
) : ILocationManager, IStartableService, ILocationPermissionChangedHandler {
private var _isShared: Boolean = false
private var _isShared: Boolean = _prefs.getBool("OneSignal", "PREFS_OS_LOCATION_SHARED", false)!!
jennantilla marked this conversation as resolved.
Show resolved Hide resolved
override var isShared
get() = _isShared
set(value) {
Logging.debug("LocationManager.setIsShared(value: $value)")
_prefs.saveBool("OneSignal", "PREFS_OS_LOCATION_SHARED", value)
jennantilla marked this conversation as resolved.
Show resolved Hide resolved
_isShared = value

onLocationPermissionChanged(value)
}

override fun start() {
Expand Down Expand Up @@ -71,7 +76,7 @@ internal class LocationManager(
var result = false
withContext(Dispatchers.Main) {
if (!isShared) {
return@withContext false
Logging.error("Location permissions must be granted by setting isShared to true")
jennantilla marked this conversation as resolved.
Show resolved Hide resolved
}

val hasFinePermissionGranted =
Expand Down Expand Up @@ -179,13 +184,20 @@ internal class LocationManager(

// Started from this class or PermissionActivity
private suspend fun startGetLocation() {
if (!isShared) {
return
}

Logging.debug("LocationManager.startGetLocation()") // with lastLocation: " + lastLocation)
try {
if (!_locationController.start()) {
Logging.warn("LocationManager.startGetLocation: not possible, no location dependency found")
}
} catch (t: Throwable) {
Logging.warn("LocationManager.startGetLocation: Location permission exists but there was an error initializing: ", t)
Logging.warn(
"LocationManager.startGetLocation: Location permission exists but there was an error initializing: ",
t,
)
}
}
}
Loading