Search in sources :

Example 1 with InstanceServerUploader

use of org.odk.collect.android.tasks.InstanceServerUploader 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 InstanceServerUploader

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

the class InstanceUploaderActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Timber.i("onCreate: %s", ((savedInstanceState == null) ? "creating" : "re-initializing"));
    alertMsg = getString(R.string.please_wait);
    uploadedInstances = new HashMap<String, String>();
    setTitle(getString(R.string.send_data));
    // get any simple saved state...
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(ALERT_MSG)) {
            alertMsg = savedInstanceState.getString(ALERT_MSG);
        }
        url = savedInstanceState.getString(AUTH_URI);
    }
    // and if we are resuming, use the TO_SEND list of not-yet-sent submissions
    // Otherwise, construct the list from the incoming intent value
    long[] selectedInstanceIDs = null;
    if (savedInstanceState != null && savedInstanceState.containsKey(TO_SEND)) {
        selectedInstanceIDs = savedInstanceState.getLongArray(TO_SEND);
    } else {
        // get instances to upload...
        Intent intent = getIntent();
        selectedInstanceIDs = intent.getLongArrayExtra(FormEntryActivity.KEY_INSTANCES);
    }
    instancesToSend = ArrayUtils.toObject(selectedInstanceIDs);
    // at this point, we don't expect this to be empty...
    if (instancesToSend.length == 0) {
        Timber.e("onCreate: No instances to upload!");
    // drop through -- everything will process through OK
    } else {
        Timber.i("onCreate: Beginning upload of %d instances!", instancesToSend.length);
    }
    // get the task if we've changed orientations. If it's null it's a new upload.
    instanceServerUploader = (InstanceServerUploader) getLastCustomNonConfigurationInstance();
    if (instanceServerUploader == null) {
        // setup dialog and upload task
        showDialog(PROGRESS_DIALOG);
        instanceServerUploader = new InstanceServerUploader();
        // register this activity with the new uploader task
        instanceServerUploader.setUploaderListener(this);
        instanceServerUploader.execute(instancesToSend);
    }
}
Also used : InstanceServerUploader(org.odk.collect.android.tasks.InstanceServerUploader) Intent(android.content.Intent)

Example 3 with InstanceServerUploader

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

the class InstanceUploaderActivity method updatedCredentials.

@Override
public void updatedCredentials() {
    showDialog(PROGRESS_DIALOG);
    instanceServerUploader = new InstanceServerUploader();
    // register this activity with the new uploader task
    instanceServerUploader.setUploaderListener(this);
    instanceServerUploader.execute(instancesToSend);
}
Also used : InstanceServerUploader(org.odk.collect.android.tasks.InstanceServerUploader)

Aggregations

InstanceServerUploader (org.odk.collect.android.tasks.InstanceServerUploader)3 Intent (android.content.Intent)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 InstanceGoogleSheetsUploader (org.odk.collect.android.tasks.InstanceGoogleSheetsUploader)1 GoogleAccountsManager (org.odk.collect.android.utilities.gdrive.GoogleAccountsManager)1