Пример #1
0
 public static async void DoRequest(this Activity activity, Task action, Action postAction = null, Action exceptionAction = null)
 {
     try
     {
         await action;
         activity.RunOnUiThread(postAction);
     }
     catch (Exception ex)
     {
         activity.RunOnUiThread(exceptionAction);
         activity.ShowMessage(ex.Message);
     }
 }
Пример #2
0
        private void RefreshMarkers()
        {
            // Update each marker position
            foreach (var pair in transportMarkers)
            {
                Transport transport = pair.Key;
                Marker    marker    = pair.Value;

                // Compute quick position
                Position quickFrom     = transport.Step.Stop.Position;
                Position quickTo       = transport.TimeStep.Step.Stop.Position;
                LatLng   quickPosition = new LatLng(quickFrom.Latitude + (quickTo.Latitude - quickFrom.Latitude) * transport.Progress, quickFrom.Longitude + (quickTo.Longitude - quickFrom.Longitude) * transport.Progress);

                // Update marker
                ValueAnimator valueAnimator;
                if (!markerAnimators.TryGetValue(marker, out valueAnimator))
                {
                    valueAnimator = new ValueAnimator();

                    valueAnimator.AddUpdateListener(new MarkerAnimator(Activity, marker, transport, p => SetMarkerPosition(transport, marker, p), refreshCancellationTokenSource));
                    valueAnimator.SetInterpolator(new LinearInterpolator());
                    valueAnimator.SetFloatValues(0, 1);
                    valueAnimator.SetDuration(1000);

                    markerAnimators.Add(marker, valueAnimator);
                }
                else
                {
                    Activity.RunOnUiThread(valueAnimator.Cancel);
                }

                Activity.RunOnUiThread(valueAnimator.Start);
            }
        }
Пример #3
0
        public override void OnDestroyView()
        {
            base.OnDestroyView();

            // Clean markers
            foreach (Marker marker in transportMarkers.Values)
            {
                Activity.RunOnUiThread(marker.Remove);
            }

            transportMarkers.Clear();
            markerSteps.Clear();

            // Stop and clean animators
            foreach (ValueAnimator valueAnimator in markerAnimators.Values)
            {
                Activity.RunOnUiThread(valueAnimator.Pause);
            }

            markerAnimators.Clear();

            // Dispose map
            googleMap.Clear();
            googleMap.Dispose();
            mapFragment.Dispose();
            mapFragment = null;
        }
Пример #4
0
        public static void ShowDialog(String title_text, String message, String NegativeButton, String PositiveButton, Action actionPositive)
        {
            Android.App.Activity act     = Locator.Current.GetService <Android.App.Activity>();
            AlertDialog.Builder  builder = new AlertDialog.Builder(act);

            if (title_text != null)
            {
                builder.SetTitle(title_text);
            }

            builder.SetMessage(message);
            builder.SetCancelable(false);
            builder.SetPositiveButton(PositiveButton, (s, ev) =>
            {
                if (actionPositive != null)
                {
                    actionPositive.Invoke();
                }
                ((AlertDialog)s).Cancel();
            });
            builder.SetNegativeButton(NegativeButton, (s, ev) =>
            {
                ((AlertDialog)s).Cancel();
            });

            act.RunOnUiThread(() =>
            {
                AlertDialog alert = builder.Show();
            });
        }
Пример #5
0
 private async void BigLongImportantMethodAsync()
 {
     System.Threading.Thread.Sleep(3000);
     _activity.RunOnUiThread(() =>
     {
     });
 }
Пример #6
0
        public void OnRefreshed(IEnumerable <TimeStep> timeSteps, IEnumerable <Transport> transports)
        {
            timeStepsCache  = timeSteps.ToArray();
            transportsCache = transports.ToArray();

            if (googleMap == null || Activity == null)
            {
                return;
            }

            List <Transport> unusedTransports = transportMarkers.Keys.ToList();
            AutoResetEvent   autoResetEvent   = new AutoResetEvent(false);

            foreach (Transport transport in transports)
            {
                Marker marker;

                // Create a new marker if needed
                if (!transportMarkers.TryGetValue(transport, out marker))
                {
                    Activity.RunOnUiThread(() =>
                    {
                        MarkerOptions markerOptions = new MarkerOptions()
                                                      .Anchor(0.5f, 0.5f)
                                                      .SetIcon(transportBitmapDescriptor)
                                                      .SetPosition(new LatLng(0, 0));

                        marker = googleMap.AddMarker(markerOptions);
                        transportMarkers.Add(transport, marker);

                        autoResetEvent.Set();
                    });

                    autoResetEvent.WaitOne();
                }
                else
                {
                    unusedTransports.Remove(transport);
                }
            }

            // Clean old markers
            foreach (Transport transport in unusedTransports)
            {
                Marker marker = transportMarkers[transport];
                transportMarkers.Remove(transport);

                Activity.RunOnUiThread(marker.Remove);

                ValueAnimator valueAnimator;
                if (markerAnimators.TryGetValue(marker, out valueAnimator))
                {
                    Activity.RunOnUiThread(valueAnimator.Cancel);
                    markerAnimators.Remove(marker);
                }
            }

            RefreshMarkers();
        }
Пример #7
0
 public static void showCenterToast(Android.Content.Context ctx, string text, Android.Widget.ToastLength duration)
 {
     Android.App.Activity act = (Android.App.Activity)ctx;
     act.RunOnUiThread(() => {
         Android.Widget.Toast toast = Android.Widget.Toast.MakeText(ctx, text, duration);
         toast.SetGravity(Android.Views.GravityFlags.Center, 0, 0);
         toast.Show();
     });
 }
Пример #8
0
        private void SetMarkerPosition(Transport transport, Marker marker, LatLng position)
        {
            if (!hasFocus)
            {
                return;
            }

            Activity.RunOnUiThread(() =>
            {
                marker.Position = position;

                //if (marker.Id == selectedMarkerId)
                //    googleMap.AnimateCamera(CameraUpdateFactory.NewLatLng(position));
            });
        }
Пример #9
0
		public void UpdateBGFeeds()
		{
#endif
			Logging.Log(this, Logging.LoggingTypeDebug, "UpdateBGFeeds()");
			BackgroundWorker bgWorker = new BackgroundWorker();
			
			bgWorker.DoWork += delegate(object sender, DoWorkEventArgs e) 
			{
				UpdateFeeds();
			};

			bgWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) 
			{
				Logging.Log(this, Logging.LoggingTypeDebug, "UpdateBGFeeds() - RunWorkerCompleted");
#if MONODROID
				
				activity.RunOnUiThread(delegate() 
				{
					if (this.activity.GetType() == typeof(de.dhoffmann.mono.adfcnewsapp.droid.News))
					{
						de.dhoffmann.mono.adfcnewsapp.droid.News aNews = (de.dhoffmann.mono.adfcnewsapp.droid.News)this.activity;
						aNews.LoadNews();
						Android.Widget.Toast.MakeText(this.activity, "Die Newsfeeds sind aktualisiert.", Android.Widget.ToastLength.Short).Show();
					}
				});
#endif
#if MONOTOUCH
				parentController.InvokeOnMainThread(delegate 
				{
					// TODO: Wartekringel entfernen
					this.parentController.BindMyData();
				});
#endif
			};
			
			bgWorker.RunWorkerAsync();
		}
Пример #10
0
        public static AlertDialog ShowDialog(String title_text, String message, String okButton)
        {
            Android.App.Activity act     = Locator.Current.GetService <Android.App.Activity>();
            AlertDialog.Builder  builder = new AlertDialog.Builder(act);

            if (title_text != null)
            {
                builder.SetTitle(title_text);
            }

            builder.SetMessage(message);
            builder.SetCancelable(false);
            builder.SetNegativeButton(okButton, (s, ev) =>
            {
                ((AlertDialog)s).Cancel();
            });
            AlertDialog alert = builder.Create();

            act.RunOnUiThread(() =>
            {
                alert.Show();
            });
            return(alert);
        }
Пример #11
0
 public static void MakeToast(this Android.App.Activity activity, string message)
 {
     activity.RunOnUiThread(delegate {
         Android.Widget.Toast.MakeText(activity, message, Android.Widget.ToastLength.Short).Show();
     });
 }
 public static void SafeInvokeOnMainThread(this Android.App.Activity activity, Action action, Action <Exception> failureAction = null)
 {
     activity.RunOnUiThread(() => SafeInvoke(((Exception error) => LogUtils.Error(error)), failureAction, action));
 }
Пример #13
0
        public async void OnMapReady(GoogleMap map)
        {
            // Register events
            googleMap = map;
            googleMap.CameraChange          += GoogleMap_CameraChange;
            googleMap.MyLocationButtonClick += GoogleMap_MyLocationButtonClick;
            googleMap.MarkerClick           += GoogleMap_MarkerClick;
            googleMap.InfoWindowClick       += GoogleMap_InfoWindowClick;

            // Enable my location if user has granted location permission
            if (ContextCompat.CheckSelfPermission(Activity, Manifest.Permission.AccessFineLocation) == Permission.Granted)
            {
                googleMap.MyLocationEnabled = true;
            }

            await Task.Run(() =>
            {
                Dictionary <string, MarkerOptions> multiStopGrayMarkerOptions = new Dictionary <string, MarkerOptions>();
                Dictionary <Stop, MarkerOptions> multiStopDetailMarkerOptions = new Dictionary <Stop, MarkerOptions>();
                Dictionary <Stop, MarkerOptions> singleStopMarkerOptions      = new Dictionary <Stop, MarkerOptions>();

                Bitmap multiStopIcon = Utils.GetStopIconForColor(Activity, Color.Gray, TramUrWayApplication.MapStopIconSize);
                BitmapDescriptor multiStopIconDescriptor = BitmapDescriptorFactory.FromBitmap(multiStopIcon);

                // Create multistop markers
                foreach (var pair in stops.Where(p => p.Value.Length > 1))
                {
                    MarkerOptions markerOptions = CreateStopMarker(multiStopIconDescriptor, pair.Value);
                    multiStopGrayMarkerOptions.Add(pair.Key, markerOptions);

                    foreach (Stop stop in pair.Value)
                    {
                        Bitmap stopIcon = Utils.GetStopIconForLine(Activity, stop.Line, TramUrWayApplication.MapStopIconSize);
                        BitmapDescriptor stopIconDescriptor = BitmapDescriptorFactory.FromBitmap(stopIcon);

                        markerOptions = CreateStopMarker(stopIconDescriptor, stop).Visible(false);
                        multiStopDetailMarkerOptions.Add(stop, markerOptions);
                    }
                }

                // Create single stop markers
                foreach (var pair in stops.Where(p => p.Value.Length == 1))
                {
                    Stop stop = pair.Value[0];

                    Bitmap stopIcon = Utils.GetStopIconForLine(Activity, stop.Line, TramUrWayApplication.MapStopIconSize);
                    BitmapDescriptor stopIconDescriptor = BitmapDescriptorFactory.FromBitmap(stopIcon);

                    MarkerOptions markerOptions = CreateStopMarker(stopIconDescriptor, stop).Visible(stop.Line.Type == LineType.Tram);
                    singleStopMarkerOptions.Add(stop, markerOptions);
                }

                // Add all markers to the map
                Activity.RunOnUiThread(() =>
                {
                    foreach (var pair in multiStopGrayMarkerOptions)
                    {
                        multiStopGrayMarkers.Add(pair.Key, googleMap.AddMarker(pair.Value));
                    }

                    foreach (var pair in multiStopDetailMarkerOptions)
                    {
                        Marker marker = googleMap.AddMarker(pair.Value);
                        multiStopDetailMarkers.Add(pair.Key, marker);
                        markerStops.Add(marker.Id, pair.Key);
                    }

                    foreach (var pair in singleStopMarkerOptions)
                    {
                        Marker marker = googleMap.AddMarker(pair.Value);
                        singleStopMarkers.Add(pair.Key, marker);
                        markerStops.Add(marker.Id, pair.Key);
                    }
                });
            });
        }