Skip to content

Commit

Permalink
Fixed app to app on recent builds of webOS which changes the namespac…
Browse files Browse the repository at this point in the history
…e of web apps
  • Loading branch information
Jeremy White committed Jun 13, 2014
1 parent 5d86697 commit 1ccd467
Showing 1 changed file with 67 additions and 64 deletions.
131 changes: 67 additions & 64 deletions src/com/connectsdk/service/WebOSTVService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -231,7 +230,8 @@ public interface LaunchPointsListener extends ResponseListener<JSONArray> { }

WebSocketClient mouseWebSocket;
WebOSTVKeyboardInput keyboardInput;


ConcurrentHashMap<String, String> mWebAppIdMappings;
ConcurrentHashMap<String, URLServiceSubscription<ResponseListener<Object>>> mAppToAppSubscriptions;
ConcurrentHashMap<String, MessageListener> mAppToAppMessageListeners;

Expand Down Expand Up @@ -260,6 +260,7 @@ public WebOSTVService(ServiceDescription serviceDescription, ServiceConfig servi
state = State.INITIAL;
pairingType = PairingType.FIRST_SCREEN;

mWebAppIdMappings = new ConcurrentHashMap<String, String>();
mAppToAppSubscriptions = new ConcurrentHashMap<String, URLServiceSubscription<ResponseListener<Object>>>();
mAppToAppMessageListeners = new ConcurrentHashMap<String, MessageListener>();

Expand Down Expand Up @@ -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<ResponseListener<Object>> appToAppSubscription = (URLServiceSubscription<ResponseListener<Object>>) 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<ResponseListener<Object>> appToAppSubscription = (URLServiceSubscription<ResponseListener<Object>>) 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();
}
Expand All @@ -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) {
Expand All @@ -2381,14 +2373,21 @@ public void run() {

@Override
public void onError(ServiceCommandError error) {
ServiceSubscription<ResponseListener<Object>> connectionSubscription = mAppToAppSubscriptions.get(subscriptionKey);
ServiceSubscription<ResponseListener<Object>> 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;
Expand Down Expand Up @@ -2418,7 +2417,7 @@ public void run() {
URLServiceSubscription<ResponseListener<Object>> subscription = new URLServiceSubscription<ResponseListener<Object>>(this, uri, payload, true, responseListener);
subscription.subscribe();

mAppToAppSubscriptions.put(subscriptionKey, subscription);
mAppToAppSubscriptions.put(appId, subscription);
}

/* Join a native/installed webOS app */
Expand Down Expand Up @@ -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<String> 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<ResponseListener<Object>> connectionSubscription = mAppToAppSubscriptions.remove(appId);
ServiceSubscription<ResponseListener<Object>> 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) {
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 1ccd467

Please sign in to comment.