use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException 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 com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException 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);
}
use of com.amaze.filemanager.file_operations.exceptions.ShellNotRunningException in project AmazeFileManager by TeamAmaze.
the class GeneralDialogCreation method setPermissionsDialog.
public static void setPermissionsDialog(final View v, View but, final HybridFile file, final String f, final Context context, final MainFragment mainFrag) {
final CheckBox readown = v.findViewById(R.id.creadown);
final CheckBox readgroup = v.findViewById(R.id.creadgroup);
final CheckBox readother = v.findViewById(R.id.creadother);
final CheckBox writeown = v.findViewById(R.id.cwriteown);
final CheckBox writegroup = v.findViewById(R.id.cwritegroup);
final CheckBox writeother = v.findViewById(R.id.cwriteother);
final CheckBox exeown = v.findViewById(R.id.cexeown);
final CheckBox exegroup = v.findViewById(R.id.cexegroup);
final CheckBox exeother = v.findViewById(R.id.cexeother);
String perm = f;
if (perm.length() < 6) {
v.setVisibility(View.GONE);
but.setVisibility(View.GONE);
Toast.makeText(context, R.string.not_allowed, Toast.LENGTH_SHORT).show();
return;
}
ArrayList<Boolean[]> arrayList = FileUtils.parse(perm);
Boolean[] read = arrayList.get(0);
Boolean[] write = arrayList.get(1);
final Boolean[] exe = arrayList.get(2);
readown.setChecked(read[0]);
readgroup.setChecked(read[1]);
readother.setChecked(read[2]);
writeown.setChecked(write[0]);
writegroup.setChecked(write[1]);
writeother.setChecked(write[2]);
exeown.setChecked(exe[0]);
exegroup.setChecked(exe[1]);
exeother.setChecked(exe[2]);
but.setOnClickListener(v1 -> {
int perms = RootHelper.permissionsToOctalString(readown.isChecked(), writeown.isChecked(), exeown.isChecked(), readgroup.isChecked(), writegroup.isChecked(), exegroup.isChecked(), readother.isChecked(), writeother.isChecked(), exeother.isChecked());
try {
ChangeFilePermissionsCommand.INSTANCE.changeFilePermissions(file.getPath(), perms, file.isDirectory(context), isSuccess -> {
if (isSuccess) {
Toast.makeText(context, mainFrag.getString(R.string.done), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, mainFrag.getString(R.string.operation_unsuccesful), Toast.LENGTH_LONG).show();
}
return null;
});
} catch (ShellNotRunningException e) {
Toast.makeText(context, mainFrag.getString(R.string.root_failure), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
});
}
Aggregations