Search in sources :

Example 1 with ExternalDataManager

use of org.odk.collect.android.external.ExternalDataManager in project collect by opendatakit.

the class FormEntryActivity method createQuitDialog.

/**
 * Create a dialog with options to save and exit or quit without
 * saving
 */
private void createQuitDialog() {
    String title;
    {
        FormController formController = getFormController();
        title = (formController == null) ? null : formController.getFormTitle();
        if (title == null) {
            title = getString(R.string.no_form_loaded);
        }
    }
    List<IconMenuItem> items;
    if ((boolean) AdminSharedPreferences.getInstance().get(AdminKeys.KEY_SAVE_MID)) {
        items = ImmutableList.of(new IconMenuItem(R.drawable.ic_save_grey_32dp_wrapped, R.string.keep_changes), new IconMenuItem(R.drawable.ic_delete_grey_32dp_wrapped, R.string.do_not_save));
    } else {
        items = ImmutableList.of(new IconMenuItem(R.drawable.ic_delete_grey_32dp_wrapped, R.string.do_not_save));
    }
    Collect.getInstance().getActivityLogger().logInstanceAction(this, "createQuitDialog", "show");
    ListView listView = DialogUtils.createActionListView(this);
    final IconMenuListAdapter adapter = new IconMenuListAdapter(this, items);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            IconMenuItem item = (IconMenuItem) adapter.getItem(position);
            if (item.getTextResId() == R.string.keep_changes) {
                Collect.getInstance().getActivityLogger().logInstanceAction(this, "createQuitDialog", "saveAndExit");
                saveDataToDisk(EXIT, InstancesDaoHelper.isInstanceComplete(false), null);
            } else {
                Collect.getInstance().getActivityLogger().logInstanceAction(this, "createQuitDialog", "discardAndExit");
                // close all open databases of external data.
                ExternalDataManager manager = Collect.getInstance().getExternalDataManager();
                if (manager != null) {
                    manager.close();
                }
                FormController formController = getFormController();
                if (formController != null) {
                    formController.getTimerLogger().logTimerEvent(TimerLogger.EventTypes.FORM_EXIT, 0, null, false, true);
                }
                removeTempInstance();
                finishReturnInstance();
            }
            alertDialog.dismiss();
        }
    });
    alertDialog = new AlertDialog.Builder(this).setTitle(getString(R.string.quit_application, title)).setPositiveButton(getString(R.string.do_not_exit), new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int id) {
            Collect.getInstance().getActivityLogger().logInstanceAction(this, "createQuitDialog", "cancel");
            dialog.cancel();
        }
    }).setView(listView).create();
    alertDialog.show();
}
Also used : FormController(org.odk.collect.android.logic.FormController) AlertDialog(android.support.v7.app.AlertDialog) DialogInterface(android.content.DialogInterface) ODKView(org.odk.collect.android.views.ODKView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ExternalDataManager(org.odk.collect.android.external.ExternalDataManager) FailedConstraint(org.odk.collect.android.logic.FormController.FailedConstraint) ListView(android.widget.ListView) IconMenuListAdapter(org.odk.collect.android.adapters.IconMenuListAdapter) OnClickListener(android.view.View.OnClickListener) IconMenuItem(org.odk.collect.android.adapters.model.IconMenuItem) AdapterView(android.widget.AdapterView)

Aggregations

DialogInterface (android.content.DialogInterface)1 AlertDialog (android.support.v7.app.AlertDialog)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 AdapterView (android.widget.AdapterView)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1 IconMenuListAdapter (org.odk.collect.android.adapters.IconMenuListAdapter)1 IconMenuItem (org.odk.collect.android.adapters.model.IconMenuItem)1 ExternalDataManager (org.odk.collect.android.external.ExternalDataManager)1 FormController (org.odk.collect.android.logic.FormController)1 FailedConstraint (org.odk.collect.android.logic.FormController.FailedConstraint)1 ODKView (org.odk.collect.android.views.ODKView)1