package android.support.v4.media;

import android.content.ComponentName;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.os.RemoteException;
import android.support.v4.app.e;
import android.support.v4.media.MediaBrowserCompat.a;
import android.support.v4.media.MediaBrowserCompat.b;
import android.support.v4.media.MediaBrowserCompat.d;
import android.support.v4.media.MediaBrowserCompat.i;
import android.support.v4.media.MediaBrowserCompat.j;
import android.support.v4.media.MediaBrowserCompat.k;
import android.support.v4.media.MediaBrowserCompat.l;
import android.support.v4.media.session.MediaSessionCompat.Token;
import java.util.List;
import java.util.Map.Entry;

class MediaBrowserCompat$h implements d, i {
    final Bundle HR;
    final a HS = new a(this);
    private final android.support.v4.f.a<String, k> HT = new android.support.v4.f.a();
    j HV;
    Messenger HW;
    private Token HX;
    final ComponentName HY;
    final b HZ;
    a Ia;
    private String Ib;
    final Context mContext;
    private Bundle mExtras;
    int mState = 1;

    public MediaBrowserCompat$h(Context context, ComponentName componentName, b bVar) {
        if (context == null) {
            throw new IllegalArgumentException("context must not be null");
        } else if (componentName == null) {
            throw new IllegalArgumentException("service component must not be null");
        } else if (bVar == null) {
            throw new IllegalArgumentException("connection callback must not be null");
        } else {
            this.mContext = context;
            this.HY = componentName;
            this.HZ = bVar;
            this.HR = null;
        }
    }

    public final void connect() {
        if (this.mState == 0 || this.mState == 1) {
            this.mState = 2;
            this.HS.post(new 1(this));
            return;
        }
        throw new IllegalStateException("connect() called while neigther disconnecting nor disconnected (state=" + av(this.mState) + ")");
    }

    public final void disconnect() {
        this.mState = 0;
        this.HS.post(new Runnable() {
            public final void run() {
                if (MediaBrowserCompat$h.this.HW != null) {
                    try {
                        MediaBrowserCompat$h.this.HV.a(2, null, MediaBrowserCompat$h.this.HW);
                    } catch (RemoteException e) {
                        new StringBuilder("RemoteException during connect for ").append(MediaBrowserCompat$h.this.HY);
                    }
                }
                int i = MediaBrowserCompat$h.this.mState;
                MediaBrowserCompat$h.this.dI();
                if (i != 0) {
                    MediaBrowserCompat$h.this.mState = i;
                }
                if (MediaBrowserCompat.DEBUG) {
                    MediaBrowserCompat$h.this.dump();
                }
            }
        });
    }

    /* Access modifiers changed, original: final */
    public final void dI() {
        if (this.Ia != null) {
            this.mContext.unbindService(this.Ia);
        }
        this.mState = 1;
        this.Ia = null;
        this.HV = null;
        this.HW = null;
        this.HS.a(null);
        this.Ib = null;
        this.HX = null;
    }

    public final void a(Messenger messenger, String str, Token token, Bundle bundle) {
        if (!a(messenger, "onConnect")) {
            return;
        }
        if (this.mState != 2) {
            new StringBuilder("onConnect from service while mState=").append(av(this.mState)).append("... ignoring");
            return;
        }
        this.Ib = str;
        this.HX = token;
        this.mExtras = bundle;
        this.mState = 3;
        if (MediaBrowserCompat.DEBUG) {
            dump();
        }
        this.HZ.onConnected();
        try {
            for (Entry entry : this.HT.entrySet()) {
                String str2 = (String) entry.getKey();
                k kVar = (k) entry.getValue();
                List list = kVar.mO;
                List list2 = kVar.Ii;
                int i = 0;
                while (true) {
                    int i2 = i;
                    if (i2 < list.size()) {
                        j jVar = this.HV;
                        IBinder iBinder = ((l) list.get(i2)).BN;
                        Bundle bundle2 = (Bundle) list2.get(i2);
                        Messenger messenger2 = this.HW;
                        Bundle bundle3 = new Bundle();
                        bundle3.putString("data_media_item_id", str2);
                        e.a(bundle3, "data_callback_token", iBinder);
                        bundle3.putBundle("data_options", bundle2);
                        jVar.a(3, bundle3, messenger2);
                        i = i2 + 1;
                    }
                }
            }
        } catch (RemoteException e) {
        }
    }

    public final void b(Messenger messenger) {
        new StringBuilder("onConnectFailed for ").append(this.HY);
        if (!a(messenger, "onConnectFailed")) {
            return;
        }
        if (this.mState != 2) {
            new StringBuilder("onConnect from service while mState=").append(av(this.mState)).append("... ignoring");
            return;
        }
        dI();
        this.HZ.onConnectionFailed();
    }

    public final void a(Messenger messenger, String str, Bundle bundle) {
        if (a(messenger, "onLoadChildren")) {
            if (MediaBrowserCompat.DEBUG) {
                new StringBuilder("onLoadChildren for ").append(this.HY).append(" id=").append(str);
            }
            k kVar = (k) this.HT.get(str);
            if (kVar == null) {
                boolean z = MediaBrowserCompat.DEBUG;
            } else {
                kVar.c(this.mContext, bundle);
            }
        }
    }

    private static String av(int i) {
        switch (i) {
            case 0:
                return "CONNECT_STATE_DISCONNECTING";
            case 1:
                return "CONNECT_STATE_DISCONNECTED";
            case 2:
                return "CONNECT_STATE_CONNECTING";
            case 3:
                return "CONNECT_STATE_CONNECTED";
            case 4:
                return "CONNECT_STATE_SUSPENDED";
            default:
                return "UNKNOWN/".concat(String.valueOf(i));
        }
    }

    private boolean a(Messenger messenger, String str) {
        if (this.HW == messenger && this.mState != 0 && this.mState != 1) {
            return true;
        }
        if (!(this.mState == 0 || this.mState == 1)) {
            new StringBuilder().append(str).append(" for ").append(this.HY).append(" with mCallbacksMessenger=").append(this.HW).append(" this=").append(this);
        }
        return false;
    }

    /* Access modifiers changed, original: final */
    public final void dump() {
        new StringBuilder("  mServiceComponent=").append(this.HY);
        new StringBuilder("  mCallback=").append(this.HZ);
        new StringBuilder("  mRootHints=").append(this.HR);
        new StringBuilder("  mState=").append(av(this.mState));
        new StringBuilder("  mServiceConnection=").append(this.Ia);
        new StringBuilder("  mServiceBinderWrapper=").append(this.HV);
        new StringBuilder("  mCallbacksMessenger=").append(this.HW);
        new StringBuilder("  mRootId=").append(this.Ib);
        new StringBuilder("  mMediaSessionToken=").append(this.HX);
    }

    public final Token dH() {
        Object obj;
        if (this.mState == 3) {
            obj = 1;
        } else {
            obj = null;
        }
        if (obj != null) {
            return this.HX;
        }
        throw new IllegalStateException("getSessionToken() called while not connected(state=" + this.mState + ")");
    }
}
