Search in sources :

Example 1 with ParseException

use of com.cloudrail.si.exceptions.ParseException in project cloudrail-si-android-sdk by CloudRail.

the class Services method prepare.

// --------- Public Methods -----------
void prepare(Activity context) {
    this.context = context;
    CloudRail.setAppKey(CLOUDRAIL_LICENSE_KEY);
    this.initDropbox();
    this.initBox();
    this.initGoogleDrive();
    this.initOneDrive();
    this.initOneDriveBusiness();
    this.initEgnyte();
    SharedPreferences sharedPreferences = context.getPreferences(Context.MODE_PRIVATE);
    try {
        String persistent = sharedPreferences.getString("dropboxPersistent", null);
        if (persistent != null)
            dropbox.get().loadAsString(persistent);
        persistent = sharedPreferences.getString("boxPersistent", null);
        if (persistent != null)
            box.get().loadAsString(persistent);
        persistent = sharedPreferences.getString("googledrivePersistent", null);
        if (persistent != null)
            googledrive.get().loadAsString(persistent);
        persistent = sharedPreferences.getString("onedrivePersistent", null);
        if (persistent != null)
            onedrive.get().loadAsString(persistent);
        persistent = sharedPreferences.getString("onedrivebusinessPersistent", null);
        if (persistent != null)
            onedrivebusiness.get().loadAsString(persistent);
        persistent = sharedPreferences.getString("egnytePersistent", null);
        if (persistent != null)
            egnyte.get().loadAsString(persistent);
    } catch (ParseException e) {
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) ParseException(com.cloudrail.si.exceptions.ParseException)

Example 2 with ParseException

use of com.cloudrail.si.exceptions.ParseException in project cloudrail-si-android-sdk by CloudRail.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // Configure CloudRail
    CloudRail.setAppKey(LICENSE_KEY);
    CloudRail.setAdvancedAuthenticationMode(true);
    // Register click listener
    Button dbLogin = (Button) findViewById(R.id.dropbox);
    dbLogin.setOnClickListener(new StartLogin());
    SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
    if (sharedPreferences.getString(AUTH_DATA, null) != null) {
        try {
            cs.loadAsString(sharedPreferences.getString(AUTH_DATA, null));
            new GetUserName().execute();
        } catch (ParseException e) {
            // If parsing fails, remove user data -> new login required
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.remove(AUTH_DATA);
            editor.apply();
        }
    } else if (sharedPreferences.getBoolean(LOGIN_PENDING, false)) {
        /* If a login is pending, continue it */
        // Set pending login to false
        final SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(LOGIN_PENDING, false);
        editor.apply();
        // Set result and continue login
        CloudRail.setAuthenticationResponse(getIntent());
        new Thread() {

            @Override
            public void run() {
                cs.login();
                editor.putString(AUTH_DATA, cs.saveAsString());
                editor.apply();
                new GetUserName().execute();
            }
        }.start();
    } else {
        TextView tv = (TextView) findViewById(R.id.username);
        tv.setText("not logged in");
    }
}
Also used : Button(android.widget.Button) SharedPreferences(android.content.SharedPreferences) TextView(android.widget.TextView) ParseException(com.cloudrail.si.exceptions.ParseException)

Example 3 with ParseException

use of com.cloudrail.si.exceptions.ParseException 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)

Aggregations

ParseException (com.cloudrail.si.exceptions.ParseException)3 SharedPreferences (android.content.SharedPreferences)2 AsyncTask (android.os.AsyncTask)1 Button (android.widget.Button)1 TextView (android.widget.TextView)1 CloudEntry (com.amaze.filemanager.database.models.CloudEntry)1 CloudPluginException (com.amaze.filemanager.exceptions.CloudPluginException)1 AuthenticationException (com.cloudrail.si.exceptions.AuthenticationException)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