use of com.cloudrail.si.interfaces.CloudStorage in project AmazeFileManager by TeamAmaze.
the class MainActivity method onLoadFinished.
@Override
public void onLoadFinished(Loader<Cursor> loader, final Cursor data) {
if (data == null) {
Toast.makeText(this, getResources().getString(R.string.cloud_error_failed_restart), Toast.LENGTH_LONG).show();
return;
}
cloudSyncTask = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
boolean hasUpdatedDrawer = false;
if (data.getCount() > 0 && data.moveToFirst()) {
do {
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();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.failed_cloud_api_key));
return false;
}
break;
case 2:
// DRIVE
try {
CloudEntry cloudEntryGdrive = null;
CloudEntry savedCloudEntryGdrive;
GoogleDrive cloudStorageDrive = new GoogleDrive(getApplicationContext(), data.getString(1), "", CLOUD_AUTHENTICATOR_REDIRECT_URI, data.getString(2));
cloudStorageDrive.useAdvancedAuthentication();
if ((savedCloudEntryGdrive = cloudHandler.findEntry(OpenMode.GDRIVE)) != null) {
try {
cloudStorageDrive.loadAsString(savedCloudEntryGdrive.getPersistData());
} 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();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_error_plugin));
deleteConnection(OpenMode.GDRIVE);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_fail_authenticate));
deleteConnection(OpenMode.GDRIVE);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.failed_cloud_new_connection));
deleteConnection(OpenMode.GDRIVE);
return false;
}
break;
case 3:
// DROPBOX
try {
CloudEntry cloudEntryDropbox = null;
CloudEntry savedCloudEntryDropbox;
CloudStorage cloudStorageDropbox = new Dropbox(getApplicationContext(), data.getString(1), data.getString(2));
if ((savedCloudEntryDropbox = cloudHandler.findEntry(OpenMode.DROPBOX)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageDropbox.loadAsString(savedCloudEntryDropbox.getPersistData());
} 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();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_error_plugin));
deleteConnection(OpenMode.DROPBOX);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_fail_authenticate));
deleteConnection(OpenMode.DROPBOX);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.failed_cloud_new_connection));
deleteConnection(OpenMode.DROPBOX);
return false;
}
break;
case 4:
// BOX
try {
CloudEntry cloudEntryBox = null;
CloudEntry savedCloudEntryBox;
CloudStorage cloudStorageBox = new Box(getApplicationContext(), data.getString(1), data.getString(2));
if ((savedCloudEntryBox = cloudHandler.findEntry(OpenMode.BOX)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageBox.loadAsString(savedCloudEntryBox.getPersistData());
} 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();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_error_plugin));
deleteConnection(OpenMode.BOX);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_fail_authenticate));
deleteConnection(OpenMode.BOX);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.failed_cloud_new_connection));
deleteConnection(OpenMode.BOX);
return false;
}
break;
case 5:
// ONEDRIVE
try {
CloudEntry cloudEntryOnedrive = null;
CloudEntry savedCloudEntryOnedrive;
CloudStorage cloudStorageOnedrive = new OneDrive(getApplicationContext(), data.getString(1), data.getString(2));
if ((savedCloudEntryOnedrive = cloudHandler.findEntry(OpenMode.ONEDRIVE)) != null) {
// we already have the entry and saved state, get it
try {
cloudStorageOnedrive.loadAsString(savedCloudEntryOnedrive.getPersistData());
} 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();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_error_plugin));
deleteConnection(OpenMode.ONEDRIVE);
return false;
} catch (AuthenticationException e) {
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.cloud_fail_authenticate));
deleteConnection(OpenMode.ONEDRIVE);
return false;
} catch (Exception e) {
// any other exception due to network conditions or other error
e.printStackTrace();
AppConfig.toast(MainActivity.this, getResources().getString(R.string.failed_cloud_new_connection));
deleteConnection(OpenMode.ONEDRIVE);
return false;
}
break;
default:
Toast.makeText(MainActivity.this, getResources().getString(R.string.cloud_error_failed_restart), Toast.LENGTH_LONG).show();
return false;
}
} while (data.moveToNext());
}
return hasUpdatedDrawer;
}
@Override
protected void onPostExecute(Boolean refreshDrawer) {
super.onPostExecute(refreshDrawer);
if (refreshDrawer) {
drawer.refreshDrawer();
}
}
}.execute();
}
use of com.cloudrail.si.interfaces.CloudStorage in project AmazeFileManager by TeamAmaze.
the class LoadFilesListTask method doInBackground.
@Override
protected Pair<OpenMode, ArrayList<LayoutElementParcelable>> doInBackground(Void... p) {
HybridFile hFile = null;
if (openmode == OpenMode.UNKNOWN) {
hFile = new HybridFile(OpenMode.UNKNOWN, path);
hFile.generateMode(ma.getActivity());
openmode = hFile.getMode();
if (hFile.isSmb()) {
ma.smbPath = path;
} else if (android.util.Patterns.EMAIL_ADDRESS.matcher(path).matches()) {
openmode = OpenMode.ROOT;
}
}
if (isCancelled())
return null;
ma.folder_count = 0;
ma.file_count = 0;
final ArrayList<LayoutElementParcelable> list;
switch(openmode) {
case SMB:
if (hFile == null) {
hFile = new HybridFile(OpenMode.SMB, path);
}
try {
SmbFile[] smbFile = hFile.getSmbFile(5000).listFiles();
list = ma.addToSmb(smbFile, path);
openmode = OpenMode.SMB;
} catch (SmbAuthException e) {
if (!e.getMessage().toLowerCase().contains("denied")) {
ma.reauthenticateSmb();
}
return null;
} catch (SmbException | NullPointerException e) {
e.printStackTrace();
return null;
}
break;
case SFTP:
HybridFile sftpHFile = new HybridFile(OpenMode.SFTP, path);
list = new ArrayList<LayoutElementParcelable>();
sftpHFile.forEachChildrenFile(c, false, file -> {
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, new OnFileFound() {
@Override
public void onFileFound(HybridFileParcelable file) {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
}
});
openmode = OpenMode.OTG;
break;
case DROPBOX:
case BOX:
case GDRIVE:
case ONEDRIVE:
CloudStorage cloudStorage = dataUtils.getAccount(openmode);
list = new ArrayList<>();
try {
listCloud(path, cloudStorage, openmode, new OnFileFound() {
@Override
public void onFileFound(HybridFileParcelable file) {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
}
});
} catch (CloudPluginException e) {
e.printStackTrace();
AppConfig.toast(c, c.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<>();
RootHelper.getFiles(path, ma.getMainActivity().isRootExplorer(), showHiddenFiles, new RootHelper.GetModeCallBack() {
@Override
public void getMode(OpenMode mode) {
openmode = mode;
}
}, new OnFileFound() {
@Override
public void onFileFound(HybridFileParcelable file) {
LayoutElementParcelable elem = createListParcelables(file);
if (elem != null)
list.add(elem);
}
});
break;
}
if (list != null && !(openmode == OpenMode.CUSTOM && ((path).equals("5") || (path).equals("6")))) {
Collections.sort(list, new FileListSorter(ma.dsort, ma.sortby, ma.asc));
}
return new Pair<>(openmode, list);
}
use of com.cloudrail.si.interfaces.CloudStorage in project AmazeFileManager by TeamAmaze.
the class HybridFile method exists.
public boolean exists() {
boolean exists = false;
if (isSftp()) {
exists = SshClientUtils.execute(new SFtpClientTemplate(path) {
@Override
public Boolean execute(SFTPClient client) throws IOException {
try {
return client.stat(SshClientUtils.extractRemotePathFrom(path)) != null;
} catch (SFTPException notFound) {
return false;
}
}
});
} else if (isSmb()) {
try {
SmbFile smbFile = getSmbFile(2000);
exists = smbFile != null && smbFile.exists();
} catch (SmbException e) {
exists = false;
}
} else if (isDropBoxFile()) {
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
exists = cloudStorageDropbox.exists(CloudUtil.stripPath(OpenMode.DROPBOX, path));
} else if (isBoxFile()) {
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
exists = cloudStorageBox.exists(CloudUtil.stripPath(OpenMode.BOX, path));
} else if (isGoogleDriveFile()) {
CloudStorage cloudStorageGoogleDrive = dataUtils.getAccount(OpenMode.GDRIVE);
exists = cloudStorageGoogleDrive.exists(CloudUtil.stripPath(OpenMode.GDRIVE, path));
} else if (isOneDriveFile()) {
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
exists = cloudStorageOneDrive.exists(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
} else if (isLocal()) {
exists = new File(path).exists();
} else if (isRoot()) {
try {
return RootHelper.fileExists(path);
} catch (ShellNotRunningException e) {
e.printStackTrace();
return false;
}
}
return exists;
}
use of com.cloudrail.si.interfaces.CloudStorage in project AmazeFileManager by TeamAmaze.
the class HybridFile method getInputStream.
public InputStream getInputStream(Context context) {
InputStream inputStream;
switch(mode) {
case SFTP:
inputStream = SshClientUtils.execute(new SFtpClientTemplate(path, false) {
@Override
public InputStream execute(final SFTPClient client) throws IOException {
final RemoteFile rf = client.open(SshClientUtils.extractRemotePathFrom(path));
return rf.new RemoteFileInputStream() {
@Override
public void close() throws IOException {
try {
super.close();
} finally {
rf.close();
client.close();
}
}
};
}
});
break;
case SMB:
try {
inputStream = new SmbFile(path).getInputStream();
} catch (IOException e) {
inputStream = null;
e.printStackTrace();
}
break;
case OTG:
ContentResolver contentResolver = context.getContentResolver();
DocumentFile documentSourceFile = OTGUtil.getDocumentFile(path, context, false);
try {
inputStream = contentResolver.openInputStream(documentSourceFile.getUri());
} catch (FileNotFoundException e) {
e.printStackTrace();
inputStream = null;
}
break;
case DROPBOX:
CloudStorage cloudStorageDropbox = dataUtils.getAccount(OpenMode.DROPBOX);
Log.d(getClass().getSimpleName(), CloudUtil.stripPath(OpenMode.DROPBOX, path));
inputStream = cloudStorageDropbox.download(CloudUtil.stripPath(OpenMode.DROPBOX, path));
break;
case BOX:
CloudStorage cloudStorageBox = dataUtils.getAccount(OpenMode.BOX);
inputStream = cloudStorageBox.download(CloudUtil.stripPath(OpenMode.BOX, path));
break;
case GDRIVE:
CloudStorage cloudStorageGDrive = dataUtils.getAccount(OpenMode.GDRIVE);
inputStream = cloudStorageGDrive.download(CloudUtil.stripPath(OpenMode.GDRIVE, path));
break;
case ONEDRIVE:
CloudStorage cloudStorageOneDrive = dataUtils.getAccount(OpenMode.ONEDRIVE);
inputStream = cloudStorageOneDrive.download(CloudUtil.stripPath(OpenMode.ONEDRIVE, path));
break;
default:
try {
inputStream = new FileInputStream(path);
} catch (FileNotFoundException e) {
inputStream = null;
e.printStackTrace();
}
break;
}
return inputStream;
}
use of com.cloudrail.si.interfaces.CloudStorage 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