Search in sources :

Example 1 with InvalidReferenceException

use of org.javarosa.core.reference.InvalidReferenceException in project collect by opendatakit.

the class MediaLayout method setAVT.

public void setAVT(FormIndex index, String selectionDesignator, TextView text, String audioURI, String imageURI, String videoURI, final String bigImageURI) {
    this.selectionDesignator = selectionDesignator;
    this.index = index;
    viewText = text;
    originalText = text.getText();
    viewText.setId(ViewIds.generateViewId());
    this.videoURI = videoURI;
    // Layout configurations for our elements in the relative layout
    RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams audioParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams imageParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams videoParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    // First set up the audio button
    if (audioURI != null) {
        // An audio file is specified
        audioButton = new AudioButton(getContext(), this.index, this.selectionDesignator, audioURI, player);
        audioButton.setPadding(22, 12, 22, 12);
        audioButton.setBackgroundColor(Color.LTGRAY);
        audioButton.setOnClickListener(this);
        // random ID to be used by the
        audioButton.setId(ViewIds.generateViewId());
    // relative layout.
    } else {
    // No audio file specified, so ignore.
    }
    // Then set up the video button
    if (videoURI != null) {
        // An video file is specified
        videoButton = new AppCompatImageButton(getContext());
        Bitmap b = BitmapFactory.decodeResource(getContext().getResources(), android.R.drawable.ic_media_play);
        videoButton.setImageBitmap(b);
        videoButton.setPadding(22, 12, 22, 12);
        videoButton.setBackgroundColor(Color.LTGRAY);
        videoButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Collect.getInstance().getActivityLogger().logInstanceAction(this, "onClick", "playVideoPrompt" + MediaLayout.this.selectionDesignator, MediaLayout.this.index);
                MediaLayout.this.playVideo();
            }
        });
        videoButton.setId(ViewIds.generateViewId());
    } else {
    // No video file specified, so ignore.
    }
    // Now set up the image view
    String errorMsg = null;
    final int imageId = ViewIds.generateViewId();
    if (imageURI != null) {
        try {
            String imageFilename = ReferenceManager.instance().DeriveReference(imageURI).getLocalURI();
            final File imageFile = new File(imageFilename);
            if (imageFile.exists()) {
                DisplayMetrics metrics = context.getResources().getDisplayMetrics();
                int screenWidth = metrics.widthPixels;
                int screenHeight = metrics.heightPixels;
                Bitmap b = FileUtils.getBitmapScaledToDisplay(imageFile, screenHeight, screenWidth);
                if (b != null) {
                    imageView = new ImageView(getContext());
                    imageView.setPadding(2, 2, 2, 2);
                    imageView.setImageBitmap(b);
                    imageView.setId(imageId);
                    imageView.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if (bigImageURI != null) {
                                Collect.getInstance().getActivityLogger().logInstanceAction(this, "onClick", "showImagePromptBigImage" + MediaLayout.this.selectionDesignator, MediaLayout.this.index);
                                try {
                                    File bigImage = new File(ReferenceManager.instance().DeriveReference(bigImageURI).getLocalURI());
                                    Intent i = new Intent("android.intent.action.VIEW");
                                    i.setDataAndType(Uri.fromFile(bigImage), "image/*");
                                    getContext().startActivity(i);
                                } catch (InvalidReferenceException e) {
                                    Timber.e(e, "Invalid image reference due to %s ", e.getMessage());
                                } catch (ActivityNotFoundException e) {
                                    Timber.d(e, "No Activity found to handle due to %s", e.getMessage());
                                    ToastUtils.showShortToast(getContext().getString(R.string.activity_not_found, getContext().getString(R.string.view_image)));
                                }
                            } else {
                                if (viewText instanceof RadioButton) {
                                    ((RadioButton) viewText).setChecked(true);
                                } else if (viewText instanceof CheckBox) {
                                    CheckBox checkbox = (CheckBox) viewText;
                                    checkbox.setChecked(!checkbox.isChecked());
                                }
                            }
                        }
                    });
                } else {
                    // Loading the image failed, so it's likely a bad file.
                    errorMsg = getContext().getString(R.string.file_invalid, imageFile);
                }
            } else {
                // 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 occurred
                Timber.e(errorMsg);
                missingImage = new TextView(getContext());
                missingImage.setText(errorMsg);
                missingImage.setPadding(10, 10, 10, 10);
                missingImage.setId(imageId);
            }
        } catch (InvalidReferenceException e) {
            Timber.e(e, "Invalid image reference due to %s ", e.getMessage());
        }
    } else {
    // There's no imageURI listed, so just ignore it.
    }
    // e.g., for TextView that flag will be true
    boolean isNotAMultipleChoiceField = !RadioButton.class.isAssignableFrom(text.getClass()) && !CheckBox.class.isAssignableFrom(text.getClass());
    // Assumes LTR, TTB reading bias!
    if (viewText.getText().length() == 0 && (imageView != null || missingImage != null)) {
        // it will show a grey bar to the right of the image icon.
        if (imageView != null) {
            imageView.setScaleType(ScaleType.FIT_START);
        }
        // 
        // In this case, we have:
        // Text upper left; image upper, left edge aligned with text right edge;
        // audio upper right; video below audio on right.
        textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        if (isNotAMultipleChoiceField) {
            imageParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        } else {
            imageParams.addRule(RelativeLayout.RIGHT_OF, viewText.getId());
        }
        imageParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        if (audioButton != null && videoButton == null) {
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            audioParams.setMargins(0, 0, 11, 0);
            imageParams.addRule(RelativeLayout.LEFT_OF, audioButton.getId());
        } else if (audioButton == null && videoButton != null) {
            videoParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            videoParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            videoParams.setMargins(0, 0, 11, 0);
            imageParams.addRule(RelativeLayout.LEFT_OF, videoButton.getId());
        } else if (audioButton != null && videoButton != null) {
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            audioParams.setMargins(0, 0, 11, 0);
            imageParams.addRule(RelativeLayout.LEFT_OF, audioButton.getId());
            videoParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            videoParams.addRule(RelativeLayout.BELOW, audioButton.getId());
            videoParams.setMargins(0, 20, 11, 0);
            imageParams.addRule(RelativeLayout.LEFT_OF, videoButton.getId());
        } else {
            // no need to bound it by the width of the parent...
            if (!isNotAMultipleChoiceField) {
                imageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            }
        }
        imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    } else {
        // In this case, we want the image to be centered...
        if (imageView != null) {
            imageView.setScaleType(ScaleType.FIT_START);
        }
        // 
        // Text upper left; audio upper right; video below audio on right.
        // image below text, audio and video buttons; left-aligned with text.
        textParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        textParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        if (audioButton != null && videoButton == null) {
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            audioParams.setMargins(0, 0, 11, 0);
            textParams.addRule(RelativeLayout.LEFT_OF, audioButton.getId());
        } else if (audioButton == null && videoButton != null) {
            videoParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            videoParams.setMargins(0, 0, 11, 0);
            textParams.addRule(RelativeLayout.LEFT_OF, videoButton.getId());
        } else if (audioButton != null && videoButton != null) {
            audioParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            audioParams.setMargins(0, 0, 11, 0);
            textParams.addRule(RelativeLayout.LEFT_OF, audioButton.getId());
            videoParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            videoParams.setMargins(0, 20, 11, 0);
            videoParams.addRule(RelativeLayout.BELOW, audioButton.getId());
        } else {
            textParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        }
        if (imageView != null || missingImage != null) {
            imageParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            imageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            if (videoButton != null) {
                imageParams.addRule(RelativeLayout.LEFT_OF, videoButton.getId());
            } else if (audioButton != null) {
                imageParams.addRule(RelativeLayout.LEFT_OF, audioButton.getId());
            }
            imageParams.addRule(RelativeLayout.BELOW, viewText.getId());
        } else {
            textParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        }
    }
    addView(viewText, textParams);
    if (audioButton != null) {
        addView(audioButton, audioParams);
    }
    if (videoButton != null) {
        addView(videoButton, videoParams);
    }
    if (imageView != null) {
        addView(imageView, imageParams);
    } else if (missingImage != null) {
        addView(missingImage, imageParams);
    }
}
Also used : Intent(android.content.Intent) AppCompatImageButton(android.support.v7.widget.AppCompatImageButton) RadioButton(android.widget.RadioButton) ImageView(android.widget.ImageView) View(android.view.View) TextView(android.widget.TextView) DisplayMetrics(android.util.DisplayMetrics) InvalidReferenceException(org.javarosa.core.reference.InvalidReferenceException) Bitmap(android.graphics.Bitmap) ActivityNotFoundException(android.content.ActivityNotFoundException) CheckBox(android.widget.CheckBox) RelativeLayout(android.widget.RelativeLayout) OnClickListener(android.view.View.OnClickListener) TextView(android.widget.TextView) ImageView(android.widget.ImageView) File(java.io.File)

Example 2 with InvalidReferenceException

use of org.javarosa.core.reference.InvalidReferenceException in project javarosa by opendatakit.

the class QuestionDefTest method testReferences.

@Test
public void testReferences() {
    QuestionDef q = fpi.getFirstQuestionDef();
    FormEntryPrompt fep = fpi.getFormEntryModel().getQuestionPrompt();
    Localizer l = fpi.getFormDef().getLocalizer();
    l.setDefaultLocale(l.getAvailableLocales()[0]);
    l.setLocale(l.getAvailableLocales()[0]);
    String audioURI = fep.getAudioText();
    String ref;
    ReferenceManager.instance().addReferenceFactory(new ResourceReferenceFactory());
    ReferenceManager.instance().addRootTranslator(new RootTranslator("jr://audio/", "jr://resource/"));
    try {
        Reference r = ReferenceManager.instance().DeriveReference(audioURI);
        ref = r.getURI();
        if (!ref.equals("jr://resource/hah.mp3")) {
            fail("Root translation failed.");
        }
    } catch (InvalidReferenceException ire) {
        fail("There was an Invalid Reference Exception:" + ire.getMessage());
        ire.printStackTrace();
    }
    ReferenceManager.instance().addRootTranslator(new RootTranslator("jr://images/", "jr://resource/"));
    q = fpi.getNextQuestion();
    fep = fpi.getFormEntryModel().getQuestionPrompt();
    String imURI = fep.getImageText();
    try {
        Reference r = ReferenceManager.instance().DeriveReference(imURI);
        ref = r.getURI();
        if (!ref.equals("jr://resource/four.gif")) {
            fail("Root translation failed.");
        }
    } catch (InvalidReferenceException ire) {
        fail("There was an Invalid Reference Exception:" + ire.getMessage());
        ire.printStackTrace();
    }
}
Also used : ResourceReferenceFactory(org.javarosa.core.reference.ResourceReferenceFactory) FormEntryPrompt(org.javarosa.form.api.FormEntryPrompt) Reference(org.javarosa.core.reference.Reference) IDataReference(org.javarosa.core.model.IDataReference) QuestionDef(org.javarosa.core.model.QuestionDef) Localizer(org.javarosa.core.services.locale.Localizer) RootTranslator(org.javarosa.core.reference.RootTranslator) InvalidReferenceException(org.javarosa.core.reference.InvalidReferenceException) Test(org.junit.Test)

Example 3 with InvalidReferenceException

use of org.javarosa.core.reference.InvalidReferenceException in project collect by opendatakit.

the class QuestionWidget method setupQuestionLabel.

private void setupQuestionLabel() {
    audioVideoImageTextLabel.setTag(getClipID(formEntryPrompt));
    audioVideoImageTextLabel.setText(formEntryPrompt.getLongText(), formEntryPrompt.isRequired(), questionTextSizeHelper.getHeadline6());
    audioVideoImageTextLabel.setMediaUtils(mediaUtils);
    String imageURI = this instanceof SelectImageMapWidget ? null : formEntryPrompt.getImageText();
    String videoURI = formEntryPrompt.getSpecialFormQuestionText("video");
    String bigImageURI = formEntryPrompt.getSpecialFormQuestionText("big-image");
    String playableAudioURI = getPlayableAudioURI(formEntryPrompt, referenceManager);
    try {
        if (imageURI != null) {
            audioVideoImageTextLabel.setImage(new File(referenceManager.deriveReference(imageURI).getLocalURI()), imageLoader);
        }
        if (bigImageURI != null) {
            audioVideoImageTextLabel.setBigImage(new File(referenceManager.deriveReference(bigImageURI).getLocalURI()));
        }
        if (videoURI != null) {
            audioVideoImageTextLabel.setVideo(new File(referenceManager.deriveReference(videoURI).getLocalURI()));
        }
        if (playableAudioURI != null) {
            audioVideoImageTextLabel.setAudio(playableAudioURI, audioHelper);
        }
    } catch (InvalidReferenceException e) {
        Timber.d(e, "Invalid media reference due to %s ", e.getMessage());
    }
    audioVideoImageTextLabel.setPlayTextColor(getPlayColor(formEntryPrompt, themeUtils));
}
Also used : SelectImageMapWidget(org.odk.collect.android.widgets.items.SelectImageMapWidget) File(java.io.File) InvalidReferenceException(org.javarosa.core.reference.InvalidReferenceException)

Example 4 with InvalidReferenceException

use of org.javarosa.core.reference.InvalidReferenceException in project collect by opendatakit.

the class MediaLayout method playVideo.

public void playVideo() {
    if (videoURI != null) {
        String videoFilename = "";
        try {
            videoFilename = ReferenceManager.instance().DeriveReference(videoURI).getLocalURI();
        } catch (InvalidReferenceException e) {
            Timber.e(e, "Invalid reference exception due to %s ", e.getMessage());
        }
        File videoFile = new File(videoFilename);
        if (!videoFile.exists()) {
            // We should have a video clip, but the file doesn't exist.
            String errorMsg = getContext().getString(R.string.file_missing, videoFilename);
            Timber.d("File %s is missing", videoFilename);
            ToastUtils.showLongToast(errorMsg);
            return;
        }
        Intent i = new Intent("android.intent.action.VIEW");
        i.setDataAndType(Uri.fromFile(videoFile), "video/*");
        if (i.resolveActivity(getContext().getPackageManager()) != null) {
            getContext().startActivity(i);
        } else {
            ToastUtils.showShortToast(getContext().getString(R.string.activity_not_found, getContext().getString(R.string.view_video)));
        }
    }
}
Also used : Intent(android.content.Intent) File(java.io.File) InvalidReferenceException(org.javarosa.core.reference.InvalidReferenceException)

Example 5 with InvalidReferenceException

use of org.javarosa.core.reference.InvalidReferenceException 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)

Aggregations

InvalidReferenceException (org.javarosa.core.reference.InvalidReferenceException)6 File (java.io.File)5 Bitmap (android.graphics.Bitmap)3 DisplayMetrics (android.util.DisplayMetrics)3 SuppressLint (android.annotation.SuppressLint)2 Intent (android.content.Intent)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 ActivityNotFoundException (android.content.ActivityNotFoundException)1 AppCompatImageButton (android.support.v7.widget.AppCompatImageButton)1 View (android.view.View)1 OnClickListener (android.view.View.OnClickListener)1 CheckBox (android.widget.CheckBox)1 LinearLayout (android.widget.LinearLayout)1 RadioButton (android.widget.RadioButton)1 RelativeLayout (android.widget.RelativeLayout)1 IDataReference (org.javarosa.core.model.IDataReference)1 QuestionDef (org.javarosa.core.model.QuestionDef)1 Reference (org.javarosa.core.reference.Reference)1 ResourceReferenceFactory (org.javarosa.core.reference.ResourceReferenceFactory)1