-
Notifications
You must be signed in to change notification settings - Fork 314
/
MediaLibraryServiceLegacyStub.java
707 lines (658 loc) · 27.3 KB
/
MediaLibraryServiceLegacyStub.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
* 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 androidx.media3.session;
import static android.support.v4.media.MediaBrowserCompat.EXTRA_PAGE;
import static android.support.v4.media.MediaBrowserCompat.EXTRA_PAGE_SIZE;
import static androidx.media.utils.MediaConstants.BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED;
import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Assertions.checkStateNotNull;
import static androidx.media3.common.util.Util.castNonNull;
import static androidx.media3.common.util.Util.postOrRun;
import static androidx.media3.session.LibraryResult.RESULT_SUCCESS;
import static androidx.media3.session.MediaUtils.TRANSACTION_SIZE_LIMIT_IN_BYTES;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.BadParcelableException;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v4.media.MediaBrowserCompat;
import android.text.TextUtils;
import androidx.annotation.GuardedBy;
import androidx.annotation.Nullable;
import androidx.core.util.ObjectsCompat;
import androidx.media.MediaBrowserServiceCompat;
import androidx.media.MediaSessionManager.RemoteUserInfo;
import androidx.media3.common.MediaItem;
import androidx.media3.common.MediaMetadata;
import androidx.media3.common.util.ConditionVariable;
import androidx.media3.common.util.Log;
import androidx.media3.common.util.NullableType;
import androidx.media3.common.util.Util;
import androidx.media3.session.MediaLibraryService.LibraryParams;
import androidx.media3.session.MediaSession.ControllerCb;
import androidx.media3.session.MediaSession.ControllerInfo;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.AsyncFunction;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.common.util.concurrent.SettableFuture;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* Implementation of {@link MediaBrowserServiceCompat} for interoperability between {@link
* MediaLibraryService} and {@link MediaBrowserCompat}.
*/
/* package */ class MediaLibraryServiceLegacyStub extends MediaSessionServiceLegacyStub {
private static final String TAG = "MLSLegacyStub";
private final ControllerCb browserLegacyCbForBroadcast;
private final MediaLibrarySessionImpl librarySessionImpl;
/** Creates a new instance. Caller must call {@link #initialize} to the instance. */
public MediaLibraryServiceLegacyStub(MediaLibrarySessionImpl session) {
super(session);
librarySessionImpl = session;
browserLegacyCbForBroadcast = new BrowserLegacyCbForBroadcast();
}
@Override
@Nullable
public BrowserRoot onGetRoot(
String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
@Nullable BrowserRoot browserRoot = super.onGetRoot(clientPackageName, clientUid, rootHints);
if (browserRoot == null) {
return null;
}
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
return null;
}
if (!getConnectedControllersManager()
.isSessionCommandAvailable(
controller, SessionCommand.COMMAND_CODE_LIBRARY_GET_LIBRARY_ROOT)) {
return null;
}
@Nullable
LibraryParams params =
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), rootHints);
AtomicReference<ListenableFuture<LibraryResult<MediaItem>>> futureReference =
new AtomicReference<>();
ConditionVariable haveFuture = new ConditionVariable();
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
futureReference.set(librarySessionImpl.onGetLibraryRootOnHandler(controller, params));
haveFuture.open();
});
@Nullable LibraryResult<MediaItem> result = null;
try {
haveFuture.block();
result = checkNotNull(futureReference.get().get(), "LibraryResult must not be null");
} catch (CancellationException | ExecutionException | InterruptedException e) {
Log.e(TAG, "Couldn't get a result from onGetLibraryRoot", e);
}
if (result != null && result.resultCode == RESULT_SUCCESS && result.value != null) {
@Nullable
Bundle extras =
result.params != null
? LegacyConversions.convertToRootHints(result.params)
: new Bundle();
boolean isSearchSessionCommandAvailable =
getConnectedControllersManager()
.isSessionCommandAvailable(controller, SessionCommand.COMMAND_CODE_LIBRARY_SEARCH);
checkNotNull(extras)
.putBoolean(BROWSER_SERVICE_EXTRAS_KEY_SEARCH_SUPPORTED, isSearchSessionCommandAvailable);
return new BrowserRoot(result.value.mediaId, extras);
}
// No library root, but keep browser compat connected to allow getting session unless the
// `Callback` implementation has not returned a `RESULT_SUCCESS`.
return result != null && result.resultCode != RESULT_SUCCESS
? null
: MediaUtils.defaultBrowserRoot;
}
// TODO(b/192455639): Optimize potential multiple calls of
// MediaBrowserCompat.SubscriptionCallback#onChildrenLoaded() with the same
// content.
@SuppressLint("RestrictedApi")
@Override
public void onSubscribe(String id, Bundle option) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
return;
}
if (TextUtils.isEmpty(id)) {
Log.w(TAG, "onSubscribe(): Ignoring empty id from " + controller);
return;
}
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
if (!getConnectedControllersManager()
.isSessionCommandAvailable(
controller, SessionCommand.COMMAND_CODE_LIBRARY_SUBSCRIBE)) {
return;
}
@Nullable
LibraryParams params =
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), option);
ignoreFuture(librarySessionImpl.onSubscribeOnHandler(controller, id, params));
});
}
@SuppressLint("RestrictedApi")
@Override
public void onUnsubscribe(String id) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
return;
}
if (TextUtils.isEmpty(id)) {
Log.w(TAG, "onUnsubscribe(): Ignoring empty id from " + controller);
return;
}
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
if (!getConnectedControllersManager()
.isSessionCommandAvailable(
controller, SessionCommand.COMMAND_CODE_LIBRARY_UNSUBSCRIBE)) {
return;
}
ignoreFuture(librarySessionImpl.onUnsubscribeOnHandler(controller, id));
});
}
@Override
public void onLoadChildren(String parentId, Result<List<MediaBrowserCompat.MediaItem>> result) {
onLoadChildren(parentId, result, /* options= */ null);
}
@Override
public void onLoadChildren(
String parentId,
Result<List<MediaBrowserCompat.MediaItem>> result,
@Nullable Bundle options) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
result.sendResult(/* result= */ null);
return;
}
if (TextUtils.isEmpty(parentId)) {
Log.w(TAG, "onLoadChildren(): Ignoring empty parentId from " + controller);
result.sendResult(/* result= */ null);
return;
}
result.detach();
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
if (!getConnectedControllersManager()
.isSessionCommandAvailable(
controller, SessionCommand.COMMAND_CODE_LIBRARY_GET_CHILDREN)) {
result.sendResult(/* result= */ null);
return;
}
if (options != null) {
options.setClassLoader(librarySessionImpl.getContext().getClassLoader());
try {
int page = options.getInt(EXTRA_PAGE);
int pageSize = options.getInt(EXTRA_PAGE_SIZE);
if (page >= 0 && pageSize > 0) {
// Requesting the list of children through pagination.
@Nullable
LibraryParams params =
LegacyConversions.convertToLibraryParams(
librarySessionImpl.getContext(), options);
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetChildrenOnHandler(
controller, parentId, page, pageSize, params);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>>
browserItemsFuture =
Util.transformFutureAsync(
future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(result, browserItemsFuture);
return;
}
// Cannot distinguish onLoadChildren() why it's called either by
// {@link MediaBrowserCompat#subscribe()} or
// {@link MediaBrowserServiceCompat#notifyChildrenChanged}.
} catch (BadParcelableException e) {
// pass-through.
}
}
// A MediaBrowserCompat called loadChildren with no pagination option.
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetChildrenOnHandler(
controller,
parentId,
/* page= */ 0,
/* pageSize= */ Integer.MAX_VALUE,
/* params= */ null);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> browserItemsFuture =
Util.transformFutureAsync(future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(result, browserItemsFuture);
});
}
@Override
public void onLoadItem(String itemId, Result<MediaBrowserCompat.MediaItem> result) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
result.sendResult(/* result= */ null);
return;
}
if (TextUtils.isEmpty(itemId)) {
Log.w(TAG, "Ignoring empty itemId from " + controller);
result.sendResult(/* result= */ null);
return;
}
result.detach();
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
if (!getConnectedControllersManager()
.isSessionCommandAvailable(
controller, SessionCommand.COMMAND_CODE_LIBRARY_GET_ITEM)) {
result.sendResult(/* result= */ null);
return;
}
ListenableFuture<LibraryResult<MediaItem>> future =
librarySessionImpl.onGetItemOnHandler(controller, itemId);
ListenableFuture<MediaBrowserCompat.@NullableType MediaItem> browserItemFuture =
Util.transformFutureAsync(future, createMediaItemToBrowserItemAsyncFunction());
sendLibraryResultWithMediaItemWhenReady(result, browserItemFuture);
});
}
@Override
public void onSearch(
String query, @Nullable Bundle extras, Result<List<MediaBrowserCompat.MediaItem>> result) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
result.sendResult(/* result= */ null);
return;
}
if (TextUtils.isEmpty(query)) {
Log.w(TAG, "Ignoring empty query from " + controller);
result.sendResult(/* result= */ null);
return;
}
if (!(controller.getControllerCb() instanceof BrowserLegacyCb)) {
return;
}
result.detach();
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
if (!getConnectedControllersManager()
.isSessionCommandAvailable(controller, SessionCommand.COMMAND_CODE_LIBRARY_SEARCH)) {
result.sendResult(/* result= */ null);
return;
}
BrowserLegacyCb cb = (BrowserLegacyCb) checkStateNotNull(controller.getControllerCb());
cb.registerSearchRequest(controller, query, extras, result);
@Nullable
LibraryParams params =
LegacyConversions.convertToLibraryParams(librarySessionImpl.getContext(), extras);
ignoreFuture(librarySessionImpl.onSearchOnHandler(controller, query, params));
// Actual search result will be sent by notifySearchResultChanged().
});
}
@Override
public void onCustomAction(String action, Bundle extras, Result<Bundle> result) {
@Nullable ControllerInfo controller = getCurrentController();
if (controller == null) {
result.sendError(/* extras= */ null);
return;
}
result.detach();
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
SessionCommand command = new SessionCommand(action, /* extras= */ Bundle.EMPTY);
if (!getConnectedControllersManager().isSessionCommandAvailable(controller, command)) {
result.sendError(/* extras= */ null);
return;
}
ListenableFuture<SessionResult> future =
librarySessionImpl.onCustomCommandOnHandler(controller, command, extras);
sendCustomActionResultWhenReady(result, future);
});
}
@Override
public ControllerInfo createControllerInfo(RemoteUserInfo remoteUserInfo, Bundle rootHints) {
return new ControllerInfo(
remoteUserInfo,
ControllerInfo.LEGACY_CONTROLLER_VERSION,
ControllerInfo.LEGACY_CONTROLLER_INTERFACE_VERSION,
getMediaSessionManager().isTrustedForMediaControl(remoteUserInfo),
new BrowserLegacyCb(remoteUserInfo),
/* connectionHints= */ rootHints);
}
public ControllerCb getBrowserLegacyCbForBroadcast() {
return browserLegacyCbForBroadcast;
}
@Nullable
private ControllerInfo getCurrentController() {
return getConnectedControllersManager().getController(getCurrentBrowserInfo());
}
private static void sendCustomActionResultWhenReady(
Result<Bundle> result, ListenableFuture<SessionResult> future) {
future.addListener(
() -> {
try {
SessionResult sessionResult =
checkNotNull(future.get(), "SessionResult must not be null");
result.sendResult(sessionResult.extras);
} catch (CancellationException | ExecutionException | InterruptedException e) {
Log.w(TAG, "Custom action failed", e);
result.sendError(/* extras= */ null);
}
},
MoreExecutors.directExecutor());
}
private static void sendLibraryResultWithMediaItemWhenReady(
Result<MediaBrowserCompat.MediaItem> result,
ListenableFuture<MediaBrowserCompat.@NullableType MediaItem> future) {
future.addListener(
() -> {
try {
MediaBrowserCompat.MediaItem mediaItem = future.get();
result.sendResult(mediaItem);
} catch (CancellationException | ExecutionException | InterruptedException e) {
Log.w(TAG, "Library operation failed", e);
result.sendResult(/* result= */ null);
}
},
MoreExecutors.directExecutor());
}
private static void sendLibraryResultWithMediaItemsWhenReady(
Result<List<MediaBrowserCompat.MediaItem>> result,
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> future) {
future.addListener(
() -> {
try {
List<MediaBrowserCompat.MediaItem> mediaItems = future.get();
result.sendResult(
(mediaItems == null)
? null
: MediaUtils.truncateListBySize(mediaItems, TRANSACTION_SIZE_LIMIT_IN_BYTES));
} catch (CancellationException | ExecutionException | InterruptedException e) {
Log.w(TAG, "Library operation failed", e);
result.sendResult(/* result= */ null);
}
},
MoreExecutors.directExecutor());
}
private AsyncFunction<
LibraryResult<ImmutableList<MediaItem>>, @NullableType List<MediaBrowserCompat.MediaItem>>
createMediaItemsToBrowserItemsAsyncFunction() {
return result -> {
checkNotNull(result, "LibraryResult must not be null");
SettableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> outputFuture =
SettableFuture.create();
if (result.resultCode != RESULT_SUCCESS || result.value == null) {
outputFuture.set(null);
return outputFuture;
}
ImmutableList<MediaItem> mediaItems = result.value;
if (mediaItems.isEmpty()) {
outputFuture.set(new ArrayList<>());
return outputFuture;
}
List<@NullableType ListenableFuture<Bitmap>> bitmapFutures = new ArrayList<>();
outputFuture.addListener(
() -> {
if (outputFuture.isCancelled()) {
cancelAllFutures(bitmapFutures);
}
},
MoreExecutors.directExecutor());
final AtomicInteger resultCount = new AtomicInteger(0);
Runnable handleBitmapFuturesTask =
() -> {
int completedBitmapFutureCount = resultCount.incrementAndGet();
if (completedBitmapFutureCount == mediaItems.size()) {
handleBitmapFuturesAllCompletedAndSetOutputFuture(
bitmapFutures, mediaItems, outputFuture);
}
};
for (int i = 0; i < mediaItems.size(); i++) {
MediaItem mediaItem = mediaItems.get(i);
MediaMetadata metadata = mediaItem.mediaMetadata;
if (metadata.artworkData == null) {
bitmapFutures.add(null);
handleBitmapFuturesTask.run();
} else {
ListenableFuture<Bitmap> bitmapFuture =
librarySessionImpl.getBitmapLoader().decodeBitmap(metadata.artworkData);
bitmapFutures.add(bitmapFuture);
bitmapFuture.addListener(handleBitmapFuturesTask, MoreExecutors.directExecutor());
}
}
return outputFuture;
};
}
private void handleBitmapFuturesAllCompletedAndSetOutputFuture(
List<@NullableType ListenableFuture<Bitmap>> bitmapFutures,
List<MediaItem> mediaItems,
SettableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> outputFuture) {
List<MediaBrowserCompat.MediaItem> outputMediaItems = new ArrayList<>();
for (int i = 0; i < bitmapFutures.size(); i++) {
@Nullable ListenableFuture<Bitmap> future = bitmapFutures.get(i);
@Nullable Bitmap bitmap = null;
if (future != null) {
try {
bitmap = Futures.getDone(future);
} catch (CancellationException | ExecutionException e) {
Log.d(TAG, "Failed to get bitmap", e);
}
}
outputMediaItems.add(LegacyConversions.convertToBrowserItem(mediaItems.get(i), bitmap));
}
outputFuture.set(outputMediaItems);
}
private static <T> void cancelAllFutures(List<@NullableType ListenableFuture<T>> futures) {
for (int i = 0; i < futures.size(); i++) {
if (futures.get(i) != null) {
futures.get(i).cancel(/* mayInterruptIfRunning= */ false);
}
}
}
private AsyncFunction<LibraryResult<MediaItem>, MediaBrowserCompat.@NullableType MediaItem>
createMediaItemToBrowserItemAsyncFunction() {
return result -> {
checkNotNull(result, "LibraryResult must not be null");
SettableFuture<MediaBrowserCompat.@NullableType MediaItem> outputFuture =
SettableFuture.create();
if (result.resultCode != RESULT_SUCCESS || result.value == null) {
outputFuture.set(null);
return outputFuture;
}
MediaItem mediaItem = result.value;
MediaMetadata metadata = mediaItem.mediaMetadata;
if (metadata.artworkData == null) {
outputFuture.set(
LegacyConversions.convertToBrowserItem(mediaItem, /* artworkBitmap= */ null));
return outputFuture;
}
ListenableFuture<Bitmap> bitmapFuture =
librarySessionImpl.getBitmapLoader().decodeBitmap(metadata.artworkData);
outputFuture.addListener(
() -> {
if (outputFuture.isCancelled()) {
bitmapFuture.cancel(/* mayInterruptIfRunning= */ false);
}
},
MoreExecutors.directExecutor());
bitmapFuture.addListener(
() -> {
@Nullable Bitmap bitmap = null;
try {
bitmap = Futures.getDone(bitmapFuture);
} catch (CancellationException | ExecutionException e) {
Log.d(TAG, "failed to get bitmap", e);
}
outputFuture.set(LegacyConversions.convertToBrowserItem(mediaItem, bitmap));
},
MoreExecutors.directExecutor());
return outputFuture;
};
}
private static <T> void ignoreFuture(Future<T> unused) {
// no-op
}
private static class SearchRequest {
public final ControllerInfo controller;
public final RemoteUserInfo remoteUserInfo;
public final String query;
@Nullable public final Bundle extras;
public final Result<List<MediaBrowserCompat.MediaItem>> result;
public SearchRequest(
ControllerInfo controller,
RemoteUserInfo remoteUserInfo,
String query,
@Nullable Bundle extras,
Result<List<MediaBrowserCompat.MediaItem>> result) {
this.controller = controller;
this.remoteUserInfo = remoteUserInfo;
this.query = query;
this.extras = extras;
this.result = result;
}
}
private final class BrowserLegacyCb implements ControllerCb {
private final Object lock;
private final RemoteUserInfo remoteUserInfo;
@GuardedBy("lock")
private final List<SearchRequest> searchRequests;
public BrowserLegacyCb(RemoteUserInfo remoteUserInfo) {
// Initialize default values.
lock = new Object();
searchRequests = new ArrayList<>();
// Initialize members with params.
this.remoteUserInfo = remoteUserInfo;
}
@Override
public void onChildrenChanged(
int seq, String parentId, int itemCount, @Nullable LibraryParams params)
throws RemoteException {
@Nullable Bundle extras = params != null ? params.extras : null;
notifyChildrenChanged(remoteUserInfo, parentId, extras != null ? extras : Bundle.EMPTY);
}
@Override
public void onSearchResultChanged(
int seq, String query, int itemCount, @Nullable LibraryParams params)
throws RemoteException {
// In MediaLibrarySession/MediaBrowser, we have two different APIs for getting size of
// search result (and also starting search) and getting result.
// However, MediaBrowserService/MediaBrowserCompat only have one search API for getting
// search result.
List<SearchRequest> searchRequests = new ArrayList<>();
synchronized (lock) {
for (int i = this.searchRequests.size() - 1; i >= 0; i--) {
SearchRequest iter = this.searchRequests.get(i);
if (Util.areEqual(remoteUserInfo, iter.remoteUserInfo) && iter.query.equals(query)) {
searchRequests.add(iter);
this.searchRequests.remove(i);
}
}
if (searchRequests.size() == 0) {
return;
}
}
postOrRun(
librarySessionImpl.getApplicationHandler(),
() -> {
for (int i = 0; i < searchRequests.size(); i++) {
SearchRequest request = searchRequests.get(i);
int page = 0;
int pageSize = Integer.MAX_VALUE;
if (request.extras != null) {
try {
request.extras.setClassLoader(librarySessionImpl.getContext().getClassLoader());
page = request.extras.getInt(MediaBrowserCompat.EXTRA_PAGE, -1);
pageSize = request.extras.getInt(MediaBrowserCompat.EXTRA_PAGE_SIZE, -1);
} catch (BadParcelableException e) {
request.result.sendResult(/* result= */ null);
return;
}
}
if (page < 0 || pageSize < 1) {
page = 0;
pageSize = Integer.MAX_VALUE;
}
@Nullable
LibraryParams libraryParams =
LegacyConversions.convertToLibraryParams(
librarySessionImpl.getContext(), request.extras);
ListenableFuture<LibraryResult<ImmutableList<MediaItem>>> future =
librarySessionImpl.onGetSearchResultOnHandler(
request.controller, request.query, page, pageSize, libraryParams);
ListenableFuture<@NullableType List<MediaBrowserCompat.MediaItem>> mediaItemsFuture =
Util.transformFutureAsync(future, createMediaItemsToBrowserItemsAsyncFunction());
sendLibraryResultWithMediaItemsWhenReady(request.result, mediaItemsFuture);
}
});
}
private void registerSearchRequest(
ControllerInfo controller,
String query,
@Nullable Bundle extras,
Result<List<MediaBrowserCompat.MediaItem>> result) {
synchronized (lock) {
searchRequests.add(
new SearchRequest(controller, controller.getRemoteUserInfo(), query, extras, result));
}
}
@Override
public int hashCode() {
return ObjectsCompat.hash(remoteUserInfo);
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof BrowserLegacyCb)) {
return false;
}
BrowserLegacyCb other = (BrowserLegacyCb) obj;
return Util.areEqual(remoteUserInfo, other.remoteUserInfo);
}
}
private final class BrowserLegacyCbForBroadcast implements ControllerCb {
@Override
public void onChildrenChanged(
int seq, String parentId, int itemCount, @Nullable LibraryParams libraryParams)
throws RemoteException {
// This will trigger {@link MediaLibraryServiceLegacyStub#onLoadChildren}.
if (libraryParams == null || libraryParams.extras == null) {
notifyChildrenChanged(parentId);
} else {
notifyChildrenChanged(parentId, castNonNull(libraryParams.extras));
}
}
@Override
public void onSearchResultChanged(
int seq, String query, int itemCount, @Nullable LibraryParams params)
throws RemoteException {
// Shouldn't be called. If it's called, it's bug.
// This method in the base class is introduced to internally send return of
// {@link MediaLibrarySessionCallback#onSearchResultChanged}. However, for
// BrowserCompat, it should be done by {@link Result#sendResult} from
// {@link MediaLibraryServiceLegacyStub#onSearch} instead.
}
}
}