Search in sources :

Example 71 with ServiceCommandError

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

the class WebOSTVService method displayImage.

@Override
public void displayImage(final String url, final String mimeType, final String title, final String description, final String iconSrc, final MediaPlayer.LaunchListener listener) {
    if ("4.0.0".equalsIgnoreCase(this.serviceDescription.getVersion())) {
        DeviceService dlnaService = this.getDLNAService();
        if (dlnaService != null) {
            MediaPlayer mediaPlayer = dlnaService.getAPI(MediaPlayer.class);
            if (mediaPlayer != null) {
                mediaPlayer.displayImage(url, mimeType, title, description, iconSrc, listener);
                return;
            }
        }
        JSONObject params = null;
        try {
            params = new JSONObject() {

                {
                    put("target", url);
                    put("title", title == null ? NULL : title);
                    put("description", description == null ? NULL : description);
                    put("mimeType", mimeType == null ? NULL : mimeType);
                    put("iconSrc", iconSrc == null ? NULL : iconSrc);
                }
            };
        } catch (JSONException ex) {
            ex.printStackTrace();
            Util.postError(listener, new ServiceCommandError(-1, ex.getLocalizedMessage(), ex));
        }
        if (params != null)
            this.displayMedia(params, listener);
    } else {
        final WebAppSession.LaunchListener webAppLaunchListener = new WebAppSession.LaunchListener() {

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

            @Override
            public void onSuccess(WebAppSession webAppSession) {
                webAppSession.displayImage(url, mimeType, title, description, iconSrc, listener);
            }
        };
        this.getWebAppLauncher().joinWebApp(MEDIA_PLAYER_ID, new WebAppSession.LaunchListener() {

            @Override
            public void onError(ServiceCommandError error) {
                getWebAppLauncher().launchWebApp(MEDIA_PLAYER_ID, webAppLaunchListener);
            }

            @Override
            public void onSuccess(WebAppSession webAppSession) {
                webAppSession.displayImage(url, mimeType, title, description, iconSrc, listener);
            }
        });
    }
}
Also used : JSONObject(org.json.JSONObject) WebAppSession(com.connectsdk.service.sessions.WebAppSession) WebOSWebAppSession(com.connectsdk.service.sessions.WebOSWebAppSession) JSONException(org.json.JSONException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) MediaPlayer(com.connectsdk.service.capability.MediaPlayer)

Example 72 with ServiceCommandError

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

the class WebOSTVService method getAppState.

private ServiceCommand<AppStateListener> getAppState(boolean subscription, LaunchSession launchSession, final AppStateListener listener) {
    ServiceCommand<AppStateListener> request;
    JSONObject params = new JSONObject();
    try {
        params.put("id", launchSession.getAppId());
        params.put("sessionId", launchSession.getSessionId());
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

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

        @Override
        public void onSuccess(Object object) {
            JSONObject json = (JSONObject) object;
            try {
                Util.postSuccess(listener, new AppState(json.getBoolean("running"), json.getBoolean("visible")));
            } catch (JSONException e) {
                Util.postError(listener, new ServiceCommandError(0, "Malformed JSONObject", null));
                e.printStackTrace();
            }
        }
    };
    if (subscription) {
        request = new URLServiceSubscription<Launcher.AppStateListener>(this, APP_STATE, params, true, responseListener);
    } else {
        request = new ServiceCommand<Launcher.AppStateListener>(this, APP_STATE, params, true, responseListener);
    }
    request.send();
    return request;
}
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)

Example 73 with ServiceCommandError

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

the class WebOSTVService method unPinWebApp.

@Override
public void unPinWebApp(String webAppId, final ResponseListener<Object> listener) {
    if (webAppId == null || webAppId.length() == 0) {
        if (listener != null) {
            listener.onError(new ServiceCommandError(-1, "You must provide a valid web app id", null));
        }
        return;
    }
    String uri = "ssap://webapp/removePinnedWebApp";
    JSONObject payload = new JSONObject();
    try {
        payload.put("webAppId", webAppId);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(final Object response) {
            JSONObject obj = (JSONObject) response;
            if (obj.has("pairingType")) {
                notifyPairingRequired();
            } else if (listener != null) {
                listener.onSuccess(response);
            }
        }

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

Example 74 with ServiceCommandError

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

the class WebOSTVServiceSocketClient method handleConnectionLost.

@SuppressWarnings("unchecked")
private void handleConnectionLost(boolean cleanDisconnect, Exception ex) {
    ServiceCommandError error = null;
    if (ex != null || !cleanDisconnect)
        error = new ServiceCommandError(0, "conneciton error", ex);
    if (mListener != null)
        mListener.onCloseWithError(error);
    for (int i = 0; i < requests.size(); i++) {
        ServiceCommand<ResponseListener<Object>> request = (ServiceCommand<ResponseListener<Object>>) requests.get(requests.keyAt(i));
        if (request != null)
            Util.postError(request.getResponseListener(), new ServiceCommandError(0, "connection lost", null));
    }
    requests.clear();
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand) SuppressLint(android.annotation.SuppressLint)

Example 75 with ServiceCommandError

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

the class WebOSTVServiceSocketClient method handleMessage.

@SuppressWarnings("unchecked")
protected void handleMessage(JSONObject message) {
    Boolean shouldProcess = true;
    if (mListener != null)
        shouldProcess = mListener.onReceiveMessage(message);
    if (!shouldProcess)
        return;
    String type = message.optString("type");
    Object payload = message.opt("payload");
    String strId = message.optString("id");
    Integer id = null;
    ServiceCommand<ResponseListener<Object>> request = null;
    if (isInteger(strId)) {
        id = Integer.valueOf(strId);
        try {
            request = (ServiceCommand<ResponseListener<Object>>) requests.get(id);
        } catch (ClassCastException ex) {
        // since request is assigned to null, don't need to do anything here
        }
    }
    if (type.length() == 0)
        return;
    if ("response".equals(type)) {
        if (request != null) {
            // Log.d(Util.T, "Found requests need to handle response");
            if (payload != null) {
                Util.postSuccess(request.getResponseListener(), payload);
            } else {
                Util.postError(request.getResponseListener(), new ServiceCommandError(-1, "JSON parse error", null));
            }
            if (!(request instanceof URLServiceSubscription)) {
                if (!(payload instanceof JSONObject && ((JSONObject) payload).has("pairingType")))
                    requests.remove(id);
            }
        } else {
            System.err.println("no matching request id: " + strId + ", payload: " + payload.toString());
        }
    } else if ("registered".equals(type)) {
        if (!(mService.getServiceConfig() instanceof WebOSTVServiceConfig)) {
            mService.setServiceConfig(new WebOSTVServiceConfig(mService.getServiceConfig().getServiceUUID()));
        }
        if (payload instanceof JSONObject) {
            String clientKey = ((JSONObject) payload).optString("client-key");
            ((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(clientKey);
            // Track SSL certificate
            // Not the prettiest way to get it, but we don't have direct access to the SSLEngine
            ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate());
            handleRegistered();
            if (id != null)
                requests.remove(id);
        }
    } else if ("error".equals(type)) {
        String error = message.optString("error");
        if (error.length() == 0)
            return;
        int errorCode = -1;
        String errorDesc = null;
        try {
            String[] parts = error.split(" ", 2);
            errorCode = Integer.parseInt(parts[0]);
            errorDesc = parts[1];
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (payload != null) {
            Log.d(Util.T, "Error Payload: " + payload.toString());
        }
        if (message.has("id")) {
            Log.d(Util.T, "Error Desc: " + errorDesc);
            if (request != null) {
                Util.postError(request.getResponseListener(), new ServiceCommandError(errorCode, errorDesc, payload));
                if (!(request instanceof URLServiceSubscription))
                    requests.remove(id);
            }
        }
    } else if ("hello".equals(type)) {
        JSONObject jsonObj = (JSONObject) payload;
        if (mService.getServiceConfig().getServiceUUID() != null) {
            if (!mService.getServiceConfig().getServiceUUID().equals(jsonObj.optString("deviceUUID"))) {
                ((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(null);
                ((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate((String) null);
                mService.getServiceConfig().setServiceUUID(null);
                mService.getServiceDescription().setIpAddress(null);
                mService.getServiceDescription().setUUID(null);
                disconnect();
            }
        } else {
            String uuid = jsonObj.optString("deviceUUID");
            mService.getServiceConfig().setServiceUUID(uuid);
            mService.getServiceDescription().setUUID(uuid);
        }
        state = State.REGISTERING;
        sendRegister();
    }
}
Also used : ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) SuppressLint(android.annotation.SuppressLint) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) KeyException(java.security.KeyException) URISyntaxException(java.net.URISyntaxException) JSONException(org.json.JSONException) CertificateException(java.security.cert.CertificateException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CertificateEncodingException(java.security.cert.CertificateEncodingException) JSONObject(org.json.JSONObject) URLServiceSubscription(com.connectsdk.service.command.URLServiceSubscription) WebOSTVServiceConfig(com.connectsdk.service.config.WebOSTVServiceConfig) JSONObject(org.json.JSONObject)

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