Search in sources :

Example 31 with ServiceCommandError

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

the class WebOSTVServiceSocketClient method sendPairingKey.

public void sendPairingKey(String pairingKey) {
    ResponseListener<Object> listener = new ResponseListener<Object>() {

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

        @Override
        public void onSuccess(Object object) {
        }
    };
    String uri = "ssap://pairing/setPin";
    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", "request");
        headers.put("id", dataId);
        headers.put("uri", uri);
        payload.put("pin", pairingKey);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    requests.put(dataId, command);
    sendMessage(headers, payload);
}
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) SuppressLint(android.annotation.SuppressLint)

Example 32 with ServiceCommandError

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

the class WebOSWebAppSession method connect.

private void connect(final Boolean joinOnly, final ResponseListener<Object> connectionListener) {
    if (socket != null && socket.getState() == WebOSTVServiceSocketClient.State.CONNECTING) {
        if (connectionListener != null) {
            connectionListener.onError(new ServiceCommandError(0, "You have a connection request pending,  please wait until it has finished", null));
        }
        return;
    }
    if (isConnected()) {
        if (connectionListener != null)
            connectionListener.onSuccess(null);
        return;
    }
    mConnectionListener = new ResponseListener<ServiceCommand<ResponseListener<Object>>>() {

        @Override
        public void onError(ServiceCommandError error) {
            if (socket != null)
                disconnectFromWebApp();
            if (connectionListener != null) {
                if (error == null) {
                    error = new ServiceCommandError(0, "Unknown error connecting to web app", null);
                }
                connectionListener.onError(error);
            }
        }

        @Override
        public void onSuccess(ServiceCommand<ResponseListener<Object>> object) {
            ResponseListener<Object> finalConnectionListener = new ResponseListener<Object>() {

                @Override
                public void onError(ServiceCommandError error) {
                    disconnectFromWebApp();
                    if (connectionListener != null)
                        connectionListener.onError(error);
                }

                @Override
                public void onSuccess(Object object) {
                    connected = true;
                    if (connectionListener != null)
                        connectionListener.onSuccess(object);
                }
            };
            service.connectToWebApp(WebOSWebAppSession.this, joinOnly, finalConnectionListener);
        }
    };
    if (socket != null) {
        if (socket.isConnected())
            mConnectionListener.onSuccess(null);
        else
            socket.connect();
    } else {
        socket = new WebOSTVServiceSocketClient(service, WebOSTVServiceSocketClient.getURI(service));
        socket.setListener(mSocketListener);
        socket.connect();
    }
}
Also used : WebOSTVServiceSocketClient(com.connectsdk.service.webos.WebOSTVServiceSocketClient) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 33 with ServiceCommandError

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

the class WebOSWebAppSession method handleMediaEvent.

public void handleMediaEvent(JSONObject payload) {
    String type = payload.optString("type");
    if (type.length() == 0) {
        String errorMsg = payload.optString("error");
        if (errorMsg.length() == 0) {
            return;
        } else {
            Log.w(Util.T, "Play State Error: " + errorMsg);
            if (mPlayStateSubscription != null) {
                for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
                    Util.postError(listener, new ServiceCommandError(errorMsg));
                }
            }
        }
    }
    if (type.equals("playState")) {
        if (mPlayStateSubscription == null)
            return;
        String playStateString = payload.optString(type);
        if (playStateString.length() == 0)
            return;
        final MediaControl.PlayStateStatus playState = parsePlayState(playStateString);
        for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
            Util.postSuccess(listener, playState);
        }
    }
}
Also used : MediaControl(com.connectsdk.service.capability.MediaControl) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError)

Example 34 with ServiceCommandError

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

the class WebOSWebAppSession method getPosition.

@Override
public void getPosition(final PositionListener 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", "getPosition");
                        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, new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            try {
                long position = ((JSONObject) response).getLong("position");
                Util.postSuccess(listener, position * 1000);
            } catch (JSONException e) {
                this.onError(new ServiceCommandError(0, "JSON Parse error", null));
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    });
    mActiveCommands.put(requestId, command);
    sendMessage(message, new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    });
}
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 35 with ServiceCommandError

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

the class WebOSWebAppSession method sendP2PMessage.

private void sendP2PMessage(final Object message, final ResponseListener<Object> listener) {
    JSONObject _payload = new JSONObject();
    try {
        _payload.put("type", "p2p");
        _payload.put("to", getFullAppId());
        _payload.put("payload", message);
    } catch (JSONException ex) {
    // do nothing
    }
    if (isConnected()) {
        socket.sendMessage(_payload, null);
        Util.postSuccess(listener, null);
    } else {
        ResponseListener<Object> connectListener = new ResponseListener<Object>() {

            @Override
            public void onError(ServiceCommandError error) {
                Util.postError(listener, error);
            }

            @Override
            public void onSuccess(Object object) {
                sendP2PMessage(message, listener);
            }
        };
        connect(connectListener);
    }
}
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)

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