Search in sources :

Example 36 with ServiceCommand

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

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

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

Example 39 with ServiceCommand

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

the class AirPlayService method getPlaybackPosition.

private void getPlaybackPosition(final PlaybackPositionListener listener) {
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            String strResponse = (String) response;
            long duration = 0;
            long position = 0;
            StringTokenizer st = new StringTokenizer(strResponse);
            while (st.hasMoreTokens()) {
                String str = st.nextToken();
                if (str.contains("duration")) {
                    duration = parseTimeValueFromString(st.nextToken());
                } else if (str.contains("position")) {
                    position = parseTimeValueFromString(st.nextToken());
                }
            }
            if (listener != null) {
                listener.onGetPlaybackPositionSuccess(duration, position);
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            if (listener != null)
                listener.onGetPlaybackPositionFailed(error);
        }
    };
    String uri = getRequestURL("scrub");
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, responseListener);
    request.setHttpMethod(ServiceCommand.TYPE_GET);
    request.send();
}
Also used : StringTokenizer(java.util.StringTokenizer) JSONObject(org.json.JSONObject) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 40 with ServiceCommand

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

the class NetcastTVService method getCurrentChannel.

@Override
public void getCurrentChannel(final ChannelListener listener) {
    String requestURL = getUDAPRequestURL(UDAP_PATH_DATA, TARGET_CURRENT_CHANNEL);
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            String strObj = (String) response;
            try {
                SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                InputStream stream = new ByteArrayInputStream(strObj.getBytes("UTF-8"));
                SAXParser saxParser = saxParserFactory.newSAXParser();
                NetcastChannelParser parser = new NetcastChannelParser();
                saxParser.parse(stream, parser);
                JSONArray channelArray = parser.getJSONChannelArray();
                if (channelArray.length() > 0) {
                    JSONObject rawData = (JSONObject) channelArray.get(0);
                    ChannelInfo channel = NetcastChannelParser.parseRawChannelData(rawData);
                    Util.postSuccess(listener, channel);
                }
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, requestURL, null, responseListener);
    request.send();
}
Also used : NetcastChannelParser(com.connectsdk.service.netcast.NetcastChannelParser) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) JSONObject(org.json.JSONObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

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