Add Util.postOrRun

To avoid repetition, it adds Util.postOrRun and replaces existing
instances with the util method.

PiperOrigin-RevId: 329472769
This commit is contained in:
gyumin 2020-09-01 11:04:47 +01:00 committed by Oliver Woodman
parent 91185500a1
commit d155416c54
7 changed files with 46 additions and 73 deletions

View File

@ -15,8 +15,10 @@
*/
package com.google.android.exoplayer2.ext.media2;
import static com.google.android.exoplayer2.util.Util.postOrRun;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import android.os.Handler;
import androidx.annotation.GuardedBy;
import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
@ -129,7 +131,7 @@ import java.util.concurrent.Callable;
// Should be only used on the handler.
private final PlayerWrapper player;
private final PlayerHandler handler;
private final Handler handler;
private final Object lock;
@GuardedBy("lock")
@ -141,7 +143,7 @@ import java.util.concurrent.Callable;
// Should be only used on the handler.
@Nullable private AsyncPlayerCommandResult pendingAsyncPlayerCommandResult;
public PlayerCommandQueue(PlayerWrapper player, PlayerHandler handler) {
public PlayerCommandQueue(PlayerWrapper player, Handler handler) {
this.player = player;
this.handler = handler;
lock = new Object();
@ -209,7 +211,7 @@ import java.util.concurrent.Callable;
}
processPendingCommandOnHandler();
},
handler::postOrRun);
(runnable) -> postOrRun(handler, runnable));
if (DEBUG) {
Log.d(TAG, "adding " + playerCommand);
}
@ -220,7 +222,8 @@ import java.util.concurrent.Callable;
}
public void notifyCommandError() {
handler.postOrRun(
postOrRun(
handler,
() -> {
@Nullable AsyncPlayerCommandResult pendingResult = pendingAsyncPlayerCommandResult;
if (pendingResult == null) {
@ -243,7 +246,8 @@ import java.util.concurrent.Callable;
if (DEBUG) {
Log.d(TAG, "notifyCommandCompleted, completedCommandCode=" + completedCommandCode);
}
handler.postOrRun(
postOrRun(
handler,
() -> {
@Nullable AsyncPlayerCommandResult pendingResult = pendingAsyncPlayerCommandResult;
if (pendingResult == null || pendingResult.commandCode != completedCommandCode) {
@ -267,7 +271,7 @@ import java.util.concurrent.Callable;
}
private void processPendingCommand() {
handler.postOrRun(this::processPendingCommandOnHandler);
postOrRun(handler, this::processPendingCommandOnHandler);
}
private void processPendingCommandOnHandler() {

View File

@ -1,41 +0,0 @@
/*
* Copyright 2019 The Android Open Source Project
*
* 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 the License.
*/
package com.google.android.exoplayer2.ext.media2;
import android.os.Handler;
import android.os.Looper;
/** A {@link Handler} that provides {@link #postOrRun(Runnable)}. */
/* package */ final class PlayerHandler extends Handler {
public PlayerHandler(Looper looper) {
super(looper);
}
/**
* Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of this
* handler. Otherwise, runs the runnable directly.
*
* @param r A runnable to either post or run.
* @return {@code true} if it's successfully run. {@code false} otherwise.
*/
public boolean postOrRun(Runnable r) {
if (Thread.currentThread() != getLooper().getThread()) {
return post(r);
}
r.run();
return true;
}
}

View File

@ -15,6 +15,9 @@
*/
package com.google.android.exoplayer2.ext.media2;
import static com.google.android.exoplayer2.util.Util.postOrRun;
import android.os.Handler;
import androidx.annotation.IntRange;
import androidx.annotation.Nullable;
import androidx.core.util.ObjectsCompat;
@ -100,7 +103,7 @@ import java.util.List;
private static final int POLL_BUFFER_INTERVAL_MS = 1000;
private final Listener listener;
private final PlayerHandler handler;
private final Handler handler;
private final Runnable pollBufferRunnable;
private final Player player;
@ -144,7 +147,7 @@ import java.util.List;
audioComponent.addAudioListener(componentListener);
}
handler = new PlayerHandler(player.getApplicationLooper());
handler = new Handler(player.getApplicationLooper());
pollBufferRunnable = new PollBufferRunnable();
media2Playlist = new ArrayList<>();
@ -436,7 +439,7 @@ import java.util.List;
private void handlePlayerStateChanged(@Player.State int state) {
if (state == Player.STATE_READY || state == Player.STATE_BUFFERING) {
handler.postOrRun(pollBufferRunnable);
postOrRun(handler, pollBufferRunnable);
} else {
handler.removeCallbacks(pollBufferRunnable);
}

View File

@ -15,6 +15,9 @@
*/
package com.google.android.exoplayer2.ext.media2;
import static com.google.android.exoplayer2.util.Util.postOrRun;
import android.os.Handler;
import androidx.annotation.FloatRange;
import androidx.annotation.GuardedBy;
import androidx.annotation.IntRange;
@ -64,7 +67,7 @@ public final class SessionPlayerConnector extends SessionPlayer {
private static final int END_OF_PLAYLIST = -1;
private final Object stateLock = new Object();
private final PlayerHandler taskHandler;
private final Handler taskHandler;
private final Executor taskHandlerExecutor;
private final PlayerWrapper player;
private final PlayerCommandQueue playerCommandQueue;
@ -106,8 +109,8 @@ public final class SessionPlayerConnector extends SessionPlayer {
Assertions.checkNotNull(controlDispatcher);
state = PLAYER_STATE_IDLE;
taskHandler = new PlayerHandler(player.getApplicationLooper());
taskHandlerExecutor = taskHandler::postOrRun;
taskHandler = new Handler(player.getApplicationLooper());
taskHandlerExecutor = (runnable) -> postOrRun(taskHandler, runnable);
ExoPlayerWrapperListener playerListener = new ExoPlayerWrapperListener();
this.player = new PlayerWrapper(playerListener, player, mediaItemConverter, controlDispatcher);
playerCommandQueue = new PlayerCommandQueue(this.player, taskHandler);
@ -598,7 +601,8 @@ public final class SessionPlayerConnector extends SessionPlayer {
private <T> T runPlayerCallableBlocking(Callable<T> callable) {
SettableFuture<T> future = SettableFuture.create();
boolean success =
taskHandler.postOrRun(
postOrRun(
taskHandler,
() -> {
try {
future.set(callable.call());

View File

@ -493,6 +493,24 @@ public final class Util {
return new Handler(looper, callback);
}
/**
* Posts the {@link Runnable} if the calling thread differs with the {@link Looper} of the {@link
* Handler}. Otherwise, runs the {@link Runnable} directly.
*
* @param handler The handler to which the {@link Runnable} will be posted.
* @param runnable The runnable to either post or run.
* @return {@code true} if the {@link Runnable} was successfully posted to the {@link Handler} or
* run. {@code false} otherwise.
*/
public static boolean postOrRun(Handler handler, Runnable runnable) {
if (handler.getLooper() == Looper.myLooper()) {
runnable.run();
return true;
} else {
return handler.post(runnable);
}
}
/**
* Returns the {@link Looper} associated with the current thread, or the {@link Looper} of the
* application's main thread if the current thread doesn't have a {@link Looper}.

View File

@ -15,8 +15,9 @@
*/
package com.google.android.exoplayer2.drm;
import static com.google.android.exoplayer2.util.Util.postOrRun;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.CheckResult;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Player;
@ -207,15 +208,6 @@ public interface DrmSessionEventListener {
}
}
/** Dispatches {@link #onDrmSessionAcquired(int, MediaPeriodId)}. */
private static void postOrRun(Handler handler, Runnable runnable) {
if (handler.getLooper() == Looper.myLooper()) {
runnable.run();
} else {
handler.post(runnable);
}
}
private static final class ListenerAndHandler {
public Handler handler;

View File

@ -15,8 +15,9 @@
*/
package com.google.android.exoplayer2.source;
import static com.google.android.exoplayer2.util.Util.postOrRun;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.CheckResult;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
@ -471,14 +472,6 @@ public interface MediaSourceEventListener {
return mediaTimeMs == C.TIME_UNSET ? C.TIME_UNSET : mediaTimeOffsetMs + mediaTimeMs;
}
private static void postOrRun(Handler handler, Runnable runnable) {
if (handler.getLooper() == Looper.myLooper()) {
runnable.run();
} else {
handler.post(runnable);
}
}
private static final class ListenerAndHandler {
public Handler handler;