-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from XYOracleNetwork/feature/location-activity
Feature/location activity
- Loading branch information
Showing
7 changed files
with
187 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
sdk/src/main/java/network/xyo/client/witness/location/info/LocationActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package network.xyo.client.witness.location.info | ||
|
||
import android.annotation.SuppressLint | ||
import android.app.Activity | ||
import android.os.Bundle | ||
import android.os.Looper | ||
import android.util.Log | ||
import com.google.android.gms.location.LocationCallback | ||
import com.google.android.gms.location.LocationRequest | ||
import com.google.android.gms.location.LocationResult | ||
import com.google.android.gms.location.LocationServices | ||
|
||
const val REQUESTING_LOCATION_UPDATES_KEY = "REQUESTING_LOCATION_UPDATES_KEY" | ||
|
||
class LocationActivity : Activity() { | ||
private var requestingLocationUpdates: Boolean = false | ||
private lateinit var locationCallback: LocationCallback | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
requestingLocationUpdates = true | ||
locationCallback = object : LocationCallback() { | ||
override fun onLocationResult(locationResult: LocationResult) { | ||
// No action necessary but we need to start the activity to get location results | ||
Log.i("xyoClient", "LocationActivity: locationCallback fired successfully") | ||
} | ||
} | ||
startLocationUpdates() | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
if (requestingLocationUpdates) startLocationUpdates() | ||
} | ||
|
||
override fun onSaveInstanceState(outState: Bundle) { | ||
outState.putBoolean(REQUESTING_LOCATION_UPDATES_KEY, requestingLocationUpdates) | ||
super.onSaveInstanceState(outState) | ||
} | ||
|
||
// Already checking in class | ||
@SuppressLint("MissingPermission") | ||
private fun startLocationUpdates() { | ||
if (LocationPermissions.check((this))) { | ||
val locationRequest: LocationRequest = LocationRequest.Builder(5000).build() | ||
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) | ||
fusedLocationClient.requestLocationUpdates(locationRequest, | ||
locationCallback, | ||
Looper.getMainLooper()) | ||
return | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
sdk/src/main/java/network/xyo/client/witness/location/info/PermissionCheck.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package network.xyo.client.witness.location.info | ||
|
||
import android.Manifest | ||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import android.util.Log | ||
import androidx.core.app.ActivityCompat | ||
import com.google.android.gms.common.GoogleApiAvailability | ||
|
||
class LocationPermissions { | ||
companion object { | ||
fun check(context: Context): Boolean { | ||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
Log.e("xyoClient", "ACCESS_FINE_LOCATION permission not allowed") | ||
return false | ||
} | ||
|
||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
Log.e("xyoClient", "ACCESS_FINE_LOCATION permission not allowed") | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
fun checkGooglePlayServices(context: Context): Boolean { | ||
val gpInstance = GoogleApiAvailability.getInstance() | ||
val available = gpInstance.isGooglePlayServicesAvailable(context) | ||
val googlePlayServicesAvailable = available == 1 | ||
if (googlePlayServicesAvailable) { | ||
Log.e("xyoClient", "Google Play Service not installed") | ||
return false | ||
} | ||
return true | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
sdk/src/main/java/network/xyo/client/witness/location/info/XyoLocationCurrent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package network.xyo.client.witness.location.info | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.location.Location | ||
import android.util.Log | ||
import com.google.android.gms.location.LocationServices | ||
import com.squareup.moshi.JsonClass | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.TimeUnit | ||
|
||
@JsonClass(generateAdapter = true) | ||
class XyoLocationCurrent { | ||
companion object { | ||
|
||
@SuppressLint("MissingPermission") | ||
fun detect(context: Context): CurrentLocation? { | ||
if (LocationPermissions.check((context)) && LocationPermissions.checkGooglePlayServices(context)) { | ||
val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context) | ||
var coordinates: Coordinates? = null | ||
|
||
try { | ||
val latch = CountDownLatch(1) | ||
|
||
fusedLocationClient.lastLocation | ||
.addOnSuccessListener { location: Location? -> | ||
if (location != null) { | ||
Log.w("xyoClient", "Location was found") | ||
coordinates = setCoordinatesFromLocation(location) | ||
// countDown to zero to lift the latch | ||
latch.countDown() | ||
} else { | ||
// countDown to zero to lift the latch | ||
latch.countDown() | ||
Log.e("xyoClient","Location not available") | ||
} | ||
} | ||
.addOnFailureListener { | ||
Log.e("xyoClient","Failed to get location: ${it.message}") | ||
} | ||
// Wait for up to 5 seconds for the location | ||
latch.await(5, TimeUnit.SECONDS) | ||
|
||
if (coordinates == null) { | ||
return null | ||
} else { | ||
return CurrentLocation(coordinates!!, System.currentTimeMillis()) | ||
} | ||
} catch (e: InterruptedException) { | ||
e.printStackTrace() | ||
} | ||
} | ||
return null | ||
} | ||
|
||
private fun setCoordinatesFromLocation(location: Location): Coordinates { | ||
return Coordinates( | ||
location.accuracy, | ||
location.altitude, | ||
null, | ||
location.bearing, | ||
location.latitude, | ||
location.longitude, | ||
location.speed | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters