no message

merge-requests/236/head
Zohaib Kambrani 4 years ago
parent 57925e3b02
commit f03adb37ce

@ -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) val 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.TriggerIntervalMillis) HMGUtils.scheduleJob(context, ReregisterGeofenceJobService::class.java,ReregisterGeofenceJobService.JobID, ReregisterGeofenceJobService.TriggerIntervalDuration)
} }
}, 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>){

@ -9,7 +9,6 @@ import android.util.Log
import com.cloud.diplomaticquarterapp.geofence.GeofenceTransition import com.cloud.diplomaticquarterapp.geofence.GeofenceTransition
import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence
import com.cloud.diplomaticquarterapp.utils.Logs import com.cloud.diplomaticquarterapp.utils.Logs
import com.cloud.diplomaticquarterapp.utils.saveLog
import com.google.android.gms.location.GeofenceStatusCodes import com.google.android.gms.location.GeofenceStatusCodes
import com.google.android.gms.location.GeofencingEvent import com.google.android.gms.location.GeofencingEvent
@ -42,7 +41,9 @@ class GeofenceBroadcastReceiver : BroadcastReceiver() {
) )
if(errorRequiredReregister.contains(errorCode)) if(errorRequiredReregister.contains(errorCode))
HMG_Geofence.shared(context).reRegister() HMG_Geofence.shared(context).register(){ status, error ->
}
} }
} }

@ -38,7 +38,6 @@ import androidx.core.app.JobIntentService
import com.cloud.diplomaticquarterapp.geofence.GeofenceTransition import com.cloud.diplomaticquarterapp.geofence.GeofenceTransition
import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence
import com.cloud.diplomaticquarterapp.utils.saveLog import com.cloud.diplomaticquarterapp.utils.saveLog
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofenceStatusCodes import com.google.android.gms.location.GeofenceStatusCodes
import com.google.android.gms.location.GeofencingEvent import com.google.android.gms.location.GeofencingEvent
@ -85,7 +84,7 @@ class GeofenceTransitionsJobIntentService : JobIntentService() {
) )
if(errorRequiredReregister.contains(errorCode)) if(errorRequiredReregister.contains(errorCode))
HMG_Geofence.shared(context).reRegister() HMG_Geofence.shared(context).register(){ status, exc -> }
} }
} }

@ -6,7 +6,7 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence
import com.cloud.diplomaticquarterapp.geofence.PREFS_STORAGE import com.cloud.diplomaticquarterapp.utils.PREFS_STORAGE
class GeofencingRebootBroadcastReceiver : BroadcastReceiver(){ class GeofencingRebootBroadcastReceiver : BroadcastReceiver(){
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
@ -16,7 +16,7 @@ class GeofencingRebootBroadcastReceiver : BroadcastReceiver(){
val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE) val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
pref.edit().putString("REBOOT_DETECTED","YES").apply() pref.edit().putString("REBOOT_DETECTED","YES").apply()
HMG_Geofence.shared(context).reRegister() HMG_Geofence.shared(context).register(){ status, error -> }
} }
} }

@ -6,13 +6,9 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.location.LocationManager import android.location.LocationManager
import android.util.Log
import com.cloud.diplomaticquarterapp.geofence.GeofenceTransition
import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence
import com.cloud.diplomaticquarterapp.geofence.PREFS_STORAGE
import com.cloud.diplomaticquarterapp.utils.HMGUtils import com.cloud.diplomaticquarterapp.utils.HMGUtils
import com.cloud.diplomaticquarterapp.utils.saveLog import com.cloud.diplomaticquarterapp.utils.PREFS_STORAGE
import com.google.android.gms.location.GeofencingEvent
class LocationProviderChangeReceiver : BroadcastReceiver() { class LocationProviderChangeReceiver : BroadcastReceiver() {
private val LOG_TAG = "LocationProviderChangeReceiver" private val LOG_TAG = "LocationProviderChangeReceiver"
@ -22,10 +18,7 @@ class LocationProviderChangeReceiver : BroadcastReceiver() {
val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE) val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
pref.edit().putString("LOCATION_PROVIDER_CHANGE","YES").apply() pref.edit().putString("LOCATION_PROVIDER_CHANGE","YES").apply()
HMG_Geofence.shared(context).unRegisterAll { status, exception -> HMG_Geofence.shared(context).register(){ s, e -> }
val geoZones = HMGUtils.getGeoZonesFromPreference(context)
HMG_Geofence.shared(context).register(geoZones)
}
} }
} }

@ -4,16 +4,17 @@ import android.app.job.JobParameters
import android.app.job.JobService import android.app.job.JobService
import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence import com.cloud.diplomaticquarterapp.geofence.HMG_Geofence
import com.cloud.diplomaticquarterapp.utils.Logs import com.cloud.diplomaticquarterapp.utils.Logs
import com.cloud.diplomaticquarterapp.utils.timeToMillis
class ReregisterGeofenceJobService : JobService(){ class ReregisterGeofenceJobService : JobService(){
companion object{ companion object{
val TriggerIntervalMillis:String = "24:00:00" val TriggerIntervalDuration:String = "06:00:00"
val JobID = 918273 val JobID = 918273
} }
override fun onStartJob(params: JobParameters?): Boolean { override fun onStartJob(params: JobParameters?): Boolean {
HMG_Geofence.shared(applicationContext).reRegister() Logs.save(applicationContext,"ReregisterGeofenceJobService.onStartJob", "triggered to re-register the geofences after $TriggerIntervalDuration >> [HH:mm:ss]")
Logs.save(applicationContext,"ReregisterGeofenceJobService.onStartJob", "triggered to re-register the geofences after $TriggerIntervalMillis >> [HH:mm:ss]") HMG_Geofence.shared(applicationContext).register(){ status, error ->
jobFinished(params, true)
}
return true return true
} }

@ -0,0 +1,8 @@
package com.cloud.diplomaticquarterapp.utils
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"

@ -11,17 +11,11 @@ import android.content.Intent
import android.os.Build import android.os.Build
import android.widget.Toast import android.widget.Toast
import androidx.annotation.Nullable import androidx.annotation.Nullable
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.TaskStackBuilder import androidx.core.app.TaskStackBuilder
import com.cloud.diplomaticquarterapp.BuildConfig import com.cloud.diplomaticquarterapp.BuildConfig
import com.cloud.diplomaticquarterapp.MainActivity import com.cloud.diplomaticquarterapp.MainActivity
import com.cloud.diplomaticquarterapp.R import com.cloud.diplomaticquarterapp.R
import com.cloud.diplomaticquarterapp.geofence.GeoZoneModel
import com.cloud.diplomaticquarterapp.geofence.PREFS_STORAGE
import com.cloud.diplomaticquarterapp.geofence.PREF_KEY_HMG_ZONES
import com.cloud.diplomaticquarterapp.geofence.PREF_KEY_LANGUAGE
import com.cloud.diplomaticquarterapp.geofence.intent_receivers.ReregisterGeofenceJobService
import com.github.kittinunf.fuel.core.extensions.jsonBody import com.github.kittinunf.fuel.core.extensions.jsonBody
import com.github.kittinunf.fuel.httpPost import com.github.kittinunf.fuel.httpPost
import com.google.gson.Gson import com.google.gson.Gson
@ -75,14 +69,6 @@ class HMGUtils {
} }
} }
fun getGeoZonesFromPreference(context: Context):List<GeoZoneModel>{
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 getLanguageCode(context: Context) : Int{ fun getLanguageCode(context: Context) : Int{
val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE) val pref = context.getSharedPreferences(PREFS_STORAGE, Context.MODE_PRIVATE)
val lang = pref.getString(PREF_KEY_LANGUAGE, "ar") val lang = pref.getString(PREF_KEY_LANGUAGE, "ar")
@ -111,6 +97,7 @@ class HMGUtils {
val serviceComponent = ComponentName(context, pendingIntentClassType) val serviceComponent = ComponentName(context, pendingIntentClassType)
val builder = JobInfo.Builder(jobId, serviceComponent) val builder = JobInfo.Builder(jobId, serviceComponent)
builder.setPersisted(true) builder.setPersisted(true)
builder.setBackoffCriteria(30000, JobInfo.BACKOFF_POLICY_LINEAR)
val intervalMillis = timeToMillis(intervalDuration,"HH:mm:ss") val intervalMillis = timeToMillis(intervalDuration,"HH:mm:ss")
builder.setMinimumLatency(intervalMillis) // wait at least builder.setMinimumLatency(intervalMillis) // wait at least

@ -4,7 +4,6 @@ import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.os.Build import android.os.Build
import com.cloud.diplomaticquarterapp.BuildConfig import com.cloud.diplomaticquarterapp.BuildConfig
import com.cloud.diplomaticquarterapp.geofence.PREFS_STORAGE
import com.google.gson.Gson import com.google.gson.Gson
class Logs { class Logs {

@ -105,7 +105,7 @@ class PlatformBridge(binaryMessenger: BinaryMessenger, flutterMainActivity: Main
override fun success(result: Any?) { override fun success(result: Any?) {
if(result is String) { if(result is String) {
val geoZones = GeoZoneModel().listFrom(result) val geoZones = GeoZoneModel().listFrom(result)
HMG_Geofence.shared(mainActivity).register(geoZones) HMG_Geofence.shared(mainActivity).register(){ s, e -> }
} }
} }

Loading…
Cancel
Save