Search in sources :

Example 1 with CloudEntry

use of com.amaze.filemanager.database.models.explorer.CloudEntry in project AmazeFileManager by TeamAmaze.

the class MainActivity method onCreateLoader.

@NonNull
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    Uri uri = Uri.withAppendedPath(Uri.parse("content://" + CloudContract.PROVIDER_AUTHORITY), "/keys.db/secret_keys");
    String[] projection = new String[] { CloudContract.COLUMN_ID, CloudContract.COLUMN_CLIENT_ID, CloudContract.COLUMN_CLIENT_SECRET_KEY };
    switch(id) {
        case REQUEST_CODE_CLOUD_LIST_KEY:
            Uri uriAppendedPath = uri;
            switch(OpenMode.getOpenMode(args.getInt(ARGS_KEY_LOADER, 2))) {
                case GDRIVE:
                    uriAppendedPath = ContentUris.withAppendedId(uri, 2);
                    break;
                case DROPBOX:
                    uriAppendedPath = ContentUris.withAppendedId(uri, 3);
                    break;
                case BOX:
                    uriAppendedPath = ContentUris.withAppendedId(uri, 4);
                    break;
                case ONEDRIVE:
                    uriAppendedPath = ContentUris.withAppendedId(uri, 5);
                    break;
            }
            return new CursorLoader(this, uriAppendedPath, projection, null, null, null);
        case REQUEST_CODE_CLOUD_LIST_KEYS:
            try {
                List<CloudEntry> cloudEntries = cloudHandler.getAllEntries();
                // we want keys for services saved in database, and the cloudrail app key which
                // is at index 1
                String[] ids = new String[cloudEntries.size() + 1];
                ids[0] = 1 + "";
                for (int i = 1; i <= cloudEntries.size(); i++) {
                    // we need to get only those cloud details which user wants
                    switch(cloudEntries.get(i - 1).getServiceType()) {
                        case GDRIVE:
                            ids[i] = 2 + "";
                            break;
                        case DROPBOX:
                            ids[i] = 3 + "";
                            break;
                        case BOX:
                            ids[i] = 4 + "";
                            break;
                        case ONEDRIVE:
                            ids[i] = 5 + "";
                            break;
                    }
                }
                return new CursorLoader(this, uri, projection, CloudContract.COLUMN_ID, ids, null);
            } catch (CloudPluginException e) {
                e.printStackTrace();
                Toast.makeText(this, getResources().getString(R.string.cloud_error_plugin), Toast.LENGTH_LONG).show();
            }
        default:
            Uri undefinedUriAppendedPath = ContentUris.withAppendedId(uri, 7);
            return new CursorLoader(this, undefinedUriAppendedPath, projection, null, null, null);
    }
}
Also used : CursorLoader(androidx.loader.content.CursorLoader) CloudPluginException(com.amaze.filemanager.file_operations.exceptions.CloudPluginException) CloudEntry(com.amaze.filemanager.database.models.explorer.CloudEntry) Uri(android.net.Uri) NonNull(androidx.annotation.NonNull)

Example 2 with CloudEntry

use of com.amaze.filemanager.database.models.explorer.CloudEntry 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;
}
Also used : CloudStorage(com.cloudrail.si.interfaces.CloudStorage) CloudPluginException(com.amaze.filemanager.file_operations.exceptions.CloudPluginException) OneDrive(com.cloudrail.si.services.OneDrive) AuthenticationException(com.cloudrail.si.exceptions.AuthenticationException) GoogleDrive(com.cloudrail.si.services.GoogleDrive) CloudEntry(com.amaze.filemanager.database.models.explorer.CloudEntry) Box(com.cloudrail.si.services.Box) MainActivity(com.amaze.filemanager.ui.activities.MainActivity) ParseException(com.cloudrail.si.exceptions.ParseException) Dropbox(com.cloudrail.si.services.Dropbox) AuthenticationException(com.cloudrail.si.exceptions.AuthenticationException) CloudPluginException(com.amaze.filemanager.file_operations.exceptions.CloudPluginException) ParseException(com.cloudrail.si.exceptions.ParseException) NonNull(androidx.annotation.NonNull)

Example 3 with CloudEntry

use of com.amaze.filemanager.database.models.explorer.CloudEntry in project AmazeFileManager by TeamAmaze.

the class ExplorerDatabaseMigrationTest method migrateFromV7.

@Test
public void migrateFromV7() throws IOException {
    SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 7);
    db.execSQL("INSERT INTO " + TABLE_CLOUD_PERSIST + "(" + COLUMN_CLOUD_ID + "," + COLUMN_CLOUD_SERVICE + "," + COLUMN_CLOUD_PERSIST + ") VALUES (1," + (OpenMode.GDRIVE.ordinal() - 1) + ",'abcd')");
    db.execSQL("INSERT INTO " + TABLE_CLOUD_PERSIST + "(" + COLUMN_CLOUD_ID + "," + COLUMN_CLOUD_SERVICE + "," + COLUMN_CLOUD_PERSIST + ") VALUES (2," + (OpenMode.DROPBOX.ordinal() - 1) + ",'efgh')");
    db.execSQL("INSERT INTO " + TABLE_CLOUD_PERSIST + "(" + COLUMN_CLOUD_ID + "," + COLUMN_CLOUD_SERVICE + "," + COLUMN_CLOUD_PERSIST + ") VALUES (3," + (OpenMode.BOX.ordinal() - 1) + ",'ijkl')");
    db.execSQL("INSERT INTO " + TABLE_CLOUD_PERSIST + "(" + COLUMN_CLOUD_ID + "," + COLUMN_CLOUD_SERVICE + "," + COLUMN_CLOUD_PERSIST + ") VALUES (4," + (OpenMode.ONEDRIVE.ordinal() - 1) + ",'mnop')");
    db.close();
    ExplorerDatabase explorerDatabase = Room.databaseBuilder(InstrumentationRegistry.getInstrumentation().getTargetContext(), ExplorerDatabase.class, TEST_DB).addMigrations(MIGRATION_7_8).allowMainThreadQueries().build();
    explorerDatabase.getOpenHelper().getWritableDatabase();
    CloudEntry verify = explorerDatabase.cloudEntryDao().findByServiceType(OpenMode.GDRIVE.ordinal()).subscribeOn(Schedulers.trampoline()).blockingGet();
    assertEquals(1, verify.getId());
    assertEquals("abcd", verify.getPersistData().toString());
    verify = explorerDatabase.cloudEntryDao().findByServiceType(OpenMode.BOX.ordinal()).subscribeOn(Schedulers.trampoline()).blockingGet();
    assertEquals(3, verify.getId());
    assertEquals("ijkl", verify.getPersistData().toString());
    verify = explorerDatabase.cloudEntryDao().findByServiceType(OpenMode.DROPBOX.ordinal()).subscribeOn(Schedulers.trampoline()).blockingGet();
    assertEquals(2, verify.getId());
    assertEquals("efgh", verify.getPersistData().toString());
    verify = explorerDatabase.cloudEntryDao().findByServiceType(OpenMode.ONEDRIVE.ordinal()).subscribeOn(Schedulers.trampoline()).blockingGet();
    assertEquals(4, verify.getId());
    assertEquals("mnop", verify.getPersistData().toString());
    explorerDatabase.close();
}
Also used : SupportSQLiteDatabase(androidx.sqlite.db.SupportSQLiteDatabase) CloudEntry(com.amaze.filemanager.database.models.explorer.CloudEntry) Test(org.junit.Test)

Aggregations

CloudEntry (com.amaze.filemanager.database.models.explorer.CloudEntry)3 NonNull (androidx.annotation.NonNull)2 CloudPluginException (com.amaze.filemanager.file_operations.exceptions.CloudPluginException)2 Uri (android.net.Uri)1 CursorLoader (androidx.loader.content.CursorLoader)1 SupportSQLiteDatabase (androidx.sqlite.db.SupportSQLiteDatabase)1 MainActivity (com.amaze.filemanager.ui.activities.MainActivity)1 AuthenticationException (com.cloudrail.si.exceptions.AuthenticationException)1 ParseException (com.cloudrail.si.exceptions.ParseException)1 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)1 Box (com.cloudrail.si.services.Box)1 Dropbox (com.cloudrail.si.services.Dropbox)1 GoogleDrive (com.cloudrail.si.services.GoogleDrive)1 OneDrive (com.cloudrail.si.services.OneDrive)1 Test (org.junit.Test)1