From 52e862afbea2bccdd28980217d7436c7c0902705 Mon Sep 17 00:00:00 2001 From: Tobias Wieck Date: Mon, 25 May 2020 12:46:02 +0000 Subject: [PATCH] Resolve "Query backend endpoint whoami" --- android/.idea/misc.xml | 2 +- .../hft/geotracker/activities/MainActivity.kt | 106 +++++++++--------- .../java/de/hft/geotracker/data/JWToken.kt | 6 +- .../retrofit/AuthenticationInterceptor.kt | 21 ++++ .../geotracker/retrofit/GeofenceService.kt | 3 +- .../de/hft/geotracker/retrofit/ValuesUser.kt | 26 +++++ .../app/src/main/res/layout/activity_home.xml | 47 +++++--- android/app/src/main/res/values/strings.xml | 1 + 8 files changed, 138 insertions(+), 74 deletions(-) create mode 100644 android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt create mode 100644 android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt diff --git a/android/.idea/misc.xml b/android/.idea/misc.xml index b6ea2b1..7bfef59 100644 --- a/android/.idea/misc.xml +++ b/android/.idea/misc.xml @@ -1,6 +1,6 @@ - + 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 c6cfd08..d634e7d 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 @@ -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(R.id.latitude).text = location.latitude.toString() - findViewById(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(R.id.button_start_stop) - btn.isEnabled = false - btn?.setBackgroundColor(resources.getColor(R.color.colorPrimaryDark)) - - } - fun enableButton() { - val btn = findViewById(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 - } + private fun showUsername() { + val call = service.getUser() + call.enqueue(object : Callback { + override fun onResponse(call: Call, response: Response) { + if(response.isSuccessful) { + val firstname = response.body()?.firstname + lbl_username.text = firstname + println("Body: " + firstname) + } else { + println("Response not successful: ${response.code()}") + } + } + override fun onFailure(call: Call, t: Throwable) { + println("Response 'whoami' failed") + } + } ) } private fun getGeofencingRequest(): GeofencingRequest { diff --git a/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt b/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt index cebf48a..3d2beaa 100644 --- a/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt +++ b/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt @@ -1,3 +1,7 @@ package de.hft.geotracker.data -data class JWToken (var token : String) \ No newline at end of file +data class JWToken (var token : String) { + public fun getJWToken() : String { + return token + } +} \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt new file mode 100644 index 0000000..8808497 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt @@ -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) + } + +} \ No newline at end of file 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 e818e19..7634199 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 @@ -7,5 +7,6 @@ interface GeofenceService { @POST("/login") fun login(@Body login_data : ValuesUserLogin) : Call - + @GET("whoami") + fun getUser() : Call } \ 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 new file mode 100644 index 0000000..25b9cc2 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt @@ -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 + +} \ No newline at end of file diff --git a/android/app/src/main/res/layout/activity_home.xml b/android/app/src/main/res/layout/activity_home.xml index 35cf9ae..fcfd7e6 100644 --- a/android/app/src/main/res/layout/activity_home.xml +++ b/android/app/src/main/res/layout/activity_home.xml @@ -27,12 +27,35 @@ app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextAppearance="@style/text_style" /> + + + + - - + app:layout_constraintVertical_bias="0.19" /> + app:layout_constraintBottom_toBottomOf="@+id/selected_acc" + app:layout_constraintEnd_toEndOf="parent" /> + app:layout_constraintTop_toBottomOf="@+id/selected_acc" /> Create Account Submit Logout + Hello