Exploit database

GH-14fb583d3d1bd2da10251bb1a37b93d6

gitchw/ghostlock-cve-2026-43499

GitHubNot listedPoC
Download repository ZIP

Sources for CVE-2026-43499

166 sources

Preferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source

Repository files25

Files from this repository share one source choice. Supporting files may cover other vulnerabilities.

workspace/decomp/HeyOTA/sources/com/heytap/accessory/BaseAgent.java

Java · 53,509 bytes
package com.heytap.accessory;

import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import com.heytap.accessory.BaseAdapter;
import com.heytap.accessory.BaseSocket;
import com.heytap.accessory.api.IPeerAgentAuthCallback;
import com.heytap.accessory.api.IPeerAgentCallback;
import com.heytap.accessory.bean.AuthenticationToken;
import com.heytap.accessory.bean.GeneralException;
import com.heytap.accessory.bean.PeerAgent;
import com.heytap.accessory.bean.SdkUnsupportedException;
import com.heytap.accessory.bean.ServiceProfile;
import com.heytap.accessory.constant.AFConstants;
import com.heytap.accessory.logging.SdkLog;
import com.heytap.accessory.utils.ConfigUtil;
import com.heytap.accessory.utils.SdkConfig;
import com.heytap.accessory.utils.buffer.BufferPool;
import java.lang.Thread;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/* loaded from: classes.dex */
public abstract class BaseAgent extends Service {
    public static final int AUTHENTICATION_FAILURE_PEER_AGENT_NOT_SUPPORTED = 10015;
    public static final int AUTHENTICATION_FAILURE_TOKEN_NOT_GENERATED = 10014;
    public static final int AUTHENTICATION_SUCCESS = 0;
    public static final int CONNECTION_ALREADY_EXIST = 10005;
    public static final int CONNECTION_DUPLICATE_REQUEST = 10009;
    public static final int CONNECTION_FAILURE_ACC_DORMANT = 10018;
    public static final int CONNECTION_FAILURE_CHANNELID_MISMATCH = 10011;
    public static final int CONNECTION_FAILURE_DEVICE_UNREACHABLE = 10004;
    public static final int CONNECTION_FAILURE_INVALID_PEERAGENT = 10008;
    public static final int CONNECTION_FAILURE_LOCAL_AGENT_NOT_FOUND = 10017;
    public static final int CONNECTION_FAILURE_NETWORK = 10012;
    public static final int CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE = 10006;
    public static final int CONNECTION_FAILURE_PEERAGENT_REJECTED = 10007;
    public static final int CONNECTION_FAILURE_SERVICE_LIMIT_REACHED = 10010;
    public static final int CONNECTION_SUCCESS = 0;
    private static final int DEFAULT_GET_AGENT_ID_RETRY_COUNT = 2;
    public static final int ERROR_CONNECTION_INVALID_PARAM = 10013;
    public static final int ERROR_FATAL = 20001;
    public static final int ERROR_PERMISSION_DENIED = 20003;
    public static final int ERROR_PERMISSION_FAILED = 20004;
    public static final int ERROR_SDK_NOT_INITIALIZED = 20002;
    public static final int FIND_PEER_DEVICE_NOT_CONNECTED = 10001;
    public static final int FIND_PEER_DUPLICATE_REQUEST = 10003;
    public static final int FIND_PEER_SERVICE_NOT_FOUND = 10002;
    public static final int FIND_PEER_TIMEOUT = 10010;
    public static final int ON_PEER_INSTALLED = 0;
    public static final int ON_PEER_UNINSTALLED = 1;
    public static final int PEER_AGENT_AVAILABLE = 1;
    public static final int PEER_AGENT_FOUND = 0;
    public static final int PEER_AGENT_UNAVAILABLE = 2;
    public static final int SERVICE_RECORD_NOT_FOUND = 10016;
    private static final String TAG = "BaseAgent";
    BaseAdapter mAdapter;
    private b mAgentCallback;
    private String mAgentId;
    c mBackgroundWorker;
    private d mConnectionCallback;
    private BaseMessage mMessage;
    private String mName;
    private PeerAgentCallback mPeerAgentCallback;
    private AuthenticationCallback mPeerAuthCallback;
    private Set<PeerAgent> mPendingRequests;
    private Class<? extends BaseSocket> mSocketImpl;
    private List<BaseSocket> mSuccessfulConnections;
    private ServiceProfile mServiceProfile = null;
    private int mGetAgentIdRetryCount = 0;

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class AuthenticationCallback extends IPeerAgentAuthCallback.Stub {
        /* synthetic */ AuthenticationCallback(BaseAgent baseAgent, a aVar) {
            this();
        }

        @Override // com.heytap.accessory.api.IPeerAgentAuthCallback
        public void onPeerAgentAuthenticated(Bundle bundle) {
            SdkLog.v(BaseAgent.TAG, "Received Authentication response");
            c cVar = BaseAgent.this.mBackgroundWorker;
            if (cVar == null) {
                SdkLog.w(BaseAgent.TAG, "onPeerAgentAuthenticated: mBackgroundWorker is null!");
                return;
            }
            Message obtainMessage = cVar.obtainMessage(10);
            obtainMessage.setData(bundle);
            BaseAgent.this.mBackgroundWorker.sendMessage(obtainMessage);
        }

        private AuthenticationCallback() {
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class PeerAgentCallback extends IPeerAgentCallback.Stub {
        /* synthetic */ PeerAgentCallback(BaseAgent baseAgent, a aVar) {
            this();
        }

        @Override // com.heytap.accessory.api.IPeerAgentCallback
        public void onPeerAgentUpdated(Bundle bundle) {
            SdkLog.v(BaseAgent.TAG, "Received peer agent update");
            bundle.setClassLoader(PeerAgent.class.getClassLoader());
            if (!bundle.containsKey(AFConstants.EXTRA_PEER_AGENTS)) {
                SdkLog.e(BaseAgent.TAG, "No peer agents in PeerAgent update callback!");
                return;
            }
            ArrayList<PeerAgent> parcelableArrayList = bundle.getParcelableArrayList(AFConstants.EXTRA_PEER_AGENTS);
            int i = bundle.getInt(AFConstants.EXTRA_PEER_AGENT_STATUS);
            if (parcelableArrayList == null) {
                SdkLog.e(BaseAgent.TAG, "Peer Update - invalid peer agent list from Accessory Framework");
                return;
            }
            if (i == 0 || i == 1) {
                SdkLog.i(BaseAgent.TAG, parcelableArrayList.size() + " Peer agent(s) updated for:" + PeerAgentCallback.class.getName());
                for (PeerAgent peerAgent : parcelableArrayList) {
                    SdkLog.i(BaseAgent.TAG, "Peer ID:" + peerAgent.getAgentId() + "Container Id:" + peerAgent.getAppName() + " Accessory" + peerAgent.getAccessory().getPeerId());
                }
                c cVar = BaseAgent.this.mBackgroundWorker;
                if (cVar == null) {
                    SdkLog.w(BaseAgent.TAG, "onPeerAgentUpdated: mBackgroundWorker is null!");
                    return;
                }
                Message obtainMessage = cVar.obtainMessage();
                obtainMessage.what = 4;
                if (i == 0) {
                    obtainMessage.arg1 = 1;
                } else {
                    obtainMessage.arg1 = 2;
                }
                obtainMessage.obj = parcelableArrayList.toArray(new PeerAgent[0]);
                BaseAgent.this.mBackgroundWorker.sendMessage(obtainMessage);
                return;
            }
            SdkLog.e(BaseAgent.TAG, "Peer Update - invalid peer status from Accessory Framework:" + i);
        }

        @Override // com.heytap.accessory.api.IPeerAgentCallback
        public void onPeerAgentsFound(Bundle bundle) {
            SdkLog.v(BaseAgent.TAG, "FindPeer response received.");
            bundle.setClassLoader(PeerAgent.class.getClassLoader());
            if (bundle.containsKey(AFConstants.EXTRA_ERROR_CODE)) {
                int i = bundle.getInt(AFConstants.EXTRA_ERROR_CODE);
                SdkLog.e(BaseAgent.TAG, "Peer Not Found(" + i + ") for: " + PeerAgentCallback.class.getName());
                c cVar = BaseAgent.this.mBackgroundWorker;
                if (cVar == null) {
                    SdkLog.w(BaseAgent.TAG, "onPeersAgentsFound: mBackgroundWorker is null!");
                    return;
                }
                Message obtainMessage = cVar.obtainMessage();
                obtainMessage.what = 3;
                obtainMessage.arg1 = i;
                BaseAgent.this.mBackgroundWorker.sendMessage(obtainMessage);
                return;
            }
            ArrayList parcelableArrayList = bundle.getParcelableArrayList(AFConstants.EXTRA_PEER_AGENTS);
            if (parcelableArrayList == null) {
                SdkLog.e(BaseAgent.TAG, "Find Peer - invalid response from Accessory Framework");
                return;
            }
            SdkLog.i(BaseAgent.TAG, parcelableArrayList.size() + " Peer agent(s) found for:" + PeerAgentCallback.class.getName());
            SdkLog.i(BaseAgent.TAG, "Peer agent(s) " + parcelableArrayList);
            c cVar2 = BaseAgent.this.mBackgroundWorker;
            if (cVar2 == null) {
                SdkLog.w(BaseAgent.TAG, "onPeerAgentsFound: mBackgroundWorker is null!");
                return;
            }
            Message obtainMessage2 = cVar2.obtainMessage();
            obtainMessage2.what = 3;
            obtainMessage2.arg1 = 0;
            obtainMessage2.obj = parcelableArrayList.toArray(new PeerAgent[parcelableArrayList.size()]);
            BaseAgent.this.mBackgroundWorker.sendMessage(obtainMessage2);
        }

        private PeerAgentCallback() {
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class a implements Thread.UncaughtExceptionHandler {

        /* renamed from: com.heytap.accessory.BaseAgent$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        class RunnableC0001a implements Runnable {

            /* renamed from: a, reason: collision with root package name */
            final /* synthetic */ Throwable f12a;

            RunnableC0001a(a aVar, Throwable th) {
                this.f12a = th;
            }

            @Override // java.lang.Runnable
            public void run() {
                throw new RuntimeException(this.f12a);
            }
        }

        a(BaseAgent baseAgent) {
        }

        @Override // java.lang.Thread.UncaughtExceptionHandler
        public void uncaughtException(Thread thread, Throwable th) {
            SdkLog.e(BaseAgent.TAG, "Exception in background thread:" + thread.getName(), th);
            new Handler(Looper.getMainLooper()).post(new RunnableC0001a(this, th));
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class b implements BaseAdapter.e {

        /* renamed from: a, reason: collision with root package name */
        private BaseAgent f13a;

        public b(BaseAgent baseAgent) {
            this.f13a = baseAgent;
        }

        @Override // com.heytap.accessory.BaseAdapter.e
        public void a() {
            c cVar = this.f13a.mBackgroundWorker;
            if (cVar == null) {
                SdkLog.w(BaseAgent.TAG, "onAgentRegistered: mBackgroundWorker is null!");
            } else {
                cVar.sendEmptyMessage(14);
            }
        }

        @Override // com.heytap.accessory.BaseAdapter.e
        public void b() {
            try {
                this.f13a.registerMexAgent();
            } catch (GeneralException e) {
                SdkLog.e(BaseAgent.TAG, "onFrameworkConnected() - Failed to register agent with message! " + e.getMessage());
            }
        }

        @Override // com.heytap.accessory.BaseAdapter.e
        public void c() {
            c cVar = this.f13a.mBackgroundWorker;
            if (cVar == null) {
                SdkLog.w(BaseAgent.TAG, "onFrameworkDisconnected: mBackgroundWorker is null!");
                return;
            }
            Message obtainMessage = cVar.obtainMessage(11);
            obtainMessage.arg1 = 20001;
            this.f13a.mBackgroundWorker.sendMessage(obtainMessage);
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    /* loaded from: classes.dex */
    public static class c extends Handler {

        /* renamed from: a, reason: collision with root package name */
        BaseAgent f14a;

        public c(BaseAgent baseAgent, Looper looper) {
            super(looper);
            this.f14a = baseAgent;
        }

        public void a() {
            getLooper().quit();
            this.f14a = null;
        }

        @Override // android.os.Handler
        public void handleMessage(Message message) {
            switch (message.what) {
                case 0:
                    try {
                        this.f14a.bindToFramework();
                        return;
                    } catch (GeneralException e) {
                        SdkLog.e(BaseAgent.TAG, "Binding to Accessory Framework failed", e);
                        this.f14a.handleError(e.getErrorCode(), null);
                        return;
                    }
                case 1:
                    this.f14a.registerService();
                    return;
                case 2:
                    this.f14a.requestPeerAgents();
                    return;
                case 3:
                    int i = message.arg1;
                    if (i == 0) {
                        BaseAgent baseAgent = this.f14a;
                        if (baseAgent instanceof NativeAgent) {
                            ((NativeAgent) baseAgent).onPeerFound(i, Arrays.asList((PeerAgent[]) message.obj));
                            return;
                        } else {
                            baseAgent.onFindPeerAgentsResponse((PeerAgent[]) message.obj, 0);
                            this.f14a.handleFindPeerErrorCode(0);
                            return;
                        }
                    }
                    this.f14a.onFindPeerAgentsResponse(null, i);
                    this.f14a.handleFindPeerErrorCode(message.arg1);
                    return;
                case 4:
                    this.f14a.onPeerAgentsUpdated((PeerAgent[]) message.obj, message.arg1);
                    this.f14a.handlePeerAgentUpdateErrorCode(message.arg1);
                    return;
                case 5:
                    this.f14a.notifyConnectionRequest((Intent) message.obj);
                    return;
                case 6:
                    this.f14a.requestConnection((PeerAgent) message.obj);
                    return;
                case 7:
                    this.f14a.acceptServiceConnectionInternal((PeerAgent) message.obj);
                    return;
                case 8:
                    this.f14a.rejectServiceConnectionInternal((PeerAgent) message.obj);
                    return;
                case 9:
                    this.f14a.requestPeerAuthInternal((PeerAgent) message.obj);
                    return;
                case 10:
                    this.f14a.handleAuthResponse(message.getData());
                    return;
                case 11:
                    Object obj = message.obj;
                    this.f14a.handleError(message.arg1, obj instanceof PeerAgent ? (PeerAgent) obj : null);
                    return;
                case 12:
                    Object obj2 = message.obj;
                    this.f14a.onServiceConnectionResponse(obj2 instanceof PeerAgent ? (PeerAgent) obj2 : null, null, message.arg1);
                    this.f14a.handleServiceConnectionErrorCode(message.arg1);
                    return;
                case 13:
                    this.f14a.cleanup();
                    return;
                case 14:
                    try {
                        this.f14a.loadAgentId();
                        return;
                    } catch (GeneralException e2) {
                        SdkLog.e(BaseAgent.TAG, "Retrieving agent id failed", e2);
                        this.f14a.handleError(e2.getErrorCode(), null);
                        return;
                    }
                case 15:
                    this.f14a.handleAccessoryStatusChanged((Intent) message.obj);
                    return;
                default:
                    SdkLog.w(BaseAgent.TAG, "Invalid msg received: " + message.what);
                    return;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public class d implements BaseSocket.d {
        /* synthetic */ d(BaseAgent baseAgent, a aVar) {
            this();
        }

        @Override // com.heytap.accessory.BaseSocket.d
        public void a(PeerAgent peerAgent, BaseSocket baseSocket) {
            BaseAgent.this.mSuccessfulConnections.add(baseSocket);
            SdkLog.d(BaseAgent.TAG, "Connection success with peer:" + peerAgent.getAgentId());
            BaseAgent.this.onServiceConnectionResponse(peerAgent, baseSocket, 0);
            BaseAgent.this.handleServiceConnectionErrorCode(0);
        }

        private d() {
        }

        @Override // com.heytap.accessory.BaseSocket.d
        public void a(PeerAgent peerAgent, int i) {
            if (i == 20001) {
                SdkLog.w(BaseAgent.TAG, "Framework disconnected during connection process!");
                BaseAgent.this.handleError(i, peerAgent);
                return;
            }
            c cVar = BaseAgent.this.mBackgroundWorker;
            if (cVar != null) {
                if (i == 10017) {
                    i = 10008;
                    BaseAgent.this.mBackgroundWorker.sendMessage(cVar.obtainMessage(1));
                }
                SdkLog.e(BaseAgent.TAG, "Connection attempt failed wih peer:" + peerAgent.getAgentId() + " reason:" + i);
                Message obtainMessage = BaseAgent.this.mBackgroundWorker.obtainMessage(12);
                obtainMessage.arg1 = i;
                obtainMessage.obj = peerAgent;
                BaseAgent.this.mBackgroundWorker.sendMessage(obtainMessage);
                return;
            }
            SdkLog.w(BaseAgent.TAG, "onConnectionFailure: mBackgroundWorker is null!");
        }
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public BaseAgent(String str, Class<? extends BaseSocket> cls) {
        if (str != null && !"".equalsIgnoreCase(str)) {
            validateSocketImplementation(cls);
            this.mName = str;
            this.mSocketImpl = cls;
            SdkLog.d(TAG, "Thread Name:" + this.mName + "BaseSocket Imple class:" + cls.getName());
            return;
        }
        throw new IllegalArgumentException("Invalid parameter name:" + str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void acceptServiceConnectionInternal(PeerAgent peerAgent) {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            handleError(20001, peerAgent);
        } else {
            instantiateSocket().acceptServiceConnection(localAgentId, peerAgent, this.mAdapter, this.mConnectionCallback);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void bindToFramework() throws GeneralException {
        this.mAdapter.registerAgentCallback(this.mAgentCallback);
        this.mAdapter.bindToFramework();
        loadAgentId();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void cleanup() {
        SdkLog.w(TAG, "Performing agent cleanup");
        cleanupConnections(false);
        String localAgentId = getLocalAgentId();
        if (localAgentId != null) {
            this.mAdapter.cleanupAgent(localAgentId);
        }
        this.mAdapter.unregisterAgentCallback(this.mAgentCallback);
        BaseMessage baseMessage = this.mMessage;
        if (baseMessage != null) {
            baseMessage.unregisterAgent();
        }
        c cVar = this.mBackgroundWorker;
        if (cVar != null) {
            cVar.a();
            this.mBackgroundWorker = null;
        }
    }

    private void cleanupConnections(boolean z) {
        synchronized (this.mSuccessfulConnections) {
            for (BaseSocket baseSocket : this.mSuccessfulConnections) {
                if (z) {
                    baseSocket.forceClose();
                } else {
                    baseSocket.close();
                }
            }
        }
        this.mSuccessfulConnections.clear();
        Initializer.clearSdkConfig();
    }

    private synchronized void fetchServiceProfile() {
        ConfigUtil defaultInstance = ConfigUtil.getDefaultInstance(getApplicationContext());
        if (defaultInstance != null) {
            ServiceProfile fetchServicesDescription = defaultInstance.fetchServicesDescription(getClass().getName());
            this.mServiceProfile = fetchServicesDescription;
            if (fetchServicesDescription == null) {
                SdkLog.e(TAG, "fetch service profile description failed !!");
            }
        } else {
            SdkLog.e(TAG, "config  util defualt instance  creation failed !!");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public synchronized void handleAccessoryStatusChanged(Intent intent) {
        intent.getIntExtra("accessoryStatus", 0);
    }

    private void handleAuthErrorCode(int i) {
        if (i == 0) {
            SdkLog.i(TAG, "onAuthenticationResponse() -> AUTHENTICATION_SUCCESS");
            return;
        }
        if (i == 10014) {
            SdkLog.i(TAG, "onAuthenticationResponse() -> AUTHENTICATION_FAILURE_TOKEN_NOT_GENERATED");
            return;
        }
        if (i != 10015) {
            SdkLog.w(TAG, "onAuthenticationResponse() errorCode: " + i);
            return;
        }
        SdkLog.i(TAG, "onAuthenticationResponse() -> AUTHENTICATION_FAILURE_PEER_AGENT_NOT_SUPPORTED");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleAuthResponse(Bundle bundle) {
        bundle.setClassLoader(PeerAgent.class.getClassLoader());
        byte[] byteArray = bundle.getByteArray(AFConstants.EXTRA_PEER_AUTH_KEY);
        int i = bundle.getInt(AFConstants.EXTRA_CERTIFICATE_TYPE);
        PeerAgent peerAgent = (PeerAgent) bundle.getParcelable(AFConstants.EXTRA_PEER_AGENT);
        long j = bundle.getLong(AFConstants.EXTRA_TRANSACTION_ID);
        if (peerAgent == null) {
            SdkLog.e(TAG, "Invalid response from framework! No peer agent in auth response.Ignoring response");
            return;
        }
        peerAgent.setTransactionId(j);
        int i2 = 0;
        if (byteArray == null) {
            i2 = 10014;
            SdkLog.e(TAG, "Authentication failed error:10014 Peer Id:" + peerAgent.getAgentId());
        } else {
            SdkLog.i(TAG, "Authentication success status: 0 for peer: " + peerAgent.getAgentId());
        }
        onAuthenticationResponse(peerAgent, new AuthenticationToken(i, byteArray), i2);
        handleAuthErrorCode(i2);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleFindPeerErrorCode(int i) {
        if (i != 0) {
            switch (i) {
                case 10001:
                    SdkLog.i(TAG, "onFindPeerAgentsResponse() -> FIND_PEER_DEVICE_NOT_CONNECTED");
                    return;
                case 10002:
                    SdkLog.i(TAG, "onFindPeerAgentsResponse() -> FIND_PEER_SERVICE_NOT_FOUND");
                    return;
                case 10003:
                    SdkLog.i(TAG, "onFindPeerAgentsResponse() -> FIND_PEER_DUPLICATE_REQUEST");
                    return;
                default:
                    SdkLog.w(TAG, "onFindPeerAgentsResponse() errorCode: " + i);
                    return;
            }
        }
        SdkLog.i(TAG, "onFindPeerAgentsResponse() -> PEER_AGENT_FOUND");
    }

    private void handleInvalidPeerAction(PeerAgent peerAgent) {
        synchronized (this.mPendingRequests) {
            Iterator<PeerAgent> it = this.mPendingRequests.iterator();
            while (it.hasNext()) {
                rejectServiceConnectionInternal(it.next());
            }
            c cVar = this.mBackgroundWorker;
            if (cVar != null) {
                Message obtainMessage = cVar.obtainMessage(11);
                obtainMessage.arg1 = 10008;
                obtainMessage.obj = peerAgent;
                this.mBackgroundWorker.sendMessage(obtainMessage);
            } else {
                SdkLog.w(TAG, "handle Invalid PeerAction: mBackgroundWorker is null!");
            }
        }
    }

    private void handleOnErrorCode(int i) {
        if (i != 10013) {
            switch (i) {
                case 20001:
                    SdkLog.i(TAG, "onError() -> ERROR_FATAL");
                    return;
                case 20002:
                    SdkLog.i(TAG, "onError() -> ERROR_SDK_NOT_INITIALIZED");
                    return;
                case 20003:
                    SdkLog.i(TAG, "onError() -> ERROR_PERMISSION_DENIED");
                    return;
                case 20004:
                    SdkLog.i(TAG, "onError() -> ERROR_PERMISSION_FAILED");
                    return;
                default:
                    SdkLog.w(TAG, "onError() errorCode: " + i);
                    return;
            }
        }
        SdkLog.i(TAG, "onError() -> ERROR_CONNECTION_INVALID_PARAM");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handlePeerAgentUpdateErrorCode(int i) {
        if (i == 1) {
            SdkLog.i(TAG, "onPeerAgentUpdated() -> PEER_AGENT_AVAILABLE");
            return;
        }
        if (i != 2) {
            SdkLog.w(TAG, "onPeerAgentUpdated() errorCode: " + i);
            return;
        }
        SdkLog.i(TAG, "onPeerAgentUpdated() -> PEER_AGENT_UNAVAILABLE");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void handleServiceConnectionErrorCode(int i) {
        if (i == 0) {
            SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_SUCCESS");
            return;
        }
        if (i == 10012) {
            SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_NETWORK");
            return;
        }
        if (i != 10018) {
            switch (i) {
                case 10004:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_DEVICE_UNREACHABLE");
                    return;
                case 10005:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_ALREADY_EXIST");
                    return;
                case 10006:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_PEERAGENT_NO_RESPONSE");
                    return;
                case 10007:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_PEERAGENT_REJECTED");
                    return;
                case 10008:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_INVALID_PEER_AGENT");
                    return;
                case 10009:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_DUPLICATE_REQUEST");
                    return;
                case 10010:
                    SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_SERVICE_LIMIT_REACHED");
                    return;
                default:
                    SdkLog.w(TAG, "onServiceConnectionResponse() errorCode: " + i);
                    return;
            }
        }
        SdkLog.i(TAG, "onServiceConnectionResponse() -> CONNECTION_FAILURE_ACC_DORMANT");
    }

    private BaseSocket instantiateSocket() {
        validateSocketImplementation(this.mSocketImpl);
        try {
            SdkLog.d(TAG, "Instantiating BaseSocket: " + this.mSocketImpl.getName());
            if (!Modifier.toString(this.mSocketImpl.getModifiers()).contains("static") && this.mSocketImpl.getEnclosingClass() != null && BaseAgent.class.isAssignableFrom(this.mSocketImpl.getEnclosingClass())) {
                Class<? extends BaseSocket> cls = this.mSocketImpl;
                Constructor<? extends BaseSocket> declaredConstructor = cls.getDeclaredConstructor(cls.getEnclosingClass());
                declaredConstructor.setAccessible(true);
                return declaredConstructor.newInstance(this);
            }
            Constructor<? extends BaseSocket> declaredConstructor2 = this.mSocketImpl.getDeclaredConstructor(null);
            declaredConstructor2.setAccessible(true);
            return declaredConstructor2.newInstance(null);
        } catch (IllegalAccessException | IllegalArgumentException | InstantiationException | NoSuchMethodException | InvocationTargetException e) {
            SdkLog.e(TAG, "Invalid implementation of BaseSocket. Provider a public default constructor." + e.getClass().getSimpleName() + " " + e.getMessage());
            throw new RuntimeException("Invalid implementation of BaseSocket. Provider a public default constructor.");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void loadAgentId() throws GeneralException {
        SharedPreferences sharedPreferences;
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            handleError(20001, null);
            return;
        }
        if (Build.VERSION.SDK_INT > 26) {
            sharedPreferences = createDeviceProtectedStorageContext().getSharedPreferences("AccessoryPreferences", 0);
        } else {
            sharedPreferences = getApplicationContext().getSharedPreferences("AccessoryPreferences", 0);
        }
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString(localAgentId, getClass().getName());
        edit.putString(getClass().getName(), localAgentId);
        SdkLog.d(TAG, "save AgentId className:" + getClass().getName() + ",agentId: " + localAgentId);
        edit.apply();
        this.mAgentId = localAgentId;
        registerMexAgent();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyConnectionRequest(Intent intent) {
        if (intent == null) {
            SdkLog.e(TAG, "Invalid service connection indication.Intent:null.Ignoring reqeuset");
            return;
        }
        long longExtra = intent.getLongExtra(AFConstants.EXTRA_TRANSACTION_ID, 0L);
        PeerAgent peerAgent = (PeerAgent) intent.getParcelableExtra(AFConstants.EXTRA_PEER_AGENT);
        String stringExtra = intent.getStringExtra(AFConstants.EXTRA_AGENT_ID);
        if (peerAgent == null) {
            SdkLog.e(TAG, "Invalid initiator peer agent:" + peerAgent + ". Ignoring connection request");
            return;
        }
        if (stringExtra == null) {
            SdkLog.e(TAG, "Invalid local agent Id:" + stringExtra + ".Ignoring connection request");
            return;
        }
        peerAgent.setTransactionId(longExtra);
        SdkLog.i(TAG, "Connection initiated by peer: " + peerAgent.getAgentId() + " on Accessory: " + peerAgent.getAccessory().getPeerId() + " Transaction: " + longExtra);
        this.mPendingRequests.add(peerAgent);
        onServiceConnectionRequested(peerAgent);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void registerMexAgent() throws GeneralException {
        String str;
        BaseMessage baseMessage = this.mMessage;
        if (baseMessage == null || (str = this.mAgentId) == null) {
            return;
        }
        baseMessage.registerAgent(str);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void registerService() {
        com.heytap.accessory.a aVar = new com.heytap.accessory.a(getApplicationContext());
        Future b2 = aVar.b();
        aVar.c();
        try {
            b2.get();
        } catch (InterruptedException unused) {
            SdkLog.e(TAG, "Regisration failed! : InterruptedException");
        } catch (ExecutionException unused2) {
            SdkLog.e(TAG, "Registration failed! : ExecutionException");
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void rejectServiceConnectionInternal(PeerAgent peerAgent) {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            handleError(20001, peerAgent);
            return;
        }
        try {
            this.mAdapter.rejectServiceConnection(localAgentId, peerAgent, peerAgent.getTransactionId());
        } catch (GeneralException e) {
            SdkLog.e(TAG, "Failed to reject Service connection!", e);
            handleError(e.getErrorCode(), peerAgent);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void requestConnection(PeerAgent peerAgent) {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            SdkLog.e(TAG, "Failed to retrieve service description.Ignoring service connection request");
            handleError(20001, peerAgent);
        } else {
            instantiateSocket().initiateServiceconnection(localAgentId, peerAgent, this.mAdapter, this.mConnectionCallback);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void requestPeerAgents() {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            SdkLog.w(TAG, "getLocalAgentId failed!");
            onFindPeerAgentsResponse(null, 10004);
            return;
        }
        try {
            int findPeerAgents = this.mAdapter.findPeerAgents(localAgentId, this.mPeerAgentCallback);
            if (findPeerAgents == 0) {
                SdkLog.d(TAG, "Find peer request successfully enqueued.");
            } else {
                SdkLog.w(TAG, "Find peer request failed:" + findPeerAgents + " for service " + getClass().getName());
                onFindPeerAgentsResponse(null, findPeerAgents);
                handleFindPeerErrorCode(findPeerAgents);
            }
        } catch (GeneralException e) {
            SdkLog.e(TAG, "Find Peer request failed!");
            handleError(e.getErrorCode(), null);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void requestPeerAuthInternal(PeerAgent peerAgent) {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            handleError(20001, peerAgent);
            return;
        }
        try {
            int authenticatePeeragent = this.mAdapter.authenticatePeeragent(localAgentId, peerAgent, this.mPeerAuthCallback, peerAgent.getTransactionId());
            if (authenticatePeeragent == 0) {
                SdkLog.i(TAG, "Auth. request for peer: " + peerAgent.getAgentId() + " done successfully");
            } else {
                SdkLog.e(TAG, "Auth. request for peer: " + peerAgent.getAgentId() + " failed as reason: " + authenticatePeeragent);
                onAuthenticationResponse(peerAgent, null, authenticatePeeragent);
                handleAuthErrorCode(authenticatePeeragent);
            }
        } catch (GeneralException e) {
            SdkLog.e(TAG, "Failed to request peer authentication!", e);
            handleError(e.getErrorCode(), peerAgent);
        }
    }

    private void validateSocketImplementation(Class<? extends BaseSocket> cls) {
        if (cls != null) {
            try {
                if (Modifier.toString(cls.getModifiers()).contains("static") || cls.getEnclosingClass() == null) {
                    cls.getDeclaredConstructor(null);
                    return;
                } else {
                    cls.getDeclaredConstructor(cls.getEnclosingClass());
                    return;
                }
            } catch (NoSuchMethodException e) {
                SdkLog.e(TAG, "exception: " + e.getMessage(), e);
                throw new RuntimeException("Invalid implemetation of BaseSocket. Provider a public default constructor in the implementation class.");
            }
        }
        throw new IllegalArgumentException("Invalid socketClass param:" + cls);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void acceptServiceConnectionRequest(PeerAgent peerAgent) {
        if (peerAgent != null) {
            try {
                Initializer.initBufferPool(getApplicationContext());
                if (this.mPendingRequests.remove(peerAgent)) {
                    String str = TAG;
                    SdkLog.i(str, "Trying to Accept service connection request from peer:" + peerAgent.getAgentId() + " Transaction:" + peerAgent.getTransactionId());
                    c cVar = this.mBackgroundWorker;
                    if (cVar != null) {
                        Message obtainMessage = cVar.obtainMessage(7);
                        obtainMessage.obj = peerAgent;
                        this.mBackgroundWorker.sendMessage(obtainMessage);
                        return;
                    }
                    SdkLog.w(str, "acceptServiceConnection: mBackgroundWorker is null!");
                    return;
                }
                SdkLog.w(TAG, "Accepting service connection with invalid peer agent:" + peerAgent.toString());
                handleInvalidPeerAction(peerAgent);
                return;
            } catch (SdkUnsupportedException e) {
                SdkLog.e(TAG, "exception: " + e.getMessage());
                handleError(20002, peerAgent);
                return;
            }
        }
        throw new IllegalArgumentException("Illegal argument peerAgent:" + peerAgent);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void authenticatePeerAgent(PeerAgent peerAgent) {
        if (peerAgent != null) {
            try {
                Initializer.initBufferPool(getApplicationContext());
                String str = TAG;
                SdkLog.i(str, "Authentication requested for peer:" + peerAgent.getAgentId());
                c cVar = this.mBackgroundWorker;
                if (cVar != null) {
                    Message obtainMessage = cVar.obtainMessage(9);
                    obtainMessage.obj = peerAgent;
                    this.mBackgroundWorker.sendMessage(obtainMessage);
                    return;
                }
                SdkLog.w(str, "authenticatePeerAgent: mBackgroundWorker is null!");
                return;
            } catch (SdkUnsupportedException e) {
                SdkLog.e(TAG, "exception: " + e.getMessage());
                handleError(20002, peerAgent);
                return;
            }
        }
        throw new IllegalArgumentException("Illegal argument peerAgent:" + peerAgent);
    }

    public int checkAuthentication() {
        try {
            BaseAdapter baseAdapter = this.mAdapter;
            return baseAdapter != null ? baseAdapter.checkAuthentication() : CommonStatusCodes.INTERNAL_EXCEPTION;
        } catch (GeneralException e) {
            SdkLog.e(TAG, "check authentication error " + e);
            return CommonStatusCodes.INTERNAL_EXCEPTION;
        }
    }

    protected void dispatchStartCommand(Intent intent, int i, int i2) {
        c cVar = this.mBackgroundWorker;
        if (cVar != null) {
            Message obtainMessage = cVar.obtainMessage();
            obtainMessage.what = i2;
            obtainMessage.arg1 = i;
            obtainMessage.obj = intent;
            this.mBackgroundWorker.sendMessage(obtainMessage);
            return;
        }
        SdkLog.w(TAG, "onStartCommand: mBackgroundWorker is null!");
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final synchronized void findPeerAgents() {
        String str = TAG;
        SdkLog.d(str, "findPeer request received by:" + getClass().getName());
        try {
            Initializer.initBufferPool(getApplicationContext());
            c cVar = this.mBackgroundWorker;
            if (cVar != null) {
                Message obtainMessage = cVar.obtainMessage();
                obtainMessage.what = 2;
                this.mBackgroundWorker.sendMessage(obtainMessage);
            } else {
                SdkLog.w(str, "findPeerAgents: mBackgroundWorker is null!");
            }
        } catch (SdkUnsupportedException e) {
            SdkLog.e(TAG, "exception: " + e.getMessage());
            handleError(20002, null);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public Handler getAgentHandler() {
        return this.mBackgroundWorker;
    }

    String getId() {
        String localAgentId = getLocalAgentId();
        if (localAgentId == null) {
            handleError(20001, null);
        }
        return localAgentId;
    }

    protected String getLocalAgentId() {
        String str = TAG;
        SdkLog.d(str, "mGetAgentIdRetryCount = " + this.mGetAgentIdRetryCount);
        if (this.mGetAgentIdRetryCount >= 2) {
            this.mGetAgentIdRetryCount = 0;
            SdkLog.e(str, "Failed to retrieve service record, retry 2");
            return null;
        }
        try {
            String localAgentId = this.mAdapter.getLocalAgentId(getClass().getName());
            SdkLog.i(str, "Agent ID retrieved successfully for " + getClass().getName() + " Agent ID:" + localAgentId);
            return localAgentId;
        } catch (GeneralException e) {
            if (e.getErrorCode() == 10016) {
                String str2 = TAG;
                SdkLog.w(str2, "Service record was not found in Accessory Framework.Registering service again!");
                this.mGetAgentIdRetryCount++;
                try {
                    registerService();
                    SdkLog.i(str2, "Trying to fetch agent ID after re-registration");
                    return this.mAdapter.getLocalAgentId(getClass().getName());
                } catch (GeneralException unused) {
                    SdkLog.e(TAG, "Failed to retrieve service record after re-registration", e);
                    return null;
                }
            }
            SdkLog.e(TAG, "Failed to retrieve service record", e);
            return null;
        }
    }

    public int getServiceChannelId(int i) {
        if (this.mServiceProfile == null) {
            SdkLog.e(TAG, "Failed because Service Profile is null");
            return -1;
        }
        if (i >= 0 && i < getServiceChannelSize()) {
            return this.mServiceProfile.getServiceChannelList().get(i).getChannelId();
        }
        SdkLog.e(TAG, "Failed because of wrong index");
        return -1;
    }

    public int getServiceChannelSize() {
        ServiceProfile serviceProfile = this.mServiceProfile;
        if (serviceProfile == null) {
            SdkLog.e(TAG, "Failed because Service Profile is null");
            return -1;
        }
        return serviceProfile.getServiceChannelList().size();
    }

    public ServiceProfile getServiceProfile() {
        return this.mServiceProfile;
    }

    public String getServiceProfileId() {
        ServiceProfile serviceProfile = this.mServiceProfile;
        if (serviceProfile == null) {
            SdkLog.e(TAG, "Failed because Service Profile is null");
            return null;
        }
        return serviceProfile.getId();
    }

    public String getServiceProfileName() {
        ServiceProfile serviceProfile = this.mServiceProfile;
        if (serviceProfile == null) {
            SdkLog.e(TAG, "Failed because Service Profile is null");
            return null;
        }
        return serviceProfile.getName();
    }

    public List<BaseSocket> getSuccessfulConnections() {
        return this.mSuccessfulConnections;
    }

    public int getVersion() {
        try {
            BaseAdapter baseAdapter = this.mAdapter;
            if (baseAdapter != null) {
                return baseAdapter.getVersion();
            }
            return 0;
        } catch (GeneralException e) {
            SdkLog.e(TAG, "getVersion error " + e);
            return 0;
        }
    }

    void handleError(int i, PeerAgent peerAgent) {
        if (i != 10008) {
            switch (i) {
                case 20001:
                    cleanupConnections(true);
                    onError(null, "Accessory Framework has died!!", i);
                    handleOnErrorCode(i);
                    return;
                case 20002:
                    SdkLog.e(TAG, "Accessory SDK cannot be initialized");
                    onError(null, "Accessory SDK cannot be initialized. Device or Build not compatible.", i);
                    handleOnErrorCode(i);
                    return;
                case 20003:
                case 20004:
                    onError(null, "Permission error!", i);
                    handleOnErrorCode(i);
                    return;
                default:
                    SdkLog.w(TAG, "Unknown error: " + i);
                    return;
            }
        }
        onServiceConnectionResponse(peerAgent, null, 10008);
        handleServiceConnectionErrorCode(10008);
    }

    protected void onAuthenticationResponse(PeerAgent peerAgent, AuthenticationToken authenticationToken, int i) {
        SdkLog.d(TAG, "Peer authentication response received:" + i);
    }

    @Override // android.app.Service
    public void onCreate() {
        super.onCreate();
        try {
            new SdkConfig(getApplicationContext());
        } catch (GeneralException e) {
            e.printStackTrace();
        }
        String str = TAG;
        SdkLog.d(str, "BaseAgent - onCreate:" + getClass().getSimpleName());
        this.mSuccessfulConnections = Collections.synchronizedList(new ArrayList());
        this.mPendingRequests = Collections.synchronizedSet(new HashSet());
        HandlerThread handlerThread = new HandlerThread(this.mName);
        handlerThread.setUncaughtExceptionHandler(new a(this));
        handlerThread.start();
        Looper looper = handlerThread.getLooper();
        if (looper != null) {
            this.mBackgroundWorker = new c(this, looper);
            try {
                Initializer.initBufferPool(getApplicationContext());
            } catch (SdkUnsupportedException e2) {
                SdkLog.e(TAG, "SDK initialization failed!", e2);
                Message obtainMessage = this.mBackgroundWorker.obtainMessage(11);
                obtainMessage.arg1 = 20002;
                this.mBackgroundWorker.sendMessage(obtainMessage);
            }
            this.mAdapter = BaseAdapter.getDefaultAdapter(getApplicationContext(), this.mBackgroundWorker);
            a aVar = null;
            this.mPeerAuthCallback = new AuthenticationCallback(this, aVar);
            this.mPeerAgentCallback = new PeerAgentCallback(this, aVar);
            this.mConnectionCallback = new d(this, aVar);
            this.mAgentCallback = new b(this);
            this.mBackgroundWorker.sendEmptyMessage(0);
            fetchServiceProfile();
            return;
        }
        SdkLog.e(str, "Unable to start Agent thread.");
        throw new RuntimeException("Unable to start Agent.Worker thread creation failed");
    }

    @Override // android.app.Service
    public void onDestroy() {
        SdkLog.d(TAG, "BaseAgent - onDestroy:" + getClass().getSimpleName());
        c cVar = this.mBackgroundWorker;
        if (cVar != null) {
            cVar.obtainMessage(13).sendToTarget();
        }
        super.onDestroy();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public void onError(PeerAgent peerAgent, String str, int i) {
        if (peerAgent == null) {
            SdkLog.e(TAG, "ACCEPT_STATE_ERROR: " + i + ": " + str + " PeerAgent: null");
            return;
        }
        SdkLog.e(TAG, "ACCEPT_STATE_ERROR: " + i + ": " + str + " PeerAgent: " + peerAgent.getAgentId());
    }

    protected void onFindPeerAgentsResponse(PeerAgent[] peerAgentArr, int i) {
        SdkLog.e(TAG, "Invalid implementation of BaseAgent.onFindPeerAgentsResponse(PeerAgent[], int) should be overrided!");
    }

    protected void onPeerAgentsUpdated(PeerAgent[] peerAgentArr, int i) {
        SdkLog.e(TAG, "Invalid implementation of BaseAgent.onPeerAgentsUpdated(PeerAgent[], int) should be overrided!");
    }

    protected void onServiceConnectionRequested(PeerAgent peerAgent) {
        if (peerAgent != null) {
            SdkLog.v(TAG, "Accepting connection request by default from Peer:" + peerAgent.getAgentId() + " Transaction:" + peerAgent.getTransactionId());
        }
        acceptServiceConnectionRequest(peerAgent);
    }

    protected void onServiceConnectionResponse(PeerAgent peerAgent, BaseSocket baseSocket, int i) {
        SdkLog.w(TAG, "No Implementaion for onServiceConnectionResponse(PeerAgent peerAgent, BaseSocket socket, int result)!");
    }

    @Override // android.app.Service
    public int onStartCommand(Intent intent, int i, int i2) {
        String action;
        if (intent == null || (action = intent.getAction()) == null) {
            return 2;
        }
        if ("com.heytap.accessory.action.SERVICE_CONNECTION_REQUESTED".equalsIgnoreCase(action)) {
            SdkLog.d(TAG, "Received incoming connection request");
            dispatchStartCommand(intent, i2, 5);
            return 2;
        }
        if (BaseAdapter.ACTION_ACCESSORY_STATUS_CHANGED.equalsIgnoreCase(action)) {
            SdkLog.d(TAG, "Received accessory status changed");
            dispatchStartCommand(intent, i2, 15);
            return 2;
        }
        SdkLog.d(TAG, "ignore action:" + action);
        return 2;
    }

    @Override // android.app.Service, android.content.ComponentCallbacks2
    public void onTrimMemory(int i) {
        BufferPool.clearCache(i);
        super.onTrimMemory(i);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public String registerMessageInstance(BaseMessage baseMessage) {
        this.mMessage = baseMessage;
        return this.mAgentId;
    }

    protected void rejectServiceConnectionRequest(PeerAgent peerAgent) {
        if (peerAgent != null) {
            try {
                Initializer.initBufferPool(getApplicationContext());
                if (this.mPendingRequests.remove(peerAgent)) {
                    String str = TAG;
                    SdkLog.i(str, "Trying to reject connection request from peer:" + peerAgent.getAgentId() + " Transaction:" + peerAgent.getTransactionId());
                    c cVar = this.mBackgroundWorker;
                    if (cVar != null) {
                        Message obtainMessage = cVar.obtainMessage(8);
                        obtainMessage.obj = peerAgent;
                        this.mBackgroundWorker.sendMessage(obtainMessage);
                        return;
                    }
                    SdkLog.w(str, "rejectServiceConnection: mBackgroundWorker is null!");
                    return;
                }
                SdkLog.w(TAG, "Rejecting service connection with invalid peer agent:" + peerAgent.toString());
                handleInvalidPeerAction(peerAgent);
                return;
            } catch (SdkUnsupportedException e) {
                SdkLog.e(TAG, "exception: " + e.getMessage());
                handleError(20002, peerAgent);
                return;
            }
        }
        throw new IllegalArgumentException("Illegal argument peerAgent:" + peerAgent);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public final void requestServiceConnection(PeerAgent peerAgent) {
        if (peerAgent != null) {
            try {
                Initializer.initBufferPool(getApplicationContext());
                String str = TAG;
                SdkLog.i(str, "Service connection requested for peer:" + peerAgent.getAgentId());
                c cVar = this.mBackgroundWorker;
                if (cVar != null) {
                    Message obtainMessage = cVar.obtainMessage(6);
                    obtainMessage.obj = peerAgent;
                    this.mBackgroundWorker.sendMessage(obtainMessage);
                    return;
                }
                SdkLog.w(str, "requestServiceConection: mBackgroundWorker is null!");
                return;
            } catch (SdkUnsupportedException e) {
                SdkLog.e(TAG, "exception: " + e.getMessage());
                handleError(20002, peerAgent);
                return;
            }
        }
        throw new IllegalArgumentException("Illegal argument peerAgent:" + peerAgent);
    }

    public boolean runOnBackgroundThread(Runnable runnable) {
        c cVar = this.mBackgroundWorker;
        if (cVar != null) {
            return cVar.post(runnable);
        }
        SdkLog.w(TAG, "runOnBackgroundThread: mBackgroundWorker is null!");
        return false;
    }

    public boolean checkAuthentication(String str) {
        try {
            return this.mAdapter.checkAuthentication(str);
        } catch (GeneralException e) {
            SdkLog.e(TAG, "check authentication method error " + e);
            return false;
        }
    }

    protected BaseAgent(String str) {
        if (str != null && !"".equalsIgnoreCase(str)) {
            this.mName = str;
            return;
        }
        throw new IllegalArgumentException("Invalid parameter name:" + str);
    }
}