Remove JWT data class

* Code formatting
* Static code analysis
This commit is contained in:
wiecktobi 2020-05-25 23:29:36 +02:00
parent 6907710f51
commit c124818fa6
10 changed files with 89 additions and 105 deletions

View File

@ -4,12 +4,10 @@ import android.content.BroadcastReceiver
import android.content.ContentValues.TAG
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.util.Log
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofenceStatusCodes
import com.google.android.gms.location.GeofencingEvent
import de.hft.geotracker.activities.MainActivity
class GeofenceBroadcastReceiver : BroadcastReceiver() {
@ -22,38 +20,39 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
return
}
// Get the transition type.
val geofenceTransition = geofencingEvent.geofenceTransition
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
when (val geofenceTransition = geofencingEvent.geofenceTransition) {
Geofence.GEOFENCE_TRANSITION_ENTER -> {
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit()
?.putBoolean("ENABLED", true)
?.apply()
// Get the geofences that were triggered. A single event can trigger
// multiple geofences.
// Get the geofences that were triggered. A single event can trigger multiple geofences.
val triggeringGeofences = geofencingEvent.triggeringGeofences
// Get the transition details as a String.
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
val geofenceTransitionDetails = "Transition: $geofenceTransition" +
"\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails)
} else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
}
Geofence.GEOFENCE_TRANSITION_EXIT -> {
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit()
?.putBoolean("ENABLED", false)
?.apply()
val triggeringGeofences = geofencingEvent.triggeringGeofences
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
val geofenceTransitionDetails =
"Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails)
} else {
// Log the error.
}
else -> {
println("Error Transition: ")
Log.e(TAG, geofenceTransition.toString())
}
}
}
}

View File

@ -5,14 +5,13 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import de.hft.geotracker.*
import de.hft.geotracker.data.JWToken
import androidx.fragment.app.Fragment
import de.hft.geotracker.R
import de.hft.geotracker.retrofit.GeofenceService
import de.hft.geotracker.retrofit.ValuesUserLogin
import retrofit2.Call
@ -25,18 +24,19 @@ import retrofit2.converter.gson.GsonConverterFactory
* A simple [Fragment] subclass.
*/
class Login : AppCompatActivity() {
lateinit var login : TextView
lateinit var reg : TextView
lateinit var service : GeofenceService
lateinit var token : JWToken
lateinit var login: TextView
lateinit var reg: TextView
lateinit var service: GeofenceService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
if (ContextCompat.checkSelfPermission(this,
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_BACKGROUND_LOCATION
) != PackageManager.PERMISSION_GRANTED) {
) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions(
this,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION),
@ -72,30 +72,25 @@ class Login : AppCompatActivity() {
private fun login() {
val name = findViewById<TextView>(R.id.setting_input_username).text.toString()
val pswd = findViewById<TextView>(R.id.input_password).text.toString()
var call= service.login(
ValuesUserLogin(
name,
pswd
)
)
val call = service.login(ValuesUserLogin(name, pswd))
call.enqueue(object : Callback<Void> {
override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
if(response != null && response.isSuccessful) {
var headers = response.headers()
var authentication = headers.get("Authorization")
if (response != null && response.isSuccessful) {
val headers = response.headers()
val authentication = headers.get("Authorization")
openFileOutput("JWToken", Context.MODE_PRIVATE).use {
it.write(authentication!!.toByteArray())
}
token = JWToken(authentication!!)
println(response.code())
println(token.token)
startActivity(intent)
} else {
if (response != null) {
println(response.code())
Toast.makeText(this@Login, "Wrong Username or Password!", Toast.LENGTH_LONG).show()
Toast.makeText(this@Login, "Wrong Username or Password!", Toast.LENGTH_LONG)
.show()
} else {
println("Response is null")
}

View File

@ -1,24 +1,19 @@
package de.hft.geotracker.activities
import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.os.Bundle
import android.os.Looper
import android.view.MenuItem
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions
import com.google.android.gms.location.*
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices
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
@ -31,8 +26,6 @@ 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() {
@ -40,7 +33,7 @@ class MainActivity : AppCompatActivity() {
lateinit var geofence: Geofence
lateinit var actionButton: TextView
var running = false
lateinit var service : GeofenceService
lateinit var service: GeofenceService
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -85,18 +78,18 @@ class MainActivity : AppCompatActivity() {
}
//JWToken lesen
var fis = openFileInput("JWToken")
var isr = InputStreamReader(fis)
val fis = openFileInput("JWToken")
val isr = InputStreamReader(fis)
val bufferedReader = BufferedReader(isr)
val stringBuilder = StringBuilder()
var text : String? = null
while ({text = bufferedReader.readLine(); text} () != null) {
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 httpClient = OkHttpClient.Builder()
val interceptor = AuthenticationInterceptor(token)
httpClient.addInterceptor(interceptor)
val builder = Retrofit.Builder()
@ -128,7 +121,7 @@ class MainActivity : AppCompatActivity() {
my_toolbar.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.settings -> {
var intent = Intent(this, Settings::class.java)
val intent = Intent(this, Settings::class.java)
startActivity(intent)
println("Settings pressed")
true
@ -153,7 +146,7 @@ class MainActivity : AppCompatActivity() {
val call = service.getUser()
call.enqueue(object : Callback<ValuesUser> {
override fun onResponse(call: Call<ValuesUser>, response: Response<ValuesUser>) {
if(response.isSuccessful) {
if (response.isSuccessful) {
val firstname = response.body()?.firstname
lbl_username.text = firstname
println("Body: " + firstname)
@ -161,10 +154,11 @@ class MainActivity : AppCompatActivity() {
println("Response not successful: ${response.code()}")
}
}
override fun onFailure(call: Call<ValuesUser>, t: Throwable) {
println("Response 'whoami' failed")
}
} )
})
}
private fun getGeofencingRequest(): GeofencingRequest {

View File

@ -1,13 +1,13 @@
package de.hft.geotracker.activities
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import de.hft.geotracker.R
class Register : AppCompatActivity() {
lateinit var reg : TextView
lateinit var reg: TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
@ -18,8 +18,7 @@ class Register : AppCompatActivity() {
}
private fun createAccount() {
var intent = Intent(this, MainActivity::class.java)
startActivity(intent)
startActivity(Intent(this, MainActivity::class.java))
}

View File

@ -2,7 +2,6 @@ package de.hft.geotracker.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import de.hft.geotracker.R
import kotlinx.android.synthetic.main.activity_home.*

View File

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

View File

@ -1,18 +1,14 @@
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
class AuthenticationInterceptor(pToken: String) : Interceptor {
private val token = pToken
override fun intercept(chain: Interceptor.Chain): Response {
var original = chain.request()
var builder = original.newBuilder()
val original = chain.request()
val builder = original.newBuilder()
.header("Authorization", token)
val request = builder.build()
return chain.proceed(request)

View File

@ -1,12 +1,14 @@
package de.hft.geotracker.retrofit
import retrofit2.Call
import retrofit2.http.*
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
interface GeofenceService {
@POST("/login")
fun login(@Body login_data : ValuesUserLogin) : Call<Void>
fun login(@Body login_data: ValuesUserLogin): Call<Void>
@GET("whoami")
fun getUser() : Call<ValuesUser>
fun getUser(): Call<ValuesUser>
}

View File

@ -2,24 +2,30 @@ package de.hft.geotracker.retrofit
import com.google.gson.annotations.SerializedName
class ValuesUser (
role : String,
firstname : String,
lastname : String,
username : String,
location : String,
id : kotlin.Int) {
class ValuesUser(
role: String,
firstname: String,
lastname: String,
username: String,
location: String,
id: Int
) {
@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

@ -2,10 +2,11 @@ package de.hft.geotracker.retrofit
import com.google.gson.annotations.SerializedName
class ValuesUserLogin (name : String, pswd : String) {
class ValuesUserLogin(name: String, pswd: String) {
@SerializedName("username")
var username = name
@SerializedName("password")
var password = pswd