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);
}
}
}
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
}
}
Aggregations