Search in sources :

Example 46 with ServiceCommandError

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

the class RokuService method launchApp.

@Override
public void launchApp(String appId, AppLaunchListener listener) {
    if (appId == null) {
        Util.postError(listener, new ServiceCommandError(0, "Must supply a valid app id", null));
        return;
    }
    AppInfo appInfo = new AppInfo();
    appInfo.setId(appId);
    launchAppWithInfo(appInfo, listener);
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) AppInfo(com.connectsdk.core.AppInfo)

Example 47 with ServiceCommandError

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

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

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

the class AirPlayService method sendCommand.

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

        @Override
        public void run() {
            try {
                StringBuilder sb = new StringBuilder();
                sb.append("http://").append(serviceDescription.getIpAddress()).append(":").append(serviceDescription.getPort());
                sb.append(serviceCommand.getTarget());
                HttpConnection connection = HttpConnection.newInstance(URI.create(sb.toString()));
                connection.setHeader("User-Agent", "ConnectSDK MediaControl/1.0");
                connection.setHeader(X_APPLE_SESSION_ID, mSessionId);
                if (password != null) {
                    String authorization = getAuthenticate(serviceCommand.getHttpMethod(), serviceCommand.getTarget(), authenticate);
                    connection.setHeader("Authorization", authorization);
                }
                Object payload = serviceCommand.getPayload();
                if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST) || serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
                    if (payload != null) {
                        if (payload instanceof String) {
                            connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, HttpMessage.CONTENT_TYPE_APPLICATION_PLIST);
                            connection.setPayload(payload.toString());
                        } else if (payload instanceof byte[]) {
                            connection.setPayload((byte[]) payload);
                        }
                    }
                }
                if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
                    connection.setMethod(HttpConnection.Method.POST);
                } else if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
                    connection.setMethod(HttpConnection.Method.PUT);
                } else {
                    connection.setHeader("Content-Length", "0");
                }
                connection.execute();
                int code = connection.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    Util.postSuccess(serviceCommand.getResponseListener(), connection.getResponseString());
                } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
                    authenticate = connection.getResponseHeader("WWW-Authenticate");
                    pendingCommand = serviceCommand;
                    Util.runOnUI(new Runnable() {

                        @Override
                        public void run() {
                            if (listener != null) {
                                listener.onPairingRequired(AirPlayService.this, pairingType, null);
                            }
                        }
                    });
                } else {
                    Util.postError(serviceCommand.getResponseListener(), ServiceCommandError.getError(code));
                }
            } catch (IOException e) {
                e.printStackTrace();
                Util.postError(serviceCommand.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) IOException(java.io.IOException)

Example 50 with ServiceCommandError

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

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