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,38 +20,39 @@ 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) {
Geofence.GEOFENCE_TRANSITION_ENTER -> {
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit() ?.edit()
?.putBoolean("ENABLED", true) ?.putBoolean("ENABLED", true)
?.apply() ?.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\nTriggering Geofences: $triggeringGeofences" val geofenceTransitionDetails = "Transition: $geofenceTransition" +
"\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ") println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails) Log.i(TAG, geofenceTransitionDetails)
} else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { }
Geofence.GEOFENCE_TRANSITION_EXIT -> {
context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit() ?.edit()
?.putBoolean("ENABLED", false) ?.putBoolean("ENABLED", false)
?.apply() ?.apply()
val triggeringGeofences = geofencingEvent.triggeringGeofences val triggeringGeofences = geofencingEvent.triggeringGeofences
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences" val geofenceTransitionDetails =
"Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ") println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails) Log.i(TAG, geofenceTransitionDetails)
} else { }
// Log the error. else -> {
println("Error Transition: ") println("Error Transition: ")
Log.e(TAG, geofenceTransition.toString()) 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
@ -28,15 +27,16 @@ 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),
@ -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() {
@ -85,8 +78,8 @@ 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
@ -96,7 +89,7 @@ class MainActivity : AppCompatActivity() {
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
@ -161,6 +154,7 @@ 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")
} }

View File

@ -1,9 +1,9 @@
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() {
@ -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,7 +1,9 @@
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")

View File

@ -8,18 +8,24 @@ class ValuesUser (
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

@ -6,6 +6,7 @@ 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