From ee7bf4b0ee5729e2f6200cc60b610def02af6497 Mon Sep 17 00:00:00 2001 From: olly Date: Tue, 2 Apr 2019 17:42:06 +0100 Subject: [PATCH] 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 --- .../android/exoplayer2/offline/DownloadHelper.java | 9 +++------ .../exoplayer2/scheduler/RequirementsWatcher.java | 12 +++++------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java index dccc15f020..ccaec105ca 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadHelper.java @@ -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 { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java index 710dee6e11..98bc7b4242 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/scheduler/RequirementsWatcher.java @@ -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();