use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method mkdirs.
@Override
public boolean mkdirs(File file) {
if (file.mkdirs()) {
return true;
}
DocumentFile f = getDirectory(app, file, false);
if (f != null) {
// already exists
return false;
}
f = getDirectory(app, file, true);
return f != null;
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method copy.
@Override
public boolean copy(File src, File dest) {
try {
FileUtils.copyFile(src, dest);
return true;
} catch (Throwable e) {
// ignore
}
DocumentFile srcF = getFile(app, src, false);
DocumentFile destF = getFile(app, dest, true);
if (srcF == null) {
LOG.error("Unable to obtain document for file: " + src);
return false;
}
if (destF == null) {
LOG.error("Unable to obtain or create document for file: " + dest);
return false;
}
return copy(app, srcF, destF);
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method openFD.
public int openFD(File file, String mode) {
if (!("r".equals(mode) || "w".equals(mode) || "rw".equals(mode))) {
LOG.error("Only r, w or rw modes supported");
return -1;
}
DocumentFile f = getFile(app, file, true);
if (f == null) {
LOG.error("Unable to obtain or create document for file: " + file);
return -1;
}
try {
ContentResolver cr = app.getContentResolver();
ParcelFileDescriptor fd = cr.openFileDescriptor(f.getUri(), mode);
if (fd == null) {
return -1;
}
return fd.detachFd();
} catch (Throwable e) {
LOG.error("Unable to get native fd", e);
return -1;
}
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method getDirectory.
private static DocumentFile getDirectory(Context context, File dir, boolean create) {
try {
String path = dir.getAbsolutePath();
DocumentFile cached = CACHE.get(path);
if (cached != null && cached.isDirectory()) {
return cached;
}
String baseFolder = getExtSdCardFolder(context, dir);
if (baseFolder == null) {
if (create) {
return dir.mkdirs() ? DocumentFile.fromFile(dir) : null;
} else {
return dir.isDirectory() ? DocumentFile.fromFile(dir) : null;
}
}
baseFolder = combineRoot(baseFolder);
String fullPath = dir.getAbsolutePath();
String relativePath = baseFolder.length() < fullPath.length() ? fullPath.substring(baseFolder.length() + 1) : "";
String[] segments = relativePath.split("/");
Uri rootUri = getDocumentUri(context, new File(baseFolder));
DocumentFile f = DocumentFile.fromTreeUri(context, rootUri);
// special FrostWire case
if (create) {
if (baseFolder.endsWith("/FrostWire") && !f.exists()) {
baseFolder = baseFolder.substring(0, baseFolder.length() - 10);
rootUri = getDocumentUri(context, new File(baseFolder));
f = DocumentFile.fromTreeUri(context, rootUri);
f = f.findFile("FrostWire");
if (f == null) {
f = f.createDirectory("FrostWire");
if (f == null) {
return null;
}
}
}
}
for (String segment : segments) {
DocumentFile child = f.findFile(segment);
if (child != null) {
f = child;
} else {
if (create) {
f = f.createDirectory(segment);
if (f == null) {
return null;
}
} else {
return null;
}
}
}
f = f.isDirectory() ? f : null;
if (f != null) {
CACHE.put(path, f);
}
return f;
} catch (Throwable e) {
LOG.error("Error getting directory: " + dir, e);
return null;
}
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class StoragePicker method handle.
public static String handle(Context context, int requestCode, int resultCode, Intent data) {
String result = null;
try {
if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) {
Uri treeUri = data.getData();
ContentResolver cr = context.getContentResolver();
final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
cr.takePersistableUriPermission(treeUri, takeFlags);
if (treeUri == null) {
UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null);
result = null;
} else {
DocumentFile file = DocumentFile.fromTreeUri(context, treeUri);
if (!file.isDirectory()) {
UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory);
result = null;
} else if (!file.canWrite()) {
UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write);
result = null;
} else {
LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem();
result = fs.getTreePath(treeUri);
if (result != null && !result.endsWith("/FrostWire")) {
DocumentFile f = file.findFile("FrostWire");
if (f == null) {
file.createDirectory("FrostWire");
}
}
}
}
}
} catch (Throwable e) {
UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error);
LOG.error("Error handling folder selection", e);
result = null;
}
if (result != null) {
ConfigurationManager.instance().setStoragePath(result);
BTEngine.ctx.dataDir = Platforms.data();
BTEngine.ctx.torrentsDir = Platforms.torrents();
}
return result;
}
Aggregations