Skip to content

GeniusRUS/CGPS

Repository files navigation

CGPS

Maven Central CircleCI codebeat badge

Short description

Simple obtaining of the current or the last location with the Kotlin Coroutines

Details

By default, there are 5-seconds timeout for operation

Also, it is checked whether the GPS adapter is enabled on the device and whether there are valid data to receive

If one of the conditions is not valid, then the corresponding exception is thrown:

  • LocationDisabledException - if GPS-adapter is disabled
  • SecurityException - if permissions (API >= 23) is not granted (ACCESS_COARSE_LOCATION for CGPS::lastLocation, and ACCESS_FINE_LOCATION for otherwise)
  • TimeoutException - if location is not received in time (by default 5000ms)
  • LocationException - others exceptions
  • ServicesAvailabilityException - if Google or Huawei Services not available on device
  • ResolutionNeedException (for GoogleCGPS/HuaweiCGPS only) - if user permission is requested to enable GPS using the system dialog

To query the current location, the accuracy of the determination (Accuracy.*) and the maximum timeout of the operation

For Huawei variant:

The library requires the agconnect-services.json file in the project, without it a LocationException error will be thrown

In the lib are three version:

HardwareCGPS - not required Google or Huawei Services. Uses only Android Location Manager

More battery friendly variants:

  • GoogleCGPS - uses Google Services
  • HuaweiCGPS - for HMS devices

Usage

  • Get last location
val location: Location = HardwareCGPS(context).lastLocation()
  • Get actual location
val location: Location = HardwareCGPS(context).actualLocation()
  • Get location updates
val flowLocation: Flow<Result<Location>> = HardwareCGPS(context).requestUpdates()
  • Get actual location with enable GPS request (Google/Huawei implementation only)
val location: Location = GoogleCGPS(context).actualLocationWithEnable()

NOTE: Use ActivityResultApi with StartIntentSenderForResult contract to handle ResolutionNeedException.intentSender

Install

Artifact is publishing to Maven Central. You can add this repository to your project with:

repositories {
    mavenCentral()
}

Add to your .gradle file:

  • Core module (with hardware implementation)
implementation "io.github.geniusrus:cgps-core:$last_version"
  • Google module (include Core)
implementation "io.github.geniusrus:cgps-google:$last_version"
  • Huawei module (include Core)
implementation "io.github.geniusrus:cgps-huawei:$last_version"

Legacy

Old artifact io.github.geniusrus:cgps is still available at 2.1.0 version. But it will no longer be supported

Sample

The sample is on app module

Developed by

License

Apache v2.0 License

Copyright (c) 2018 Viktor Likhanov

About

Android location library on coroutines

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages