From dad0594854a5ff469077a3cffea8f340c7eec65b Mon Sep 17 00:00:00 2001 From: wiecktobi Date: Thu, 28 May 2020 11:33:11 +0200 Subject: [PATCH] Set geofenc according to users location values --- .../hft/geotracker/activities/MainActivity.kt | 66 ++++++++++++++----- .../geotracker/retrofit/GeofenceService.kt | 3 + .../hft/geotracker/retrofit/ValuesLocation.kt | 19 ++++++ .../retrofit/ValuesTimetrackAccounts.kt | 19 ++++++ .../de/hft/geotracker/retrofit/ValuesUser.kt | 2 +- 5 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 android/app/src/main/java/de/hft/geotracker/retrofit/ValuesLocation.kt create mode 100644 android/app/src/main/java/de/hft/geotracker/retrofit/ValuesTimetrackAccounts.kt diff --git a/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt b/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt index 836d029..2fe38d0 100644 --- a/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt @@ -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> { + override fun onResponse( + call: Call>, + response: Response> + ) { + if (response.isSuccessful) { + val responseBody = response.body()!! + responseBody.forEach { + println(it.name) + } + } + + } + override fun onFailure(call: Call>, 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, response: Response) { 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, 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 { diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt index 5cf681c..8759a25 100644 --- a/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt @@ -11,4 +11,7 @@ interface GeofenceService { @GET("whoami") fun getUser(): Call + + @GET("accounts") + fun getAccounts(): Call> } \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesLocation.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesLocation.kt new file mode 100644 index 0000000..06c79e5 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesLocation.kt @@ -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() +} \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesTimetrackAccounts.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesTimetrackAccounts.kt new file mode 100644 index 0000000..3f94cd6 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesTimetrackAccounts.kt @@ -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 +} \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt index cb47e5b..76eb7a7 100644 --- a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt @@ -7,7 +7,7 @@ class ValuesUser( firstname: String, lastname: String, username: String, - location: String, + location: ValuesLocation, id: Int ) {