Implemented Geofence and updated package structure

This commit is contained in:
wiecktobi 2020-05-18 17:06:01 +02:00
parent 4c34ac3497
commit 5d366e3c8b
14 changed files with 114 additions and 54 deletions

View File

@ -3,7 +3,6 @@
package="de.hft.geotracker"> package="de.hft.geotracker">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Required if your app targets Android 10 (API level 29) or higher --> <!-- Required if your app targets Android 10 (API level 29) or higher -->
@ -17,22 +16,23 @@
android:usesCleartextTraffic="true" android:usesCleartextTraffic="true"
android:theme="@style/AppTheme"> android:theme="@style/AppTheme">
<activity android:name=".Register" /> <activity android:name=".activities.Register" />
<activity <activity
android:name=".Settings" android:name=".activities.Settings"
android:label="@string/title_activity_settings" /> android:label="@string/title_activity_settings" />
<activity android:name=".Login"> <activity android:name=".activities.Login">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".MainActivity"> <activity android:name=".activities.MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
</intent-filter> </intent-filter>
</activity> </activity>
<receiver android:name=".GeofenceBroadcastReceiver"/>
</application> </application>
</manifest> </manifest>

View File

@ -0,0 +1,44 @@
package de.hft.geotracker
import android.content.BroadcastReceiver
import android.content.ContentValues.TAG
import android.content.Context
import android.content.Intent
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
class GeofenceBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) {
val errorMessage = GeofenceStatusCodes.getStatusCodeString(geofencingEvent.errorCode)
println("Event error")
Log.e(TAG, errorMessage)
return
}
// Get the transition type.
val geofenceTransition = geofencingEvent.geofenceTransition
// Test that the reported transition was of interest.
if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER ||
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) {
// MainActivity().showFence()
// 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.toString()
println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails)
} else {
// Log the error.
println("Error Transition: ")
Log.e(TAG, geofenceTransition.toString())
}
}
}

View File

@ -1,4 +1,4 @@
package de.hft.geotracker package de.hft.geotracker.activities
import android.content.Intent import android.content.Intent
import android.os.Bundle import android.os.Bundle
@ -6,7 +6,10 @@ 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 okhttp3.HttpUrl import de.hft.geotracker.*
import de.hft.geotracker.data.JWToken
import de.hft.geotracker.retrofit.GeofenceService
import de.hft.geotracker.retrofit.ValuesUserLogin
import retrofit2.Call import retrofit2.Call
import retrofit2.Callback import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
@ -51,7 +54,12 @@ 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(ValuesUserLogin(name, pswd)) var call= service.login(
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>?) {
@ -62,7 +70,6 @@ class Login : AppCompatActivity() {
token = JWToken(authentication!!) token = JWToken(authentication!!)
println(response.code()) println(response.code())
println(token.token) println(token.token)
startActivity(intent) startActivity(intent)
} else { } else {
if (response != null) { if (response != null) {

View File

@ -1,35 +1,30 @@
package de.hft.geotracker package de.hft.geotracker.activities
import android.Manifest.permission.ACCESS_FINE_LOCATION import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.annotation.SuppressLint import android.app.PendingIntent
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager import android.content.pm.PackageManager
import android.location.Location import android.location.Location
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.os.Looper import android.os.Looper
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.Window
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Spinner import android.widget.Spinner
import android.widget.TextView import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityCompat.requestPermissions import androidx.core.app.ActivityCompat.requestPermissions
import androidx.navigation.findNavController
import androidx.databinding.DataBindingUtil
import androidx.navigation.ui.NavigationUI
import com.google.android.gms.location.* import com.google.android.gms.location.*
import com.google.android.material.textfield.TextInputLayout import de.hft.geotracker.GeofenceBroadcastReceiver
import java.util.jar.Manifest import de.hft.geotracker.R
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
lateinit var fusedLocationClient : FusedLocationProviderClient lateinit var fusedLocationClient : FusedLocationProviderClient
lateinit var locationRequest : LocationRequest lateinit var locationRequest : LocationRequest
lateinit var locationCallback: LocationCallback lateinit var locationCallback: LocationCallback
lateinit var geofencingClient : GeofencingClient
lateinit var geofence : Geofence
lateinit var actionButton : TextView lateinit var actionButton : TextView
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -51,29 +46,28 @@ class MainActivity : AppCompatActivity() {
// Update UI with location data // Update UI with location data
findViewById<TextView>(R.id.latitude).text = location.latitude.toString() findViewById<TextView>(R.id.latitude).text = location.latitude.toString()
findViewById<TextView>(R.id.longitude).text = location.longitude.toString() findViewById<TextView>(R.id.longitude).text = location.longitude.toString()
findViewById<TextView>(R.id.altitude).text = location.altitude.toString()
} }
} }
} }
// val binding = DataBindingUtil.setContentView(this, R.layout.activity_main) geofencingClient = LocationServices.getGeofencingClient(this)
// val navController = this.findNavController(R.id.HostFragment) geofence = Geofence.Builder().setRequestId("Test")
// NavigationUI.setupActionBarWithNavController(this, navController) .setCircularRegion(48.3575, 8.9745, 50F)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
// val dropdown : TextInputLayout = findViewById(R.id.filled_exposed_dropdown) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
/*val editTextFilledExposedDropdown : AutoCompleteTextView = findViewById(R.id.filled_exposed_dropdown) .build()
ArrayAdapter.createFromResource(this, R.array.accounts, R.layout.spinner_layout).also { geofencingClient?.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
arrayAdapter -> arrayAdapter.setDropDownViewResource(R.layout.spinner_layout) addOnSuccessListener {
editTextFilledExposedDropdown.setAdapter(arrayAdapter) println("Geofence added")
}*/ }
addOnFailureListener {
/*val array = arrayOf("Test1", "Test2") println("Error: " + it.stackTrace.forEach { println(it.toString()) })
val a : ArrayAdapter<String> = ArrayAdapter(this, R.layout.spinner_layout, array) }
val editTextFilledExposedDropdown : AutoCompleteTextView = findViewById(R.id.filled_exposed_dropdown) }
editTextFilledExposedDropdown.setAdapter(a)*/
val spinner: Spinner = findViewById(R.id.account_spinner) val spinner: Spinner = findViewById(R.id.account_spinner)
// Create an ArrayAdapter using the string array and a default spinner layout // Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter.createFromResource(this, R.array.accounts, android.R.layout.simple_spinner_item).also { adapter -> ArrayAdapter.createFromResource(this,
R.array.accounts, android.R.layout.simple_spinner_item).also { adapter ->
// Specify the layout to use when the list of choices appears // Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner // Apply the adapter to the spinner
@ -92,7 +86,6 @@ class MainActivity : AppCompatActivity() {
if (location != null) { if (location != null) {
var label = findViewById<TextView>(R.id.display_acc) var label = findViewById<TextView>(R.id.display_acc)
label.text = location.latitude.toString() label.text = location.latitude.toString()
} else { } else {
println("Location = null") println("Location = null")
} }
@ -109,13 +102,26 @@ class MainActivity : AppCompatActivity() {
locationCallback, locationCallback,
Looper.getMainLooper()) Looper.getMainLooper())
} }
fun createLocationRequest() { private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply { locationRequest = LocationRequest.create().apply {
interval = 10000 interval = 10000
fastestInterval = 5000 fastestInterval = 5000
priority = LocationRequest.PRIORITY_HIGH_ACCURACY priority = LocationRequest.PRIORITY_HIGH_ACCURACY
} }
} }
private fun getGeofencingRequest() : GeofencingRequest {
return GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
addGeofence(geofence)
}.build()
}
private val geofencePendingIntent: PendingIntent by lazy {
val intent = Intent(this, GeofenceBroadcastReceiver::class.java)
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling
// addGeofences() and removeGeofences().
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) { override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
R.id.settings -> { R.id.settings -> {
// User chose the "Settings" item, show the app settings UI... // User chose the "Settings" item, show the app settings UI...

View File

@ -1,9 +1,10 @@
package de.hft.geotracker package de.hft.geotracker.activities
import android.content.Intent import android.content.Intent
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.widget.TextView import android.widget.TextView
import de.hft.geotracker.R
class Register : AppCompatActivity() { class Register : AppCompatActivity() {
lateinit var reg : TextView lateinit var reg : TextView

View File

@ -1,8 +1,9 @@
package de.hft.geotracker 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 androidx.preference.PreferenceFragmentCompat
import de.hft.geotracker.R
class Settings : AppCompatActivity() { class Settings : AppCompatActivity() {
@ -11,7 +12,10 @@ class Settings : AppCompatActivity() {
setContentView(R.layout.settings_activity) setContentView(R.layout.settings_activity)
supportFragmentManager supportFragmentManager
.beginTransaction() .beginTransaction()
.replace(R.id.settings, SettingsFragment()) .replace(
R.id.settings,
SettingsFragment()
)
.commit() .commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
} }

View File

@ -1,3 +1,3 @@
package de.hft.geotracker package de.hft.geotracker.data
data class JWToken (var token : String) data class JWToken (var token : String)

View File

@ -1,4 +1,4 @@
package de.hft.geotracker package de.hft.geotracker.retrofit
import retrofit2.Call import retrofit2.Call
import retrofit2.http.* import retrofit2.http.*
@ -8,5 +8,4 @@ interface GeofenceService {
fun login(@Body login_data : ValuesUserLogin) : Call<Void> fun login(@Body login_data : ValuesUserLogin) : Call<Void>
} }

View File

@ -1,4 +1,4 @@
package de.hft.geotracker package de.hft.geotracker.retrofit
import com.google.gson.annotations.SerializedName import com.google.gson.annotations.SerializedName

View File

@ -7,7 +7,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background_grey" android:background="@color/background_grey"
tools:context=".MainActivity"> tools:context=".activities.MainActivity">
<!-- TODO: Update blank fragment layout --> <!-- TODO: Update blank fragment layout -->
@ -89,7 +89,6 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="16dp" android:layout_marginTop="16dp"
android:text="dummy"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/longitude" /> app:layout_constraintTop_toBottomOf="@+id/longitude" />

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background_grey" android:background="@color/background_grey"
tools:context=".Login"> tools:context=".activities.Login">
<EditText <EditText
android:id="@+id/setting_input_username" android:id="@+id/setting_input_username"

View File

@ -6,7 +6,7 @@
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:background="@color/background_grey" android:background="@color/background_grey"
tools:context=".MainActivity" > tools:context=".activities.MainActivity" >
<fragment <fragment
android:id="@+id/HostFragment" android:id="@+id/HostFragment"

View File

@ -5,7 +5,7 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:background="@color/background_grey" android:background="@color/background_grey"
tools:context=".Register"> tools:context=".activities.Register">
<EditText <EditText
android:id="@+id/input_password2" android:id="@+id/input_password2"

View File

@ -7,12 +7,12 @@
<fragment <fragment
android:id="@+id/login" android:id="@+id/login"
android:name="de.hft.geotracker.Login" android:name="de.hft.geotracker.activities.Login"
android:label="fragment_login" android:label="fragment_login"
tools:layout="@layout/activity_login" /> tools:layout="@layout/activity_login" />
<activity <activity
android:id="@+id/mainActivity" android:id="@+id/mainActivity"
android:name="de.hft.geotracker.MainActivity" android:name="de.hft.geotracker.activities.MainActivity"
android:label="activity_home" android:label="activity_home"
tools:layout="@layout/activity_home" /> tools:layout="@layout/activity_home" />
</navigation> </navigation>