Exemple #1
0
        private void BeginBackup(Uri uri)
        {
            var fragment = new BackupPasswordBottomSheet(BackupPasswordBottomSheet.Mode.Backup);

            fragment.PasswordEntered += async(sender, password) =>
            {
                try
                {
                    await DoBackup(uri, password);
                }
                catch (Exception)
                {
                    ShowSnackbar(Resource.String.filePickError, Snackbar.LengthShort);
                }

                PreferenceManager.GetDefaultSharedPreferences(this)
                .Edit()
                .PutBoolean("needsBackup", false)
                .Commit();

                ((BackupPasswordBottomSheet)sender).Dismiss();
                ShowSnackbar(Resource.String.saveSuccess, Snackbar.LengthLong);
            };

            fragment.Cancel += (sender, _) =>
            {
                // TODO: Delete empty file only if we just created it
                // DocumentsContract.DeleteDocument(ContentResolver, uri);
                ((BackupPasswordBottomSheet)sender).Dismiss();
            };

            fragment.Show(SupportFragmentManager, fragment.Tag);
        }
Exemple #2
0
        private async Task BeginRestore(Uri uri)
        {
            MemoryStream memoryStream = null;
            Stream       stream       = null;

            byte[] fileData;

            try
            {
                stream       = ContentResolver.OpenInputStream(uri);
                memoryStream = new MemoryStream();
                await stream.CopyToAsync(memoryStream);

                fileData = memoryStream.ToArray();
            }
            catch (Exception)
            {
                ShowSnackbar(Resource.String.filePickError, Snackbar.LengthShort);
                return;
            }
            finally
            {
                memoryStream?.Close();
                stream?.Close();
            }

            if (fileData.Length == 0)
            {
                ShowSnackbar(Resource.String.invalidFileError, Snackbar.LengthShort);
                return;
            }

            async Task TryRestore(string password, BackupPasswordBottomSheet sheet)
            {
                int authCount, categoryCount;

                try
                {
                    (authCount, categoryCount) = await DoRestore(fileData, password);
                }
                catch (InvalidAuthenticatorException)
                {
                    sheet?.Dismiss();
                    ShowSnackbar(Resource.String.invalidFileError, Snackbar.LengthShort);
                    return;
                }
                catch (Exception)
                {
                    if (sheet != null)
                    {
                        sheet.Error = GetString(Resource.String.restoreError);
                    }
                    else
                    {
                        ShowSnackbar(Resource.String.restoreError, Snackbar.LengthShort);
                    }

                    return;
                }

                sheet?.Dismiss();

                await _customIconSource.Update();

                await _authSource.Update();

                // This is required because we're probably not running on the main thread
                // as the method was called from a task
                RunOnUiThread(async() =>
                {
                    CheckEmptyState();
                    await RefreshAuthenticators(true);
                });

                await _categorySource.Update();

                var message = String.Format(GetString(Resource.String.restoredFromBackup), authCount, categoryCount);

                ShowSnackbar(message, Snackbar.LengthLong);

                await NotifyWearAppOfChange();
            }

            // Open and closed curly brace (file is not encrypted)
            if (fileData[0] == 0x7b && fileData[^ 1] == 0x7d)
            {
                await TryRestore(null, null);

                return;
            }

            var fragment = new BackupPasswordBottomSheet(BackupPasswordBottomSheet.Mode.Restore);

            fragment.PasswordEntered += async(sender, password) =>
            {
                await TryRestore(password, fragment);
            };

            fragment.Cancel += delegate
            {
                fragment.Dismiss();
            };

            fragment.Show(SupportFragmentManager, fragment.Tag);
        }