Resolve "Disable start button"

This commit is contained in:
Tobias Wieck 2020-05-23 16:39:27 +00:00
parent 4b2fe41d9f
commit 841a9545e6
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.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() {
override fun onReceive(context: Context?, intent: Intent?) {
val geofencingEvent = GeofencingEvent.fromIntent(intent)
if (geofencingEvent.hasError()) {
@ -23,16 +26,27 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
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()
if (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.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: ")
Log.i(TAG, geofenceTransitionDetails)
} else {
@ -41,4 +55,5 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
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.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
@ -17,6 +18,8 @@ import androidx.core.app.ActivityCompat.requestPermissions
import com.google.android.gms.location.*
import de.hft.geotracker.GeofenceBroadcastReceiver
import de.hft.geotracker.R
import kotlinx.android.synthetic.main.activity_home.*
import kotlin.properties.Delegates
class MainActivity : AppCompatActivity() {
@ -26,17 +29,41 @@ class MainActivity : AppCompatActivity() {
lateinit var geofencingClient: GeofencingClient
lateinit var geofence: Geofence
lateinit var actionButton: TextView
var running = false
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)
?.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)
createLocationRequest()
fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
if (ActivityCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
PackageManager.PERMISSION_GRANTED
) {
requestPermissions(this, arrayOf(ACCESS_FINE_LOCATION), 1000)
}
locationCallback = object : LocationCallback() {
@ -55,7 +82,7 @@ class MainActivity : AppCompatActivity() {
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER or Geofence.GEOFENCE_TRANSITION_EXIT)
.build()
geofencingClient?.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
geofencingClient.addGeofences(getGeofencingRequest(), geofencePendingIntent)?.run {
addOnSuccessListener {
println("Geofence added")
}
@ -64,10 +91,14 @@ class MainActivity : AppCompatActivity() {
}
}
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
@ -80,16 +111,22 @@ class MainActivity : AppCompatActivity() {
}
private fun callStartStop() {
println("StartStop pressed")
fusedLocationClient.lastLocation
.addOnSuccessListener { location : Location? ->
if (location != null) {
var label = findViewById<TextView>(R.id.display_acc)
label.text = location.latitude.toString()
} else {
println("Location = null")
running = !running
println("StartStop pressed: $running")
//ToDO call /track Endpoint
}
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() {
@ -98,10 +135,13 @@ class MainActivity : AppCompatActivity() {
}
private fun startLocationUpdates() {
fusedLocationClient.requestLocationUpdates(locationRequest,
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper())
Looper.getMainLooper()
)
}
private fun createLocationRequest() {
locationRequest = LocationRequest.create().apply {
interval = 10000
@ -109,12 +149,14 @@ class MainActivity : AppCompatActivity() {
priority = LocationRequest.PRIORITY_HIGH_ACCURACY
}
}
private fun getGeofencingRequest(): GeofencingRequest {
return GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER)
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

View File

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