Search in sources :

Example 11 with DocumentFile

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;
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) NonNull(androidx.annotation.NonNull)

Example 12 with DocumentFile

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));
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Test(org.junit.Test)

Example 13 with DocumentFile

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"));
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Test(org.junit.Test)

Example 14 with DocumentFile

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);
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) IOException(java.io.IOException) Uri(android.net.Uri) SimpleDateFormat(java.text.SimpleDateFormat) NotificationController(org.thoughtcrime.securesms.service.NotificationController) Date(java.util.Date) FullBackupExporter(org.thoughtcrime.securesms.backup.FullBackupExporter)

Example 15 with DocumentFile

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;
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ArrayList(java.util.ArrayList) Uri(android.net.Uri) RequiresApi(androidx.annotation.RequiresApi)

Aggregations

DocumentFile (androidx.documentfile.provider.DocumentFile)32 Uri (android.net.Uri)12 AssetsDocumentFile (androidx.documentfile.provider.AssetsDocumentFile)10 Test (org.junit.Test)8 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)5 OutputStream (java.io.OutputStream)3 Intent (android.content.Intent)2 NonNull (androidx.annotation.NonNull)2 RequiresApi (androidx.annotation.RequiresApi)2 Feed (de.danoeh.antennapod.model.feed.Feed)2 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 FullBackupExporter (org.thoughtcrime.securesms.backup.FullBackupExporter)2 NotificationController (org.thoughtcrime.securesms.service.NotificationController)2 Instrumentation (android.app.Instrumentation)1 ActivityNotFoundException (android.content.ActivityNotFoundException)1