Some Leanback extension + minVersion bump fixes

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=165219604
This commit is contained in:
olly 2017-08-14 13:20:23 -07:00 committed by Oliver Woodman
parent d9cd4641f2
commit 08d16c55d4
5 changed files with 36 additions and 65 deletions

View File

@ -21,11 +21,8 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.MockitoAnnotations.initMocks; import static org.mockito.MockitoAnnotations.initMocks;
import android.annotation.TargetApi;
import android.os.Build.VERSION_CODES;
import android.support.test.InstrumentationRegistry; import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4; import android.support.test.runner.AndroidJUnit4;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
@ -67,7 +64,6 @@ public final class ByteArrayUploadDataProviderTest {
assertArrayEquals(TEST_DATA, byteBuffer.array()); assertArrayEquals(TEST_DATA, byteBuffer.array());
} }
@TargetApi(VERSION_CODES.GINGERBREAD)
@Test @Test
public void testReadPartialBuffer() throws IOException { public void testReadPartialBuffer() throws IOException {
byte[] firstHalf = Arrays.copyOfRange(TEST_DATA, 0, TEST_DATA.length / 2); byte[] firstHalf = Arrays.copyOfRange(TEST_DATA, 0, TEST_DATA.length / 2);

View File

@ -3,7 +3,7 @@
## Description ## ## Description ##
This [Leanback][] Extension provides a [PlayerAdapter][] implementation for This [Leanback][] Extension provides a [PlayerAdapter][] implementation for
Exoplayer. ExoPlayer.
[PlayerAdapter]: https://developer.android.com/reference/android/support/v17/leanback/media/PlayerAdapter.html [PlayerAdapter]: https://developer.android.com/reference/android/support/v17/leanback/media/PlayerAdapter.html
[Leanback]: https://developer.android.com/reference/android/support/v17/leanback/package-summary.html [Leanback]: https://developer.android.com/reference/android/support/v17/leanback/package-summary.html

View File

@ -19,7 +19,7 @@ android {
buildToolsVersion project.ext.buildToolsVersion buildToolsVersion project.ext.buildToolsVersion
defaultConfig { defaultConfig {
minSdkVersion project.ext.minSdkVersion minSdkVersion 17
targetSdkVersion project.ext.targetSdkVersion targetSdkVersion project.ext.targetSdkVersion
} }
} }

View File

@ -22,12 +22,14 @@ import android.support.v17.leanback.media.PlaybackGlueHost;
import android.support.v17.leanback.media.PlayerAdapter; import android.support.v17.leanback.media.PlayerAdapter;
import android.support.v17.leanback.media.SurfaceHolderGlueHost; import android.support.v17.leanback.media.SurfaceHolderGlueHost;
import android.util.Pair; import android.util.Pair;
import android.view.Surface;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlaybackException; import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayer; import com.google.android.exoplayer2.ExoPlayer;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.PlaybackParameters; import com.google.android.exoplayer2.PlaybackParameters;
import com.google.android.exoplayer2.Player;
import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.Timeline; import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
@ -39,6 +41,10 @@ import com.google.android.exoplayer2.util.ErrorMessageProvider;
*/ */
public final class LeanbackPlayerAdapter extends PlayerAdapter { public final class LeanbackPlayerAdapter extends PlayerAdapter {
static {
ExoPlayerLibraryInfo.registerModule("goog.exo.leanback");
}
private final Context context; private final Context context;
private final SimpleExoPlayer player; private final SimpleExoPlayer player;
private final Handler handler; private final Handler handler;
@ -53,7 +59,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
private SurfaceHolderGlueHost surfaceHolderGlueHost; private SurfaceHolderGlueHost surfaceHolderGlueHost;
private boolean initialized; private boolean initialized;
private boolean hasDisplay; private boolean hasSurface;
private boolean isBuffering; private boolean isBuffering;
private ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider; private ErrorMessageProvider<? super ExoPlaybackException> errorMessageProvider;
private final int updatePeriod; private final int updatePeriod;
@ -70,10 +76,6 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
} }
}; };
static {
ExoPlayerLibraryInfo.registerModule("goog.exo.leanback");
}
/** /**
* Constructor. * Constructor.
* Users are responsible for managing {@link SimpleExoPlayer} lifecycle. You must * Users are responsible for managing {@link SimpleExoPlayer} lifecycle. You must
@ -92,32 +94,28 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
@Override @Override
public void onAttachedToHost(PlaybackGlueHost host) { public void onAttachedToHost(PlaybackGlueHost host) {
if (host instanceof SurfaceHolderGlueHost) { if (host instanceof SurfaceHolderGlueHost) {
surfaceHolderGlueHost = ((SurfaceHolderGlueHost) host); surfaceHolderGlueHost = ((SurfaceHolderGlueHost) host);
surfaceHolderGlueHost.setSurfaceHolderCallback(new VideoPlayerSurfaceHolderCallback()); surfaceHolderGlueHost.setSurfaceHolderCallback(new VideoPlayerSurfaceHolderCallback());
} }
initializePlayer();
}
private void initializePlayer() {
notifyListeners(); notifyListeners();
this.player.addListener(exoPlayerListener); player.addListener(exoPlayerListener);
this.player.setVideoListener(videoListener); player.addVideoListener(videoListener);
} }
private void notifyListeners() { private void notifyListeners() {
boolean oldIsPrepared = isPrepared(); boolean oldIsPrepared = isPrepared();
int playbackState = player.getPlaybackState(); int playbackState = player.getPlaybackState();
boolean isInitialized = playbackState != ExoPlayer.STATE_IDLE; boolean isInitialized = playbackState != Player.STATE_IDLE;
isBuffering = playbackState == ExoPlayer.STATE_BUFFERING; isBuffering = playbackState == Player.STATE_BUFFERING;
boolean hasEnded = playbackState == ExoPlayer.STATE_ENDED; boolean hasEnded = playbackState == Player.STATE_ENDED;
initialized = isInitialized; initialized = isInitialized;
if (oldIsPrepared != isPrepared()) { if (oldIsPrepared != isPrepared()) {
getCallback().onPreparedStateChanged(LeanbackPlayerAdapter.this); getCallback().onPreparedStateChanged(this);
} }
getCallback().onPlayStateChanged(this); getCallback().onPlayStateChanged(this);
notifyBufferingState(); getCallback().onBufferingStateChanged(this, isBuffering || !initialized);
if (hasEnded) { if (hasEnded) {
getCallback().onPlayCompleted(this); getCallback().onPlayCompleted(this);
@ -134,37 +132,20 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
this.errorMessageProvider = errorMessageProvider; this.errorMessageProvider = errorMessageProvider;
} }
private void uninitializePlayer() {
if (initialized) {
initialized = false;
notifyBufferingState();
if (hasDisplay) {
getCallback().onPlayStateChanged(LeanbackPlayerAdapter.this);
getCallback().onPreparedStateChanged(LeanbackPlayerAdapter.this);
}
player.removeListener(exoPlayerListener);
player.clearVideoListener(videoListener);
}
}
/**
* Notify the state of buffering. For example, an app may enable/disable a loading figure
* according to the state of buffering.
*/
private void notifyBufferingState() {
getCallback().onBufferingStateChanged(LeanbackPlayerAdapter.this,
isBuffering || !initialized);
}
@Override @Override
public void onDetachedFromHost() { public void onDetachedFromHost() {
player.removeListener(exoPlayerListener);
player.removeVideoListener(videoListener);
if (surfaceHolderGlueHost != null) { if (surfaceHolderGlueHost != null) {
surfaceHolderGlueHost.setSurfaceHolderCallback(null); surfaceHolderGlueHost.setSurfaceHolderCallback(null);
surfaceHolderGlueHost = null; surfaceHolderGlueHost = null;
} }
uninitializePlayer(); initialized = false;
hasDisplay = false; hasSurface = false;
Callback callback = getCallback();
callback.onBufferingStateChanged(this, false);
callback.onPlayStateChanged(this);
callback.onPreparedStateChanged(this);
} }
@Override @Override
@ -194,7 +175,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
@Override @Override
public void play() { public void play() {
if (player.getPlaybackState() == ExoPlayer.STATE_ENDED) { if (player.getPlaybackState() == Player.STATE_ENDED) {
seekTo(0); seekTo(0);
} }
player.setPlayWhenReady(true); player.setPlayWhenReady(true);
@ -208,8 +189,8 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
} }
@Override @Override
public void seekTo(long newPosition) { public void seekTo(long positionMs) {
player.seekTo(newPosition); player.seekTo(positionMs);
} }
@Override @Override
@ -223,26 +204,22 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
*/ */
@Override @Override
public boolean isPrepared() { public boolean isPrepared() {
return initialized && (surfaceHolderGlueHost == null || hasDisplay); return initialized && (surfaceHolderGlueHost == null || hasSurface);
} }
/** private void setVideoSurface(Surface surface) {
* @see SimpleExoPlayer#setVideoSurfaceHolder(SurfaceHolder) hasSurface = surface != null;
*/ player.setVideoSurface(surface);
private void setDisplay(SurfaceHolder surfaceHolder) {
hasDisplay = surfaceHolder != null;
player.setVideoSurface(surfaceHolder.getSurface());
getCallback().onPreparedStateChanged(this); getCallback().onPreparedStateChanged(this);
} }
/** /**
* Implements {@link SurfaceHolder.Callback} that can then be set on the * Implements {@link SurfaceHolder.Callback} that can then be set on the {@link PlaybackGlueHost}.
* {@link PlaybackGlueHost}.
*/ */
private final class VideoPlayerSurfaceHolderCallback implements SurfaceHolder.Callback { private final class VideoPlayerSurfaceHolderCallback implements SurfaceHolder.Callback {
@Override @Override
public void surfaceCreated(SurfaceHolder surfaceHolder) { public void surfaceCreated(SurfaceHolder surfaceHolder) {
setDisplay(surfaceHolder); setVideoSurface(surfaceHolder.getSurface());
} }
@Override @Override
@ -251,7 +228,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
@Override @Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) { public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
setDisplay(null); setVideoSurface(null);
} }
} }
@ -264,7 +241,6 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter {
@Override @Override
public void onPlayerError(ExoPlaybackException exception) { public void onPlayerError(ExoPlaybackException exception) {
String errMsg = "";
if (errorMessageProvider != null) { if (errorMessageProvider != null) {
Pair<Integer, String> message = errorMessageProvider.getErrorMessage(exception); Pair<Integer, String> message = errorMessageProvider.getErrorMessage(exception);
if (message != null) { if (message != null) {

View File

@ -378,7 +378,6 @@ public class DefaultTimeBar extends View implements TimeBar {
super.onSizeChanged(width, height, oldWidth, oldHeight); super.onSizeChanged(width, height, oldWidth, oldHeight);
} }
@TargetApi(14)
@Override @Override
public void onInitializeAccessibilityEvent(AccessibilityEvent event) { public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event); super.onInitializeAccessibilityEvent(event);