Skip to content

Commit

Permalink
Implemented HEAD-requests for checking for notifications(#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xf104a committed Oct 26, 2023
1 parent 06f7576 commit 4e72dd7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 4e72dd7

Please sign in to comment.