From 5d366e3c8b1724237794a5468a968095d96825a4 Mon Sep 17 00:00:00 2001 From: wiecktobi Date: Mon, 18 May 2020 17:06:01 +0200 Subject: [PATCH] Implemented Geofence and updated package structure --- android/app/src/main/AndroidManifest.xml | 10 +-- .../geotracker/GeofenceBroadcastReceiver.kt | 44 ++++++++++++ .../hft/geotracker/{ => activities}/Login.kt | 15 ++-- .../{ => activities}/MainActivity.kt | 68 ++++++++++--------- .../geotracker/{ => activities}/Register.kt | 3 +- .../geotracker/{ => activities}/Settings.kt | 8 ++- .../de/hft/geotracker/{ => data}/JWToken.kt | 2 +- .../{ => retrofit}/GeofenceService.kt | 3 +- .../{ => retrofit}/ValuesUserLogin.kt | 2 +- .../app/src/main/res/layout/activity_home.xml | 3 +- .../src/main/res/layout/activity_login.xml | 2 +- .../app/src/main/res/layout/activity_main.xml | 2 +- .../src/main/res/layout/activity_register.xml | 2 +- .../src/main/res/navigation/navigation.xml | 4 +- 14 files changed, 114 insertions(+), 54 deletions(-) create mode 100644 android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt rename android/app/src/main/java/de/hft/geotracker/{ => activities}/Login.kt (88%) rename android/app/src/main/java/de/hft/geotracker/{ => activities}/MainActivity.kt (71%) rename android/app/src/main/java/de/hft/geotracker/{ => activities}/Register.kt (90%) rename android/app/src/main/java/de/hft/geotracker/{ => activities}/Settings.kt (80%) rename android/app/src/main/java/de/hft/geotracker/{ => data}/JWToken.kt (56%) rename android/app/src/main/java/de/hft/geotracker/{ => retrofit}/GeofenceService.kt (82%) rename android/app/src/main/java/de/hft/geotracker/{ => retrofit}/ValuesUserLogin.kt (86%) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 9ef0dfd..c11df70 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -3,7 +3,6 @@ package="de.hft.geotracker"> - @@ -17,22 +16,23 @@ android:usesCleartextTraffic="true" android:theme="@style/AppTheme"> - + - + - + + \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt b/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt new file mode 100644 index 0000000..cf69800 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/GeofenceBroadcastReceiver.kt @@ -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()) + } + } +} \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/Login.kt b/android/app/src/main/java/de/hft/geotracker/activities/Login.kt similarity index 88% rename from android/app/src/main/java/de/hft/geotracker/Login.kt rename to android/app/src/main/java/de/hft/geotracker/activities/Login.kt index f9e942f..5de4b5b 100644 --- a/android/app/src/main/java/de/hft/geotracker/Login.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Login.kt @@ -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(R.id.setting_input_username).text.toString() val pswd = findViewById(R.id.input_password).text.toString() - var call= service.login(ValuesUserLogin(name, pswd)) + var call= service.login( + ValuesUserLogin( + name, + pswd + ) + ) call.enqueue(object : Callback { override fun onResponse(call: Call?, response: Response?) { @@ -62,7 +70,6 @@ class Login : AppCompatActivity() { token = JWToken(authentication!!) println(response.code()) println(token.token) - startActivity(intent) } else { if (response != null) { diff --git a/android/app/src/main/java/de/hft/geotracker/MainActivity.kt b/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt similarity index 71% rename from android/app/src/main/java/de/hft/geotracker/MainActivity.kt rename to android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt index bbb7442..902a9b9 100644 --- a/android/app/src/main/java/de/hft/geotracker/MainActivity.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/MainActivity.kt @@ -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(R.id.latitude).text = location.latitude.toString() findViewById(R.id.longitude).text = location.longitude.toString() - findViewById(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 = 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(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... diff --git a/android/app/src/main/java/de/hft/geotracker/Register.kt b/android/app/src/main/java/de/hft/geotracker/activities/Register.kt similarity index 90% rename from android/app/src/main/java/de/hft/geotracker/Register.kt rename to android/app/src/main/java/de/hft/geotracker/activities/Register.kt index 96762fa..f2dbecc 100644 --- a/android/app/src/main/java/de/hft/geotracker/Register.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Register.kt @@ -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 diff --git a/android/app/src/main/java/de/hft/geotracker/Settings.kt b/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt similarity index 80% rename from android/app/src/main/java/de/hft/geotracker/Settings.kt rename to android/app/src/main/java/de/hft/geotracker/activities/Settings.kt index d3610b1..c9951bf 100644 --- a/android/app/src/main/java/de/hft/geotracker/Settings.kt +++ b/android/app/src/main/java/de/hft/geotracker/activities/Settings.kt @@ -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) } diff --git a/android/app/src/main/java/de/hft/geotracker/JWToken.kt b/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt similarity index 56% rename from android/app/src/main/java/de/hft/geotracker/JWToken.kt rename to android/app/src/main/java/de/hft/geotracker/data/JWToken.kt index c9af2dd..cebf48a 100644 --- a/android/app/src/main/java/de/hft/geotracker/JWToken.kt +++ b/android/app/src/main/java/de/hft/geotracker/data/JWToken.kt @@ -1,3 +1,3 @@ -package de.hft.geotracker +package de.hft.geotracker.data data class JWToken (var token : String) \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt similarity index 82% rename from android/app/src/main/java/de/hft/geotracker/GeofenceService.kt rename to android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt index c9faeb3..e818e19 100644 --- a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/GeofenceService.kt @@ -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 - } \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt similarity index 86% rename from android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt rename to android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt index 429b9b2..76acbd6 100644 --- a/android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt +++ b/android/app/src/main/java/de/hft/geotracker/retrofit/ValuesUserLogin.kt @@ -1,4 +1,4 @@ -package de.hft.geotracker +package de.hft.geotracker.retrofit import com.google.gson.annotations.SerializedName diff --git a/android/app/src/main/res/layout/activity_home.xml b/android/app/src/main/res/layout/activity_home.xml index 53b5411..9f562dc 100644 --- a/android/app/src/main/res/layout/activity_home.xml +++ b/android/app/src/main/res/layout/activity_home.xml @@ -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"> @@ -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" /> diff --git a/android/app/src/main/res/layout/activity_login.xml b/android/app/src/main/res/layout/activity_login.xml index 44f82d3..16bac8f 100644 --- a/android/app/src/main/res/layout/activity_login.xml +++ b/android/app/src/main/res/layout/activity_login.xml @@ -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"> + tools:context=".activities.MainActivity" > + tools:context=".activities.Register"> \ No newline at end of file