Search in sources :

Example 26 with ServiceCommand

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

the class WebOSTVService method getLaunchPoints.

public void getLaunchPoints(final LaunchPointsListener listener) {
    String uri = "ssap://com.webos.applicationManager/listLaunchPoints";
    ResponseListener<Object> responseListener = new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            try {
                JSONObject jsonObj = (JSONObject) response;
                JSONArray launchPoints = (JSONArray) jsonObj.get("launchPoints");
                Util.postSuccess(listener, launchPoints);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    };
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, true, responseListener);
    request.send();
}
Also used : JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) 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 27 with ServiceCommand

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

the class WebOSTVServiceSocketClient method sendPairingKey.

public void sendPairingKey(String pairingKey) {
    ResponseListener<Object> listener = new ResponseListener<Object>() {

        @Override
        public void onError(ServiceCommandError error) {
            state = State.INITIAL;
            if (mListener != null)
                mListener.onFailWithError(error);
        }

        @Override
        public void onSuccess(Object object) {
        }
    };
    String uri = "ssap://pairing/setPin";
    int dataId = this.nextRequestId++;
    ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(this, null, null, listener);
    command.setRequestId(dataId);
    JSONObject headers = new JSONObject();
    JSONObject payload = new JSONObject();
    try {
        headers.put("type", "request");
        headers.put("id", dataId);
        headers.put("uri", uri);
        payload.put("pin", pairingKey);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    requests.put(dataId, command);
    sendMessage(headers, payload);
}
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) SuppressLint(android.annotation.SuppressLint)

Example 28 with ServiceCommand

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

the class WebOSTVServiceSocketClient method helloTV.

private void helloTV() {
    Context context = DiscoveryManager.getInstance().getContext();
    PackageManager packageManager = context.getPackageManager();
    // app Id
    String packageName = context.getPackageName();
    // SDK Version
    String sdkVersion = DiscoveryManager.CONNECT_SDK_VERSION;
    // Device Model
    String deviceModel = Build.MODEL;
    // OS Version
    String OSVersion = String.valueOf(android.os.Build.VERSION.SDK_INT);
    // resolution
    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();
    @SuppressWarnings("deprecation") int // deprecated, but still needed for supporting API levels 10-12
    width = display.getWidth();
    @SuppressWarnings("deprecation") int // deprecated, but still needed for supporting API levels 10-12
    height = display.getHeight();
    String screenResolution = String.format("%dx%d", width, height);
    // app Name
    ApplicationInfo applicationInfo;
    try {
        applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
    } catch (final NameNotFoundException e) {
        applicationInfo = null;
    }
    String applicationName = (String) (applicationInfo != null ? packageManager.getApplicationLabel(applicationInfo) : "(unknown)");
    // app Region
    Locale current = context.getResources().getConfiguration().locale;
    String appRegion = current.getDisplayCountry();
    JSONObject payload = new JSONObject();
    try {
        payload.put("sdkVersion", sdkVersion);
        payload.put("deviceModel", deviceModel);
        payload.put("OSVersion", OSVersion);
        payload.put("resolution", screenResolution);
        payload.put("appId", packageName);
        payload.put("appName", applicationName);
        payload.put("appRegion", appRegion);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    int dataId = this.nextRequestId++;
    JSONObject sendData = new JSONObject();
    try {
        sendData.put("id", dataId);
        sendData.put("type", "hello");
        sendData.put("payload", payload);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, null, sendData, true, null);
    this.sendCommandImmediately(request);
}
Also used : Context(android.content.Context) SSLContext(javax.net.ssl.SSLContext) Locale(java.util.Locale) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException) ApplicationInfo(android.content.pm.ApplicationInfo) JSONException(org.json.JSONException) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) SuppressLint(android.annotation.SuppressLint) WindowManager(android.view.WindowManager) PackageManager(android.content.pm.PackageManager) JSONObject(org.json.JSONObject) JSONObject(org.json.JSONObject) ServiceCommand(com.connectsdk.service.command.ServiceCommand) Display(android.view.Display)

Example 29 with ServiceCommand

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

the class WebOSWebAppSession method connect.

private void connect(final Boolean joinOnly, final ResponseListener<Object> connectionListener) {
    if (socket != null && socket.getState() == WebOSTVServiceSocketClient.State.CONNECTING) {
        if (connectionListener != null) {
            connectionListener.onError(new ServiceCommandError(0, "You have a connection request pending,  please wait until it has finished", null));
        }
        return;
    }
    if (isConnected()) {
        if (connectionListener != null)
            connectionListener.onSuccess(null);
        return;
    }
    mConnectionListener = new ResponseListener<ServiceCommand<ResponseListener<Object>>>() {

        @Override
        public void onError(ServiceCommandError error) {
            if (socket != null)
                disconnectFromWebApp();
            if (connectionListener != null) {
                if (error == null) {
                    error = new ServiceCommandError(0, "Unknown error connecting to web app", null);
                }
                connectionListener.onError(error);
            }
        }

        @Override
        public void onSuccess(ServiceCommand<ResponseListener<Object>> object) {
            ResponseListener<Object> finalConnectionListener = new ResponseListener<Object>() {

                @Override
                public void onError(ServiceCommandError error) {
                    disconnectFromWebApp();
                    if (connectionListener != null)
                        connectionListener.onError(error);
                }

                @Override
                public void onSuccess(Object object) {
                    connected = true;
                    if (connectionListener != null)
                        connectionListener.onSuccess(object);
                }
            };
            service.connectToWebApp(WebOSWebAppSession.this, joinOnly, finalConnectionListener);
        }
    };
    if (socket != null) {
        if (socket.isConnected())
            mConnectionListener.onSuccess(null);
        else
            socket.connect();
    } else {
        socket = new WebOSTVServiceSocketClient(service, WebOSTVServiceSocketClient.getURI(service));
        socket.setListener(mSocketListener);
        socket.connect();
    }
}
Also used : WebOSTVServiceSocketClient(com.connectsdk.service.webos.WebOSTVServiceSocketClient) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

Example 30 with ServiceCommand

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

the class WebOSWebAppSession method getPosition.

@Override
public void getPosition(final PositionListener listener) {
    int requestIdNumber = getNextId();
    final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
    JSONObject message = null;
    try {
        message = new JSONObject() {

            {
                put("contentType", namespaceKey + "mediaCommand");
                put("mediaCommand", new JSONObject() {

                    {
                        put("type", "getPosition");
                        put("requestId", requestId);
                    }
                });
            }
        };
    } catch (JSONException e) {
        Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
    }
    ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
            try {
                long position = ((JSONObject) response).getLong("position");
                Util.postSuccess(listener, position * 1000);
            } catch (JSONException e) {
                this.onError(new ServiceCommandError(0, "JSON Parse error", null));
            }
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    });
    mActiveCommands.put(requestId, command);
    sendMessage(message, new ResponseListener<Object>() {

        @Override
        public void onSuccess(Object response) {
        }

        @Override
        public void onError(ServiceCommandError error) {
            Util.postError(listener, error);
        }
    });
}
Also used : JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) JSONObject(org.json.JSONObject) ResponseListener(com.connectsdk.service.capability.listeners.ResponseListener) ServiceCommand(com.connectsdk.service.command.ServiceCommand)

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