From bc1436563e583e3664bfa99210087d7a8a156daf Mon Sep 17 00:00:00 2001 From: Aswin Ly Date: Tue, 21 Jun 2016 10:27:02 +0200 Subject: [PATCH 1/2] Fix start app from DIAL appInfo.getId() should be used instead of appInfo.getName() --- src/com/connectsdk/service/DIALService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/com/connectsdk/service/DIALService.java b/src/com/connectsdk/service/DIALService.java index 475bcb93..003bbc28 100644 --- a/src/com/connectsdk/service/DIALService.java +++ b/src/com/connectsdk/service/DIALService.java @@ -141,7 +141,7 @@ public void launchAppWithInfo(AppInfo appInfo, AppLaunchListener listener) { public void launchAppWithInfo(final AppInfo appInfo, Object params, final AppLaunchListener listener) { ServiceCommand> command = new ServiceCommand>(getCommandProcessor(), - requestURL(appInfo.getName()), params, new ResponseListener() { + requestURL(appInfo.getId()), params, new ResponseListener() { @Override public void onError(ServiceCommandError error) { Util.postError(listener, new ServiceCommandError(0, "Problem Launching app", null)); From ace3466235982d1696092fef63ad5fc8a181bf3c Mon Sep 17 00:00:00 2001 From: Aswin Ly Date: Tue, 21 Jun 2016 15:48:29 +0200 Subject: [PATCH 2/2] Fix DIAL functionality for Android --- .../connectsdk/etc/helper/HttpMessage.java | 16 +-- src/com/connectsdk/service/DIALService.java | 112 ++++++++++-------- 2 files changed, 73 insertions(+), 55 deletions(-) diff --git a/src/com/connectsdk/etc/helper/HttpMessage.java b/src/com/connectsdk/etc/helper/HttpMessage.java index b99df57b..3ff73aac 100644 --- a/src/com/connectsdk/etc/helper/HttpMessage.java +++ b/src/com/connectsdk/etc/helper/HttpMessage.java @@ -29,8 +29,10 @@ import org.apache.http.client.methods.HttpPost; public class HttpMessage { + public final static String CONTENT_LENGTH_HEADER = "Content-Length"; public final static String CONTENT_TYPE_HEADER = "Content-Type"; - public final static String CONTENT_TYPE_TEXT_XML = "text/xml; charset=utf-8"; + public final static String CONTENT_TYPE_TEXT_XML = "text/xml; charset=\"utf-8\""; + public final static String CONTENT_TYPE_TEXT_PLAIN = "text/plain; charset=\"utf-8\""; public final static String CONTENT_TYPE_APPLICATION_PLIST = "application/x-apple-binary-plist"; public final static String UDAP_USER_AGENT = "UDAP/2.0"; public final static String LG_ELECTRONICS = "LG Electronics"; @@ -43,7 +45,7 @@ public static HttpPost getHttpPost(String uri) { HttpPost post = null; try { post = new HttpPost(uri); - post.setHeader("Content-Type", CONTENT_TYPE_TEXT_XML); + post.setHeader(CONTENT_TYPE_HEADER, CONTENT_TYPE_TEXT_XML); } catch (IllegalArgumentException e) { e.printStackTrace(); } @@ -53,7 +55,7 @@ public static HttpPost getHttpPost(String uri) { public static HttpPost getUDAPHttpPost(String uri) { HttpPost post = getHttpPost(uri); - post.setHeader("User-Agent", UDAP_USER_AGENT); + post.setHeader(USER_AGENT, UDAP_USER_AGENT); return post; } @@ -76,18 +78,18 @@ public static HttpPost getDLNAHttpPostRenderControl(String uri, String action) { return post; } - public static HttpGet getHttpGet(String url) { + public static HttpGet getHttpGet(String url) { return new HttpGet(url); } - public static HttpGet getUDAPHttpGet(String uri) { + public static HttpGet getUDAPHttpGet(String uri) { HttpGet get = getHttpGet(uri); - get.setHeader("User-Agent", UDAP_USER_AGENT); + get.setHeader(USER_AGENT, UDAP_USER_AGENT); return get; } - public static HttpDelete getHttpDelete(String url) { + public static HttpDelete getHttpDelete(String url) { return new HttpDelete(url); } diff --git a/src/com/connectsdk/service/DIALService.java b/src/com/connectsdk/service/DIALService.java index 003bbc28..9c19ae93 100644 --- a/src/com/connectsdk/service/DIALService.java +++ b/src/com/connectsdk/service/DIALService.java @@ -89,7 +89,7 @@ public static DiscoveryFilter discoveryFilter() { public void setServiceDescription(ServiceDescription serviceDescription) { super.setServiceDescription(serviceDescription); - Map> responseHeaders = this.getServiceDescription().getResponseHeaders(); + Map> responseHeaders = this.getServiceDescription().getResponseHeaders(); if (responseHeaders != null) { String commandPath; @@ -138,28 +138,40 @@ public void launchAppWithInfo(AppInfo appInfo, AppLaunchListener listener) { } @Override - public void launchAppWithInfo(final AppInfo appInfo, Object params, final AppLaunchListener listener) { - ServiceCommand> command = - new ServiceCommand>(getCommandProcessor(), - requestURL(appInfo.getId()), params, new ResponseListener() { + public void launchAppWithInfo(final AppInfo appInfo, final Object params, final AppLaunchListener listener) { + LaunchSession launchSession = new LaunchSession(); + launchSession.setAppId(appInfo.getId()); + getAppState(launchSession, new AppStateListener() { @Override - public void onError(ServiceCommandError error) { - Util.postError(listener, new ServiceCommandError(0, "Problem Launching app", null)); + public void onSuccess(Launcher.AppState object) { + ServiceCommand > command = + new ServiceCommand>(getCommandProcessor(), + requestURL(appInfo.getId()), params, new ResponseListener() { + @Override + public void onError(ServiceCommandError error) { + Util.postError(listener, new ServiceCommandError(0, "Problem Launching app", null)); + } + + @Override + public void onSuccess(Object object) { + LaunchSession launchSession = LaunchSession.launchSessionForAppId(appInfo.getId()); + launchSession.setAppName(appInfo.getName()); + launchSession.setSessionId((String) object); + launchSession.setService(DIALService.this); + launchSession.setSessionType(LaunchSessionType.App); + + Util.postSuccess(listener, launchSession); + } + }); + + command.send(); } @Override - public void onSuccess(Object object) { - LaunchSession launchSession = LaunchSession.launchSessionForAppId(appInfo.getId()); - launchSession.setAppName(appInfo.getName()); - launchSession.setSessionId((String)object); - launchSession.setService(DIALService.this); - launchSession.setSessionType(LaunchSessionType.App); - - Util.postSuccess(listener, launchSession); + public void onError(ServiceCommandError error) { + Util.postError(listener, error); } }); - - command.send(); } @Override @@ -169,11 +181,11 @@ public void launchBrowser(String url, AppLaunchListener listener) { @Override public void closeApp(final LaunchSession launchSession, final ResponseListener listener) { - getAppState(launchSession.getAppName(), new AppStateListener() { + getAppState(launchSession, new AppStateListener() { @Override public void onSuccess(AppState state) { - String uri = requestURL(launchSession.getAppName()); + String uri; if (launchSession.getSessionId().contains("http://") || launchSession.getSessionId().contains("https://")) @@ -255,7 +267,25 @@ public void launchAppStore(String appId, AppLaunchListener listener) { Util.postError(listener, ServiceCommandError.notSupported()); } - private void getAppState(String appName, final AppStateListener listener) { + @Override + public void getAppList(AppListListener listener) { + Util.postError(listener, ServiceCommandError.notSupported()); + } + + @Override + public void getRunningApp(AppInfoListener listener) { + Util.postError(listener, ServiceCommandError.notSupported()); + } + + @Override + public ServiceSubscription subscribeRunningApp(AppInfoListener listener) { + Util.postError(listener, ServiceCommandError.notSupported()); + + return new NotSupportedServiceSubscription(); + } + + @Override + public void getAppState(LaunchSession launchSession, final AppStateListener listener) { ResponseListener responseListener = new ResponseListener() { @Override @@ -277,7 +307,7 @@ public void onSuccess(Object response) { Util.postSuccess(listener, appState); // TODO: This isn't actually reporting anything. -// if (listener != null) +// if (listener != null) // listener.onAppStateSuccess(state); } else { Util.postError(listener, new ServiceCommandError(0, "Malformed response for app state", null)); @@ -290,7 +320,7 @@ public void onError(ServiceCommandError error) { } }; - String uri = requestURL(appName); + String uri = requestURL(launchSession.getAppId()); ServiceCommand> request = new ServiceCommand>(getCommandProcessor(), uri, null, @@ -300,29 +330,6 @@ public void onError(ServiceCommandError error) { request.send(); } - @Override - public void getAppList(AppListListener listener) { - Util.postError(listener, ServiceCommandError.notSupported()); - } - - @Override - public void getRunningApp(AppInfoListener listener) { - Util.postError(listener, ServiceCommandError.notSupported()); - } - - @Override - public ServiceSubscription subscribeRunningApp(AppInfoListener listener) { - Util.postError(listener, ServiceCommandError.notSupported()); - - return new NotSupportedServiceSubscription(); - } - - @Override - public void getAppState(LaunchSession launchSession, AppStateListener listener) { - // TODO Auto-generated method stub - - } - @Override public ServiceSubscription subscribeAppState( LaunchSession launchSession, @@ -402,10 +409,15 @@ public void run() { HttpConnection connection = createHttpConnection(mCommand.getTarget()); if (payload != null || command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_POST)) { connection.setMethod(HttpConnection.Method.POST); + // Always set Content-Type to bypass Android bug: https://code.google.com/p/android/issues/detail?id=205273 + connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, "text/plain; " + + "charset=\"utf-8\""); if (payload != null) { - connection.setHeader(HttpMessage.CONTENT_TYPE_HEADER, "text/plain; " + - "charset=\"utf-8\""); - connection.setPayload(payload.toString()); + String payloadData = payload.toString(); + connection.setHeader(HttpMessage.CONTENT_LENGTH_HEADER, String.valueOf(payloadData.length())); + connection.setPayload(payloadData); + } else { + connection.setHeader(HttpMessage.CONTENT_LENGTH_HEADER, "0"); } } else if (command.getHttpMethod().equalsIgnoreCase(ServiceCommand.TYPE_DEL)) { connection.setMethod(HttpConnection.Method.DELETE); @@ -416,6 +428,10 @@ public void run() { Util.postSuccess(command.getResponseListener(), connection.getResponseString()); } else if (code == 201) { Util.postSuccess(command.getResponseListener(), connection.getResponseHeader("Location")); + } else if (code == 404) { + Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Could not find requested application", null)); + } else if (code == 501) { + Util.postError(command.getResponseListener(), new ServiceCommandError(0, "Was unable to perform the requested action, not supported", null)); } else { Util.postError(command.getResponseListener(), ServiceCommandError.getError(code)); }