Make callback Handler instantiation consistent

Most of our components that have Handler instances for calling
back to application code instantiate the Handler using Util.getLooper
in the constructor. This makes the two components that do
something else consistent with that model.

PiperOrigin-RevId: 241545575
This commit is contained in:
olly 2019-04-02 17:42:06 +01:00 committed by Oliver Woodman
parent 7c265dfb3a
commit ee7bf4b0ee
2 changed files with 8 additions and 13 deletions

View file

@ -18,7 +18,6 @@ package com.google.android.exoplayer2.offline;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.Nullable;
import android.util.Pair;
@ -315,10 +314,10 @@ public final class DownloadHelper {
private final DefaultTrackSelector trackSelector;
private final RendererCapabilities[] rendererCapabilities;
private final SparseIntArray scratchSet;
private final Handler callbackHandler;
private boolean isPreparedWithMedia;
private @MonotonicNonNull Callback callback;
private @MonotonicNonNull Handler callbackHandler;
private @MonotonicNonNull MediaPreparer mediaPreparer;
private TrackGroupArray @MonotonicNonNull [] trackGroupArrays;
private MappedTrackInfo @MonotonicNonNull [] mappedTrackInfos;
@ -354,20 +353,18 @@ public final class DownloadHelper {
this.scratchSet = new SparseIntArray();
trackSelector.setParameters(trackSelectorParameters);
trackSelector.init(/* listener= */ () -> {}, new DummyBandwidthMeter());
callbackHandler = new Handler(Util.getLooper());
}
/**
* Initializes the helper for starting a download.
*
* @param callback A callback to be notified when preparation completes or fails. The callback
* will be invoked on the calling thread unless that thread does not have an associated {@link
* Looper}, in which case it will be called on the application's main thread.
* @param callback A callback to be notified when preparation completes or fails.
* @throws IllegalStateException If the download helper has already been prepared.
*/
public void prepare(Callback callback) {
Assertions.checkState(this.callback == null);
this.callback = callback;
callbackHandler = new Handler(Util.getLooper());
if (mediaSource != null) {
mediaPreparer = new MediaPreparer(mediaSource, /* downloadHelper= */ this);
} else {

View file

@ -28,7 +28,6 @@ import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
@ -59,11 +58,12 @@ public final class RequirementsWatcher {
private final Context context;
private final Listener listener;
private final Requirements requirements;
private final Handler handler;
private DeviceStatusChangeReceiver receiver;
@Requirements.RequirementFlags private int notMetRequirements;
private CapabilityValidatedCallback networkCallback;
private Handler handler;
/**
* @param context Any context.
@ -71,9 +71,10 @@ public final class RequirementsWatcher {
* @param requirements The requirements to watch.
*/
public RequirementsWatcher(Context context, Listener listener, Requirements requirements) {
this.requirements = requirements;
this.listener = listener;
this.context = context.getApplicationContext();
this.listener = listener;
this.requirements = requirements;
handler = new Handler(Util.getLooper());
logd(this + " created");
}
@ -85,9 +86,6 @@ public final class RequirementsWatcher {
*/
@Requirements.RequirementFlags
public int start() {
Assertions.checkNotNull(Looper.myLooper());
handler = new Handler();
notMetRequirements = requirements.getNotMetRequirements(context);
IntentFilter filter = new IntentFilter();