Search in sources :

Example 1 with ExternalSelectChoice

use of org.odk.collect.android.externaldata.ExternalSelectChoice in project collect by opendatakit.

the class ExternalDataHandlerSearch method createDynamicSelectChoices.

protected ArrayList<SelectChoice> createDynamicSelectChoices(Cursor c, LinkedHashMap<String, String> selectColumnMap, String safeImageColumn) {
    List<String> columnsToExcludeFromLabels = new ArrayList<>();
    if (safeImageColumn != null) {
        columnsToExcludeFromLabels.add(safeImageColumn);
    }
    ArrayList<SelectChoice> selectChoices = new ArrayList<>();
    if (c.getCount() > 0) {
        c.moveToPosition(-1);
        int index = 0;
        Set<String> uniqueValues = new HashSet<>();
        while (c.moveToNext()) {
            // the value is always the first column
            String value = c.getString(0);
            if (!uniqueValues.contains(value)) {
                String label = buildLabel(c, selectColumnMap, columnsToExcludeFromLabels);
                ExternalSelectChoice selectChoice;
                if (StringUtils.isBlank(label)) {
                    selectChoice = new ExternalSelectChoice(value, value, false);
                } else {
                    selectChoice = new ExternalSelectChoice(label, value, false);
                }
                selectChoice.setIndex(index);
                if (safeImageColumn != null && safeImageColumn.trim().length() > 0) {
                    String image = c.getString(c.getColumnIndex(safeImageColumn));
                    if (image != null && image.trim().length() > 0) {
                        selectChoice.setImage(ExternalDataUtil.JR_IMAGES_PREFIX + image);
                    }
                }
                selectChoices.add(selectChoice);
                index++;
                uniqueValues.add(value);
            }
        }
    }
    return selectChoices;
}
Also used : SelectChoice(org.javarosa.core.model.SelectChoice) ExternalSelectChoice(org.odk.collect.android.externaldata.ExternalSelectChoice) ArrayList(java.util.ArrayList) ExternalSelectChoice(org.odk.collect.android.externaldata.ExternalSelectChoice) LocalizedApplicationKt.getLocalizedString(org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString) HashSet(java.util.HashSet)

Example 2 with ExternalSelectChoice

use of org.odk.collect.android.externaldata.ExternalSelectChoice in project collect by opendatakit.

the class LabelWidget method addItems.

private void addItems(Context context, QuestionDetails questionDetails) {
    // Layout holds the horizontal list of buttons
    LinearLayout listItems = findViewById(R.id.answer_container);
    if (items != null) {
        for (int i = 0; i < items.size(); i++) {
            String imageURI;
            if (items.get(i) instanceof ExternalSelectChoice) {
                imageURI = ((ExternalSelectChoice) items.get(i)).getImage();
            } else {
                imageURI = questionDetails.getPrompt().getSpecialFormSelectChoiceText(items.get(i), FormEntryCaption.TEXT_FORM_IMAGE);
            }
            // build image view (if an image is provided)
            ImageView imageView = null;
            TextView missingImage = null;
            final int labelId = View.generateViewId();
            // Now set up the image view
            String errorMsg = null;
            if (imageURI != null) {
                try {
                    String imageFilename = ReferenceManager.instance().deriveReference(imageURI).getLocalURI();
                    final File imageFile = new File(imageFilename);
                    if (imageFile.exists()) {
                        Bitmap b = null;
                        try {
                            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                            int screenWidth = metrics.widthPixels;
                            int screenHeight = metrics.heightPixels;
                            b = ImageFileUtils.getBitmapScaledToDisplay(imageFile, screenHeight, screenWidth);
                        } catch (OutOfMemoryError e) {
                            Timber.e(e);
                            errorMsg = "ERROR: " + e.getMessage();
                        }
                        if (b != null) {
                            imageView = new ImageView(getContext());
                            imageView.setPadding(2, 2, 2, 2);
                            imageView.setAdjustViewBounds(true);
                            imageView.setImageBitmap(b);
                            imageView.setId(labelId);
                        } else if (errorMsg == null) {
                            // An error hasn't been logged and loading the image failed, so it's
                            // likely
                            // a bad file.
                            errorMsg = getContext().getString(R.string.file_invalid, imageFile);
                        }
                    } else {
                        // An error hasn't been logged. We should have an image, but the file
                        // doesn't
                        // exist.
                        errorMsg = getContext().getString(R.string.file_missing, imageFile);
                    }
                    if (errorMsg != null) {
                        // errorMsg is only set when an error has occured
                        Timber.e(errorMsg);
                        missingImage = new TextView(getContext());
                        missingImage.setText(errorMsg);
                        missingImage.setPadding(2, 2, 2, 2);
                        missingImage.setId(labelId);
                    }
                } catch (InvalidReferenceException e) {
                    Timber.e(e, "Invalid image reference");
                }
            }
            // build text label. Don't assign the text to the built in label to he
            // button because it aligns horizontally, and we want the label on top
            TextView label = new TextView(getContext());
            label.setText(questionDetails.getPrompt().getSelectChoiceText(items.get(i)));
            label.setTextSize(TypedValue.COMPLEX_UNIT_DIP, getAnswerFontSize());
            label.setGravity(Gravity.CENTER_HORIZONTAL);
            // answer layout holds the label text/image on top and the radio button on bottom
            LinearLayout answer = new LinearLayout(getContext());
            answer.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams headerParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            headerParams.gravity = Gravity.CENTER_HORIZONTAL;
            LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            buttonParams.gravity = Gravity.CENTER_HORIZONTAL;
            if (imageView != null) {
                imageView.setScaleType(ScaleType.CENTER);
                answer.addView(imageView, headerParams);
            } else if (missingImage != null) {
                answer.addView(missingImage, headerParams);
            } else {
                label.setId(labelId);
                answer.addView(label, headerParams);
            }
            answer.setPadding(4, 0, 4, 0);
            // Each button gets equal weight
            LinearLayout.LayoutParams answerParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            answerParams.weight = 1;
            listItems.addView(answer, answerParams);
        }
    }
}
Also used : ExternalSelectChoice(org.odk.collect.android.externaldata.ExternalSelectChoice) DisplayMetrics(android.util.DisplayMetrics) SuppressLint(android.annotation.SuppressLint) InvalidReferenceException(org.javarosa.core.reference.InvalidReferenceException) Bitmap(android.graphics.Bitmap) TextView(android.widget.TextView) ImageView(android.widget.ImageView) File(java.io.File) LinearLayout(android.widget.LinearLayout)

Example 3 with ExternalSelectChoice

use of org.odk.collect.android.externaldata.ExternalSelectChoice in project collect by opendatakit.

the class LikertWidget method getImageAsImageView.

public ImageView getImageAsImageView(int index) {
    ImageView view = getImageView();
    String imageURI;
    if (items.get(index) instanceof ExternalSelectChoice) {
        imageURI = ((ExternalSelectChoice) items.get(index)).getImage();
    } else {
        imageURI = getFormEntryPrompt().getSpecialFormSelectChoiceText(items.get(index), FormEntryCaption.TEXT_FORM_IMAGE);
    }
    if (imageURI != null) {
        String error = setImageFromOtherSource(imageURI, view);
        if (error != null) {
            return null;
        }
        return view;
    } else {
        return null;
    }
}
Also used : ExternalSelectChoice(org.odk.collect.android.externaldata.ExternalSelectChoice) ImageView(android.widget.ImageView)

Aggregations

ExternalSelectChoice (org.odk.collect.android.externaldata.ExternalSelectChoice)3 ImageView (android.widget.ImageView)2 SuppressLint (android.annotation.SuppressLint)1 Bitmap (android.graphics.Bitmap)1 DisplayMetrics (android.util.DisplayMetrics)1 LinearLayout (android.widget.LinearLayout)1 TextView (android.widget.TextView)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 SelectChoice (org.javarosa.core.model.SelectChoice)1 InvalidReferenceException (org.javarosa.core.reference.InvalidReferenceException)1 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)1