use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVServiceSocketClient method sendPairingKey.
public void sendPairingKey(String pairingKey) {
ResponseListener<Object> listener = new ResponseListener<Object>() {
@Override
public void onError(ServiceCommandError error) {
state = State.INITIAL;
if (mListener != null)
mListener.onFailWithError(error);
}
@Override
public void onSuccess(Object object) {
}
};
String uri = "ssap://pairing/setPin";
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", "request");
headers.put("id", dataId);
headers.put("uri", uri);
payload.put("pin", pairingKey);
} 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 connect.
private void connect(final Boolean joinOnly, final ResponseListener<Object> connectionListener) {
if (socket != null && socket.getState() == WebOSTVServiceSocketClient.State.CONNECTING) {
if (connectionListener != null) {
connectionListener.onError(new ServiceCommandError(0, "You have a connection request pending, please wait until it has finished", null));
}
return;
}
if (isConnected()) {
if (connectionListener != null)
connectionListener.onSuccess(null);
return;
}
mConnectionListener = new ResponseListener<ServiceCommand<ResponseListener<Object>>>() {
@Override
public void onError(ServiceCommandError error) {
if (socket != null)
disconnectFromWebApp();
if (connectionListener != null) {
if (error == null) {
error = new ServiceCommandError(0, "Unknown error connecting to web app", null);
}
connectionListener.onError(error);
}
}
@Override
public void onSuccess(ServiceCommand<ResponseListener<Object>> object) {
ResponseListener<Object> finalConnectionListener = new ResponseListener<Object>() {
@Override
public void onError(ServiceCommandError error) {
disconnectFromWebApp();
if (connectionListener != null)
connectionListener.onError(error);
}
@Override
public void onSuccess(Object object) {
connected = true;
if (connectionListener != null)
connectionListener.onSuccess(object);
}
};
service.connectToWebApp(WebOSWebAppSession.this, joinOnly, finalConnectionListener);
}
};
if (socket != null) {
if (socket.isConnected())
mConnectionListener.onSuccess(null);
else
socket.connect();
} else {
socket = new WebOSTVServiceSocketClient(service, WebOSTVServiceSocketClient.getURI(service));
socket.setListener(mSocketListener);
socket.connect();
}
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method handleMediaEvent.
public void handleMediaEvent(JSONObject payload) {
String type = payload.optString("type");
if (type.length() == 0) {
String errorMsg = payload.optString("error");
if (errorMsg.length() == 0) {
return;
} else {
Log.w(Util.T, "Play State Error: " + errorMsg);
if (mPlayStateSubscription != null) {
for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
Util.postError(listener, new ServiceCommandError(errorMsg));
}
}
}
}
if (type.equals("playState")) {
if (mPlayStateSubscription == null)
return;
String playStateString = payload.optString(type);
if (playStateString.length() == 0)
return;
final MediaControl.PlayStateStatus playState = parsePlayState(playStateString);
for (PlayStateListener listener : mPlayStateSubscription.getListeners()) {
Util.postSuccess(listener, playState);
}
}
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method getPosition.
@Override
public void getPosition(final PositionListener 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", "getPosition");
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("position");
Util.postSuccess(listener, position * 1000);
} catch (JSONException e) {
this.onError(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);
}
});
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSWebAppSession method sendP2PMessage.
private void sendP2PMessage(final Object message, final ResponseListener<Object> listener) {
JSONObject _payload = new JSONObject();
try {
_payload.put("type", "p2p");
_payload.put("to", getFullAppId());
_payload.put("payload", message);
} catch (JSONException ex) {
// do nothing
}
if (isConnected()) {
socket.sendMessage(_payload, null);
Util.postSuccess(listener, null);
} else {
ResponseListener<Object> connectListener = new ResponseListener<Object>() {
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
@Override
public void onSuccess(Object object) {
sendP2PMessage(message, listener);
}
};
connect(connectListener);
}
}
Aggregations