Search in sources :

Example 1 with GeneralSharedPreferences

use of org.odk.collect.android.preferences.GeneralSharedPreferences 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 GeneralSharedPreferences

use of org.odk.collect.android.preferences.GeneralSharedPreferences in project collect by opendatakit.

the class SharedPreferencesTest method generalSharedPreferencesUpgradeTest.

@Test
public void generalSharedPreferencesUpgradeTest() {
    GeneralSharedPreferences.getInstance().save(KEY_COMPLETED_DEFAULT, false);
    GeneralSharedPreferences.getInstance().reloadPreferences();
    HashMap<String, Object> defaultValues = PreferenceKeys.GENERAL_KEYS;
    GeneralSharedPreferences generalSharedPreferences = GeneralSharedPreferences.getInstance();
    for (String key : SharedPreferencesUtils.getAllGeneralKeys()) {
        if (key.equals(KEY_COMPLETED_DEFAULT)) {
            assertEquals(generalSharedPreferences.get(key), false);
        } else {
            assertEquals(generalSharedPreferences.get(key), defaultValues.get(key));
        }
    }
}
Also used : GeneralSharedPreferences(org.odk.collect.android.preferences.GeneralSharedPreferences) Test(org.junit.Test)

Example 3 with GeneralSharedPreferences

use of org.odk.collect.android.preferences.GeneralSharedPreferences in project collect by opendatakit.

the class SharedPreferencesTest method generalDefaultSharedPreferencesTest.

@Test
public void generalDefaultSharedPreferencesTest() {
    GeneralSharedPreferences.getInstance().loadDefaultPreferences();
    HashMap<String, Object> defaultValues = PreferenceKeys.GENERAL_KEYS;
    GeneralSharedPreferences generalSharedPreferences = GeneralSharedPreferences.getInstance();
    for (String key : SharedPreferencesUtils.getAllGeneralKeys()) {
        assertEquals(generalSharedPreferences.get(key), defaultValues.get(key));
    }
}
Also used : GeneralSharedPreferences(org.odk.collect.android.preferences.GeneralSharedPreferences) Test(org.junit.Test)

Aggregations

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