private async Task DoBackup(Uri uri, string password) { var backup = new Backup( _authSource.GetAll(), _categorySource.GetAll(), _authSource.CategoryBindings, _customIconSource.GetAll() ); var dataToWrite = backup.ToBytes(password); // This is the only way of reliably writing files using SAF on Xamarin. // A file output stream will usually create 0 byte files on virtual storage such as Google Drive var output = ContentResolver.OpenOutputStream(uri, "rwt"); var dataStream = new DataOutputStream(output); try { await dataStream.WriteAsync(dataToWrite); await dataStream.FlushAsync(); } finally { dataStream.Close(); output.Close(); } }
private async Task <BackupResult> BackupToDir(Uri destUri) { if (!_authSource.GetAll().Any()) { return(new BackupResult()); } if (!HasPersistablePermissionsAtUri(destUri)) { throw new Exception("No permission at URI"); } var password = await SecureStorageWrapper.GetAutoBackupPassword(); if (password == null) { throw new Exception("No password defined."); } var backup = new Backup( _authSource.GetAll(), _categorySource.GetAll(), _authSource.CategoryBindings, _customIconSource.GetAll() ); var dataToWrite = backup.ToBytes(password); var directory = DocumentFile.FromTreeUri(_context, destUri); var file = directory.CreateFile(Backup.MimeType, $"backup-{DateTime.Now:yyyy-MM-dd_HHmmss}.{Backup.FileExtension}"); if (file == null) { throw new Exception("File creation failed, got null."); } await FileUtil.WriteFile(_context, file.Uri, dataToWrite); return(new BackupResult(file.Name)); }
private async Task GetSyncBundle(string nodeId) { await _authSource.Update(); var auths = new List <WearAuthenticator>(); foreach (var auth in _authSource.GetView()) { var bindings = _authSource.CategoryBindings .Where(c => c.AuthenticatorSecret == auth.Secret) .Select(c => new WearAuthenticatorCategory(c.CategoryId, c.Ranking)) .ToList(); var item = new WearAuthenticator( auth.Type, auth.Secret, auth.Icon, auth.Issuer, auth.Username, auth.Period, auth.Digits, auth.Algorithm, auth.Ranking, bindings); auths.Add(item); } await _categorySource.Update(); var categories = _categorySource.GetAll().Select(c => new WearCategory(c.Id, c.Name)).ToList(); await _customIconSource.Update(); var customIconIds = _customIconSource.GetAll().Select(i => i.Id).ToList(); var preferenceWrapper = new PreferenceWrapper(this); var preferences = new WearPreferences(preferenceWrapper.DefaultCategory, preferenceWrapper.SortMode); var bundle = new WearSyncBundle(auths, categories, customIconIds, preferences); var json = JsonConvert.SerializeObject(bundle); var data = Encoding.UTF8.GetBytes(json); await WearableClass.GetMessageClient(this).SendMessageAsync(nodeId, GetSyncBundleCapability, data); }