|
26 | 26 | import com.google.common.collect.ImmutableList;
|
27 | 27 | import com.google.common.util.concurrent.ListenableFuture;
|
28 | 28 | import com.google.common.util.concurrent.MoreExecutors;
|
| 29 | +import java.lang.ref.WeakReference; |
29 | 30 | import java.util.ArrayDeque;
|
30 | 31 | import java.util.Deque;
|
31 | 32 | import java.util.concurrent.atomic.AtomicBoolean;
|
@@ -62,14 +63,14 @@ public interface AsyncCommand {
|
62 | 63 | private final ArrayMap<ControllerInfo, ConnectedControllerRecord<T>> controllerRecords =
|
63 | 64 | new ArrayMap<>();
|
64 | 65 |
|
65 |
| - private final MediaSessionImpl sessionImpl; |
| 66 | + private final WeakReference<MediaSessionImpl> sessionImpl; |
66 | 67 |
|
67 | 68 | public ConnectedControllersManager(MediaSessionImpl session) {
|
68 | 69 | // Initialize default values.
|
69 | 70 | lock = new Object();
|
70 | 71 |
|
71 | 72 | // Initialize members with params.
|
72 |
| - sessionImpl = session; |
| 73 | + sessionImpl = new WeakReference<>(session); |
73 | 74 | }
|
74 | 75 |
|
75 | 76 | public void addController(
|
@@ -136,6 +137,10 @@ record = controllerRecords.remove(controllerInfo);
|
136 | 137 | }
|
137 | 138 |
|
138 | 139 | record.sequencedFutureManager.release();
|
| 140 | + @Nullable MediaSessionImpl sessionImpl = this.sessionImpl.get(); |
| 141 | + if (sessionImpl == null || sessionImpl.isReleased()) { |
| 142 | + return; |
| 143 | + } |
139 | 144 | postOrRun(
|
140 | 145 | sessionImpl.getApplicationHandler(),
|
141 | 146 | () -> {
|
@@ -214,8 +219,10 @@ public boolean isPlayerCommandAvailable(
|
214 | 219 | synchronized (lock) {
|
215 | 220 | info = controllerRecords.get(controllerInfo);
|
216 | 221 | }
|
| 222 | + @Nullable MediaSessionImpl sessionImpl = this.sessionImpl.get(); |
217 | 223 | return info != null
|
218 | 224 | && info.playerCommands.contains(commandCode)
|
| 225 | + && sessionImpl != null |
219 | 226 | && sessionImpl.getPlayerWrapper().getAvailableCommands().contains(commandCode);
|
220 | 227 | }
|
221 | 228 |
|
@@ -248,6 +255,10 @@ public void flushCommandQueue(ControllerInfo controllerInfo) {
|
248 | 255 |
|
249 | 256 | @GuardedBy("lock")
|
250 | 257 | private void flushCommandQueue(ConnectedControllerRecord<T> info) {
|
| 258 | + @Nullable MediaSessionImpl sessionImpl = this.sessionImpl.get(); |
| 259 | + if (sessionImpl == null) { |
| 260 | + return; |
| 261 | + } |
251 | 262 | AtomicBoolean continueRunning = new AtomicBoolean(true);
|
252 | 263 | while (continueRunning.get()) {
|
253 | 264 | continueRunning.set(false);
|
|
0 commit comments