Search in sources :

Example 1 with CastWebAppSession

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

Aggregations

ServiceCommandError (com.connectsdk.service.command.ServiceCommandError)1 CastWebAppSession (com.connectsdk.service.sessions.CastWebAppSession)1 LaunchSession (com.connectsdk.service.sessions.LaunchSession)1 WebAppSession (com.connectsdk.service.sessions.WebAppSession)1 ApplicationMetadata (com.google.android.gms.cast.ApplicationMetadata)1 ApplicationConnectionResult (com.google.android.gms.cast.Cast.ApplicationConnectionResult)1 LaunchOptions (com.google.android.gms.cast.LaunchOptions)1 IOException (java.io.IOException)1