Example #1
0
        private void Authenticate(string user, string pass)
        {
            if (!IsOnline())
            {
                Toast.MakeText(this, "Verifique a conexão com a internet", ToastLength.Long).Show();
                return;
            }

            var login      = new HttpParam[] { new HttpParam("user_user", user), new HttpParam("user_pass", pass) };
            var deviceData = GetData();
            var data       = new HttpParam[login.Length + deviceData.Length];

            login.CopyTo(data, 0);
            deviceData.CopyTo(data, login.Length);

            var progressDialog = ProgressDialog.Show(this, "Aguarde", "Autenticando aparelho...", true);
            var t = new Thread(new ThreadStart(delegate
            {
                var res = Request.GetInstance().Post <Autenticacao>("authentication", "register", "", data);
                RunOnUiThread(() =>
                {
                    progressDialog.Hide();

                    if (res.status == null)
                    {
#if DEBUG
                        AlertDialog.Builder alerta = new AlertDialog.Builder(this);
                        alerta.SetTitle("Debug");
                        alerta.SetMessage(res.debug);
                        alerta.SetPositiveButton("Fechar", (sender, e) => { });
                        alerta.Show();
                        return;
#else
                        Toast.MakeText(this, "Erro no servidor!", ToastLength.Long).Show();
                        return;
#endif
                    }

                    if (res.status.code == 200)
                    {
                        var editor = PreferenceManager.GetDefaultSharedPreferences(this).Edit();
                        editor.PutString("guid", guid);
                        System.Diagnostics.Debug.WriteLine("[#] " + res.data.ToString());
                        editor.PutString("authentication", Serializador.ToXML(res.data));
                        editor.Apply();
                        var intent = new Intent(this, typeof(LoginActivity));
                        intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask);
                        StartActivity(intent);
                        Finish();
                    }
                    else                     //420 = ja cadastrado
                    {
                        AlertDialog.Builder alerta = new AlertDialog.Builder(this);
                        alerta.SetTitle(res.status.message);
                        //alerta.SetMessage("Não foi possível autenticar o aparelho!");
                        alerta.SetMessage(res.status.description);
                        alerta.SetPositiveButton("Fechar", (sender, e) => { });
                        alerta.Show();
                        GetId();
                    }
                });
            }));

            t.Start();
        }