Search in sources :

Example 6 with CloudPluginException

use of com.amaze.filemanager.exceptions.CloudPluginException 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();
}
Also used : AuthenticationException(com.cloudrail.si.exceptions.AuthenticationException) AsyncTask(android.os.AsyncTask) GoogleDrive(com.cloudrail.si.services.GoogleDrive) CloudEntry(com.amaze.filemanager.database.models.CloudEntry) Box(com.cloudrail.si.services.Box) ParseException(com.cloudrail.si.exceptions.ParseException) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) AuthenticationException(com.cloudrail.si.exceptions.AuthenticationException) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) OneDrive(com.cloudrail.si.services.OneDrive) ParseException(com.cloudrail.si.exceptions.ParseException) Dropbox(com.cloudrail.si.services.Dropbox)

Example 7 with CloudPluginException

use of com.amaze.filemanager.exceptions.CloudPluginException 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);
}
Also used : LayoutElementParcelable(com.amaze.filemanager.adapters.data.LayoutElementParcelable) OpenMode(com.amaze.filemanager.utils.OpenMode) SmbFile(jcifs.smb.SmbFile) SmbException(jcifs.smb.SmbException) HybridFileParcelable(com.amaze.filemanager.filesystem.HybridFileParcelable) CloudStorage(com.cloudrail.si.interfaces.CloudStorage) HybridFile(com.amaze.filemanager.filesystem.HybridFile) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SmbAuthException(jcifs.smb.SmbAuthException) RootHelper(com.amaze.filemanager.filesystem.RootHelper) FileListSorter(com.amaze.filemanager.utils.files.FileListSorter) OnFileFound(com.amaze.filemanager.utils.OnFileFound) Pair(android.support.v4.util.Pair)

Example 8 with CloudPluginException

use of com.amaze.filemanager.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.

the class HybridFile method listFiles.

/**
 * Helper method to list children of this file
 * @deprecated use forEachChildrenFile()
 */
public ArrayList<HybridFileParcelable> listFiles(Context context, boolean isRoot) {
    ArrayList<HybridFileParcelable> arrayList = new ArrayList<>();
    switch(mode) {
        case SFTP:
            try {
                arrayList = SshClientUtils.execute(new SFtpClientTemplate(path) {

                    @Override
                    public ArrayList<HybridFileParcelable> execute(SFTPClient client) throws IOException {
                        ArrayList<HybridFileParcelable> retval = new ArrayList<HybridFileParcelable>();
                        try {
                            for (RemoteResourceInfo info : client.ls(SshClientUtils.extractRemotePathFrom(path))) {
                                HybridFileParcelable f = new HybridFileParcelable(String.format("%s/%s", path, info.getName()));
                                f.setName(info.getName());
                                f.setMode(OpenMode.SFTP);
                                f.setDirectory(info.isDirectory());
                                f.setDate(info.getAttributes().getMtime() * 1000);
                                f.setSize(f.isDirectory() ? 0 : info.getAttributes().getSize());
                                f.setPermission(Integer.toString(FilePermission.toMask(info.getAttributes().getPermissions()), 8));
                                retval.add(f);
                            }
                        } catch (IOException e) {
                            Log.w("DEBUG.listFiles", "IOException", e);
                        }
                        return retval;
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
                arrayList.clear();
            }
            break;
        case SMB:
            try {
                SmbFile smbFile = new SmbFile(path);
                for (SmbFile smbFile1 : smbFile.listFiles()) {
                    HybridFileParcelable baseFile = new HybridFileParcelable(smbFile1.getPath());
                    baseFile.setName(smbFile1.getName());
                    baseFile.setMode(OpenMode.SMB);
                    baseFile.setDirectory(smbFile1.isDirectory());
                    baseFile.setDate(smbFile1.lastModified());
                    baseFile.setSize(baseFile.isDirectory() ? 0 : smbFile1.length());
                    arrayList.add(baseFile);
                }
            } catch (MalformedURLException e) {
                if (arrayList != null)
                    arrayList.clear();
                e.printStackTrace();
            } catch (SmbException e) {
                if (arrayList != null)
                    arrayList.clear();
                e.printStackTrace();
            }
            break;
        case OTG:
            arrayList = OTGUtil.getDocumentFilesList(path, context);
            break;
        case DROPBOX:
        case BOX:
        case GDRIVE:
        case ONEDRIVE:
            try {
                arrayList = CloudUtil.listFiles(path, dataUtils.getAccount(mode), mode);
            } catch (CloudPluginException e) {
                e.printStackTrace();
                arrayList = new ArrayList<>();
            }
            break;
        default:
            arrayList = RootHelper.getFilesList(path, isRoot, true, null);
    }
    return arrayList;
}
Also used : SmbException(jcifs.smb.SmbException) SFtpClientTemplate(com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) MalformedURLException(java.net.MalformedURLException) ArrayList(java.util.ArrayList) SFTPClient(net.schmizz.sshj.sftp.SFTPClient) RemoteResourceInfo(net.schmizz.sshj.sftp.RemoteResourceInfo) IOException(java.io.IOException) ShellNotRunningException(com.amaze.filemanager.exceptions.ShellNotRunningException) SmbException(jcifs.smb.SmbException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SFTPException(net.schmizz.sshj.sftp.SFTPException) SmbFile(jcifs.smb.SmbFile)

Example 9 with CloudPluginException

use of com.amaze.filemanager.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.

the class CloudHandler method getAllEntries.

public List<CloudEntry> getAllEntries() throws CloudPluginException {
    if (!CloudSheetFragment.isCloudProviderAvailable(context))
        throw new CloudPluginException();
    List<CloudEntry> entryList = new ArrayList<>();
    // Select all query
    String query = "Select * FROM " + TABLE_CLOUD_PERSIST;
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = sqLiteDatabase.rawQuery(query, null);
        // Looping through all rows and adding them to list
        if (cursor.getCount() > 0 && cursor.moveToFirst()) {
            do {
                CloudEntry cloudEntry = new CloudEntry();
                cloudEntry.setId((cursor.getInt(0)));
                cloudEntry.setServiceType(OpenMode.getOpenMode(cursor.getInt(1)));
                try {
                    cloudEntry.setPersistData(CryptUtil.decryptPassword(context, cursor.getString(2)));
                } catch (Exception e) {
                    e.printStackTrace();
                    cloudEntry.setPersistData("");
                    entryList.add(cloudEntry);
                    continue;
                }
                entryList.add(cloudEntry);
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return entryList;
}
Also used : CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) ArrayList(java.util.ArrayList) CloudEntry(com.amaze.filemanager.database.models.CloudEntry) Cursor(android.database.Cursor) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException)

Example 10 with CloudPluginException

use of com.amaze.filemanager.exceptions.CloudPluginException in project AmazeFileManager by TeamAmaze.

the class CloudHandler method findEntry.

public CloudEntry findEntry(OpenMode serviceType) throws CloudPluginException {
    if (!CloudSheetFragment.isCloudProviderAvailable(context))
        throw new CloudPluginException();
    String query = "Select * FROM " + TABLE_CLOUD_PERSIST + " WHERE " + COLUMN_CLOUD_SERVICE + "= \"" + serviceType.ordinal() + "\"";
    SQLiteDatabase sqLiteDatabase = getReadableDatabase();
    Cursor cursor = sqLiteDatabase.rawQuery(query, null);
    CloudEntry cloudEntry = new CloudEntry();
    if (cursor.moveToFirst()) {
        cloudEntry.setId((cursor.getInt(0)));
        cloudEntry.setServiceType(serviceType);
        try {
            cloudEntry.setPersistData(CryptUtil.decryptPassword(context, cursor.getString(2)));
        } catch (Exception e) {
            e.printStackTrace();
            cloudEntry.setPersistData("");
            return cloudEntry;
        }
        cursor.close();
    } else {
        cloudEntry = null;
    }
    return cloudEntry;
}
Also used : CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) CloudEntry(com.amaze.filemanager.database.models.CloudEntry) Cursor(android.database.Cursor) CloudPluginException(com.amaze.filemanager.exceptions.CloudPluginException)

Aggregations

CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)11 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 CloudEntry (com.amaze.filemanager.database.models.CloudEntry)4 SmbException (jcifs.smb.SmbException)3 SmbFile (jcifs.smb.SmbFile)3 ContentValues (android.content.ContentValues)2 Cursor (android.database.Cursor)2 CursorLoader (android.support.v4.content.CursorLoader)2 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)2 HybridFileParcelable (com.amaze.filemanager.filesystem.HybridFileParcelable)2 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)2 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 RemoteResourceInfo (net.schmizz.sshj.sftp.RemoteResourceInfo)2 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)2 SFTPException (net.schmizz.sshj.sftp.SFTPException)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1