Skip to content

Commit

Permalink
Updates samples to v3.35.0.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 673054076
  • Loading branch information
google-ima-devrel-bot authored and IMA Developer Relations committed Sep 11, 2024
1 parent 9a98510 commit c07dfc6
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 70 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'
}
2 changes: 1 addition & 1 deletion AdvancedExample/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.ads.interactivemedia.v3.samples.videoplayerapp">
>
<!-- Required permissions for the IMA SDK -->
<uses-permission android:name="android.permission.INTERNET"/>

Expand Down
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 @@ -5,11 +5,13 @@
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.multidex.MultiDex;
import java.util.Objects;

/** Main Activity. */
public class MyActivity extends AppCompatActivity
Expand Down Expand Up @@ -61,16 +63,15 @@ public boolean onCreateOptionsMenu(Menu menu) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
public boolean onOptionsItemSelected(@NonNull 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);
}

@Override
public void onConfigurationChanged(Configuration configuration) {
public void onConfigurationChanged(@NonNull Configuration configuration) {
super.onConfigurationChanged(configuration);
orientAppUi();
}
Expand Down Expand Up @@ -123,6 +124,7 @@ public void onVideoSelected(VideoItem videoItem) {
VideoListFragment videoListFragment =
(VideoListFragment)
getSupportFragmentManager().findFragmentByTag(VIDEO_PLAYLIST_FRAGMENT_TAG);
assert videoListFragment != null;
int videoPlaylistFragmentId = videoListFragment.getId();

videoFragment = new VideoFragment();
Expand Down Expand Up @@ -154,11 +156,11 @@ private void hideStatusBar() {
.getDecorView()
.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN);
getSupportActionBar().hide();
Objects.requireNonNull(getSupportActionBar()).hide();
}

private void showStatusBar() {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
getSupportActionBar().show();
Objects.requireNonNull(getSupportActionBar()).show();
}
}
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 @@ -7,20 +7,22 @@
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.List;

/** 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);
this.layoutResourceId = layoutResourceId;
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
public View getView(int position, View convertView, @NonNull ViewGroup parent) {
VideoItemHolder videoItemHolder;
View row = convertView;

Expand All @@ -29,23 +31,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
2 changes: 1 addition & 1 deletion AdvancedExample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip

2 changes: 1 addition & 1 deletion basicexample/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.browser:browser:1.8.0'
implementation 'androidx.media:media:1.7.0'
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 @@ -58,7 +58,6 @@ public class MyActivity extends AppCompatActivity {
// the VideoView.
private VideoView videoPlayer;
private MediaController mediaController;
private View playButton;
private VideoAdPlayerAdapter videoAdPlayerAdapter;

@Override
Expand Down Expand Up @@ -91,15 +90,15 @@ protected void onCreate(Bundle savedInstanceState) {
new AdErrorEvent.AdErrorListener() {
/** An event raised when there is an error loading or playing ads. */
@Override
public void onAdError(AdErrorEvent adErrorEvent) {
public void onAdError(@NonNull AdErrorEvent adErrorEvent) {
Log.i(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());
resumeContent();
}
});
adsLoader.addAdsLoadedListener(
new AdsLoader.AdsLoadedListener() {
@Override
public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
public void onAdsManagerLoaded(@NonNull AdsManagerLoadedEvent adsManagerLoadedEvent) {
// Ads were successfully loaded, so get the AdsManager instance. AdsManager has
// events for ad playback and errors.
adsManager = adsManagerLoadedEvent.getAdsManager();
Expand All @@ -109,7 +108,7 @@ public void onAdsManagerLoaded(AdsManagerLoadedEvent adsManagerLoadedEvent) {
new AdErrorEvent.AdErrorListener() {
/** An event raised when there is an error loading or playing ads. */
@Override
public void onAdError(AdErrorEvent adErrorEvent) {
public void onAdError(@NonNull AdErrorEvent adErrorEvent) {
Log.e(LOGTAG, "Ad Error: " + adErrorEvent.getError().getMessage());
String universalAdIds =
Arrays.toString(adsManager.getCurrentAd().getUniversalAdIds());
Expand All @@ -125,7 +124,7 @@ public void onAdError(AdErrorEvent adErrorEvent) {
new AdEvent.AdEventListener() {
/** Responds to AdEvents. */
@Override
public void onAdEvent(AdEvent adEvent) {
public void onAdEvent(@NonNull AdEvent adEvent) {
if (adEvent.getType() != AdEvent.AdEventType.AD_PROGRESS) {
Log.i(LOGTAG, "Event: " + adEvent.getType());
}
Expand Down Expand Up @@ -182,7 +181,7 @@ public void onAdEvent(AdEvent adEvent) {
});

// When the play button is clicked, request ads and hide the button.
playButton = findViewById(R.id.playButton);
View playButton = findViewById(R.id.playButton);
playButton.setOnClickListener(
view -> {
videoPlayer.setVideoPath(SAMPLE_VIDEO_URL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.net.Uri;
import android.util.Log;
import android.widget.VideoView;
import androidx.annotation.NonNull;
import com.google.ads.interactivemedia.v3.api.AdPodInfo;
import com.google.ads.interactivemedia.v3.api.player.AdMediaInfo;
import com.google.ads.interactivemedia.v3.api.player.VideoAdPlayer;
Expand Down Expand Up @@ -40,12 +41,12 @@ public VideoAdPlayerAdapter(VideoView videoPlayer, AudioManager audioManager) {
}

@Override
public void addCallback(VideoAdPlayerCallback videoAdPlayerCallback) {
public void addCallback(@NonNull VideoAdPlayerCallback videoAdPlayerCallback) {
videoAdPlayerCallbacks.add(videoAdPlayerCallback);
}

@Override
public void loadAd(AdMediaInfo adMediaInfo, AdPodInfo adPodInfo) {
public void loadAd(@NonNull AdMediaInfo adMediaInfo, @NonNull AdPodInfo adPodInfo) {
// This simple ad loading logic works because preloading is disabled. To support
// preloading ads your app must maintain state for the currently playing ad
// while handling upcoming ad downloading and buffering at the same time.
Expand All @@ -55,7 +56,7 @@ public void loadAd(AdMediaInfo adMediaInfo, AdPodInfo adPodInfo) {
}

@Override
public void pauseAd(AdMediaInfo adMediaInfo) {
public void pauseAd(@NonNull AdMediaInfo adMediaInfo) {
Log.i(LOGTAG, "pauseAd");
savedAdPosition = videoPlayer.getCurrentPosition();
stopAdTracking();
Expand Down Expand Up @@ -89,12 +90,12 @@ public void release() {
}

@Override
public void removeCallback(VideoAdPlayerCallback videoAdPlayerCallback) {
public void removeCallback(@NonNull VideoAdPlayerCallback videoAdPlayerCallback) {
videoAdPlayerCallbacks.remove(videoAdPlayerCallback);
}

@Override
public void stopAd(AdMediaInfo adMediaInfo) {
public void stopAd(@NonNull AdMediaInfo adMediaInfo) {
Log.i(LOGTAG, "stopAd");
stopAdTracking();
}
Expand Down Expand Up @@ -176,6 +177,7 @@ private void stopAdTracking() {
}
}

@NonNull
@Override
public VideoProgressUpdate getAdProgress() {
long adPosition = videoPlayer.getCurrentPosition();
Expand Down
2 changes: 1 addition & 1 deletion basicexample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-all.zip

0 comments on commit c07dfc6

Please sign in to comment.