diff --git a/README.md b/README.md index b63854d..b5edcf0 100644 --- a/README.md +++ b/README.md @@ -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( diff --git a/app/src/main/java/dev/arpan/location/engine/demo/MainActivity.kt b/app/src/main/java/dev/arpan/location/engine/demo/MainActivity.kt index f98dc7b..4d682ca 100644 --- a/app/src/main/java/dev/arpan/location/engine/demo/MainActivity.kt +++ b/app/src/main/java/dev/arpan/location/engine/demo/MainActivity.kt @@ -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 @@ -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 @@ -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) @@ -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 + ) + ) } } diff --git a/gradle.properties b/gradle.properties index 6488fc2..be46b63 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 diff --git a/location-engine/build.gradle b/location-engine/build.gradle index 3ed8f68..e559097 100644 --- a/location-engine/build.gradle +++ b/location-engine/build.gradle @@ -1,6 +1,7 @@ plugins { id 'com.android.library' id 'kotlin-android' + id 'kotlin-parcelize' id 'org.jetbrains.dokka' } diff --git a/location-engine/src/main/java/dev/arpan/location/engine/LocationFetcher.kt b/location-engine/src/main/java/dev/arpan/location/engine/LocationFetcher.kt index b05028a..fa90972 100644 --- a/location-engine/src/main/java/dev/arpan/location/engine/LocationFetcher.kt +++ b/location-engine/src/main/java/dev/arpan/location/engine/LocationFetcher.kt @@ -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) { diff --git a/location-engine/src/main/java/dev/arpan/location/engine/LocationRequest.kt b/location-engine/src/main/java/dev/arpan/location/engine/LocationRequest.kt new file mode 100644 index 0000000..948b8b6 --- /dev/null +++ b/location-engine/src/main/java/dev/arpan/location/engine/LocationRequest.kt @@ -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 + } +} diff --git a/location-engine/src/main/java/dev/arpan/location/engine/LocationSettingsActivity.kt b/location-engine/src/main/java/dev/arpan/location/engine/LocationSettingsActivity.kt index b47b6d2..1097fce 100644 --- a/location-engine/src/main/java/dev/arpan/location/engine/LocationSettingsActivity.kt +++ b/location-engine/src/main/java/dev/arpan/location/engine/LocationSettingsActivity.kt @@ -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 @@ -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 @@ -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?) { @@ -70,7 +81,9 @@ class LocationSettingsActivity : Activity() { permissions: Array, 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) { diff --git a/location-engine/src/main/java/dev/arpan/location/engine/impl/GoogleLocationProvider.kt b/location-engine/src/main/java/dev/arpan/location/engine/impl/GoogleLocationProvider.kt index 94069df..ee87ff8 100644 --- a/location-engine/src/main/java/dev/arpan/location/engine/impl/GoogleLocationProvider.kt +++ b/location-engine/src/main/java/dev/arpan/location/engine/impl/GoogleLocationProvider.kt @@ -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) diff --git a/location-engine/src/main/java/dev/arpan/location/engine/impl/HMSLocationProvider.kt b/location-engine/src/main/java/dev/arpan/location/engine/impl/HMSLocationProvider.kt index ead7ab0..39929c5 100644 --- a/location-engine/src/main/java/dev/arpan/location/engine/impl/HMSLocationProvider.kt +++ b/location-engine/src/main/java/dev/arpan/location/engine/impl/HMSLocationProvider.kt @@ -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)