@Test public void setData_whenPromptHasDifferentAudioFile_deletesOriginalAnswer() { FormEntryPrompt prompt = promptWithAnswer(new StringData(FILE_PATH)); when(prompt.getIndex()).thenReturn(formIndex); AudioWidget widget = createWidget(prompt); widget.setBinaryData(mockedFile); assertThat(questionMediaManager.originalFiles.get("questionIndex"), equalTo(widget.getInstanceFolder() + File.separator + "blah.mp3")); }
@Override public void setBinaryData(Object object) { File newAudio; if (object instanceof Uri) { String sourcePath = getSourcePathFromUri((Uri) object); String destinationPath = FileWidgetUtils.getDestinationPathFromSourcePath(sourcePath, getInstanceFolder(), fileUtil); File source = fileUtil.getFileAtPath(sourcePath); newAudio = fileUtil.getFileAtPath(destinationPath); fileUtil.copyFile(source, newAudio); } else if (object instanceof File) { newAudio = (File) object; } else { Timber.w("AudioWidget's setBinaryData must receive a File or Uri object."); return; } if (newAudio.exists()) { ContentValues values = new ContentValues(6); values.put(Audio.Media.TITLE, newAudio.getName()); values.put(Audio.Media.DISPLAY_NAME, newAudio.getName()); values.put(Audio.Media.DATE_ADDED, System.currentTimeMillis()); values.put(Audio.Media.DATA, newAudio.getAbsolutePath()); questionMediaManager.replaceRecentFileForQuestion(getFormEntryPrompt().getIndex().toString(), newAudio.getAbsolutePath()); Uri audioURI = getContext().getContentResolver().insert(Audio.Media.EXTERNAL_CONTENT_URI, values); if (audioURI != null) { Timber.i("Inserting AUDIO returned uri = %s", audioURI.toString()); } if (binaryName != null && !binaryName.equals(newAudio.getName())) { deleteFile(); } binaryName = newAudio.getName(); Timber.i("Setting current answer to %s", newAudio.getName()); widgetValueChanged(); updatePlayerMedia(); } else { Timber.e("Inserting Audio file FAILED"); } }
AudioWidget extends QuestionWidget implements FileWidget, BinaryDataReceiver { @Override public void setBinaryData(Object object) { File newAudio; if (object instanceof Uri) { String sourcePath = getSourcePathFromUri((Uri) object); String destinationPath = FileWidgetUtils.getDestinationPathFromSourcePath(sourcePath, getInstanceFolder(), fileUtil); File source = fileUtil.getFileAtPath(sourcePath); newAudio = fileUtil.getFileAtPath(destinationPath); fileUtil.copyFile(source, newAudio); } else if (object instanceof File) { newAudio = (File) object; } else { Timber.w("AudioWidget's setBinaryData must receive a File or Uri object."); return; } if (newAudio.exists()) { ContentValues values = new ContentValues(6); values.put(Audio.Media.TITLE, newAudio.getName()); values.put(Audio.Media.DISPLAY_NAME, newAudio.getName()); values.put(Audio.Media.DATE_ADDED, System.currentTimeMillis()); values.put(Audio.Media.DATA, newAudio.getAbsolutePath()); questionMediaManager.replaceRecentFileForQuestion(getFormEntryPrompt().getIndex().toString(), newAudio.getAbsolutePath()); Uri audioURI = getContext().getContentResolver().insert(Audio.Media.EXTERNAL_CONTENT_URI, values); if (audioURI != null) { Timber.i("Inserting AUDIO returned uri = %s", audioURI.toString()); } if (binaryName != null && !binaryName.equals(newAudio.getName())) { deleteFile(); } binaryName = newAudio.getName(); Timber.i("Setting current answer to %s", newAudio.getName()); widgetValueChanged(); updatePlayerMedia(); } else { Timber.e("Inserting Audio file FAILED"); } } }
AudioWidget extends QuestionWidget implements FileWidget, BinaryDataReceiver { @Override public void setBinaryData(Object object) { File newAudio; if (object instanceof Uri) { String sourcePath = getSourcePathFromUri((Uri) object); String destinationPath = FileWidgetUtils.getDestinationPathFromSourcePath(sourcePath, getInstanceFolder(), fileUtil); File source = fileUtil.getFileAtPath(sourcePath); newAudio = fileUtil.getFileAtPath(destinationPath); fileUtil.copyFile(source, newAudio); } else if (object instanceof File) { newAudio = (File) object; } else { Timber.w("AudioWidget's setBinaryData must receive a File or Uri object."); return; } if (newAudio.exists()) { ContentValues values = new ContentValues(6); values.put(Audio.Media.TITLE, newAudio.getName()); values.put(Audio.Media.DISPLAY_NAME, newAudio.getName()); values.put(Audio.Media.DATE_ADDED, System.currentTimeMillis()); values.put(Audio.Media.DATA, newAudio.getAbsolutePath()); questionMediaManager.replaceRecentFileForQuestion(getFormEntryPrompt().getIndex().toString(), newAudio.getAbsolutePath()); Uri audioURI = getContext().getContentResolver().insert(Audio.Media.EXTERNAL_CONTENT_URI, values); if (audioURI != null) { Timber.i("Inserting AUDIO returned uri = %s", audioURI.toString()); } if (binaryName != null && !binaryName.equals(newAudio.getName())) { deleteFile(); } binaryName = newAudio.getName(); Timber.i("Setting current answer to %s", newAudio.getName()); widgetValueChanged(); updatePlayerMedia(); } else { Timber.e("Inserting Audio file FAILED"); } } AudioWidget(Context context, QuestionDetails prompt, QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry);   AudioWidget(Context context, QuestionDetails questionDetails, @NonNull FileUtil fileUtil, @NonNull MediaUtil mediaUtil, @NonNull AudioControllerView audioController,
                QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry, AudioHelper audioHelper, ActivityAvailability activityAvailability); }
AudioWidget extends QuestionWidget implements FileWidget, BinaryDataReceiver { @Override public void setBinaryData(Object object) { File newAudio; if (object instanceof Uri) { String sourcePath = getSourcePathFromUri((Uri) object); String destinationPath = FileWidgetUtils.getDestinationPathFromSourcePath(sourcePath, getInstanceFolder(), fileUtil); File source = fileUtil.getFileAtPath(sourcePath); newAudio = fileUtil.getFileAtPath(destinationPath); fileUtil.copyFile(source, newAudio); } else if (object instanceof File) { newAudio = (File) object; } else { Timber.w("AudioWidget's setBinaryData must receive a File or Uri object."); return; } if (newAudio.exists()) { ContentValues values = new ContentValues(6); values.put(Audio.Media.TITLE, newAudio.getName()); values.put(Audio.Media.DISPLAY_NAME, newAudio.getName()); values.put(Audio.Media.DATE_ADDED, System.currentTimeMillis()); values.put(Audio.Media.DATA, newAudio.getAbsolutePath()); questionMediaManager.replaceRecentFileForQuestion(getFormEntryPrompt().getIndex().toString(), newAudio.getAbsolutePath()); Uri audioURI = getContext().getContentResolver().insert(Audio.Media.EXTERNAL_CONTENT_URI, values); if (audioURI != null) { Timber.i("Inserting AUDIO returned uri = %s", audioURI.toString()); } if (binaryName != null && !binaryName.equals(newAudio.getName())) { deleteFile(); } binaryName = newAudio.getName(); Timber.i("Setting current answer to %s", newAudio.getName()); widgetValueChanged(); updatePlayerMedia(); } else { Timber.e("Inserting Audio file FAILED"); } } AudioWidget(Context context, QuestionDetails prompt, QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry);   AudioWidget(Context context, QuestionDetails questionDetails, @NonNull FileUtil fileUtil, @NonNull MediaUtil mediaUtil, @NonNull AudioControllerView audioController,
                QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry, AudioHelper audioHelper, ActivityAvailability activityAvailability); @Override void deleteFile(); @Override void clearAnswer(); @Override IAnswerData getAnswer(); @Override void setBinaryData(Object object); @Override void setOnLongClickListener(OnLongClickListener l); @Override void cancelLongPress(); }
AudioWidget extends QuestionWidget implements FileWidget, BinaryDataReceiver { @Override public void setBinaryData(Object object) { File newAudio; if (object instanceof Uri) { String sourcePath = getSourcePathFromUri((Uri) object); String destinationPath = FileWidgetUtils.getDestinationPathFromSourcePath(sourcePath, getInstanceFolder(), fileUtil); File source = fileUtil.getFileAtPath(sourcePath); newAudio = fileUtil.getFileAtPath(destinationPath); fileUtil.copyFile(source, newAudio); } else if (object instanceof File) { newAudio = (File) object; } else { Timber.w("AudioWidget's setBinaryData must receive a File or Uri object."); return; } if (newAudio.exists()) { ContentValues values = new ContentValues(6); values.put(Audio.Media.TITLE, newAudio.getName()); values.put(Audio.Media.DISPLAY_NAME, newAudio.getName()); values.put(Audio.Media.DATE_ADDED, System.currentTimeMillis()); values.put(Audio.Media.DATA, newAudio.getAbsolutePath()); questionMediaManager.replaceRecentFileForQuestion(getFormEntryPrompt().getIndex().toString(), newAudio.getAbsolutePath()); Uri audioURI = getContext().getContentResolver().insert(Audio.Media.EXTERNAL_CONTENT_URI, values); if (audioURI != null) { Timber.i("Inserting AUDIO returned uri = %s", audioURI.toString()); } if (binaryName != null && !binaryName.equals(newAudio.getName())) { deleteFile(); } binaryName = newAudio.getName(); Timber.i("Setting current answer to %s", newAudio.getName()); widgetValueChanged(); updatePlayerMedia(); } else { Timber.e("Inserting Audio file FAILED"); } } AudioWidget(Context context, QuestionDetails prompt, QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry);   AudioWidget(Context context, QuestionDetails questionDetails, @NonNull FileUtil fileUtil, @NonNull MediaUtil mediaUtil, @NonNull AudioControllerView audioController,
                QuestionMediaManager questionMediaManager, WaitingForDataRegistry waitingForDataRegistry, AudioHelper audioHelper, ActivityAvailability activityAvailability); @Override void deleteFile(); @Override void clearAnswer(); @Override IAnswerData getAnswer(); @Override void setBinaryData(Object object); @Override void setOnLongClickListener(OnLongClickListener l); @Override void cancelLongPress();  }