use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.
the class Operations method mkfile.
public static void mkfile(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> safCreateFile = input -> {
if (input != null && input.isDirectory()) {
boolean result = false;
try {
result = input.createFile(file.getName(context).substring(file.getName(context).lastIndexOf(".")), file.getName(context)) != null;
} catch (Exception e) {
Log.w(getClass().getSimpleName(), "Failed to make file", e);
}
errorCallBack.done(file, result);
} else
errorCallBack.done(file, false);
return null;
};
@Override
protected Void doInBackground(Void... params) {
// check whether filename is valid or not
if (!Operations.isFileNameValid(file.getName(context))) {
errorCallBack.invalidName(file);
return null;
}
if (file.exists()) {
errorCallBack.exists(file);
return null;
}
if (file.isSftp()) {
OutputStream out = file.getOutputStream(context);
if (out == null) {
errorCallBack.done(file, false);
return null;
}
try {
out.close();
errorCallBack.done(file, true);
return null;
} catch (IOException e) {
errorCallBack.done(file, false);
return null;
}
}
if (file.isSmb()) {
try {
file.getSmbFile(2000).createNewFile();
} catch (SmbException e) {
e.printStackTrace();
errorCallBack.done(file, false);
return null;
}
errorCallBack.done(file, file.exists());
return null;
} else if (file.isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
try {
byte[] tempBytes = new byte[0];
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
cloudStorageDropbox.upload(CloudUtil.stripPath(OpenMode.DROPBOX, file.getPath()), byteArrayInputStream, 0l, true);
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
try {
byte[] tempBytes = new byte[0];
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
cloudStorageBox.upload(CloudUtil.stripPath(OpenMode.BOX, file.getPath()), byteArrayInputStream, 0l, true);
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
try {
byte[] tempBytes = new byte[0];
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
cloudStorageOneDrive.upload(CloudUtil.stripPath(OpenMode.ONEDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isGoogleDriveFile()) {
CloudStorage cloudStorageGdrive = dataUtils.getAccount(OpenMode.GDRIVE);
try {
byte[] tempBytes = new byte[0];
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(tempBytes);
cloudStorageGdrive.upload(CloudUtil.stripPath(OpenMode.GDRIVE, file.getPath()), byteArrayInputStream, 0l, true);
errorCallBack.done(file, true);
} catch (Exception e) {
e.printStackTrace();
errorCallBack.done(file, false);
}
} else if (file.isOtgFile()) {
if (checkOtgNewFileExists(file, context)) {
errorCallBack.exists(file);
return null;
}
safCreateFile.apply(OTGUtil.getDocumentFile(parentFile.getPath(), context, false));
return null;
} else if (file.isDocumentFile()) {
if (checkDocumentFileNewFileExists(file, context)) {
errorCallBack.exists(file);
return null;
}
safCreateFile.apply(OTGUtil.getDocumentFile(parentFile.getPath(), SafRootHolder.getUriRoot(), context, OpenMode.DOCUMENT_FILE, false));
return null;
} 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)
MakeFileOperation.mkfile(file.getFile(), context);
if (!file.exists() && rootMode) {
file.setMode(OpenMode.ROOT);
if (file.exists())
errorCallBack.exists(file);
try {
MakeFileCommand.INSTANCE.makeFile(file.getPath());
} 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 com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.
the class MoveFiles method doInBackground.
@Override
protected Boolean doInBackground(ArrayList<String>... strings) {
paths = strings[0];
if (files.size() == 0)
return true;
for (ArrayList<HybridFileParcelable> filesCurrent : files) {
totalBytes += FileUtils.getTotalBytes(filesCurrent, context);
}
HybridFile destination = new HybridFile(mode, paths.get(0));
destinationSize = destination.getUsableSpace();
for (int i = 0; i < paths.size(); i++) {
for (HybridFileParcelable baseFile : files.get(i)) {
String destPath = paths.get(i) + "/" + baseFile.getName(context);
if (baseFile.getPath().indexOf('?') > 0)
destPath += baseFile.getPath().substring(baseFile.getPath().indexOf('?'));
if (!isMoveOperationValid(baseFile, new HybridFile(mode, paths.get(i)))) {
// TODO: 30/06/20 Replace runtime exception with generic exception
Log.w(getClass().getSimpleName(), "Some files failed to be moved", new RuntimeException());
invalidOperation = true;
continue;
}
switch(mode) {
case FILE:
File dest = new File(destPath);
File source = new File(baseFile.getPath());
if (!source.renameTo(dest)) {
// check if we have root
if (isRootExplorer) {
try {
if (!RenameFileCommand.INSTANCE.renameFile(baseFile.getPath(), destPath))
return false;
} catch (ShellNotRunningException e) {
e.printStackTrace();
return false;
}
} else
return false;
}
break;
case DROPBOX:
case BOX:
case ONEDRIVE:
case GDRIVE:
DataUtils dataUtils = DataUtils.getInstance();
CloudStorage cloudStorage = dataUtils.getAccount(mode);
if (baseFile.getMode() == mode) {
// source and target both in same filesystem, use API method
try {
cloudStorage.move(CloudUtil.stripPath(mode, baseFile.getPath()), CloudUtil.stripPath(mode, destPath));
} catch (Exception e) {
e.printStackTrace();
return false;
}
} else {
// not in same filesystem, execute service
return false;
}
default:
return false;
}
}
}
return true;
}
use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.
the class ReadFileTask method loadFile.
private InputStream loadFile(File file) {
InputStream inputStream = null;
if (!file.canWrite() && isRootExplorer) {
// try loading stream associated using root
try {
cachedFile = new File(externalCacheDir, file.getName());
// creating a cache file
CopyFilesCommand.INSTANCE.copyFiles(file.getAbsolutePath(), cachedFile.getPath());
inputStream = new FileInputStream(cachedFile);
} catch (ShellNotRunningException e) {
e.printStackTrace();
inputStream = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
inputStream = null;
}
} else if (file.canRead()) {
// readable file in filesystem
try {
inputStream = new FileInputStream(file.getAbsolutePath());
} catch (FileNotFoundException e) {
Log.e(TAG, "Unable to open file [" + file.getAbsolutePath() + "] for reading", e);
inputStream = null;
}
}
return inputStream;
}
use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException 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 com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.
the class DatabaseViewerActivity method load.
private void load(final File file) {
new Thread(() -> {
File file1 = getExternalCacheDir();
// first copying it in cache dir
if (!file.canRead() && isRootExplorer()) {
try {
CopyFilesCommand.INSTANCE.copyFiles(pathFile.getPath(), new File(file1.getPath(), file.getName()).getPath());
pathFile = new File(file1.getPath(), file.getName());
} catch (ShellNotRunningException e) {
e.printStackTrace();
}
delete = true;
}
try {
sqLiteDatabase = SQLiteDatabase.openDatabase(pathFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
c = sqLiteDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
arrayList = getDbTableNames(c);
arrayAdapter = new ArrayAdapter(DatabaseViewerActivity.this, android.R.layout.simple_list_item_1, arrayList);
} catch (Exception e) {
e.printStackTrace();
finish();
}
runOnUiThread(() -> {
listView.setAdapter(arrayAdapter);
});
}).start();
}
Aggregations