use of com.connectsdk.service.capability.listeners.ResponseListener 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.capability.listeners.ResponseListener 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.capability.listeners.ResponseListener 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();
}
use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.
the class RokuService method sendCommand.
@Override
public void sendCommand(final ServiceCommand<?> mCommand) {
Util.runInBackground(new Runnable() {
@SuppressWarnings("unchecked")
@Override
public void run() {
ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mCommand;
Object payload = command.getPayload();
try {
Log.d("", "RESP " + command.getTarget());
HttpConnection connection = HttpConnection.newInstance(URI.create(command.getTarget()));
if (command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
connection.setMethod(HttpConnection.Method.POST);
if (payload != null) {
connection.setPayload(payload.toString());
}
}
connection.execute();
int code = connection.getResponseCode();
Log.d("", "RESP " + code);
if (code == 200 || code == 201) {
Util.postSuccess(command.getResponseListener(), connection.getResponseString());
} else {
Util.postError(command.getResponseListener(), ServiceCommandError.getError(code));
}
} catch (IOException e) {
e.printStackTrace();
Util.postError(command.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
}
}
});
}
use of com.connectsdk.service.capability.listeners.ResponseListener in project butter-android by butterproject.
the class DIALService method getAppState.
private void getAppState(String appName, final AppStateListener listener) {
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
String str = (String) response;
String[] stateTAG = new String[2];
stateTAG[0] = "<state>";
stateTAG[1] = "</state>";
int start = str.indexOf(stateTAG[0]);
int end = str.indexOf(stateTAG[1]);
if (start != -1 && end != -1) {
start += stateTAG[0].length();
String state = str.substring(start, end);
AppState appState = new AppState("running".equals(state), "running".equals(state));
Util.postSuccess(listener, appState);
// TODO: This isn't actually reporting anything.
// if (listener != null)
// listener.onAppStateSuccess(state);
} else {
Util.postError(listener, new ServiceCommandError(0, "Malformed response for app state", null));
}
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
};
String uri = requestURL(appName);
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, responseListener);
request.setHttpMethod(ServiceCommand.TYPE_GET);
request.send();
}
Aggregations