use of android.support.v4.provider.DocumentFile in project Shuttle by timusus.
the class SafManager method getWriteableDocumentFiles.
/**
* Checks the passed in paths to see whether the file at the given path is available in our
* document tree. If it is, and we have write permission, the document file is added to the
* passed in list of document files.
*/
public List<DocumentFile> getWriteableDocumentFiles(List<File> files) {
List<DocumentFile> documentFiles = new ArrayList<>();
String treeUri = getDocumentTree();
if (treeUri == null) {
// We don't have any document tree at all - so we're not going to have permission for any files.
return documentFiles;
}
// we're satisfied we don't have permission.
for (File file : files) {
DocumentFile documentFile = getWriteableDocumentFile(file);
if (documentFile != null && documentFile.canWrite()) {
documentFiles.add(documentFile);
}
}
return documentFiles;
}
use of android.support.v4.provider.DocumentFile in project Shuttle by timusus.
the class DeleteDialog method deleteSongs.
@SuppressLint("CheckResult")
Single<Integer> deleteSongs() {
return Single.fromCallable(() -> {
int deletedSongs = 0;
if (!documentFilesForDeletion.isEmpty()) {
deletedSongs += Stream.of(documentFilesForDeletion).filter(DocumentFile::delete).count();
tidyUp(songsForSafDeletion);
documentFilesForDeletion.clear();
songsForSafDeletion.clear();
}
if (!songsForNormalDeletion.isEmpty()) {
deletedSongs += Stream.of(songsForNormalDeletion).filter(SongExtKt::delete).count();
tidyUp(songsForNormalDeletion);
songsForNormalDeletion.clear();
}
return deletedSongs;
});
}
use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.
the class DocumentStorageService method load.
private void load(Uri document) {
try {
boolean weHavePermission = false;
boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());
if (isContent) {
int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
getContentResolver().takePersistableUriPermission(document, perms);
for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
if (perm.getUri().equals(document)) {
weHavePermission = true;
}
}
} else {
weHavePermission = true;
}
if (weHavePermission) {
try {
InputStream is = getContentResolver().openInputStream(document);
try {
String text = slurp(is);
DocumentFile docFile;
if (isContent) {
docFile = DocumentFile.fromSingleUri(this, document);
} else {
docFile = DocumentFile.fromFile(new File(document.getPath()));
}
EventBus.getDefault().post(new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
} finally {
is.close();
}
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
}
} else {
Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
}
} catch (SecurityException e) {
Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
}
}
use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.
the class DurablizerService method onHandleIntent.
@Override
protected void onHandleIntent(Intent intent) {
Uri document = intent.getData();
boolean weHaveDurablePermission = obtainDurablePermission(document);
if (!weHaveDurablePermission) {
document = makeLocalCopy(document);
}
if (weHaveDurablePermission || document != null) {
Log.d(getClass().getSimpleName(), document.toString());
DocumentFile docFile = buildDocFileForUri(document);
Log.d(getClass().getSimpleName(), "Display name: " + docFile.getName());
Log.d(getClass().getSimpleName(), "Size: " + Long.toString(docFile.length()));
EventBus.getDefault().post(new ContentReadyEvent(docFile));
}
}
use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.
the class DurablizerService method makeLocalCopy.
private Uri makeLocalCopy(Uri document) {
DocumentFile docFile = buildDocFileForUri(document);
Uri result = null;
if (docFile.getName() != null) {
File f = new File(getFilesDir(), docFile.getName());
try {
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream out = new BufferedOutputStream(fos);
InputStream in = getContentResolver().openInputStream(document);
try {
byte[] buffer = new byte[8192];
int len = 0;
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
out.flush();
result = Uri.fromFile(f);
} finally {
fos.getFD().sync();
out.close();
in.close();
}
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Exception copying content to file", e);
}
}
return (result);
}
Aggregations