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
@ -28,15 +27,16 @@ class Login : AppCompatActivity() {
lateinit var login: TextView
lateinit var reg: TextView
lateinit var service: GeofenceService
lateinit var token : JWToken
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")
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() {
@ -85,8 +78,8 @@ 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
@ -96,7 +89,7 @@ class MainActivity : AppCompatActivity() {
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
@ -161,6 +154,7 @@ class MainActivity : AppCompatActivity() {
println("Response not successful: ${response.code()}")
}
}
override fun onFailure(call: Call<ValuesUser>, t: Throwable) {
println("Response 'whoami' failed")
}

View File

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

View File

@ -8,18 +8,24 @@ class ValuesUser (
lastname: String,
username: String,
location: String,
id : kotlin.Int) {
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

@ -6,6 +6,7 @@ class ValuesUserLogin (name : String, pswd : String) {
@SerializedName("username")
var username = name
@SerializedName("password")
var password = pswd