use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class RokuService method launchApp.
@Override
public void launchApp(String appId, AppLaunchListener listener) {
if (appId == null) {
Util.postError(listener, new ServiceCommandError(0, "Must supply a valid app id", null));
return;
}
AppInfo appInfo = new AppInfo();
appInfo.setId(appId);
launchAppWithInfo(appInfo, listener);
}
use of com.connectsdk.service.command.ServiceCommandError 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.ServiceCommandError 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.ServiceCommandError in project butter-android by butterproject.
the class AirPlayService method sendCommand.
@Override
public void sendCommand(final ServiceCommand<?> serviceCommand) {
Util.runInBackground(new Runnable() {
@Override
public void run() {
try {
StringBuilder sb = new StringBuilder();
sb.append("http://").append(serviceDescription.getIpAddress()).append(":").append(serviceDescription.getPort());
sb.append(serviceCommand.getTarget());
HttpConnection connection = HttpConnection.newInstance(URI.create(sb.toString()));
connection.setHeader("User-Agent", "ConnectSDK MediaControl/1.0");
connection.setHeader(X_APPLE_SESSION_ID, mSessionId);
if (password != null) {
String authorization = getAuthenticate(serviceCommand.getHttpMethod(), serviceCommand.getTarget(), authenticate);
connection.setHeader("Authorization", authorization);
}
Object payload = serviceCommand.getPayload();
if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST) || serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
if (payload != null) {
if (payload instanceof String) {
connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, HttpMessage.CONTENT_TYPE_APPLICATION_PLIST);
connection.setPayload(payload.toString());
} else if (payload instanceof byte[]) {
connection.setPayload((byte[]) payload);
}
}
}
if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) {
connection.setMethod(HttpConnection.Method.POST);
} else if (serviceCommand.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_PUT)) {
connection.setMethod(HttpConnection.Method.PUT);
} else {
connection.setHeader("Content-Length", "0");
}
connection.execute();
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
Util.postSuccess(serviceCommand.getResponseListener(), connection.getResponseString());
} else if (code == HttpURLConnection.HTTP_UNAUTHORIZED) {
authenticate = connection.getResponseHeader("WWW-Authenticate");
pendingCommand = serviceCommand;
Util.runOnUI(new Runnable() {
@Override
public void run() {
if (listener != null) {
listener.onPairingRequired(AirPlayService.this, pairingType, null);
}
}
});
} else {
Util.postError(serviceCommand.getResponseListener(), ServiceCommandError.getError(code));
}
} catch (IOException e) {
e.printStackTrace();
Util.postError(serviceCommand.getResponseListener(), new ServiceCommandError(0, e.getMessage(), null));
}
}
});
}
use of com.connectsdk.service.command.ServiceCommandError 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();
}
Aggregations