Resolve "Query backend endpoint whoami"

This commit is contained in:
Tobias Wieck 2020-05-25 12:46:02 +00:00
parent 83bd086792
commit 52e862afbe
8 changed files with 138 additions and 74 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="JDK" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -18,18 +18,29 @@ import androidx.core.app.ActivityCompat.requestPermissions
import com.google.android.gms.location.*
import de.hft.geotracker.GeofenceBroadcastReceiver
import de.hft.geotracker.R
import de.hft.geotracker.data.JWToken
import de.hft.geotracker.retrofit.AuthenticationInterceptor
import de.hft.geotracker.retrofit.GeofenceService
import de.hft.geotracker.retrofit.ValuesUser
import kotlinx.android.synthetic.main.activity_home.*
import okhttp3.OkHttpClient
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
lateinit var fusedLocationClient: FusedLocationProviderClient
lateinit var locationRequest: LocationRequest
lateinit var locationCallback: LocationCallback
lateinit var geofencingClient: GeofencingClient
lateinit var geofence: Geofence
lateinit var actionButton: TextView
var running = false
lateinit var service : GeofenceService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -58,24 +69,7 @@ class MainActivity : AppCompatActivity() {
button_start_stop.isEnabled = btnState
}
// val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
createLocationRequest()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED
) {
requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000)
}
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
// Update UI with location data
findViewById<TextView>(R.id.latitude).text = location.latitude.toString()
findViewById<TextView>(R.id.longitude).text = location.longitude.toString()
}
}
}
//Setup geofence
geofencingClient = LocationServices.getGeofencingClient(this)
geofence = Geofence.Builder().setRequestId("Test")
.setCircularRegion(48.3575, 8.9745, 50F)
@ -91,7 +85,28 @@ class MainActivity : AppCompatActivity() {
}
}
//JWToken lesen
var fis = openFileInput("JWToken")
var isr = InputStreamReader(fis)
val bufferedReader = BufferedReader(isr)
val stringBuilder = StringBuilder()
var text : String? = null
while ({text = bufferedReader.readLine(); text} () != null) {
stringBuilder.append(text)
}
val token = stringBuilder.toString()
println("Token Main: " + token)
//Retrofit declaration
var httpClient = OkHttpClient.Builder()
val interceptor = AuthenticationInterceptor(token)
httpClient.addInterceptor(interceptor)
val builder = Retrofit.Builder()
.baseUrl("http://plesk.icaotix.de:5000")
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
val retrofit = builder.build()
service = retrofit.create(GeofenceService::class.java)
showUsername()
val spinner: Spinner = findViewById(R.id.account_spinner)
// Create an ArrayAdapter using the string array and a default spinner layout
@ -117,37 +132,22 @@ class MainActivity : AppCompatActivity() {
//ToDO call /track Endpoint
}
fun disableButton() {
val btn = findViewById<TextView>(R.id.button_start_stop)
btn.isEnabled = false
btn?.setBackgroundColor(resources.getColor(R.color.colorPrimaryDark))
private fun showUsername() {
val call = service.getUser()
call.enqueue(object : Callback<ValuesUser> {
override fun onResponse(call: Call<ValuesUser>, response: Response<ValuesUser>) {
if(response.isSuccessful) {
val firstname = response.body()?.firstname
lbl_username.text = firstname
println("Body: " + firstname)
} else {
println("Response not successful: ${response.code()}")
}
fun enableButton() {
val btn = findViewById<TextView>(R.id.button_start_stop)
btn.isEnabled = true
btn?.setBackgroundColor(resources.getColor(R.color.logo_blue))
}
override fun onResume() {
super.onResume()
startLocationUpdates()
}
private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply {
interval = 10000
fastestInterval = 5000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
override fun onFailure(call: Call<ValuesUser>, t: Throwable) {
println("Response 'whoami' failed")
}
} )
}
private fun getGeofencingRequest(): GeofencingRequest {

View File

@ -1,3 +1,7 @@
package de.hft.geotracker.data
data class JWToken (var token : String)
data class JWToken (var token : String) {
public fun getJWToken() : String {
return token
}
}

View File

@ -0,0 +1,21 @@
package de.hft.geotracker.retrofit
import de.hft.geotracker.data.JWToken
import okhttp3.Interceptor
import okhttp3.Response
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
class AuthenticationInterceptor(pToken : String) : Interceptor {
val token = pToken
override fun intercept(chain: Interceptor.Chain): Response {
var original = chain.request()
var builder = original.newBuilder()
.header("Authorization", token)
val request = builder.build()
return chain.proceed(request)
}
}

View File

@ -7,5 +7,6 @@ interface GeofenceService {
@POST("/login")
fun login(@Body login_data : ValuesUserLogin) : Call<Void>
@GET("whoami")
fun getUser() : Call<ValuesUser>
}

View File

@ -0,0 +1,26 @@
package de.hft.geotracker.retrofit
import com.google.gson.annotations.SerializedName
class ValuesUser (
role : String,
firstname : String,
lastname : String,
username : String,
location : String,
id : Integer) {
@SerializedName("role")
var role = role
@SerializedName("firstname")
var firstname = firstname
@SerializedName("lastname")
var lastname = lastname
@SerializedName("username")
var username = username
@SerializedName("location")
var location = location
@SerializedName("id")
var id = id
}

View File

@ -27,12 +27,35 @@
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextAppearance="@style/text_style" />
<TextView
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin16"
android:layout_marginTop="16dp"
android:text="@string/hello"
android:textAppearance="@style/text_style"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/selected_acc"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/lbl_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:textAppearance="@style/text_style"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="@+id/hello"
app:layout_constraintStart_toEndOf="@+id/hello" />
<TextView
android:id="@+id/selected_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:fontFamily="@font/montserrat"
android:text="@string/timetrack_account"
android:textAppearance="@style/text_style"
@ -42,18 +65,7 @@
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/display_acc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:layout_marginEnd="28dp"
android:text="@string/no_account"
android:textAppearance="@style/text_style"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintVertical_bias="0.19" />
<ToggleButton
android:id="@+id/button_start_stop"
@ -80,9 +92,8 @@
android:layout_marginEnd="@dimen/margin16"
android:background="@color/logo_white"
android:textAlignment="textEnd"
app:layout_constraintBottom_toTopOf="@+id/display_acc"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/my_toolbar" />
app:layout_constraintBottom_toBottomOf="@+id/selected_acc"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/altitude"
@ -100,12 +111,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:text="dummy"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/display_acc" />
app:layout_constraintTop_toBottomOf="@+id/selected_acc" />
<TextView
android:id="@+id/longitude"

View File

@ -35,4 +35,5 @@
<string name="create_account">Create Account</string>
<string name="submit">Submit</string>
<string name="logout">Logout</string>
<string name="hello">Hello</string>
</resources>