Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f5f729eb authored by tonihei's avatar tonihei
Browse files

Reformat MediaBrowser.connect

Moving the core logic to its own connectInternal method.

Flag: EXEMPT refactor
Test: existing tests
Change-Id: I15fa160ab500e77d66fd97cb167d21a1ec4c9b31
parent 05e9f83e
Loading
Loading
Loading
Loading
+59 −57
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import android.os.ResultReceiver;
import android.service.media.IMediaBrowserService;
import android.service.media.IMediaBrowserServiceCallbacks;
import android.service.media.MediaBrowserService;
import android.service.media.MediaBrowserService.BrowserRoot;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
@@ -114,16 +115,18 @@ public final class MediaBrowser {
     * @param context The context.
     * @param serviceComponent The component name of the media browser service.
     * @param callback The connection callback.
     * @param rootHints An optional bundle of service-specific arguments to send
     * to the media browser service when connecting and retrieving the root id
     * for browsing, or null if none. The contents of this bundle may affect
     * the information returned when browsing.
     * @see android.service.media.MediaBrowserService.BrowserRoot#EXTRA_RECENT
     * @see android.service.media.MediaBrowserService.BrowserRoot#EXTRA_OFFLINE
     * @see android.service.media.MediaBrowserService.BrowserRoot#EXTRA_SUGGESTED
     * @param rootHints An optional bundle of service-specific arguments to send to the media
     *     browser service when connecting and retrieving the root id for browsing, or null if none.
     *     The contents of this bundle may affect the information returned when browsing.
     * @see BrowserRoot#EXTRA_RECENT
     * @see BrowserRoot#EXTRA_OFFLINE
     * @see BrowserRoot#EXTRA_SUGGESTED
     */
    public MediaBrowser(Context context, ComponentName serviceComponent,
            ConnectionCallback callback, Bundle rootHints) {
    public MediaBrowser(
            Context context,
            ComponentName serviceComponent,
            ConnectionCallback callback,
            Bundle rootHints) {
        if (context == null) {
            throw new IllegalArgumentException("context must not be null");
        }
@@ -153,54 +156,7 @@ public final class MediaBrowser {
        }

        mState = CONNECT_STATE_CONNECTING;
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if (mState == CONNECT_STATE_DISCONNECTING) {
                    return;
                }
                mState = CONNECT_STATE_CONNECTING;
                // TODO: remove this extra check.
                if (DBG) {
                    if (mServiceConnection != null) {
                        throw new RuntimeException("mServiceConnection should be null. Instead it"
                                + " is " + mServiceConnection);
                    }
                }
                if (mServiceBinder != null) {
                    throw new RuntimeException("mServiceBinder should be null. Instead it is "
                            + mServiceBinder);
                }
                if (mServiceCallbacks != null) {
                    throw new RuntimeException("mServiceCallbacks should be null. Instead it is "
                            + mServiceCallbacks);
                }

                final Intent intent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
                intent.setComponent(mServiceComponent);

                mServiceConnection = new MediaServiceConnection();

                boolean bound = false;
                try {
                    bound = mContext.bindService(intent, mServiceConnection,
                            Context.BIND_AUTO_CREATE | Context.BIND_INCLUDE_CAPABILITIES);
                } catch (Exception ex) {
                    Log.e(TAG, "Failed binding to service " + mServiceComponent);
                }

                if (!bound) {
                    // Tell them that it didn't work.
                    forceCloseConnection();
                    mCallback.onConnectionFailed();
                }

                if (DBG) {
                    Log.d(TAG, "connect...");
                    dump();
                }
            }
        });
        mHandler.post(this::connectInternal);
    }

    /**
@@ -479,6 +435,52 @@ public final class MediaBrowser {
        }
    }

    private void connectInternal() {
        if (mState == CONNECT_STATE_DISCONNECTING) {
            return;
        }
        mState = CONNECT_STATE_CONNECTING;
        // TODO: remove this extra check.
        if (DBG) {
            if (mServiceConnection != null) {
                throw new RuntimeException(
                        "mServiceConnection should be null. Instead it is " + mServiceConnection);
            }
        }
        if (mServiceBinder != null) {
            throw new RuntimeException(
                    "mServiceBinder should be null. Instead it is " + mServiceBinder);
        }
        if (mServiceCallbacks != null) {
            throw new RuntimeException(
                    "mServiceCallbacks should be null. Instead it is " + mServiceCallbacks);
        }

        final Intent intent = new Intent(MediaBrowserService.SERVICE_INTERFACE);
        intent.setComponent(mServiceComponent);

        mServiceConnection = new MediaServiceConnection();

        boolean bound = false;
        try {
            int bindServiceFlags = Context.BIND_AUTO_CREATE | Context.BIND_INCLUDE_CAPABILITIES;
            bound = mContext.bindService(intent, mServiceConnection, bindServiceFlags);
        } catch (Exception ex) {
            Log.e(TAG, "Failed binding to service " + mServiceComponent);
        }

        if (!bound) {
            // Tell them that it didn't work.
            forceCloseConnection();
            mCallback.onConnectionFailed();
        }

        if (DBG) {
            Log.d(TAG, "connect...");
            dump();
        }
    }

    private void subscribeInternal(String parentId, Bundle options, SubscriptionCallback callback) {
        // Check arguments.
        if (TextUtils.isEmpty(parentId)) {