use of com.connectsdk.service.command.ServiceCommand 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.command.ServiceCommand 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.command.ServiceCommand 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();
}
use of com.connectsdk.service.command.ServiceCommand in project butter-android by butterproject.
the class AirPlayService method getPlaybackPosition.
private void getPlaybackPosition(final PlaybackPositionListener listener) {
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
String strResponse = (String) response;
long duration = 0;
long position = 0;
StringTokenizer st = new StringTokenizer(strResponse);
while (st.hasMoreTokens()) {
String str = st.nextToken();
if (str.contains("duration")) {
duration = parseTimeValueFromString(st.nextToken());
} else if (str.contains("position")) {
position = parseTimeValueFromString(st.nextToken());
}
}
if (listener != null) {
listener.onGetPlaybackPositionSuccess(duration, position);
}
}
@Override
public void onError(ServiceCommandError error) {
if (listener != null)
listener.onGetPlaybackPositionFailed(error);
}
};
String uri = getRequestURL("scrub");
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, uri, null, responseListener);
request.setHttpMethod(ServiceCommand.TYPE_GET);
request.send();
}
use of com.connectsdk.service.command.ServiceCommand in project butter-android by butterproject.
the class NetcastTVService method getCurrentChannel.
@Override
public void getCurrentChannel(final ChannelListener listener) {
String requestURL = getUDAPRequestURL(UDAP_PATH_DATA, TARGET_CURRENT_CHANNEL);
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
String strObj = (String) response;
try {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
InputStream stream = new ByteArrayInputStream(strObj.getBytes("UTF-8"));
SAXParser saxParser = saxParserFactory.newSAXParser();
NetcastChannelParser parser = new NetcastChannelParser();
saxParser.parse(stream, parser);
JSONArray channelArray = parser.getJSONChannelArray();
if (channelArray.length() > 0) {
JSONObject rawData = (JSONObject) channelArray.get(0);
ChannelInfo channel = NetcastChannelParser.parseRawChannelData(rawData);
Util.postSuccess(listener, channel);
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
};
ServiceCommand<ResponseListener<Object>> request = new ServiceCommand<ResponseListener<Object>>(this, requestURL, null, responseListener);
request.send();
}
Aggregations