Search in sources :

Example 1 with NoExternalStorageException

use of org.thoughtcrime.securesms.database.NoExternalStorageException in project Signal-Android by WhisperSystems.

the class StorageUtil method getOrCreateBackupDirectory.

public static File getOrCreateBackupDirectory() throws NoExternalStorageException {
    File storage = Environment.getExternalStorageDirectory();
    if (!storage.canWrite()) {
        throw new NoExternalStorageException();
    }
    File backups = getBackupDirectory();
    if (!backups.exists()) {
        if (!backups.mkdirs()) {
            throw new NoExternalStorageException("Unable to create backup directory...");
        }
    }
    return backups;
}
Also used : NoExternalStorageException(org.thoughtcrime.securesms.database.NoExternalStorageException) File(java.io.File)

Example 2 with NoExternalStorageException

use of org.thoughtcrime.securesms.database.NoExternalStorageException in project Signal-Android by signalapp.

the class BackupUtil method deleteOldBackups.

public static void deleteOldBackups() {
    try {
        File backupDirectory = StorageUtil.getBackupDirectory();
        File[] backups = backupDirectory.listFiles();
        if (backups != null && backups.length > 5) {
            Arrays.sort(backups, (left, right) -> {
                long leftTimestamp = getBackupTimestamp(left);
                long rightTimestamp = getBackupTimestamp(right);
                if (leftTimestamp == -1 && rightTimestamp == -1)
                    return 0;
                else if (leftTimestamp == -1)
                    return 1;
                else if (rightTimestamp == -1)
                    return -1;
                return (int) (rightTimestamp - leftTimestamp);
            });
            for (int i = 5; i < backups.length; i++) {
                Log.w(TAG, "Deleting: " + backups[i].getAbsolutePath());
                if (!backups[i].delete()) {
                    Log.w(TAG, "Delete failed: " + backups[i].getAbsolutePath());
                }
            }
        }
    } catch (NoExternalStorageException e) {
        Log.w(TAG, e);
    }
}
Also used : NoExternalStorageException(org.thoughtcrime.securesms.database.NoExternalStorageException) File(java.io.File)

Example 3 with NoExternalStorageException

use of org.thoughtcrime.securesms.database.NoExternalStorageException in project Signal-Android by signalapp.

the class BackupUtil method deleteAllBackups.

@SuppressWarnings("ResultOfMethodCallIgnored")
public static void deleteAllBackups() {
    try {
        File backupDirectory = StorageUtil.getBackupDirectory();
        File[] backups = backupDirectory.listFiles();
        for (File backup : backups) {
            if (backup.isFile())
                backup.delete();
        }
    } catch (NoExternalStorageException e) {
        Log.w(TAG, e);
    }
}
Also used : NoExternalStorageException(org.thoughtcrime.securesms.database.NoExternalStorageException) File(java.io.File)

Example 4 with NoExternalStorageException

use of org.thoughtcrime.securesms.database.NoExternalStorageException in project Signal-Android by WhisperSystems.

the class BackupsPreferenceFragment method setBackupFolderName.

private void setBackupFolderName() {
    folder.setVisibility(View.GONE);
    if (BackupUtil.canUserAccessBackupDirectory(requireContext())) {
        if (BackupUtil.isUserSelectionRequired(requireContext()) && BackupUtil.canUserAccessBackupDirectory(requireContext())) {
            Uri backupUri = Objects.requireNonNull(SignalStore.settings().getSignalBackupDirectory());
            folder.setVisibility(View.VISIBLE);
            folderName.setText(StorageUtil.getDisplayPath(requireContext(), backupUri));
        } else if (StorageUtil.canWriteInSignalStorageDir()) {
            try {
                folder.setVisibility(View.VISIBLE);
                folderName.setText(StorageUtil.getOrCreateBackupDirectory().getPath());
            } catch (NoExternalStorageException e) {
                Log.w(TAG, "Could not display folder name.", e);
            }
        }
    }
}
Also used : NoExternalStorageException(org.thoughtcrime.securesms.database.NoExternalStorageException) Uri(android.net.Uri)

Example 5 with NoExternalStorageException

use of org.thoughtcrime.securesms.database.NoExternalStorageException in project Signal-Android by signalapp.

the class BackupsPreferenceFragment method setBackupFolderName.

private void setBackupFolderName() {
    folder.setVisibility(View.GONE);
    if (BackupUtil.canUserAccessBackupDirectory(requireContext())) {
        if (BackupUtil.isUserSelectionRequired(requireContext()) && BackupUtil.canUserAccessBackupDirectory(requireContext())) {
            Uri backupUri = Objects.requireNonNull(SignalStore.settings().getSignalBackupDirectory());
            folder.setVisibility(View.VISIBLE);
            folderName.setText(StorageUtil.getDisplayPath(requireContext(), backupUri));
        } else if (StorageUtil.canWriteInSignalStorageDir()) {
            try {
                folder.setVisibility(View.VISIBLE);
                folderName.setText(StorageUtil.getOrCreateBackupDirectory().getPath());
            } catch (NoExternalStorageException e) {
                Log.w(TAG, "Could not display folder name.", e);
            }
        }
    }
}
Also used : NoExternalStorageException(org.thoughtcrime.securesms.database.NoExternalStorageException) Uri(android.net.Uri)

Aggregations

NoExternalStorageException (org.thoughtcrime.securesms.database.NoExternalStorageException)6 File (java.io.File)4 Uri (android.net.Uri)2