Skip to content

Commit

Permalink
add LocationRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
Bloody-Badboy committed Jul 11, 2021
1 parent 114fb86 commit 5d1e560
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 26 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ A library to request location update for both GMS and HMS with less boilerplate
---

# Usages
Create `LocationRequest` object

Initilize `LocationFetcher` in your activity or fragment
Initialize `LocationFetcher` in your activity or fragment
```kotlin
private val fetcher: LocationFetcher by lazy {
LocationFetcher(
Expand Down
19 changes: 15 additions & 4 deletions app/src/main/java/dev/arpan/location/engine/demo/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.arpan.location.engine.demo

import android.content.Intent
import android.location.Address
import android.location.Geocoder
import android.os.Bundle
Expand All @@ -11,6 +10,7 @@ import androidx.databinding.DataBindingUtil
import androidx.lifecycle.lifecycleScope
import dev.arpan.location.engine.LocationFetcher
import dev.arpan.location.engine.LocationReceiver
import dev.arpan.location.engine.LocationRequest
import dev.arpan.location.engine.LocationSettingsActivity
import dev.arpan.location.engine.demo.databinding.ActivityMainBinding
import dev.arpan.location.engine.model.Location
Expand Down Expand Up @@ -44,11 +44,17 @@ class MainActivity : AppCompatActivity() {
}
}

private val locationRequest = LocationRequest().apply {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
interval = 20000L
fastestInterval = 10000L
}

private val fetcher: LocationFetcher by lazy {
LocationFetcher(
context = this,
updateInterval = 4000L,
locationReceiver = object : LocationReceiver {
request = locationRequest,
receiver = object : LocationReceiver {
override fun onReceived(location: Location) {
binding.tvLocation.text = location.toString()
getAddress(location)
Expand All @@ -65,7 +71,12 @@ class MainActivity : AppCompatActivity() {
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)

binding.button.setOnClickListener {
activityResultLauncher.launch(Intent(this, LocationSettingsActivity::class.java))
activityResultLauncher.launch(
LocationSettingsActivity.buildIntent(
this,
locationRequest
)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ systemProp.org.gradle.internal.http.socketTimeout=120000
GROUP=dev.arpan
POM_PACKAGING=aar

VERSION_NAME=1.0.0-SNAPSHOT
VERSION_NAME=1.0.1-SNAPSHOT

POM_ARTIFACT_ID=location-engine
POM_NAME=location-engine
Expand Down
1 change: 1 addition & 0 deletions location-engine/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-parcelize'
id 'org.jetbrains.dokka'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ import dev.arpan.location.engine.util.ApiAvailabilityUtil

class LocationFetcher(
context: Context,
updateInterval: Long = 10000L,
locationReceiver: LocationReceiver? = null
request: LocationRequest,
receiver: LocationReceiver? = null
) : ILocationFetcher {

private val locationFetcher: ILocationFetcher =
if (ApiAvailabilityUtil.isGmsAvailable(context)) {
GoogleLocationProvider(context, updateInterval, locationReceiver)
GoogleLocationProvider(
context,
request.toGoogleLocationRequest(),
receiver
)
} else {
HMSLocationProvider(context, updateInterval, locationReceiver)
HMSLocationProvider(
context,
request.toHMSLocationRequest(),
receiver
)
}

override fun checkDeviceLocationSettings(callback: LocationSettingCallback) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package dev.arpan.location.engine

import android.os.Parcelable
import kotlinx.parcelize.Parcelize
import com.google.android.gms.location.LocationRequest as GoogleLocationRequest
import com.huawei.hms.location.LocationRequest as HMSLocationRequest

@Parcelize
data class LocationRequest(
var priority: Int = PRIORITY_HIGH_ACCURACY,
var interval: Long = 10000L,
var fastestInterval: Long = (interval / 6.0).toLong(),
var smallestDisplacement: Float = 0.0f
) : Parcelable {
companion object {
const val PRIORITY_HIGH_ACCURACY = 100
const val PRIORITY_BALANCED_POWER_ACCURACY = 102
const val PRIORITY_LOW_POWER = 104
const val PRIORITY_NO_POWER = 105
}

fun toHMSLocationRequest(): HMSLocationRequest {
val request = HMSLocationRequest()
request.priority = priority
request.interval = interval
request.fastestInterval = fastestInterval
request.smallestDisplacement = smallestDisplacement
return request
}

fun toGoogleLocationRequest(): GoogleLocationRequest {
val request = GoogleLocationRequest.create()
request.priority = priority
request.interval = interval
request.fastestInterval = fastestInterval
request.smallestDisplacement = smallestDisplacement
return request
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dev.arpan.location.engine

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
Expand All @@ -12,6 +13,13 @@ import timber.log.Timber

class LocationSettingsActivity : Activity() {
companion object {
private const val EXTRA_LOCATION_REQUEST = "location_request"

fun buildIntent(context: Context, request: LocationRequest) =
Intent(context, LocationSettingsActivity::class.java).apply {
putExtra(EXTRA_LOCATION_REQUEST, request)
}

private const val REQUEST_CODE_LOCATION_PERMISSION = 100
private const val REQUEST_CODE_LOCATION_SETTINGS = 101

Expand All @@ -21,7 +29,10 @@ class LocationSettingsActivity : Activity() {
}

private val fetcher: LocationFetcher by lazy(LazyThreadSafetyMode.NONE) {
LocationFetcher(this)
LocationFetcher(
this,
checkNotNull(intent.getParcelableExtra(EXTRA_LOCATION_REQUEST))
)
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -70,7 +81,9 @@ class LocationSettingsActivity : Activity() {
permissions: Array<out String>,
grantResults: IntArray
) {
if (requestCode == REQUEST_CODE_LOCATION_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (requestCode == REQUEST_CODE_LOCATION_PERMISSION &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
checkDeviceLocationSettings()
} else {
if (isLocationPermissionPermanentlyDenied) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,10 @@ import timber.log.Timber

internal class GoogleLocationProvider(
private val context: Context,
private val updateInterval: Long,
private val locationRequest: LocationRequest,
private val locationReceiver: LocationReceiver?
) : ILocationFetcher {

private val locationRequest = LocationRequest.create().apply {
interval = updateInterval
fastestInterval = updateInterval / 2
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}

private val locationSettingsRequest = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
.setAlwaysShow(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,10 @@ import timber.log.Timber

internal class HMSLocationProvider(
private val context: Context,
private val updateInterval: Long,
private val locationRequest: LocationRequest,
private val locationReceiver: LocationReceiver?
) : ILocationFetcher {

private val locationRequest = LocationRequest().apply {
interval = updateInterval
fastestInterval = updateInterval / 2
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}

private val locationSettingsRequest = LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest)
.setAlwaysShow(true)
Expand Down

0 comments on commit 5d1e560

Please sign in to comment.