Search in sources :

Example 56 with ServiceCommand

use of com.connectsdk.service.command.ServiceCommand in project butter-android by butterproject.

the class WebOSTVService method launchWebApp.

public void launchWebApp(final String webAppId, final JSONObject params, final WebAppSession.LaunchListener listener) {
    if (webAppId == null || webAppId.length() == 0) {
        Util.postError(listener, new ServiceCommandError(-1, "You need to provide a valid webAppId.", null));
        return;
    }
    final WebOSWebAppSession _webAppSession = mWebAppSessions.get(webAppId);
    String uri = "ssap://webapp/launchWebApp";
    JSONObject payload = new JSONObject();
    try {
        payload.put("webAppId", webAppId);
        if (params != null)
            payload.put("urlParams", params);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(final Object response) {
            JSONObject obj = (JSONObject) response;
            LaunchSession launchSession = null;
            WebOSWebAppSession webAppSession = _webAppSession;
            if (webAppSession != null)
                launchSession = webAppSession.launchSession;
            else {
                launchSession = LaunchSession.launchSessionForAppId(webAppId);
                webAppSession = new WebOSWebAppSession(launchSession, WebOSTVService.this);
                mWebAppSessions.put(webAppId, webAppSession);
            }
            launchSession.setService(WebOSTVService.this);
            launchSession.setSessionId(obj.optString("sessionId"));
            launchSession.setSessionType(LaunchSessionType.WebApp);
            launchSession.setRawData(obj);
            Util.postSuccess(listener, webAppSession);
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, payload, true, responseListener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) LaunchSession(com.connectsdk.service.sessions.LaunchSession) JSONException(org.json.JSONException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) WebOSWebAppSession(com.connectsdk.service.sessions.WebOSWebAppSession) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 57 with ServiceCommand

use of com.connectsdk.service.command.ServiceCommand 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 58 with ServiceCommand

use of com.connectsdk.service.command.ServiceCommand 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 59 with ServiceCommand

use of com.connectsdk.service.command.ServiceCommand 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 60 with ServiceCommand

use of com.connectsdk.service.command.ServiceCommand 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)64 ServiceCommand (com.connectsdk.service.command.ServiceCommand)64 JSONObject (org.json.JSONObject)64 ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)55 JSONException (org.json.JSONException)47 SuppressLint (android.annotation.SuppressLint)9 URLServiceSubscription (com.connectsdk.service.command.URLServiceSubscription)9 LaunchSession (com.connectsdk.service.sessions.LaunchSession)9 JSONArray (org.json.JSONArray)8 IOException (java.io.IOException)7 AppInfo (com.connectsdk.core.AppInfo)6 ChannelInfo (com.connectsdk.core.ChannelInfo)5 HashMap (java.util.HashMap)5 ArrayList (java.util.ArrayList)4 HttpConnection (com.connectsdk.etc.helper.HttpConnection)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Context (android.content.Context)2 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)2