Skip to content

Commit

Permalink
Merge pull request #16 from XYOracleNetwork/feature/location-witness
Browse files Browse the repository at this point in the history
Feature/location witness
  • Loading branch information
jonesmac authored Nov 8, 2024
2 parents 1c00471 + 46b5ed0 commit ff57781
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0'
implementation 'com.squareup.moshi:moshi-kotlin:1.15.1'
implementation 'androidx.core:core-ktx:1.15.0'
implementation 'com.google.android.gms:play-services-location:21.3.0'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.madgag.spongycastle:prov:1.58.0.0'
implementation 'com.madgag.spongycastle:pkix:1.54.0.0'
Expand Down
2 changes: 2 additions & 0 deletions sdk/src/androidTest/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

</manifest>
2 changes: 2 additions & 0 deletions sdk/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" android:required="false"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package network.xyo.client.witness.location.info

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.location.Location
import android.util.Log
import androidx.core.content.ContextCompat
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.location.LocationServices
import com.squareup.moshi.JsonClass
import network.xyo.client.payload.XyoPayload
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

data class Coordinates(
val accuracy: Float?,
val altitude: Double?,
val altitudeAccuracy: Double?,
val heading: Float?,
val latitude: Double,
val longitude: Double,
val speed: Float?
)

data class CurrentLocation(
val coords: Coordinates,
val timestamp: Long
)



@JsonClass(generateAdapter = true)
class XyoLocationPayload (
currentLocation: CurrentLocation?
): XyoPayload() {
override var schema: String = "network.xyo.location.android"

companion object {

fun detect(context: Context): XyoLocationPayload? {
val gpInstance = GoogleApiAvailability.getInstance()
val available = gpInstance.isGooglePlayServicesAvailable(context)
val googlePlayServicesAvailable = available == 1
if (googlePlayServicesAvailable) {
Log.e("xyoClient", "Google Play Service not installed")
return null
}

if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.e("xyoClient", "ACCESS_FINE_LOCATION permission not allowed")
return null
}

if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
Log.e("xyoClient", "ACCESS_FINE_LOCATION permission not allowed")
return null
}

val fusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
var coordinates: Coordinates? = null

try {
val latch = CountDownLatch(1)

fusedLocationClient.lastLocation
.addOnSuccessListener { location: Location? ->
if (location != null) {
coordinates = Coordinates(
location.accuracy,
location.altitude,
null,
location.bearing,
location.latitude,
location.longitude,
location.speed
)
// 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 {
val currentLocation = CurrentLocation(coordinates!!, System.currentTimeMillis())
return XyoLocationPayload(currentLocation)
}
} catch (e: InterruptedException) {
e.printStackTrace()
}
return null

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package network.xyo.client.witness.location.info

import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import network.xyo.client.XyoWitness
import network.xyo.client.address.XyoAccount


@RequiresApi(Build.VERSION_CODES.M)
class XyoLocationWitness(address: XyoAccount = XyoAccount()) : XyoWitness<XyoLocationPayload>(
address,
fun (context: Context, _: String?): XyoLocationPayload? {
return XyoLocationPayload.detect(context)
})

0 comments on commit ff57781

Please sign in to comment.