Skip to content

Commit

Permalink
Updates samples to v3.35.0.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676842914
  • Loading branch information
google-ima-devrel-bot authored and IMA Developer Relations committed Sep 20, 2024
1 parent 9a98510 commit b320308
Show file tree
Hide file tree
Showing 28 changed files with 207 additions and 288 deletions.
2 changes: 1 addition & 1 deletion AdvancedExample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.mediarouter:mediarouter:1.7.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation 'com.google.ads.interactivemedia.v3:interactivemedia:3.34.0'
implementation 'com.google.ads.interactivemedia.v3:interactivemedia:3.35.0'
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.util.AttributeSet;
Expand All @@ -24,7 +23,7 @@ private enum PlaybackState {

private MediaController mediaController;
private PlaybackState playbackState;
private final List<PlayerCallback> videoPlayerCallbacks = new ArrayList<PlayerCallback>(1);
private final List<PlayerCallback> videoPlayerCallbacks = new ArrayList<>(1);

public SampleVideoPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Expand All @@ -49,36 +48,28 @@ private void init() {

// Set OnCompletionListener to notify our callbacks when the video is completed.
super.setOnCompletionListener(
new OnCompletionListener() {

@Override
public void onCompletion(MediaPlayer mediaPlayer) {
// Reset the MediaPlayer.
playbackState = PlaybackState.STOPPED;
mediaPlayer.reset();
mediaPlayer.setDisplay(getHolder());

for (PlayerCallback callback : videoPlayerCallbacks) {
callback.onComplete();
}
mediaPlayer -> {
// Reset the MediaPlayer.
playbackState = PlaybackState.STOPPED;
mediaPlayer.reset();
mediaPlayer.setDisplay(getHolder());

for (PlayerCallback callback : videoPlayerCallbacks) {
callback.onComplete();
}
});

// Set OnErrorListener to notify our callbacks if the video errors.
super.setOnErrorListener(
new OnErrorListener() {

@Override
public boolean onError(MediaPlayer mp, int what, int extra) {
playbackState = PlaybackState.STOPPED;
for (PlayerCallback callback : videoPlayerCallbacks) {
callback.onError();
}

// Returning true signals to MediaPlayer that we handled the error. This will
// prevent the completion handler from being called.
return true;
(mp, what, extra) -> {
playbackState = PlaybackState.STOPPED;
for (PlayerCallback callback : videoPlayerCallbacks) {
callback.onError();
}

// Returning true signals to MediaPlayer that we handled the error. This will
// prevent the completion handler from being called.
return true;
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public interface VideoPlayer {

/** Interface for alerting caller of major video events. */
public interface PlayerCallback {
interface PlayerCallback {

/** Called when the current video starts playing from the beginning. */
void onPlay();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class VideoFragment extends Fragment {

/** Listener called when the fragment's onCreateView is fired. */
public interface OnVideoFragmentViewCreatedListener {
public void onVideoFragmentViewCreated();
void onVideoFragmentViewCreated();
}

@Override
Expand Down Expand Up @@ -61,7 +61,7 @@ private void initUi(View rootView) {

// Make the dummyScrollContent height the size of the screen height.
DisplayMetrics displayMetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
requireActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
ConstraintLayout constraintLayout = rootView.findViewById(R.id.constraintLayout);
ConstraintSet forceHeight = new ConstraintSet();
forceHeight.clone(constraintLayout);
Expand All @@ -72,13 +72,10 @@ private void initUi(View rootView) {

// Provide an implementation of a logger so we can output SDK events to the UI.
VideoPlayerController.Logger logger =
new VideoPlayerController.Logger() {
@Override
public void log(String message) {
Log.i("ImaExample", message);
if (logText != null) {
logText.append(message);
}
message -> {
Log.i("ImaExample", message);
if (logText != null) {
logText.append(message);
}
};

Expand Down Expand Up @@ -113,10 +110,6 @@ public void makeFullscreen(boolean isFullscreen) {
}
}

public VideoPlayerController getVideoPlayerController() {
return videoPlayerController;
}

@Override
public void onPause() {
if (videoPlayerController != null) {
Expand All @@ -141,8 +134,4 @@ public void onDestroy() {
}
super.onDestroy();
}

public boolean isVmap() {
return videoItem.getIsVmap();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/** Renders VideoItems into a GridView for displaying videos in a playlist format. */
public class VideoItemAdapter extends ArrayAdapter<VideoItem> {

private int layoutResourceId;
private final int layoutResourceId;

public VideoItemAdapter(Context context, int layoutResourceId, List<VideoItem> data) {
super(context, layoutResourceId, data);
Expand All @@ -29,23 +29,24 @@ public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(getContext());
row = inflater.inflate(layoutResourceId, parent, false);
videoItemHolder = new VideoItemHolder();
videoItemHolder.title = (TextView) row.findViewById(R.id.videoItemText);
videoItemHolder.image = (ImageView) row.findViewById(R.id.videoItemImage);
videoItemHolder.title = row.findViewById(R.id.videoItemText);
videoItemHolder.image = row.findViewById(R.id.videoItemImage);
row.setTag(videoItemHolder);
} else {
videoItemHolder = (VideoItemHolder) row.getTag();
}

VideoItem item = getItem(position);

assert item != null;
videoItemHolder.title.setText(item.getTitle());
videoItemHolder.image.setImageResource(item.getImageResource());

return row;
}

/** Holds the UI element equivalents of a VideoItem. */
private class VideoItemHolder {
private static class VideoItemHolder {

TextView title;
ImageView image;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
Expand All @@ -18,7 +16,7 @@
/** Fragment for displaying a playlist of video thumbnails that the user can select from to play. */
public class VideoListFragment extends Fragment {

private OnVideoSelectedListener selectedCallback;
OnVideoSelectedListener selectedCallback;
LayoutInflater inflater;
ViewGroup container;

Expand All @@ -44,16 +42,14 @@ public void onAttach(Context context) {
selectedCallback = (OnVideoSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(
context.toString() + " must implement " + OnVideoSelectedListener.class.getName());
context + " must implement " + OnVideoSelectedListener.class.getName());
}

try {
resumeCallback = (OnVideoListFragmentResumedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(
context.toString()
+ " must implement "
+ OnVideoListFragmentResumedListener.class.getName());
context + " must implement " + OnVideoListFragmentResumedListener.class.getName());
}
}

Expand All @@ -64,24 +60,21 @@ public View onCreateView(
this.container = container;
View rootView = inflater.inflate(R.layout.fragment_video_list, container, false);

final ListView listView = (ListView) rootView.findViewById(R.id.videoListView);
final ListView listView = rootView.findViewById(R.id.videoListView);
VideoItemAdapter videoItemAdapter =
new VideoItemAdapter(rootView.getContext(), R.layout.video_item, getVideoItems());
listView.setAdapter(videoItemAdapter);

listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
if (selectedCallback != null) {
VideoItem selectedVideo = (VideoItem) listView.getItemAtPosition(position);

// If applicable, prompt the user to input a custom ad tag.
if (selectedVideo.getAdTagUrl().equals(getString(R.string.custom_ad_tag_value))) {
getCustomAdTag(selectedVideo);
} else {
selectedCallback.onVideoSelected(selectedVideo);
}
(parent, v, position, id) -> {
if (selectedCallback != null) {
VideoItem selectedVideo = (VideoItem) listView.getItemAtPosition(position);

// If applicable, prompt the user to input a custom ad tag.
if (selectedVideo.getAdTagUrl().equals(getString(R.string.custom_ad_tag_value))) {
getCustomAdTag(selectedVideo);
} else {
selectedCallback.onVideoSelected(selectedVideo);
}
}
});
Expand All @@ -91,42 +84,36 @@ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

private void getCustomAdTag(VideoItem originalVideoItem) {
View dialogueView = inflater.inflate(R.layout.custom_ad_tag, container, false);
final EditText txtUrl = (EditText) dialogueView.findViewById(R.id.customTag);
final EditText txtUrl = dialogueView.findViewById(R.id.customTag);
txtUrl.setHint("VAST ad tag URL");
final CheckBox isVmap = (CheckBox) dialogueView.findViewById(R.id.isVmap);
final CheckBox isVmap = dialogueView.findViewById(R.id.isVmap);
final VideoItem videoItem = originalVideoItem;

new AlertDialog.Builder(this.getActivity())
.setTitle("Custom VAST Ad Tag URL")
.setView(dialogueView)
.setPositiveButton(
"OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String customAdTagUrl = txtUrl.getText().toString();
VideoItem customAdTagVideoItem =
new VideoItem(
videoItem.getVideoUrl(),
videoItem.getTitle(),
customAdTagUrl,
videoItem.getImageResource(),
isVmap.isChecked());

if (selectedCallback != null) {
selectedCallback.onVideoSelected(customAdTagVideoItem);
}
(dialog, whichButton) -> {
String customAdTagUrl = txtUrl.getText().toString();
VideoItem customAdTagVideoItem =
new VideoItem(
videoItem.getVideoUrl(),
videoItem.getTitle(),
customAdTagUrl,
videoItem.getImageResource(),
isVmap.isChecked());

if (selectedCallback != null) {
selectedCallback.onVideoSelected(customAdTagVideoItem);
}
})
.setNegativeButton(
"Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {}
})
.setNegativeButton("Cancel", (dialog, whichButton) -> {})
.show();
}

private List<VideoItem> getVideoItems() {
final List<VideoItem> videoItems = new ArrayList<VideoItem>();
final List<VideoItem> videoItems = new ArrayList<>();

// Iterate through the videos' metadata and add video items to list.
for (int i = 0; i < VideoMetadata.APP_VIDEOS.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public enum VideoMetadata {
/** If the ad is VMAP. */
public final boolean isVmap;

private VideoMetadata(
String videoUrl, String title, String adTagUrl, int thumbnail, boolean isVmap) {
VideoMetadata(String videoUrl, String title, String adTagUrl, int thumbnail, boolean isVmap) {
this.videoUrl = videoUrl;
this.title = title;
this.adTagUrl = adTagUrl;
Expand Down
Loading

0 comments on commit b320308

Please sign in to comment.