Merge branch '73-implement-geofence' into 'master'

Resolve "Implement geofence"

Closes #73

See merge request marcel.schwarz/2020ss-qbc-geofence-timetracking!55
This commit is contained in:
Tobias Wieck 2020-05-18 15:29:41 +00:00
commit 10a1d3a3fa
14 changed files with 114 additions and 54 deletions

View File

@ -3,7 +3,6 @@
package="de.hft.geotracker">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<!-- Required if your app targets Android 10 (API level 29) or higher -->
@ -17,22 +16,23 @@
android:usesCleartextTraffic="true"
android:theme="@style/AppTheme">
<activity android:name=".Register" />
<activity android:name=".activities.Register" />
<activity
android:name=".Settings"
android:name=".activities.Settings"
android:label="@string/title_activity_settings" />
<activity android:name=".Login">
<activity android:name=".activities.Login">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity">
<activity android:name=".activities.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
<receiver android:name=".GeofenceBroadcastReceiver"/>
</application>
</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.os.Bundle
@ -6,7 +6,10 @@ import androidx.fragment.app.Fragment
import android.widget.TextView
import android.widget.Toast
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.Callback
import retrofit2.Response
@ -51,7 +54,12 @@ 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))
var call= service.login(
ValuesUserLogin(
name,
pswd
)
)
call.enqueue(object : Callback<Void> {
override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
@ -62,7 +70,6 @@ class Login : AppCompatActivity() {
token = JWToken(authentication!!)
println(response.code())
println(token.token)
startActivity(intent)
} else {
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.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Looper
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.Window
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
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 androidx.navigation.findNavController
import androidx.databinding.DataBindingUtil
import androidx.navigation.ui.NavigationUI
import com.google.android.gms.location.*
import com.google.android.material.textfield.TextInputLayout
import java.util.jar.Manifest
import de.hft.geotracker.GeofenceBroadcastReceiver
import de.hft.geotracker.R
class MainActivity : AppCompatActivity() {
lateinit var fusedLocationClient : FusedLocationProviderClient
lateinit var locationRequest : LocationRequest
lateinit var locationCallback: LocationCallback
lateinit var geofencingClient : GeofencingClient
lateinit var geofence : Geofence
lateinit var actionButton : TextView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -51,29 +46,28 @@ class MainActivity : AppCompatActivity() {
// Update UI with location data
findViewById<TextView>(R.id.latitude).text = location.latitude.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)
// val navController = this.findNavController(R.id.HostFragment)
// NavigationUI.setupActionBarWithNavController(this, navController)
// val dropdown : TextInputLayout = findViewById(R.id.filled_exposed_dropdown)
/*val editTextFilledExposedDropdown : AutoCompleteTextView = findViewById(R.id.filled_exposed_dropdown)
ArrayAdapter.createFromResource(this, R.array.accounts, R.layout.spinner_layout).also {
arrayAdapter -> arrayAdapter.setDropDownViewResource(R.layout.spinner_layout)
editTextFilledExposedDropdown.setAdapter(arrayAdapter)
}*/
/*val array = arrayOf("Test1", "Test2")
val a : ArrayAdapter<String> = ArrayAdapter(this, R.layout.spinner_layout, array)
val editTextFilledExposedDropdown : AutoCompleteTextView = findViewById(R.id.filled_exposed_dropdown)
editTextFilledExposedDropdown.setAdapter(a)*/
geofencingClient = LocationServices.getGeofencingClient(this)
geofence = Geofence.Builder().setRequestId("Test")
.setCircularRegion(48.3575, 8.9745, 50F)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
.build()
geofencingClient?.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
addOnSuccessListener {
println("Geofence added")
}
addOnFailureListener {
println("Error: " + it.stackTrace.forEach { println(it.toString()) })
}
}
val spinner: Spinner = findViewById(R.id.account_spinner)
// 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
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
@ -92,7 +86,6 @@ class MainActivity : AppCompatActivity() {
if (location != null) {
var label = findViewById<TextView>(R.id.display_acc)
label.text = location.latitude.toString()
} else {
println("Location = null")
}
@ -109,13 +102,26 @@ class MainActivity : AppCompatActivity() {
locationCallback,
Looper.getMainLooper())
}
fun createLocationRequest() {
private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply {
interval = 10000
fastestInterval = 5000
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) {
R.id.settings -> {
// 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 androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import de.hft.geotracker.R
class Register : AppCompatActivity() {
lateinit var reg : TextView

View File

@ -1,8 +1,9 @@
package de.hft.geotracker
package de.hft.geotracker.activities
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat
import de.hft.geotracker.R
class Settings : AppCompatActivity() {
@ -11,7 +12,10 @@ class Settings : AppCompatActivity() {
setContentView(R.layout.settings_activity)
supportFragmentManager
.beginTransaction()
.replace(R.id.settings, SettingsFragment())
.replace(
R.id.settings,
SettingsFragment()
)
.commit()
supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

View File

@ -1,3 +1,3 @@
package de.hft.geotracker
package de.hft.geotracker.data
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.http.*
@ -8,5 +8,4 @@ interface GeofenceService {
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

View File

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

View File

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

View File

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

View File

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

View File

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