Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DIAL functionality for Android #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/com/connectsdk/etc/helper/HttpMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
}
Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down
112 changes: 64 additions & 48 deletions src/com/connectsdk/service/DIALService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static DiscoveryFilter discoveryFilter() {
public void setServiceDescription(ServiceDescription serviceDescription) {
super.setServiceDescription(serviceDescription);

Map<String, List<String>> responseHeaders = this.getServiceDescription().getResponseHeaders();
Map<String, List<String>> responseHeaders = this.getServiceDescription().getResponseHeaders();

if (responseHeaders != null) {
String commandPath;
Expand Down Expand Up @@ -138,28 +138,40 @@ public void launchAppWithInfo(AppInfo appInfo, AppLaunchListener listener) {
}

@Override
public void launchAppWithInfo(final AppInfo appInfo, Object params, final AppLaunchListener listener) {
ServiceCommand<ResponseListener<Object>> command =
new ServiceCommand<ResponseListener<Object>>(getCommandProcessor(),
requestURL(appInfo.getName()), params, new ResponseListener<Object>() {
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 <ResponseListener <Object>> command =
new ServiceCommand<ResponseListener<Object>>(getCommandProcessor(),
requestURL(appInfo.getId()), params, new ResponseListener<Object>() {
@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
Expand All @@ -169,11 +181,11 @@ public void launchBrowser(String url, AppLaunchListener listener) {

@Override
public void closeApp(final LaunchSession launchSession, final ResponseListener<Object> 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://"))
Expand Down Expand Up @@ -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<AppInfoListener> subscribeRunningApp(AppInfoListener listener) {
Util.postError(listener, ServiceCommandError.notSupported());

return new NotSupportedServiceSubscription<AppInfoListener>();
}

@Override
public void getAppState(LaunchSession launchSession, final AppStateListener listener) {
ResponseListener<Object> responseListener = new ResponseListener<Object>() {

@Override
Expand All @@ -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));
Expand All @@ -290,7 +320,7 @@ public void onError(ServiceCommandError error) {
}
};

String uri = requestURL(appName);
String uri = requestURL(launchSession.getAppId());

ServiceCommand<ResponseListener<Object>> request =
new ServiceCommand<ResponseListener<Object>>(getCommandProcessor(), uri, null,
Expand All @@ -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<AppInfoListener> subscribeRunningApp(AppInfoListener listener) {
Util.postError(listener, ServiceCommandError.notSupported());

return new NotSupportedServiceSubscription<AppInfoListener>();
}

@Override
public void getAppState(LaunchSession launchSession, AppStateListener listener) {
// TODO Auto-generated method stub

}

@Override
public ServiceSubscription<AppStateListener> subscribeAppState(
LaunchSession launchSession,
Expand Down Expand Up @@ -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);
Expand All @@ -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));
}
Expand Down