Search in sources :

Example 1 with GoogleAccountsManager

use of org.odk.collect.android.utilities.gdrive.GoogleAccountsManager 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 GoogleAccountsManager

use of org.odk.collect.android.utilities.gdrive.GoogleAccountsManager in project collect by opendatakit.

the class GoogleDriveActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setContentView(R.layout.drive_layout);
    super.onCreate(savedInstanceState);
    setProgressBarVisibility(true);
    initToolbar();
    parentId = null;
    alertShowing = false;
    toDownload = new ArrayList<>();
    filteredList = new ArrayList<>();
    driveList = new ArrayList<>();
    if (savedInstanceState != null && savedInstanceState.containsKey(MY_DRIVE_KEY)) {
        // recover state on rotate
        myDrive = savedInstanceState.getBoolean(MY_DRIVE_KEY);
        String[] patharray = savedInstanceState.getStringArray(PATH_KEY);
        currentPath = buildPath(patharray);
        parentId = savedInstanceState.getString(PARENT_KEY);
        alertMsg = savedInstanceState.getString(ALERT_MSG_KEY);
        alertShowing = savedInstanceState.getBoolean(ALERT_SHOWING_KEY);
        ArrayList<DriveListItem> dl = savedInstanceState.getParcelableArrayList(DRIVE_ITEMS_KEY);
        filteredList.addAll(dl);
    } else {
        // new
        myDrive = false;
        if (!isDeviceOnline()) {
            createAlertDialog(getString(R.string.no_connection));
        }
    }
    // restore any task state
    if (getLastCustomNonConfigurationInstance() instanceof RetrieveDriveFileContentsAsyncTask) {
        retrieveDriveFileContentsAsyncTask = (RetrieveDriveFileContentsAsyncTask) getLastNonConfigurationInstance();
        setProgressBarIndeterminateVisibility(true);
    } else {
        getFileTask = (GetFileTask) getLastNonConfigurationInstance();
        if (getFileTask != null) {
            getFileTask.setGoogleDriveFormDownloadListener(this);
        }
    }
    if (getFileTask != null && getFileTask.getStatus() == AsyncTask.Status.FINISHED) {
        try {
            dismissDialog(PROGRESS_DIALOG);
        } catch (Exception e) {
            Timber.i("Exception was thrown while dismissing a dialog.");
        }
    }
    if (alertShowing) {
        try {
            dismissDialog(PROGRESS_DIALOG);
        } catch (Exception e) {
            // don't care...
            Timber.i("Exception was thrown while dismissing a dialog.");
        }
        createAlertDialog(alertMsg);
    }
    rootButton = findViewById(R.id.root_button);
    if (myDrive) {
        rootButton.setText(getString(R.string.go_shared));
    } else {
        rootButton.setText(getString(R.string.go_drive));
    }
    rootButton.setOnClickListener(this);
    backButton = findViewById(R.id.back_button);
    backButton.setEnabled(parentId != null);
    backButton.setOnClickListener(this);
    downloadButton = findViewById(R.id.download_button);
    downloadButton.setOnClickListener(this);
    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listView.setItemsCanFocus(false);
    sortingOptions = new String[] { getString(R.string.sort_by_name_asc), getString(R.string.sort_by_name_desc) };
    accountsManager = new GoogleAccountsManager(this);
    accountsManager.setListener(this);
    driveHelper = accountsManager.getDriveHelper();
    getResultsFromApi();
}
Also used : DriveListItem(org.odk.collect.android.logic.DriveListItem) GoogleAccountsManager(org.odk.collect.android.utilities.gdrive.GoogleAccountsManager) UserRecoverableAuthIOException(com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException) IOException(java.io.IOException) MultipleFoldersFoundException(org.odk.collect.android.exception.MultipleFoldersFoundException)

Example 3 with GoogleAccountsManager

use of org.odk.collect.android.utilities.gdrive.GoogleAccountsManager in project collect by opendatakit.

the class GoogleSheetsUploaderActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Timber.i("onCreate: %s", ((savedInstanceState == null) ? "creating" : "re-initializing"));
    // if we start this activity, the following must be true:
    // 1) Google Sheets is selected in preferences
    // 2) A google user is selected
    // default initializers
    alertMsg = getString(R.string.please_wait);
    alertShowing = false;
    setTitle(getString(R.string.send_data));
    // resets alert message and showing dialog if the screen is rotated
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(ALERT_MSG)) {
            alertMsg = savedInstanceState.getString(ALERT_MSG);
        }
        if (savedInstanceState.containsKey(ALERT_SHOWING)) {
            alertShowing = savedInstanceState.getBoolean(ALERT_SHOWING, false);
        }
    }
    long[] selectedInstanceIDs;
    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);
    }
    accountsManager = new GoogleAccountsManager(this);
    accountsManager.setListener(this);
    getResultsFromApi();
}
Also used : GoogleAccountsManager(org.odk.collect.android.utilities.gdrive.GoogleAccountsManager) Intent(android.content.Intent)

Example 4 with GoogleAccountsManager

use of org.odk.collect.android.utilities.gdrive.GoogleAccountsManager in project collect by opendatakit.

the class ServerPreferencesFragment method initAccountPreferences.

public void initAccountPreferences() {
    accountsManager = new GoogleAccountsManager(this);
    accountsManager.setListener(this);
    accountsManager.disableAutoChooseAccount();
    selectedGoogleAccountPreference.setSummary(accountsManager.getSelectedAccount());
    selectedGoogleAccountPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference preference) {
            accountsManager.chooseAccount();
            return true;
        }
    });
}
Also used : GoogleAccountsManager(org.odk.collect.android.utilities.gdrive.GoogleAccountsManager) EditTextPreference(android.preference.EditTextPreference) Preference(android.preference.Preference)

Aggregations

GoogleAccountsManager (org.odk.collect.android.utilities.gdrive.GoogleAccountsManager)4 Intent (android.content.Intent)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 EditTextPreference (android.preference.EditTextPreference)1 Preference (android.preference.Preference)1 UserRecoverableAuthIOException (com.google.api.client.googleapis.extensions.android.gms.auth.UserRecoverableAuthIOException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 InstancesDao (org.odk.collect.android.dao.InstancesDao)1 MultipleFoldersFoundException (org.odk.collect.android.exception.MultipleFoldersFoundException)1 DriveListItem (org.odk.collect.android.logic.DriveListItem)1 GeneralSharedPreferences (org.odk.collect.android.preferences.GeneralSharedPreferences)1 InstanceGoogleSheetsUploader (org.odk.collect.android.tasks.InstanceGoogleSheetsUploader)1 InstanceServerUploader (org.odk.collect.android.tasks.InstanceServerUploader)1