MediaSession2: Fix random NPE in MediaBrowser2Test
MediaSessionService#getSession() may be null until the
MediaSessionService#onCreate(), and its session callback can be called
before that. (e.g. System service can try connecting to the session
in another thread)
Don't use MediaSessionService#getSession() when it's unclear whether
the MediaSessionService#onCreate() is finished.
Bug: 74177171
Test: Run MediaComponents test 10 times
Change-Id: I89f3d1a59f84f5d950a14a7b412cfbbdad2c8bee
diff --git a/media/java/android/media/MediaSessionService2.java b/media/java/android/media/MediaSessionService2.java
index 9032e0e..d352a86 100644
--- a/media/java/android/media/MediaSessionService2.java
+++ b/media/java/android/media/MediaSessionService2.java
@@ -173,10 +173,12 @@
/**
* Get instance of the {@link MediaSession2} that you've previously created with the
* {@link #onCreateSession} for this service.
+ * <p>
+ * This may be {@code null} before the {@link #onCreate()} is finished.
*
* @return created session
*/
- public final MediaSession2 getSession() {
+ public final @Nullable MediaSession2 getSession() {
return mProvider.getSession_impl();
}