mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-22 10:46:50 +00:00
Add support for specific toasts when backup restoration cannot proceed.
Fixes #10918
This commit is contained in:
committed by
Greyson Parrelli
parent
80598814bd
commit
63dab3f4b0
@@ -26,6 +26,7 @@ import androidx.annotation.MainThread;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.annotation.StringRes;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.lifecycle.ViewModelProvider;
|
||||
import androidx.navigation.Navigation;
|
||||
@@ -37,6 +38,7 @@ import net.zetetic.database.sqlcipher.SQLiteDatabase;
|
||||
import org.greenrobot.eventbus.EventBus;
|
||||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.greenrobot.eventbus.ThreadMode;
|
||||
import org.signal.core.util.ThreadUtil;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.AppInitialization;
|
||||
import org.thoughtcrime.securesms.LoggingFragment;
|
||||
@@ -216,10 +218,36 @@ public final class RestoreBackupFragment extends LoggingFragment {
|
||||
@NonNull Uri backupUri,
|
||||
@NonNull OnBackupSearchResultListener listener)
|
||||
{
|
||||
SimpleTask.run(() -> BackupUtil.getBackupInfoFromSingleUri(context, backupUri),
|
||||
SimpleTask.run(() -> {
|
||||
try {
|
||||
return BackupUtil.getBackupInfoFromSingleUri(context, backupUri);
|
||||
} catch (BackupUtil.BackupFileException e) {
|
||||
Log.w(TAG, "Could not restore backup.", e);
|
||||
postToastForBackupRestorationFailure(context, e);
|
||||
return null;
|
||||
}
|
||||
},
|
||||
listener::run);
|
||||
}
|
||||
|
||||
private static void postToastForBackupRestorationFailure(@NonNull Context context, @NonNull BackupUtil.BackupFileException exception) {
|
||||
final @StringRes int errorResId;
|
||||
switch (exception.getState()) {
|
||||
case READABLE:
|
||||
throw new AssertionError("Unexpected error state.");
|
||||
case NOT_FOUND:
|
||||
errorResId = R.string.RestoreBackupFragment__backup_not_found;
|
||||
break;
|
||||
case UNSUPPORTED_FILE_EXTENSION:
|
||||
errorResId = R.string.RestoreBackupFragment__backup_has_a_bad_extension;
|
||||
break;
|
||||
default:
|
||||
errorResId = R.string.RestoreBackupFragment__backup_could_not_be_read;
|
||||
}
|
||||
|
||||
ThreadUtil.postToMain(() -> Toast.makeText(context, errorResId, Toast.LENGTH_LONG).show());
|
||||
}
|
||||
|
||||
private void handleRestore(@NonNull Context context, @NonNull BackupUtil.BackupInfo backup) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.enter_backup_passphrase_dialog, null);
|
||||
EditText prompt = view.findViewById(R.id.restore_passphrase_input);
|
||||
|
||||
@@ -6,11 +6,11 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
|
||||
import org.signal.core.util.logging.Log;
|
||||
@@ -166,14 +166,22 @@ public class BackupUtil {
|
||||
return backups;
|
||||
}
|
||||
|
||||
public static @Nullable BackupInfo getBackupInfoFromSingleUri(@NonNull Context context, @NonNull Uri singleUri) {
|
||||
DocumentFile documentFile = DocumentFile.fromSingleUri(context, singleUri);
|
||||
public static @Nullable BackupInfo getBackupInfoFromSingleUri(@NonNull Context context, @NonNull Uri singleUri) throws BackupFileException {
|
||||
DocumentFile documentFile = Objects.requireNonNull(DocumentFile.fromSingleUri(context, singleUri));
|
||||
|
||||
if (isBackupFileReadable(documentFile)) {
|
||||
return getBackupInfoFromSingleDocumentFile(documentFile);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static @Nullable BackupInfo getBackupInfoFromSingleDocumentFile(@NonNull DocumentFile documentFile) throws BackupFileException {
|
||||
BackupFileState backupFileState = getBackupFileState(documentFile);
|
||||
|
||||
if (backupFileState.isSuccess()) {
|
||||
long backupTimestamp = getBackupTimestamp(Objects.requireNonNull(documentFile.getName()));
|
||||
return new BackupInfo(backupTimestamp, documentFile.length(), documentFile.getUri());
|
||||
} else {
|
||||
Log.w(TAG, "Could not load backup info.");
|
||||
backupFileState.throwIfError();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -258,21 +266,58 @@ public class BackupUtil {
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static boolean isBackupFileReadable(@Nullable DocumentFile documentFile) {
|
||||
if (documentFile == null) {
|
||||
throw new AssertionError("We do not support platforms prior to KitKat.");
|
||||
} else if (!documentFile.exists()) {
|
||||
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri cannot be found.");
|
||||
return false;
|
||||
private static BackupFileState getBackupFileState(@NonNull DocumentFile documentFile) {
|
||||
if (!documentFile.exists()) {
|
||||
return BackupFileState.NOT_FOUND;
|
||||
} else if (!documentFile.canRead()) {
|
||||
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri cannot be read.");
|
||||
return false;
|
||||
} else if (TextUtils.isEmpty(documentFile.getName()) || !documentFile.getName().endsWith(".backup")) {
|
||||
Log.w(TAG, "isBackupFileReadable: The document at the specified Uri has an unsupported file extension.");
|
||||
return false;
|
||||
return BackupFileState.NOT_READABLE;
|
||||
} else if (Util.isEmpty(documentFile.getName()) || !documentFile.getName().endsWith(".backup")) {
|
||||
return BackupFileState.UNSUPPORTED_FILE_EXTENSION;
|
||||
} else {
|
||||
Log.i(TAG, "isBackupFileReadable: The document at the specified Uri looks like a readable backup");
|
||||
return true;
|
||||
return BackupFileState.READABLE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the validity of a backup file.
|
||||
*/
|
||||
public enum BackupFileState {
|
||||
READABLE("The document at the specified Uri looks like a readable backup."),
|
||||
NOT_FOUND("The document at the specified Uri cannot be found."),
|
||||
NOT_READABLE("The document at the specified Uri cannot be read."),
|
||||
UNSUPPORTED_FILE_EXTENSION("The document at the specified Uri has an unsupported file extension.");
|
||||
|
||||
private final String message;
|
||||
|
||||
BackupFileState(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return this == READABLE;
|
||||
}
|
||||
|
||||
public void throwIfError() throws BackupFileException {
|
||||
if (!isSuccess()) {
|
||||
throw new BackupFileException(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapping exception for a non-successful BackupFileState.
|
||||
*/
|
||||
public static class BackupFileException extends Exception {
|
||||
|
||||
private final BackupFileState state;
|
||||
|
||||
BackupFileException(BackupFileState backupFileState) {
|
||||
super(backupFileState.message);
|
||||
this.state = backupFileState;
|
||||
}
|
||||
|
||||
public @NonNull BackupFileState getState() {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user