Search in sources :

Example 41 with ServiceCommandError

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

the class CastService method launchWebApp.

@Override
public void launchWebApp(final String webAppId, final boolean relaunchIfRunning, final WebAppSession.LaunchListener listener) {
    launchingAppId = webAppId;
    final LaunchWebAppListener launchWebAppListener = new LaunchWebAppListener() {

        @Override
        public void onSuccess(WebAppSession webAppSession) {
            Util.postSuccess(listener, webAppSession);
        }

        @Override
        public void onFailure(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ConnectionListener connectionListener = new ConnectionListener() {

        @Override
        public void onConnected() {
            // TODO Workaround, for some reason, if relaunchIfRunning is false, launchApplication returns 2005 error and cannot launch.
            try {
                if (relaunchIfRunning == false) {
                    Cast.CastApi.joinApplication(mApiClient).setResultCallback(new ResultCallback<Cast.ApplicationConnectionResult>() {

                        @Override
                        public void onResult(ApplicationConnectionResult result) {
                            if (result.getStatus().isSuccess() && result.getApplicationMetadata() != null && result.getApplicationMetadata().getName() != null && result.getApplicationMetadata().getApplicationId().equals(webAppId)) {
                                ApplicationMetadata applicationMetadata = result.getApplicationMetadata();
                                currentAppId = applicationMetadata.getApplicationId();
                                LaunchSession launchSession = LaunchSession.launchSessionForAppId(applicationMetadata.getApplicationId());
                                launchSession.setAppName(applicationMetadata.getName());
                                launchSession.setSessionId(result.getSessionId());
                                launchSession.setSessionType(LaunchSessionType.WebApp);
                                launchSession.setService(CastService.this);
                                CastWebAppSession webAppSession = new CastWebAppSession(launchSession, CastService.this);
                                webAppSession.setMetadata(applicationMetadata);
                                sessions.put(applicationMetadata.getApplicationId(), webAppSession);
                                Util.postSuccess(listener, webAppSession);
                            } else {
                                LaunchOptions options = new LaunchOptions();
                                options.setRelaunchIfRunning(true);
                                try {
                                    Cast.CastApi.launchApplication(mApiClient, webAppId, options).setResultCallback(new ApplicationConnectionResultCallback(launchWebAppListener));
                                } catch (Exception e) {
                                    Util.postError(listener, new ServiceCommandError(0, "Unable to launch", null));
                                }
                            }
                        }
                    });
                } else {
                    LaunchOptions options = new LaunchOptions();
                    options.setRelaunchIfRunning(relaunchIfRunning);
                    Cast.CastApi.launchApplication(mApiClient, webAppId, options).setResultCallback(new ApplicationConnectionResultCallback(launchWebAppListener));
                }
            } catch (Exception e) {
                Util.postError(listener, new ServiceCommandError(0, "Unable to launch", null));
            }
        }
    };
    runCommand(connectionListener);
}
Also used : WebAppSession(com.connectsdk.service.sessions.WebAppSession) CastWebAppSession(com.connectsdk.service.sessions.CastWebAppSession) LaunchSession(com.connectsdk.service.sessions.LaunchSession) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) IOException(java.io.IOException) LaunchOptions(com.google.android.gms.cast.LaunchOptions) ApplicationConnectionResult(com.google.android.gms.cast.Cast.ApplicationConnectionResult) ApplicationMetadata(com.google.android.gms.cast.ApplicationMetadata) CastWebAppSession(com.connectsdk.service.sessions.CastWebAppSession)

Example 42 with ServiceCommandError

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

the class CastWebAppSession method connect.

@Override
public void connect(final ResponseListener<Object> listener) {
    if (castServiceChannel != null) {
        disconnectFromWebApp();
    }
    castServiceChannel = new CastServiceChannel(launchSession.getAppId(), this);
    try {
        Cast.CastApi.setMessageReceivedCallbacks(service.getApiClient(), castServiceChannel.getNamespace(), castServiceChannel);
        Util.postSuccess(listener, null);
    } catch (IOException e) {
        castServiceChannel = null;
        Util.postError(listener, new ServiceCommandError(0, "Failed to create channel", null));
    }
}
Also used : CastServiceChannel(com.connectsdk.service.google_cast.CastServiceChannel) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) IOException(java.io.IOException)

Example 43 with ServiceCommandError

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

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

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

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