use of com.amaze.filemanager.file_operations.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.
the class LoadFilesListTask method doInBackground.
@Override
@Nullable
protected Pair<OpenMode, ArrayList<LayoutElementParcelable>> doInBackground(Void... p) {
final MainFragment mainFragment = this.mainFragmentReference.get();
final Context context = this.context.get();
if (mainFragment == null || context == null || mainFragment.getMainFragmentViewModel() == null) {
cancel(true);
return null;
}
HybridFile hFile = null;
if (OpenMode.UNKNOWN.equals(openmode) || OpenMode.CUSTOM.equals(openmode)) {
hFile = new HybridFile(openmode, path);
hFile.generateMode(mainFragment.getActivity());
openmode = hFile.getMode();
if (hFile.isSmb()) {
mainFragment.getMainFragmentViewModel().setSmbPath(path);
}
}
if (isCancelled())
return null;
mainFragment.getMainFragmentViewModel().setFolderCount(0);
mainFragment.getMainFragmentViewModel().setFileCount(0);
final ArrayList<LayoutElementParcelable> list;
switch(openmode) {
case SMB:
if (hFile == null) {
hFile = new HybridFile(OpenMode.SMB, path);
}
if (!hFile.getPath().endsWith("/")) {
hFile.setPath(hFile.getPath() + "/");
}
try {
SmbFile[] smbFile = hFile.getSmbFile(5000).listFiles();
list = mainFragment.addToSmb(smbFile, path, showHiddenFiles);
openmode = OpenMode.SMB;
} catch (SmbAuthException e) {
if (!e.getMessage().toLowerCase().contains("denied")) {
mainFragment.reauthenticateSmb();
}
e.printStackTrace();
return null;
} catch (SmbException | NullPointerException e) {
Log.w(getClass().getSimpleName(), "Failed to load smb files for path: " + path, e);
mainFragment.reauthenticateSmb();
return null;
}
break;
case SFTP:
HybridFile sftpHFile = new HybridFile(OpenMode.SFTP, path);
list = new ArrayList();
sftpHFile.forEachChildrenFile(context, false, file -> {
if (!(dataUtils.isFileHidden(file.getPath()) || file.isHidden() && !showHiddenFiles)) {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null) {
list.add(elem);
}
}
});
break;
case CUSTOM:
switch(Integer.parseInt(path)) {
case 0:
list = listImages();
break;
case 1:
list = listVideos();
break;
case 2:
list = listaudio();
break;
case 3:
list = listDocs();
break;
case 4:
list = listApks();
break;
case 5:
list = listRecent();
break;
case 6:
list = listRecentFiles();
break;
default:
throw new IllegalStateException();
}
break;
case OTG:
list = new ArrayList<>();
listOtg(path, file -> {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
});
openmode = OpenMode.OTG;
break;
case DOCUMENT_FILE:
list = new ArrayList<>();
listDocumentFiles(file -> {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
});
openmode = OpenMode.DOCUMENT_FILE;
break;
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
CloudStorage cloudStorage = dataUtils.getAccount(openmode);
list = new ArrayList<>();
try {
listCloud(path, cloudStorage, openmode, file -> {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
});
} catch (CloudPluginException e) {
e.printStackTrace();
AppConfig.toast(context, context.getResources().getString(R.string.failed_no_connection));
return new Pair<>(openmode, list);
}
break;
default:
// we're neither in OTG not in SMB, load the list based on root/general filesystem
list = new ArrayList<>();
final OpenMode[] currentOpenMode = new OpenMode[1];
ListFilesCommand.INSTANCE.listFiles(path, mainFragment.getMainActivity().isRootExplorer(), showHiddenFiles, mode -> {
currentOpenMode[0] = mode;
return null;
}, hybridFileParcelable -> {
LayoutElementParcelable elem = createListParcelables(hybridFileParcelable);
if (elem != null)
list.add(elem);
return null;
});
if (null != currentOpenMode[0]) {
openmode = currentOpenMode[0];
}
break;
}
if (list != null && !(openmode == OpenMode.CUSTOM && ((path).equals("5") || (path).equals("6")))) {
int t = SortHandler.getSortType(context, path);
int sortby;
int asc;
if (t <= 3) {
sortby = t;
asc = 1;
} else {
asc = -1;
sortby = t - 4;
}
MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel();
if (viewModel != null) {
Collections.sort(list, new FileListSorter(viewModel.getDsort(), sortby, asc));
} else {
Log.e(TAG, "MainFragmentViewModel is null, this is a bug");
}
}
return new Pair<>(openmode, list);
}
use of com.amaze.filemanager.file_operations.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.
the class LoadFilesListTask method listCloud.
private void listCloud(String path, CloudStorage cloudStorage, OpenMode openMode, OnFileFound fileFoundCallback) throws CloudPluginException {
final Context context = this.context.get();
if (context == null) {
cancel(true);
return;
}
if (!CloudSheetFragment.isCloudProviderAvailable(context)) {
throw new CloudPluginException();
}
CloudUtil.getCloudFiles(path, cloudStorage, openMode, fileFoundCallback);
}
use of com.amaze.filemanager.file_operations.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.
the class CloudLoaderAsyncTask method doInBackground.
@Override
@NonNull
public Boolean doInBackground(Void... voids) {
boolean hasUpdatedDrawer = false;
if (data == null)
return false;
if (data.getCount() > 0 && data.moveToFirst()) {
do {
if (mainActivity.get() == null || isCancelled()) {
cancel(true);
return false;
}
switch(data.getInt(0)) {
case 1:
try {
CloudRail.setAppKey(data.getString(1));
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.failed_cloud_api_key);
} else {
cancel(true);
}
return false;
}
break;
case 2:
// DRIVE
try {
CloudEntry cloudEntryGdrive = null;
CloudEntry savedCloudEntryGdrive;
GoogleDrive cloudStorageDrive;
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
cloudStorageDrive = new GoogleDrive(mainActivity.getApplicationContext(), data.getString(1), "", MainActivity.CLOUD_AUTHENTICATOR_REDIRECT_URI, data.getString(2));
} else {
cancel(true);
return false;
}
cloudStorageDrive.useAdvancedAuthentication();
if ((savedCloudEntryGdrive = cloudHandler.findEntry(OpenMode.GDRIVE)) != null) {
try {
cloudStorageDrive.loadAsString(savedCloudEntryGdrive.getPersistData().value);
} catch (ParseException e) {
e.printStackTrace();
// we need to update the persist string as existing one is been compromised
cloudStorageDrive.login();
cloudEntryGdrive = new CloudEntry(OpenMode.GDRIVE, cloudStorageDrive.saveAsString());
cloudHandler.updateEntry(OpenMode.GDRIVE, cloudEntryGdrive);
}
} else {
cloudStorageDrive.login();
cloudEntryGdrive = new CloudEntry(OpenMode.GDRIVE, cloudStorageDrive.saveAsString());
cloudHandler.addEntry(cloudEntryGdrive);
}
dataUtils.addAccount(cloudStorageDrive);
hasUpdatedDrawer = true;
} catch (CloudPluginException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_error_plugin);
mainActivity.deleteConnection(OpenMode.GDRIVE);
} else {
cancel(true);
}
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_fail_authenticate);
mainActivity.deleteConnection(OpenMode.GDRIVE);
} else {
cancel(true);
}
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.failed_cloud_new_connection);
mainActivity.deleteConnection(OpenMode.GDRIVE);
} else {
cancel(true);
}
return false;
}
break;
case 3:
// DROPBOX
try {
CloudEntry cloudEntryDropbox = null;
CloudEntry savedCloudEntryDropbox;
CloudStorage cloudStorageDropbox;
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
cloudStorageDropbox = new Dropbox(mainActivity.getApplicationContext(), data.getString(1), data.getString(2));
} else {
cancel(true);
return false;
}
if ((savedCloudEntryDropbox = cloudHandler.findEntry(OpenMode.DROPBOX)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageDropbox.loadAsString(savedCloudEntryDropbox.getPersistData().value);
} catch (ParseException e) {
e.printStackTrace();
// we need to persist data again
cloudStorageDropbox.login();
cloudEntryDropbox = new CloudEntry(OpenMode.DROPBOX, cloudStorageDropbox.saveAsString());
cloudHandler.updateEntry(OpenMode.DROPBOX, cloudEntryDropbox);
}
} else {
cloudStorageDropbox.login();
cloudEntryDropbox = new CloudEntry(OpenMode.DROPBOX, cloudStorageDropbox.saveAsString());
cloudHandler.addEntry(cloudEntryDropbox);
}
dataUtils.addAccount(cloudStorageDropbox);
hasUpdatedDrawer = true;
} catch (CloudPluginException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_error_plugin);
mainActivity.deleteConnection(OpenMode.DROPBOX);
} else {
cancel(true);
}
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_fail_authenticate);
mainActivity.deleteConnection(OpenMode.DROPBOX);
} else
cancel(true);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.failed_cloud_new_connection);
mainActivity.deleteConnection(OpenMode.DROPBOX);
} else
cancel(true);
return false;
}
break;
case 4:
// BOX
try {
CloudEntry cloudEntryBox = null;
CloudEntry savedCloudEntryBox;
CloudStorage cloudStorageBox;
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
cloudStorageBox = new Box(mainActivity.getApplicationContext(), data.getString(1), data.getString(2));
} else {
cancel(true);
return false;
}
if ((savedCloudEntryBox = cloudHandler.findEntry(OpenMode.BOX)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageBox.loadAsString(savedCloudEntryBox.getPersistData().value);
} catch (ParseException e) {
e.printStackTrace();
// we need to persist data again
cloudStorageBox.login();
cloudEntryBox = new CloudEntry(OpenMode.BOX, cloudStorageBox.saveAsString());
cloudHandler.updateEntry(OpenMode.BOX, cloudEntryBox);
}
} else {
cloudStorageBox.login();
cloudEntryBox = new CloudEntry(OpenMode.BOX, cloudStorageBox.saveAsString());
cloudHandler.addEntry(cloudEntryBox);
}
dataUtils.addAccount(cloudStorageBox);
hasUpdatedDrawer = true;
} catch (CloudPluginException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_error_plugin);
mainActivity.deleteConnection(OpenMode.BOX);
} else
cancel(true);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_fail_authenticate);
mainActivity.deleteConnection(OpenMode.BOX);
} else
cancel(true);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.failed_cloud_new_connection);
mainActivity.deleteConnection(OpenMode.BOX);
} else
cancel(true);
return false;
}
break;
case 5:
// ONEDRIVE
try {
CloudEntry cloudEntryOnedrive = null;
CloudEntry savedCloudEntryOnedrive;
CloudStorage cloudStorageOnedrive;
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
cloudStorageOnedrive = new OneDrive(mainActivity.getApplicationContext(), data.getString(1), data.getString(2));
} else {
cancel(true);
return false;
}
if ((savedCloudEntryOnedrive = cloudHandler.findEntry(OpenMode.ONEDRIVE)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageOnedrive.loadAsString(savedCloudEntryOnedrive.getPersistData().value);
} catch (ParseException e) {
e.printStackTrace();
// we need to persist data again
cloudStorageOnedrive.login();
cloudEntryOnedrive = new CloudEntry(OpenMode.ONEDRIVE, cloudStorageOnedrive.saveAsString());
cloudHandler.updateEntry(OpenMode.ONEDRIVE, cloudEntryOnedrive);
}
} else {
cloudStorageOnedrive.login();
cloudEntryOnedrive = new CloudEntry(OpenMode.ONEDRIVE, cloudStorageOnedrive.saveAsString());
cloudHandler.addEntry(cloudEntryOnedrive);
}
dataUtils.addAccount(cloudStorageOnedrive);
hasUpdatedDrawer = true;
} catch (CloudPluginException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_error_plugin);
mainActivity.deleteConnection(OpenMode.ONEDRIVE);
} else
cancel(true);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.cloud_fail_authenticate);
mainActivity.deleteConnection(OpenMode.ONEDRIVE);
} else
cancel(true);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
AppConfig.toast(mainActivity, R.string.failed_cloud_new_connection);
mainActivity.deleteConnection(OpenMode.ONEDRIVE);
} else
cancel(true);
return false;
}
break;
default:
final MainActivity mainActivity = this.mainActivity.get();
if (mainActivity != null) {
Toast.makeText(mainActivity, R.string.cloud_error_failed_restart, Toast.LENGTH_LONG).show();
} else
cancel(true);
return false;
}
} while (data.moveToNext());
}
return hasUpdatedDrawer;
}
Aggregations