diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml
index 1032b29b..79bc3dc4 100644
--- a/android/app/src/main/AndroidManifest.xml
+++ b/android/app/src/main/AndroidManifest.xml
@@ -76,13 +76,21 @@
-
-
-
+
+
+
+
+
+
+
+
+
+
+
diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/MainActivity.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/MainActivity.kt
index 8b73b0c7..e67f646f 100644
--- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/MainActivity.kt
+++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/MainActivity.kt
@@ -2,8 +2,7 @@ package com.cloud.diplomaticquarterapp
import android.os.Bundle
import android.util.Log
import androidx.annotation.NonNull;
-import com.cloud.diplomaticquarterapp.utils.FlutterText
-import com.cloud.diplomaticquarterapp.utils.PlatformBridge
+import com.cloud.diplomaticquarterapp.utils.*
import io.flutter.embedding.android.FlutterFragmentActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel
@@ -15,6 +14,16 @@ class MainActivity: FlutterFragmentActivity() {
// Create Flutter Platform Bridge
PlatformBridge(flutterEngine.dartExecutor.binaryMessenger, this).create()
+ val time = timeToMillis("04:00:00", "HH:mm:ss")
+ print(time)
+
+// val d1 = Logs.list(this)
+// val d2 = Logs.raw(this)
+// val d3 = Logs.RegisterGeofence.list(this)
+// val d4 = Logs.RegisterGeofence.raw(this)
+// val d5 = Logs.GeofenceEvent.list(this)
+// val d6 = Logs.GeofenceEvent.raw(this)
+ print("")
}
override fun onResume() {
diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeoZoneModel.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeoZoneModel.kt
index 7eba1ead..b3fb4f56 100644
--- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeoZoneModel.kt
+++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeoZoneModel.kt
@@ -37,6 +37,7 @@ class GeoZoneModel {
val rad = Radius.toFloat()
if(lat != null && long != null){
+ val loiteringDelayMinutes:Int = 2 // in Minutes
return Geofence.Builder()
.setRequestId(identifier())
.setCircularRegion(
@@ -45,7 +46,8 @@ class GeoZoneModel {
rad
)
.setTransitionTypes(GeofenceTransition.ENTER_EXIT.value)
-// .setNotificationResponsiveness(0)
+ .setNotificationResponsiveness(0)
+ .setLoiteringDelay(loiteringDelayMinutes * 60 * 1000)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.build()
}
diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt
deleted file mode 100644
index 8fc1faae..00000000
--- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/GeofenceBroadcastReceiver.kt
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-package com.cloud.diplomaticquarterapp.geofence
-
-import android.content.BroadcastReceiver
-import android.content.Context
-import android.content.Intent
-
-class GeofenceBroadcastReceiver : BroadcastReceiver() {
- override fun onReceive(context: Context, intent: Intent) {
- GeofenceTransitionsJobIntentService.enqueueWork(context, intent)
- }
-}
\ No newline at end of file
diff --git a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt
index 4d2c48b3..840075eb 100644
--- a/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt
+++ b/android/app/src/main/kotlin/com/cloud/diplomaticquarterapp/geofence/HMG_Geofence.kt
@@ -6,7 +6,11 @@ import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
+import android.location.Location
import androidx.core.content.ContextCompat
+import com.cloud.diplomaticquarterapp.geofence.intent_receivers.GeofenceBroadcastReceiver
+import com.cloud.diplomaticquarterapp.geofence.intent_receivers.ReregisterGeofenceJobService
+import com.cloud.diplomaticquarterapp.utils.*
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
@@ -17,8 +21,10 @@ import com.google.gson.reflect.TypeToken
enum class GeofenceTransition(val value: Int) {
ENTER(1),
EXIT(2),
+ DWELL(4),
+
ENTER_EXIT((ENTER.value or EXIT.value)),
- DWELL(4);
+ DWELL_EXIT((DWELL.value or EXIT.value));
companion object {
fun fromInt(value: Int) = GeofenceTransition.values().first { it.value == value }
@@ -27,17 +33,13 @@ enum class GeofenceTransition(val value: Int) {
fun named():String{
if (value == 1)return "Enter"
if (value == 2)return "Exit"
- if (value == (ENTER.value or EXIT.value))return "Enter or Exit"
if (value == 4)return "dWell"
+ if (value == (ENTER.value or EXIT.value))return "Enter or Exit"
+ if (value == (DWELL.value or EXIT.value))return "DWell or Exit"
return "unknown"
}
}
-const val PREFS_STORAGE = "FlutterSharedPreferences"
-const val PREF_KEY_SUCCESS = "HMG_GEOFENCE_SUCCESS"
-const val PREF_KEY_FAILED = "HMG_GEOFENCE_FAILED"
-const val PREF_KEY_HMG_ZONES = "flutter.hmg-geo-fences"
-
class HMG_Geofence {
// https://developer.android.com/training/location/geofencing#java
@@ -69,13 +71,53 @@ class HMG_Geofence {
}
}
- fun register(geoZones: List){
+ fun limitize(zones: List):List{
+ var geoZones_ = zones
+ if(zones.size > 100)
+ geoZones_ = zones.subList(0, 99)
+ return geoZones_
+ }
+
+
+ fun register(completion:((Boolean, java.lang.Exception?)->Unit)){
+ unRegisterAll { status, exception ->
+ val geoZones = getGeoZonesFromPreference(context)
+ doRegister(geoZones){ status_, error ->
+ completion.let { it(status_, error) }
+ }
+ }
+ }
+
+ fun unRegisterAll(completion: (status: Boolean, exception: Exception?) -> Unit){
+ getActiveGeofences({ success ->
+ removeActiveGeofences()
+ if(success.isNotEmpty())
+ geofencingClient
+ .removeGeofences(success)
+ .addOnSuccessListener {
+ completion(true, null)
+ }
+ .addOnFailureListener {
+ completion(false, it)
+ saveLog(context, "error:REMOVE_GEOFENCES", it.localizedMessage)
+ }
+ else
+ completion(true, null)
+
+ }, { failed ->
+ // Nothing to do with failed geofences.
+ })
+ }
+
+ private fun doRegister(geoZones: List, completion:((Boolean, java.lang.Exception?)->Unit)? = null){
if (geoZones.isEmpty())
return
+ val geoZones_ = limitize(geoZones)
+
fun buildGeofencingRequest(geofences: List): GeofencingRequest {
return GeofencingRequest.Builder()
- .setInitialTrigger(0)
+ .setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL)
.addGeofences(geofences)
.build()
}
@@ -83,9 +125,9 @@ class HMG_Geofence {
getActiveGeofences({ active ->
val geofences = mutableListOf()
- geoZones.forEach {
- it.toGeofence()?.let { geof ->
- if(!active.contains(geof.requestId)){ // if not already registered then register
+ geoZones_.forEach {
+ it.toGeofence()?.let { geof ->
+ if (!active.contains(geof.requestId)) { // if not already registered then register
geofences.add(geof)
}
}
@@ -95,31 +137,29 @@ class HMG_Geofence {
geofencingClient
.addGeofences(buildGeofencingRequest(geofences), geofencePendingIntent)
.addOnSuccessListener {
+ Logs.RegisterGeofence.save(context,"SUCCESS", "Successfuly registered the geofences", Logs.STATUS.SUCCESS)
saveActiveGeofence(geofences.map { it.requestId }, listOf())
+ completion?.let { it(true,null) }
}
- .addOnFailureListener {
- print(it.localizedMessage)
+ .addOnFailureListener { exc ->
+ Logs.RegisterGeofence.save(context,"FAILED_TO_REGISTER", "Failed to register geofence",Logs.STATUS.ERROR)
+ completion?.let { it(false,exc) }
}
+
+ // Schedule the job to register after specified duration (due to: events not calling after long period.. days or days [Needs to register fences again])
+ HMGUtils.scheduleJob(context, ReregisterGeofenceJobService::class.java,ReregisterGeofenceJobService.JobID, ReregisterGeofenceJobService.TriggerIntervalDuration)
}
- },null)
+
+ }, null)
+
}
- fun unRegisterAll(completion: (status: Boolean, exception:Exception?) -> Unit){
- getActiveGeofences({ success ->
- val mList = success.toMutableList()
- mList.add("12345")
- geofencingClient
- .removeGeofences(success)
- .addOnSuccessListener {
- completion(true, null)
- }
- .addOnFailureListener {
- completion(false, it)
- }
- removeActiveGeofences()
- }, { failed ->
- // Nothing to do with failed geofences.
- })
+ fun getGeoZonesFromPreference(context: Context):List{
+ val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
+ val json = pref.getString(PREF_KEY_HMG_ZONES, "[]")
+
+ val geoZones = GeoZoneModel().listFrom(json!!)
+ return geoZones
}
fun saveActiveGeofence(success: List, failed: List){
@@ -130,8 +170,8 @@ class HMG_Geofence {
}
fun removeActiveGeofences(){
- preferences.edit().putString(PREF_KEY_SUCCESS,"[]").apply()
- preferences.edit().putString(PREF_KEY_FAILED,"[]").apply()
+ preferences.edit().putString(PREF_KEY_SUCCESS, "[]").apply()
+ preferences.edit().putString(PREF_KEY_FAILED, "[]").apply()
}
fun getActiveGeofences(success: (success: List) -> Unit, failure: ((failed: List) -> Unit)?){
@@ -154,12 +194,48 @@ class HMG_Geofence {
}
fun getPatientID():Int?{
- val profileJson = preferences.getString("flutter.imei-user-data", "{}")
+ var profileJson = preferences.getString("flutter.imei-user-data", null)
+ if (profileJson == null)
+ profileJson = preferences.getString("flutter.user-profile", null)
+
val type = object : TypeToken