Repository: expo/expo
File: packages/expo-location/android/src/main/java/expo/modules/location/taskConsumers/LocationTaskConsumer.kt (Kotlin)

Code before:
    maybeStartForegroundService()
  }

  override fun didReceiveBroadcast(intent: Intent) {
    mTask ?: return
    val result = LocationResult.extractResult(intent)
    if (result != null) {
      val locations = result.locations
      // Foreground: report immediately for responsive UI (matches iOS behavior)
      // Background: use deferred buffer for battery optimization
      if (!mIsHostPaused) {
        reportLocationsImmediately(locations)
      } else {
        deferLocations(locations)
        maybeReportDeferredLocations()
      }
    } else {
      try {
        mLocationClient.lastLocation.addOnCompleteListener { task ->
          task.result?.let {
            if (!mIsHostPaused) {
              reportLocationsImmediately(listOf(it))
            } else {
              deferLocations(listOf(it))
              maybeReportDeferredLocations()
            }
          }
        }
      } catch (e: SecurityException) {
        Log.e(TAG, "Cannot get last location: " + e.message)
      }
    }
  }

  override fun didExecuteJob(jobService: JobService, params: JobParameters): Boolean {
    val data = taskManagerUtils.extractDataFromJobParams(params)
    val locationBundles = ArrayList<Bundle>()
    for (persistableLocationBundle in data) {
      val locationBundle = Bundle()
      val coordsBundle = Bundle()
      if (persistableLocationBundle != null) {
        coordsBundle.putAll(persistableLocationBundle.getPersistableBundle("coords"))
        locationBundle.putAll(persistableLocationBundle)
        locationBundle.putBundle("coords", coordsBundle)
        locationBundles.add(locationBundle)
      }
    }
    executeTaskWithLocationBundles(locationBundles) { jobService.jobFinished(params, false) }

    // Returning `true` indicates that the job is still running, but in async mode.
    // In that case we're obligated to call `jobService.jobFinished` as soon as the async block finishes.

Review comment (suggestion):
Could we change this logic so it is a bit clearer? Maybe let the method `maybeReportDeferredLocations` take the locations as its parameter - and let it decide if we're in the foreground or background? As it is now it looks like this code is a bit redundant.

Code after:
    // Restart foreground service if its option has changed.
    maybeStartForegroundService()
  }

  override fun didReceiveBroadcast(intent: Intent) {
    mTask ?: return
    val result = LocationResult.extractResult(intent)
    if (result != null) {
      handleLocationUpdate(result.locations)
    } else {
      try {
        mLocationClient.lastLocation.addOnCompleteListener { task ->
          task.result?.let { handleLocationUpdate(listOf(it)) }
        }
      } catch (e: SecurityException) {
        Log.e(TAG, "Cannot get last location: " + e.message)
      }
    }
  }

  /**
   * Handles incoming location updates by either reporting immediately (foreground)
   * or deferring for battery optimization (background).
   */
  private fun handleLocationUpdate(locations: List<Location>) {
    if (locations.isEmpty()) return

    // Foreground: report immediately for responsive UI (matches iOS behavior)
    if (!mIsHostPaused) {
      reportLocationsImmediately(locations)
      return
    }

    // Background: use deferred buffer for battery optimization
    deferLocations(locations)
    maybeReportDeferredLocations()
  }

  override fun didExecuteJob(jobService: JobService, params: JobParameters): Boolean {
    val data = taskManagerUtils.extractDataFromJobParams(params)
    val locationBundles = ArrayList<Bundle>()
    for (persistableLocationBundle in data) {
      val locationBundle = Bundle()
      val coordsBundle = Bundle()
      if (persistableLocationBundle != null) {
        coordsBundle.putAll(persistableLocationBundle.getPersistableBundle("coords"))
        locationBundle.putAll(persistableLocationBundle)
        locationBundle.putBundle("coords", coordsBundle)
        locationBundles.add(locationBundle)
      }
    }