use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method connectToWebApp.
public void connectToWebApp(final WebOSWebAppSession webAppSession, final boolean joinOnly, final ResponseListener<Object> connectionListener) {
if (mWebAppSessions == null)
mWebAppSessions = new ConcurrentHashMap<String, WebOSWebAppSession>();
if (mAppToAppIdMappings == null)
mAppToAppIdMappings = new ConcurrentHashMap<String, String>();
if (webAppSession == null || webAppSession.launchSession == null) {
Util.postError(connectionListener, new ServiceCommandError(0, "You must provide a valid LaunchSession object", null));
return;
}
String _appId = webAppSession.launchSession.getAppId();
String _idKey = null;
if (webAppSession.launchSession.getSessionType() == LaunchSession.LaunchSessionType.WebApp)
_idKey = "webAppId";
else
_idKey = "appId";
if (_appId == null || _appId.length() == 0) {
Util.postError(connectionListener, new ServiceCommandError(-1, "You must provide a valid web app session", null));
return;
}
final String appId = _appId;
final String idKey = _idKey;
String uri = "ssap://webapp/connectToApp";
JSONObject payload = new JSONObject();
try {
payload.put(idKey, appId);
} catch (JSONException e) {
e.printStackTrace();
}
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(final Object response) {
JSONObject jsonObj = (JSONObject) response;
String state = jsonObj.optString("state");
if (!state.equalsIgnoreCase("CONNECTED")) {
if (joinOnly && state.equalsIgnoreCase("WAITING_FOR_APP")) {
Util.postError(connectionListener, new ServiceCommandError(0, "Web app is not currently running", null));
}
return;
}
String fullAppId = jsonObj.optString("appId");
if (fullAppId != null && fullAppId.length() != 0) {
if (webAppSession.launchSession.getSessionType() == LaunchSessionType.WebApp)
mAppToAppIdMappings.put(fullAppId, appId);
webAppSession.setFullAppId(fullAppId);
}
if (connectionListener != null) {
Util.runOnUI(new Runnable() {
@Override
public void run() {
connectionListener.onSuccess(response);
}
});
}
}
@Override
public void onError(ServiceCommandError error) {
webAppSession.disconnectFromWebApp();
boolean appChannelDidClose = false;
if (error != null && error.getPayload() != null)
appChannelDidClose = error.getPayload().toString().contains("app channel closed");
if (appChannelDidClose) {
if (webAppSession.getWebAppSessionListener() != null) {
Util.runOnUI(new Runnable() {
@Override
public void run() {
webAppSession.getWebAppSessionListener().onWebAppSessionDisconnect(webAppSession);
}
});
}
} else {
Util.postError(connectionListener, error);
}
}
};
webAppSession.appToAppSubscription = new URLServiceSubscription<ResponseListener<Object>>(webAppSession.socket, uri, payload, true, responseListener);
webAppSession.appToAppSubscription.subscribe();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method getExternalInputList.
@Override
public void getExternalInputList(final ExternalInputListListener listener) {
String uri = "ssap://tv/getExternalInputList";
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
try {
JSONObject jsonObj = (JSONObject) response;
JSONArray devices = (JSONArray) jsonObj.get("devices");
Util.postSuccess(listener, externalnputInfoFromJSONArray(devices));
} 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();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method closeWebApp.
@Override
public void closeWebApp(LaunchSession launchSession, final ResponseListener<Object> listener) {
if (launchSession == null || launchSession.getAppId() == null || launchSession.getAppId().length() == 0) {
Util.postError(listener, new ServiceCommandError(0, "Must provide a valid launch session", null));
return;
}
final WebOSWebAppSession webAppSession = mWebAppSessions.get(launchSession.getAppId());
if (webAppSession != null) {
webAppSession.disconnectFromWebApp();
}
String uri = "ssap://webapp/closeWebApp";
JSONObject payload = new JSONObject();
try {
if (launchSession.getAppId() != null)
payload.put("webAppId", launchSession.getAppId());
if (launchSession.getSessionId() != null)
payload.put("sessionId", launchSession.getSessionId());
} catch (JSONException e) {
e.printStackTrace();
}
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, payload, true, listener);
request.send();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method secureAccessTest.
public void secureAccessTest(final SecureAccessTestListener listener) {
String uri = "ssap://com.webos.service.secondscreen.gateway/test/secure";
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
try {
JSONObject jsonObj = (JSONObject) response;
boolean isSecure = (Boolean) jsonObj.get("returnValue");
Util.postSuccess(listener, isSecure);
} 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();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method sendMessage.
@SuppressWarnings("unused")
private void sendMessage(Object message, LaunchSession launchSession, ResponseListener<Object> listener) {
if (launchSession == null || launchSession.getAppId() == null) {
Util.postError(listener, new ServiceCommandError(0, "Must provide a valid LaunchSession object", null));
return;
}
if (message == null) {
Util.postError(listener, new ServiceCommandError(0, "Cannot send a null message", null));
return;
}
if (socket == null) {
connect();
}
String appId = launchSession.getAppId();
String fullAppId = appId;
if (launchSession.getSessionType() == LaunchSessionType.WebApp)
fullAppId = mAppToAppIdMappings.get(appId);
if (fullAppId == null || fullAppId.length() == 0) {
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", fullAppId);
payload.put("payload", message);
} catch (JSONException e) {
e.printStackTrace();
}
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, null, payload, true, listener);
sendCommand(request);
}
Aggregations