Search in sources :

Example 1 with WebAppSession

use of com.connectsdk.service.sessions.WebAppSession in project butter-android by butterproject.

the class WebOSTVService method playMedia.

@Override
public void playMedia(final String url, final String mimeType, final String title, final String description, final String iconSrc, final boolean shouldLoop, 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.playMedia(url, mimeType, title, description, iconSrc, shouldLoop, 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);
                    put("loop", shouldLoop);
                }
            };
        } 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.playMedia(url, mimeType, title, description, iconSrc, shouldLoop, 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.playMedia(url, mimeType, title, description, iconSrc, shouldLoop, 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 2 with WebAppSession

use of com.connectsdk.service.sessions.WebAppSession 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 3 with WebAppSession

use of com.connectsdk.service.sessions.WebAppSession in project butter-android by butterproject.

the class CastService method playMedia.

private void playMedia(final com.google.android.gms.cast.MediaInfo mediaInformation, final String mediaAppId, final LaunchListener listener) {
    final ApplicationConnectionResultCallback webAppLaunchCallback = new ApplicationConnectionResultCallback(new LaunchWebAppListener() {

        @Override
        public void onSuccess(final WebAppSession webAppSession) {
            ConnectionListener connectionListener = new ConnectionListener() {

                @Override
                public void onConnected() {
                    try {
                        mMediaPlayer.load(mApiClient, mediaInformation, true).setResultCallback(new ResultCallback<RemoteMediaPlayer.MediaChannelResult>() {

                            @Override
                            public void onResult(MediaChannelResult result) {
                                Status status = result.getStatus();
                                if (status.isSuccess()) {
                                    webAppSession.launchSession.setSessionType(LaunchSessionType.Media);
                                    // White text, black outline, no background
                                    TextTrackStyle textTrackStyle = new TextTrackStyle();
                                    textTrackStyle.setForegroundColor(Color.parseColor("#FFFFFFFF"));
                                    textTrackStyle.setBackgroundColor(Color.parseColor("#01000000"));
                                    textTrackStyle.setWindowType(TextTrackStyle.WINDOW_TYPE_NONE);
                                    textTrackStyle.setEdgeType(TextTrackStyle.EDGE_TYPE_OUTLINE);
                                    textTrackStyle.setEdgeColor(Color.BLACK);
                                    textTrackStyle.setFontGenericFamily(TextTrackStyle.FONT_FAMILY_SANS_SERIF);
                                    mMediaPlayer.setTextTrackStyle(mApiClient, textTrackStyle);
                                    mMediaPlayer.setActiveMediaTracks(mApiClient, new long[] { 1 });
                                    Util.postSuccess(listener, new MediaLaunchObject(webAppSession.launchSession, CastService.this));
                                } else {
                                    Util.postError(listener, new ServiceCommandError(status.getStatusCode(), status.getStatusMessage(), status));
                                }
                            }
                        });
                    } catch (Exception e) {
                        Util.postError(listener, new ServiceCommandError(0, "Unable to load", null));
                    }
                }
            };
            runCommand(connectionListener);
        }

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

        @Override
        public void onConnected() {
            boolean relaunchIfRunning = false;
            try {
                if (Cast.CastApi.getApplicationStatus(mApiClient) == null || (!mediaAppId.equals(currentAppId))) {
                    relaunchIfRunning = true;
                }
                LaunchOptions options = new LaunchOptions();
                options.setRelaunchIfRunning(relaunchIfRunning);
                Cast.CastApi.launchApplication(mApiClient, mediaAppId, options).setResultCallback(webAppLaunchCallback);
            } catch (Exception e) {
                Util.postError(listener, new ServiceCommandError(0, "Unable to launch", null));
            }
        }
    };
    runCommand(connectionListener);
}
Also used : Status(com.google.android.gms.common.api.Status) ResultCallback(com.google.android.gms.common.api.ResultCallback) RemoteMediaPlayer(com.google.android.gms.cast.RemoteMediaPlayer) WebAppSession(com.connectsdk.service.sessions.WebAppSession) CastWebAppSession(com.connectsdk.service.sessions.CastWebAppSession) ServiceCommandError(com.connectsdk.service.command.ServiceCommandError) IOException(java.io.IOException) LaunchOptions(com.google.android.gms.cast.LaunchOptions) TextTrackStyle(com.google.android.gms.cast.TextTrackStyle) MediaChannelResult(com.google.android.gms.cast.RemoteMediaPlayer.MediaChannelResult)

Example 4 with WebAppSession

use of com.connectsdk.service.sessions.WebAppSession 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)

Aggregations

ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)4 WebAppSession (com.connectsdk.service.sessions.WebAppSession)4 MediaPlayer (com.connectsdk.service.capability.MediaPlayer)2 CastWebAppSession (com.connectsdk.service.sessions.CastWebAppSession)2 WebOSWebAppSession (com.connectsdk.service.sessions.WebOSWebAppSession)2 LaunchOptions (com.google.android.gms.cast.LaunchOptions)2 IOException (java.io.IOException)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 LaunchSession (com.connectsdk.service.sessions.LaunchSession)1 ApplicationMetadata (com.google.android.gms.cast.ApplicationMetadata)1 ApplicationConnectionResult (com.google.android.gms.cast.Cast.ApplicationConnectionResult)1 RemoteMediaPlayer (com.google.android.gms.cast.RemoteMediaPlayer)1 MediaChannelResult (com.google.android.gms.cast.RemoteMediaPlayer.MediaChannelResult)1 TextTrackStyle (com.google.android.gms.cast.TextTrackStyle)1 ResultCallback (com.google.android.gms.common.api.ResultCallback)1 Status (com.google.android.gms.common.api.Status)1