use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class HybridFile method getInputStream.
@Nullable
public InputStream getInputStream(Context context) {
InputStream inputStream;
switch(mode) {
case SFTP:
inputStream = SshClientUtils.execute(new SFtpClientTemplate<InputStream>(path, false) {
@Override
public InputStream execute(final SFTPClient client) throws IOException {
final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path));
return rf.new RemoteFileInputStream() {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
rf.close();
client.close();
}
}
};
}
});
break;
case SMB:
try {
inputStream = getSmbFile().getInputStream();
} catch (IOException e) {
inputStream = null;
e.printStackTrace();
}
break;
case DOCUMENT_FILE:
ContentResolver contentResolver = context.getContentResolver();
DocumentFile documentSourceFile = getDocumentFile(false);
try {
inputStream = contentResolver.openInputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
inputStream = null;
}
break;
case OTG:
contentResolver = context.getContentResolver();
documentSourceFile = OTGUtil.getDocumentFile(path, context, false);
try {
inputStream = contentResolver.openInputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
inputStream = null;
}
break;
case DROPBOX:
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
Log.d(getClass().getSimpleName(), CloudUtil.stripPath(OpenMode.DROPBOX, path));
inputStream = cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, path));
break;
case BOX:
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
inputStream = cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, path));
break;
case GDRIVE:
CloudStorage cloudStorageGDrive = dataUtils.getAccount(OpenMode.GDRIVE);
inputStream = cloudStorageGDrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, path));
break;
case ONEDRIVE:
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
inputStream = cloudStorageOneDrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
break;
default:
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
inputStream = null;
e.printStackTrace();
}
break;
}
return inputStream;
}
use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class HybridFile method getOutputStream.
@Nullable
public OutputStream getOutputStream(Context context) {
OutputStream outputStream;
switch(mode) {
case SFTP:
return SshClientUtils.execute(new SshClientTemplate<OutputStream>(path, false) {
@Override
public OutputStream execute(final SSHClient ssh) throws IOException {
final SFTPClient client = ssh.newSFTPClient();
final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path), EnumSet.of(net.schmizz.sshj.sftp.OpenMode.WRITE, net.schmizz.sshj.sftp.OpenMode.CREAT));
return rf.new RemoteFileOutputStream() {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
try {
rf.close();
client.close();
} catch (Exception e) {
Log.w(TAG, "Error closing stream", e);
}
}
}
};
}
});
case SMB:
try {
outputStream = getSmbFile().getOutputStream();
} catch (IOException e) {
outputStream = null;
e.printStackTrace();
}
break;
case DOCUMENT_FILE:
ContentResolver contentResolver = context.getContentResolver();
DocumentFile documentSourceFile = getDocumentFile(true);
try {
outputStream = contentResolver.openOutputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
outputStream = null;
}
break;
case OTG:
contentResolver = context.getContentResolver();
documentSourceFile = OTGUtil.getDocumentFile(path, context, true);
try {
outputStream = contentResolver.openOutputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
outputStream = null;
}
break;
default:
try {
outputStream = FileUtil.getOutputStream(getFile(), context);
} catch (Exception e) {
outputStream = null;
e.printStackTrace();
}
}
return outputStream;
}
use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class FileUtils method scanFile.
/**
* Triggers media store for the file path
*
* @param hybridFile the file which was changed (directory not supported)
* @param context given context
*/
private static void scanFile(@NonNull HybridFile hybridFile, Context context) {
if ((hybridFile.isLocal() || hybridFile.isOtgFile()) && hybridFile.exists(context)) {
Uri uri = null;
if (Build.VERSION.SDK_INT >= 19) {
DocumentFile documentFile = ExternalSdCardOperation.getDocumentFile(hybridFile.getFile(), hybridFile.isDirectory(context), context);
// If FileUtil.getDocumentFile() returns null, fall back to DocumentFile.fromFile()
if (documentFile == null)
documentFile = DocumentFile.fromFile(hybridFile.getFile());
uri = documentFile.getUri();
} else {
if (hybridFile.isLocal()) {
uri = Uri.fromFile(hybridFile.getFile());
}
}
if (uri != null) {
FileUtils.scanFile(uri, context);
}
}
}
use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class Operations method mkdir.
public static void mkdir(final HybridFile parentFile, @NonNull final HybridFile file, final Context context, final boolean rootMode, @NonNull final ErrorCallBack errorCallBack) {
new AsyncTask<Void, Void, Void>() {
private DataUtils dataUtils = DataUtils.getInstance();
private Function<DocumentFile, Void> safCreateDirectory = input -> {
if (input != null && input.isDirectory()) {
boolean result = false;
try {
result = input.createDirectory(file.getName(context)) != null;
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Failed to make directory", e);
}
errorCallBack.done(file, result);
} else
errorCallBack.done(file, false);
return null;
};
@Override
protected Void doInBackground(Void... params) {
// checking whether filename is valid or a recursive call possible
if (!Operations.isFileNameValid(file.getName(context))) {
errorCallBack.invalidName(file);
return null;
}
if (file.exists()) {
errorCallBack.exists(file);
return null;
}
if (file.isSftp()) {
file.mkdir(context);
return null;
}
if (file.isSmb()) {
try {
file.getSmbFile(2000).mkdirs();
} catch (SmbException e) {
e.printStackTrace();
errorCallBack.done(file, false);
return null;
}
errorCallBack.done(file, file.exists());
return null;
} else if (file.isOtgFile()) {
if (checkOtgNewFileExists(file, context)) {
errorCallBack.exists(file);
return null;
}
safCreateDirectory.apply(OTGUtil.getDocumentFile(parentFile.getPath(), context, false));
return null;
} else if (file.isDocumentFile()) {
if (checkDocumentFileNewFileExists(file, context)) {
errorCallBack.exists(file);
return null;
}
safCreateDirectory.apply(OTGUtil.getDocumentFile(parentFile.getPath(), SafRootHolder.getUriRoot(), context, OpenMode.DOCUMENT_FILE, false));
return null;
} else if (file.isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
try {
cloudStorageDropbox.createFolder(CloudUtil.stripPath(OpenMode.DROPBOX, file.getPath()));
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
try {
cloudStorageBox.createFolder(CloudUtil.stripPath(OpenMode.BOX, file.getPath()));
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
try {
cloudStorageOneDrive.createFolder(CloudUtil.stripPath(OpenMode.ONEDRIVE, file.getPath()));
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isGoogleDriveFile()) {
CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
try {
cloudStorageGdrive.createFolder(CloudUtil.stripPath(OpenMode.GDRIVE, file.getPath()));
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else {
if (file.isLocal() || file.isRoot()) {
int mode = checkFolder(new File(file.getParent(context)), context);
if (mode == 2) {
errorCallBack.launchSAF(file);
return null;
}
if (mode == 1 || mode == 0)
MakeDirectoryOperation.mkdir(file.getFile(), context);
if (!file.exists() && rootMode) {
file.setMode(OpenMode.ROOT);
if (file.exists())
errorCallBack.exists(file);
try {
MakeDirectoryCommand.INSTANCE.makeDirectory(file.getParent(context), file.getName(context));
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
errorCallBack.done(file, file.exists());
return null;
}
errorCallBack.done(file, file.exists());
return null;
}
errorCallBack.done(file, file.exists());
}
return null;
}
}.executeOnExecutor(executor);
}
use of androidx.documentfile.provider.DocumentFile in project AmazeFileManager by TeamAmaze.
the class Operations method rename.
public static void rename(@NonNull final HybridFile oldFile, @NonNull final HybridFile newFile, final boolean rootMode, @NonNull final Context context, @NonNull final ErrorCallBack errorCallBack) {
new AsyncTask<Void, Void, Void>() {
private final DataUtils dataUtils = DataUtils.getInstance();
private Function<DocumentFile, Void> safRenameFile = input -> {
boolean result = false;
try {
result = input.renameTo(newFile.getName(context));
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Failed to rename", e);
}
errorCallBack.done(newFile, result);
return null;
};
@Override
protected Void doInBackground(Void... params) {
// If rename is on OTG, we are skipping
if (!Operations.isFileNameValid(newFile.getName(context))) {
errorCallBack.invalidName(newFile);
return null;
}
if (newFile.exists()) {
errorCallBack.exists(newFile);
return null;
}
if (oldFile.isSmb()) {
try {
SmbFile smbFile = oldFile.getSmbFile();
// FIXME: smbFile1 should be created from SmbUtil too so it can be mocked
SmbFile smbFile1 = new SmbFile(new URL(newFile.getPath()), smbFile.getContext());
if (newFile.exists()) {
errorCallBack.exists(newFile);
return null;
}
smbFile.renameTo(smbFile1);
if (!smbFile.exists() && smbFile1.exists())
errorCallBack.done(newFile, true);
} catch (SmbException | MalformedURLException e) {
String errmsg = context.getString(R.string.cannot_rename_file, HybridFile.parseAndFormatUriForDisplay(oldFile.getPath()), e.getMessage());
try {
ArrayList<HybridFileParcelable> failedOps = new ArrayList<>();
failedOps.add(new HybridFileParcelable(oldFile.getSmbFile()));
context.sendBroadcast(new Intent(TAG_INTENT_FILTER_GENERAL).putParcelableArrayListExtra(TAG_INTENT_FILTER_FAILED_OPS, failedOps));
} catch (SmbException exceptionThrownDuringBuildParcelable) {
Log.e(TAG, "Error creating HybridFileParcelable", exceptionThrownDuringBuildParcelable);
}
Log.e(TAG, errmsg, e);
}
return null;
} else if (oldFile.isSftp()) {
SshClientUtils.execute(new SFtpClientTemplate<Void>(oldFile.getPath()) {
@Override
public Void execute(@NonNull SFTPClient client) {
try {
client.rename(SshClientUtils.extractRemotePathFrom(oldFile.getPath()), SshClientUtils.extractRemotePathFrom(newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (IOException e) {
String errmsg = context.getString(R.string.cannot_rename_file, HybridFile.parseAndFormatUriForDisplay(oldFile.getPath()), e.getMessage());
Log.e(TAG, errmsg);
ArrayList<HybridFileParcelable> failedOps = new ArrayList<>();
// Nobody care the size or actual permission here. Put a simple "r" and zero
// here
failedOps.add(new HybridFileParcelable(oldFile.getPath(), "r", oldFile.lastModified(), 0, oldFile.isDirectory(context)));
context.sendBroadcast(new Intent(TAG_INTENT_FILTER_GENERAL).putParcelableArrayListExtra(TAG_INTENT_FILTER_FAILED_OPS, failedOps));
errorCallBack.done(newFile, false);
}
return null;
}
});
} else if (oldFile.isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
try {
cloudStorageDropbox.move(CloudUtil.stripPath(OpenMode.DROPBOX, oldFile.getPath()), CloudUtil.stripPath(OpenMode.DROPBOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
try {
cloudStorageBox.move(CloudUtil.stripPath(OpenMode.BOX, oldFile.getPath()), CloudUtil.stripPath(OpenMode.BOX, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
try {
cloudStorageOneDrive.move(CloudUtil.stripPath(OpenMode.ONEDRIVE, oldFile.getPath()), CloudUtil.stripPath(OpenMode.ONEDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isGoogleDriveFile()) {
CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
try {
cloudStorageGdrive.move(CloudUtil.stripPath(OpenMode.GDRIVE, oldFile.getPath()), CloudUtil.stripPath(OpenMode.GDRIVE, newFile.getPath()));
errorCallBack.done(newFile, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(newFile, false);
}
} else if (oldFile.isOtgFile()) {
if (checkOtgNewFileExists(newFile, context)) {
errorCallBack.exists(newFile);
return null;
}
safRenameFile.apply(OTGUtil.getDocumentFile(oldFile.getPath(), context, false));
return null;
} else if (oldFile.isDocumentFile()) {
if (checkDocumentFileNewFileExists(newFile, context)) {
errorCallBack.exists(newFile);
return null;
}
safRenameFile.apply(OTGUtil.getDocumentFile(oldFile.getPath(), SafRootHolder.getUriRoot(), context, OpenMode.DOCUMENT_FILE, false));
return null;
} else {
File file = new File(oldFile.getPath());
File file1 = new File(newFile.getPath());
switch(oldFile.getMode()) {
case FILE:
int mode = checkFolder(file.getParentFile(), context);
if (mode == 2) {
errorCallBack.launchSAF(oldFile, newFile);
} else if (mode == 1 || mode == 0) {
try {
RenameOperation.renameFolder(file, file1, context);
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
boolean a = !file.exists() && file1.exists();
if (!a && rootMode) {
try {
RenameFileCommand.INSTANCE.renameFile(file.getPath(), file1.getPath());
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
oldFile.setMode(OpenMode.ROOT);
newFile.setMode(OpenMode.ROOT);
a = !file.exists() && file1.exists();
}
errorCallBack.done(newFile, a);
return null;
}
break;
case ROOT:
try {
RenameFileCommand.INSTANCE.renameFile(file.getPath(), file1.getPath());
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
newFile.setMode(OpenMode.ROOT);
errorCallBack.done(newFile, true);
break;
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (newFile != null && oldFile != null) {
HybridFile[] hybridFiles = { newFile, oldFile };
FileUtils.scanFile(context, hybridFiles);
}
}
}.executeOnExecutor(executor);
}
Aggregations