use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class WriteFileAbstraction method doInBackground.
@Override
protected Integer doInBackground(Void... voids) {
try {
OutputStream outputStream;
File destFile = null;
switch(fileAbstraction.scheme) {
case CONTENT:
if (fileAbstraction.uri == null)
throw new NullPointerException("Something went really wrong!");
try {
if (fileAbstraction.uri.getAuthority().equals(context.get().getPackageName())) {
DocumentFile documentFile = DocumentFile.fromSingleUri(AppConfig.getInstance(), fileAbstraction.uri);
if (documentFile != null && documentFile.exists() && documentFile.canWrite())
outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
else {
destFile = FileUtils.fromContentUri(fileAbstraction.uri);
outputStream = openFile(destFile, context.get());
}
} else {
outputStream = contentResolver.openOutputStream(fileAbstraction.uri);
}
} catch (RuntimeException e) {
throw new StreamNotFoundException(e);
}
break;
case FILE:
final HybridFileParcelable hybridFileParcelable = fileAbstraction.hybridFileParcelable;
if (hybridFileParcelable == null)
throw new NullPointerException("Something went really wrong!");
Context context = this.context.get();
if (context == null) {
cancel(true);
return null;
}
outputStream = openFile(hybridFileParcelable.getFile(), context);
destFile = fileAbstraction.hybridFileParcelable.getFile();
break;
default:
throw new IllegalArgumentException("The scheme for '" + fileAbstraction.scheme + "' cannot be processed!");
}
if (outputStream == null)
throw new StreamNotFoundException();
outputStream.write(dataToSave.getBytes());
outputStream.close();
if (cachedFile != null && cachedFile.exists() && destFile != null) {
// cat cache content to original file and delete cache file
ConcatenateFileCommand.INSTANCE.concatenateFile(cachedFile.getPath(), destFile.getPath());
cachedFile.delete();
}
} catch (IOException e) {
e.printStackTrace();
return EXCEPTION_IO;
} catch (StreamNotFoundException e) {
e.printStackTrace();
return EXCEPTION_STREAM_NOT_FOUND;
} catch (ShellNotRunningException e) {
e.printStackTrace();
return EXCEPTION_SHELL_NOT_RUNNING;
}
return NORMAL;
}
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();
}
});
}
Aggregations