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 all 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,9 @@ 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.preferences.PreferenceOneSignalKeys
import com.onesignal.core.internal.preferences.PreferenceStores
import com.onesignal.core.internal.startup.IStartableService
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
Expand All @@ -22,13 +25,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(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.PREFS_OS_LOCATION_SHARED, false)!!
override var isShared
get() = _isShared
set(value) {
Logging.debug("LocationManager.setIsShared(value: $value)")
_prefs.saveBool(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.PREFS_OS_LOCATION_SHARED, value)
_isShared = value

onLocationPermissionChanged(value)
}

override fun start() {
Expand Down Expand Up @@ -71,7 +78,7 @@ internal class LocationManager(
var result = false
withContext(Dispatchers.Main) {
if (!isShared) {
return@withContext false
Logging.warn("Requesting location permission, but location sharing must also be enabled by setting isShared to true")
}

val hasFinePermissionGranted =
Expand Down Expand Up @@ -179,13 +186,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