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);
}
}
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;
}
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);
}
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();
}
});
}
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));
}
}
Aggregations