Use Util method to create Handler instead of using deprecated method.

Calls to new Handler() without arguments are deprecated as of the latest Android
version. Replace them by a Util.createHandler call similar to the ones we
already have.

PiperOrigin-RevId: 283532891
This commit is contained in:
tonihei 2019-12-03 14:25:54 +00:00 committed by bachinger
parent fb6a8a2c5d
commit 65c4a58825
16 changed files with 64 additions and 38 deletions

View file

@ -36,6 +36,7 @@ import com.google.android.exoplayer2.Player.DiscontinuityReason;
import com.google.android.exoplayer2.Player.TimelineChangeReason;
import com.google.android.exoplayer2.Timeline;
import com.google.android.exoplayer2.util.ErrorMessageProvider;
import com.google.android.exoplayer2.util.Util;
import com.google.android.exoplayer2.video.VideoListener;
/** Leanback {@code PlayerAdapter} implementation for {@link Player}. */
@ -71,7 +72,7 @@ public final class LeanbackPlayerAdapter extends PlayerAdapter implements Runnab
this.context = context;
this.player = player;
this.updatePeriodMs = updatePeriodMs;
handler = new Handler();
handler = Util.createHandler();
componentListener = new ComponentListener();
controlDispatcher = new DefaultControlDispatcher();
}

View file

@ -427,7 +427,7 @@ import java.util.Set;
(source, timeline) -> playlistInfoListener.onPlaylistUpdateRequested();
MediaSourceEventListener eventListener = new ForwardingEventListener(holder);
childSources.put(holder, new MediaSourceAndListener(mediaSource, caller, eventListener));
mediaSource.addEventListener(new Handler(), eventListener);
mediaSource.addEventListener(Util.createHandler(), eventListener);
mediaSource.prepareSource(caller, mediaTransferListener);
}

View file

@ -46,7 +46,7 @@ public abstract class CompositeMediaSource<T> extends BaseMediaSource {
@CallSuper
protected void prepareSourceInternal(@Nullable TransferListener mediaTransferListener) {
this.mediaTransferListener = mediaTransferListener;
eventHandler = new Handler();
eventHandler = Util.createHandler();
}
@Override

View file

@ -189,7 +189,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType;
.onContinueLoadingRequested(ProgressiveMediaPeriod.this);
}
};
handler = new Handler();
handler = Util.createHandler();
sampleQueueTrackIds = new TrackId[0];
sampleQueues = new SampleQueue[0];
pendingResetPositionUs = C.TIME_UNSET;

View file

@ -37,6 +37,7 @@ import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
@ -365,7 +366,7 @@ public final class AdsMediaSource extends CompositeMediaSource<MediaPeriodId> {
* events on the external event listener thread.
*/
public ComponentListener() {
playerHandler = new Handler();
playerHandler = Util.createHandler();
}
/** Releases the component listener. */

View file

@ -366,6 +366,17 @@ public final class Util {
/* length= */ second.length);
return concatenation;
}
/**
* Creates a {@link Handler} on the current {@link Looper} thread.
*
* <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link
* Looper} is used.
*/
public static Handler createHandler() {
return createHandler(/* callback= */ null);
}
/**
* Creates a {@link Handler} with the specified {@link Handler.Callback} on the current {@link
* Looper} thread. The method accepts partially initialized objects as callback under the
@ -375,10 +386,11 @@ public final class Util {
* <p>If the current thread doesn't have a {@link Looper}, the application's main thread {@link
* Looper} is used.
*
* @param callback A {@link Handler.Callback}. May be a partially initialized class.
* @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no
* callback is required.
* @return A {@link Handler} with the specified callback on the current {@link Looper} thread.
*/
public static Handler createHandler(Handler.@UnknownInitialization Callback callback) {
public static Handler createHandler(@Nullable Handler.@UnknownInitialization Callback callback) {
return createHandler(getLooper(), callback);
}
@ -389,12 +401,13 @@ public final class Util {
* initialized.
*
* @param looper A {@link Looper} to run the callback on.
* @param callback A {@link Handler.Callback}. May be a partially initialized class.
* @param callback A {@link Handler.Callback}. May be a partially initialized class, or null if no
* callback is required.
* @return A {@link Handler} with the specified callback on the current {@link Looper} thread.
*/
@SuppressWarnings({"nullness:argument.type.incompatible", "nullness:return.type.incompatible"})
public static Handler createHandler(
Looper looper, Handler.@UnknownInitialization Callback callback) {
Looper looper, @Nullable Handler.@UnknownInitialization Callback callback) {
return new Handler(looper, callback);
}
@ -1988,7 +2001,7 @@ public final class Util {
@Nullable DrmSessionManager<FrameworkMediaCrypto> drmSessionManager) {
Renderer[] renderers =
renderersFactory.createRenderers(
new Handler(),
Util.createHandler(),
new VideoRendererEventListener() {},
new AudioRendererEventListener() {},
(cues) -> {},

View file

@ -1799,7 +1799,7 @@ public class MediaCodecVideoRenderer extends MediaCodecRenderer {
private final class OnFrameRenderedListenerV23 implements MediaCodec.OnFrameRenderedListener {
private OnFrameRenderedListenerV23(MediaCodec codec) {
codec.setOnFrameRenderedListener(this, new Handler());
codec.setOnFrameRenderedListener(/* listener= */ this, Util.createHandler());
}
@Override

View file

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.source;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
@ -37,6 +36,7 @@ import com.google.android.exoplayer2.testutil.MediaSourceTestRunner;
import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
@ -553,7 +553,7 @@ public final class ClippingMediaSourceTest {
testRunner.runOnPlaybackThread(
() ->
clippingMediaSource.addEventListener(
new Handler(),
Util.createHandler(),
new MediaSourceEventListener() {
@Override
public void onDownstreamFormatChanged(

View file

@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import android.os.Handler;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Player;
@ -34,6 +33,7 @@ import com.google.android.exoplayer2.testutil.FakeTimeline;
import com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition;
import com.google.android.exoplayer2.testutil.MediaSourceTestRunner;
import com.google.android.exoplayer2.testutil.TimelineAsserts;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -414,7 +414,7 @@ public final class ConcatenatingMediaSourceTest {
dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(
createFakeMediaSource(), new Handler(), runnableInvoked::countDown));
createFakeMediaSource(), Util.createHandler(), runnableInvoked::countDown));
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -430,7 +430,7 @@ public final class ConcatenatingMediaSourceTest {
() ->
mediaSource.addMediaSources(
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
Util.createHandler(),
runnableInvoked::countDown));
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -446,7 +446,10 @@ public final class ConcatenatingMediaSourceTest {
dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(
/* index */ 0, createFakeMediaSource(), new Handler(), runnableInvoked::countDown));
/* index */ 0,
createFakeMediaSource(),
Util.createHandler(),
runnableInvoked::countDown));
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -463,7 +466,7 @@ public final class ConcatenatingMediaSourceTest {
mediaSource.addMediaSources(
/* index */ 0,
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
Util.createHandler(),
runnableInvoked::countDown));
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -479,7 +482,8 @@ public final class ConcatenatingMediaSourceTest {
dummyMainThread.runOnMainThread(
() -> {
mediaSource.addMediaSource(createFakeMediaSource());
mediaSource.removeMediaSource(/* index */ 0, new Handler(), runnableInvoked::countDown);
mediaSource.removeMediaSource(
/* index */ 0, Util.createHandler(), runnableInvoked::countDown);
});
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -497,7 +501,7 @@ public final class ConcatenatingMediaSourceTest {
mediaSource.addMediaSources(
Arrays.asList(new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}));
mediaSource.moveMediaSource(
/* fromIndex */ 1, /* toIndex */ 0, new Handler(), runnableInvoked::countDown);
/* fromIndex */ 1, /* toIndex */ 0, Util.createHandler(), runnableInvoked::countDown);
});
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -513,7 +517,8 @@ public final class ConcatenatingMediaSourceTest {
final TimelineGrabber timelineGrabber = new TimelineGrabber(testRunner);
dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(createFakeMediaSource(), new Handler(), timelineGrabber));
mediaSource.addMediaSource(
createFakeMediaSource(), Util.createHandler(), timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(1);
} finally {
@ -532,7 +537,7 @@ public final class ConcatenatingMediaSourceTest {
mediaSource.addMediaSources(
Arrays.asList(
new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
Util.createHandler(),
timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(2);
@ -550,7 +555,7 @@ public final class ConcatenatingMediaSourceTest {
dummyMainThread.runOnMainThread(
() ->
mediaSource.addMediaSource(
/* index */ 0, createFakeMediaSource(), new Handler(), timelineGrabber));
/* index */ 0, createFakeMediaSource(), Util.createHandler(), timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(1);
} finally {
@ -570,7 +575,7 @@ public final class ConcatenatingMediaSourceTest {
/* index */ 0,
Arrays.asList(
new MediaSource[] {createFakeMediaSource(), createFakeMediaSource()}),
new Handler(),
Util.createHandler(),
timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(2);
@ -589,7 +594,8 @@ public final class ConcatenatingMediaSourceTest {
final TimelineGrabber timelineGrabber = new TimelineGrabber(testRunner);
dummyMainThread.runOnMainThread(
() -> mediaSource.removeMediaSource(/* index */ 0, new Handler(), timelineGrabber));
() ->
mediaSource.removeMediaSource(/* index */ 0, Util.createHandler(), timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(0);
} finally {
@ -613,7 +619,7 @@ public final class ConcatenatingMediaSourceTest {
dummyMainThread.runOnMainThread(
() ->
mediaSource.moveMediaSource(
/* fromIndex */ 1, /* toIndex */ 0, new Handler(), timelineGrabber));
/* fromIndex */ 1, /* toIndex */ 0, Util.createHandler(), timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getWindowCount()).isEqualTo(2);
} finally {
@ -634,7 +640,7 @@ public final class ConcatenatingMediaSourceTest {
mediaSource.moveMediaSource(
/* currentIndex= */ 0,
/* newIndex= */ 1,
new Handler(),
Util.createHandler(),
callbackCalledCondition::countDown);
mediaSource.releaseSource(caller);
});
@ -886,7 +892,7 @@ public final class ConcatenatingMediaSourceTest {
testRunner.prepareSource();
final TimelineGrabber timelineGrabber = new TimelineGrabber(testRunner);
dummyMainThread.runOnMainThread(() -> mediaSource.clear(new Handler(), timelineGrabber));
dummyMainThread.runOnMainThread(() -> mediaSource.clear(Util.createHandler(), timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.isEmpty()).isTrue();
@ -1038,7 +1044,7 @@ public final class ConcatenatingMediaSourceTest {
() ->
mediaSource.setShuffleOrder(
new ShuffleOrder.UnshuffledShuffleOrder(/* length= */ 0),
new Handler(),
Util.createHandler(),
runnableInvoked::countDown));
runnableInvoked.await(MediaSourceTestRunner.TIMEOUT_MS, TimeUnit.MILLISECONDS);
dummyMainThread.release();
@ -1058,7 +1064,7 @@ public final class ConcatenatingMediaSourceTest {
() ->
mediaSource.setShuffleOrder(
new ShuffleOrder.UnshuffledShuffleOrder(/* length= */ 3),
new Handler(),
Util.createHandler(),
timelineGrabber));
Timeline timeline = timelineGrabber.assertTimelineChangeBlocking();
assertThat(timeline.getFirstWindowIndex(/* shuffleModeEnabled= */ true)).isEqualTo(0);

View file

@ -663,7 +663,7 @@ public final class DashMediaSource extends BaseMediaSource {
} else {
dataSource = manifestDataSourceFactory.createDataSource();
loader = new Loader("Loader:DashMediaSource");
handler = new Handler();
handler = Util.createHandler();
startLoadingManifest();
}
}

View file

@ -219,7 +219,7 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
@SuppressWarnings("nullness:methodref.receiver.bound.invalid")
Runnable onTracksEndedRunnable = this::onTracksEnded;
this.onTracksEndedRunnable = onTracksEndedRunnable;
handler = new Handler();
handler = Util.createHandler();
lastSeekPositionUs = positionUs;
pendingResetPositionUs = positionUs;
}

View file

@ -31,6 +31,7 @@ import com.google.android.exoplayer2.upstream.Loader;
import com.google.android.exoplayer2.upstream.Loader.LoadErrorAction;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
@ -117,7 +118,7 @@ public final class DefaultHlsPlaylistTracker
Uri initialPlaylistUri,
EventDispatcher eventDispatcher,
PrimaryPlaylistListener primaryPlaylistListener) {
this.playlistRefreshHandler = new Handler();
this.playlistRefreshHandler = Util.createHandler();
this.eventDispatcher = eventDispatcher;
this.primaryPlaylistListener = primaryPlaylistListener;
ParsingLoadable<HlsPlaylist> masterPlaylistLoadable =

View file

@ -50,6 +50,7 @@ import com.google.android.exoplayer2.upstream.LoaderErrorThrower;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@ -560,7 +561,7 @@ public final class SsMediaSource extends BaseMediaSource
manifestDataSource = manifestDataSourceFactory.createDataSource();
manifestLoader = new Loader("Loader:Manifest");
manifestLoaderErrorThrower = manifestLoader;
manifestRefreshHandler = new Handler();
manifestRefreshHandler = Util.createHandler();
startLoadingManifest();
}
}

View file

@ -37,6 +37,7 @@ import com.google.android.exoplayer2.trackselection.DefaultTrackSelector.Paramet
import com.google.android.exoplayer2.util.ConditionVariable;
import com.google.android.exoplayer2.util.HandlerWrapper;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
/** Base class for actions to perform during playback tests. */
public abstract class Action {
@ -422,7 +423,7 @@ public abstract class Action {
} else {
message.setPosition(positionMs);
}
message.setHandler(new Handler());
message.setHandler(Util.createHandler());
message.setDeleteAfterDelivery(deleteAfterDelivery);
message.send();
}
@ -503,7 +504,7 @@ public abstract class Action {
final Surface surface,
final HandlerWrapper handler,
final ActionNode nextAction) {
Handler testThreadHandler = new Handler();
Handler testThreadHandler = Util.createHandler();
// Schedule a message on the playback thread to ensure the player is paused immediately.
player
.createMessage(

View file

@ -30,6 +30,7 @@ import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.TrackSelection;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.Collections;
@ -126,7 +127,7 @@ public class FakeMediaPeriod implements MediaPeriod {
SystemClock.elapsedRealtime());
prepareCallback = callback;
if (deferOnPrepared) {
playerHandler = new Handler();
playerHandler = Util.createHandler();
} else {
finishPreparation();
}

View file

@ -37,6 +37,7 @@ import com.google.android.exoplayer2.upstream.Allocator;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
@ -97,7 +98,7 @@ public class FakeMediaSource extends BaseMediaSource {
transferListener = mediaTransferListener;
preparedSource = true;
releasedSource = false;
sourceInfoRefreshHandler = new Handler();
sourceInfoRefreshHandler = Util.createHandler();
if (timeline != null) {
finishSourcePreparation();
}