Search in sources :

Example 11 with ResponseListener

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

the class WebOSTVService method closeMedia.

@Override
public void closeMedia(LaunchSession launchSession, ResponseListener<Object> listener) {
    JSONObject payload = new JSONObject();
    try {
        if (launchSession.getAppId() != null && launchSession.getAppId().length() > 0)
            payload.put("id", launchSession.getAppId());
        if (launchSession.getSessionId() != null && launchSession.getSessionId().length() > 0)
            payload.put("sessionId", launchSession.getSessionId());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(launchSession.getService(), CLOSE_MEDIA_URI, payload, true, listener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 12 with ResponseListener

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

the class WebOSTVService method getChannelList.

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

        @Override
        public void onSuccess(Object response) {
            try {
                JSONObject jsonObj = (JSONObject) response;
                ArrayList<ChannelInfo> list = new ArrayList<ChannelInfo>();
                JSONArray array = (JSONArray) jsonObj.get("channelList");
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = (JSONObject) array.get(i);
                    ChannelInfo channel = parseRawChannelData(object);
                    list.add(channel);
                }
                Util.postSuccess(listener, list);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

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

Example 13 with ResponseListener

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

the class WebOSTVService method pinWebApp.

@Override
public void pinWebApp(String webAppId, final ResponseListener<Object> listener) {
    if (webAppId == null || webAppId.length() == 0) {
        if (listener != null) {
            listener.onError(new ServiceCommandError(-1, "You must provide a valid web app id", null));
        }
        return;
    }
    String uri = "ssap://webapp/pinWebApp";
    JSONObject payload = new JSONObject();
    try {
        payload.put("webAppId", webAppId);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(final Object response) {
            JSONObject obj = (JSONObject) response;
            if (obj.has("pairingType")) {
                notifyPairingRequired();
            } else if (listener != null) {
                listener.onSuccess(response);
            }
        }

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

Example 14 with ResponseListener

use of com.connectsdk.service.capability.listeners.ResponseListener 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 15 with ResponseListener

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

the class WebOSTVService method setVolume.

@Override
public void setVolume(float volume, ResponseListener<Object> listener) {
    String uri = "ssap://audio/setVolume";
    JSONObject payload = new JSONObject();
    int intVolume = (int) Math.round(volume * 100.0f);
    try {
        payload.put("volume", intVolume);
    } 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) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SuppressLint(android.annotation.SuppressLint)

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