Merge branch '74-disable-start-button' into 'master'

Resolve "Disable start button"

Closes #74

See merge request marcel.schwarz/2020ss-qbc-geofence-timetracking!64
This commit is contained in:
Tobias Wieck 2020-05-23 16:39:27 +00:00
commit 70e3a60930
3 changed files with 87 additions and 29 deletions

View File

@ -4,12 +4,15 @@ 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() {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
val geofencingEvent = GeofencingEvent.fromIntent(intent) val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) { if (geofencingEvent.hasError()) {
@ -23,16 +26,27 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
val geofenceTransition = geofencingEvent.geofenceTransition 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 || if (geofenceTransition == Geofence.GEOFENCE_TRANSITION_ENTER) {
geofenceTransition == Geofence.GEOFENCE_TRANSITION_EXIT) { context!!.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
// MainActivity().showFence() ?.edit()
?.putBoolean("ENABLED", true)
?.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.toString() 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()
val triggeringGeofences = geofencingEvent.triggeringGeofences
val geofenceTransitionDetails = "Transition: $geofenceTransition\nTriggering Geofences: $triggeringGeofences"
println("Success Transition: ") println("Success Transition: ")
Log.i(TAG, geofenceTransitionDetails) Log.i(TAG, geofenceTransitionDetails)
} else { } else {
@ -41,4 +55,5 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
Log.e(TAG, geofenceTransition.toString()) Log.e(TAG, geofenceTransition.toString())
} }
} }
} }

View File

@ -2,6 +2,7 @@ package de.hft.geotracker.activities
import android.Manifest.permission.ACCESS_FINE_LOCATION import android.Manifest.permission.ACCESS_FINE_LOCATION
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Context
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
@ -17,32 +18,58 @@ import androidx.core.app.ActivityCompat.requestPermissions
import com.google.android.gms.location.* import com.google.android.gms.location.*
import de.hft.geotracker.GeofenceBroadcastReceiver import de.hft.geotracker.GeofenceBroadcastReceiver
import de.hft.geotracker.R import de.hft.geotracker.R
import kotlinx.android.synthetic.main.activity_home.*
import kotlin.properties.Delegates
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 geofencingClient: GeofencingClient
lateinit var geofence : Geofence lateinit var geofence: Geofence
lateinit var actionButton : TextView lateinit var actionButton: TextView
var running = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setSupportActionBar(findViewById(R.id.my_toolbar)) setSupportActionBar(findViewById(R.id.my_toolbar))
setContentView(R.layout.activity_home) setContentView(R.layout.activity_home)
this.getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
?.edit()
?.putBoolean("ENABLED", false)
?.apply()
getSharedPreferences("LOCATION", Context.MODE_PRIVATE)
.registerOnSharedPreferenceChangeListener { sharedPreferences, key ->
val btnState = sharedPreferences.getBoolean("ENABLED", false)
println("Buttonstate: $btnState")
if (btnState) {
button_start_stop?.setBackgroundColor(resources.getColor(R.color.logo_blue))
} else {
button_start_stop?.setBackgroundColor(resources.getColor(R.color.colorPrimaryDark))
if (running) {
button_start_stop.toggle()
callStartStop()
}
}
button_start_stop.isEnabled = btnState
}
// val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest) // val builder = LocationSettingsRequest.Builder().addLocationRequest(locationRequest)
createLocationRequest() createLocationRequest()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this) fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) != if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) { PackageManager.PERMISSION_GRANTED
) {
requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000) requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000)
} }
locationCallback = object : LocationCallback() { locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) { override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return locationResult ?: return
for (location in locationResult.locations){ for (location in locationResult.locations) {
// 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()
@ -55,7 +82,7 @@ class MainActivity : AppCompatActivity() {
.setExpirationDuration(Geofence.NEVER_EXPIRE) .setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT) .setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
.build() .build()
geofencingClient?.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run { geofencingClient.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
addOnSuccessListener { addOnSuccessListener {
println("Geofence added") println("Geofence added")
} }
@ -64,10 +91,14 @@ class MainActivity : AppCompatActivity() {
} }
} }
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, ArrayAdapter.createFromResource(
R.array.accounts, android.R.layout.simple_spinner_item).also { adapter -> 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
@ -80,16 +111,22 @@ class MainActivity : AppCompatActivity() {
} }
private fun callStartStop() { private fun callStartStop() {
println("StartStop pressed")
fusedLocationClient.lastLocation running = !running
.addOnSuccessListener { location : Location? -> println("StartStop pressed: $running")
if (location != null) { //ToDO call /track Endpoint
var label = findViewById<TextView>(R.id.display_acc)
label.text = location.latitude.toString()
} else {
println("Location = null")
} }
fun disableButton() {
val btn = findViewById<TextView>(R.id.button_start_stop)
btn.isEnabled = false
btn?.setBackgroundColor(resources.getColor(R.color.colorPrimaryDark))
} }
fun enableButton() {
val btn = findViewById<TextView>(R.id.button_start_stop)
btn.isEnabled = true
btn?.setBackgroundColor(resources.getColor(R.color.logo_blue))
} }
override fun onResume() { override fun onResume() {
@ -98,10 +135,13 @@ class MainActivity : AppCompatActivity() {
} }
private fun startLocationUpdates() { private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(locationRequest, fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback, locationCallback,
Looper.getMainLooper()) Looper.getMainLooper()
)
} }
private fun createLocationRequest() { private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply { locationRequest = LocationRequest.create().apply {
interval = 10000 interval = 10000
@ -109,12 +149,14 @@ class MainActivity : AppCompatActivity() {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY priority = LocationRequest.PRIORITY_HIGH_ACCURACY
} }
} }
private fun getGeofencingRequest() : GeofencingRequest {
private fun getGeofencingRequest(): GeofencingRequest {
return GeofencingRequest.Builder().apply { return GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER) setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
addGeofence(geofence) addGeofence(geofence)
}.build() }.build()
} }
private val geofencePendingIntent: PendingIntent by lazy { private val geofencePendingIntent: PendingIntent by lazy {
val intent = Intent(this, GeofenceBroadcastReceiver::class.java) val intent = Intent(this, GeofenceBroadcastReceiver::class.java)
// We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling // We use FLAG_UPDATE_CURRENT so that we get the same pending intent back when calling

View File

@ -62,8 +62,9 @@
android:layout_marginStart="@dimen/margin16" android:layout_marginStart="@dimen/margin16"
android:layout_marginEnd="@dimen/margin16" android:layout_marginEnd="@dimen/margin16"
android:layout_marginBottom="@dimen/margin16" android:layout_marginBottom="@dimen/margin16"
android:background="@drawable/outlined_button_filled" android:background="@color/colorPrimaryDark"
android:checked="true" android:checked="true"
android:enabled="false"
android:textAppearance="@style/text_style" android:textAppearance="@style/text_style"
android:textOff="@string/stop" android:textOff="@string/stop"
android:textOn="@string/btn_start_text" android:textOn="@string/btn_start_text"