Search in sources :

Example 41 with ResponseListener

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

the class WebOSTVKeyboardInput method connect.

public URLServiceSubscription<TextInputStatusListener> connect(final TextInputStatusListener listener) {
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            JSONObject jsonObj = (JSONObject) response;
            TextInputStatusInfo keyboard = parseRawKeyboardData(jsonObj);
            Util.postSuccess(listener, keyboard);
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    URLServiceSubscription<TextInputStatusListener> subscription = new URLServiceSubscription<TextInputStatusListener>(service, KEYBOARD_INPUT, null, true, responseListener);
    subscription.send();
    return subscription;
}
Also used : JSONObject(org.json.JSONObject) URLServiceSubscription(com.connectsdk.service.command.URLServiceSubscription) TextInputStatusListener(com.connectsdk.service.capability.TextInputControl.TextInputStatusListener) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) TextInputStatusInfo(com.connectsdk.core.TextInputStatusInfo)

Example 42 with ResponseListener

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

the class WebOSTVKeyboardInput method sendData.

private void sendData() {
    waiting = true;
    String uri;
    String typeTest = toSend.get(0);
    JSONObject payload = new JSONObject();
    if (typeTest.equals(ENTER)) {
        toSend.remove(0);
        uri = "ssap://com.webos.service.ime/sendEnterKey";
    } else if (typeTest.equals(DELETE)) {
        uri = "ssap://com.webos.service.ime/deleteCharacters";
        int count = 0;
        while (toSend.size() > 0 && toSend.get(0).equals(DELETE)) {
            toSend.remove(0);
            count++;
        }
        try {
            payload.put("count", count);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        uri = "ssap://com.webos.service.ime/insertText";
        StringBuilder sb = new StringBuilder();
        while (toSend.size() > 0 && !(toSend.get(0).equals(DELETE) || toSend.get(0).equals(ENTER))) {
            String text = toSend.get(0);
            sb.append(text);
            toSend.remove(0);
        }
        try {
            payload.put("text", sb.toString());
            payload.put("replace", 0);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            waiting = false;
            if (toSend.size() > 0)
                sendData();
        }

        @Override
        public void onError(ServiceCommandError error) {
            waiting = false;
            if (toSend.size() > 0)
                sendData();
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(service, uri, payload, 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 43 with ResponseListener

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

the class RokuService method launchAppWithInfo.

@Override
public void launchAppWithInfo(final AppInfo appInfo, Object params, final Launcher.AppLaunchListener listener) {
    if (appInfo == null || appInfo.getId() == null) {
        Util.postError(listener, new ServiceCommandError(-1, "Cannot launch app without valid AppInfo object", appInfo));
        return;
    }
    String baseTargetURL = requestURL("launch", appInfo.getId());
    String queryParams = "";
    if (params != null && params instanceof JSONObject) {
        JSONObject jsonParams = (JSONObject) params;
        int count = 0;
        Iterator<?> jsonIterator = jsonParams.keys();
        while (jsonIterator.hasNext()) {
            String key = (String) jsonIterator.next();
            String value = null;
            try {
                value = jsonParams.getString(key);
            } catch (JSONException ex) {
            }
            if (value == null)
                continue;
            String urlSafeKey = null;
            String urlSafeValue = null;
            String prefix = (count == 0) ? "?" : "&";
            try {
                urlSafeKey = URLEncoder.encode(key, "UTF-8");
                urlSafeValue = URLEncoder.encode(value, "UTF-8");
            } catch (UnsupportedEncodingException ex) {
            }
            if (urlSafeKey == null || urlSafeValue == null)
                continue;
            String appendString = prefix + urlSafeKey + "=" + urlSafeValue;
            queryParams = queryParams + appendString;
            count++;
        }
    }
    String targetURL = null;
    if (queryParams.length() > 0)
        targetURL = baseTargetURL + queryParams;
    else
        targetURL = baseTargetURL;
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            Util.postSuccess(listener, new RokuLaunchSession(RokuService.this, appInfo.getId(), appInfo.getName()));
        }

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

Example 44 with ResponseListener

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

the class RokuService method sendCommand.

@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
    Util.runInBackground(new Runnable() {

        @SuppressWarnings("unchecked")
        @Override
        public void run() {
            ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
            Object payload = command.getPayload();
            try {
                Log.d("", "RESP " + command.getTarget());
                HttpConnection connection = HttpConnection.newInstance(URI.create(command.getTarget()));
                if (command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                    connection.setMethod(HttpConnection.Method.POST);
                    if (payload != null) {
                        connection.setPayload(payload.toString());
                    }
                }
                connection.execute();
                int code = connection.getResponseCode();
                Log.d("", "RESP " + code);
                if (code == 200 || code == 201) {
                    Util.postSuccess(command.getResponseListener(), connection.getResponseString());
                } else {
                    Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                e.printStackTrace();
                Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
            }
        }
    });
}
Also used : HttpConnection(com.connectsdk.etc.helper.HttpConnection) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) IOException(java.io.IOException) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 45 with ResponseListener

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

the class DIALService method getAppState.

private void getAppState(String appName, final AppStateListener listener) {
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            String str = (String) response;
            String[] stateTAG = new String[2];
            stateTAG[0] = "<state>";
            stateTAG[1] = "</state>";
            int start = str.indexOf(stateTAG[0]);
            int end = str.indexOf(stateTAG[1]);
            if (start != -1 && end != -1) {
                start += stateTAG[0].length();
                String state = str.substring(start, end);
                AppState appState = new AppState("running".equals(state), "running".equals(state));
                Util.postSuccess(listener, appState);
            // TODO: This isn't actually reporting anything.
            // if (listener != null)
            // listener.onAppStateSuccess(state);
            } else {
                Util.postError(listener, new ServiceCommandError(0, "Malformed response for app state", null));
            }
        }

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