Пример #1
0
        protected override void OnLocationReceived(Context context, Android.Locations.Location loc)
        {
            //base.OnLocationReceived(context, loc);
            RunManager rm = RunManager.Get(mRunFragment.Activity);

            // trying to ignore old locations, but this call always seems to pass a location with the current time
            // so leaving out for now
            //			var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            //			var now = DateTime.UtcNow;
            //			var seconds = (now - epoch).TotalSeconds;
            //
            //			var locTimeSeconds = loc.Time /1000;
            //
            //			if (locTimeSeconds < seconds - 60)
            //				return;

            var now         = DateTime.UtcNow;
            var lastLocTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(loc.Time);

            if ((now - lastLocTime).TotalSeconds < 10.0)
            {
                if (rm.IsTrackingRun())
                {
                    mRunFragment.LastLocation = loc;
                    // Moved to LocationReceiver
//					RunLocation rl = new RunLocation();
//					rl.RunId = mRunFragment.CurrentRun.Id;
//					rl.Latitude = loc.Latitude;
//					rl.Longitude = loc.Longitude;
//					rl.Altitude = loc.Altitude;
//					rl.Time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(loc.Time);
//					rl.Provider = loc.Provider;
//					rm.InsertItem<RunLocation>(rl);
                }

                if (mRunFragment.IsVisible)
                {
                    mRunFragment.UpdateUI();
                }
            }
        }
Пример #2
0
        protected virtual void OnLocationReceived(Context context, Location loc)
        {
            //			Console.WriteLine("[{0}] {1} Got location from {2}: {3}, {4}", TAG, this, loc.Provider, loc.Latitude, loc.Longitude);
            RunManager rm  = RunManager.Get(context);
            Run        run = rm.GetActiveRun();

            if (rm.IsTrackingRun() && run != null)
            {
                RunLocation rl = new RunLocation();
                rl.RunId     = run.Id;
                rl.Latitude  = loc.Latitude;
                rl.Longitude = loc.Longitude;
                rl.Altitude  = loc.Altitude;
                rl.Time      = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(loc.Time);
                rl.Provider  = loc.Provider;
                rm.InsertItem <RunLocation>(rl);

                if ((DateTime.UtcNow - run.StartDate).Minutes % 5 == 0 && (DateTime.UtcNow - run.StartDate).Seconds % 60 == 0)
                {
                    // Open activity on click notification. (Need to handle properly so off for now)
                    Intent intent = new Intent(context, typeof(RunActivity));
                    intent.AddFlags(ActivityFlags.SingleTop);
                    const int     pendingIntentId = 0;
                    PendingIntent pendingIntent   = PendingIntent.GetActivity(context, pendingIntentId, intent, PendingIntentFlags.OneShot);

                    Notification notification = new Notification.Builder(context)
                                                .SetTicker(context.Resources.GetString(Resource.String.tracking_run_notification_title))
                                                .SetSmallIcon(Android.Resource.Drawable.IcMenuView)
                                                .SetContentTitle(context.Resources.GetString(Resource.String.tracking_run_notification_title))
                                                .SetContentText(context.Resources.GetString(Resource.String.tracking_run_notification_text))
                                                .SetContentIntent(pendingIntent)
                                                .SetAutoCancel(true)
                                                .Build();

                    NotificationManager notificationManager = context.GetSystemService(Context.NotificationService) as NotificationManager;

                    const int notificationId = 0;
                    notificationManager.Notify(notificationId, notification);
                }
            }
        }
Пример #3
0
        public void UpdateUI()
        {
            if (CurrentRun != null && !CurrentRun.Active)
            {
                mRunLocations = mRunManager.GetLocationsForRun(CurrentRun.Id);

                if (mRunLocations.Count > 0)
                {
                    mStartedTextView.Text = CurrentRun.StartDate.ToLocalTime().ToString();

                    mLatitudeTextView.Text  = mRunLocations[0].Latitude.ToString();
                    mLongitudeTextView.Text = mRunLocations[0].Longitude.ToString();
                    mAltitudeTextView.Text  = mRunLocations[0].Altitude.ToString();

                    int durationSeconds = (int)Math.Ceiling((mRunLocations[mRunLocations.Count - 1].Time - CurrentRun.StartDate).TotalSeconds);
                    mDurationTextView.Text = Run.FormatDuration(durationSeconds);

//					var location = new LatLng(mRunLocations[mRunLocations.Count -1].Latitude, mRunLocations[mRunLocations.Count -1].Longitude);
//					var cu = CameraUpdateFactory.NewLatLngZoom (location, 20);
//					mGoogleMap.MoveCamera (cu);
                    DrawRunTrack(false);
                }
                else
                {
                    var     toast   = Toast.MakeText(Activity, Resource.String.empty_run_message, ToastLength.Long);
                    Display display = Activity.WindowManager.DefaultDisplay;
                    var     size    = new Android.Graphics.Point();
                    display.GetSize(size);
                    toast.SetGravity(GravityFlags.Top, 0, size.Y / 6);
                    toast.Show();
                }

                if (mRunManager.IsTrackingRun())
                {
                    mStartButton.Enabled    = false;
                    mStartButton.Visibility = ViewStates.Invisible;
                    mStopButton.Enabled     = false;
                    mStopButton.Visibility  = ViewStates.Invisible;
                }
                else
                {
                    mStartButton.Enabled = true;
                    mStopButton.Enabled  = false;
                }
            }
            else
            {
                bool started = mRunManager.IsTrackingRun();

                if (CurrentRun != null)
                {
                    mRunLocations         = mRunManager.GetLocationsForRun(CurrentRun.Id);
                    mStartedTextView.Text = CurrentRun.StartDate.ToLocalTime().ToString();
                }

                int durationSeconds = 0;
                if (CurrentRun != null && LastLocation != null)
                {
                    DateTime lastLocTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(LastLocation.Time);
                    durationSeconds         = CurrentRun.GetDurationSeconds(lastLocTime);
                    mLatitudeTextView.Text  = LastLocation.Latitude.ToString();
                    mLongitudeTextView.Text = LastLocation.Longitude.ToString();
                    mAltitudeTextView.Text  = LastLocation.Altitude.ToString();
                    mDurationTextView.Text  = Run.FormatDuration(durationSeconds);
                    CurrentRun.Duration     = durationSeconds;
                    mRunManager.UpdateItem <Run>(CurrentRun);

//					var location = new LatLng(LastLocation.Latitude, LastLocation.Longitude);
//					var cu = CameraUpdateFactory.NewLatLngZoom (location, 20);
//					mGoogleMap.MoveCamera (cu);
                    DrawRunTrack(true);
                }

                mStartButton.Enabled = !started;
                mStopButton.Enabled  = started;
            }
        }