Skip to content

Commit

Permalink
Merge pull request #1942 from OneSignal/fix/location_sharing
Browse files Browse the repository at this point in the history
Fix: Update startGetLocation to only run if location is shared
  • Loading branch information
jennantilla authored and jinliu9508 committed Feb 6, 2024
2 parents 58d4bfa + f47c114 commit d5405c0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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,
)
}
}
}

0 comments on commit d5405c0

Please sign in to comment.