private async void OnAddDialogSubmit(object sender, EditCategoryBottomSheet.EditCategoryEventArgs e)
        {
            var dialog   = (EditCategoryBottomSheet)sender;
            var category = new Category(e.Name);

            if (_categorySource.IsDuplicate(category))
            {
                dialog.NameError = GetString(Resource.String.duplicateCategory);
                return;
            }

            try
            {
                await _categorySource.Add(category);
            }
            catch
            {
                ShowSnackbar(Resource.String.genericError, Snackbar.LengthShort);
                return;
            }
            finally
            {
                RunOnUiThread(dialog.Dismiss);
            }

            RunOnUiThread(delegate
            {
                _categoryListAdapter.NotifyDataSetChanged();
                CheckEmptyState();
            });
        }
Пример #2
0
        private async Task <Tuple <int, int> > DoRestore(byte[] fileData, string password)
        {
            var backup = Backup.FromBytes(fileData, password);

            if (backup.Authenticators == null)
            {
                throw new InvalidAuthenticatorException();
            }

            var authsInserted      = 0;
            var categoriesInserted = 0;

            foreach (var auth in backup.Authenticators.Where(auth => !_authenticatorSource.IsDuplicate(auth)))
            {
                auth.Validate();
                await _connection.InsertAsync(auth);

                authsInserted++;
            }

            foreach (var category in backup.Categories.Where(category => !_categorySource.IsDuplicate(category)))
            {
                await _connection.InsertAsync(category);

                categoriesInserted++;
            }

            foreach (var binding in backup.AuthenticatorCategories.Where(binding => !_authenticatorSource.IsDuplicateCategoryBinding(binding)))
            {
                await _connection.InsertAsync(binding);
            }

            return(new Tuple <int, int>(authsInserted, categoriesInserted));
        }
        private async void OnAddDialogSubmit(object sender, EditCategoryBottomSheet.EditCategoryEventArgs e)
        {
            var category = new Category(e.Name);

            if (_categorySource.IsDuplicate(category))
            {
                _addDialog.NameError = GetString(Resource.String.duplicateCategory);
                return;
            }

            _addDialog.Dismiss();
            await _connection.InsertAsync(category);

            await _categorySource.Update();

            _categoryListAdapter.NotifyDataSetChanged();
            CheckEmptyState();
        }
        private async void AddDialogPositive(object sender, EventArgs e)
        {
            if (_addDialog.Name.Trim() == "")
            {
                _addDialog.Error = GetString(Resource.String.noCategoryName);
                return;
            }

            var category = new Category(_addDialog.Name.Truncate(32));

            if (_categorySource.IsDuplicate(category))
            {
                _addDialog.Error = GetString(Resource.String.duplicateCategory);
                return;
            }

            _addDialog.Dismiss();
            await _connection.InsertAsync(category);

            await _categorySource.Update();

            _categoryAdapter.NotifyDataSetChanged();
            CheckEmptyState();
        }
Пример #5
0
        private async Task <Tuple <int, int> > DoRestore(byte[] fileData, string password)
        {
            var backup = Backup.FromBytes(fileData, password);

            if (backup.Authenticators == null)
            {
                throw new ArgumentException();
            }

            var authsInserted      = 0;
            var categoriesInserted = 0;

            foreach (var auth in backup.Authenticators.Where(auth => !_authSource.IsDuplicate(auth)))
            {
                if (!auth.IsValid())
                {
                    continue;
                }

                await _connection.InsertAsync(auth);

                authsInserted++;
            }

            foreach (var category in backup.Categories.Where(category => !_categorySource.IsDuplicate(category)))
            {
                await _connection.InsertAsync(category);

                categoriesInserted++;
            }

            foreach (var binding in backup.AuthenticatorCategories.Where(binding => !_authSource.IsDuplicateCategoryBinding(binding)))
            {
                await _connection.InsertAsync(binding);
            }

            // Older backups might not have custom icons
            if (backup.CustomIcons != null)
            {
                foreach (var icon in backup.CustomIcons.Where(i => !_customIconSource.IsDuplicate(i.Id)))
                {
                    await _connection.InsertAsync(icon);
                }
            }

            return(new Tuple <int, int>(authsInserted, categoriesInserted));
        }
        private async Task Restore(string password = "")
        {
            try
            {
                string contents;

                if (String.IsNullOrEmpty(password))
                {
                    contents = Encoding.UTF8.GetString(_fileData);
                }
                else
                {
                    var sha256        = SHA256.Create();
                    var passwordBytes = Encoding.UTF8.GetBytes(password);
                    var keyMaterial   = sha256.ComputeHash(passwordBytes);

                    var provider =
                        WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);

                    var key = provider.CreateSymmetricKey(keyMaterial);

                    var raw = WinRTCrypto.CryptographicEngine.Decrypt(key, _fileData);
                    contents = Encoding.UTF8.GetString(raw);
                    _dialog.Dismiss();
                }

                var backup = JsonConvert.DeserializeObject <Backup>(contents);

                if (backup.Authenticators == null)
                {
                    Toast.MakeText(this, Resource.String.invalidFileError, ToastLength.Short).Show();
                    return;
                }

                var authsInserted      = 0;
                var categoriesInserted = 0;

                foreach (var auth in backup.Authenticators.Where(auth => !_authSource.IsDuplicate(auth)))
                {
                    await _connection.InsertAsync(auth);

                    authsInserted++;
                }

                foreach (var category in backup.Categories.Where(category => !_categorySource.IsDuplicate(category)))
                {
                    await _connection.InsertAsync(category);

                    categoriesInserted++;
                }

                foreach (var binding in backup.AuthenticatorCategories.Where(binding => !_authSource.IsDuplicateCategoryBinding(binding)))
                {
                    await _connection.InsertAsync(binding);
                }

                var message = String.Format(GetString(Resource.String.restoredFromBackup), authsInserted,
                                            categoriesInserted);
                Toast.MakeText(this, message, ToastLength.Long).Show();

                Finish();
            }
            catch
            {
                _dialog.Error = GetString(Resource.String.restoreError);
            }
        }