Commit ee7bf4b0 by olly Committed by Oliver Woodman

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
parent 7c265dfb
...@@ -18,7 +18,6 @@ package com.google.android.exoplayer2.offline; ...@@ -18,7 +18,6 @@ package com.google.android.exoplayer2.offline;
import android.net.Uri; import android.net.Uri;
import android.os.Handler; import android.os.Handler;
import android.os.HandlerThread; import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message; import android.os.Message;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import android.util.Pair; import android.util.Pair;
...@@ -315,10 +314,10 @@ public final class DownloadHelper { ...@@ -315,10 +314,10 @@ public final class DownloadHelper {
private final DefaultTrackSelector trackSelector; private final DefaultTrackSelector trackSelector;
private final RendererCapabilities[] rendererCapabilities; private final RendererCapabilities[] rendererCapabilities;
private final SparseIntArray scratchSet; private final SparseIntArray scratchSet;
private final Handler callbackHandler;
private boolean isPreparedWithMedia; private boolean isPreparedWithMedia;
private @MonotonicNonNull Callback callback; private @MonotonicNonNull Callback callback;
private @MonotonicNonNull Handler callbackHandler;
private @MonotonicNonNull MediaPreparer mediaPreparer; private @MonotonicNonNull MediaPreparer mediaPreparer;
private TrackGroupArray @MonotonicNonNull [] trackGroupArrays; private TrackGroupArray @MonotonicNonNull [] trackGroupArrays;
private MappedTrackInfo @MonotonicNonNull [] mappedTrackInfos; private MappedTrackInfo @MonotonicNonNull [] mappedTrackInfos;
...@@ -354,20 +353,18 @@ public final class DownloadHelper { ...@@ -354,20 +353,18 @@ public final class DownloadHelper {
this.scratchSet = new SparseIntArray(); this.scratchSet = new SparseIntArray();
trackSelector.setParameters(trackSelectorParameters); trackSelector.setParameters(trackSelectorParameters);
trackSelector.init(/* listener= */ () -> {}, new DummyBandwidthMeter()); trackSelector.init(/* listener= */ () -> {}, new DummyBandwidthMeter());
callbackHandler = new Handler(Util.getLooper());
} }
/** /**
* Initializes the helper for starting a download. * Initializes the helper for starting a download.
* *
* @param callback A callback to be notified when preparation completes or fails. The callback * @param callback A callback to be notified when preparation completes or fails.
* 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.
* @throws IllegalStateException If the download helper has already been prepared. * @throws IllegalStateException If the download helper has already been prepared.
*/ */
public void prepare(Callback callback) { public void prepare(Callback callback) {
Assertions.checkState(this.callback == null); Assertions.checkState(this.callback == null);
this.callback = callback; this.callback = callback;
callbackHandler = new Handler(Util.getLooper());
if (mediaSource != null) { if (mediaSource != null) {
mediaPreparer = new MediaPreparer(mediaSource, /* downloadHelper= */ this); mediaPreparer = new MediaPreparer(mediaSource, /* downloadHelper= */ this);
} else { } else {
......
...@@ -28,7 +28,6 @@ import android.os.Handler; ...@@ -28,7 +28,6 @@ import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.os.PowerManager; import android.os.PowerManager;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -59,11 +58,12 @@ public final class RequirementsWatcher { ...@@ -59,11 +58,12 @@ public final class RequirementsWatcher {
private final Context context; private final Context context;
private final Listener listener; private final Listener listener;
private final Requirements requirements; private final Requirements requirements;
private final Handler handler;
private DeviceStatusChangeReceiver receiver; private DeviceStatusChangeReceiver receiver;
@Requirements.RequirementFlags private int notMetRequirements; @Requirements.RequirementFlags private int notMetRequirements;
private CapabilityValidatedCallback networkCallback; private CapabilityValidatedCallback networkCallback;
private Handler handler;
/** /**
* @param context Any context. * @param context Any context.
...@@ -71,9 +71,10 @@ public final class RequirementsWatcher { ...@@ -71,9 +71,10 @@ public final class RequirementsWatcher {
* @param requirements The requirements to watch. * @param requirements The requirements to watch.
*/ */
public RequirementsWatcher(Context context, Listener listener, Requirements requirements) { public RequirementsWatcher(Context context, Listener listener, Requirements requirements) {
this.requirements = requirements;
this.listener = listener;
this.context = context.getApplicationContext(); this.context = context.getApplicationContext();
this.listener = listener;
this.requirements = requirements;
handler = new Handler(Util.getLooper());
logd(this + " created"); logd(this + " created");
} }
...@@ -85,9 +86,6 @@ public final class RequirementsWatcher { ...@@ -85,9 +86,6 @@ public final class RequirementsWatcher {
*/ */
@Requirements.RequirementFlags @Requirements.RequirementFlags
public int start() { public int start() {
Assertions.checkNotNull(Looper.myLooper());
handler = new Handler();
notMetRequirements = requirements.getNotMetRequirements(context); notMetRequirements = requirements.getNotMetRequirements(context);
IntentFilter filter = new IntentFilter(); IntentFilter filter = new IntentFilter();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment