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.ContentValues.TAG
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.SharedPreferences
import android.util.Log import android.util.Log
import com.google.android.gms.location.Geofence import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofenceStatusCodes import com.google.android.gms.location.GeofenceStatusCodes
import com.google.android.gms.location.GeofencingEvent import com.google.android.gms.location.GeofencingEvent
import de.hft.geotracker.activities.MainActivity
class GeofenceBroadcastReceiver : BroadcastReceiver() { class GeofenceBroadcastReceiver : BroadcastReceiver() {
@ -22,37 +20,38 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
return return
} }
// Get the transition type.
val geofenceTransition = geofencingEvent.geofenceTransition
// Test that the reported transition was of interest. // Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) { when (val geofenceTransition = geofencingEvent.geofenceTransition) {
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) Geofence.GEOFENCE_TRANSITION_ENTER -> {
?.edit() context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.putBoolean("ENABLED", true) ?.edit()
?.apply() ?.putBoolean("ENABLED", true)
?.apply()
// Get the geofences that were triggered. A single event can trigger // Get the geofences that were triggered. A single event can trigger multiple geofences.
// multiple geofences. val triggeringGeofences = geofencingEvent.triggeringGeofences
val triggeringGeofences = geofencingEvent.triggeringGeofences // Get the transition details as a String.
// Get the transition details as a String. val geofenceTransitionDetails = "Transition: $geofenceTransition" +
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences" "\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ") println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails) Log.i(TAG, geofenceTransitionDetails)
} else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { }
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) Geofence.GEOFENCE_TRANSITION_EXIT -> {
?.edit() context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.putBoolean("ENABLED", false) ?.edit()
?.apply() ?.putBoolean("ENABLED", false)
?.apply()
val triggeringGeofences = geofencingEvent.triggeringGeofences val triggeringGeofences = geofencingEvent.triggeringGeofences
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences" val geofenceTransitionDetails =
println("Success Transition: ") "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
Log.i(TAG, geofenceTransitionDetails) println("Success Transition: ")
} else { Log.i(TAG, geofenceTransitionDetails)
// Log the error. }
println("Error Transition: ") else -> {
Log.e(TAG, geofenceTransition.toString()) 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.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.os.Bundle import android.os.Bundle
import androidx.fragment.app.Fragment
import android.widget.TextView import android.widget.TextView
import android.widget.Toast 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.content.ContextCompat import androidx.core.content.ContextCompat
import de.hft.geotracker.* import androidx.fragment.app.Fragment
import de.hft.geotracker.data.JWToken import de.hft.geotracker.R
import de.hft.geotracker.retrofit.GeofenceService import de.hft.geotracker.retrofit.GeofenceService
import de.hft.geotracker.retrofit.ValuesUserLogin import de.hft.geotracker.retrofit.ValuesUserLogin
import retrofit2.Call import retrofit2.Call
@ -24,19 +23,20 @@ import retrofit2.converter.gson.GsonConverterFactory
/** /**
* A simple [Fragment] subclass. * A simple [Fragment] subclass.
*/ */
class Login : AppCompatActivity() { class Login : AppCompatActivity() {
lateinit var login : TextView lateinit var login: TextView
lateinit var reg : TextView lateinit var reg: TextView
lateinit var service : GeofenceService lateinit var service: GeofenceService
lateinit var token : JWToken
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login) setContentView(R.layout.activity_login)
if (ContextCompat.checkSelfPermission(this, if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_BACKGROUND_LOCATION Manifest.permission.ACCESS_BACKGROUND_LOCATION
) != PackageManager.PERMISSION_GRANTED) { ) != PackageManager.PERMISSION_GRANTED
) {
ActivityCompat.requestPermissions( ActivityCompat.requestPermissions(
this, this,
arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION),
@ -49,8 +49,8 @@ class Login : AppCompatActivity() {
val retrofit = Retrofit.Builder() val retrofit = Retrofit.Builder()
.baseUrl("http://plesk.icaotix.de:5000") .baseUrl("http://plesk.icaotix.de:5000")
.addConverterFactory(GsonConverterFactory.create()) .addConverterFactory(GsonConverterFactory.create())
.build() .build()
service = retrofit.create(GeofenceService::class.java) service = retrofit.create(GeofenceService::class.java)
login = findViewById(R.id.button_create_account) login = findViewById(R.id.button_create_account)
@ -72,30 +72,25 @@ class Login : AppCompatActivity() {
private fun login() { private fun login() {
val name = findViewById<TextView>(R.id.setting_input_username).text.toString() val name = findViewById<TextView>(R.id.setting_input_username).text.toString()
val pswd = findViewById<TextView>(R.id.input_password).text.toString() val pswd = findViewById<TextView>(R.id.input_password).text.toString()
var call= service.login( val call = service.login(ValuesUserLogin(name, pswd))
ValuesUserLogin(
name,
pswd
)
)
call.enqueue(object : Callback<Void> { call.enqueue(object : Callback<Void> {
override fun onResponse(call: Call<Void>?, response: Response<Void>?) { override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
if(response != null && response.isSuccessful) { if (response != null && response.isSuccessful) {
var headers = response.headers() val headers = response.headers()
var authentication = headers.get("Authorization") val authentication = headers.get("Authorization")
openFileOutput("JWToken", Context.MODE_PRIVATE).use { openFileOutput("JWToken", Context.MODE_PRIVATE).use {
it.write(authentication!!.toByteArray()) it.write(authentication!!.toByteArray())
} }
token = JWToken(authentication!!)
println(response.code()) println(response.code())
println(token.token)
startActivity(intent) startActivity(intent)
} else { } else {
if (response != null) { if (response != null) {
println(response.code()) 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 { } else {
println("Response is null") println("Response is null")
} }

View File

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

View File

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

View File

@ -2,7 +2,6 @@ package de.hft.geotracker.activities
import android.os.Bundle import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import de.hft.geotracker.R import de.hft.geotracker.R
import kotlinx.android.synthetic.main.activity_home.* 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 package de.hft.geotracker.retrofit
import de.hft.geotracker.data.JWToken
import okhttp3.Interceptor import okhttp3.Interceptor
import okhttp3.Response import okhttp3.Response
import java.io.BufferedReader
import java.io.InputStreamReader
import java.lang.StringBuilder
class AuthenticationInterceptor(pToken : String) : Interceptor { class AuthenticationInterceptor(pToken: String) : Interceptor {
val token = pToken private val token = pToken
override fun intercept(chain: Interceptor.Chain): Response { override fun intercept(chain: Interceptor.Chain): Response {
var original = chain.request() val original = chain.request()
var builder = original.newBuilder() val builder = original.newBuilder()
.header("Authorization", token) .header("Authorization", token)
val request = builder.build() val request = builder.build()
return chain.proceed(request) return chain.proceed(request)

View File

@ -1,12 +1,14 @@
package de.hft.geotracker.retrofit package de.hft.geotracker.retrofit
import retrofit2.Call import retrofit2.Call
import retrofit2.http.* import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
interface GeofenceService { interface GeofenceService {
@POST("/login") @POST("/login")
fun login(@Body login_data : ValuesUserLogin) : Call<Void> fun login(@Body login_data: ValuesUserLogin): Call<Void>
@GET("whoami") @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 import com.google.gson.annotations.SerializedName
class ValuesUser ( class ValuesUser(
role : String, role: String,
firstname : String, firstname: String,
lastname : String, lastname: String,
username : String, username: String,
location : String, location: String,
id : kotlin.Int) { id: Int
) {
@SerializedName("role") @SerializedName("role")
var role = role var role = role
@SerializedName("firstname") @SerializedName("firstname")
var firstname = firstname var firstname = firstname
@SerializedName("lastname") @SerializedName("lastname")
var lastname = lastname var lastname = lastname
@SerializedName("username") @SerializedName("username")
var username = username var username = username
@SerializedName("location") @SerializedName("location")
var location = location var location = location
@SerializedName("id") @SerializedName("id")
var id = id var id = id

View File

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