diff --git a/app/src/main/java/com/polar/nextcloudservices/API/NextcloudHttpAPI.java b/app/src/main/java/com/polar/nextcloudservices/API/NextcloudHttpAPI.java index 0bf3a98..54aff56 100644 --- a/app/src/main/java/com/polar/nextcloudservices/API/NextcloudHttpAPI.java +++ b/app/src/main/java/com/polar/nextcloudservices/API/NextcloudHttpAPI.java @@ -23,6 +23,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.util.Objects; import javax.net.ssl.HttpsURLConnection; @@ -32,6 +33,7 @@ public class NextcloudHttpAPI implements NextcloudAbstractAPI { private String mStatusString = "Updating settings"; private boolean lastPollSuccessful = false; private final ServiceSettings mServiceSettings; + private String mETag = ""; public NextcloudHttpAPI(ServiceSettings settings){ mServiceSettings = settings; @@ -179,7 +181,17 @@ public void sendAction(String link, @Override public boolean checkNewNotifications() throws Exception { - //TODO: implement checking that new notifications available via HEAD request + HttpURLConnection connection = request( + "/ocs/v2.php/apps/notifications/api/v2/notifications", + "HEAD", false); + connection.setConnectTimeout(5000); + connection.setDoInput(true); + String lastETag = connection.getHeaderField("ETag"); + if(!Objects.equals(lastETag, mETag)){ + Log.d(TAG, "Detected new notifications"); + mETag = lastETag; + return true; + } return false; } diff --git a/app/src/main/java/com/polar/nextcloudservices/API/NextcloudSSOAPI.java b/app/src/main/java/com/polar/nextcloudservices/API/NextcloudSSOAPI.java index 476e7d3..e45c099 100644 --- a/app/src/main/java/com/polar/nextcloudservices/API/NextcloudSSOAPI.java +++ b/app/src/main/java/com/polar/nextcloudservices/API/NextcloudSSOAPI.java @@ -14,6 +14,7 @@ import com.nextcloud.android.sso.QueryParam; import com.nextcloud.android.sso.aidl.NextcloudRequest; import com.nextcloud.android.sso.api.NextcloudAPI; +import com.nextcloud.android.sso.api.Response; import com.nextcloud.android.sso.model.SingleSignOnAccount; import com.polar.nextcloudservices.Services.PollUpdateListener; import com.polar.nextcloudservices.Services.Status.Status; @@ -29,19 +30,20 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; +import java.util.Objects; public class NextcloudSSOAPI implements NextcloudAbstractAPI { final private NextcloudAPI API; final private static String TAG = "NextcloudSSOAPI"; private boolean lastPollSuccessful = false; private String mStatusString = "Updating settings"; + private String mETag = ""; public NextcloudSSOAPI(Context context, SingleSignOnAccount ssoAccount) { - // ignore this one… see 5) NextcloudAPI.ApiConnectedListener apiCallback = new NextcloudAPI.ApiConnectedListener() { @Override public void onConnected() { - // ignore this one… see 5) + /*stub*/ } @Override @@ -168,7 +170,16 @@ public void sendAction(String link, String method) throws Exception { @Override public boolean checkNewNotifications() throws Exception { - //TODO: implement checking that new notifications available via HEAD request + NextcloudRequest request = new NextcloudRequest.Builder().setMethod("HEAD") + .setUrl(Uri.encode("/ocs/v2.php/apps/notifications/api/v2/notifications", "/")) + .build(); + Response response = API.performNetworkRequestV2(request); + String lastEtag = Objects.requireNonNull(response.getPlainHeader("ETag")).toString(); + if(!lastEtag.equals(mETag)){ + Log.d(TAG, "New notifications found"); + mETag = lastEtag; + return true; + } return false; }