Search in sources :

Example 1 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class DownloadFormListTaskTest method shouldProcessAndReturnAFormList.

@Test
public void shouldProcessAndReturnAFormList() throws Exception {
    // given
    willRespondWith(RESPONSE);
    // when
    final Map<String, FormDetails> fetched = new DownloadFormListTask().doInBackground();
    // then
    RecordedRequest r = nextRequest();
    assertEquals("GET", r.getMethod());
    assertEquals("/formList", r.getPath());
    assertMatches("Dalvik/.* org.odk.collect.android/.*", r.getHeader("User-Agent"));
    assertEquals("1.0", r.getHeader("X-OpenRosa-Version"));
    assertEquals("gzip", r.getHeader("Accept-Encoding"));
    // and
    assertEquals(2, fetched.size());
    // and
    FormDetails f1 = fetched.get("one");
    assertNull(f1.getErrorStr());
    assertEquals("The First Form", f1.getFormName());
    assertEquals("https://example.com/formXml?formId=one", f1.getDownloadUrl());
    assertNull(f1.getManifestUrl());
    assertEquals("one", f1.getFormID());
    assertNull(f1.getFormVersion());
    assertFalse(f1.isNewerFormVersionAvailable());
    assertFalse(f1.areNewerMediaFilesAvailable());
    // and
    FormDetails f2 = fetched.get("two");
    assertNull(f2.getErrorStr());
    assertEquals("The Second Form", f2.getFormName());
    assertEquals("https://example.com/formXml?formId=two", f2.getDownloadUrl());
    assertNull(f2.getManifestUrl());
    assertEquals("two", f2.getFormID());
    assertNull(f2.getFormVersion());
    assertFalse(f1.isNewerFormVersionAvailable());
    assertFalse(f1.areNewerMediaFilesAvailable());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) FormDetails(org.odk.collect.android.logic.FormDetails) MockedServerTest(org.odk.collect.android.test.MockedServerTest) Test(org.junit.Test)

Example 2 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class FormDownloadList method onItemClick.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    toggleButtonLabel(toggleButton, listView);
    downloadButton.setEnabled(listView.getCheckedItemCount() > 0);
    Object o = listView.getAdapter().getItem(position);
    @SuppressWarnings("unchecked") HashMap<String, String> item = (HashMap<String, String>) o;
    FormDetails detail = formNamesAndURLs.get(item.get(FORMDETAIL_KEY));
    if (detail != null) {
        Collect.getInstance().getActivityLogger().logAction(this, "onListItemClick", detail.getDownloadUrl());
    } else {
        Collect.getInstance().getActivityLogger().logAction(this, "onListItemClick", "<missing form detail>");
    }
    if (listView.isItemChecked(position)) {
        selectedForms.add(((HashMap<String, String>) listView.getAdapter().getItem(position)).get(FORMDETAIL_KEY));
    } else {
        selectedForms.remove(((HashMap<String, String>) listView.getAdapter().getItem(position)).get(FORMDETAIL_KEY));
    }
}
Also used : FormDetails(org.odk.collect.android.logic.FormDetails) HashMap(java.util.HashMap)

Example 3 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class FormDownloadList method downloadSelectedFiles.

/**
 * starts the task to download the selected forms, also shows progress dialog
 */
@SuppressWarnings("unchecked")
private void downloadSelectedFiles() {
    int totalCount = 0;
    ArrayList<FormDetails> filesToDownload = new ArrayList<FormDetails>();
    SparseBooleanArray sba = listView.getCheckedItemPositions();
    for (int i = 0; i < listView.getCount(); i++) {
        if (sba.get(i, false)) {
            HashMap<String, String> item = (HashMap<String, String>) listView.getAdapter().getItem(i);
            filesToDownload.add(formNamesAndURLs.get(item.get(FORMDETAIL_KEY)));
        }
    }
    totalCount = filesToDownload.size();
    Collect.getInstance().getActivityLogger().logAction(this, "downloadSelectedFiles", Integer.toString(totalCount));
    if (totalCount > 0) {
        // show dialog box
        showDialog(PROGRESS_DIALOG);
        downloadFormsTask = new DownloadFormsTask();
        downloadFormsTask.setDownloaderListener(this);
        downloadFormsTask.execute(filesToDownload);
    } else {
        ToastUtils.showShortToast(R.string.noselect_error);
    }
}
Also used : FormDetails(org.odk.collect.android.logic.FormDetails) HashMap(java.util.HashMap) DownloadFormsTask(org.odk.collect.android.tasks.DownloadFormsTask) ArrayList(java.util.ArrayList) SparseBooleanArray(android.util.SparseBooleanArray)

Example 4 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class FormDownloadList method downloadFormList.

/**
 * Starts the download task and shows the progress dialog.
 */
private void downloadFormList() {
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = connectivityManager.getActiveNetworkInfo();
    if (ni == null || !ni.isConnected()) {
        ToastUtils.showShortToast(R.string.no_connection);
    } else {
        formNamesAndURLs = new HashMap<String, FormDetails>();
        if (progressDialog != null) {
            // This is needed because onPrepareDialog() is broken in 1.6.
            progressDialog.setMessage(getString(R.string.please_wait));
        }
        showDialog(PROGRESS_DIALOG);
        if (downloadFormListTask != null && downloadFormListTask.getStatus() != AsyncTask.Status.FINISHED) {
            // we are already doing the download!!!
            return;
        } else if (downloadFormListTask != null) {
            downloadFormListTask.setDownloaderListener(null);
            downloadFormListTask.cancel(true);
            downloadFormListTask = null;
        }
        downloadFormListTask = new DownloadFormListTask();
        downloadFormListTask.setDownloaderListener(this);
        downloadFormListTask.execute();
    }
}
Also used : FormDetails(org.odk.collect.android.logic.FormDetails) NetworkInfo(android.net.NetworkInfo) ConnectivityManager(android.net.ConnectivityManager) DownloadFormListTask(org.odk.collect.android.tasks.DownloadFormListTask)

Example 5 with FormDetails

use of org.odk.collect.android.logic.FormDetails in project collect by opendatakit.

the class FormDownloadList method onCreate.

@SuppressWarnings("unchecked")
@Override
public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.remote_file_manage_list);
    super.onCreate(savedInstanceState);
    setTitle(getString(R.string.get_forms));
    alertMsg = getString(R.string.please_wait);
    downloadButton = findViewById(R.id.add_button);
    downloadButton.setEnabled(listView.getCheckedItemCount() > 0);
    downloadButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // this is called in downloadSelectedFiles():
            // Collect.getInstance().getActivityLogger().logAction(this,
            // "downloadSelectedFiles", ...);
            downloadSelectedFiles();
        }
    });
    toggleButton = findViewById(R.id.toggle_button);
    toggleButton.setEnabled(listView.getCheckedItemCount() > 0);
    toggleButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            downloadButton.setEnabled(toggleChecked(listView));
            toggleButtonLabel(toggleButton, listView);
            selectedForms.clear();
            if (listView.getCheckedItemCount() == listView.getCount()) {
                for (HashMap<String, String> map : formList) {
                    selectedForms.add(map.get(FORMDETAIL_KEY));
                }
            }
        }
    });
    Button refreshButton = findViewById(R.id.refresh_button);
    refreshButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Collect.getInstance().getActivityLogger().logAction(this, "refreshForms", "");
            formList.clear();
            updateAdapter();
            clearChoices();
            downloadFormList();
        }
    });
    if (savedInstanceState != null) {
        // If the screen has rotated, the hashmap with the form ids and urls is passed here.
        if (savedInstanceState.containsKey(BUNDLE_FORM_MAP)) {
            formNamesAndURLs = (HashMap<String, FormDetails>) savedInstanceState.getSerializable(BUNDLE_FORM_MAP);
        }
        // Android should keep track of this, but broken on rotate...
        if (savedInstanceState.containsKey(BUNDLE_SELECTED_COUNT)) {
            downloadButton.setEnabled(savedInstanceState.getInt(BUNDLE_SELECTED_COUNT) > 0);
        }
        // to restore alert dialog.
        if (savedInstanceState.containsKey(DIALOG_TITLE)) {
            alertTitle = savedInstanceState.getString(DIALOG_TITLE);
        }
        if (savedInstanceState.containsKey(DIALOG_MSG)) {
            alertMsg = savedInstanceState.getString(DIALOG_MSG);
        }
        if (savedInstanceState.containsKey(DIALOG_SHOWING)) {
            alertShowing = savedInstanceState.getBoolean(DIALOG_SHOWING);
        }
        if (savedInstanceState.containsKey(SHOULD_EXIT)) {
            shouldExit = savedInstanceState.getBoolean(SHOULD_EXIT);
        }
        if (savedInstanceState.containsKey(SELECTED_FORMS)) {
            selectedForms = (LinkedHashSet<String>) savedInstanceState.getSerializable(SELECTED_FORMS);
        }
    }
    if (savedInstanceState != null && savedInstanceState.containsKey(FORMLIST)) {
        formList = (ArrayList<HashMap<String, String>>) savedInstanceState.getSerializable(FORMLIST);
    } else {
        formList = new ArrayList<HashMap<String, String>>();
    }
    filteredFormList.addAll(formList);
    if (getLastNonConfigurationInstance() instanceof DownloadFormListTask) {
        downloadFormListTask = (DownloadFormListTask) getLastNonConfigurationInstance();
        if (downloadFormListTask.getStatus() == AsyncTask.Status.FINISHED) {
            try {
                dismissDialog(PROGRESS_DIALOG);
            } catch (IllegalArgumentException e) {
                Timber.i("Attempting to close a dialog that was not previously opened");
            }
            downloadFormsTask = null;
        }
    } else if (getLastNonConfigurationInstance() instanceof DownloadFormsTask) {
        downloadFormsTask = (DownloadFormsTask) getLastNonConfigurationInstance();
        if (downloadFormsTask.getStatus() == AsyncTask.Status.FINISHED) {
            try {
                dismissDialog(PROGRESS_DIALOG);
            } catch (IllegalArgumentException e) {
                Timber.i("Attempting to close a dialog that was not previously opened");
            }
            downloadFormsTask = null;
        }
    } else if (formNamesAndURLs.isEmpty() && getLastNonConfigurationInstance() == null) {
        // first time, so get the formlist
        downloadFormList();
    }
    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) };
}
Also used : FormDetails(org.odk.collect.android.logic.FormDetails) HashMap(java.util.HashMap) Button(android.widget.Button) DownloadFormsTask(org.odk.collect.android.tasks.DownloadFormsTask) OnClickListener(android.view.View.OnClickListener) DownloadFormListTask(org.odk.collect.android.tasks.DownloadFormListTask) View(android.view.View) AdapterView(android.widget.AdapterView) ListView(android.widget.ListView)

Aggregations

FormDetails (org.odk.collect.android.logic.FormDetails)8 HashMap (java.util.HashMap)5 DownloadFormListTask (org.odk.collect.android.tasks.DownloadFormListTask)2 DownloadFormsTask (org.odk.collect.android.tasks.DownloadFormsTask)2 SharedPreferences (android.content.SharedPreferences)1 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)1 SparseBooleanArray (android.util.SparseBooleanArray)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 Button (android.widget.Button)1 ListView (android.widget.ListView)1 ArrayList (java.util.ArrayList)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 Test (org.junit.Test)1 Element (org.kxml2.kdom.Element)1 FormsDao (org.odk.collect.android.dao.FormsDao)1 MediaFile (org.odk.collect.android.logic.MediaFile)1 MockedServerTest (org.odk.collect.android.test.MockedServerTest)1