Search in sources :

Example 1 with DownloadFormsTask

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

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

HashMap (java.util.HashMap)2 FormDetails (org.odk.collect.android.logic.FormDetails)2 DownloadFormsTask (org.odk.collect.android.tasks.DownloadFormsTask)2 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 DownloadFormListTask (org.odk.collect.android.tasks.DownloadFormListTask)1