[go: nahoru, domu]

Fix failing MediaBrowserTest in API >= 28

The previously submitted CL "149c421: Fix throwing Exception
in getCurrentBrowserInfo/getBrowserRootHints" had a bug, which resulted
the MediaBrowserTest started to fail in API >= 28.

The bug was calling getBrowserRootHints() in
MediaBrowserServiceImplApi28#setCurrentConnectionFromFramework(),
before the variable mConnection is set.

Since the bug was not caught due to the low readability of the code,
this CL not only fixes the bug, but also does some refactoring.

Bug: 120638640
Test: ./media/version-compat-tests/runtest.sh 4 (in API 28)
Change-Id: I10c887f70028103b6a122130acf318419b419336
diff --git a/media/src/main/java/androidx/media/MediaBrowserServiceCompat.java b/media/src/main/java/androidx/media/MediaBrowserServiceCompat.java
index fa534c4..2938c1d 100644
--- a/media/src/main/java/androidx/media/MediaBrowserServiceCompat.java
+++ b/media/src/main/java/androidx/media/MediaBrowserServiceCompat.java
@@ -184,7 +184,7 @@
     private @interface ResultFlags {
     }
 
-    final ConnectionRecord mUnknownConnectionFromFwk = new ConnectionRecord(
+    final ConnectionRecord mConnectionFromFwk = new ConnectionRecord(
             LEGACY_CONTROLLER, UNKNOWN_PID, UNKNOWN_UID, null, null);
     final ArrayList<ConnectionRecord> mPendingConnections = new ArrayList<>();
     final ArrayMap<IBinder, ConnectionRecord> mConnections = new ArrayMap<>();
@@ -421,18 +421,9 @@
                             resultWrapper.detach();
                         }
                     };
-            setCurrentConnectionFromFramework(true);
+            mCurConnection = mConnectionFromFwk;
             MediaBrowserServiceCompat.this.onLoadChildren(parentId, result);
-            setCurrentConnectionFromFramework(false);
-        }
-
-        void setCurrentConnectionFromFramework(boolean set) {
-            if (set) {
-                // In API < 28, we cannot get the remote user info.
-                mCurConnection = mUnknownConnectionFromFwk;
-            } else {
-                mCurConnection = null;
-            }
+            mCurConnection = null;
         }
 
         void notifyChildrenChangedForFramework(final String parentId, final Bundle options) {
@@ -554,9 +545,9 @@
                             resultWrapper.detach();
                         }
                     };
-            setCurrentConnectionFromFramework(true);
+            mCurConnection = mConnectionFromFwk;
             MediaBrowserServiceCompat.this.onLoadItem(itemId, result);
-            setCurrentConnectionFromFramework(false);
+            mCurConnection = null;
         }
 
         class MediaBrowserServiceApi23 extends MediaBrowserServiceApi21 {
@@ -611,9 +602,9 @@
                             resultWrapper.detach();
                         }
                     };
-            setCurrentConnectionFromFramework(true);
+            mCurConnection = mConnectionFromFwk;
             MediaBrowserServiceCompat.this.onLoadChildren(parentId, result, options);
-            setCurrentConnectionFromFramework(false);
+            mCurConnection = null;
         }
 
         @Override
@@ -622,7 +613,7 @@
                 throw new IllegalStateException("This should be called inside of onGetRoot,"
                         + " onLoadChildren, onLoadItem, onSearch, or onCustomAction methods");
             }
-            if (mCurConnection == mUnknownConnectionFromFwk) {
+            if (mCurConnection == mConnectionFromFwk) {
                 return mServiceFwk.getBrowserRootHints();
             }
             return mCurConnection.rootHints == null ? null : new Bundle(mCurConnection.rootHints);
@@ -646,34 +637,25 @@
             public void onLoadChildren(String parentId, Result<List<MediaBrowser.MediaItem>> result,
                     Bundle options) {
                 MediaSessionCompat.ensureClassLoader(options);
-                setCurrentConnectionFromFramework(true);
+                mCurConnection = mConnectionFromFwk;
                 MediaBrowserServiceImplApi26.this.onLoadChildren(parentId,
                         new ResultWrapper<List<Parcel>>(result), options);
-                setCurrentConnectionFromFramework(false);
+                mCurConnection = null;
             }
         }
     }
 
     @RequiresApi(28)
     class MediaBrowserServiceImplApi28 extends MediaBrowserServiceImplApi26 {
-
-        @Override
-        void setCurrentConnectionFromFramework(boolean set) {
-            if (set) {
-                RemoteUserInfo info = new RemoteUserInfo(mServiceFwk.getCurrentBrowserInfo());
-                mCurConnection = new ConnectionRecord(info.getPackageName(), info.getPid(),
-                        info.getUid(), getBrowserRootHints(), null);
-            } else {
-                mCurConnection = null;
-            }
-        }
-
         @Override
         public RemoteUserInfo getCurrentBrowserInfo() {
             if (mCurConnection == null) {
                 throw new IllegalStateException("This should be called inside of onGetRoot,"
                         + " onLoadChildren, onLoadItem, onSearch, or onCustomAction methods");
             }
+            if (mCurConnection == mConnectionFromFwk) {
+                return new RemoteUserInfo(mServiceFwk.getCurrentBrowserInfo());
+            }
             return mCurConnection.browserInfo;
         }
     }