示例#1
0
        protected override void OnCreate(Bundle bundle)
        {
            try
            {
                base.OnCreate(bundle);

                SetContentView(Resource.Layout.Main);

                // Get our button from the layout resource,
                // and attach an event to it
                Button   button        = FindViewById <Button>(Resource.Id.button1);
                EditText textoUsuario  = FindViewById <EditText>(Resource.Id.editText1);
                EditText textoPassword = FindViewById <EditText>(Resource.Id.editText2);

                string usuarioinformacion = Intent.GetStringExtra("usuarioinformacion") ?? string.Empty;

                if (!string.IsNullOrEmpty(usuarioinformacion))
                {
                    UsuarioBO resultado = Newtonsoft.Json.JsonConvert.DeserializeObject <UsuarioBO>(usuarioinformacion);

                    if (resultado != null && !string.IsNullOrEmpty(resultado.Usuario) && !string.IsNullOrEmpty(resultado.Password))
                    {
                        textoUsuario.Text  = resultado.Usuario;
                        textoPassword.Text = resultado.Password;
                    }
                }

                button.Click += async delegate
                {
                    button.Enabled = false;

                    if (isInCall)
                    {
                        Toast.MakeText(this, "Su Solicitud se esta procesando...", ToastLength.Short).Show();
                        button.Enabled = true;
                        return;
                    }

                    isInCall = true;

                    if (string.IsNullOrEmpty(textoUsuario.Text))
                    {
                        Toast.MakeText(this, "Ingrese su usuario...", ToastLength.Short).Show();
                        button.Enabled = true;
                        isInCall       = false;
                        return;
                    }

                    if (string.IsNullOrEmpty(textoPassword.Text))
                    {
                        Toast.MakeText(this, "Ingrese su contraseña...", ToastLength.Short).Show();
                        button.Enabled = true;
                        isInCall       = false;
                        return;
                    }

                    if (!GetIsInternetAccessAvailable())
                    {
                        Toast.MakeText(context, "Sin Acceso a Internet. Verifique", ToastLength.Short).Show();
                        button.Enabled = true;
                        isInCall       = false;
                        return;
                    }

                    if (!IsHostReachable())
                    {
                        Toast.MakeText(context, "Sin comunicación con el Servicio. Verifique", ToastLength.Short).Show();
                        button.Enabled = true;
                        isInCall       = false;
                        return;
                    }

                    progress = new ProgressDialog(this);
                    progress.Indeterminate = false;
                    progress.SetProgressStyle(ProgressDialogStyle.Spinner);
                    progress.SetMessage("Validando información...");
                    progress.SetCancelable(false);
                    progress.Show();

                    string resultado = await LogInuser(textoUsuario.Text, textoPassword.Text);

                    if (string.IsNullOrEmpty(resultado))
                    {
                        var intent = new Intent(this, typeof(ProcesoPrincipalActivity));
                        intent.PutExtra("contenido", contenido);

                        RunOnUiThread(() =>
                        {
                            button.Enabled = true;
                            isInCall       = false;
                            progress.SetMessage("Cargando contenido...");
                            progress.Hide();
                            progress.Dispose();
                            StartActivity(intent);
                        });
                    }
                    else
                    {
                        RunOnUiThread(() =>
                        {
                            button.Enabled = true;
                            isInCall       = false;
                            progress.Hide();
                            progress.Dispose();
                            Toast.MakeText(this, resultado, ToastLength.Long).Show();
                        });
                    }
                };
            }
            catch (Exception ex)
            {
                isInCall = false;
                Toast.MakeText(context, "La aplicación tuvo un incoveniente. Intente más tarde. Error: " + ex.Message, ToastLength.Long).Show();
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            try
            {
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.ProcesoPrincipalActivity);

                string contenido = Intent.GetStringExtra("contenido") ?? string.Empty;

                if (!string.IsNullOrEmpty(contenido))
                {
                    AutenticarUsuarioResult resultado = Newtonsoft.Json.JsonConvert.DeserializeObject <AutenticarUsuarioResult>(contenido);
                    citas = resultado.lista;

                    if (citas != null && citas.Any())
                    {
                        UsuarioBO usuario = new UsuarioBO {
                            Usuario = citas[0].Usuario, Password = citas[0].Password
                        };
                        usuarioinformacion = Newtonsoft.Json.JsonConvert.SerializeObject(usuario);
                    }
                }

                Spinner spinner = FindViewById <Spinner>(Resource.Id.spinner1);
                spinner.Adapter       = new CitasAdapter(this, citas);
                spinner.ItemSelected += new EventHandler <AdapterView.ItemSelectedEventArgs>(spinner_ItemSelected);

                Button btnCheckin  = FindViewById <Button>(Resource.Id.button1);
                Button btnCheckout = FindViewById <Button>(Resource.Id.button2);
                Button btnCrm      = FindViewById <Button>(Resource.Id.button3);

                #region btnCheckIn

                btnCheckin.Click += async delegate
                {
                    btnCheckin.Enabled = false;

                    if (isInCall)
                    {
                        Toast.MakeText(this, "Su Solicitud se esta procesando...", ToastLength.Short).Show();
                        btnCheckin.Enabled = true;
                        return;
                    }

                    isInCall = true;

                    try
                    {
                        if (!GetIsInternetAccessAvailable())
                        {
                            Toast.MakeText(context, "Sin Acceso a Internet. Verifique", ToastLength.Short).Show();
                            btnCheckin.Enabled = true;
                            isInCall           = false;
                            return;
                        }

                        if (!IsHostReachable())
                        {
                            Toast.MakeText(context, "Sin comunicación con el Servicio. Verifique", ToastLength.Short).Show();
                            btnCheckin.Enabled = true;
                            isInCall           = false;
                            return;
                        }

                        if (citaSeleccionada != null)
                        {
                            progress = new ProgressDialog(this);
                            progress.Indeterminate = false;
                            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
                            progress.SetMessage("Realizando Check-In...");
                            progress.SetCancelable(false);
                            progress.Show();

                            var locator = CrossGeolocator.Current;
                            locator.DesiredAccuracy = 1;
                            var position = await locator.GetPositionAsync(timeoutMilliseconds : 10000);

                            if (position != null)
                            {
                                DateTime momento = DateTime.Now;

                                citaSeleccionada.LatitudInicio  = position.Latitude;
                                citaSeleccionada.LongitudInicio = position.Longitude;
                                citaSeleccionada.HoraLlegada    = momento.ToString("dd/MM/yyyy HH:mm");
                                citaSeleccionada.year           = momento.Year;
                                citaSeleccionada.month          = momento.Month;
                                citaSeleccionada.day            = momento.Day;
                                citaSeleccionada.hour           = momento.Hour;
                                citaSeleccionada.minute         = momento.Minute;

                                string resultado = await DoCheckInAsync();

                                if (string.IsNullOrEmpty(resultado))
                                {
                                    RunOnUiThread(() =>
                                    {
                                        btnCheckin.Enabled = true;
                                        isInCall           = false;

                                        progress.Hide();
                                        progress.Dispose();

                                        CargaInformacionConResultado("CI");
                                    });
                                }
                                else
                                {
                                    RunOnUiThread(() =>
                                    {
                                        btnCheckin.Enabled              = true;
                                        isInCall                        = false;
                                        citaSeleccionada.LatitudInicio  = 0;
                                        citaSeleccionada.LongitudInicio = 0;
                                        citaSeleccionada.HoraLlegada    = string.Empty;
                                        citaSeleccionada.year           = 0;
                                        citaSeleccionada.month          = 0;
                                        citaSeleccionada.day            = 0;
                                        citaSeleccionada.hour           = 0;
                                        citaSeleccionada.minute         = 0;

                                        progress.Hide();
                                        progress.Dispose();

                                        AlertDialog.Builder alert = new AlertDialog.Builder(this);
                                        alert.SetTitle("Información");
                                        alert.SetMessage(resultado);
                                        alert.SetPositiveButton("Aceptar", (senderAlert, args) => { });
                                        Dialog dialog = alert.Create();
                                        dialog.Show();
                                    });
                                }
                            }
                            else
                            {
                                btnCheckin.Enabled = true;
                                isInCall           = false;
                                Toast.MakeText(context, "No se logro optener su posición actual", ToastLength.Long).Show();
                                return;
                            }
                        }
                        else
                        {
                            btnCheckin.Enabled = true;
                            isInCall           = false;
                            Toast.MakeText(context, "Debe seleccionar una cita", ToastLength.Long).Show();
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        progress.Hide();

                        btnCheckin.Enabled = true;
                        isInCall           = false;
                        Toast.MakeText(this, "No se logro realizar el Check In. Intente más tarde. Error: " + ex.Message, ToastLength.Long).Show();
                    }
                };

                #endregion btnCheckIn

                #region btnCheckout

                btnCheckout.Click += async delegate
                {
                    btnCheckout.Enabled = false;

                    if (isInCall)
                    {
                        Toast.MakeText(this, "Su Solicitud se esta procesando...", ToastLength.Short).Show();
                        btnCheckout.Enabled = true;
                        return;
                    }

                    isInCall = true;

                    try
                    {
                        if (!GetIsInternetAccessAvailable())
                        {
                            btnCheckout.Enabled = true;
                            isInCall            = false;
                            Toast.MakeText(context, "Sin Acceso a Internet. Verifique", ToastLength.Long).Show();
                            return;
                        }

                        if (!IsHostReachable())
                        {
                            btnCheckout.Enabled = true;
                            isInCall            = false;
                            Toast.MakeText(context, "Sin comunicación con el Servicio. Verifique", ToastLength.Long).Show();
                            return;
                        }

                        if (citaSeleccionada != null)
                        {
                            progress = new ProgressDialog(this);
                            progress.Indeterminate = false;
                            progress.SetProgressStyle(ProgressDialogStyle.Spinner);
                            progress.SetMessage("Realizando Check-Out...");
                            progress.SetCancelable(false);
                            progress.Show();

                            var locator = CrossGeolocator.Current;
                            locator.DesiredAccuracy = 1;
                            var position = await locator.GetPositionAsync(timeoutMilliseconds : 10000);

                            if (position != null)
                            {
                                DateTime momento = DateTime.Now;

                                citaSeleccionada.LatitudFin  = position.Latitude;
                                citaSeleccionada.LongitudFin = position.Longitude;
                                citaSeleccionada.HoraSalida  = momento.ToString("dd/MM/yyyy HH:mm");
                                citaSeleccionada.year        = momento.Year;
                                citaSeleccionada.month       = momento.Month;
                                citaSeleccionada.day         = momento.Day;
                                citaSeleccionada.hour        = momento.Hour;
                                citaSeleccionada.minute      = momento.Minute;

                                string resultado = await DoCheckOutAsync();

                                if (string.IsNullOrEmpty(resultado))
                                {
                                    RunOnUiThread(() =>
                                    {
                                        btnCheckout.Enabled = true;
                                        isInCall            = false;

                                        progress.Hide();
                                        progress.Dispose();

                                        CargaInformacionConResultado("CO");
                                    });
                                }
                                else
                                {
                                    RunOnUiThread(() =>
                                    {
                                        btnCheckout.Enabled          = true;
                                        isInCall                     = false;
                                        citaSeleccionada.LatitudFin  = 0;
                                        citaSeleccionada.LongitudFin = 0;
                                        citaSeleccionada.HoraSalida  = string.Empty;
                                        citaSeleccionada.year        = 0;
                                        citaSeleccionada.month       = 0;
                                        citaSeleccionada.day         = 0;
                                        citaSeleccionada.hour        = 0;
                                        citaSeleccionada.minute      = 0;

                                        progress.Hide();
                                        progress.Dispose();

                                        AlertDialog.Builder alert = new AlertDialog.Builder(this);
                                        alert.SetTitle("Información");
                                        alert.SetMessage(resultado);
                                        alert.SetPositiveButton("Aceptar", (senderAlert, args) => { });
                                        Dialog dialog = alert.Create();
                                        dialog.Show();
                                    });
                                }
                            }
                            else
                            {
                                btnCheckout.Enabled = true;
                                isInCall            = false;
                                Toast.MakeText(context, "No se logro optener su posición actual", ToastLength.Long).Show();
                                return;
                            }
                        }
                        else
                        {
                            btnCheckout.Enabled = true;
                            isInCall            = false;
                            Toast.MakeText(context, "Debe seleccionar una cita", ToastLength.Long).Show();
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        progress.Hide();
                        btnCheckout.Enabled = true;
                        isInCall            = false;
                        Toast.MakeText(this, "No se logro realizar el Check Out. Intente más tarde. Error: " + ex.Message, ToastLength.Long).Show();
                    }
                };

                #endregion btnCheckout

                #region btnCRM

                btnCrm.Click += delegate
                {
                    try
                    {
                        var existIntent = ApplicationContext.PackageManager.GetLaunchIntentForPackage("com.microsoft.crm.crmhost");
                        //var existIntent = ApplicationContext.PackageManager.GetLaunchIntentForPackage("com.whatsapp");

                        if (existIntent != null)
                        {
                            StartActivity(existIntent);
                        }
                        else
                        {
                            existIntent = ApplicationContext.PackageManager.GetLaunchIntentForPackage("com.microsoft.crm.crmphone");

                            if (existIntent != null)
                            {
                                StartActivity(existIntent);
                            }
                            else
                            {
                                var uri    = Android.Net.Uri.Parse("market://details?id=com.microsoft.crm.crmhost");
                                var intent = new Intent(Intent.ActionView, uri);
                                StartActivity(intent);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Toast.MakeText(this, "No se pudo abrir Dynamics CRM. Intente más tarde. Error: " + ex.Message, ToastLength.Long).Show();
                        return;
                    }
                };

                #endregion btnCRM

                AndroidEnvironment.UnhandledExceptionRaiser += new EventHandler <RaiseThrowableEventArgs>(AndroidEnvironment_UnhandledExceptionRaiser);
            }
            catch (Exception ex)
            {
                Toast.MakeText(context, "La aplicación tuvo un incoveniente. Intente más tarde. Error: " + ex.Message, ToastLength.Long).Show();
            }
        }