Search in sources :

Example 6 with ChannelInfo

use of com.connectsdk.core.ChannelInfo in project butter-android by butterproject.

the class WebOSTVService method getCurrentChannel.

private ServiceCommand<ResponseListener<Object>> getCurrentChannel(boolean isSubscription, final ChannelListener listener) {
    ServiceCommand<ResponseListener<Object>> request;
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            JSONObject jsonObj = (JSONObject) response;
            ChannelInfo channel = parseRawChannelData(jsonObj);
            Util.postSuccess(listener, channel);
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    if (isSubscription) {
        request = new URLServiceSubscription<ResponseListener<Object>>(this, CHANNEL, null, true, responseListener);
    } else
        request = new ServiceCommand<ResponseListener<Object>>(this, CHANNEL, null, true, responseListener);
    request.send();
    return request;
}
Also used : JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ChannelInfo(com.connectsdk.core.ChannelInfo) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 7 with ChannelInfo

use of com.connectsdk.core.ChannelInfo in project butter-android by butterproject.

the class WebOSTVService method parseRawProgramInfo.

private ProgramInfo parseRawProgramInfo(JSONObject programRawData) {
    String programId;
    String programName;
    ProgramInfo programInfo = new ProgramInfo();
    programInfo.setRawData(programRawData);
    programId = programRawData.optString("programId");
    programName = programRawData.optString("programName");
    ChannelInfo channelInfo = parseRawChannelData(programRawData);
    programInfo.setId(programId);
    programInfo.setName(programName);
    programInfo.setChannelInfo(channelInfo);
    return programInfo;
}
Also used : ProgramInfo(com.connectsdk.core.ProgramInfo) ChannelInfo(com.connectsdk.core.ChannelInfo)

Example 8 with ChannelInfo

use of com.connectsdk.core.ChannelInfo in project butter-android by butterproject.

the class WebOSTVService method parseRawChannelData.

private ChannelInfo parseRawChannelData(JSONObject channelRawData) {
    String channelName = null;
    String channelId = null;
    String channelNumber = null;
    int minorNumber;
    int majorNumber;
    ChannelInfo channelInfo = new ChannelInfo();
    channelInfo.setRawData(channelRawData);
    try {
        if (!channelRawData.isNull("channelName"))
            channelName = (String) channelRawData.get("channelName");
        if (!channelRawData.isNull("channelId"))
            channelId = (String) channelRawData.get("channelId");
        channelNumber = channelRawData.optString("channelNumber");
        if (!channelRawData.isNull("majorNumber"))
            majorNumber = (Integer) channelRawData.get("majorNumber");
        else
            majorNumber = parseMajorNumber(channelNumber);
        if (!channelRawData.isNull("minorNumber"))
            minorNumber = (Integer) channelRawData.get("minorNumber");
        else
            minorNumber = parseMinorNumber(channelNumber);
        channelInfo.setName(channelName);
        channelInfo.setId(channelId);
        channelInfo.setNumber(channelNumber);
        channelInfo.setMajorNumber(majorNumber);
        channelInfo.setMinorNumber(minorNumber);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return channelInfo;
}
Also used : JSONException(org.json.JSONException) ChannelInfo(com.connectsdk.core.ChannelInfo) SuppressLint(android.annotation.SuppressLint)

Example 9 with ChannelInfo

use of com.connectsdk.core.ChannelInfo in project butter-android by butterproject.

the class NetcastHttpServer method start.

public void start() {
    // TODO: this method is too complex and should be refactored
    if (running)
        return;
    running = true;
    try {
        welcomeSocket = new ServerSocket(this.port);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    while (running) {
        if (welcomeSocket == null || welcomeSocket.isClosed()) {
            stop();
            break;
        }
        Socket connectionSocket = null;
        BufferedReader inFromClient = null;
        DataOutputStream outToClient = null;
        try {
            connectionSocket = welcomeSocket.accept();
        } catch (IOException ex) {
            ex.printStackTrace();
            // this socket may have been closed, so we'll stop
            stop();
            return;
        }
        String str = null;
        int c;
        StringBuilder sb = new StringBuilder();
        try {
            inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            while ((str = inFromClient.readLine()) != null) {
                if (str.equals("")) {
                    break;
                }
            }
            while ((c = inFromClient.read()) != -1) {
                sb.append((char) c);
                String temp = sb.toString();
                if (temp.endsWith("</envelope>"))
                    break;
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        String body = sb.toString();
        Log.d(Util.T, "got message body: " + body);
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
        String date = dateFormat.format(calendar.getTime());
        String androidOSVersion = android.os.Build.VERSION.RELEASE;
        PrintWriter out = null;
        try {
            outToClient = new DataOutputStream(connectionSocket.getOutputStream());
            out = new PrintWriter(outToClient);
            out.println("HTTP/1.1 200 OK");
            out.println("Server: Android/" + androidOSVersion + " UDAP/2.0 ConnectSDK/1.2.1");
            out.println("Cache-Control: no-store, no-cache, must-revalidate");
            out.println("Date: " + date);
            out.println("Connection: Close");
            out.println("Content-Length: 0");
            out.println();
            out.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                inFromClient.close();
                out.close();
                outToClient.close();
                connectionSocket.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        InputStream stream = null;
        try {
            stream = new ByteArrayInputStream(body.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        NetcastPOSTRequestParser handler = new NetcastPOSTRequestParser();
        SAXParser saxParser;
        try {
            saxParser = saxParserFactory.newSAXParser();
            saxParser.parse(stream, handler);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        if (body.contains("ChannelChanged")) {
            ChannelInfo channel = NetcastChannelParser.parseRawChannelData(handler.getJSONObject());
            Log.d(Util.T, "Channel Changed: " + channel.getNumber());
            for (URLServiceSubscription<?> sub : subscriptions) {
                if (sub.getTarget().equalsIgnoreCase("ChannelChanged")) {
                    for (int i = 0; i < sub.getListeners().size(); i++) {
                        @SuppressWarnings("unchecked") ResponseListener<Object> listener = (ResponseListener<Object>) sub.getListeners().get(i);
                        Util.postSuccess(listener, channel);
                    }
                }
            }
        } else if (body.contains("KeyboardVisible")) {
            boolean focused = false;
            TextInputStatusInfo keyboard = new TextInputStatusInfo();
            keyboard.setRawData(handler.getJSONObject());
            try {
                JSONObject currentWidget = (JSONObject) handler.getJSONObject().get("currentWidget");
                focused = (Boolean) currentWidget.get("focus");
                keyboard.setFocused(focused);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            Log.d(Util.T, "KeyboardFocused?: " + focused);
            for (URLServiceSubscription<?> sub : subscriptions) {
                if (sub.getTarget().equalsIgnoreCase("KeyboardVisible")) {
                    for (int i = 0; i < sub.getListeners().size(); i++) {
                        @SuppressWarnings("unchecked") ResponseListener<Object> listener = (ResponseListener<Object>) sub.getListeners().get(i);
                        Util.postSuccess(listener, keyboard);
                    }
                }
            }
        } else if (body.contains("TextEdited")) {
            System.out.println("TextEdited");
            String newValue = "";
            try {
                newValue = handler.getJSONObject().getString("value");
            } catch (JSONException ex) {
                ex.printStackTrace();
            }
            Util.postSuccess(textChangedListener, newValue);
        } else if (body.contains("3DMode")) {
            try {
                String enabled = (String) handler.getJSONObject().get("value");
                boolean bEnabled;
                bEnabled = enabled.equalsIgnoreCase("true");
                for (URLServiceSubscription<?> sub : subscriptions) {
                    if (sub.getTarget().equalsIgnoreCase("3DMode")) {
                        for (int i = 0; i < sub.getListeners().size(); i++) {
                            @SuppressWarnings("unchecked") ResponseListener<Object> listener = (ResponseListener<Object>) sub.getListeners().get(i);
                            Util.postSuccess(listener, bEnabled);
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) ChannelInfo(com.connectsdk.core.ChannelInfo) SAXException(org.xml.sax.SAXException) URLServiceSubscription(com.connectsdk.service.command.URLServiceSubscription) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TextInputStatusInfo(com.connectsdk.core.TextInputStatusInfo) PrintWriter(java.io.PrintWriter) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Calendar(java.util.Calendar) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JSONException(org.json.JSONException) ServerSocket(java.net.ServerSocket) IOException(java.io.IOException) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) JSONObject(org.json.JSONObject) SimpleDateFormat(java.text.SimpleDateFormat) Socket(java.net.Socket) ServerSocket(java.net.ServerSocket) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Aggregations

ChannelInfo (com.connectsdk.core.ChannelInfo)9 JSONException (org.json.JSONException)7 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)6 JSONObject (org.json.JSONObject)6 ServiceCommand (com.connectsdk.service.command.ServiceCommand)5 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)5 JSONArray (org.json.JSONArray)4 URLServiceSubscription (com.connectsdk.service.command.URLServiceSubscription)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 SAXParser (javax.xml.parsers.SAXParser)3 SAXParserFactory (javax.xml.parsers.SAXParserFactory)3 SAXException (org.xml.sax.SAXException)3 SuppressLint (android.annotation.SuppressLint)2 NetcastChannelParser (com.connectsdk.service.netcast.NetcastChannelParser)2 ArrayList (java.util.ArrayList)2 ProgramInfo (com.connectsdk.core.ProgramInfo)1 ProgramList (com.connectsdk.core.ProgramList)1