Ejemplo n.º 1
0
        public void GetConfig()
        {
            SQLiteRepository con = new SQLiteRepository();

            con.CreateDatabase();
            ConfigRepository configRepository = new ConfigRepository(con);
            Config           c = new Config()
            {
                Phone     = "34676681420",
                Voz       = false,
                Velocidad = (float)0.5
            };

            using (var connection = con.GetConnection())
            {
                configRepository.InsertConfig(c);
                var result = configRepository.GetConfig();
                Assert.AreEqual(false, result.Voz);
            }
        }
Ejemplo n.º 2
0
        public void OnResults(Bundle results)
        {
            int countOnResults = 0;
            IEnumerable <string> matches = results.GetStringArrayList(SpeechRecognizer.ResultsRecognition);
            string   text = "", codigo = "", hash = "";
            bool     isRegistered = true;
            TLUser   user;
            DateTime ahora = DateTime.UtcNow;

            Android.Net.Uri inboxURI = Android.Net.Uri.Parse("content://sms/inbox");

            foreach (string result in matches)
            {
                countOnResults++;
                try
                {
                    if (countOnResults == 1)
                    {
                        record = true;
                        if (int.TryParse(result[0].ToString(), out int num))
                        {
                            text = result.Replace(" ", string.Empty);
                            if (text.Length == 9)
                            {
                                telephone  = "34" + text;
                                phone.Text = telephone;

                                client = loginService.Connect();

                                isRegistered = loginService.IsPhoneRegistered(client, telephone);
                                if (!isRegistered)
                                {
                                    textToSpeak = "No tiene una cuenta registrada. ¿Cuál quiere que sea su nombre de usuario?";
                                    toSpeech    = new TextToSpeech(this, this);
                                }
                                else
                                {
                                    if (!client.IsUserAuthorized())
                                    {
                                        hash   = loginService.SendCodeRequest(client, telephone);
                                        codigo = LookForSms(ahora);
                                        if (codigo == "Ha habido un error")
                                        {
                                            record      = false;
                                            textToSpeak = "Ha habido un error en la recepción del SMS. Por favor, inténtelo en aproximadamente media hora";
                                            toSpeech    = new TextToSpeech(this, this);
                                        }
                                        else
                                        {
                                            code.Text = codigo;
                                            user      = loginService.MakeAuth(client, telephone, hash, codigo);

                                            try
                                            {
                                                User u = new User
                                                {
                                                    Phone          = telephone,
                                                    Username       = user.Username,
                                                    Hash           = hash,
                                                    Code           = codigo,
                                                    SessionExpires = (new DateTime(1970, 1, 1)).AddMilliseconds(client.Session.SessionExpires).AddHours(1),
                                                    AccessHash     = user.AccessHash
                                                };
                                                userRepo.InsertUser(u);

                                                configuracion = new Models.Config()
                                                {
                                                    Phone     = telephone,
                                                    Voz       = true,
                                                    Velocidad = (float)1.0,
                                                    TipoVoz   = toSpeech.Voice != null ? toSpeech.Voice.Name : ""
                                                };
                                                configRepo.InsertConfig(configuracion);
                                                bool contactos           = contactService.GetContacts(client, contactRepo);
                                                bool contactosBloqueados = contactService.GetBlockedContacts(client, contactRepo);
                                            }
                                            catch (Exception ex) { }

                                            Vibrator vibrator = (Vibrator)this.GetSystemService(Context.VibratorService);
                                            vibrator.Vibrate(1000);
                                            record = false;
                                            // Creación sesión
                                            InitSession(telephone);

                                            textToSpeak = "Login correcto";
                                            toSpeech    = new TextToSpeech(this, this);
                                        }
                                    }
                                    else
                                    {
                                        user = client.Session.TLUser;

                                        try
                                        {
                                            User u = new User
                                            {
                                                Phone          = telephone,
                                                Username       = user.Username,
                                                Hash           = hash,
                                                Code           = codigo,
                                                SessionExpires = (new DateTime(1970, 1, 1)).AddMilliseconds(client.Session.SessionExpires).AddHours(1),
                                                AccessHash     = user.AccessHash
                                            };
                                            userRepo.InsertUser(u);
                                            configuracion = new Models.Config()
                                            {
                                                Phone     = telephone,
                                                Voz       = true,
                                                Velocidad = (float)1.0,
                                                TipoVoz   = toSpeech.Voice != null ? toSpeech.Voice.Name : ""
                                            };
                                            configRepo.InsertConfig(configuracion);
                                        }
                                        catch (Exception ex) { }
                                        // Creación sesión
                                        InitSession(telephone);
                                        Vibrator vibrator = (Vibrator)this.GetSystemService(Context.VibratorService);
                                        vibrator.Vibrate(1000);
                                        record      = false;
                                        textToSpeak = "Login correcto";
                                        toSpeech    = new TextToSpeech(this, this);
                                    }
                                }
                            }
                            else
                            {
                                record      = true;
                                textToSpeak = "El número de teléfono no tiene un formato correcto. Por favor, diga un número de teléfono válido";
                                toSpeech    = new TextToSpeech(this, this);
                            }
                        }
                        else
                        {
                            client = loginService.Connect();
                            hash   = loginService.SendCodeRequest(client, telephone);

                            codigo = LookForSms(ahora);

                            if (int.TryParse(codigo, out int num2))
                            {
                                record      = false;
                                textToSpeak = "Ha habido un error esperando el código de verificación. Por favor, inténtelo en 30 minutos";
                                toSpeech    = new TextToSpeech(this, this);
                            }
                            else
                            {
                                user = loginService.SignUp(client, telephone, hash, codigo, result, "");
                                user = loginService.MakeAuth(client, telephone, hash, codigo);

                                try
                                {
                                    User u = new User
                                    {
                                        Phone          = telephone,
                                        Username       = user.Username,
                                        Hash           = hash,
                                        Code           = codigo,
                                        SessionExpires = (new DateTime(1970, 1, 1)).AddMilliseconds(client.Session.SessionExpires).AddHours(1),
                                        AccessHash     = user.AccessHash
                                    };
                                    userRepo.InsertUser(u);

                                    configuracion = new Models.Config()
                                    {
                                        Phone     = telephone,
                                        Voz       = true,
                                        Velocidad = (float)1.0,
                                        TipoVoz   = toSpeech.Voice != null ? toSpeech.Voice.Name : ""
                                    };
                                    configRepo.InsertConfig(configuracion);

                                    bool contactos           = contactService.GetContacts(client, contactRepo);
                                    bool contactosBloqueados = contactService.GetBlockedContacts(client, contactRepo);
                                }
                                catch (Exception ex) { }
                                Vibrator vibrator = (Vibrator)this.GetSystemService(Context.VibratorService);
                                vibrator.Vibrate(1000);
                                record = false;

                                // Creación sesión
                                InitSession(telephone);

                                textToSpeak = "Registro y login correctos";
                                toSpeech    = new TextToSpeech(this, this);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    // TODO: cerrar la aplicación
                    record = false;
                    database.DeleteDataDatabase();
                    FakeSessionDelete.DeleteSession();
                    textToSpeak = "Ha ocurrido un error. Por favor, inténtelo más tarde";
                    toSpeech    = new TextToSpeech(this, this);
                }
            }
        }