diff --git a/src/com/connectsdk/service/WebOSTVService.java b/src/com/connectsdk/service/WebOSTVService.java index 78e361e8..6c9830a7 100644 --- a/src/com/connectsdk/service/WebOSTVService.java +++ b/src/com/connectsdk/service/WebOSTVService.java @@ -33,7 +33,6 @@ import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Arrays; -import java.util.Enumeration; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; @@ -231,7 +230,8 @@ public interface LaunchPointsListener extends ResponseListener { } WebSocketClient mouseWebSocket; WebOSTVKeyboardInput keyboardInput; - + + ConcurrentHashMap mWebAppIdMappings; ConcurrentHashMap>> mAppToAppSubscriptions; ConcurrentHashMap mAppToAppMessageListeners; @@ -260,6 +260,7 @@ public WebOSTVService(ServiceDescription serviceDescription, ServiceConfig servi state = State.INITIAL; pairingType = PairingType.FIRST_SCREEN; + mWebAppIdMappings = new ConcurrentHashMap(); mAppToAppSubscriptions = new ConcurrentHashMap>>(); mAppToAppMessageListeners = new ConcurrentHashMap(); @@ -2289,44 +2290,47 @@ public void connectToWebApp(final WebOSWebAppSession webAppSession, final boolea return; } - String _fullAppId = null; - String _subscriptionKey = null; + String _appId = webAppSession.launchSession.getAppId(); String _idKey = null; - if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.WebApp) { - // TODO: don't hard code com.webos.app.webapphost - _fullAppId = String.format("com.webos.app.webapphost.%s", webAppSession.launchSession.getAppId()); - _subscriptionKey = webAppSession.launchSession.getAppId(); + if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.WebApp) _idKey = "webAppId"; - } else if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.App) { - _fullAppId = _subscriptionKey = webAppSession.launchSession.getAppId(); + else if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.App) _idKey = "appId"; - } - if (_fullAppId == null || _fullAppId.length() == 0) { + if (_appId == null || _appId.length() == 0) { Util.postError(connectionListener, new ServiceCommandError(-1, "You must provide a valid web app session", null)); return; } - URLServiceSubscription> appToAppSubscription = (URLServiceSubscription>) mAppToAppSubscriptions.get(_subscriptionKey); + String _subscriptionKey = null; + + if (mWebAppIdMappings.get(_appId) != null) + _subscriptionKey = mWebAppIdMappings.get(_appId); + else + _subscriptionKey = _appId; - if (appToAppSubscription != null) { - mAppToAppMessageListeners.put(_fullAppId, webAppSession.messageHandler); + if (_subscriptionKey != null && _subscriptionKey.length() > 0) + { + URLServiceSubscription> appToAppSubscription = (URLServiceSubscription>) mAppToAppSubscriptions.get(_appId); - Util.postSuccess(connectionListener, webAppSession); - return; + if (appToAppSubscription != null) { + mAppToAppMessageListeners.put(_subscriptionKey, webAppSession.messageHandler); + + Util.postSuccess(connectionListener, webAppSession); + return; + } } - final String fullAppId = _fullAppId; - final String subscriptionKey = _subscriptionKey; + final String appId = _appId; final String idKey = _idKey; String uri = "ssap://webapp/connectToApp"; JSONObject payload = new JSONObject(); try { - payload.put(idKey, subscriptionKey); + payload.put(idKey, appId); } catch (JSONException e) { e.printStackTrace(); } @@ -2347,25 +2351,13 @@ public void onSuccess(final Object response) { return; } - String appId = jsonObj.optString("appId"); + String fullAppId = jsonObj.optString("appId"); - if (appId != null && appId.length() != 0) { - mAppToAppMessageListeners.put(appId, webAppSession.messageHandler); - - JSONObject newRawData; - - if (webAppSession.launchSession.getRawData() != null) - newRawData = (JSONObject) webAppSession.launchSession.getRawData(); - else - newRawData = new JSONObject(); - - try { - newRawData.put(idKey, appId); - } catch (JSONException ex) { - ex.printStackTrace(); - } - - webAppSession.launchSession.setRawData(newRawData); + if (fullAppId != null && fullAppId.length() != 0) { + if (webAppSession.launchSession.getSessionType() == LaunchSessionType.WebApp) + mWebAppIdMappings.put(appId, fullAppId); + + mAppToAppMessageListeners.put(fullAppId, webAppSession.messageHandler); } if (connectionListener != null) { @@ -2381,14 +2373,21 @@ public void run() { @Override public void onError(ServiceCommandError error) { - ServiceSubscription> connectionSubscription = mAppToAppSubscriptions.get(subscriptionKey); + ServiceSubscription> connectionSubscription = mAppToAppSubscriptions.get(appId); if (connectionSubscription != null) { if (!serviceDescription.getVersion().contains("4.0.2")) connectionSubscription.unsubscribe(); - mAppToAppSubscriptions.remove(subscriptionKey); - mAppToAppMessageListeners.remove(fullAppId); + mAppToAppSubscriptions.remove(appId); + + String fullAppId = appId; + + if (webAppSession.launchSession.getSessionType() == LaunchSessionType.WebApp) + fullAppId = mWebAppIdMappings.get(appId); + + if (fullAppId != null && fullAppId.length() > 0) + mAppToAppMessageListeners.remove(fullAppId); } boolean appChannelDidClose = false; @@ -2418,7 +2417,7 @@ public void run() { URLServiceSubscription> subscription = new URLServiceSubscription>(this, uri, payload, true, responseListener); subscription.subscribe(); - mAppToAppSubscriptions.put(subscriptionKey, subscription); + mAppToAppSubscriptions.put(appId, subscription); } /* Join a native/installed webOS app */ @@ -2481,34 +2480,25 @@ public void joinWebApp(String webAppId, WebAppSession.LaunchListener listener) { joinWebApp(launchSession, listener); } - public void disconnectFromWebApp(final WebOSWebAppSession webAppSession) { - JSONObject rawData = (JSONObject) webAppSession.launchSession.getRawData(); - String appId = null; - - try { appId = rawData.getString("webAppId"); } catch (JSONException ex) { } + public void disconnectFromWebApp(final WebOSWebAppSession webAppSession) { + String appId = webAppSession.launchSession.getAppId(); + String fullAppId = appId; - if (appId == null && mAppToAppMessageListeners != null) { - Enumeration enumeration = mAppToAppMessageListeners.keys(); - - while (enumeration.hasMoreElements()) { - String key = enumeration.nextElement(); - - if (key.contains(webAppSession.launchSession.getAppId())) { - appId = key; - break; - } - } - } + if (webAppSession.launchSession.getSessionType() == LaunchSessionType.WebApp) + fullAppId = mWebAppIdMappings.get(appId); - if (appId != null) - mAppToAppMessageListeners.remove(appId); + if (fullAppId != null) + mAppToAppMessageListeners.remove(fullAppId); - ServiceSubscription> connectionSubscription = mAppToAppSubscriptions.remove(appId); + ServiceSubscription> connectionSubscription = mAppToAppSubscriptions.get(appId); if (connectionSubscription != null) { if (!this.serviceDescription.getVersion().contains("4.0.2")) { connectionSubscription.unsubscribe(); + mAppToAppSubscriptions.remove(appId); } + + mAppToAppMessageListeners.remove(fullAppId); } if (webAppSession.getWebAppSessionListener() != null) { @@ -2538,12 +2528,25 @@ private void sendMessage(Object message, LaunchSession launchSession, ResponseLi connect(); } - String appId = "com.webos.app.webapphost." + launchSession.getAppId(); + String appId = launchSession.getAppId(); + String fullAppId = appId; + + if (launchSession.getSessionType() == LaunchSessionType.WebApp) + fullAppId = mWebAppIdMappings.get(appId); + + if (fullAppId == null || fullAppId.length() == 0) + { + if (listener != null) + Util.postError(listener, new ServiceCommandError(-1, "You must provide a valid LaunchSession to send messages to", null)); + + return; + } + JSONObject payload = new JSONObject(); try { payload.put("type", "p2p"); - payload.put("to", appId); + payload.put("to", fullAppId); payload.put("payload", message); Object payTest = payload.get("payload");