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.ArrayAdapter
|
||||||
import android.widget.Spinner
|
import android.widget.Spinner
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.app.ActivityCompat.requestPermissions
|
import androidx.core.app.ActivityCompat.requestPermissions
|
||||||
@ -18,6 +19,7 @@ import de.hft.geotracker.GeofenceBroadcastReceiver
|
|||||||
import de.hft.geotracker.R
|
import de.hft.geotracker.R
|
||||||
import de.hft.geotracker.retrofit.AuthenticationInterceptor
|
import de.hft.geotracker.retrofit.AuthenticationInterceptor
|
||||||
import de.hft.geotracker.retrofit.GeofenceService
|
import de.hft.geotracker.retrofit.GeofenceService
|
||||||
|
import de.hft.geotracker.retrofit.ValuesTimetrackAccounts
|
||||||
import de.hft.geotracker.retrofit.ValuesUser
|
import de.hft.geotracker.retrofit.ValuesUser
|
||||||
import kotlinx.android.synthetic.main.activity_home.*
|
import kotlinx.android.synthetic.main.activity_home.*
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
@ -80,22 +82,6 @@ class MainActivity : AppCompatActivity() {
|
|||||||
button_start_stop.isEnabled = btnState
|
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
|
//JWToken lesen
|
||||||
val fis = openFileInput("JWToken")
|
val fis = openFileInput("JWToken")
|
||||||
val isr = InputStreamReader(fis)
|
val isr = InputStreamReader(fis)
|
||||||
@ -119,6 +105,28 @@ class MainActivity : AppCompatActivity() {
|
|||||||
service = retrofit.create(GeofenceService::class.java)
|
service = retrofit.create(GeofenceService::class.java)
|
||||||
showUsername()
|
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)
|
val spinner: Spinner = findViewById(R.id.account_spinner)
|
||||||
// Create an ArrayAdapter using the string array and a default spinner layout
|
// Create an ArrayAdapter using the string array and a default spinner layout
|
||||||
ArrayAdapter.createFromResource(
|
ArrayAdapter.createFromResource(
|
||||||
@ -130,6 +138,7 @@ class MainActivity : AppCompatActivity() {
|
|||||||
// Apply the adapter to the spinner
|
// Apply the adapter to the spinner
|
||||||
spinner.adapter = adapter
|
spinner.adapter = adapter
|
||||||
}
|
}
|
||||||
|
|
||||||
actionButton = findViewById(R.id.button_start_stop)
|
actionButton = findViewById(R.id.button_start_stop)
|
||||||
actionButton.setOnClickListener {
|
actionButton.setOnClickListener {
|
||||||
callStartStop()
|
callStartStop()
|
||||||
@ -165,18 +174,41 @@ class MainActivity : AppCompatActivity() {
|
|||||||
override fun onResponse(call: Call<ValuesUser>, response: Response<ValuesUser>) {
|
override fun onResponse(call: Call<ValuesUser>, response: Response<ValuesUser>) {
|
||||||
if (response.isSuccessful) {
|
if (response.isSuccessful) {
|
||||||
val firstname = response.body()?.firstname
|
val firstname = response.body()?.firstname
|
||||||
|
val location = response.body()?.location
|
||||||
lbl_username.text = "Hello " + firstname
|
lbl_username.text = "Hello " + firstname
|
||||||
println("Body: " + 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 {
|
} else {
|
||||||
println("Response not successful: ${response.code()}")
|
println("Response not successful: ${response.code()}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(call: Call<ValuesUser>, t: Throwable) {
|
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 {
|
private fun getGeofencingRequest(): GeofencingRequest {
|
||||||
return GeofencingRequest.Builder().apply {
|
return GeofencingRequest.Builder().apply {
|
||||||
|
@ -11,4 +11,7 @@ interface GeofenceService {
|
|||||||
|
|
||||||
@GET("whoami")
|
@GET("whoami")
|
||||||
fun getUser(): Call<ValuesUser>
|
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,
|
firstname: String,
|
||||||
lastname: String,
|
lastname: String,
|
||||||
username: String,
|
username: String,
|
||||||
location: String,
|
location: ValuesLocation,
|
||||||
id: Int
|
id: Int
|
||||||
) {
|
) {
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user