use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVServiceSocketClient method sendRegister.
protected void sendRegister() {
ResponseListener<Object> listener = new ResponseListener<Object>() {
@Override
public void onError(ServiceCommandError error) {
state = State.INITIAL;
if (mListener != null)
mListener.onRegistrationFailed(error);
}
@Override
public void onSuccess(Object object) {
if (object instanceof JSONObject) {
PairingType pairingType = PairingType.NONE;
JSONObject jsonObj = (JSONObject) object;
String type = jsonObj.optString("pairingType");
if (type.equalsIgnoreCase("PROMPT")) {
pairingType = PairingType.FIRST_SCREEN;
} else if (type.equalsIgnoreCase("PIN")) {
pairingType = PairingType.PIN_CODE;
}
if (mListener != null)
mListener.onBeforeRegister(pairingType);
}
}
};
int dataId = this.nextRequestId++;
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(this, null, null, listener);
command.setRequestId(dataId);
JSONObject headers = new JSONObject();
JSONObject payload = new JSONObject();
try {
headers.put("type", "register");
headers.put("id", dataId);
if (!(mService.getServiceConfig() instanceof WebOSTVServiceConfig)) {
mService.setServiceConfig(new WebOSTVServiceConfig(mService.getServiceConfig().getServiceUUID()));
}
if (((WebOSTVServiceConfig) mService.getServiceConfig()).getClientKey() != null) {
payload.put("client-key", ((WebOSTVServiceConfig) mService.getServiceConfig()).getClientKey());
}
if (PairingType.PIN_CODE.equals(mService.getPairingType())) {
payload.put("pairingType", "PIN");
}
if (manifest != null) {
payload.put("manifest", manifest);
}
} catch (JSONException e) {
e.printStackTrace();
}
requests.put(dataId, command);
sendMessage(headers, payload);
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method next.
@Override
public void next(final ResponseListener<Object> listener) {
int requestIdNumber = getNextId();
final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
JSONObject message = null;
try {
message = new JSONObject() {
{
put("contentType", namespaceKey + "mediaCommand");
put("mediaCommand", new JSONObject() {
{
put("type", "playNext");
put("requestId", requestId);
}
});
}
};
} catch (JSONException e) {
Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
return;
}
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, listener);
mActiveCommands.put(requestId, command);
sendMessage(message, listener);
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method seek.
@Override
public void seek(final long position, ResponseListener<Object> listener) {
if (position < 0) {
Util.postError(listener, new ServiceCommandError(0, "Must pass a valid positive value", null));
return;
}
int requestIdNumber = getNextId();
final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
JSONObject message = null;
try {
message = new JSONObject() {
{
put("contentType", namespaceKey + "mediaCommand");
put("mediaCommand", new JSONObject() {
{
put("type", "seek");
put("position", position / 1000);
put("requestId", requestId);
}
});
}
};
} catch (JSONException e) {
Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
}
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, listener);
mActiveCommands.put(requestId, command);
sendMessage(message, listener);
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method handleMediaCommandResponse.
@SuppressWarnings("unchecked")
public void handleMediaCommandResponse(final JSONObject payload) {
String requestID = payload.optString("requestId");
if (requestID.length() == 0)
return;
final ServiceCommand<ResponseListener<Object>> command = (ServiceCommand<ResponseListener<Object>>) mActiveCommands.get(requestID);
if (command == null)
return;
String mError = payload.optString("error");
if (mError.length() != 0) {
Util.postError(command.getResponseListener(), new ServiceCommandError(0, mError, null));
} else {
Util.postSuccess(command.getResponseListener(), payload);
}
mActiveCommands.remove(requestID);
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method getDuration.
@Override
public void getDuration(final DurationListener listener) {
int requestIdNumber = getNextId();
final String requestId = String.format(Locale.US, "req%d", requestIdNumber);
JSONObject message = null;
try {
message = new JSONObject() {
{
put("contentType", namespaceKey + "mediaCommand");
put("mediaCommand", new JSONObject() {
{
put("type", "getDuration");
put("requestId", requestId);
}
});
}
};
} catch (JSONException e) {
Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
}
ServiceCommand<ResponseListener<Object>> command = new ServiceCommand<ResponseListener<Object>>(null, null, null, new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
try {
long position = ((JSONObject) response).getLong("duration");
Util.postSuccess(listener, position * 1000);
} catch (JSONException e) {
Util.postError(listener, new ServiceCommandError(0, "JSON Parse error", null));
}
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
});
mActiveCommands.put(requestId, command);
sendMessage(message, new ResponseListener<Object>() {
@Override
public void onSuccess(Object response) {
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
});
}
Aggregations