Search in sources :

Example 1 with InstanceGoogleSheetsUploader

use of org.odk.collect.android.tasks.InstanceGoogleSheetsUploader in project collect by opendatakit.

the class NetworkReceiver method uploadForms.

/**
 * @param isFormAutoSendOptionEnabled represents whether the auto-send option is enabled at the app level
 */
private void uploadForms(Context context, boolean isFormAutoSendOptionEnabled) {
    if (!running) {
        running = true;
        ArrayList<Long> toUpload = new ArrayList<>();
        Cursor c = new InstancesDao().getFinalizedInstancesCursor();
        try {
            if (c != null && c.getCount() > 0) {
                c.move(-1);
                String formId;
                while (c.moveToNext()) {
                    formId = c.getString(c.getColumnIndex(InstanceColumns.JR_FORM_ID));
                    if (isFormAutoSendEnabled(formId, isFormAutoSendOptionEnabled)) {
                        Long l = c.getLong(c.getColumnIndex(InstanceColumns._ID));
                        toUpload.add(l);
                    }
                }
            }
        } finally {
            if (c != null) {
                c.close();
            }
        }
        if (toUpload.size() < 1) {
            running = false;
            return;
        }
        Long[] toSendArray = new Long[toUpload.size()];
        toUpload.toArray(toSendArray);
        GeneralSharedPreferences settings = GeneralSharedPreferences.getInstance();
        String protocol = (String) settings.get(PreferenceKeys.KEY_PROTOCOL);
        if (protocol.equals(context.getString(R.string.protocol_google_sheets))) {
            String googleUsername = (String) settings.get(PreferenceKeys.KEY_SELECTED_GOOGLE_ACCOUNT);
            if (googleUsername == null || googleUsername.isEmpty()) {
                // just quit if there's no username
                running = false;
                return;
            }
            GoogleAccountsManager accountsManager = new GoogleAccountsManager(Collect.getInstance());
            accountsManager.getCredential().setSelectedAccountName(googleUsername);
            instanceGoogleSheetsUploader = new InstanceGoogleSheetsUploader(accountsManager);
            instanceGoogleSheetsUploader.setUploaderListener(this);
            instanceGoogleSheetsUploader.execute(toSendArray);
        } else {
            // get the username, password, and server from preferences
            String storedUsername = (String) settings.get(PreferenceKeys.KEY_USERNAME);
            String storedPassword = (String) settings.get(PreferenceKeys.KEY_PASSWORD);
            String server = (String) settings.get(PreferenceKeys.KEY_SERVER_URL);
            String url = server + settings.get(PreferenceKeys.KEY_FORMLIST_URL);
            Uri u = Uri.parse(url);
            WebUtils.addCredentials(storedUsername, storedPassword, u.getHost());
            instanceServerUploader = new InstanceServerUploader();
            instanceServerUploader.setUploaderListener(this);
            instanceServerUploader.execute(toSendArray);
        }
    }
}
Also used : InstanceServerUploader(org.odk.collect.android.tasks.InstanceServerUploader) GoogleAccountsManager(org.odk.collect.android.utilities.gdrive.GoogleAccountsManager) InstancesDao(org.odk.collect.android.dao.InstancesDao) ArrayList(java.util.ArrayList) GeneralSharedPreferences(org.odk.collect.android.preferences.GeneralSharedPreferences) Cursor(android.database.Cursor) InstanceGoogleSheetsUploader(org.odk.collect.android.tasks.InstanceGoogleSheetsUploader) Uri(android.net.Uri)

Example 2 with InstanceGoogleSheetsUploader

use of org.odk.collect.android.tasks.InstanceGoogleSheetsUploader in project collect by opendatakit.

the class GoogleSheetsUploaderActivity method runTask.

private void runTask() {
    instanceGoogleSheetsUploader = (InstanceGoogleSheetsUploader) getLastCustomNonConfigurationInstance();
    if (instanceGoogleSheetsUploader == null) {
        instanceGoogleSheetsUploader = new InstanceGoogleSheetsUploader(accountsManager);
        // ensure we have a google account selected
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        String googleUsername = prefs.getString(PreferenceKeys.KEY_SELECTED_GOOGLE_ACCOUNT, null);
        if (googleUsername == null || googleUsername.equals("")) {
            showDialog(GOOGLE_USER_DIALOG);
            return;
        }
        showDialog(PROGRESS_DIALOG);
        instanceGoogleSheetsUploader.setUploaderListener(this);
        instanceGoogleSheetsUploader.execute(instancesToSend);
    } else {
    // it's not null, so we have a task running
    // progress dialog is handled by the system
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) InstanceGoogleSheetsUploader(org.odk.collect.android.tasks.InstanceGoogleSheetsUploader)

Aggregations

InstanceGoogleSheetsUploader (org.odk.collect.android.tasks.InstanceGoogleSheetsUploader)2 SharedPreferences (android.content.SharedPreferences)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 ArrayList (java.util.ArrayList)1 InstancesDao (org.odk.collect.android.dao.InstancesDao)1 GeneralSharedPreferences (org.odk.collect.android.preferences.GeneralSharedPreferences)1 InstanceServerUploader (org.odk.collect.android.tasks.InstanceServerUploader)1 GoogleAccountsManager (org.odk.collect.android.utilities.gdrive.GoogleAccountsManager)1