导航菜单

页面标题

页面副标题

Chaupal v3.2 - DLNAService.java 源代码

正在查看: Chaupal v3.2 应用的 DLNAService.java JAVA 源代码文件

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


package com.connectsdk.service;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.util.Xml;
import com.connectsdk.core.MediaInfo;
import com.connectsdk.core.SubtitleInfo;
import com.connectsdk.core.Util;
import com.connectsdk.device.ConnectableDevice;
import com.connectsdk.discovery.DiscoveryFilter;
import com.connectsdk.discovery.DiscoveryManager;
import com.connectsdk.discovery.provider.ssdp.Service;
import com.connectsdk.etc.helper.DeviceServiceReachability;
import com.connectsdk.etc.helper.HttpConnection;
import com.connectsdk.etc.helper.HttpMessage;
import com.connectsdk.service.DeviceService;
import com.connectsdk.service.capability.CapabilityMethods;
import com.connectsdk.service.capability.MediaControl;
import com.connectsdk.service.capability.MediaPlayer;
import com.connectsdk.service.capability.PlaylistControl;
import com.connectsdk.service.capability.VolumeControl;
import com.connectsdk.service.capability.listeners.ResponseListener;
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.ServiceConfig;
import com.connectsdk.service.config.ServiceDescription;
import com.connectsdk.service.sessions.LaunchSession;
import com.connectsdk.service.upnp.DLNAHttpServer;
import com.connectsdk.service.upnp.DLNAMediaInfoParser;
import com.google.ads.interactivemedia.v3.internal.anq;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xmlpull.v1.XmlPullParser;

public class DLNAService extends DeviceService implements PlaylistControl, MediaControl, MediaPlayer, VolumeControl {
    protected static final String AV_TRANSPORT = "AVTransport";
    public static final String AV_TRANSPORT_URN = "urn:schemas-upnp-org:service:AVTransport:1";
    protected static final String CONNECTION_MANAGER = "ConnectionManager";
    public static final String CONNECTION_MANAGER_URN = "urn:schemas-upnp-org:service:ConnectionManager:1";
    public static final String DEFAULT_SUBTITLE_MIMETYPE = "text/srt";
    public static final String DEFAULT_SUBTITLE_TYPE = "srt";
    protected static final String GROUP_RENDERING_CONTROL = "GroupRenderingControl";
    public static final String ID = "DLNA";
    public static final String PLAY_STATE = "playState";
    protected static final String RENDERING_CONTROL = "RenderingControl";
    public static final String RENDERING_CONTROL_URN = "urn:schemas-upnp-org:service:RenderingControl:1";
    protected static final String SUBSCRIBE = "SUBSCRIBE";
    private static int TIMEOUT = 300;
    protected static final String UNSUBSCRIBE = "UNSUBSCRIBE";
    Map<String, String> SIDList;
    String avTransportURL;
    String connectionControlURL;
    Context context;
    DLNAHttpServer httpServer;
    String renderingControlURL;
    Timer resubscriptionTimer;

    static class AnonymousClass20 {
        static final int[] $SwitchMap$com$connectsdk$service$capability$PlaylistControl$PlayMode;

        static {
            int[] iArr = new int[PlaylistControl.PlayMode.values().length];
            $SwitchMap$com$connectsdk$service$capability$PlaylistControl$PlayMode = iArr;
            try {
                iArr[PlaylistControl.PlayMode.RepeatAll.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$connectsdk$service$capability$PlaylistControl$PlayMode[PlaylistControl.PlayMode.RepeatOne.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$connectsdk$service$capability$PlaylistControl$PlayMode[PlaylistControl.PlayMode.Shuffle.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    interface PositionInfoListener {
        void onGetPositionInfoFailed(ServiceCommandError serviceCommandError);

        void onGetPositionInfoSuccess(String str);
    }

    public DLNAService(ServiceDescription serviceDescription, ServiceConfig serviceConfig) {
        this(serviceDescription, serviceConfig, DiscoveryManager.getInstance().getContext(), new DLNAHttpServer());
    }

    private void addSubscription(URLServiceSubscription<?> uRLServiceSubscription) {
        if (!this.httpServer.isRunning()) {
            Util.runInBackground(new Runnable() {
                @Override
                public void run() {
                    DLNAService.this.httpServer.start();
                }
            });
            subscribeServices();
        }
        this.httpServer.getSubscriptions().add(uRLServiceSubscription);
    }

    public static DiscoveryFilter discoveryFilter() {
        return new DiscoveryFilter(ID, "urn:schemas-upnp-org:device:MediaRenderer:1");
    }

    private void getDeviceCapabilities(final PositionInfoListener positionInfoListener) {
        new ServiceCommand(this, "GetDeviceCapabilities", getMessageXml(AV_TRANSPORT_URN, "GetDeviceCapabilities", "0", null), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoFailed(serviceCommandError);
                }
            }

            @Override
            public void onSuccess(Object obj) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoSuccess((String) obj);
                }
            }
        }).send();
    }

    private void getPositionInfo(final PositionInfoListener positionInfoListener) {
        new ServiceCommand(this, "GetPositionInfo", getMessageXml(AV_TRANSPORT_URN, "GetPositionInfo", "0", null), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoFailed(serviceCommandError);
                }
            }

            @Override
            public void onSuccess(Object obj) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoSuccess((String) obj);
                }
            }
        }).send();
    }

    private void getProtocolInfo(final PositionInfoListener positionInfoListener) {
        new ServiceCommand(this, "GetProtocolInfo", getMessageXml(CONNECTION_MANAGER_URN, "GetProtocolInfo", null, null), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoFailed(serviceCommandError);
                }
            }

            @Override
            public void onSuccess(Object obj) {
                PositionInfoListener positionInfoListener2 = positionInfoListener;
                if (positionInfoListener2 != null) {
                    positionInfoListener2.onGetPositionInfoSuccess((String) obj);
                }
            }
        }).send();
    }

    private boolean isXmlEncoded(String str) {
        if (str == null || str.length() < 4) {
            return false;
        }
        return str.trim().substring(0, 4).equals("&lt;");
    }

    private void updateControlURL() {
        List<Service> serviceList = this.serviceDescription.getServiceList();
        if (serviceList != null) {
            for (int i7 = 0; i7 < serviceList.size(); i7++) {
                if (!serviceList.get(i7).baseURL.endsWith("/")) {
                    StringBuilder sb = new StringBuilder();
                    Service service = serviceList.get(i7);
                    sb.append(service.baseURL);
                    sb.append("/");
                    service.baseURL = sb.toString();
                }
                if (serviceList.get(i7).serviceType.contains(AV_TRANSPORT)) {
                    this.avTransportURL = makeControlURL(serviceList.get(i7).baseURL, serviceList.get(i7).controlURL);
                } else if (serviceList.get(i7).serviceType.contains(RENDERING_CONTROL) && !serviceList.get(i7).serviceType.contains(GROUP_RENDERING_CONTROL)) {
                    this.renderingControlURL = makeControlURL(serviceList.get(i7).baseURL, serviceList.get(i7).controlURL);
                } else if (serviceList.get(i7).serviceType.contains(CONNECTION_MANAGER)) {
                    this.connectionControlURL = makeControlURL(serviceList.get(i7).baseURL, serviceList.get(i7).controlURL);
                }
            }
        }
    }

    @Override
    public void closeMedia(LaunchSession launchSession, ResponseListener<Object> responseListener) {
        if (launchSession.getService() instanceof DLNAService) {
            ((DLNAService) launchSession.getService()).stop(responseListener);
        }
    }

    @Override
    public void connect() {
        this.connected = true;
        reportConnected(true);
    }

    long convertStrTimeFormatToLong(String str) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        try {
            return simpleDateFormat.parse(str).getTime() - simpleDateFormat.parse("00:00:00").getTime();
        } catch (NullPointerException unused) {
            Log.w(Util.T, "Null time argument");
            return 0L;
        } catch (ParseException unused2) {
            Log.w(Util.T, "Invalid Time Format: " + str);
            return 0L;
        }
    }

    HttpConnection createHttpConnection(String str) throws IOException {
        return HttpConnection.newInstance(URI.create(str));
    }

    @Override
    public LaunchSession decodeLaunchSession(String str, JSONObject jSONObject) {
        if (!str.equals("dlna")) {
            return null;
        }
        LaunchSession launchSessionFromJSONObject = LaunchSession.launchSessionFromJSONObject(jSONObject);
        launchSessionFromJSONObject.setService(this);
        return launchSessionFromJSONObject;
    }

    @Override
    public void disconnect() {
        this.connected = false;
        DeviceServiceReachability deviceServiceReachability = this.mServiceReachability;
        if (deviceServiceReachability != null) {
            deviceServiceReachability.stop();
        }
        Util.runOnUI(new Runnable() {
            @Override
            public void run() {
                DLNAService dLNAService = DLNAService.this;
                DeviceService.DeviceServiceListener deviceServiceListener = dLNAService.listener;
                if (deviceServiceListener != null) {
                    deviceServiceListener.onDisconnect(dLNAService, null);
                }
            }
        });
        Util.runInBackground(new Runnable() {
            @Override
            public void run() {
                DLNAService.this.httpServer.stop();
            }
        }, true);
    }

    @Override
    public void displayImage(String str, String str2, String str3, String str4, String str5, MediaPlayer.LaunchListener launchListener) {
        displayMedia(str, null, str2, str3, str4, str5, launchListener);
    }

    @Deprecated
    public void displayMedia(String str, String str2, String str3, String str4, String str5, MediaPlayer.LaunchListener launchListener) {
        displayMedia(str, null, str2, str3, str4, str5, launchListener);
    }

    String encodeURL(String str) throws MalformedURLException, URISyntaxException, UnsupportedEncodingException {
        if (str == null || str.isEmpty()) {
            return "";
        }
        if (!URLDecoder.decode(str, "UTF-8").equals(str)) {
            return str;
        }
        URL url = new URL(str);
        return new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()).toASCIIString();
    }

    @Override
    public void fastForward(ResponseListener<Object> responseListener) {
        Util.postError(responseListener, ServiceCommandError.notSupported());
    }

    @Override
    public void getDuration(final MediaControl.DurationListener durationListener) {
        getPositionInfo(new PositionInfoListener() {
            @Override
            public void onGetPositionInfoFailed(ServiceCommandError serviceCommandError) {
                Util.postError(durationListener, serviceCommandError);
            }

            @Override
            public void onGetPositionInfoSuccess(String str) {
                String parseData = DLNAService.this.parseData(str, "TrackDuration");
                MediaInfo mediaInfo = DLNAMediaInfoParser.getMediaInfo(DLNAService.this.parseData(str, "TrackMetaData"));
                if (parseData.equals("0:00:00") && !mediaInfo.getMimeType().contains("image")) {
                    new Timer().schedule(new TimerTask() {
                        @Override
                        public void run() {
                            AnonymousClass4 anonymousClass4 = AnonymousClass4.this;
                            DLNAService.this.getDuration(durationListener);
                        }
                    }, 1000L);
                } else {
                    Util.postSuccess(durationListener, Long.valueOf(DLNAService.this.convertStrTimeFormatToLong(parseData)));
                }
            }
        });
    }

    @Override
    public MediaControl getMediaControl() {
        return this;
    }

    @Override
    public CapabilityMethods.CapabilityPriorityLevel getMediaControlCapabilityLevel() {
        return CapabilityMethods.CapabilityPriorityLevel.NORMAL;
    }

    @Override
    public void getMediaInfo(final MediaPlayer.MediaInfoListener mediaInfoListener) {
        getPositionInfo(new PositionInfoListener() {
            @Override
            public void onGetPositionInfoFailed(ServiceCommandError serviceCommandError) {
                Util.postError(mediaInfoListener, serviceCommandError);
            }

            @Override
            public void onGetPositionInfoSuccess(final String str) {
                Util.runInBackground(new Runnable() {
                    @Override
                    public void run() {
                        Util.postSuccess(mediaInfoListener, DLNAMediaInfoParser.getMediaInfo(DLNAService.this.parseData(str, "TrackMetaData"), "http://" + DLNAService.this.getServiceDescription().getIpAddress() + ":" + DLNAService.this.getServiceDescription().getPort()));
                    }
                });
            }
        });
    }

    @Override
    public MediaPlayer getMediaPlayer() {
        return this;
    }

    @Override
    public CapabilityMethods.CapabilityPriorityLevel getMediaPlayerCapabilityLevel() {
        return CapabilityMethods.CapabilityPriorityLevel.NORMAL;
    }

    protected String getMessageXml(String str, String str2, String str3, Map<String, String> map) {
        try {
            Document newDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            newDocument.setXmlStandalone(true);
            newDocument.setXmlVersion("1.0");
            Element createElement = newDocument.createElement("s:Envelope");
            Element createElement2 = newDocument.createElement("s:Body");
            Element createElementNS = newDocument.createElementNS(str, "u:" + str2);
            Element createElement3 = newDocument.createElement("InstanceID");
            createElement.setAttribute("s:encodingStyle", "http://schemas.xmlsoap.org/soap/encoding/");
            createElement.setAttribute("xmlns:s", "http://schemas.xmlsoap.org/soap/envelope/");
            newDocument.appendChild(createElement);
            createElement.appendChild(createElement2);
            createElement2.appendChild(createElementNS);
            if (str3 != null) {
                createElement3.setTextContent(str3);
                createElementNS.appendChild(createElement3);
            }
            if (map != null) {
                for (Map.Entry<String, String> entry : map.entrySet()) {
                    String key = entry.getKey();
                    String value = entry.getValue();
                    Element createElement4 = newDocument.createElement(key);
                    createElement4.setTextContent(value);
                    createElementNS.appendChild(createElement4);
                }
            }
            return xmlToString(newDocument, true);
        } catch (Exception unused) {
            return null;
        }
    }

    protected String getMetadata(String str, SubtitleInfo subtitleInfo, String str2, String str3, String str4, String str5) {
        Document document;
        try {
            String str6 = "";
            if (str2.startsWith("image")) {
                str6 = "object.item.imageItem";
            } else if (str2.startsWith("video")) {
                str6 = "object.item.videoItem";
            } else if (str2.startsWith("audio")) {
                str6 = "object.item.audioItem";
            }
            Document newDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element createElement = newDocument.createElement("DIDL-Lite");
            Element createElement2 = newDocument.createElement("item");
            Element createElement3 = newDocument.createElement("dc:title");
            Element createElement4 = newDocument.createElement("dc:description");
            Element createElement5 = newDocument.createElement("res");
            Element createElement6 = newDocument.createElement("upnp:albumArtURI");
            Element createElement7 = newDocument.createElement("upnp:class");
            createElement.appendChild(createElement2);
            createElement2.appendChild(createElement3);
            createElement2.appendChild(createElement4);
            createElement2.appendChild(createElement5);
            createElement2.appendChild(createElement6);
            createElement2.appendChild(createElement7);
            createElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/");
            createElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:upnp", "urn:schemas-upnp-org:metadata-1-0/upnp/");
            createElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:dc", "http://purl.org/dc/elements/1.1/");
            createElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:sec", "http://www.sec.co.kr/");
            createElement3.setTextContent(str3);
            createElement4.setTextContent(str4);
            createElement5.setTextContent(encodeURL(str));
            createElement6.setTextContent(encodeURL(str5));
            createElement7.setTextContent(str6);
            createElement2.setAttribute(ConnectableDevice.KEY_ID, "1000");
            createElement2.setAttribute("parentID", "0");
            createElement2.setAttribute("restricted", "0");
            createElement5.setAttribute("protocolInfo", "http-get:*:" + str2 + ":DLNA.ORG_OP=01");
            if (subtitleInfo != null) {
                String mimeType = subtitleInfo.getMimeType();
                String str7 = DEFAULT_SUBTITLE_TYPE;
                String mimeType2 = mimeType == null ? DEFAULT_SUBTITLE_TYPE : subtitleInfo.getMimeType();
                String[] split = mimeType2.split("/");
                if (split == null || split.length != 2) {
                    mimeType2 = DEFAULT_SUBTITLE_MIMETYPE;
                } else {
                    str7 = split[1];
                }
                createElement5.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:pv", "http://www.pv.com/pvns/");
                createElement5.setAttribute("pv:subtitleFileUri", subtitleInfo.getUrl());
                createElement5.setAttribute("pv:subtitleFileType", str7);
                Document document2 = newDocument;
                Element createElement8 = document2.createElement("res");
                createElement8.setAttribute("protocolInfo", "http-get:*:smi/caption");
                createElement8.setTextContent(subtitleInfo.getUrl());
                createElement2.appendChild(createElement8);
                Element createElement9 = document2.createElement("res");
                createElement9.setAttribute("protocolInfo", "http-get:*:" + mimeType2 + ":");
                createElement9.setTextContent(subtitleInfo.getUrl());
                createElement2.appendChild(createElement9);
                Element createElement10 = document2.createElement("sec:CaptionInfoEx");
                createElement10.setAttribute("sec:type", str7);
                createElement10.setTextContent(subtitleInfo.getUrl());
                createElement2.appendChild(createElement10);
                Element createElement11 = document2.createElement("sec:CaptionInfo");
                createElement11.setAttribute("sec:type", str7);
                createElement11.setTextContent(subtitleInfo.getUrl());
                createElement2.appendChild(createElement11);
                document = document2;
            } else {
                document = newDocument;
            }
            document.appendChild(createElement);
            return xmlToString(document, false);
        } catch (Exception unused) {
            return null;
        }
    }

    @Override
    public void getMute(final VolumeControl.MuteListener muteListener) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Channel", "Master");
        new ServiceCommand(this, "GetMute", getMessageXml(RENDERING_CONTROL_URN, "GetMute", "0", linkedHashMap), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(muteListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Object obj) {
                Util.postSuccess(muteListener, Boolean.valueOf(Boolean.parseBoolean(DLNAService.this.parseData((String) obj, "CurrentMute"))));
            }
        }).send();
    }

    @Override
    public void getPlayState(final MediaControl.PlayStateListener playStateListener) {
        new ServiceCommand(this, "GetTransportInfo", getMessageXml(AV_TRANSPORT_URN, "GetTransportInfo", "0", null), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(playStateListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Object obj) {
                Util.postSuccess(playStateListener, MediaControl.PlayStateStatus.convertTransportStateToPlayStateStatus(DLNAService.this.parseData((String) obj, "CurrentTransportState")));
            }
        }).send();
    }

    @Override
    public PlaylistControl getPlaylistControl() {
        return this;
    }

    @Override
    public CapabilityMethods.CapabilityPriorityLevel getPlaylistControlCapabilityLevel() {
        return CapabilityMethods.CapabilityPriorityLevel.NORMAL;
    }

    @Override
    public void getPosition(final MediaControl.PositionListener positionListener) {
        getPositionInfo(new PositionInfoListener() {
            @Override
            public void onGetPositionInfoFailed(ServiceCommandError serviceCommandError) {
                Util.postError(positionListener, serviceCommandError);
            }

            @Override
            public void onGetPositionInfoSuccess(String str) {
                Util.postSuccess(positionListener, Long.valueOf(DLNAService.this.convertStrTimeFormatToLong(DLNAService.this.parseData(str, "RelTime"))));
            }
        });
    }

    @Override
    public CapabilityMethods.CapabilityPriorityLevel getPriorityLevel(Class<? extends CapabilityMethods> cls) {
        return cls.equals(MediaPlayer.class) ? getMediaPlayerCapabilityLevel() : cls.equals(MediaControl.class) ? getMediaControlCapabilityLevel() : cls.equals(VolumeControl.class) ? getVolumeControlCapabilityLevel() : cls.equals(PlaylistControl.class) ? getPlaylistControlCapabilityLevel() : CapabilityMethods.CapabilityPriorityLevel.NOT_SUPPORTED;
    }

    @Override
    public void getVolume(final VolumeControl.VolumeListener volumeListener) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Channel", "Master");
        new ServiceCommand(this, "GetVolume", getMessageXml(RENDERING_CONTROL_URN, "GetVolume", "0", linkedHashMap), new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(volumeListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Object obj) {
                try {
                    Integer.parseInt(DLNAService.this.parseData((String) obj, "CurrentVolume"));
                } catch (RuntimeException unused) {
                }
                Util.postSuccess(volumeListener, Float.valueOf((float) (0 / 100.0d)));
            }
        }).send();
    }

    @Override
    public VolumeControl getVolumeControl() {
        return this;
    }

    @Override
    public CapabilityMethods.CapabilityPriorityLevel getVolumeControlCapabilityLevel() {
        return CapabilityMethods.CapabilityPriorityLevel.NORMAL;
    }

    @Override
    public boolean isConnectable() {
        return true;
    }

    @Override
    public boolean isConnected() {
        return this.connected;
    }

    @Override
    public void jumpToTrack(long j7, ResponseListener<Object> responseListener) {
        seek("TRACK_NR", Long.toString(j7 + 1), responseListener);
    }

    String makeControlURL(String str, String str2) {
        if (str == null || str2 == null) {
            return null;
        }
        if (str2.startsWith("/")) {
            return str + str2.substring(1);
        }
        return str + str2;
    }

    @Override
    public void next(ResponseListener<Object> responseListener) {
        new ServiceCommand(this, "Next", getMessageXml(AV_TRANSPORT_URN, "Next", "0", null), responseListener).send();
    }

    @Override
    public void onLoseReachability(DeviceServiceReachability deviceServiceReachability) {
        if (this.connected) {
            disconnect();
        } else {
            this.mServiceReachability.stop();
        }
    }

    String parseData(String str, String str2) {
        int next;
        if (isXmlEncoded(str)) {
            str = Html.fromHtml(str).toString();
        }
        XmlPullParser newPullParser = Xml.newPullParser();
        try {
            newPullParser.setInput(new StringReader(str));
            boolean z6 = false;
            do {
                next = newPullParser.next();
                if (next == 2) {
                    if (str2.equals(newPullParser.getName())) {
                        z6 = true;
                    }
                } else if (next == 4 && z6) {
                    return newPullParser.getText();
                }
            } while (next != 1);
            return "";
        } catch (Exception unused) {
            return "";
        }
    }

    @Override
    public void pause(ResponseListener<Object> responseListener) {
        new ServiceCommand(this, "Pause", getMessageXml(AV_TRANSPORT_URN, "Pause", "0", null), responseListener).send();
    }

    @Override
    public void play(ResponseListener<Object> responseListener) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Speed", "1");
        new ServiceCommand(this, "Play", getMessageXml(AV_TRANSPORT_URN, "Play", "0", linkedHashMap), responseListener).send();
    }

    @Override
    public void playMedia(String str, String str2, String str3, String str4, String str5, boolean z6, MediaPlayer.LaunchListener launchListener) {
        displayMedia(str, null, str2, str3, str4, str5, launchListener);
    }

    @Override
    public void previous(ResponseListener<Object> responseListener) {
        new ServiceCommand(this, "Previous", getMessageXml(AV_TRANSPORT_URN, "Previous", "0", null), responseListener).send();
    }

    public void resubscribeServices() {
        Timer timer = new Timer();
        this.resubscriptionTimer = timer;
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Util.runInBackground(new Runnable() {
                    @Override
                    public void run() {
                        List<Service> serviceList = DLNAService.this.serviceDescription.getServiceList();
                        if (serviceList != null) {
                            for (int i7 = 0; i7 < serviceList.size(); i7++) {
                                String makeControlURL = DLNAService.this.makeControlURL("/", serviceList.get(i7).eventSubURL);
                                if (makeControlURL != null) {
                                    String str = DLNAService.this.SIDList.get(serviceList.get(i7).serviceType);
                                    try {
                                        HttpConnection newSubscriptionInstance = HttpConnection.newSubscriptionInstance(new URI("http", "", DLNAService.this.serviceDescription.getIpAddress(), DLNAService.this.serviceDescription.getPort(), makeControlURL, "", ""));
                                        newSubscriptionInstance.setMethod(HttpConnection.Method.SUBSCRIBE);
                                        newSubscriptionInstance.setHeader("TIMEOUT", "Second-" + DLNAService.TIMEOUT);
                                        newSubscriptionInstance.setHeader("SID", str);
                                        newSubscriptionInstance.execute();
                                    } catch (Exception unused) {
                                    }
                                }
                            }
                        }
                    }
                });
            }
        };
        int i7 = TIMEOUT;
        timer.scheduleAtFixedRate(timerTask, (i7 / 2) * anq.f11307f, (i7 / 2) * anq.f11307f);
    }

    @Override
    public void rewind(ResponseListener<Object> responseListener) {
        Util.postError(responseListener, ServiceCommandError.notSupported());
    }

    @Override
    public void seek(long j7, ResponseListener<Object> responseListener) {
        seek("REL_TIME", String.format(Locale.US, "%02d:%02d:%02d", Long.valueOf((j7 / 3600000) % 24), Long.valueOf((j7 / 60000) % 60), Long.valueOf((j7 / 1000) % 60)), responseListener);
    }

    @Override
    public void sendCommand(final ServiceCommand<?> serviceCommand) {
        Util.runInBackground(new Runnable() {
            @Override
            public void run() {
                String str;
                ServiceCommand serviceCommand2 = serviceCommand;
                String target = serviceCommand2.getTarget();
                String str2 = (String) serviceCommand2.getPayload();
                if (str2 == null) {
                    Util.postError(serviceCommand2.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"payload\" is missed", null));
                    return;
                }
                String str3 = DLNAService.AV_TRANSPORT_URN;
                if (str2.contains(DLNAService.AV_TRANSPORT_URN)) {
                    str = DLNAService.this.avTransportURL;
                } else if (str2.contains(DLNAService.RENDERING_CONTROL_URN)) {
                    str = DLNAService.this.renderingControlURL;
                    str3 = DLNAService.RENDERING_CONTROL_URN;
                } else if (str2.contains(DLNAService.CONNECTION_MANAGER_URN)) {
                    str = DLNAService.this.connectionControlURL;
                    str3 = DLNAService.CONNECTION_MANAGER_URN;
                } else {
                    str3 = null;
                    str = null;
                }
                if (str3 == null) {
                    Util.postError(serviceCommand2.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"serviceURN\" is missed", null));
                    return;
                }
                if (str == null) {
                    Util.postError(serviceCommand2.getResponseListener(), new ServiceCommandError(0, "Cannot process the command, \"targetURL\" is missed", null));
                    return;
                }
                try {
                    HttpConnection createHttpConnection = DLNAService.this.createHttpConnection(str);
                    createHttpConnection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, HttpMessage.CONTENT_TYPE_TEXT_XML);
                    createHttpConnection.setHeader("SOAPAction", String.format("\"%s#%s\"", str3, target));
                    createHttpConnection.setMethod(HttpConnection.Method.POST);
                    createHttpConnection.setPayload(str2);
                    createHttpConnection.execute();
                    int responseCode = createHttpConnection.getResponseCode();
                    if (responseCode == 200) {
                        Util.postSuccess(serviceCommand2.getResponseListener(), createHttpConnection.getResponseString());
                    } else {
                        Util.postError(serviceCommand2.getResponseListener(), ServiceCommandError.getError(responseCode));
                    }
                } catch (IOException e7) {
                    Util.postError(serviceCommand2.getResponseListener(), new ServiceCommandError(0, e7.getMessage(), null));
                }
            }
        });
    }

    @Override
    public void setMute(boolean z6, ResponseListener<Object> responseListener) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Channel", "Master");
        linkedHashMap.put("DesiredMute", String.valueOf(z6 ? 1 : 0));
        new ServiceCommand(this, "SetMute", getMessageXml(RENDERING_CONTROL_URN, "SetMute", "0", linkedHashMap), responseListener).send();
    }

    @Override
    public void setPlayMode(PlaylistControl.PlayMode playMode, ResponseListener<Object> responseListener) {
        int i7 = AnonymousClass20.$SwitchMap$com$connectsdk$service$capability$PlaylistControl$PlayMode[playMode.ordinal()];
        String str = i7 != 1 ? i7 != 2 ? i7 != 3 ? "NORMAL" : "SHUFFLE" : "REPEAT_ONE" : "REPEAT_ALL";
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("NewPlayMode", str);
        new ServiceCommand(this, "SetPlayMode", getMessageXml(AV_TRANSPORT_URN, "SetPlayMode", "0", linkedHashMap), responseListener).send();
    }

    @Override
    public void setServiceDescription(ServiceDescription serviceDescription) {
        super.setServiceDescription(serviceDescription);
        updateControlURL();
    }

    @Override
    public void setVolume(float f2, ResponseListener<Object> responseListener) {
        String valueOf = String.valueOf((int) (f2 * 100.0f));
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Channel", "Master");
        linkedHashMap.put("DesiredVolume", valueOf);
        new ServiceCommand(this, "SetVolume", getMessageXml(RENDERING_CONTROL_URN, "SetVolume", "0", linkedHashMap), responseListener).send();
    }

    @Override
    public void stop(ResponseListener<Object> responseListener) {
        new ServiceCommand(this, "Stop", getMessageXml(AV_TRANSPORT_URN, "Stop", "0", null), responseListener).send();
    }

    @Override
    public ServiceSubscription<MediaPlayer.MediaInfoListener> subscribeMediaInfo(MediaPlayer.MediaInfoListener mediaInfoListener) {
        URLServiceSubscription<?> uRLServiceSubscription = new URLServiceSubscription<>(this, "info", null, null);
        uRLServiceSubscription.addListener((URLServiceSubscription<?>) mediaInfoListener);
        addSubscription(uRLServiceSubscription);
        return uRLServiceSubscription;
    }

    @Override
    public ServiceSubscription<VolumeControl.MuteListener> subscribeMute(VolumeControl.MuteListener muteListener) {
        URLServiceSubscription<?> uRLServiceSubscription = new URLServiceSubscription<>(this, "mute", null, null);
        uRLServiceSubscription.addListener((URLServiceSubscription<?>) muteListener);
        addSubscription(uRLServiceSubscription);
        return uRLServiceSubscription;
    }

    @Override
    public ServiceSubscription<MediaControl.PlayStateListener> subscribePlayState(MediaControl.PlayStateListener playStateListener) {
        URLServiceSubscription<?> uRLServiceSubscription = new URLServiceSubscription<>(this, PLAY_STATE, null, null);
        uRLServiceSubscription.addListener((URLServiceSubscription<?>) playStateListener);
        addSubscription(uRLServiceSubscription);
        return uRLServiceSubscription;
    }

    public void subscribeServices() {
        Util.runInBackground(new Runnable() {
            @Override
            public void run() {
                String str;
                try {
                    str = Util.getIpAddress(DLNAService.this.context).getHostAddress();
                } catch (UnknownHostException unused) {
                    str = null;
                }
                List<Service> serviceList = DLNAService.this.serviceDescription.getServiceList();
                if (serviceList != null) {
                    for (int i7 = 0; i7 < serviceList.size(); i7++) {
                        String makeControlURL = DLNAService.this.makeControlURL("/", serviceList.get(i7).eventSubURL);
                        if (makeControlURL != null) {
                            try {
                                HttpConnection newSubscriptionInstance = HttpConnection.newSubscriptionInstance(new URI("http", "", DLNAService.this.serviceDescription.getIpAddress(), DLNAService.this.serviceDescription.getPort(), makeControlURL, "", ""));
                                newSubscriptionInstance.setMethod(HttpConnection.Method.SUBSCRIBE);
                                newSubscriptionInstance.setHeader("CALLBACK", "<http://" + str + ":" + DLNAService.this.httpServer.getPort() + makeControlURL + ">");
                                newSubscriptionInstance.setHeader("NT", "upnp:event");
                                StringBuilder sb = new StringBuilder();
                                sb.append("Second-");
                                sb.append(DLNAService.TIMEOUT);
                                newSubscriptionInstance.setHeader("TIMEOUT", sb.toString());
                                newSubscriptionInstance.setHeader("Connection", "close");
                                newSubscriptionInstance.setHeader("Content-length", "0");
                                newSubscriptionInstance.setHeader("USER-AGENT", "Android UPnp/1.1 ConnectSDK");
                                newSubscriptionInstance.execute();
                                if (newSubscriptionInstance.getResponseCode() == 200) {
                                    DLNAService.this.SIDList.put(serviceList.get(i7).serviceType, newSubscriptionInstance.getResponseHeader("SID"));
                                }
                            } catch (Exception unused2) {
                            }
                        }
                    }
                }
            }
        });
        resubscribeServices();
    }

    @Override
    public ServiceSubscription<VolumeControl.VolumeListener> subscribeVolume(VolumeControl.VolumeListener volumeListener) {
        URLServiceSubscription<?> uRLServiceSubscription = new URLServiceSubscription<>(this, "volume", null, null);
        uRLServiceSubscription.addListener((URLServiceSubscription<?>) volumeListener);
        addSubscription(uRLServiceSubscription);
        return uRLServiceSubscription;
    }

    @Override
    public void unsubscribe(URLServiceSubscription<?> uRLServiceSubscription) {
        this.httpServer.getSubscriptions().remove(uRLServiceSubscription);
        if (this.httpServer.getSubscriptions().isEmpty()) {
            unsubscribeServices();
        }
    }

    public void unsubscribeServices() {
        Timer timer = this.resubscriptionTimer;
        if (timer != null) {
            timer.cancel();
        }
        Util.runInBackground(new Runnable() {
            @Override
            public void run() {
                List<Service> serviceList = DLNAService.this.serviceDescription.getServiceList();
                if (serviceList != null) {
                    for (int i7 = 0; i7 < serviceList.size(); i7++) {
                        String makeControlURL = DLNAService.this.makeControlURL("/", serviceList.get(i7).eventSubURL);
                        if (makeControlURL != null) {
                            String str = DLNAService.this.SIDList.get(serviceList.get(i7).serviceType);
                            try {
                                HttpConnection newSubscriptionInstance = HttpConnection.newSubscriptionInstance(new URI("http", "", DLNAService.this.serviceDescription.getIpAddress(), DLNAService.this.serviceDescription.getPort(), makeControlURL, "", ""));
                                newSubscriptionInstance.setMethod(HttpConnection.Method.UNSUBSCRIBE);
                                newSubscriptionInstance.setHeader("SID", str);
                                newSubscriptionInstance.execute();
                                if (newSubscriptionInstance.getResponseCode() == 200) {
                                    DLNAService.this.SIDList.remove(serviceList.get(i7).serviceType);
                                }
                            } catch (Exception unused) {
                            }
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void updateCapabilities() {
        ArrayList arrayList = new ArrayList();
        arrayList.add(MediaPlayer.Display_Image);
        arrayList.add("MediaPlayer.Play.Video");
        arrayList.add("MediaPlayer.Play.Audio");
        arrayList.add(MediaPlayer.Play_Playlist);
        arrayList.add(MediaPlayer.Close);
        arrayList.add(MediaPlayer.Subtitle_SRT);
        arrayList.add(MediaPlayer.MetaData_Title);
        arrayList.add(MediaPlayer.MetaData_MimeType);
        arrayList.add(MediaPlayer.MediaInfo_Get);
        arrayList.add(MediaPlayer.MediaInfo_Subscribe);
        arrayList.add(MediaControl.Play);
        arrayList.add(MediaControl.Pause);
        arrayList.add(MediaControl.Stop);
        arrayList.add(MediaControl.Seek);
        arrayList.add(MediaControl.Position);
        arrayList.add(MediaControl.Duration);
        arrayList.add(MediaControl.PlayState);
        arrayList.add(MediaControl.PlayState_Subscribe);
        arrayList.add(MediaControl.Next);
        arrayList.add(MediaControl.Previous);
        arrayList.add(PlaylistControl.Next);
        arrayList.add(PlaylistControl.Previous);
        arrayList.add(PlaylistControl.JumpToTrack);
        arrayList.add(PlaylistControl.SetPlayMode);
        arrayList.add(VolumeControl.Volume_Set);
        arrayList.add(VolumeControl.Volume_Get);
        arrayList.add(VolumeControl.Volume_Up_Down);
        arrayList.add(VolumeControl.Volume_Subscribe);
        arrayList.add(VolumeControl.Mute_Get);
        arrayList.add(VolumeControl.Mute_Set);
        arrayList.add(VolumeControl.Mute_Subscribe);
        setCapabilities(arrayList);
    }

    @Override
    public void volumeDown(final ResponseListener<Object> responseListener) {
        getVolume(new VolumeControl.VolumeListener() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(responseListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Float f2) {
                if (f2.floatValue() <= 0.0d) {
                    Util.postSuccess(responseListener, null);
                    return;
                }
                float floatValue = (float) (f2.floatValue() - 0.01d);
                if (floatValue < 0.0d) {
                    floatValue = 0.0f;
                }
                DLNAService.this.setVolume(floatValue, responseListener);
                Util.postSuccess(responseListener, null);
            }
        });
    }

    @Override
    public void volumeUp(final ResponseListener<Object> responseListener) {
        getVolume(new VolumeControl.VolumeListener() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(responseListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Float f2) {
                if (f2.floatValue() >= 1.0d) {
                    Util.postSuccess(responseListener, null);
                    return;
                }
                float floatValue = (float) (f2.floatValue() + 0.01d);
                if (floatValue > 1.0d) {
                    floatValue = 1.0f;
                }
                DLNAService.this.setVolume(floatValue, responseListener);
                Util.postSuccess(responseListener, null);
            }
        });
    }

    String xmlToString(Node node, boolean z6) throws TransformerException {
        DOMSource dOMSource = new DOMSource(node);
        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);
        Transformer newTransformer = TransformerFactory.newInstance().newTransformer();
        if (!z6) {
            newTransformer.setOutputProperty("omit-xml-declaration", "yes");
        }
        newTransformer.transform(dOMSource, streamResult);
        return stringWriter.toString();
    }

    public DLNAService(ServiceDescription serviceDescription, ServiceConfig serviceConfig, Context context, DLNAHttpServer dLNAHttpServer) {
        super(serviceDescription, serviceConfig);
        this.context = context;
        this.SIDList = new HashMap();
        updateControlURL();
        this.httpServer = dLNAHttpServer;
    }

    private void displayMedia(String str, SubtitleInfo subtitleInfo, String str2, String str3, String str4, String str5, final MediaPlayer.LaunchListener launchListener) {
        String[] split = str2.split("/");
        String str6 = split[0];
        String str7 = split[1];
        if (str6 == null || str6.length() == 0 || str7 == null || str7.length() == 0) {
            Util.postError(launchListener, new ServiceCommandError(0, "You must provide a valid mimeType (audio/*,  video/*, etc)", null));
            return;
        }
        if ("mp3".equals(str7)) {
            str7 = "mpeg";
        }
        String format = String.format("%s/%s", str6, str7);
        ResponseListener<Object> responseListener = new ResponseListener<Object>() {
            @Override
            public void onError(ServiceCommandError serviceCommandError) {
                Util.postError(launchListener, serviceCommandError);
            }

            @Override
            public void onSuccess(Object obj) {
                HashMap hashMap = new HashMap();
                hashMap.put("Speed", "1");
                new ServiceCommand(DLNAService.this, "Play", DLNAService.this.getMessageXml(DLNAService.AV_TRANSPORT_URN, "Play", "0", hashMap), new ResponseListener<Object>() {
                    @Override
                    public void onError(ServiceCommandError serviceCommandError) {
                        Util.postError(launchListener, serviceCommandError);
                    }

                    @Override
                    public void onSuccess(Object obj2) {
                        LaunchSession launchSession = new LaunchSession();
                        launchSession.setService(DLNAService.this);
                        launchSession.setSessionType(LaunchSession.LaunchSessionType.Media);
                        AnonymousClass2 anonymousClass2 = AnonymousClass2.this;
                        MediaPlayer.LaunchListener launchListener2 = launchListener;
                        DLNAService dLNAService = DLNAService.this;
                        Util.postSuccess(launchListener2, new MediaPlayer.MediaLaunchObject(launchSession, dLNAService, dLNAService));
                    }
                }).send();
            }
        };
        String metadata = getMetadata(str, subtitleInfo, format, str3, str4, str5);
        if (metadata == null) {
            Util.postError(launchListener, ServiceCommandError.getError(500));
            return;
        }
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        try {
            linkedHashMap.put("CurrentURI", encodeURL(str));
            linkedHashMap.put("CurrentURIMetaData", metadata);
            new ServiceCommand(this, "SetAVTransportURI", getMessageXml(AV_TRANSPORT_URN, "SetAVTransportURI", "0", linkedHashMap), responseListener).send();
        } catch (Exception unused) {
            Util.postError(launchListener, ServiceCommandError.getError(500));
        }
    }

    @Override
    public void displayImage(MediaInfo mediaInfo, MediaPlayer.LaunchListener launchListener) {
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        String str6 = null;
        if (mediaInfo != null) {
            String url = mediaInfo.getUrl();
            String mimeType = mediaInfo.getMimeType();
            String title = mediaInfo.getTitle();
            String description = mediaInfo.getDescription();
            if (mediaInfo.getImages() != null && mediaInfo.getImages().size() > 0) {
                str6 = mediaInfo.getImages().get(0).getUrl();
            }
            str5 = str6;
            str = url;
            str2 = mimeType;
            str3 = title;
            str4 = description;
        } else {
            str = null;
            str2 = null;
            str3 = null;
            str4 = null;
            str5 = null;
        }
        displayImage(str, str2, str3, str4, str5, launchListener);
    }

    @Override
    public void playMedia(MediaInfo mediaInfo, boolean z6, MediaPlayer.LaunchListener launchListener) {
        String str;
        SubtitleInfo subtitleInfo;
        String str2;
        String str3;
        String str4;
        String str5;
        String str6 = null;
        if (mediaInfo != null) {
            String url = mediaInfo.getUrl();
            SubtitleInfo subtitleInfo2 = mediaInfo.getSubtitleInfo();
            String mimeType = mediaInfo.getMimeType();
            String title = mediaInfo.getTitle();
            String description = mediaInfo.getDescription();
            if (mediaInfo.getImages() != null && mediaInfo.getImages().size() > 0) {
                str6 = mediaInfo.getImages().get(0).getUrl();
            }
            str5 = str6;
            str = url;
            subtitleInfo = subtitleInfo2;
            str2 = mimeType;
            str3 = title;
            str4 = description;
        } else {
            str = null;
            subtitleInfo = null;
            str2 = null;
            str3 = null;
            str4 = null;
            str5 = null;
        }
        displayMedia(str, subtitleInfo, str2, str3, str4, str5, launchListener);
    }

    protected void seek(String str, String str2, ResponseListener<Object> responseListener) {
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("Unit", str);
        linkedHashMap.put("Target", str2);
        new ServiceCommand(this, "Seek", getMessageXml(AV_TRANSPORT_URN, "Seek", "0", linkedHashMap), responseListener).send();
    }
}