diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt b/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt index 89208dd..d2bd2d6 100644 --- a/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt +++ b/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt @@ -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,37 +20,38 @@ 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) { - context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) - ?.edit() - ?.putBoolean("ENABLED", true) - ?.apply() + 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. - val triggeringGeofences = geofencingEvent.triggeringGeofences - // Get the transition details as a String. - val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences" - println("Success Transition: ") - Log.i(TAG, geofenceTransitionDetails) - } else if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { - context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) - ?.edit() - ?.putBoolean("ENABLED", false) - ?.apply() + // 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" + println("Success Transition: ") + Log.i(TAG, geofenceTransitionDetails) + } + 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" - println("Success Transition: ") - Log.i(TAG, geofenceTransitionDetails) - } else { - // Log the error. - println("Error Transition: ") - Log.e(TAG, geofenceTransition.toString()) + val triggeringGeofences = geofencingEvent.triggeringGeofences + val geofenceTransitionDetails = + "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences" + println("Success Transition: ") + Log.i(TAG, geofenceTransitionDetails) + } + else -> { + println("Error Transition: ") + Log.e(TAG, geofenceTransition.toString()) + } } } diff --git a/android/app/src/main/java/de/hft/geotracker/activities/Login.kt b/android/app/src/main/java/de/hft/geotracker/activities/Login.kt index 9857068..005cd3e 100644 --- a/android/app/src/main/java/de/hft/geotracker/activities/Login.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Login.kt @@ -1,17 +1,17 @@ package de.hft.geotracker.activities import android.Manifest +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 @@ -23,19 +23,20 @@ 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 +class Login : AppCompatActivity() { + 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), @@ -48,8 +49,8 @@ class Login : AppCompatActivity() { val retrofit = Retrofit.Builder() .baseUrl("http://plesk.icaotix.de:5000") - .addConverterFactory(GsonConverterFactory.create()) - .build() + .addConverterFactory(GsonConverterFactory.create()) + .build() service = retrofit.create(GeofenceService::class.java) login = findViewById(R.id.button_create_account) @@ -71,27 +72,26 @@ class Login : AppCompatActivity() { private fun login() { val name = findViewById(R.id.setting_input_username).text.toString() val pswd = findViewById(R.id.input_password).text.toString() - var call= service.login( - ValuesUserLogin( - name, - pswd - ) - ) + val call = service.login(ValuesUserLogin(name, pswd)) call.enqueue(object : Callback { override fun onResponse(call: Call?, response: Response?) { - 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") - token = JWToken(authentication!!) - println(response.code()) - println(token.token) - startActivity(intent) + deleteFile("JWToken") + openFileOutput("JWToken", Context.MODE_PRIVATE).use { + it.write(authentication!!.toByteArray()) + } + + println(response.code()) + 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") } 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 d634e7d..9e924f2 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 @@ -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,12 +33,11 @@ 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) - setSupportActionBar(findViewById(R.id.my_toolbar)) setContentView(R.layout.activity_home) this.getSharedPreferences("LOCATION", Context.MODE_PRIVATE) @@ -86,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() @@ -123,6 +115,24 @@ class MainActivity : AppCompatActivity() { actionButton.setOnClickListener { callStartStop() } + my_toolbar.setNavigationOnClickListener { + altitude.text = "Pressed" + } + my_toolbar.setOnMenuItemClickListener { menuItem -> + when (menuItem.itemId) { + R.id.settings -> { + val intent = Intent(this, Settings::class.java) + startActivity(intent) + println("Settings pressed") + true + } + R.id.logout -> { + println("Logout pressed") + true + } + else -> false + } + } } private fun callStartStop() { @@ -136,7 +146,7 @@ class MainActivity : AppCompatActivity() { val call = service.getUser() call.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { - if(response.isSuccessful) { + if (response.isSuccessful) { val firstname = response.body()?.firstname lbl_username.text = firstname println("Body: " + firstname) @@ -144,10 +154,11 @@ class MainActivity : AppCompatActivity() { println("Response not successful: ${response.code()}") } } + override fun onFailure(call: Call, t: Throwable) { println("Response 'whoami' failed") } - } ) + }) } private fun getGeofencingRequest(): GeofencingRequest { @@ -164,28 +175,6 @@ class MainActivity : AppCompatActivity() { PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT) } - override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { - R.id.settings -> { - // User chose the "Settings" item, show the app settings UI... - var intent = Intent(this, Settings::class.java) - startActivity(intent) - println("test") - true - } - R.id.logout -> { - // User chose the "Settings" item, show the app settings UI... - var intent = Intent(this, Login::class.java) - startActivity(intent) - true - } - else -> { - // If we got here, the user's action was not recognized. - // Invoke the superclass to handle it. - println("test") - super.onOptionsItemSelected(item) - } - } - override fun onBackPressed() { } diff --git a/android/app/src/main/java/de/hft/geotracker/activities/Register.kt b/android/app/src/main/java/de/hft/geotracker/activities/Register.kt index f2dbecc..faa0ffd 100644 --- a/android/app/src/main/java/de/hft/geotracker/activities/Register.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Register.kt @@ -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)) } diff --git a/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt b/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt index c9951bf..16e2f70 100644 --- a/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt @@ -2,27 +2,17 @@ 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.* class Settings : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.settings_activity) - supportFragmentManager - .beginTransaction() - .replace( - R.id.settings, - SettingsFragment() - ) - .commit() - supportActionBar?.setDisplayHomeAsUpEnabled(true) - } - - class SettingsFragment : PreferenceFragmentCompat() { - override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { - setPreferencesFromResource(R.xml.root_preferences, rootKey) + my_toolbar.setNavigationOnClickListener { + onBackPressed() } } + } \ No newline at end of file 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 deleted file mode 100644 index 3d2beaa..0000000 --- a/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt +++ /dev/null @@ -1,7 +0,0 @@ -package de.hft.geotracker.data - -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 index 8808497..daab6df 100644 --- a/android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/AuthenticationInterceptor.kt @@ -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) 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 7634199..5cf681c 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 @@ -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 + fun login(@Body login_data: ValuesUserLogin): Call @GET("whoami") - fun getUser() : Call + 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 index 25b9cc2..cb47e5b 100644 --- a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUser.kt @@ -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 : Integer) { +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 diff --git a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt index 76acbd6..5612e6a 100644 --- a/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt @@ -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 diff --git a/android/app/src/main/res/drawable/ic_logo.xml b/android/app/src/main/res/drawable/ic_logo.xml new file mode 100644 index 0000000..dc623bf --- /dev/null +++ b/android/app/src/main/res/drawable/ic_logo.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/android/app/src/main/res/layout/activity_home.xml b/android/app/src/main/res/layout/activity_home.xml index fcfd7e6..12a8837 100644 --- a/android/app/src/main/res/layout/activity_home.xml +++ b/android/app/src/main/res/layout/activity_home.xml @@ -1,6 +1,5 @@ - - - + app:layout_constraintTop_toTopOf="parent"> + + + + + diff --git a/android/app/src/main/res/layout/activity_login.xml b/android/app/src/main/res/layout/activity_login.xml index 16bac8f..52cfba4 100644 --- a/android/app/src/main/res/layout/activity_login.xml +++ b/android/app/src/main/res/layout/activity_login.xml @@ -6,6 +6,21 @@ android:layout_height="match_parent" android:background="@color/background_grey" tools:context=".activities.Login"> + + + + + + + - + android:layout_height="59dp" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp" /> \ No newline at end of file diff --git a/android/app/src/main/res/layout/settings_activity.xml b/android/app/src/main/res/layout/settings_activity.xml index 15fc665..4f4c508 100644 --- a/android/app/src/main/res/layout/settings_activity.xml +++ b/android/app/src/main/res/layout/settings_activity.xml @@ -3,17 +3,49 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:background="@color/background_grey"> + android:background="@color/background_grey" + android:orientation="vertical"> + + + + + + + android:layout_height="match_parent"> + +