use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method displayImage.
@Override
public void displayImage(final String url, final String mimeType, final String title, final String description, final String iconSrc, final MediaPlayer.LaunchListener listener) {
if ("4.0.0".equalsIgnoreCase(this.serviceDescription.getVersion())) {
DeviceService dlnaService = this.getDLNAService();
if (dlnaService != null) {
MediaPlayer mediaPlayer = dlnaService.getAPI(MediaPlayer.class);
if (mediaPlayer != null) {
mediaPlayer.displayImage(url, mimeType, title, description, iconSrc, listener);
return;
}
}
JSONObject params = null;
try {
params = new JSONObject() {
{
put("target", url);
put("title", title == null ? NULL : title);
put("description", description == null ? NULL : description);
put("mimeType", mimeType == null ? NULL : mimeType);
put("iconSrc", iconSrc == null ? NULL : iconSrc);
}
};
} catch (JSONException ex) {
ex.printStackTrace();
Util.postError(listener, new ServiceCommandError(-1, ex.getLocalizedMessage(), ex));
}
if (params != null)
this.displayMedia(params, listener);
} else {
final WebAppSession.LaunchListener webAppLaunchListener = new WebAppSession.LaunchListener() {
@Override
public void onError(ServiceCommandError error) {
listener.onError(error);
}
@Override
public void onSuccess(WebAppSession webAppSession) {
webAppSession.displayImage(url, mimeType, title, description, iconSrc, listener);
}
};
this.getWebAppLauncher().joinWebApp(MEDIA_PLAYER_ID, new WebAppSession.LaunchListener() {
@Override
public void onError(ServiceCommandError error) {
getWebAppLauncher().launchWebApp(MEDIA_PLAYER_ID, webAppLaunchListener);
}
@Override
public void onSuccess(WebAppSession webAppSession) {
webAppSession.displayImage(url, mimeType, title, description, iconSrc, listener);
}
});
}
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method getAppState.
private ServiceCommand<AppStateListener> getAppState(boolean subscription, LaunchSession launchSession, final AppStateListener listener) {
ServiceCommand<AppStateListener> request;
JSONObject params = new JSONObject();
try {
params.put("id", launchSession.getAppId());
params.put("sessionId", launchSession.getSessionId());
} catch (JSONException e) {
e.printStackTrace();
}
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
@Override
public void onSuccess(Object object) {
JSONObject json = (JSONObject) object;
try {
Util.postSuccess(listener, new AppState(json.getBoolean("running"), json.getBoolean("visible")));
} catch (JSONException e) {
Util.postError(listener, new ServiceCommandError(0, "Malformed JSONObject", null));
e.printStackTrace();
}
}
};
if (subscription) {
request = new URLServiceSubscription<Launcher.AppStateListener>(this, APP_STATE, params, true, responseListener);
} else {
request = new ServiceCommand<Launcher.AppStateListener>(this, APP_STATE, params, true, responseListener);
}
request.send();
return request;
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVService method unPinWebApp.
@Override
public void unPinWebApp(String webAppId, final ResponseListener<Object> listener) {
if (webAppId == null || webAppId.length() == 0) {
if (listener != null) {
listener.onError(new ServiceCommandError(-1, "You must provide a valid web app id", null));
}
return;
}
String uri = "ssap://webapp/removePinnedWebApp";
JSONObject payload = new JSONObject();
try {
payload.put("webAppId", webAppId);
} catch (JSONException e) {
e.printStackTrace();
}
ResponseListener<Object> responseListener = new ResponseListener<Object>() {
@Override
public void onSuccess(final Object response) {
JSONObject obj = (JSONObject) response;
if (obj.has("pairingType")) {
notifyPairingRequired();
} else if (listener != null) {
listener.onSuccess(response);
}
}
@Override
public void onError(ServiceCommandError error) {
Util.postError(listener, error);
}
};
ServiceCommand<ResponseListener<Object>> request = new URLServiceSubscription<ResponseListener<Object>>(this, uri, payload, true, responseListener);
request.send();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVServiceSocketClient method handleConnectionLost.
@SuppressWarnings("unchecked")
private void handleConnectionLost(boolean cleanDisconnect, Exception ex) {
ServiceCommandError error = null;
if (ex != null || !cleanDisconnect)
error = new ServiceCommandError(0, "conneciton error", ex);
if (mListener != null)
mListener.onCloseWithError(error);
for (int i = 0; i < requests.size(); i++) {
ServiceCommand<ResponseListener<Object>> request = (ServiceCommand<ResponseListener<Object>>) requests.get(requests.keyAt(i));
if (request != null)
Util.postError(request.getResponseListener(), new ServiceCommandError(0, "connection lost", null));
}
requests.clear();
}
use of com.connectsdk.service.command.ServiceCommandError in project butter-android by butterproject.
the class WebOSTVServiceSocketClient method handleMessage.
@SuppressWarnings("unchecked")
protected void handleMessage(JSONObject message) {
Boolean shouldProcess = true;
if (mListener != null)
shouldProcess = mListener.onReceiveMessage(message);
if (!shouldProcess)
return;
String type = message.optString("type");
Object payload = message.opt("payload");
String strId = message.optString("id");
Integer id = null;
ServiceCommand<ResponseListener<Object>> request = null;
if (isInteger(strId)) {
id = Integer.valueOf(strId);
try {
request = (ServiceCommand<ResponseListener<Object>>) requests.get(id);
} catch (ClassCastException ex) {
// since request is assigned to null, don't need to do anything here
}
}
if (type.length() == 0)
return;
if ("response".equals(type)) {
if (request != null) {
// Log.d(Util.T, "Found requests need to handle response");
if (payload != null) {
Util.postSuccess(request.getResponseListener(), payload);
} else {
Util.postError(request.getResponseListener(), new ServiceCommandError(-1, "JSON parse error", null));
}
if (!(request instanceof URLServiceSubscription)) {
if (!(payload instanceof JSONObject && ((JSONObject) payload).has("pairingType")))
requests.remove(id);
}
} else {
System.err.println("no matching request id: " + strId + ", payload: " + payload.toString());
}
} else if ("registered".equals(type)) {
if (!(mService.getServiceConfig() instanceof WebOSTVServiceConfig)) {
mService.setServiceConfig(new WebOSTVServiceConfig(mService.getServiceConfig().getServiceUUID()));
}
if (payload instanceof JSONObject) {
String clientKey = ((JSONObject) payload).optString("client-key");
((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(clientKey);
// Track SSL certificate
// Not the prettiest way to get it, but we don't have direct access to the SSLEngine
((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate(customTrustManager.getLastCheckedCertificate());
handleRegistered();
if (id != null)
requests.remove(id);
}
} else if ("error".equals(type)) {
String error = message.optString("error");
if (error.length() == 0)
return;
int errorCode = -1;
String errorDesc = null;
try {
String[] parts = error.split(" ", 2);
errorCode = Integer.parseInt(parts[0]);
errorDesc = parts[1];
} catch (Exception e) {
e.printStackTrace();
}
if (payload != null) {
Log.d(Util.T, "Error Payload: " + payload.toString());
}
if (message.has("id")) {
Log.d(Util.T, "Error Desc: " + errorDesc);
if (request != null) {
Util.postError(request.getResponseListener(), new ServiceCommandError(errorCode, errorDesc, payload));
if (!(request instanceof URLServiceSubscription))
requests.remove(id);
}
}
} else if ("hello".equals(type)) {
JSONObject jsonObj = (JSONObject) payload;
if (mService.getServiceConfig().getServiceUUID() != null) {
if (!mService.getServiceConfig().getServiceUUID().equals(jsonObj.optString("deviceUUID"))) {
((WebOSTVServiceConfig) mService.getServiceConfig()).setClientKey(null);
((WebOSTVServiceConfig) mService.getServiceConfig()).setServerCertificate((String) null);
mService.getServiceConfig().setServiceUUID(null);
mService.getServiceDescription().setIpAddress(null);
mService.getServiceDescription().setUUID(null);
disconnect();
}
} else {
String uuid = jsonObj.optString("deviceUUID");
mService.getServiceConfig().setServiceUUID(uuid);
mService.getServiceDescription().setUUID(uuid);
}
state = State.REGISTERING;
sendRegister();
}
}
Aggregations