Esempio n. 1
0
        /// <summary>
        /// Invoked when a contact has published his or her activity.
        /// </summary>
        /// <param name="jid">The JID of the XMPP entity that published the
        /// activity information.</param>
        /// <param name="item">The 'item' Xml element of the pubsub publish
        /// event.</param>
        private void onActivity(Jid jid, XmlElement item)
        {
            if (item == null || item["activity"] == null)
            {
                return;
            }
            var             activityElement = item["activity"];
            XmlElement      temp            = null;
            GeneralActivity?activity        = null;

            if (activityElement.IsEmpty)
            {
                activity = GeneralActivity.Undefined;
            }
            else
            {
                // Look for a GeneralActivity value element.
                foreach (var v in Enum.GetValues(typeof(GeneralActivity)))
                {
                    string s = GeneralActivityToTagName((GeneralActivity)v);
                    if (activityElement[s] != null)
                    {
                        activity = (GeneralActivity)v;
                        temp     = activityElement[s];
                    }
                }
            }
            SpecificActivity specific = SpecificActivity.Other;

            if (temp != null)
            {
                // Look for a SpecificActivity value element.
                foreach (var v in Enum.GetValues(typeof(SpecificActivity)))
                {
                    string s = SpecificActivityToTagName((SpecificActivity)v);
                    if (temp[s] != null)
                    {
                        specific = (SpecificActivity)v;
                    }
                }
            }
            string text = activityElement["text"] != null ?
                          activityElement["text"].InnerText : null;

            // Raise the 'ActivityChanged' event.
            if (activity.HasValue)
            {
                ActivityChanged.Raise(this, new ActivityChangedEventArgs(jid,
                                                                         activity.Value, specific, text));
            }
        }
Esempio n. 2
0
 protected void FireActivityChange(Activity activity, MvxActivityState state, object extras = null)
 {
     ActivityChanged?.Invoke(this, new MvxActivityEventArgs(activity, state, extras));
 }
Esempio n. 3
0
        public override void OnReceive(global::Android.Content.Context context, Intent intent)
        {
            // this method is usually called on the UI thread and can crash the app if it throws an exception
            try
            {
                if (intent == null)
                {
                    throw new ArgumentNullException(nameof(intent));
                }

                if (intent.Action == AndroidAwarenessProbe.AWARENESS_PENDING_INTENT_ACTION)
                {
                    FenceState fenceState = FenceState.Extract(intent);
                    if (fenceState == null)
                    {
                        SensusException.Report("Null awareness fence state.");
                        return;
                    }

                    // is this a location fence event?
                    if (fenceState.FenceKey == AndroidLocationFenceProbe.AWARENESS_EXITING_LOCATION_FENCE_KEY)
                    {
                        if (fenceState.CurrentState == FenceState.True)
                        {
                            LocationChanged?.Invoke(this, fenceState);
                        }
                    }
                    // otherwise, it should be an activity fence event.
                    else
                    {
                        string[] fenceKeyParts;
                        if (fenceState.FenceKey == null || (fenceKeyParts = fenceState.FenceKey.Split('.')).Length != 2)
                        {
                            SensusException.Report("Awareness fence key \"" + fenceState.FenceKey + "\" does not have two parts.");
                            return;
                        }

                        Activities activity;
                        if (!Enum.TryParse(fenceKeyParts[0], out activity))
                        {
                            SensusException.Report("Unrecognized awareness activity:  " + fenceKeyParts[0]);
                            return;
                        }

                        ActivityPhase phase;
                        if (!Enum.TryParse(fenceKeyParts[1], out phase))
                        {
                            SensusException.Report("Unrecognized awareness activity phase:  " + fenceKeyParts[1]);
                            return;
                        }

                        DateTimeOffset timestamp = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan()).AddMilliseconds(fenceState.LastFenceUpdateTimeMillis);

                        if (fenceState.CurrentState == FenceState.True)
                        {
                            ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Active, ActivityConfidence.NotAvailable));
                        }
                        else if (fenceState.CurrentState == FenceState.False)
                        {
                            ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Inactive, ActivityConfidence.NotAvailable));
                        }
                        else if (fenceState.CurrentState == FenceState.Unknown)
                        {
                            ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Unknown, ActivityConfidence.NotAvailable));
                        }
                        else
                        {
                            SensusException.Report("Unrecognized fence state:  " + fenceState.CurrentState);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                SensusException.Report("Exception in Awareness broadcast receiver:  " + ex.Message, ex);
            }
        }
 public void Handle(ActivityChanged @event)
 {
     UpdateActivity(@event.ActivityId, @event.ActivityMode);
 }
        public override void OnReceive(global::Android.Content.Context context, Intent intent)
        {
            if (intent.Action == AndroidActivityProbe.AWARENESS_PENDING_INTENT_ACTION)
            {
                FenceState fenceState = FenceState.Extract(intent);
                if (fenceState == null)
                {
                    SensusException.Report("Null awareness fence state.");
                    return;
                }

                if (fenceState.FenceKey == AndroidActivityProbe.AWARENESS_EXITING_LOCATION_FENCE_KEY)
                {
                    if (fenceState.CurrentState == FenceState.True)
                    {
                        LocationChanged?.Invoke(this, fenceState);
                    }
                }
                else
                {
                    string[] fenceKeyParts;
                    if (fenceState.FenceKey == null || (fenceKeyParts = fenceState.FenceKey.Split('.')).Length != 2)
                    {
                        SensusException.Report("Awareness fence key \"" + fenceState.FenceKey + "\" does not have two parts.");
                        return;
                    }

                    Activities activity;
                    if (!Enum.TryParse(fenceKeyParts[0], out activity))
                    {
                        SensusException.Report("Unrecognized awareness activity:  " + fenceKeyParts[0]);
                        return;
                    }

                    ActivityPhase phase;
                    if (!Enum.TryParse(fenceKeyParts[1], out phase))
                    {
                        SensusException.Report("Unrecognized awareness activity phase:  " + fenceKeyParts[1]);
                        return;
                    }

                    DateTimeOffset timestamp = new DateTimeOffset(1970, 1, 1, 0, 0, 0, new TimeSpan()).AddMilliseconds(fenceState.LastFenceUpdateTimeMillis);

                    if (fenceState.CurrentState == FenceState.True)
                    {
                        ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Active));
                    }
                    else if (fenceState.CurrentState == FenceState.False)
                    {
                        ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Inactive));
                    }
                    else if (fenceState.CurrentState == FenceState.Unknown)
                    {
                        ActivityChanged?.Invoke(this, new ActivityDatum(timestamp, activity, phase, ActivityState.Unknown));
                    }
                    else
                    {
                        SensusException.Report("Unrecognized fence state:  " + fenceState.CurrentState);
                    }
                }
            }
        }