Search in sources :

Example 16 with ServiceCommandError

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

the class WebOSTVService method connectToWebApp.

public void connectToWebApp(final WebOSWebAppSession webAppSession, final boolean joinOnly, final ResponseListener<Object> connectionListener) {
    if (mWebAppSessions == null)
        mWebAppSessions = new ConcurrentHashMap<String, WebOSWebAppSession>();
    if (mAppToAppIdMappings == null)
        mAppToAppIdMappings = new ConcurrentHashMap<String, String>();
    if (webAppSession == null || webAppSession.launchSession == null) {
        Util.postError(connectionListener, new ServiceCommandError(0, "You must provide a valid LaunchSession object", null));
        return;
    }
    String _appId = webAppSession.launchSession.getAppId();
    String _idKey = null;
    if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.WebApp)
        _idKey = "webAppId";
    else
        _idKey = "appId";
    if (_appId == null || _appId.length() == 0) {
        Util.postError(connectionListener, new ServiceCommandError(-1, "You must provide a valid web app session", null));
        return;
    }
    final String appId = _appId;
    final String idKey = _idKey;
    String uri = "ssap://webapp/connectToApp";
    JSONObject payload = new JSONObject();
    try {
        payload.put(idKey, appId);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(final Object response) {
            JSONObject jsonObj = (JSONObject) response;
            String state = jsonObj.optString("state");
            if (!state.equalsIgnoreCase("CONNECTED")) {
                if (joinOnly && state.equalsIgnoreCase("WAITING_FOR_APP")) {
                    Util.postError(connectionListener, new ServiceCommandError(0, "Web app is not currently running", null));
                }
                return;
            }
            String fullAppId = jsonObj.optString("appId");
            if (fullAppId != null && fullAppId.length() != 0) {
                if (webAppSession.launchSession.getSessionType() == LaunchSessionType.WebApp)
                    mAppToAppIdMappings.put(fullAppId, appId);
                webAppSession.setFullAppId(fullAppId);
            }
            if (connectionListener != null) {
                Util.runOnUI(new Runnable() {

                    @Override
                    public void run() {
                        connectionListener.onSuccess(response);
                    }
                });
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            webAppSession.disconnectFromWebApp();
            boolean appChannelDidClose = false;
            if (error != null && error.getPayload() != null)
                appChannelDidClose = error.getPayload().toString().contains("app channel closed");
            if (appChannelDidClose) {
                if (webAppSession.getWebAppSessionListener() != null) {
                    Util.runOnUI(new Runnable() {

                        @Override
                        public void run() {
                            webAppSession.getWebAppSessionListener().onWebAppSessionDisconnect(webAppSession);
                        }
                    });
                }
            } else {
                Util.postError(connectionListener, error);
            }
        }
    };
    webAppSession.appToAppSubscription = new URLServiceSubscription<ResponseListener<Object>>(webAppSession.socket, uri, payload, true, responseListener);
    webAppSession.appToAppSubscription.subscribe();
}
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) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 17 with ServiceCommandError

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

the class WebOSTVService method getExternalInputList.

@Override
public void getExternalInputList(final ExternalInputListListener listener) {
    String uri = "ssap://tv/getExternalInputList";
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            try {
                JSONObject jsonObj = (JSONObject) response;
                JSONArray devices = (JSONArray) jsonObj.get("devices");
                Util.postSuccess(listener, externalnputInfoFromJSONArray(devices));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, true, responseListener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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)

Example 18 with ServiceCommandError

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

the class WebOSTVService method closeWebApp.

@Override
public void closeWebApp(LaunchSession launchSession, final ResponseListener<Object> listener) {
    if (launchSession == null || launchSession.getAppId() == null || launchSession.getAppId().length() == 0) {
        Util.postError(listener, new ServiceCommandError(0, "Must provide a valid launch session", null));
        return;
    }
    final WebOSWebAppSession webAppSession = mWebAppSessions.get(launchSession.getAppId());
    if (webAppSession != null) {
        webAppSession.disconnectFromWebApp();
    }
    String uri = "ssap://webapp/closeWebApp";
    JSONObject payload = new JSONObject();
    try {
        if (launchSession.getAppId() != null)
            payload.put("webAppId", launchSession.getAppId());
        if (launchSession.getSessionId() != null)
            payload.put("sessionId", launchSession.getSessionId());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, payload, true, listener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) 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 19 with ServiceCommandError

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

the class WebOSTVService method secureAccessTest.

public void secureAccessTest(final SecureAccessTestListener listener) {
    String uri = "ssap://com.webos.service.secondscreen.gateway/test/secure";
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            try {
                JSONObject jsonObj = (JSONObject) response;
                boolean isSecure = (Boolean) jsonObj.get("returnValue");
                Util.postSuccess(listener, isSecure);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, true, responseListener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) 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)

Example 20 with ServiceCommandError

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

the class WebOSTVService method sendMessage.

@SuppressWarnings("unused")
private void sendMessage(Object message, LaunchSession launchSession, ResponseListener<Object> listener) {
    if (launchSession == null || launchSession.getAppId() == null) {
        Util.postError(listener, new ServiceCommandError(0, "Must provide a valid LaunchSession object", null));
        return;
    }
    if (message == null) {
        Util.postError(listener, new ServiceCommandError(0, "Cannot send a null message", null));
        return;
    }
    if (socket == null) {
        connect();
    }
    String appId = launchSession.getAppId();
    String fullAppId = appId;
    if (launchSession.getSessionType() == LaunchSessionType.WebApp)
        fullAppId = mAppToAppIdMappings.get(appId);
    if (fullAppId == null || fullAppId.length() == 0) {
        Util.postError(listener, new ServiceCommandError(-1, "You must provide a valid LaunchSession to send messages to", null));
        return;
    }
    JSONObject payload = new JSONObject();
    try {
        payload.put("type", "p2p");
        payload.put("to", fullAppId);
        payload.put("payload", message);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, null, payload, true, listener);
    sendCommand(request);
}
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

ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)84 JSONObject (org.json.JSONObject)70 ResponseListener (com.connectsdk.service.capability.listeners.ResponseListener)65 ServiceCommand (com.connectsdk.service.command.ServiceCommand)55 JSONException (org.json.JSONException)47 URLServiceSubscription (com.connectsdk.service.command.URLServiceSubscription)13 IOException (java.io.IOException)13 LaunchSession (com.connectsdk.service.sessions.LaunchSession)12 AppInfo (com.connectsdk.core.AppInfo)11 SuppressLint (android.annotation.SuppressLint)8 JSONArray (org.json.JSONArray)7 WebOSWebAppSession (com.connectsdk.service.sessions.WebOSWebAppSession)6 ChannelInfo (com.connectsdk.core.ChannelInfo)5 HttpConnection (com.connectsdk.etc.helper.HttpConnection)4 WebAppSession (com.connectsdk.service.sessions.WebAppSession)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 MediaInfo (com.connectsdk.core.MediaInfo)3 MediaPlayer (com.connectsdk.service.capability.MediaPlayer)3