use of com.amaze.filemanager.utils.DataUtils in project AmazeFileManager by TeamAmaze.
the class Operations method mkfile.
public static void mkfile(@NonNull final HybridFile file, final Context context, final boolean rootMode, @NonNull final ErrorCallBack errorCallBack) {
new AsyncTask<Void, Void, Void>() {
private DataUtils dataUtils = DataUtils.getInstance();
@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);
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()) {
// first check whether new file already exists
DocumentFile fileToCreate = OTGUtil.getDocumentFile(file.getPath(), context, false);
if (fileToCreate != null)
errorCallBack.exists(file);
DocumentFile parentDirectory = OTGUtil.getDocumentFile(file.getParent(), context, false);
if (parentDirectory.isDirectory()) {
parentDirectory.createFile(file.getName(context).substring(file.getName().lastIndexOf(".")), file.getName(context));
errorCallBack.done(file, true);
} else
errorCallBack.done(file, false);
return null;
} else {
if (file.isLocal() || file.isRoot()) {
int mode = checkFolder(new File(file.getParent()), context);
if (mode == 2) {
errorCallBack.launchSAF(file);
return null;
}
if (mode == 1 || mode == 0)
try {
FileUtil.mkfile(file.getFile(), context);
} catch (IOException e) {
}
if (!file.exists() && rootMode) {
file.setMode(OpenMode.ROOT);
if (file.exists())
errorCallBack.exists(file);
try {
RootUtils.mkFile(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(AsyncTask.THREAD_POOL_EXECUTOR);
}
use of com.amaze.filemanager.utils.DataUtils in project AmazeFileManager by TeamAmaze.
the class CloudUtil method checkToken.
/**
* Asynctask checks if the item pressed on is a cloud account, and if the token that
* is saved for it is invalid or not, in which case, we'll clear off the
* saved token and authenticate the user again
* @param path the path of item in drawer
* @param mainActivity reference to main activity to fire callbacks to delete/add connection
*/
public static void checkToken(String path, final MainActivity mainActivity) {
new AsyncTask<String, Void, Boolean>() {
OpenMode serviceType;
private DataUtils dataUtils = DataUtils.getInstance();
@Override
protected Boolean doInBackground(String... params) {
boolean isTokenValid = true;
String path = params[0];
if (path.startsWith(CloudHandler.CLOUD_PREFIX_DROPBOX)) {
// dropbox account
serviceType = OpenMode.DROPBOX;
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
try {
cloudStorageDropbox.getUserLogin();
} catch (Exception e) {
e.printStackTrace();
isTokenValid = false;
}
} else if (path.startsWith(CloudHandler.CLOUD_PREFIX_ONE_DRIVE)) {
serviceType = OpenMode.ONEDRIVE;
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
try {
cloudStorageOneDrive.getUserLogin();
} catch (Exception e) {
e.printStackTrace();
isTokenValid = false;
}
} else if (path.startsWith(CloudHandler.CLOUD_PREFIX_BOX)) {
serviceType = OpenMode.BOX;
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
try {
cloudStorageBox.getUserLogin();
} catch (Exception e) {
e.printStackTrace();
isTokenValid = false;
}
} else if (path.startsWith(CloudHandler.CLOUD_PREFIX_GOOGLE_DRIVE)) {
serviceType = OpenMode.GDRIVE;
CloudStorage cloudStorageGDrive = dataUtils.getAccount(OpenMode.GDRIVE);
try {
cloudStorageGDrive.getUserLogin();
} catch (Exception e) {
e.printStackTrace();
isTokenValid = false;
}
}
return isTokenValid;
}
@Override
protected void onPostExecute(Boolean aBoolean) {
super.onPostExecute(aBoolean);
if (!aBoolean) {
// delete account and create a new one
Toast.makeText(mainActivity, mainActivity.getResources().getString(R.string.cloud_token_lost), Toast.LENGTH_LONG).show();
mainActivity.deleteConnection(serviceType);
mainActivity.addConnection(serviceType);
}
}
}.execute(path);
}
use of com.amaze.filemanager.utils.DataUtils 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;
switch(mode) {
case SMB:
for (int i = 0; i < paths.size(); i++) {
for (HybridFileParcelable f : files.get(i)) {
try {
SmbFile source = new SmbFile(f.getPath());
SmbFile dest = new SmbFile(paths.get(i) + "/" + f.getName());
source.renameTo(dest);
} catch (MalformedURLException e) {
e.printStackTrace();
return false;
} catch (SmbException e) {
e.printStackTrace();
return false;
}
}
}
break;
case FILE:
for (int i = 0; i < paths.size(); i++) {
for (HybridFileParcelable f : files.get(i)) {
File dest = new File(paths.get(i) + "/" + f.getName());
File source = new File(f.getPath());
if (!source.renameTo(dest)) {
// check if we have root
if (mainFrag.getMainActivity().isRootExplorer()) {
try {
if (!RootUtils.rename(f.getPath(), paths.get(i) + "/" + f.getName()))
return false;
} catch (ShellNotRunningException e) {
e.printStackTrace();
return false;
}
} else
return false;
}
}
}
break;
case DROPBOX:
case BOX:
case ONEDRIVE:
case GDRIVE:
for (int i = 0; i < paths.size(); i++) {
for (HybridFileParcelable baseFile : files.get(i)) {
DataUtils dataUtils = DataUtils.getInstance();
CloudStorage cloudStorage = dataUtils.getAccount(mode);
String targetPath = paths.get(i) + "/" + baseFile.getName();
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, targetPath));
} catch (Exception e) {
return false;
}
} else {
// not in same filesystem, execute service
return false;
}
}
}
default:
return false;
}
return true;
}
use of com.amaze.filemanager.utils.DataUtils in project AmazeFileManager by TeamAmaze.
the class ItemPopupMenu method onMenuItemClick.
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId()) {
case R.id.about:
GeneralDialogCreation.showPropertiesDialogWithPermissions((rowItem).generateBaseFile(), rowItem.permissions, (ThemedActivity) mainFragment.getActivity(), mainActivity.isRootExplorer(), utilitiesProvider.getAppTheme());
/*
PropertiesSheet propertiesSheet = new PropertiesSheet();
Bundle arguments = new Bundle();
arguments.putParcelable(PropertiesSheet.KEY_FILE, rowItem.generateBaseFile());
arguments.putString(PropertiesSheet.KEY_PERMISSION, rowItem.getPermissions());
arguments.putBoolean(PropertiesSheet.KEY_ROOT, ThemedActivity.rootMode);
propertiesSheet.setArguments(arguments);
propertiesSheet.show(main.getFragmentManager(), PropertiesSheet.TAG_FRAGMENT);
*/
return true;
case R.id.share:
switch(rowItem.getMode()) {
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
FileUtils.shareCloudFile(rowItem.desc, rowItem.getMode(), context);
break;
default:
ArrayList<File> arrayList = new ArrayList<>();
arrayList.add(new File(rowItem.desc));
FileUtils.shareFiles(arrayList, mainFragment.getMainActivity(), utilitiesProvider.getAppTheme(), accentColor);
break;
}
return true;
case R.id.rename:
mainFragment.rename(rowItem.generateBaseFile());
return true;
case R.id.cpy:
case R.id.cut:
{
int op = item.getItemId() == R.id.cpy ? PasteHelper.OPERATION_COPY : PasteHelper.OPERATION_CUT;
PasteHelper pasteHelper = new PasteHelper(op, new HybridFileParcelable[] { rowItem.generateBaseFile() });
mainFragment.getMainActivity().setPaste(pasteHelper);
return true;
}
case R.id.ex:
mainFragment.getMainActivity().mainActivityHelper.extractFile(new File(rowItem.desc));
return true;
case R.id.book:
DataUtils dataUtils = DataUtils.getInstance();
dataUtils.addBook(new String[] { rowItem.title, rowItem.desc }, true);
mainFragment.getMainActivity().getDrawer().refreshDrawer();
Toast.makeText(mainFragment.getActivity(), mainFragment.getResources().getString(R.string.bookmarksadded), Toast.LENGTH_LONG).show();
return true;
case R.id.delete:
ArrayList<LayoutElementParcelable> positions = new ArrayList<>();
positions.add(rowItem);
GeneralDialogCreation.deleteFilesDialog(context, mainFragment.getElementsList(), mainFragment.getMainActivity(), positions, utilitiesProvider.getAppTheme());
return true;
case R.id.open_with:
boolean useNewStack = sharedPrefs.getBoolean(PreferencesConstants.PREFERENCE_TEXTEDITOR_NEWSTACK, false);
FileUtils.openWith(new File(rowItem.desc), mainFragment.getActivity(), useNewStack);
return true;
case R.id.encrypt:
final Intent encryptIntent = new Intent(context, EncryptService.class);
encryptIntent.putExtra(EncryptService.TAG_OPEN_MODE, rowItem.getMode().ordinal());
encryptIntent.putExtra(EncryptService.TAG_SOURCE, rowItem.generateBaseFile());
final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
final EncryptDecryptUtils.EncryptButtonCallbackInterface encryptButtonCallbackInterfaceAuthenticate = new EncryptDecryptUtils.EncryptButtonCallbackInterface() {
@Override
public void onButtonPressed(Intent intent) {
}
@Override
public void onButtonPressed(Intent intent, String password) throws Exception {
EncryptDecryptUtils.startEncryption(context, rowItem.generateBaseFile().getPath(), password, intent);
}
};
EncryptDecryptUtils.EncryptButtonCallbackInterface encryptButtonCallbackInterface = new EncryptDecryptUtils.EncryptButtonCallbackInterface() {
@Override
public void onButtonPressed(Intent intent) throws Exception {
// check if a master password or fingerprint is set
if (!preferences.getString(PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD, PreferencesConstants.PREFERENCE_CRYPT_MASTER_PASSWORD_DEFAULT).equals("")) {
EncryptDecryptUtils.startEncryption(context, rowItem.generateBaseFile().getPath(), PreferencesConstants.ENCRYPT_PASSWORD_MASTER, encryptIntent);
} else if (preferences.getBoolean(PreferencesConstants.PREFERENCE_CRYPT_FINGERPRINT, PreferencesConstants.PREFERENCE_CRYPT_FINGERPRINT_DEFAULT)) {
EncryptDecryptUtils.startEncryption(context, rowItem.generateBaseFile().getPath(), PreferencesConstants.ENCRYPT_PASSWORD_FINGERPRINT, encryptIntent);
} else {
// let's ask a password from user
GeneralDialogCreation.showEncryptAuthenticateDialog(context, encryptIntent, mainFragment.getMainActivity(), utilitiesProvider.getAppTheme(), encryptButtonCallbackInterfaceAuthenticate);
}
}
@Override
public void onButtonPressed(Intent intent, String password) {
}
};
if (preferences.getBoolean(PreferencesConstants.PREFERENCE_CRYPT_WARNING_REMEMBER, PreferencesConstants.PREFERENCE_CRYPT_WARNING_REMEMBER_DEFAULT)) {
// let's skip warning dialog call
try {
encryptButtonCallbackInterface.onButtonPressed(encryptIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(context, mainFragment.getResources().getString(R.string.crypt_encryption_fail), Toast.LENGTH_LONG).show();
}
} else {
GeneralDialogCreation.showEncryptWarningDialog(encryptIntent, mainFragment, utilitiesProvider.getAppTheme(), encryptButtonCallbackInterface);
}
return true;
case R.id.decrypt:
EncryptDecryptUtils.decryptFile(context, mainActivity, mainFragment, mainFragment.openMode, rowItem.generateBaseFile(), rowItem.generateBaseFile().getParent(context), utilitiesProvider, false);
return true;
case R.id.return_select:
mainFragment.returnIntentResults(rowItem.generateBaseFile());
return true;
}
return false;
}
use of com.amaze.filemanager.utils.DataUtils in project AmazeFileManager by TeamAmaze.
the class Operations method mkdir.
public static void mkdir(@NonNull final HybridFile file, final Context context, final boolean rootMode, @NonNull final ErrorCallBack errorCallBack) {
new AsyncTask<Void, Void, Void>() {
private DataUtils dataUtils = DataUtils.getInstance();
@Override
protected Void doInBackground(Void... params) {
// checking whether filename is valid or a recursive call possible
if (MainActivityHelper.isNewDirectoryRecursive(file) || !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()) {
// first check whether new directory already exists
DocumentFile directoryToCreate = OTGUtil.getDocumentFile(file.getPath(), context, false);
if (directoryToCreate != null)
errorCallBack.exists(file);
DocumentFile parentDirectory = OTGUtil.getDocumentFile(file.getParent(), context, false);
if (parentDirectory.isDirectory()) {
parentDirectory.createDirectory(file.getName(context));
errorCallBack.done(file, true);
} else
errorCallBack.done(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);
if (mode == 2) {
errorCallBack.launchSAF(file);
return null;
}
if (mode == 1 || mode == 0)
FileUtil.mkdir(file.getFile(), context);
if (!file.exists() && rootMode) {
file.setMode(OpenMode.ROOT);
if (file.exists())
errorCallBack.exists(file);
try {
RootUtils.mkDir(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(AsyncTask.THREAD_POOL_EXECUTOR);
}
Aggregations