Skip to content

Commit

Permalink
Merge pull request #1233 from alexbakker/explain-uri-perms
Browse files Browse the repository at this point in the history
Explain vault backup permission error
  • Loading branch information
michaelschattgen committed Nov 29, 2023
2 parents 88caafd + 08c7392 commit 3dd70de
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
15 changes: 12 additions & 3 deletions app/src/main/java/com/beemdevelopment/aegis/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.beemdevelopment.aegis.util.JsonUtils;
import com.beemdevelopment.aegis.util.TimeUtils;
import com.beemdevelopment.aegis.vault.VaultBackupPermissionException;

import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -507,14 +508,16 @@ public static class BackupResult {
private final Date _time;
private boolean _isBuiltIn;
private final String _error;
private final boolean _isPermissionError;

public BackupResult(@Nullable Exception e) {
this(new Date(), e == null ? null : e.toString());
this(new Date(), e == null ? null : e.toString(), e instanceof VaultBackupPermissionException);
}

private BackupResult(Date time, @Nullable String error) {
private BackupResult(Date time, @Nullable String error, boolean isPermissionError) {
_time = time;
_error = error;
_isPermissionError = isPermissionError;
}

@Nullable
Expand Down Expand Up @@ -542,12 +545,17 @@ private void setIsBuiltIn(boolean isBuiltIn) {
_isBuiltIn = isBuiltIn;
}

public boolean isPermissionError() {
return _isPermissionError;
}

public String toJson() {
JSONObject obj = new JSONObject();

try {
obj.put("time", _time.getTime());
obj.put("error", _error == null ? JSONObject.NULL : _error);
obj.put("isPermissionError", _isPermissionError);
} catch (JSONException e) {
throw new RuntimeException(e);
}
Expand All @@ -559,7 +567,8 @@ public static BackupResult fromJson(String json) throws JSONException {
JSONObject obj = new JSONObject(json);
long time = obj.getLong("time");
String error = JsonUtils.optString(obj, "error");
return new BackupResult(new Date(time), error);
boolean isPermissionError = obj.optBoolean("isPermissionError");
return new BackupResult(new Date(time), error, isPermissionError);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ public static void showErrorDialog(Context context, String message, CharSequence

public static void showBackupErrorDialog(Context context, Preferences.BackupResult backupRes, DialogInterface.OnClickListener listener) {
String system = context.getString(backupRes.isBuiltIn() ? R.string.backup_system_builtin : R.string.backup_system_android);
String message = context.getString(R.string.backup_error_dialog_details, system, backupRes.getElapsedSince(context));
@StringRes int details = backupRes.isPermissionError() ? R.string.backup_permission_error_dialog_details : R.string.backup_error_dialog_details;
String message = context.getString(details, system, backupRes.getElapsedSince(context));
Dialogs.showErrorDialog(context, message, backupRes.getError(), listener);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,23 @@ public void scheduleBackup(File tempFile, Uri dirUri, int versionsToKeep) {
try {
createBackup(tempFile, dirUri, versionsToKeep);
_prefs.setBuiltInBackupResult(new Preferences.BackupResult(null));
} catch (VaultRepositoryException e) {
} catch (VaultRepositoryException | VaultBackupPermissionException e) {
e.printStackTrace();
_prefs.setBuiltInBackupResult(new Preferences.BackupResult(e));
}
});
}

private void createBackup(File tempFile, Uri dirUri, int versionsToKeep) throws VaultRepositoryException {
private void createBackup(File tempFile, Uri dirUri, int versionsToKeep)
throws VaultRepositoryException, VaultBackupPermissionException {
FileInfo fileInfo = new FileInfo(FILENAME_PREFIX);
DocumentFile dir = DocumentFile.fromTreeUri(_context, dirUri);

try {
Log.i(TAG, String.format("Creating backup at %s: %s", Uri.decode(dir.getUri().toString()), fileInfo.toString()));

if (!hasPermissionsAt(dirUri)) {
throw new VaultRepositoryException("No persisted URI permissions");
throw new VaultBackupPermissionException("No persisted URI permissions");
}

// If we create a file with a name that already exists, SAF will append a number
Expand All @@ -92,7 +93,7 @@ private void createBackup(File tempFile, Uri dirUri, int versionsToKeep) throws
} catch (IOException e) {
throw new VaultRepositoryException(e);
}
} catch (VaultRepositoryException e) {
} catch (VaultRepositoryException | VaultBackupPermissionException e) {
Log.e(TAG, String.format("Unable to create backup: %s", e.toString()));
throw e;
} finally {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.beemdevelopment.aegis.vault;

public class VaultBackupPermissionException extends Exception {
public VaultBackupPermissionException(String message) {
super(message);
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@
comment="The first parameter is the type of backup (e.g. built-in or Android backup). The second parameter is an elapsed time in the style of 'x seconds/minutes/days ago'.">
A recent vault backup attempt using %s failed because an error occurred. The backup was attempted %s. Please check your backup settings to make sure backups can complete successfully.
</string>
<string
name="backup_permission_error_dialog_details"
comment="The first parameter is the type of backup (e.g. built-in or Android backup). The second parameter is an elapsed time in the style of 'x seconds/minutes/days ago'.">
A recent vault backup attempt using %s failed because Aegis did not have permission to write to the backup destination. The backup was attempted %s. This error can occur if you moved/renamed the backup destination or if you recently restored Aegis from a backup. Please reconfigure the backup destination.
</string>
<string name="backup_system_builtin">Aegis\' built-in automatic backups</string>
<string name="backup_system_android">Android\'s cloud backup system</string>
<string
Expand Down

0 comments on commit 3dd70de

Please sign in to comment.