|
|
|
@ -6,7 +6,9 @@ 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.utils.*
|
|
|
|
|
import com.google.android.gms.location.Geofence
|
|
|
|
|
import com.google.android.gms.location.GeofencingClient
|
|
|
|
|
import com.google.android.gms.location.GeofencingRequest
|
|
|
|
@ -37,6 +39,7 @@ 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"
|
|
|
|
|
const val PREF_KEY_LANGUAGE = "flutter.language"
|
|
|
|
|
|
|
|
|
|
class HMG_Geofence {
|
|
|
|
|
// https://developer.android.com/training/location/geofencing#java
|
|
|
|
@ -99,6 +102,7 @@ class HMG_Geofence {
|
|
|
|
|
}
|
|
|
|
|
.addOnFailureListener {
|
|
|
|
|
print(it.localizedMessage)
|
|
|
|
|
saveLog(context,"error:ADD_GEOFENCES", it.localizedMessage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},null)
|
|
|
|
@ -107,7 +111,6 @@ class HMG_Geofence {
|
|
|
|
|
fun unRegisterAll(completion: (status: Boolean, exception:Exception?) -> Unit){
|
|
|
|
|
getActiveGeofences({ success ->
|
|
|
|
|
val mList = success.toMutableList()
|
|
|
|
|
mList.add("12345")
|
|
|
|
|
geofencingClient
|
|
|
|
|
.removeGeofences(success)
|
|
|
|
|
.addOnSuccessListener {
|
|
|
|
@ -115,6 +118,7 @@ class HMG_Geofence {
|
|
|
|
|
}
|
|
|
|
|
.addOnFailureListener {
|
|
|
|
|
completion(false, it)
|
|
|
|
|
saveLog(context,"error:REMOVE_GEOFENCES", it.localizedMessage)
|
|
|
|
|
}
|
|
|
|
|
removeActiveGeofences()
|
|
|
|
|
}, { failed ->
|
|
|
|
@ -154,7 +158,10 @@ 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<Map<String?, Any?>?>() {}.type
|
|
|
|
|
return gson.fromJson<Map<String?, Any?>?>(profileJson,type)
|
|
|
|
|
?.get("PatientID")
|
|
|
|
@ -162,4 +169,34 @@ class HMG_Geofence {
|
|
|
|
|
.toDoubleOrNull()
|
|
|
|
|
?.toInt()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fun handleEvent(triggerGeofences: List<Geofence>, location: Location, transition:GeofenceTransition) {
|
|
|
|
|
getPatientID()?.let { patientId ->
|
|
|
|
|
getActiveGeofences({ activeGeofences ->
|
|
|
|
|
|
|
|
|
|
triggerGeofences.forEach { geofence ->
|
|
|
|
|
// Extract PointID from 'geofence.requestId' and find from active geofences
|
|
|
|
|
val pointID = activeGeofences.firstOrNull {it == geofence.requestId}?.split('_')?.first()
|
|
|
|
|
if(!pointID.isNullOrEmpty() && pointID.toIntOrNull() != null){
|
|
|
|
|
|
|
|
|
|
val body = mutableMapOf<String,Any?>(
|
|
|
|
|
"PointsID" to pointID.toIntOrNull(),
|
|
|
|
|
"GeoType" to transition.value,
|
|
|
|
|
"PatientID" to patientId
|
|
|
|
|
)
|
|
|
|
|
body.putAll(HMGUtils.defaultHTTPParams(context))
|
|
|
|
|
|
|
|
|
|
httpPost<Map<String,Any>>(API.LOG_GEOFENCE, body, { response ->
|
|
|
|
|
sendNotification(context, transition.named(), geofence.requestId, "Notified to server.😎")
|
|
|
|
|
},{ exception ->
|
|
|
|
|
sendNotification(context, transition.named(), geofence.requestId, "Failed to notify server.😔")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
},null)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|