-
-
Notifications
You must be signed in to change notification settings - Fork 5
Add HTA-side implementation of sync-interrupted scenario (HT-508). Also replace deprecated AsyncTask. #13
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
base: master
Are you sure you want to change the base?
Changes from all commits
5f52643
fd35b08
3727d83
399e532
678cd7c
00f8cb0
a875cf3
fd78761
f743ece
fc89607
fed5646
08ab9e0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,24 @@ | ||
| package org.sil.hearthis; | ||
| import android.content.Context; | ||
| import android.util.Log; // WM, TEMPORARY! | ||
|
|
||
| import org.apache.http.HttpException; | ||
| import org.apache.http.HttpRequest; | ||
| import org.apache.http.HttpResponse; | ||
| import org.apache.http.entity.StringEntity; | ||
| import org.apache.http.protocol.HttpContext; | ||
| import org.apache.http.protocol.HttpRequestHandler; | ||
| import java.io.IOException; | ||
| import java.net.URI; | ||
| import java.util.ArrayList; | ||
|
|
||
| /** | ||
| * Created by Thomson on 1/18/2016. | ||
| */ | ||
| public class AcceptNotificationHandler implements HttpRequestHandler { | ||
|
|
||
| private static String minHtaVersion = null; | ||
|
|
||
| public interface NotificationListener { | ||
| void onNotification(String message); | ||
| } | ||
|
|
@@ -32,9 +37,55 @@ public void handle(HttpRequest request, HttpResponse response, HttpContext httpC | |
|
|
||
| // Enhance: allow the notification to contain a message, and pass it on. | ||
| // The copy is made because the onNotification calls may well remove listeners, leading to concurrent modification exceptions. | ||
|
|
||
| // HT-508: to prevent HTA from getting stuck in a bad state when sync is interrupted, | ||
| // extract and handle sync status that HT inserted into the notification. HT also sets up | ||
| // that notification by first sending a notification containing the minimum HTA version | ||
| // needed for this exchange. | ||
| // The notifications received from the HearThis PC are HttpRequest (RFC 7230), like this: | ||
| // POST /notify?minHtaVersion=1.0 HTTP/1.1 -- HT sends this first | ||
| // POST /notify?status=sync_success HTTP/1.1 -- HT sends this second | ||
| // Payload is in the portion after the 'notify'. Extract it and send it along. | ||
| // If something goes wrong and that is not possible, send along an error indication. | ||
| // NOTIFICATION ORDER IS IMPORTANT. HT must send the HTA version info first, and then the | ||
| // sync final status. This is enforced by an early return when the HTA version info is seen. | ||
| // | ||
| // NOTE: like several things in HearThisAndroid, HttpRequest is deprecated. It will be | ||
| // replaced with something more appropriate, hopefully soon. When that happens this logic | ||
| // will most likely also change. | ||
|
|
||
| String status = 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("status")) { | ||
| status = pair[1]; | ||
| } else if (pair[0].equals("minHtaVersion")) { | ||
| minHtaVersion = pair[1]; | ||
| response.setEntity(new StringEntity("sync_success")); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| //Log.d("Sync", "handle, results: status = " + status + ", minHtaVersion = " + minHtaVersion); // implement for tech support | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| if (status == null) { | ||
| // We got something but it wasn't "status". Make sure the user sees an error message. | ||
| status = "sync_error"; | ||
|
Comment on lines
+79
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 Backward-incompatible: older HearThis desktop without query params triggers false sync_error When an older HearThis desktop (pre-HT-508) sends Detailed ExplanationThe old With the new code at
This means any user running the updated HearThisAndroid app with an older HearThis desktop will always see a sync error, even on a perfectly successful sync. The fallback for missing query parameters should be Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| for (NotificationListener listener: notificationListeners.toArray(new NotificationListener[notificationListeners.size()])) { | ||
| listener.onNotification(""); | ||
| listener.onNotification(status); | ||
| } | ||
| response.setEntity(new StringEntity("success")); | ||
| response.setEntity(new StringEntity(status)); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.