Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moves media player related functionalities to Media Widget class #2817

Merged
merged 16 commits into from
Feb 8, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,18 @@ public void playVideo() {
}
}

public void setAVT(TextView text, String audioURI, String imageURI, String videoURI,
String bigImageURI, MediaPlayer player) {
this.bigImageURI = bigImageURI;
this.player = player;
this.videoURI = videoURI;

public void setLabelTextView(TextView text) {
viewText = text;
originalText = text.getText();
viewText.setId(ViewIds.generateViewId());
flContainer.removeAllViews();
flContainer.addView(viewText);
}

public void setAVT(String audioURI, String imageURI, String videoURI, String bigImageURI, MediaPlayer player) {
this.bigImageURI = bigImageURI;
this.player = player;
this.videoURI = videoURI;

// Setup audio button
if (audioURI != null) {
Expand Down Expand Up @@ -237,9 +240,6 @@ public void setAVT(TextView text, String audioURI, String imageURI, String video
Timber.e(e, "Invalid image reference due to %s ", e.getMessage());
}
}

flContainer.removeAllViews();
flContainer.addView(viewText);
}

public TextView getView_Text() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*
* Copyright (C) 2011 University of Washington
*
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
Expand Down Expand Up @@ -55,6 +55,7 @@
import org.odk.collect.android.utilities.ThemeUtils;
import org.odk.collect.android.utilities.ToastUtils;
import org.odk.collect.android.utilities.ViewIds;
import org.odk.collect.android.widgets.MediaWidget;
import org.odk.collect.android.widgets.QuestionWidget;
import org.odk.collect.android.widgets.WidgetFactory;
import org.odk.collect.android.widgets.interfaces.BinaryWidget;
Expand Down Expand Up @@ -86,7 +87,7 @@ public class ODKView extends FrameLayout implements OnLongClickListener {
public static final String FIELD_LIST = "field-list";

public ODKView(Context context, final FormEntryPrompt[] questionPrompts,
FormEntryCaption[] groups, boolean advancingPage) {
FormEntryCaption[] groups, boolean advancingPage) {
super(context);

inflate(getContext(), R.layout.nested_scroll_view, this); // keep in an xml file to enable the vertical scrollbar
Expand Down Expand Up @@ -216,19 +217,13 @@ public void onClick(View v) {

// see if there is an autoplay option.
// Only execute it during forward swipes through the form
if (advancingPage && widgets.size() == 1) {
final String playOption = widgets.get(
0).getFormEntryPrompt().getFormElement().getAdditionalAttribute(null, "autoplay");
if (advancingPage && widgets.size() == 1 && widgets.get(0) instanceof MediaWidget) {
MediaWidget mediaWidget = ((MediaWidget) widgets.get(0));
final String playOption = mediaWidget.getFormEntryPrompt().getFormElement().getAdditionalAttribute(null, "autoplay");
if (playOption != null) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (playOption.equalsIgnoreCase("audio")) {
widgets.get(0).playAudio();
} else if (playOption.equalsIgnoreCase("video")) {
widgets.get(0).playVideo();
}
new Handler().postDelayed(() -> {
if (playOption.equalsIgnoreCase("audio") || playOption.equalsIgnoreCase("video")) {
mediaWidget.playAllPromptText(playOption);
}
}, 150);
}
Expand Down Expand Up @@ -299,7 +294,7 @@ public static String getGroupsPath(FormEntryCaption[] groups) {
/**
* Builds a string representing the 'path' of the list of groups.
* Each level is separated by `>`.
*
* <p>
* Some views (e.g. the repeat picker) may want to hide the multiplicity of the last item,
* i.e. show `Friends` instead of `Friends > 1`.
*/
Expand Down Expand Up @@ -350,7 +345,7 @@ public void setBinaryData(Object answer) {
} catch (Exception e) {
Timber.e(e);
ToastUtils.showLongToast(getContext().getString(R.string.error_attaching_binary_file,
e.getMessage()));
e.getMessage()));
}
set = true;
break;
Expand Down Expand Up @@ -415,12 +410,12 @@ public void cancelWaitingForBinaryData() {

if (count != 1) {
Timber.w("Attempting to cancel waiting for binary data to a widget or set of widgets "
+ "not looking for data");
+ "not looking for data");
}
}

public boolean suppressFlingGesture(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float velocityY) {
for (QuestionWidget q : widgets) {
if (q.suppressFlingGesture(e1, e2, velocityX, velocityY)) {
return true;
Expand Down Expand Up @@ -469,7 +464,9 @@ public void cancelLongPress() {
}

public void stopAudio() {
widgets.get(0).stopAudio();
if (widgets.get(0) instanceof MediaWidget) {
((MediaWidget) widgets.get(0)).stopAudio();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
*/

@SuppressLint("ViewConstructor")
public class AudioWidget extends QuestionWidget implements FileWidget {
public class AudioWidget extends MediaWidget implements FileWidget {

@NonNull
private FileUtil fileUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* @author Jeff Beorse (jeff@beorse.net)
*/
@SuppressLint("ViewConstructor")
public class GridMultiWidget extends QuestionWidget implements MultiChoiceWidget {
public class GridMultiWidget extends MediaWidget implements MultiChoiceWidget {

private final int bgOrange = getResources().getColor(R.color.highContrastHighlight);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
* @author Jeff Beorse (jeff@beorse.net)
*/
@SuppressLint("ViewConstructor")
public class GridWidget extends QuestionWidget implements MultiChoiceWidget {
public class GridWidget extends MediaWidget implements MultiChoiceWidget {

private final int bgOrange = getResources().getColor(R.color.highContrastHighlight);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.odk.collect.android.widgets;

import android.content.Context;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.support.annotation.CallSuper;

import org.javarosa.form.api.FormEntryPrompt;

import timber.log.Timber;

public abstract class MediaWidget extends QuestionWidget {

private int playColor;

public MediaWidget(Context context, FormEntryPrompt prompt) {
super(context, prompt);

String imageURI = this instanceof SelectImageMapWidget ? null : prompt.getImageText();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I think we’re using the convention that initialisms (like URI) are capitalized like uri or imageUri. I have an audio clip of Josh Bloch himself recommending this.

String audioURI = prompt.getAudioText();
String videoURI = prompt.getSpecialFormQuestionText("video");

// shown when image is clicked
String bigImageURI = prompt.getSpecialFormQuestionText("big-image");

getQuestionMediaLayout().setAVT(audioURI, imageURI, videoURI, bigImageURI, player);
getQuestionMediaLayout().setAudioListener(this);

initPlayColor();
}

private void initPlayColor() {
playColor = themeUtils.getAccentColor();

String playColorString = getFormEntryPrompt().getFormElement().getAdditionalAttribute(null, "playColor");
if (playColorString != null) {
try {
playColor = Color.parseColor(playColorString);
} catch (IllegalArgumentException e) {
Timber.e(e, "Argument %s is incorrect", playColorString);
Copy link
Contributor

@dcbriccetti dcbriccetti Feb 6, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argument %s to Color.parseColor is incorrect might be more helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you didn’t create, but just moved this code, so disregard the above if you like.

}
}

getQuestionMediaLayout().setPlayTextColor(playColor);
}

@Override
public void release() {
super.release();

if (player != null) {
player.release();
player = null;
}
}

@Override
protected void onWindowVisibilityChanged(int visibility) {
if (visibility == INVISIBLE || visibility == GONE) {
stopAudio();
}
}

/*
* Prompts with items must override this
*/
@CallSuper
public void playAllPromptText(String playOption) {
if (playOption.equalsIgnoreCase("audio")) {
getQuestionMediaLayout().playAudio();
} else if (playOption.equalsIgnoreCase("video")) {
getQuestionMediaLayout().playVideo();
}
}

public void stopAudio() {
if (player != null && player.isPlaying()) {
player.stop();
player.reset();
}
}

public MediaPlayer getPlayer() {
return player;
}

public int getPlayColor() {
return playColor;
}
}
Loading