use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.
the class LocalFeedUpdaterTest method mockDocumentFolder.
/**
* Create a DocumentFile folder mock object with a list of files.
*/
@NonNull
private static DocumentFile mockDocumentFolder(DocumentFile... files) {
DocumentFile documentFolder = mock(DocumentFile.class);
when(documentFolder.listFiles()).thenReturn(files);
return documentFolder;
}
use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.
the class LocalFeedUpdaterTest method testGetImageUrl_OtherImageFilenameUnsupportedMimeType.
@Test
public void testGetImageUrl_OtherImageFilenameUnsupportedMimeType() {
DocumentFile documentFolder = mockDocumentFolder(mockDocumentFile("audio.mp3", "audio/mp3"), mockDocumentFile("my-image.svg", "image/svg+xml"));
String imageUrl = LocalFeedUpdater.getImageUrl(context, documentFolder);
String defaultImageName = context.getResources().getResourceEntryName(R.raw.local_feed_default_icon);
assertThat(imageUrl, endsWith(defaultImageName));
}
use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.
the class LocalFeedUpdaterTest method testGetImageUrl_OtherImageFilenameJpg.
@Test
public void testGetImageUrl_OtherImageFilenameJpg() {
DocumentFile documentFolder = mockDocumentFolder(mockDocumentFile("audio.mp3", "audio/mp3"), mockDocumentFile("my-image.jpg", "image/jpeg"));
String imageUrl = LocalFeedUpdater.getImageUrl(context, documentFolder);
assertThat(imageUrl, endsWith("my-image.jpg"));
}
use of androidx.documentfile.provider.DocumentFile in project Signal-Android by WhisperSystems.
the class LocalBackupJobApi29 method onRun.
@Override
public void onRun() throws IOException {
Log.i(TAG, "Executing backup job...");
BackupFileIOError.clearNotification(context);
if (!BackupUtil.isUserSelectionRequired(context)) {
throw new IOException("Wrong backup job!");
}
Uri backupDirectoryUri = SignalStore.settings().getSignalBackupDirectory();
if (backupDirectoryUri == null || backupDirectoryUri.getPath() == null) {
throw new IOException("Backup Directory has not been selected!");
}
ProgressUpdater updater = new ProgressUpdater();
try (NotificationController notification = GenericForegroundService.startForegroundTask(context, context.getString(R.string.LocalBackupJob_creating_signal_backup), NotificationChannels.BACKUPS, R.drawable.ic_signal_backup)) {
updater.setNotification(notification);
EventBus.getDefault().register(updater);
notification.setIndeterminateProgress();
String backupPassword = BackupPassphrase.get(context);
DocumentFile backupDirectory = DocumentFile.fromTreeUri(context, backupDirectoryUri);
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US).format(new Date());
String fileName = String.format("signal-%s.backup", timestamp);
if (backupDirectory == null || !backupDirectory.canWrite()) {
BackupFileIOError.ACCESS_ERROR.postNotification(context);
throw new IOException("Cannot write to backup directory location.");
}
deleteOldTemporaryBackups(backupDirectory);
if (backupDirectory.findFile(fileName) != null) {
throw new IOException("Backup file already exists!");
}
String temporaryName = String.format(Locale.US, "%s%s%s", TEMP_BACKUP_FILE_PREFIX, UUID.randomUUID(), TEMP_BACKUP_FILE_SUFFIX);
DocumentFile temporaryFile = backupDirectory.createFile("application/octet-stream", temporaryName);
if (temporaryFile == null) {
throw new IOException("Failed to create temporary backup file.");
}
if (backupPassword == null) {
throw new IOException("Backup password is null");
}
try {
FullBackupExporter.export(context, AttachmentSecretProvider.getInstance(context).getOrCreateAttachmentSecret(), SignalDatabase.getBackupDatabase(), temporaryFile, backupPassword, this::isCanceled);
if (!temporaryFile.renameTo(fileName)) {
Log.w(TAG, "Failed to rename temp file");
throw new IOException("Renaming temporary backup file failed!");
}
} catch (FullBackupExporter.BackupCanceledException e) {
Log.w(TAG, "Backup cancelled");
throw e;
} catch (IOException e) {
Log.w(TAG, "Error during backup!", e);
BackupFileIOError.postNotificationForException(context, e, getRunAttempt());
throw e;
} finally {
DocumentFile fileToCleanUp = backupDirectory.findFile(temporaryName);
if (fileToCleanUp != null) {
if (fileToCleanUp.delete()) {
Log.w(TAG, "Backup failed. Deleted temp file");
} else {
Log.w(TAG, "Backup failed. Failed to delete temp file " + temporaryName);
}
}
}
BackupUtil.deleteOldBackups();
} finally {
EventBus.getDefault().unregister(updater);
updater.setNotification(null);
}
}
use of androidx.documentfile.provider.DocumentFile in project Signal-Android by WhisperSystems.
the class BackupUtil method getAllBackupsNewestFirstApi29.
@RequiresApi(29)
private static List<BackupInfo> getAllBackupsNewestFirstApi29() {
Uri backupDirectoryUri = SignalStore.settings().getSignalBackupDirectory();
if (backupDirectoryUri == null) {
Log.i(TAG, "Backup directory is not set. Returning an empty list.");
return Collections.emptyList();
}
DocumentFile backupDirectory = DocumentFile.fromTreeUri(ApplicationDependencies.getApplication(), backupDirectoryUri);
if (backupDirectory == null || !backupDirectory.exists() || !backupDirectory.canRead()) {
Log.w(TAG, "Backup directory is inaccessible. Returning an empty list.");
return Collections.emptyList();
}
DocumentFile[] files = backupDirectory.listFiles();
List<BackupInfo> backups = new ArrayList<>(files.length);
for (DocumentFile file : files) {
if (file.isFile() && file.getName() != null && file.getName().endsWith(".backup")) {
long backupTimestamp = getBackupTimestamp(file.getName());
if (backupTimestamp != -1) {
backups.add(new BackupInfo(backupTimestamp, file.length(), file.getUri()));
}
}
}
Collections.sort(backups, (a, b) -> Long.compare(b.timestamp, a.timestamp));
return backups;
}
Aggregations