示例#1
0
 public GeofenceResult(GeofenceResult result)
 {
     if (result != null)
     {
         this.LastEnterTime = result.LastEnterTime;
         this.LastExitTime  = result.LastExitTime;
         this.Transition    = result.Transition;
         this.RegionId      = result.RegionId;
         this.Latitude      = result.Latitude;
         this.Longitude     = result.Longitude;
         this.Accuracy      = result.Accuracy;
     }
 }
        /// <summary>
        /// Handles geofence intent arrival
        /// </summary>
        /// <param name="intent"></param>
        protected override void OnHandleIntent(Intent intent)
        {
            try
            {
                lock (IntentLock)
                {
                    if (intent == null)
                    {
                        intent = new Intent(Application.Context, typeof(GeofenceTransitionsIntentService));
                    }

                    Context context = Android.App.Application.Context;
                    Bundle  extras  = intent.Extras;
                    Android.Gms.Location.GeofencingEvent geofencingEvent = Android.Gms.Location.GeofencingEvent.FromIntent(intent);

                    if (geofencingEvent.HasError)
                    {
                        string errorMessage = Android.Gms.Location.GeofenceStatusCodes.GetStatusCodeString(geofencingEvent.ErrorCode);
                        string message      = string.Format("{0} - {1}", CrossGeofence.Id, errorMessage);
                        System.Diagnostics.Debug.WriteLine(message);
                        ((GeofenceImplementation)CrossGeofence.Current).LocationHasError = true;
                        CrossGeofence.GeofenceListener.OnError(message);
                        return;
                    }

                    // Get the geofences that were triggered. A single event can trigger multiple geofences.
                    IList <Android.Gms.Location.IGeofence> triggeringGeofences = geofencingEvent.TriggeringGeofences;
                    List <GeofenceResult> geofenceTransitions = new List <GeofenceResult>();
                    Dictionary <string, GeofenceCircularRegion> transitionRegions = new Dictionary <string, GeofenceCircularRegion>();
                    if (triggeringGeofences != null)
                    {
                        foreach (Android.Gms.Location.IGeofence geofence in triggeringGeofences)
                        {
                            if (geofence == null)
                            {
                                continue;
                            }
                            int geofenceTransition         = geofencingEvent.GeofenceTransition;
                            GeofenceTransition gTransition = GeofenceTransition.Unknown;
                            // Get the transition type.
                            lock (GeofenceImplementation.Lock)
                            {
                                ((GeofenceImplementation)CrossGeofence.Current).CurrentRequestType = Plugin.Geofence.GeofenceImplementation.RequestType.Update;
                                if (!CrossGeofence.Current.GeofenceResults.ContainsKey(geofence.RequestId))
                                {
                                    ((GeofenceImplementation)CrossGeofence.Current).AddGeofenceResult(geofence.RequestId);
                                }
                                GeofenceResult result = CrossGeofence.Current.GeofenceResults[geofence.RequestId];

                                //geofencingEvent.TriggeringLocation.Accuracy
                                result.Latitude  = geofencingEvent.TriggeringLocation.Latitude;
                                result.Longitude = geofencingEvent.TriggeringLocation.Longitude;
                                result.Accuracy  = geofencingEvent.TriggeringLocation.Accuracy;

                                double   seconds    = geofencingEvent.TriggeringLocation.Time / 1000;
                                DateTime resultDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(seconds).ToLocalTime();
                                switch (geofenceTransition)
                                {
                                case Android.Gms.Location.Geofence.GeofenceTransitionEnter:
                                    gTransition          = GeofenceTransition.Entered;
                                    result.LastEnterTime = resultDate;
                                    result.LastExitTime  = null;
                                    break;

                                case Android.Gms.Location.Geofence.GeofenceTransitionExit:
                                    gTransition         = GeofenceTransition.Exited;
                                    result.LastExitTime = resultDate;
                                    break;

                                case Android.Gms.Location.Geofence.GeofenceTransitionDwell:
                                    gTransition = GeofenceTransition.Stayed;
                                    break;

                                default:
                                    string message = string.Format("{0} - {1}", CrossGeofence.Id, "Invalid transition type");
                                    System.Diagnostics.Debug.WriteLine(message);
                                    gTransition = GeofenceTransition.Unknown;
                                    break;
                                }
                                System.Diagnostics.Debug.WriteLine($"{CrossGeofence.Id} - {result.RegionId} - Transition: {gTransition}");
                                if (result.Transition != gTransition)
                                {
                                    result.Transition = gTransition;
                                    geofenceTransitions.Add(new GeofenceResult(result));
                                    if (CrossGeofence.Current.Regions.ContainsKey(result.RegionId))
                                    {
                                        GeofenceCircularRegion region = CrossGeofence.Current.Regions[result.RegionId];
                                        transitionRegions.Add(result.RegionId, new GeofenceCircularRegion(region));
                                    }
                                    //Check if device has stayed in region asynchronosly
                                    //Commented because is already handled using DWELL on Android
                                    //CheckIfStayed(geofence.RequestId);
                                }
                            }
                        }
                    }

                    Task.Factory.StartNew(() =>
                    {
                        foreach (GeofenceResult result in geofenceTransitions)
                        {
                            if (result == null)
                            {
                                continue;
                            }
                            CrossGeofence.GeofenceListener.OnRegionStateChanged(result);
                            if (transitionRegions.ContainsKey(result.RegionId))
                            {
                                GeofenceCircularRegion region = transitionRegions[result.RegionId];
                                if (region.ShowNotification)
                                {
                                    string message = result.ToString();
                                    switch (result.Transition)
                                    {
                                    case GeofenceTransition.Entered:
                                        if (!region.ShowEntryNotification)
                                        {
                                            return;
                                        }
                                        message = string.IsNullOrEmpty(region.NotificationEntryMessage) ? message : region.NotificationEntryMessage;
                                        break;

                                    case GeofenceTransition.Exited:
                                        if (!region.ShowExitNotification)
                                        {
                                            return;
                                        }
                                        message = string.IsNullOrEmpty(region.NotificationExitMessage) ? message : region.NotificationExitMessage;
                                        break;

                                    case GeofenceTransition.Stayed:
                                        if (!region.ShowStayNotification)
                                        {
                                            return;
                                        }
                                        message = string.IsNullOrEmpty(region.NotificationStayMessage) ? message : region.NotificationStayMessage;
                                        break;
                                    }
                                    using (Handler handler = new Handler(Looper.MainLooper))
                                    {
                                        handler.Post(() =>
                                        {
                                            CreateNotification(context.ApplicationInfo.LoadLabel(context.PackageManager), message);
                                        });
                                    }
                                }
                            }
                        }
                    });
                }
            }
            catch (Java.Lang.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}", CrossGeofence.Id, ex.ToString()));
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(string.Format("{0} - {1}", CrossGeofence.Id, ex.ToString()));
            }
        }