Skip to content
25 changes: 21 additions & 4 deletions app/src/main/java/org/sil/hearthis/AcceptNotificationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;

/**
Expand Down Expand Up @@ -42,10 +43,26 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext httpC
// NOTE: like several things in HearThisAndroid, HttpRequest is deprecated. It will be
// replaced with something more appropriate, hopefully soon.

String s1 = request.getRequestLine().toString();
// We want the part between the two space chars, and after the '='
String s2 = s1.substring(s1.indexOf(' ') + 1, s1.lastIndexOf(' '));
String status = s2.substring(s2.indexOf('=') + 1);
String status = null;
try {
String s1 = request.getRequestLine().getUri();
URI uri = new URI(s1);
String query = uri.getQuery();
for (String param : query.split("&")) {
String[] pair = param.split("=");
if (pair.length == 2 && pair[0].equals("message")) {
status = pair[1];
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}

if (status == null) {
// Something went wrong. Make sure the user sees a non-success message.
status = "sync_interrupted";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should display a different message in the case where we get an unexpected status message. I assume the processing should be the same (since we can't possibly know how else to interpret the unexpected message), but if this ever happens, we could give the user some indication that we got an unexpected message and suggest they check for an update. We could even include in the query (from HT) an indication of the minimum version of HTA known to handle the particular status message. Then if we ever get an unexpected status and it includes a version number as well, we could tell the user the minimum version of HTA that is compatible with the version of HT they are using. The HTA code would then look something like this:

String status = null;
String minHtaVersion = null;
try {
    String s1 = request.getRequestLine().getUri();
    URI uri = new URI(s1);
    String query = uri.getQuery();
    if (query != null) {
        for (String param : query.split("&")) {
            String[] pair = param.split("=", 2); // limit=2 in case value contains '='
            if (pair.length == 2) {
                if (pair[0].equals("message")) {
                    status = pair[1];
                } else if (pair[0].equals("minHtaVersion")) {
                    minHtaVersion = pair[1];
                }
            }
        }
    }
} catch (Exception e) {
    e.printStackTrace();
}

if (status == null || isUnexpectedStatus(status)) {
    if (minHtaVersion != null) {
        showError("Unexpected sync status received. This version of HearThis for Android appears to be incompatible with your version of HearThis. Please update HearThis for Android to at least version " + minHtaVersion + ".");
    } else {
        showError("Unexpected sync status received. Please check for updates.");
    }
    status = "sync_interrupted"; // Or possibly a distinct status
}

}

for (NotificationListener listener: notificationListeners.toArray(new NotificationListener[notificationListeners.size()])) {
listener.onNotification(status);
Expand Down