MediaBrowser: Add a disconnecting state

By adding a CONNECT_STATE_DISCONNECTING, we could prevent
the ConnectionCallback to be called after disconnect().

Bug: 36106103
Test: manual using Support4Demos
Change-Id: Ia6f9864dce90b1e58437c07bbcaf6300e162ab92
diff --git a/media/java/android/media/browse/MediaBrowser.java b/media/java/android/media/browse/MediaBrowser.java
index 7122eaf..5bf205e 100644
--- a/media/java/android/media/browse/MediaBrowser.java
+++ b/media/java/android/media/browse/MediaBrowser.java
@@ -87,10 +87,11 @@
      */
     public static final String EXTRA_PAGE_SIZE = "android.media.browse.extra.PAGE_SIZE";
 
-    private static final int CONNECT_STATE_DISCONNECTED = 0;
-    private static final int CONNECT_STATE_CONNECTING = 1;
-    private static final int CONNECT_STATE_CONNECTED = 2;
-    private static final int CONNECT_STATE_SUSPENDED = 3;
+    private static final int CONNECT_STATE_DISCONNECTING = 0;
+    private static final int CONNECT_STATE_DISCONNECTED = 1;
+    private static final int CONNECT_STATE_CONNECTING = 2;
+    private static final int CONNECT_STATE_CONNECTED = 3;
+    private static final int CONNECT_STATE_SUSPENDED = 4;
 
     private final Context mContext;
     private final ComponentName mServiceComponent;
@@ -213,6 +214,7 @@
         // It's ok to call this any state, because allowing this lets apps not have
         // to check isConnected() unnecessarily. They won't appreciate the extra
         // assertions for this. We do everything we can here to go back to a sane state.
+        mState = CONNECT_STATE_DISCONNECTING;
         mHandler.post(new Runnable() {
             @Override
             public void run() {
@@ -535,7 +537,7 @@
 
         // If we are connected, tell the service that we are watching. If we aren't connected,
         // the service will be told when we connect.
-        if (mState == CONNECT_STATE_CONNECTED) {
+        if (isConnected()) {
             try {
                 if (options == null) {
                     mServiceBinder.addSubscriptionDeprecated(parentId, mServiceCallbacks);
@@ -563,7 +565,7 @@
         // Tell the service if necessary.
         try {
             if (callback == null) {
-                if (mState == CONNECT_STATE_CONNECTED) {
+                if (isConnected()) {
                     mServiceBinder.removeSubscriptionDeprecated(parentId, mServiceCallbacks);
                     mServiceBinder.removeSubscription(parentId, null, mServiceCallbacks);
                 }
@@ -572,7 +574,7 @@
                 final List<Bundle> optionsList = sub.getOptionsList();
                 for (int i = callbacks.size() - 1; i >= 0; --i) {
                     if (callbacks.get(i) == callback) {
-                        if (mState == CONNECT_STATE_CONNECTED) {
+                        if (isConnected()) {
                             mServiceBinder.removeSubscription(
                                     parentId, callback.mToken, mServiceCallbacks);
                         }
@@ -597,6 +599,8 @@
      */
     private static String getStateLabel(int state) {
         switch (state) {
+            case CONNECT_STATE_DISCONNECTING:
+                return "CONNECT_STATE_DISCONNECTING";
             case CONNECT_STATE_DISCONNECTED:
                 return "CONNECT_STATE_DISCONNECTED";
             case CONNECT_STATE_CONNECTING: