Example #1
0
        private void OnSelectLocationClick(object sender, EventArgs args)
        {
            var intent = new Intent(Intent.ActionOpenDocumentTree);

            intent.AddFlags(ActivityFlags.GrantReadUriPermission | ActivityFlags.GrantWriteUriPermission |
                            ActivityFlags.GrantPersistableUriPermission | ActivityFlags.GrantPrefixUriPermission);

            if (_preferences.AutoBackupUri != null)
            {
                intent.PutExtra(DocumentsContract.ExtraInitialUri, _preferences.AutoBackupUri);
            }

            var baseApplication = ((SettingsActivity)Context).BaseApplication;

            baseApplication.PreventNextAutoLock = true;

            try
            {
                _locationSelectResultLauncher.Launch(intent);
            }
            catch (ActivityNotFoundException e)
            {
                Logger.Error(e);
                Toast.MakeText(Context, Resource.String.filePickerMissing, ToastLength.Long);
                baseApplication.PreventNextAutoLock = false;
            }
        }
Example #2
0
        private async Task <RestoreResult> RestoreFromDir(Uri destUri)
        {
            if (!HasPersistablePermissionsAtUri(destUri))
            {
                throw new Exception("No permission at URI");
            }

            var directory = DocumentFile.FromTreeUri(_context, destUri);
            var files     = directory.ListFiles();

            var mostRecentBackup = files
                                   .Where(f => f.IsFile && f.Type == Backup.MimeType && f.Name.EndsWith(Backup.FileExtension) && f.Length() > 0 && f.CanRead())
                                   .OrderByDescending(f => f.LastModified())
                                   .FirstOrDefault();

            if (mostRecentBackup == null || mostRecentBackup.LastModified() <= _preferences.MostRecentBackupModifiedAt)
            {
                return(new RestoreResult());
            }

            _preferences.MostRecentBackupModifiedAt = mostRecentBackup.LastModified();
            var password = await SecureStorageWrapper.GetAutoBackupPassword();

            if (password == null)
            {
                throw new Exception("No password defined.");
            }

            var data = await FileUtil.ReadFile(_context, mostRecentBackup.Uri);

            var backup = Backup.FromBytes(data, password);

            var(authsAdded, authsUpdated) = await _authSource.AddOrUpdateMany(backup.Authenticators);

            var categoriesAdded = backup.Categories != null
                ? await _categorySource.AddMany(backup.Categories)
                : 0;

            if (backup.AuthenticatorCategories != null)
            {
                await _authSource.AddOrUpdateManyCategoryBindings(backup.AuthenticatorCategories);
            }

            var customIconsAdded = backup.CustomIcons != null
                ? await _customIconSource.AddMany(backup.CustomIcons)
                : 0;

            try
            {
                await _customIconSource.CullUnused();
            }
            catch (Exception e)
            {
                // ignored
                Logger.Error(e);
            }

            return(new RestoreResult(authsAdded, authsUpdated, categoriesAdded, customIconsAdded));
        }
Example #3
0
        private void UpdateLocationStatusText()
        {
            var uri = _preferences.AutoBackupUri;

            if (uri == null)
            {
                _locationStatusText.SetText(Resource.String.noLocationSelected);
                return;
            }

            string dirName;

            try
            {
                dirName = FileUtil.GetDocumentName(Context.ContentResolver, uri);
            }
            catch (Exception e)
            {
                Logger.Error(e);
                dirName = "Unknown";
            }

            _locationStatusText.Text = String.Format(GetString(Resource.String.locationSetTo), dirName);
        }
        private async Task <Result> DoWorkAsync()
        {
            var destination = _preferences.AutoBackupUri;

            var restoreTriggered = _preferences.AutoRestoreTrigger;

            _preferences.AutoRestoreTrigger = false;

            var backupTriggered = _preferences.AutoBackupTrigger;

            _preferences.AutoBackupTrigger = false;

            var restoreSucceeded = true;

            if (!backupTriggered && (restoreTriggered || _preferences.AutoRestoreEnabled))
            {
                try
                {
                    await _initTask.Value;
                    var result = await RestoreFromDir(destination);

                    if (!result.IsVoid() || restoreTriggered)
                    {
                        ShowNotification(NotificationContext.RestoreSuccess, true, result);
                    }

                    if (!result.IsVoid())
                    {
                        _preferences.AutoRestoreCompleted = true;
                    }
                }
                catch (Exception e)
                {
                    restoreSucceeded = false;
                    ShowNotification(NotificationContext.RestoreFailure, true);
                    Logger.Error(e);
                }
            }

            var backupSucceeded = true;

            if (!restoreTriggered && (backupTriggered || (_preferences.AutoBackupEnabled && restoreSucceeded &&
                                                          _preferences.BackupRequired !=
                                                          BackupRequirement.NotRequired)))
            {
                try
                {
                    await _initTask.Value;
                    var result = await BackupToDir(destination);

                    ShowNotification(NotificationContext.BackupSuccess, false, result);
                    _preferences.BackupRequired = BackupRequirement.NotRequired;

                    // Don't update value if backup triggered, won't combine with restore
                    if (!result.IsVoid() && !backupTriggered)
                    {
                        _preferences.MostRecentBackupModifiedAt = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                    }
                }
                catch (Exception e)
                {
                    backupSucceeded = false;
                    ShowNotification(NotificationContext.BackupFailure, true);
                    Logger.Error(e);
                }
            }

            if (_shouldCloseDatabase)
            {
                await _database.Close();
            }

            return(backupSucceeded && restoreSucceeded
                ? Result.InvokeSuccess()
                : Result.InvokeFailure());
        }