Search in sources :

Example 66 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.

the class WebOSTVServiceSocketClient method handleConnectionLost.

@SuppressWarnings("unchecked")
private void handleConnectionLost(boolean cleanDisconnect, Exception ex) {
    ServiceCommandError error = null;
    if (ex != null || !cleanDisconnect)
        error = new ServiceCommandError(0, "conneciton error", ex);
    if (mListener != null)
        mListener.onCloseWithError(error);
    for (int i = 0; i < requests.size(); i++) {
        ServiceCommand<ResponseListener<Object>> request = (ServiceCommand<ResponseListener<Object>>) requests.get(requests.keyAt(i));
        if (request != null)
            Util.postError(request.getResponseListener(), new ServiceCommandError(0, "connection lost", null));
    }
    requests.clear();
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SuppressLint(android.annotation.SuppressLint)

Example 67 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.

the class WebOSTVServiceSocketClient method handleMessage.

@SuppressWarnings("unchecked")
protected void handleMessage(JSONObject message) {
    Boolean shouldProcess = true;
    if (mListener != null)
        shouldProcess = mListener.onReceiveMessage(message);
    if (!shouldProcess)
        return;
    String type = message.optString("type");
    Object payload = message.opt("payload");
    String strId = message.optString("id");
    Integer id = null;
    ServiceCommand<ResponseListener<Object>> request = null;
    if (isInteger(strId)) {
        id = Integer.valueOf(strId);
        try {
            request = (ServiceCommand<ResponseListener<Object>>) requests.get(id);
        } catch (ClassCastException ex) {
        // since request is assigned to null, don't need to do anything here
        }
    }
    if (type.length() == 0)
        return;
    if ("response".equals(type)) {
        if (request != null) {
            // Log.d(Util.T, "Found requests need to handle response");
            if (payload != null) {
                Util.postSuccess(request.getResponseListener(), payload);
            } else {
                Util.postError(request.getResponseListener(), new ServiceCommandError(-1, "JSON parse error", null));
            }
            if (!(request instanceof URLServiceSubscription)) {
                if (!(payload instanceof JSONObject && ((JSONObject) payload).has("pairingType")))
                    requests.remove(id);
            }
        } else {
            System.err.println("no matching request id: " + strId + ", payload: " + payload.toString());
        }
    } else if ("registered".equals(type)) {
        if (!(mService.getServiceConfig() instanceof WebOSTVServiceConfig)) {
            mService.setServiceConfig(new WebOSTVServiceConfig(mService.getServiceConfig().getServiceUUID()));
        }
        if (payload instanceof JSONObject) {
            String clientKey = ((JSONObject) payload).optString("client-key");
            ((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(clientKey);
            // Track SSL certificate
            // Not the prettiest way to get it, but we don't have direct access to the SSLEngine
            ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate());
            handleRegistered();
            if (id != null)
                requests.remove(id);
        }
    } else if ("error".equals(type)) {
        String error = message.optString("error");
        if (error.length() == 0)
            return;
        int errorCode = -1;
        String errorDesc = null;
        try {
            String[] parts = error.split(" ", 2);
            errorCode = Integer.parseInt(parts[0]);
            errorDesc = parts[1];
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (payload != null) {
            Log.d(Util.T, "Error Payload: " + payload.toString());
        }
        if (message.has("id")) {
            Log.d(Util.T, "Error Desc: " + errorDesc);
            if (request != null) {
                Util.postError(request.getResponseListener(), new ServiceCommandError(errorCode, errorDesc, payload));
                if (!(request instanceof URLServiceSubscription))
                    requests.remove(id);
            }
        }
    } else if ("hello".equals(type)) {
        JSONObject jsonObj = (JSONObject) payload;
        if (mService.getServiceConfig().getServiceUUID() != null) {
            if (!mService.getServiceConfig().getServiceUUID().equals(jsonObj.optString("deviceUUID"))) {
                ((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(null);
                ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate((String) null);
                mService.getServiceConfig().setServiceUUID(null);
                mService.getServiceDescription().setIpAddress(null);
                mService.getServiceDescription().setUUID(null);
                disconnect();
            }
        } else {
            String uuid = jsonObj.optString("deviceUUID");
            mService.getServiceConfig().setServiceUUID(uuid);
            mService.getServiceDescription().setUUID(uuid);
        }
        state = State.REGISTERING;
        sendRegister();
    }
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) SuppressLint(android.annotation.SuppressLint) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) KeyException(java.security.KeyException) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CertificateEncodingException(java.security.cert.CertificateEncodingException) JSONObject(org.json.JSONObject) URLServiceSubscription(com.connectsdk.service.command.URLServiceSubscription) WebOSTVServiceConfig(com.connectsdk.service.config.WebOSTVServiceConfig) JSONObject(org.json.JSONObject)

Example 68 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.

the class WebOSTVServiceSocketClient method sendRegister.

protected void sendRegister() {
    ResponseListener<Object> listener = new ResponseListener<Object>() {

        @Override
        public void onError(ServiceCommandError error) {
            state = State.INITIAL;
            if (mListener != null)
                mListener.onRegistrationFailed(error);
        }

        @Override
        public void onSuccess(Object object) {
            if (object instanceof JSONObject) {
                PairingType pairingType = PairingType.NONE;
                JSONObject jsonObj = (JSONObject) object;
                String type = jsonObj.optString("pairingType");
                if (type.equalsIgnoreCase("PROMPT")) {
                    pairingType = PairingType.FIRST_SCREEN;
                } else if (type.equalsIgnoreCase("PIN")) {
                    pairingType = PairingType.PIN_CODE;
                }
                if (mListener != null)
                    mListener.onBeforeRegister(pairingType);
            }
        }
    };
    int dataId = this.nextRequestId++;
    ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(this, null, null, listener);
    command.setRequestId(dataId);
    JSONObject headers = new JSONObject();
    JSONObject payload = new JSONObject();
    try {
        headers.put("type", "register");
        headers.put("id", dataId);
        if (!(mService.getServiceConfig() instanceof WebOSTVServiceConfig)) {
            mService.setServiceConfig(new WebOSTVServiceConfig(mService.getServiceConfig().getServiceUUID()));
        }
        if (((WebOSTVServiceConfig) mService.getServiceConfig()).getClientKey() != null) {
            payload.put("client-key", ((WebOSTVServiceConfig) mService.getServiceConfig()).getClientKey());
        }
        if (PairingType.PIN_CODE.equals(mService.getPairingType())) {
            payload.put("pairingType", "PIN");
        }
        if (manifest != null) {
            payload.put("manifest", manifest);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    requests.put(dataId, command);
    sendMessage(headers, payload);
}
Also used : JSONObject(org.json.JSONObject) WebOSTVServiceConfig(com.connectsdk.service.config.WebOSTVServiceConfig) PairingType(com.connectsdk.service.DeviceService.PairingType) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SuppressLint(android.annotation.SuppressLint)

Example 69 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.

the class WebOSWebAppSession method next.

@Override
public void next(final ResponseListener<Object> listener) {
    int requestIdNumber = getNextId();
    final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
    JSONObject message = null;
    try {
        message = new JSONObject() {

            {
                put("contentType", namespaceKey + "mediaCommand");
                put("mediaCommand", new JSONObject() {

                    {
                        put("type", "playNext");
                        put("requestId", requestId);
                    }
                });
            }
        };
    } catch (JSONException e) {
        Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
        return;
    }
    ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, listener);
    mActiveCommands.put(requestId, command);
    sendMessage(message, listener);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 70 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.

the class WebOSWebAppSession method seek.

@Override
public void seek(final long position, ResponseListener<Object> listener) {
    if (position < 0) {
        Util.postError(listener, new ServiceCommandError(0, "Must pass a valid positive value", null));
        return;
    }
    int requestIdNumber = getNextId();
    final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
    JSONObject message = null;
    try {
        message = new JSONObject() {

            {
                put("contentType", namespaceKey + "mediaCommand");
                put("mediaCommand", new JSONObject() {

                    {
                        put("type", "seek");
                        put("position", position / 1000);
                        put("requestId", requestId);
                    }
                });
            }
        };
    } catch (JSONException e) {
        Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
    }
    ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, listener);
    mActiveCommands.put(requestId, command);
    sendMessage(message, listener);
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Aggregations

ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)76 JSONObject (org.json.JSONObject)75 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)65 ServiceCommand (com.connectsdk.service.command.ServiceCommand)64 JSONException (org.json.JSONException)54 URLServiceSubscription (com.connectsdk.service.command.URLServiceSubscription)14 SuppressLint (android.annotation.SuppressLint)10 LaunchSession (com.connectsdk.service.sessions.LaunchSession)9 IOException (java.io.IOException)9 JSONArray (org.json.JSONArray)7 AppInfo (com.connectsdk.core.AppInfo)6 ChannelInfo (com.connectsdk.core.ChannelInfo)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)3 HttpConnection (com.connectsdk.etc.helper.HttpConnection)3 InputStream (java.io.InputStream)3 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3