Set geofenc according to users location values
This commit is contained in:
parent
2a1eb3feef
commit
dad0594854
@ -10,6 +10,7 @@ import android.os.Looper
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.Spinner
|
||||
import android.widget.TextView
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.ActivityCompat.requestPermissions
|
||||
@ -18,6 +19,7 @@ import de.hft.geotracker.GeofenceBroadcastReceiver
|
||||
import de.hft.geotracker.R
|
||||
import de.hft.geotracker.retrofit.AuthenticationInterceptor
|
||||
import de.hft.geotracker.retrofit.GeofenceService
|
||||
import de.hft.geotracker.retrofit.ValuesTimetrackAccounts
|
||||
import de.hft.geotracker.retrofit.ValuesUser
|
||||
import kotlinx.android.synthetic.main.activity_home.*
|
||||
import okhttp3.OkHttpClient
|
||||
@ -80,22 +82,6 @@ class MainActivity : AppCompatActivity() {
|
||||
button_start_stop.isEnabled = btnState
|
||||
}
|
||||
|
||||
//Setup geofence
|
||||
geofencingClient = LocationServices.getGeofencingClient(this)
|
||||
geofence = Geofence.Builder().setRequestId("Test")
|
||||
.setCircularRegion(48.3575, 8.9745, 50F)
|
||||
.setExpirationDuration(Geofence.NEVER_EXPIRE)
|
||||
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
|
||||
.build()
|
||||
geofencingClient.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
|
||||
addOnSuccessListener {
|
||||
println("Geofence added")
|
||||
}
|
||||
addOnFailureListener {
|
||||
println("Error: " + it.stackTrace.forEach { println(it.toString()) })
|
||||
}
|
||||
}
|
||||
|
||||
//JWToken lesen
|
||||
val fis = openFileInput("JWToken")
|
||||
val isr = InputStreamReader(fis)
|
||||
@ -119,6 +105,28 @@ class MainActivity : AppCompatActivity() {
|
||||
service = retrofit.create(GeofenceService::class.java)
|
||||
showUsername()
|
||||
|
||||
//Setup geofence
|
||||
|
||||
|
||||
//Get Timetrack Accounts
|
||||
/*val call = service.getAccounts()
|
||||
call.enqueue(object: Callback<List<ValuesTimetrackAccounts>> {
|
||||
override fun onResponse(
|
||||
call: Call<List<ValuesTimetrackAccounts>>,
|
||||
response: Response<List<ValuesTimetrackAccounts>>
|
||||
) {
|
||||
if (response.isSuccessful) {
|
||||
val responseBody = response.body()!!
|
||||
responseBody.forEach {
|
||||
println(it.name)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
override fun onFailure(call: Call<List<ValuesTimetrackAccounts>>, t: Throwable) {
|
||||
println("Get Timetrack Accounts ended with errors. " + t.message)
|
||||
}
|
||||
})*/
|
||||
val spinner: Spinner = findViewById(R.id.account_spinner)
|
||||
// Create an ArrayAdapter using the string array and a default spinner layout
|
||||
ArrayAdapter.createFromResource(
|
||||
@ -130,6 +138,7 @@ class MainActivity : AppCompatActivity() {
|
||||
// Apply the adapter to the spinner
|
||||
spinner.adapter = adapter
|
||||
}
|
||||
|
||||
actionButton = findViewById(R.id.button_start_stop)
|
||||
actionButton.setOnClickListener {
|
||||
callStartStop()
|
||||
@ -165,18 +174,41 @@ class MainActivity : AppCompatActivity() {
|
||||
override fun onResponse(call: Call<ValuesUser>, response: Response<ValuesUser>) {
|
||||
if (response.isSuccessful) {
|
||||
val firstname = response.body()?.firstname
|
||||
val location = response.body()?.location
|
||||
lbl_username.text = "Hello " + firstname
|
||||
println("Body: " + firstname)
|
||||
if (location?.latitude == null) {
|
||||
Toast.makeText(this@MainActivity, "No geofence set for you", Toast.LENGTH_LONG)
|
||||
.show()
|
||||
} else {
|
||||
initializeGeofence(location?.latitude, location?.longitude, location?.radius)
|
||||
}
|
||||
} else {
|
||||
println("Response not successful: ${response.code()}")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onFailure(call: Call<ValuesUser>, t: Throwable) {
|
||||
println("Response 'whoami' failed")
|
||||
println("Response 'whoami' failed. " + t.message)
|
||||
}
|
||||
})
|
||||
}
|
||||
private fun initializeGeofence(lat: Double, long: Double, rad: Float) {
|
||||
geofencingClient = LocationServices.getGeofencingClient(this)
|
||||
geofence = Geofence.Builder().setRequestId("Test")
|
||||
.setCircularRegion(lat, long, rad)
|
||||
.setExpirationDuration(Geofence.NEVER_EXPIRE)
|
||||
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
|
||||
.build()
|
||||
geofencingClient.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
|
||||
addOnSuccessListener {
|
||||
println("Geofence added with: latitude: $lat longitude: $long radius: $rad")
|
||||
}
|
||||
addOnFailureListener {
|
||||
println("Error: " + it.stackTrace.forEach { println(it.toString()) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getGeofencingRequest(): GeofencingRequest {
|
||||
return GeofencingRequest.Builder().apply {
|
||||
|
@ -11,4 +11,7 @@ interface GeofenceService {
|
||||
|
||||
@GET("whoami")
|
||||
fun getUser(): Call<ValuesUser>
|
||||
|
||||
@GET("accounts")
|
||||
fun getAccounts(): Call<List<ValuesTimetrackAccounts>>
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package de.hft.geotracker.retrofit
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
class ValuesLocation(
|
||||
latitude: Double,
|
||||
longitude: Double,
|
||||
radius: Int
|
||||
) {
|
||||
|
||||
@SerializedName("latitude")
|
||||
var latitude = latitude
|
||||
|
||||
@SerializedName("longitude")
|
||||
var longitude = longitude
|
||||
|
||||
@SerializedName("radius")
|
||||
var radius = radius.toFloat()
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package de.hft.geotracker.retrofit
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
class ValuesTimetrackAccounts(
|
||||
revenue: Double,
|
||||
name: String,
|
||||
description: String
|
||||
) {
|
||||
|
||||
@SerializedName("revenue")
|
||||
var revenue = revenue
|
||||
|
||||
@SerializedName("name")
|
||||
var name = name
|
||||
|
||||
@SerializedName("description")
|
||||
var description = description
|
||||
}
|
@ -7,7 +7,7 @@ class ValuesUser(
|
||||
firstname: String,
|
||||
lastname: String,
|
||||
username: String,
|
||||
location: String,
|
||||
location: ValuesLocation,
|
||||
id: Int
|
||||
) {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user