Search in sources :

Example 16 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project Signal-Android by WhisperSystems.

the class BackupUtil method canUserAccessBackupDirectory.

public static boolean canUserAccessBackupDirectory(@NonNull Context context) {
    if (isUserSelectionRequired(context)) {
        Uri backupDirectoryUri = SignalStore.settings().getSignalBackupDirectory();
        if (backupDirectoryUri == null) {
            return false;
        }
        DocumentFile backupDirectory = DocumentFile.fromTreeUri(context, backupDirectoryUri);
        return backupDirectory != null && backupDirectory.exists() && backupDirectory.canRead() && backupDirectory.canWrite();
    } else {
        return Permissions.hasAll(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) Uri(android.net.Uri)

Example 17 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class AddFeedFragment method addLocalFolder.

private Feed addLocalFolder(Uri uri) {
    if (Build.VERSION.SDK_INT < 21) {
        return null;
    }
    getActivity().getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    DocumentFile documentFile = DocumentFile.fromTreeUri(getContext(), uri);
    if (documentFile == null) {
        throw new IllegalArgumentException("Unable to retrieve document tree");
    }
    String title = documentFile.getName();
    if (title == null) {
        title = getString(R.string.local_folder);
    }
    Feed dirFeed = new Feed(Feed.PREFIX_LOCAL_FOLDER + uri.toString(), null, title);
    dirFeed.setItems(Collections.emptyList());
    dirFeed.setSortOrder(SortOrder.EPISODE_TITLE_A_Z);
    Feed fromDatabase = DBTasks.updateFeed(getContext(), dirFeed, false);
    DBTasks.forceRefreshFeed(getContext(), fromDatabase, true);
    return fromDatabase;
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) Feed(de.danoeh.antennapod.model.feed.Feed)

Example 18 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdater method tryUpdateFeed.

private static void tryUpdateFeed(Feed feed, Context context) throws IOException {
    String uriString = feed.getDownload_url().replace(Feed.PREFIX_LOCAL_FOLDER, "");
    DocumentFile documentFolder = DocumentFile.fromTreeUri(context, Uri.parse(uriString));
    if (documentFolder == null) {
        throw new IOException("Unable to retrieve document tree. " + "Try re-connecting the folder on the podcast info page.");
    }
    if (!documentFolder.exists() || !documentFolder.canRead()) {
        throw new IOException("Cannot read local directory. " + "Try re-connecting the folder on the podcast info page.");
    }
    if (feed.getItems() == null) {
        feed.setItems(new ArrayList<>());
    }
    // make sure it is the latest 'version' of this feed from the db (all items etc)
    feed = DBTasks.updateFeed(context, feed, false);
    // list files in feed folder
    List<DocumentFile> mediaFiles = new ArrayList<>();
    Set<String> mediaFileNames = new HashSet<>();
    for (DocumentFile file : documentFolder.listFiles()) {
        String mime = file.getType();
        if (mime == null) {
            continue;
        }
        MediaType mediaType = MediaType.fromMimeType(mime);
        if (mediaType == MediaType.UNKNOWN) {
            String path = file.getUri().toString();
            int fileExtensionPosition = path.lastIndexOf('.');
            if (fileExtensionPosition >= 0) {
                String extensionWithoutDot = path.substring(fileExtensionPosition + 1);
                mediaType = MediaType.fromFileExtension(extensionWithoutDot);
            }
        }
        if (mediaType == MediaType.AUDIO || mediaType == MediaType.VIDEO) {
            mediaFiles.add(file);
            mediaFileNames.add(file.getName());
        }
    }
    // add new files to feed and update item data
    List<FeedItem> newItems = feed.getItems();
    for (DocumentFile f : mediaFiles) {
        FeedItem oldItem = feedContainsFile(feed, f.getName());
        FeedItem newItem = createFeedItem(feed, f, context);
        if (oldItem == null) {
            newItems.add(newItem);
        } else {
            oldItem.updateFromOther(newItem);
        }
    }
    // remove feed items without corresponding file
    Iterator<FeedItem> it = newItems.iterator();
    while (it.hasNext()) {
        FeedItem feedItem = it.next();
        if (!mediaFileNames.contains(feedItem.getLink())) {
            it.remove();
        }
    }
    feed.setImageUrl(getImageUrl(context, documentFolder));
    feed.getPreferences().setAutoDownload(false);
    feed.getPreferences().setAutoDeleteAction(FeedPreferences.AutoDeleteAction.NO);
    feed.setDescription(context.getString(R.string.local_feed_description));
    feed.setAuthor(context.getString(R.string.local_folder));
    // update items, delete items without existing file;
    // only delete items if the folder contains at least one element to avoid accidentally
    // deleting played state or position in case the folder is temporarily unavailable.
    boolean removeUnlistedItems = (newItems.size() >= 1);
    DBTasks.updateFeed(context, feed, removeUnlistedItems);
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FeedItem(de.danoeh.antennapod.model.feed.FeedItem) MediaType(de.danoeh.antennapod.model.playback.MediaType) HashSet(java.util.HashSet)

Example 19 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class DocumentFileExportWorker method exportObservable.

public Observable<DocumentFile> exportObservable() {
    DocumentFile output = DocumentFile.fromSingleUri(context, outputFileUri);
    return Observable.create(subscriber -> {
        OutputStream outputStream = null;
        OutputStreamWriter writer = null;
        try {
            Uri uri = output.getUri();
            outputStream = context.getContentResolver().openOutputStream(uri);
            if (outputStream == null) {
                throw new IOException();
            }
            writer = new OutputStreamWriter(outputStream, Charset.forName("UTF-8"));
            exportWriter.writeDocument(DBReader.getFeedList(), writer, context);
            subscriber.onNext(output);
        } catch (IOException e) {
            subscriber.onError(e);
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    subscriber.onError(e);
                }
            }
            subscriber.onComplete();
        }
    });
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) Uri(android.net.Uri)

Example 20 with DocumentFile

use of androidx.documentfile.provider.DocumentFile in project AntennaPod by AntennaPod.

the class LocalFeedUpdaterTest method testGetImageUrl_PreferredImagesFilenames.

@Test
public void testGetImageUrl_PreferredImagesFilenames() {
    for (String filename : LocalFeedUpdater.PREFERRED_FEED_IMAGE_FILENAMES) {
        DocumentFile documentFolder = mockDocumentFolder(mockDocumentFile("audio.mp3", "audio/mp3"), // image MIME type doesn't matter
        mockDocumentFile(filename, "image/jpeg"));
        String imageUrl = LocalFeedUpdater.getImageUrl(context, documentFolder);
        assertThat(imageUrl, endsWith(filename));
    }
}
Also used : DocumentFile(androidx.documentfile.provider.DocumentFile) AssetsDocumentFile(androidx.documentfile.provider.AssetsDocumentFile) Test(org.junit.Test)

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