Skip to content

Commit

Permalink
Implementation for DocumentFileAmazeFilesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
EmmanuelMess committed Jan 28, 2022
1 parent a53a4b7 commit 4620cdf
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 173 deletions.
153 changes: 36 additions & 117 deletions app/src/main/java/com/amaze/filemanager/filesystem/HybridFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.amaze.filemanager.file_operations.filesystem.filetypes.cloud.onedrive.OnedriveAccount;
import com.amaze.filemanager.file_operations.filesystem.root.NativeOperations;
import com.amaze.filemanager.filesystem.cloud.CloudUtil;
import com.amaze.filemanager.filesystem.files.DocumentFileAmazeFilesystem;
import com.amaze.filemanager.filesystem.files.FileUtils;
import com.amaze.filemanager.filesystem.root.DeleteFileCommand;
import com.amaze.filemanager.filesystem.root.ListFilesCommand;
Expand Down Expand Up @@ -96,9 +97,6 @@ public class HybridFile {

protected static final String TAG = HybridFile.class.getSimpleName();

public static final String DOCUMENT_FILE_PREFIX =
"content://com.android.externalstorage.documents";

protected String path;
protected OpenMode mode;
protected String name;
Expand Down Expand Up @@ -136,7 +134,7 @@ public void generateMode(Context context) {
mode = OpenMode.SFTP;
} else if (path.startsWith(OTGUtil.PREFIX_OTG)) {
mode = OpenMode.OTG;
} else if (path.startsWith(DOCUMENT_FILE_PREFIX)) {
} else if (path.startsWith(DocumentFileAmazeFilesystem.DOCUMENT_FILE_PREFIX)) {
mode = OpenMode.DOCUMENT_FILE;
} else if (isCustomPath()) {
mode = OpenMode.CUSTOM;
Expand Down Expand Up @@ -260,9 +258,12 @@ public long lastModified() {
case SFTP:
case SMB:
case FILE:
return new AmazeFile(path).lastModified();
case DOCUMENT_FILE:
return getDocumentFile(false).lastModified();
case BOX:
case DROPBOX:
case GDRIVE:
case ONEDRIVE:
return new AmazeFile(path).lastModified();
case ROOT:
HybridFileParcelable baseFile = generateBaseFileFromParent();
if (baseFile != null) return baseFile.getDate();
Expand All @@ -282,6 +283,7 @@ public long length(Context context) {
case ONEDRIVE:
case GDRIVE:
case OTG:
case DOCUMENT_FILE:
try {
return new AmazeFile(path).length(() -> context);
} catch (IOException e) {
Expand All @@ -291,9 +293,6 @@ public long length(Context context) {
HybridFileParcelable baseFile = generateBaseFileFromParent();
if (baseFile != null) return baseFile.getSize();
break;
case DOCUMENT_FILE:
s = getDocumentFile(false).length();
break;
default:
break;
}
Expand All @@ -310,6 +309,12 @@ public String getSimpleName() {
case SFTP:
case SMB:
case FILE:
case ONEDRIVE:
case GDRIVE:
case DROPBOX:
case BOX:
case OTG:
case DOCUMENT_FILE:
return new AmazeFile(path).getName();
default:
StringBuilder builder = new StringBuilder(path);
Expand All @@ -327,16 +332,11 @@ public String getName(Context context) {
case ONEDRIVE:
case GDRIVE:
case OTG:
case SFTP:
case DOCUMENT_FILE:
return new AmazeFile(path).getName();
case ROOT:
return getFile().getName();
case DOCUMENT_FILE:
if (!Utils.isNullOrEmpty(name)) {
return name;
}
return OTGUtil.getDocumentFile(
path, SafRootHolder.getUriRoot(), context, OpenMode.DOCUMENT_FILE, false)
.getName();
default:
if (path.isEmpty()) {
return "";
Expand Down Expand Up @@ -375,6 +375,7 @@ public String getParent(Context context) {
case GDRIVE:
case SFTP:
case OTG:
case DOCUMENT_FILE:
return new AmazeFile(path).getParent();
default:
if (path.length() == getName(context).length()) {
Expand All @@ -397,15 +398,15 @@ public boolean isDirectory() {
boolean isDirectory;
switch (mode) {
case SFTP:
return isDirectory(AppConfig.getInstance());
case SMB:
case FILE:
case DROPBOX:
case BOX:
case ONEDRIVE:
case GDRIVE:
case OTG:
return new AmazeFile(path).isDirectory(() -> null);
case DOCUMENT_FILE:
return new AmazeFile(path).isDirectory(AppConfig::getInstance);
case ROOT:
isDirectory = NativeOperations.isDirectory(path);
break;
Expand All @@ -420,46 +421,18 @@ public boolean isDirectory(Context context) {
boolean isDirectory;
switch (mode) {
case SFTP:
final Boolean returnValue =
SshClientUtils.<Boolean>execute(
new SFtpClientTemplate<Boolean>(path) {
@Override
public Boolean execute(SFTPClient client) {
try {
return client
.stat(SshClientUtils.extractRemotePathFrom(path))
.getType()
.equals(FileMode.Type.DIRECTORY);
} catch (IOException notFound) {
Log.e(
getClass().getSimpleName(),
"Fail to execute isDirectory for SFTP path :" + path,
notFound);
return false;
}
}
});

if (returnValue == null) {
Log.e(TAG, "Error obtaining if path is directory over SFTP");
}

//noinspection SimplifiableConditionalExpression
return returnValue == null ? false : returnValue;
case SMB:
case FILE:
case DROPBOX:
case BOX:
case ONEDRIVE:
case GDRIVE:
case OTG:
case DOCUMENT_FILE:
return new AmazeFile(path).isDirectory(() -> context);
case ROOT:
isDirectory = NativeOperations.isDirectory(path);
break;
case DOCUMENT_FILE:
isDirectory = getDocumentFile(false).isDirectory();
break;
default:
isDirectory = getFile().isDirectory();
break;
Expand Down Expand Up @@ -533,12 +506,9 @@ public long getUsableSpace() {
case ONEDRIVE:
case GDRIVE:
case SFTP:
case OTG:
return new AmazeFile(path).getUsableSpace();
case OTG:
case DOCUMENT_FILE:
size =
FileProperties.getDeviceStorageRemainingSpace(SafRootHolder.INSTANCE.getVolumeLabel());
break;
return new AmazeFile(path).getUsableSpace();
}
return size;
}
Expand All @@ -555,11 +525,9 @@ public long getTotal(Context context) {
case ONEDRIVE:
case GDRIVE:
case SFTP:
case OTG:
return new AmazeFile(path).getTotalSpace(() -> context);
case OTG:
case DOCUMENT_FILE:
size = getDocumentFile(false).length();
break;
return new AmazeFile(path).getTotalSpace(() -> context);
}
return size;
}
Expand Down Expand Up @@ -747,18 +715,9 @@ public InputStream getInputStream(Context context) {
case BOX:
case ONEDRIVE:
case GDRIVE:
case OTG:
return new AmazeFile(getPath()).getInputStream(() -> context);
case OTG:
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;
return new AmazeFile(getPath()).getInputStream(() -> context);
default:
try {
inputStream = new FileInputStream(path);
Expand All @@ -783,17 +742,8 @@ public OutputStream getOutputStream(Context context) {
case ONEDRIVE:
case GDRIVE:
case OTG:
return new AmazeFile(path).getOutputStream(() -> context);
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;
return new AmazeFile(path).getOutputStream(() -> context);
default:
try {
outputStream = FileUtil.getOutputStream(getFile(), context);
Expand All @@ -806,43 +756,22 @@ public OutputStream getOutputStream(Context context) {
}

public boolean exists() {
boolean exists = false;
if (isSftp()) {
// TODO use Amaze file
final Boolean executionReturn =
SshClientUtils.<Boolean>execute(
new SFtpClientTemplate<Boolean>(path) {
@Override
public Boolean execute(SFTPClient client) throws IOException {
try {
return client.stat(SshClientUtils.extractRemotePathFrom(path)) != null;
} catch (SFTPException notFound) {
return false;
}
}
});

if (executionReturn == null) {
Log.e(TAG, "Error obtaining existance of file over SFTP");
}

//noinspection SimplifiableConditionalExpression
exists = executionReturn == null ? false : executionReturn;
} else if (isSmb() || isLocal() || isDropBoxFile() || isBoxFile() || isGoogleDriveFile() || isOneDriveFile()) {
if (isSmb() || isLocal() || isDropBoxFile() || isBoxFile() || isGoogleDriveFile()
|| isOneDriveFile() || isDocumentFile() || isSftp()) {
return new AmazeFile(path).exists(() -> null);
} else if (isRoot()) {
return RootHelper.fileExists(path);
}

return exists;
return false;
}

/** Helper method to check file existence in otg */
public boolean exists(Context context) {
boolean exists = false;
try {
if (isSmb() || isLocal() || isDropBoxFile() || isBoxFile() || isGoogleDriveFile()
|| isOneDriveFile() || isOtgFile() || isSftp()) {
|| isOneDriveFile() || isOtgFile() || isSftp() || isDocumentFile()) {
return new AmazeFile(path).exists(() -> context);
} else if (isDocumentFile()) {
exists =
Expand Down Expand Up @@ -878,29 +807,19 @@ public boolean isSimpleFile() {
}

public boolean setLastModified(final long date) {
if (isSmb() || isLocal() || isOneDriveFile() || isBoxFile() || isGoogleDriveFile() || isDropBoxFile() || isSftp() || isOtgFile()) {
if (isSmb() || isLocal() || isOneDriveFile() || isBoxFile() || isGoogleDriveFile()
|| isDropBoxFile() || isSftp() || isOtgFile()) {
return new AmazeFile(path).setLastModified(date);
}
File f = getFile();
return f.setLastModified(date);
}

public void mkdir(Context context) {
if (isSftp() || isSmb() || isLocal() || isRoot() || isCustomPath() || isUnknownFile() || isOneDriveFile() || isBoxFile() || isGoogleDriveFile() || isDropBoxFile() || isOtgFile()) {
if (isSftp() || isSmb() || isLocal() || isRoot() || isCustomPath() || isUnknownFile()
|| isOneDriveFile() || isBoxFile() || isGoogleDriveFile() || isDropBoxFile()
|| isOtgFile() || isDocumentFile()) {
new AmazeFile(path).mkdirs(() -> context);
} else if (isDocumentFile()) {
if (!exists(context)) {
DocumentFile parentDirectory =
OTGUtil.getDocumentFile(
getParent(context),
SafRootHolder.getUriRoot(),
context,
OpenMode.DOCUMENT_FILE,
true);
if (parentDirectory.isDirectory()) {
parentDirectory.createDirectory(getName(context));
}
}
} else {
throw new IllegalStateException();
}
Expand Down
Loading

0 comments on commit 4620cdf

Please sign in to comment.