use of com.connectsdk.service.command.ServiceCommandError 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);
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class CastWebAppSession method connect.
@Override
public void connect(final ResponseListener<Object> listener) {
if (castServiceChannel != null) {
disconnectFromWebApp();
}
castServiceChannel = new CastServiceChannel(launchSession.getAppId(), this);
try {
Cast.CastApi.setMessageReceivedCallbacks(service.getApiClient(), castServiceChannel.getNamespace(), castServiceChannel);
Util.postSuccess(listener, null);
} catch (IOException e) {
castServiceChannel = null;
Util.postError(listener, new ServiceCommandError(0, "Failed to create channel", null));
}
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVKeyboardInput method connect.
public URLServiceSubscription<TextInputStatusListener> connect(final TextInputStatusListener listener) {
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
JSONObject jsonObj = (JSONObject) response;
TextInputStatusInfo keyboard = parseRawKeyboardData(jsonObj);
Util.postSuccess(listener, keyboard);
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
};
URLServiceSubscription<TextInputStatusListener> subscription = new URLServiceSubscription<TextInputStatusListener>(service, KEYBOARD_INPUT, null, true, responseListener);
subscription.send();
return subscription;
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVKeyboardInput method sendData.
private void sendData() {
waiting = true;
String uri;
String typeTest = toSend.get(0);
JSONObject payload = new JSONObject();
if (typeTest.equals(ENTER)) {
toSend.remove(0);
uri = "ssap://com.webos.service.ime/sendEnterKey";
} else if (typeTest.equals(DELETE)) {
uri = "ssap://com.webos.service.ime/deleteCharacters";
int count = 0;
while (toSend.size() > 0 && toSend.get(0).equals(DELETE)) {
toSend.remove(0);
count++;
}
try {
payload.put("count", count);
} catch (JSONException e) {
e.printStackTrace();
}
} else {
uri = "ssap://com.webos.service.ime/insertText";
StringBuilder sb = new StringBuilder();
while (toSend.size() > 0 && !(toSend.get(0).equals(DELETE) || toSend.get(0).equals(ENTER))) {
String text = toSend.get(0);
sb.append(text);
toSend.remove(0);
}
try {
payload.put("text", sb.toString());
payload.put("replace", 0);
} catch (JSONException e) {
e.printStackTrace();
}
}
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
waiting = false;
if (toSend.size() > 0)
sendData();
}
@Override
public void onError(ServiceCommandError error) {
waiting = false;
if (toSend.size() > 0)
sendData();
}
};
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(service, uri, payload, true, responseListener);
request.send();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class RokuService method launchAppWithInfo.
@Override
public void launchAppWithInfo(final AppInfo appInfo, Object params, final Launcher.AppLaunchListener listener) {
if (appInfo == null || appInfo.getId() == null) {
Util.postError(listener, new ServiceCommandError(-1, "Cannot launch app without valid AppInfo object", appInfo));
return;
}
String baseTargetURL = requestURL("launch", appInfo.getId());
String queryParams = "";
if (params != null && params instanceof JSONObject) {
JSONObject jsonParams = (JSONObject) params;
int count = 0;
Iterator<?> jsonIterator = jsonParams.keys();
while (jsonIterator.hasNext()) {
String key = (String) jsonIterator.next();
String value = null;
try {
value = jsonParams.getString(key);
} catch (JSONException ex) {
}
if (value == null)
continue;
String urlSafeKey = null;
String urlSafeValue = null;
String prefix = (count == 0) ? "?" : "&";
try {
urlSafeKey = URLEncoder.encode(key, "UTF-8");
urlSafeValue = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException ex) {
}
if (urlSafeKey == null || urlSafeValue == null)
continue;
String appendString = prefix + urlSafeKey + "=" + urlSafeValue;
queryParams = queryParams + appendString;
count++;
}
}
String targetURL = null;
if (queryParams.length() > 0)
targetURL = baseTargetURL + queryParams;
else
targetURL = baseTargetURL;
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
Util.postSuccess(listener, new RokuLaunchSession(RokuService.this, appInfo.getId(), appInfo.getName()));
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
};
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, targetURL, null, responseListener);
request.send();
}
Aggregations