Search in sources :

Example 1 with DownloadFormListTask

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

use of org.odk.collect.android.tasks.DownloadFormListTask 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)2 DownloadFormListTask (org.odk.collect.android.tasks.DownloadFormListTask)2 ConnectivityManager (android.net.ConnectivityManager)1 NetworkInfo (android.net.NetworkInfo)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 HashMap (java.util.HashMap)1 DownloadFormsTask (org.odk.collect.android.tasks.DownloadFormsTask)1