导航菜单

页面标题

页面副标题

FanCode v7.24.0 - AirPlayServiceSocketClient.java 源代码

正在查看: FanCode v7.24.0 应用的 AirPlayServiceSocketClient.java JAVA 源代码文件

本页面展示 JAVA 反编译生成的源代码文件,支持语法高亮显示。 仅供安全研究与技术分析使用,严禁用于任何非法用途。请遵守相关法律法规。


package com.connectsdk.service.airplay;

import android.annotation.SuppressLint;
import androidx.media3.common.PlaybackException;
import com.connectsdk.core.Util;
import com.connectsdk.etc.helper.HttpMessage;
import com.connectsdk.service.DeviceService;
import com.connectsdk.service.airplay.auth.AirPlayAuth;
import com.connectsdk.service.airplay.auth.AuthUtils;
import com.connectsdk.service.command.ServiceCommand;
import com.connectsdk.service.command.ServiceCommandError;
import com.connectsdk.service.command.ServiceSubscription;
import com.connectsdk.service.command.URLServiceSubscription;
import com.connectsdk.service.config.AirPlayServiceConfig;
import com.connectsdk.service.webos.lgcast.common.utils.StringUtil;
import com.newrelic.agent.android.instrumentation.Instrumented;
import com.newrelic.agent.android.instrumentation.LogInstrumentation;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import org.json.JSONObject;

@SuppressLint({"DefaultLocale"})
@Instrumented
public class AirPlayServiceSocketClient implements ServiceCommand.ServiceCommandProcessor {
    int PORT = PlaybackException.ERROR_CODE_VIDEO_FRAME_PROCESSOR_INIT_FAILED;
    AirPlayAuth airPlayAuth;
    AirPlayServiceSocketClientListener mListener;
    DeviceService.PairingType mPairingType;
    AirPlayServiceConfig mconfig;
    Socket socket;
    State state;

    public interface AirPlayServiceSocketClientListener {
        void onBeforeRegister(DeviceService.PairingType pairingType);

        void onCloseWithError(ServiceCommandError serviceCommandError);

        void onConnect();

        void onFailWithError(ServiceCommandError serviceCommandError);

        Boolean onReceiveMessage(JSONObject jSONObject);

        void onRegistrationFailed(ServiceCommandError serviceCommandError);
    }

    public enum State {
        NONE,
        INITIAL,
        CONNECTING,
        REGISTERING,
        REGISTERED,
        DISCONNECTING
    }

    public AirPlayServiceSocketClient(AirPlayServiceConfig airPlayServiceConfig, DeviceService.PairingType pairingType, String str) {
        State state = State.INITIAL;
        this.state = state;
        this.mPairingType = pairingType;
        this.mconfig = airPlayServiceConfig;
        if (airPlayServiceConfig.getAuthToken() == StringUtil.EMPTY) {
            this.mconfig.setAuthToken(AirPlayAuth.generateNewAuthToken());
        }
        this.airPlayAuth = new AirPlayAuth(new InetSocketAddress(str, this.PORT), this.mconfig.getAuthToken());
        this.state = state;
    }

    public void connect() {
        synchronized (this) {
            if (this.state != State.INITIAL) {
                LogInstrumentation.w(Util.T, "already connecting; not trying to connect again: " + this.state);
                return;
            }
            this.state = State.CONNECTING;
            try {
                try {
                    this.socket = this.airPlayAuth.authenticate();
                    this.state = State.REGISTERED;
                    AirPlayServiceSocketClientListener airPlayServiceSocketClientListener = this.mListener;
                    if (airPlayServiceSocketClientListener != null) {
                        airPlayServiceSocketClientListener.onConnect();
                    }
                } catch (Exception unused) {
                    this.airPlayAuth.startPairing();
                    AirPlayServiceSocketClientListener airPlayServiceSocketClientListener2 = this.mListener;
                    if (airPlayServiceSocketClientListener2 != null) {
                        airPlayServiceSocketClientListener2.onBeforeRegister(this.mPairingType);
                    }
                }
            } catch (Exception e9) {
                e9.printStackTrace();
                this.mListener.onRegistrationFailed(new ServiceCommandError(e9.toString()));
            }
            int i9 = 0;
            while (this.state == State.CONNECTING) {
                i9++;
                try {
                    Thread.sleep(100L);
                } catch (InterruptedException e10) {
                    e10.printStackTrace();
                    this.mListener.onRegistrationFailed(new ServiceCommandError(e10.toString()));
                }
                if (i9 > 200) {
                    this.mListener.onRegistrationFailed(new ServiceCommandError("Pairing Timeout"));
                    return;
                }
                continue;
            }
        }
    }

    public void disconnect() {
        this.mPairingType = null;
        this.mconfig = null;
        this.airPlayAuth = null;
        this.state = State.INITIAL;
        try {
            Socket socket = this.socket;
            if (socket != null) {
                socket.close();
            }
        } catch (IOException e9) {
            e9.printStackTrace();
        }
    }

    public void disconnectWithError(ServiceCommandError serviceCommandError) {
        this.state = State.INITIAL;
        AirPlayServiceSocketClientListener airPlayServiceSocketClientListener = this.mListener;
        if (airPlayServiceSocketClientListener != null) {
            airPlayServiceSocketClientListener.onCloseWithError(serviceCommandError);
        }
    }

    public String getAuthToken() {
        return this.mconfig.getAuthToken();
    }

    public State getState() {
        return this.state;
    }

    public boolean isConnected() {
        return getState() == State.REGISTERED;
    }

    public void pair(final String str) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    AirPlayServiceSocketClient.this.airPlayAuth.doPairing(str);
                    AirPlayServiceSocketClient airPlayServiceSocketClient = AirPlayServiceSocketClient.this;
                    airPlayServiceSocketClient.socket = airPlayServiceSocketClient.airPlayAuth.authenticate();
                    AirPlayServiceSocketClient airPlayServiceSocketClient2 = AirPlayServiceSocketClient.this;
                    airPlayServiceSocketClient2.state = State.REGISTERED;
                    AirPlayServiceSocketClientListener airPlayServiceSocketClientListener = airPlayServiceSocketClient2.mListener;
                    if (airPlayServiceSocketClientListener != null) {
                        airPlayServiceSocketClientListener.onConnect();
                    }
                } catch (Exception e9) {
                    AirPlayServiceSocketClient.this.state = State.INITIAL;
                    e9.printStackTrace();
                    AirPlayServiceSocketClient.this.mListener.onRegistrationFailed(new ServiceCommandError(e9.toString()));
                }
            }
        }).start();
    }

    @Override
    public void sendCommand(final ServiceCommand<?> serviceCommand) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] bArr = (byte[]) serviceCommand.getPayload();
                String target = serviceCommand.getTarget();
                try {
                    if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_GET)) {
                        Util.postSuccess(serviceCommand.getResponseListener(), AuthUtils.getData(AirPlayServiceSocketClient.this.socket, target));
                    } else if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                        Util.postSuccess(serviceCommand.getResponseListener(), AuthUtils.postData(AirPlayServiceSocketClient.this.socket, target, HttpMessage.CONTENT_TYPE_APPLICATION_PLIST, bArr));
                    } else if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
                        Util.postSuccess(serviceCommand.getResponseListener(), AuthUtils.putData(AirPlayServiceSocketClient.this.socket, target, HttpMessage.CONTENT_TYPE_APPLICATION_PLIST, bArr));
                    }
                } catch (IOException e9) {
                    e9.printStackTrace();
                    Util.postError(serviceCommand.getResponseListener(), ServiceCommandError.getError(Integer.parseInt(e9.getMessage().split(StringUtil.SPACE)[r0.length - 1])));
                }
            }
        }).start();
    }

    public void setListener(AirPlayServiceSocketClientListener airPlayServiceSocketClientListener) {
        this.mListener = airPlayServiceSocketClientListener;
    }

    @Override
    public void unsubscribe(ServiceSubscription<?> serviceSubscription) {
    }

    @Override
    public void unsubscribe(URLServiceSubscription<?> uRLServiceSubscription) {
    }
}