@ -40,12 +40,6 @@ enum class GeofenceTransition(val value: Int) {
}
}
}
}
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 {
class HMG _Geofence {
// https://developer.android.com/training/location/geofencing#java
// https://developer.android.com/training/location/geofencing#java
@ -84,11 +78,42 @@ class HMG_Geofence {
return geoZones _
return geoZones _
}
}
fun register ( geoZones : List < GeoZoneModel > ) {
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 < GeoZoneModel > , completion : ( ( Boolean , java . lang . Exception ? ) -> Unit ) ? = null ) {
if ( geoZones . isEmpty ( ) )
if ( geoZones . isEmpty ( ) )
return
return
var geoZones _ = limitize ( geoZones )
va l geoZones _ = limitize ( geoZones )
fun buildGeofencingRequest ( geofences : List < Geofence > ) : GeofencingRequest {
fun buildGeofencingRequest ( geofences : List < Geofence > ) : GeofencingRequest {
return GeofencingRequest . Builder ( )
return GeofencingRequest . Builder ( )
@ -114,42 +139,27 @@ class HMG_Geofence {
. addOnSuccessListener {
. addOnSuccessListener {
Logs . RegisterGeofence . save ( context , " SUCCESS " , " Successfuly registered the geofences " , Logs . STATUS . SUCCESS )
Logs . RegisterGeofence . save ( context , " SUCCESS " , " Successfuly registered the geofences " , Logs . STATUS . SUCCESS )
saveActiveGeofence ( geofences . map { it . requestId } , listOf ( ) )
saveActiveGeofence ( geofences . map { it . requestId } , listOf ( ) )
completion ?. let { it ( true , null ) }
}
}
. addOnFailureListener {
. addOnFailureListener { exc ->
Logs . RegisterGeofence . save ( context , " FAILED_TO_REGISTER " , " Failed to register geofence " , Logs . STATUS . ERROR )
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])
// 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 . TriggerInterval Millis )
HMGUtils . scheduleJob ( context , ReregisterGeofenceJobService :: class . java , ReregisterGeofenceJobService . JobID , ReregisterGeofenceJobService . TriggerInterval Duration )
}
}
} , null )
} , null )
}
}
fun unRegisterAll ( completion : ( status : Boolean , exception : Exception ? ) -> Unit ) {
fun getGeoZonesFromPreference ( context : Context ) : List < GeoZoneModel > {
getActiveGeofences ( { success ->
val pref = context . getSharedPreferences ( PREFS _STORAGE , Context . MODE _PRIVATE )
val mList = success . toMutableList ( )
val json = pref . getString ( PREF _KEY _HMG _ZONES , " [] " )
removeActiveGeofences ( )
geofencingClient
. removeGeofences ( success )
. addOnSuccessListener {
completion ( true , null )
}
. addOnFailureListener {
completion ( false , it )
saveLog ( context , " error:REMOVE_GEOFENCES " , it . localizedMessage )
}
} , { failed ->
// Nothing to do with failed geofences.
} )
}
fun reRegister ( ) {
val geoZones = GeoZoneModel ( ) . listFrom ( json )
unRegisterAll { status , exception ->
return geoZones
val geoZones = HMGUtils . getGeoZonesFromPreference ( context )
register ( geoZones )
}
}
}
fun saveActiveGeofence ( success : List < String > , failed : List < String > ) {
fun saveActiveGeofence ( success : List < String > , failed : List < String > ) {